ethereum-js for encoding/decoding asset proxy data
This commit is contained in:
committed by
Amir Bandeali
parent
e7b0c24d9d
commit
a0dfdefd16
@@ -1,66 +1,57 @@
|
||||
import { BigNumber } from '@0xproject/utils';
|
||||
import BN = require('bn.js');
|
||||
import ethUtil = require('ethereumjs-util');
|
||||
import * as Web3 from 'web3';
|
||||
|
||||
import { AssetProxyId } from './types';
|
||||
|
||||
export function zeroPad(value: string, width: number): string {
|
||||
return '0'.repeat(width - value.length) + value;
|
||||
export function encodeAssetProxyId(assetProxyId: AssetProxyId): Buffer {
|
||||
const formattedAssetProxyId = new BN(assetProxyId);
|
||||
const encodedAssetProxyId = ethUtil.toUnsigned(formattedAssetProxyId);
|
||||
return encodedAssetProxyId;
|
||||
}
|
||||
|
||||
export function encodeAssetProxyId(assetProxyId: AssetProxyId, encodedMetadata: { value: string }) {
|
||||
encodedMetadata.value += zeroPad(new BigNumber(assetProxyId).toString(16), 2);
|
||||
export function encodeAddress(address: string): Buffer {
|
||||
if (!ethUtil.isValidAddress(address)) {
|
||||
throw new Error(`Invalid Address: ${address}`);
|
||||
}
|
||||
const encodedAddress = ethUtil.toBuffer(address);
|
||||
return encodedAddress;
|
||||
}
|
||||
|
||||
export function encodeAddress(address: string, encodedMetadata: { value: string }) {
|
||||
encodedMetadata.value += zeroPad(address.replace('0x', ''), 40);
|
||||
}
|
||||
|
||||
export function encodeUint256(value: BigNumber, encodedMetadata: { value: string }) {
|
||||
encodedMetadata.value += zeroPad(value.toString(16), 64);
|
||||
export function encodeUint256(value: BigNumber): Buffer {
|
||||
const formattedValue = new BN(value.toString(10));
|
||||
const encodedValue = ethUtil.toUnsigned(formattedValue);
|
||||
return encodedValue;
|
||||
}
|
||||
|
||||
export function encodeERC20ProxyMetadata_V1(tokenAddress: string) {
|
||||
// Encode metadata
|
||||
const encodedMetadata = { value: '0x' };
|
||||
encodeAssetProxyId(AssetProxyId.ERC20_V1, encodedMetadata);
|
||||
encodeAddress(tokenAddress, encodedMetadata);
|
||||
const encodedAssetProxyId = encodeAssetProxyId(AssetProxyId.ERC20_V1);
|
||||
const encodedAddress = encodeAddress(tokenAddress);
|
||||
const encodedMetadata = Buffer.concat([encodedAssetProxyId, encodedAddress]);
|
||||
const encodedMetadataHex = ethUtil.bufferToHex(encodedMetadata);
|
||||
|
||||
// Verify encoding length - '0x' plus 21 bytes of encoded data
|
||||
if (encodedMetadata.value.length != 44) {
|
||||
throw Error('Bad encoding length. Expected 44, got ' + encodedMetadata.value.length);
|
||||
}
|
||||
|
||||
// Return encoded metadata
|
||||
return encodedMetadata.value;
|
||||
return encodedMetadataHex;
|
||||
}
|
||||
|
||||
export function encodeERC20ProxyMetadata(tokenAddress: string) {
|
||||
// Encode metadata
|
||||
const encodedMetadata = { value: '0x' };
|
||||
encodeAssetProxyId(AssetProxyId.ERC20, encodedMetadata);
|
||||
encodeAddress(tokenAddress, encodedMetadata);
|
||||
const encodedAssetProxyId = encodeAssetProxyId(AssetProxyId.ERC20);
|
||||
const encodedAddress = encodeAddress(tokenAddress);
|
||||
const encodedMetadata = Buffer.concat([encodedAssetProxyId, encodedAddress]);
|
||||
const encodedMetadataHex = ethUtil.bufferToHex(encodedMetadata);
|
||||
|
||||
// Verify encoding length - '0x' plus 21 bytes of encoded data
|
||||
if (encodedMetadata.value.length != 44) {
|
||||
throw Error('Bad encoding length. Expected 44, got ' + encodedMetadata.value.length);
|
||||
}
|
||||
|
||||
// Return encoded metadata
|
||||
return encodedMetadata.value;
|
||||
return encodedMetadataHex;
|
||||
}
|
||||
|
||||
export function encodeERC721ProxyMetadata(tokenAddress: string, tokenId: BigNumber) {
|
||||
// Encode metadata
|
||||
const encodedMetadata = { value: '0x' };
|
||||
encodeAssetProxyId(AssetProxyId.ERC721, encodedMetadata);
|
||||
encodeAddress(tokenAddress, encodedMetadata);
|
||||
encodeUint256(tokenId, encodedMetadata);
|
||||
const encodedAssetProxyId = encodeAssetProxyId(AssetProxyId.ERC721);
|
||||
const encodedAddress = encodeAddress(tokenAddress);
|
||||
const encodedTokenId = encodeUint256(tokenId);
|
||||
const encodedMetadata = Buffer.concat([encodedAssetProxyId, encodedAddress, encodedTokenId]);
|
||||
const encodedMetadataHex = ethUtil.bufferToHex(encodedMetadata);
|
||||
|
||||
// Verify encoding length - '0x' plus 53 bytes of encoded data
|
||||
if (encodedMetadata.value.length != 108) {
|
||||
throw Error('Bad encoding length. Expected 108, got ' + encodedMetadata.value.length);
|
||||
}
|
||||
|
||||
// Return encoded metadata
|
||||
return encodedMetadata.value;
|
||||
return encodedMetadataHex;
|
||||
}
|
||||
|
Reference in New Issue
Block a user