diff options
Diffstat (limited to 'packages')
-rw-r--r-- | packages/contracts/src/utils/asset_proxy_utils.ts | 73 |
1 files changed, 32 insertions, 41 deletions
diff --git a/packages/contracts/src/utils/asset_proxy_utils.ts b/packages/contracts/src/utils/asset_proxy_utils.ts index 7fcf3dd26..835f444f6 100644 --- a/packages/contracts/src/utils/asset_proxy_utils.ts +++ b/packages/contracts/src/utils/asset_proxy_utils.ts @@ -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, encodedMetadata: { value: string }) { - encodedMetadata.value += zeroPad(address.replace('0x', ''), 40); +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 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); - - // 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); - } + const encodedAssetProxyId = encodeAssetProxyId(AssetProxyId.ERC20_V1); + const encodedAddress = encodeAddress(tokenAddress); + const encodedMetadata = Buffer.concat([encodedAssetProxyId, encodedAddress]); + const encodedMetadataHex = ethUtil.bufferToHex(encodedMetadata); - // 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); - - // 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); - } + 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); - // Return encoded metadata - return encodedMetadata.value; + return encodedMetadataHex; } |