From 7b0c13c1219fbe2d47b540476eaebb1f6358213d Mon Sep 17 00:00:00 2001 From: Remco Bloemen Date: Fri, 22 Jun 2018 07:03:42 +0200 Subject: WIP --- packages/order-utils/src/asset_proxy_utils.ts | 44 +++++++++++++++++---------- 1 file changed, 28 insertions(+), 16 deletions(-) (limited to 'packages/order-utils/src/asset_proxy_utils.ts') diff --git a/packages/order-utils/src/asset_proxy_utils.ts b/packages/order-utils/src/asset_proxy_utils.ts index 915ee5032..fe91a3525 100644 --- a/packages/order-utils/src/asset_proxy_utils.ts +++ b/packages/order-utils/src/asset_proxy_utils.ts @@ -4,7 +4,7 @@ import BN = require('bn.js'); import ethUtil = require('ethereumjs-util'); import * as _ from 'lodash'; -const ERC20_ASSET_DATA_BYTE_LENGTH = 21; +const ERC20_ASSET_DATA_BYTE_LENGTH = 36; const ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH = 53; const ASSET_DATA_ADDRESS_OFFSET = 0; const ERC721_ASSET_DATA_TOKEN_ID_OFFSET = 20; @@ -16,17 +16,26 @@ export const assetProxyUtils = { return ethUtil.toBuffer(assetProxyId); }, decodeAssetProxyId(encodedAssetProxyId: Buffer): AssetProxyId { - return ethUtil.bufferToInt(encodedAssetProxyId); + const string = ethUtil.bufferToHex(encodedAssetProxyId); + if (string === AssetProxyId.ERC20) { + return AssetProxyId.ERC20; + } + if (string === AssetProxyId.ERC721) { + return AssetProxyId.ERC721; + } + throw new Error(`Invalid ProxyId: ${string}`); }, encodeAddress(address: string): Buffer { if (!ethUtil.isValidAddress(address)) { throw new Error(`Invalid Address: ${address}`); } const encodedAddress = ethUtil.toBuffer(address); - return encodedAddress; + const padded = ethUtil.setLengthLeft(encodedAddress, 32); + return padded; }, decodeAddress(encodedAddress: Buffer): string { - const address = ethUtil.bufferToHex(encodedAddress); + const unpadded = ethUtil.setLengthLeft(encodedAddress, 20); + const address = ethUtil.bufferToHex(unpadded); if (!ethUtil.isValidAddress(address)) { throw new Error(`Invalid Address: ${address}`); } @@ -48,7 +57,7 @@ export const assetProxyUtils = { encodeERC20AssetData(tokenAddress: string): string { const encodedAssetProxyId = assetProxyUtils.encodeAssetProxyId(AssetProxyId.ERC20); const encodedAddress = assetProxyUtils.encodeAddress(tokenAddress); - const encodedAssetData = Buffer.concat([encodedAddress, encodedAssetProxyId]); + const encodedAssetData = Buffer.concat([encodedAssetProxyId, encodedAddress]); const encodedAssetDataHex = ethUtil.bufferToHex(encodedAssetData); return encodedAssetDataHex; }, @@ -56,13 +65,12 @@ export const assetProxyUtils = { const encodedAssetData = ethUtil.toBuffer(proxyData); if (encodedAssetData.byteLength !== ERC20_ASSET_DATA_BYTE_LENGTH) { throw new Error( - `Could not decode ERC20 Proxy Data. Expected length of encoded data to be 21. Got ${ + `Could not decode ERC20 Proxy Data. Expected length of encoded data to be ${ERC20_ASSET_DATA_BYTE_LENGTH}. Got ${ encodedAssetData.byteLength }`, ); } - const assetProxyIdOffset = encodedAssetData.byteLength - 1; - const encodedAssetProxyId = encodedAssetData.slice(assetProxyIdOffset); + const encodedAssetProxyId = encodedAssetData.slice(0, 4); const assetProxyId = assetProxyUtils.decodeAssetProxyId(encodedAssetProxyId); if (assetProxyId !== AssetProxyId.ERC20) { throw new Error( @@ -71,7 +79,7 @@ export const assetProxyUtils = { }), but got ${assetProxyId}`, ); } - const encodedTokenAddress = encodedAssetData.slice(ASSET_DATA_ADDRESS_OFFSET, assetProxyIdOffset); + const encodedTokenAddress = encodedAssetData.slice(16, 20); const tokenAddress = assetProxyUtils.decodeAddress(encodedTokenAddress); const erc20AssetData = { assetProxyId, @@ -90,26 +98,30 @@ export const assetProxyUtils = { const encodedReceiverDataLength = assetProxyUtils.encodeUint256(receiverDataLength); encodedAssetData = Buffer.concat([encodedAssetData, encodedReceiverDataLength, encodedReceiverData]); } - encodedAssetData = Buffer.concat([encodedAssetData, encodedAssetProxyId]); + encodedAssetData = Buffer.concat([encodedAssetProxyId, encodedAssetData]); const encodedAssetDataHex = ethUtil.bufferToHex(encodedAssetData); return encodedAssetDataHex; }, decodeERC721AssetData(assetData: string): ERC721AssetData { + const encodedAssetData = ethUtil.toBuffer(assetData); if (encodedAssetData.byteLength < ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH) { throw new Error( - `Could not decode ERC20 Proxy Data. Expected length of encoded data to be at least 53. Got ${ + `Could not decode ERC20 Proxy Data. Expected length of encoded data to be at least ${ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH}. Got ${ encodedAssetData.byteLength }`, ); } + console.log(rawDecode); + //const result = ethAbi.rawDecode(['address', 'uint256', 'bytes'], encodedAssetData.slice(4)); + //console.log(result); + const encodedTokenAddress = encodedAssetData.slice( ASSET_DATA_ADDRESS_OFFSET, ERC721_ASSET_DATA_TOKEN_ID_OFFSET, ); - const proxyIdOffset = encodedAssetData.byteLength - 1; - const encodedAssetProxyId = encodedAssetData.slice(proxyIdOffset); + const encodedAssetProxyId = encodedAssetData.slice(0, 4); const assetProxyId = assetProxyUtils.decodeAssetProxyId(encodedAssetProxyId); if (assetProxyId !== AssetProxyId.ERC721) { throw new Error( @@ -155,14 +167,14 @@ export const assetProxyUtils = { }, decodeAssetDataId(assetData: string): AssetProxyId { const encodedAssetData = ethUtil.toBuffer(assetData); - if (encodedAssetData.byteLength < 1) { + if (encodedAssetData.byteLength < 4) { throw new Error( - `Could not decode Proxy Data. Expected length of encoded data to be at least 1. Got ${ + `Could not decode Proxy Data. Expected length of encoded data to be at least 4. Got ${ encodedAssetData.byteLength }`, ); } - const encodedAssetProxyId = encodedAssetData.slice(-1); + const encodedAssetProxyId = encodedAssetData.slice(0, 4); const assetProxyId = assetProxyUtils.decodeAssetProxyId(encodedAssetProxyId); return assetProxyId; }, -- cgit v1.2.3 From 08d22d2f3c358b61699459f54ac191ee91827da3 Mon Sep 17 00:00:00 2001 From: Remco Bloemen Date: Fri, 22 Jun 2018 09:51:59 +0200 Subject: Update AssetProxyUtils --- packages/order-utils/src/asset_proxy_utils.ts | 131 ++++++++------------------ 1 file changed, 41 insertions(+), 90 deletions(-) (limited to 'packages/order-utils/src/asset_proxy_utils.ts') diff --git a/packages/order-utils/src/asset_proxy_utils.ts b/packages/order-utils/src/asset_proxy_utils.ts index fe91a3525..aad14d687 100644 --- a/packages/order-utils/src/asset_proxy_utils.ts +++ b/packages/order-utils/src/asset_proxy_utils.ts @@ -1,8 +1,7 @@ import { AssetProxyId, ERC20AssetData, ERC721AssetData } from '@0xproject/types'; -import { BigNumber, NULL_BYTES } from '@0xproject/utils'; +import { BigNumber } from '@0xproject/utils'; import BN = require('bn.js'); import ethUtil = require('ethereumjs-util'); -import * as _ from 'lodash'; const ERC20_ASSET_DATA_BYTE_LENGTH = 36; const ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH = 53; @@ -11,6 +10,13 @@ const ERC721_ASSET_DATA_TOKEN_ID_OFFSET = 20; const ERC721_ASSET_DATA_RECEIVER_DATA_LENGTH_OFFSET = 52; const ERC721_ASSET_DATA_RECEIVER_DATA_OFFSET = 84; +// TODO: Push upstream to DefinitelyTyped +interface EthAbi { + simpleEncode(signature: string, ...args: any[]): Buffer; + rawDecode(signature: string[], data: Buffer): any[]; +} +const ethAbi = require('ethereumjs-abi') as EthAbi; + export const assetProxyUtils = { encodeAssetProxyId(assetProxyId: AssetProxyId): Buffer { return ethUtil.toBuffer(assetProxyId); @@ -55,123 +61,68 @@ export const assetProxyUtils = { return value; }, encodeERC20AssetData(tokenAddress: string): string { - const encodedAssetProxyId = assetProxyUtils.encodeAssetProxyId(AssetProxyId.ERC20); - const encodedAddress = assetProxyUtils.encodeAddress(tokenAddress); - const encodedAssetData = Buffer.concat([encodedAssetProxyId, encodedAddress]); - const encodedAssetDataHex = ethUtil.bufferToHex(encodedAssetData); - return encodedAssetDataHex; + return ethUtil.bufferToHex(ethAbi.simpleEncode( + 'ERC20Token(address)', + tokenAddress, + )); }, - decodeERC20AssetData(proxyData: string): ERC20AssetData { - const encodedAssetData = ethUtil.toBuffer(proxyData); - if (encodedAssetData.byteLength !== ERC20_ASSET_DATA_BYTE_LENGTH) { + decodeERC20AssetData(assetData: string): ERC20AssetData { + const data = ethUtil.toBuffer(assetData); + if (data.byteLength < ERC20_ASSET_DATA_BYTE_LENGTH) { throw new Error( - `Could not decode ERC20 Proxy Data. Expected length of encoded data to be ${ERC20_ASSET_DATA_BYTE_LENGTH}. Got ${ - encodedAssetData.byteLength - }`, + `Could not decode ERC20 Proxy Data. Expected length of encoded data to be at least ${ERC20_ASSET_DATA_BYTE_LENGTH}. Got ${data.byteLength}`, ); } - const encodedAssetProxyId = encodedAssetData.slice(0, 4); - const assetProxyId = assetProxyUtils.decodeAssetProxyId(encodedAssetProxyId); + const assetProxyId = ethUtil.bufferToHex(data.slice(0, 4)); if (assetProxyId !== AssetProxyId.ERC20) { throw new Error( - `Could not decode ERC20 Proxy Data. Expected Asset Proxy Id to be ERC20 (${ - AssetProxyId.ERC20 - }), but got ${assetProxyId}`, + `Could not decode ERC20 Proxy Data. Expected Asset Proxy Id to be ERC20 (${AssetProxyId.ERC20}), but got ${assetProxyId}`, ); } - const encodedTokenAddress = encodedAssetData.slice(16, 20); - const tokenAddress = assetProxyUtils.decodeAddress(encodedTokenAddress); - const erc20AssetData = { + const [tokenAddress] = ethAbi.rawDecode(['address'], data.slice(4)); + return { assetProxyId, - tokenAddress, + tokenAddress: ethUtil.addHexPrefix(tokenAddress), }; - return erc20AssetData; }, encodeERC721AssetData(tokenAddress: string, tokenId: BigNumber, receiverData?: string): string { - const encodedAssetProxyId = assetProxyUtils.encodeAssetProxyId(AssetProxyId.ERC721); - const encodedAddress = assetProxyUtils.encodeAddress(tokenAddress); - const encodedTokenId = assetProxyUtils.encodeUint256(tokenId); - let encodedAssetData = Buffer.concat([encodedAddress, encodedTokenId]); - if (!_.isUndefined(receiverData)) { - const encodedReceiverData = ethUtil.toBuffer(receiverData); - const receiverDataLength = new BigNumber(encodedReceiverData.byteLength); - const encodedReceiverDataLength = assetProxyUtils.encodeUint256(receiverDataLength); - encodedAssetData = Buffer.concat([encodedAssetData, encodedReceiverDataLength, encodedReceiverData]); - } - encodedAssetData = Buffer.concat([encodedAssetProxyId, encodedAssetData]); - const encodedAssetDataHex = ethUtil.bufferToHex(encodedAssetData); - return encodedAssetDataHex; + // TODO: Pass `tokendId` as a BigNumber. + return ethUtil.bufferToHex(ethAbi.simpleEncode( + 'ERC721Token(address,uint256,bytes)', + tokenAddress, + tokenId.toString(16), + ethUtil.toBuffer(receiverData || '0x'), + )); }, decodeERC721AssetData(assetData: string): ERC721AssetData { - - const encodedAssetData = ethUtil.toBuffer(assetData); - if (encodedAssetData.byteLength < ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH) { + const data = ethUtil.toBuffer(assetData); + if (data.byteLength < ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH) { throw new Error( - `Could not decode ERC20 Proxy Data. Expected length of encoded data to be at least ${ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH}. Got ${ - encodedAssetData.byteLength - }`, + `Could not decode ERC721 Asset Data. Expected length of encoded data to be at least ${ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH}. Got ${data.byteLength}`, ); } - - console.log(rawDecode); - //const result = ethAbi.rawDecode(['address', 'uint256', 'bytes'], encodedAssetData.slice(4)); - //console.log(result); - - const encodedTokenAddress = encodedAssetData.slice( - ASSET_DATA_ADDRESS_OFFSET, - ERC721_ASSET_DATA_TOKEN_ID_OFFSET, - ); - const encodedAssetProxyId = encodedAssetData.slice(0, 4); - const assetProxyId = assetProxyUtils.decodeAssetProxyId(encodedAssetProxyId); + const assetProxyId = ethUtil.bufferToHex(data.slice(0, 4)); if (assetProxyId !== AssetProxyId.ERC721) { throw new Error( - `Could not decode ERC721 Proxy Data. Expected Asset Proxy Id to be ERC721 (${ - AssetProxyId.ERC721 - }), but got ${assetProxyId}`, + `Could not decode ERC721 Asset Data. Expected Asset Proxy Id to be ERC721 (${AssetProxyId.ERC721}), but got ${assetProxyId}`, ); } - const tokenAddress = assetProxyUtils.decodeAddress(encodedTokenAddress); - const encodedTokenId = encodedAssetData.slice( - ERC721_ASSET_DATA_TOKEN_ID_OFFSET, - ERC721_ASSET_DATA_RECEIVER_DATA_LENGTH_OFFSET, + const [tokenAddress, tokenId, receiverData] = ethAbi.rawDecode( + ['address', 'uint256', 'bytes'], + data.slice(4), ); - const tokenId = assetProxyUtils.decodeUint256(encodedTokenId); - let receiverData = NULL_BYTES; - const lengthUpToReceiverDataLength = ERC721_ASSET_DATA_RECEIVER_DATA_LENGTH_OFFSET + 1; - if (encodedAssetData.byteLength > lengthUpToReceiverDataLength) { - const encodedReceiverDataLength = encodedAssetData.slice( - ERC721_ASSET_DATA_RECEIVER_DATA_LENGTH_OFFSET, - ERC721_ASSET_DATA_RECEIVER_DATA_OFFSET, - ); - const receiverDataLength = assetProxyUtils.decodeUint256(encodedReceiverDataLength); - const lengthUpToReceiverData = ERC721_ASSET_DATA_RECEIVER_DATA_OFFSET + 1; - const expectedReceiverDataLength = new BigNumber(encodedAssetData.byteLength - lengthUpToReceiverData); - if (!receiverDataLength.equals(expectedReceiverDataLength)) { - throw new Error( - `Data length (${receiverDataLength}) does not match actual length of data (${expectedReceiverDataLength})`, - ); - } - const encodedReceiverData = encodedAssetData.slice( - ERC721_ASSET_DATA_RECEIVER_DATA_OFFSET, - receiverDataLength.add(ERC721_ASSET_DATA_RECEIVER_DATA_OFFSET).toNumber(), - ); - receiverData = ethUtil.bufferToHex(encodedReceiverData); - } - const erc721AssetData: ERC721AssetData = { + return { assetProxyId, - tokenAddress, - tokenId, - receiverData, + tokenAddress: ethUtil.addHexPrefix(tokenAddress), + tokenId: new BigNumber(tokenId.toString()), + receiverData: ethUtil.bufferToHex(receiverData), }; - return erc721AssetData; }, decodeAssetDataId(assetData: string): AssetProxyId { const encodedAssetData = ethUtil.toBuffer(assetData); if (encodedAssetData.byteLength < 4) { throw new Error( - `Could not decode Proxy Data. Expected length of encoded data to be at least 4. Got ${ - encodedAssetData.byteLength - }`, + `Could not decode Proxy Data. Expected length of encoded data to be at least 4. Got ${encodedAssetData.byteLength}`, ); } const encodedAssetProxyId = encodedAssetData.slice(0, 4); -- cgit v1.2.3 From 64a6fb2344ede6e81017e2336311688d9fa27c06 Mon Sep 17 00:00:00 2001 From: Remco Bloemen Date: Fri, 22 Jun 2018 16:37:10 +0200 Subject: Fix AssetProxyUtils --- packages/order-utils/src/asset_proxy_utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/order-utils/src/asset_proxy_utils.ts') diff --git a/packages/order-utils/src/asset_proxy_utils.ts b/packages/order-utils/src/asset_proxy_utils.ts index aad14d687..43e23a7fd 100644 --- a/packages/order-utils/src/asset_proxy_utils.ts +++ b/packages/order-utils/src/asset_proxy_utils.ts @@ -90,7 +90,7 @@ export const assetProxyUtils = { return ethUtil.bufferToHex(ethAbi.simpleEncode( 'ERC721Token(address,uint256,bytes)', tokenAddress, - tokenId.toString(16), + '0x' + tokenId.toString(16), ethUtil.toBuffer(receiverData || '0x'), )); }, -- cgit v1.2.3 From a2ff63daa5dce79cd5109a01f645a7075335565d Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Fri, 22 Jun 2018 17:41:38 -0700 Subject: Run prettier and fix linting errors --- packages/order-utils/src/asset_proxy_utils.ts | 79 ++++++++++++++------------- 1 file changed, 42 insertions(+), 37 deletions(-) (limited to 'packages/order-utils/src/asset_proxy_utils.ts') diff --git a/packages/order-utils/src/asset_proxy_utils.ts b/packages/order-utils/src/asset_proxy_utils.ts index 43e23a7fd..8140ad89d 100644 --- a/packages/order-utils/src/asset_proxy_utils.ts +++ b/packages/order-utils/src/asset_proxy_utils.ts @@ -3,18 +3,14 @@ import { BigNumber } from '@0xproject/utils'; import BN = require('bn.js'); import ethUtil = require('ethereumjs-util'); -const ERC20_ASSET_DATA_BYTE_LENGTH = 36; -const ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH = 53; -const ASSET_DATA_ADDRESS_OFFSET = 0; -const ERC721_ASSET_DATA_TOKEN_ID_OFFSET = 20; -const ERC721_ASSET_DATA_RECEIVER_DATA_LENGTH_OFFSET = 52; -const ERC721_ASSET_DATA_RECEIVER_DATA_OFFSET = 84; +import { constants } from './constants'; // TODO: Push upstream to DefinitelyTyped interface EthAbi { simpleEncode(signature: string, ...args: any[]): Buffer; rawDecode(signature: string[], data: Buffer): any[]; } +// tslint:disable:no-var-requires const ethAbi = require('ethereumjs-abi') as EthAbi; export const assetProxyUtils = { @@ -22,25 +18,25 @@ export const assetProxyUtils = { return ethUtil.toBuffer(assetProxyId); }, decodeAssetProxyId(encodedAssetProxyId: Buffer): AssetProxyId { - const string = ethUtil.bufferToHex(encodedAssetProxyId); - if (string === AssetProxyId.ERC20) { + const hexString = ethUtil.bufferToHex(encodedAssetProxyId); + if (hexString === AssetProxyId.ERC20) { return AssetProxyId.ERC20; } - if (string === AssetProxyId.ERC721) { + if (hexString === AssetProxyId.ERC721) { return AssetProxyId.ERC721; } - throw new Error(`Invalid ProxyId: ${string}`); + throw new Error(`Invalid ProxyId: ${hexString}`); }, encodeAddress(address: string): Buffer { if (!ethUtil.isValidAddress(address)) { throw new Error(`Invalid Address: ${address}`); } const encodedAddress = ethUtil.toBuffer(address); - const padded = ethUtil.setLengthLeft(encodedAddress, 32); + const padded = ethUtil.setLengthLeft(encodedAddress, constants.WORD_LENGTH); return padded; }, decodeAddress(encodedAddress: Buffer): string { - const unpadded = ethUtil.setLengthLeft(encodedAddress, 20); + const unpadded = ethUtil.setLengthLeft(encodedAddress, constants.ADDRESS_LENGTH); const address = ethUtil.bufferToHex(unpadded); if (!ethUtil.isValidAddress(address)) { throw new Error(`Invalid Address: ${address}`); @@ -52,34 +48,35 @@ export const assetProxyUtils = { const formattedValue = new BN(value.toString(base)); const encodedValue = ethUtil.toBuffer(formattedValue); // tslint:disable-next-line:custom-no-magic-numbers - const paddedValue = ethUtil.setLengthLeft(encodedValue, 32); + const paddedValue = ethUtil.setLengthLeft(encodedValue, constants.WORD_LENGTH); return paddedValue; }, decodeUint256(encodedValue: Buffer): BigNumber { const formattedValue = ethUtil.bufferToHex(encodedValue); - const value = new BigNumber(formattedValue, 16); + const value = new BigNumber(formattedValue, constants.BASE_16); return value; }, encodeERC20AssetData(tokenAddress: string): string { - return ethUtil.bufferToHex(ethAbi.simpleEncode( - 'ERC20Token(address)', - tokenAddress, - )); + return ethUtil.bufferToHex(ethAbi.simpleEncode('ERC20Token(address)', tokenAddress)); }, decodeERC20AssetData(assetData: string): ERC20AssetData { const data = ethUtil.toBuffer(assetData); - if (data.byteLength < ERC20_ASSET_DATA_BYTE_LENGTH) { + if (data.byteLength < constants.ERC20_ASSET_DATA_BYTE_LENGTH) { throw new Error( - `Could not decode ERC20 Proxy Data. Expected length of encoded data to be at least ${ERC20_ASSET_DATA_BYTE_LENGTH}. Got ${data.byteLength}`, + `Could not decode ERC20 Proxy Data. Expected length of encoded data to be at least ${ + constants.ERC20_ASSET_DATA_BYTE_LENGTH + }. Got ${data.byteLength}`, ); } - const assetProxyId = ethUtil.bufferToHex(data.slice(0, 4)); + const assetProxyId = ethUtil.bufferToHex(data.slice(0, constants.SELECTOR_LENGTH)); if (assetProxyId !== AssetProxyId.ERC20) { throw new Error( - `Could not decode ERC20 Proxy Data. Expected Asset Proxy Id to be ERC20 (${AssetProxyId.ERC20}), but got ${assetProxyId}`, + `Could not decode ERC20 Proxy Data. Expected Asset Proxy Id to be ERC20 (${ + AssetProxyId.ERC20 + }), but got ${assetProxyId}`, ); } - const [tokenAddress] = ethAbi.rawDecode(['address'], data.slice(4)); + const [tokenAddress] = ethAbi.rawDecode(['address'], data.slice(constants.SELECTOR_LENGTH)); return { assetProxyId, tokenAddress: ethUtil.addHexPrefix(tokenAddress), @@ -87,29 +84,35 @@ export const assetProxyUtils = { }, encodeERC721AssetData(tokenAddress: string, tokenId: BigNumber, receiverData?: string): string { // TODO: Pass `tokendId` as a BigNumber. - return ethUtil.bufferToHex(ethAbi.simpleEncode( - 'ERC721Token(address,uint256,bytes)', - tokenAddress, - '0x' + tokenId.toString(16), - ethUtil.toBuffer(receiverData || '0x'), - )); + return ethUtil.bufferToHex( + ethAbi.simpleEncode( + 'ERC721Token(address,uint256,bytes)', + tokenAddress, + `0x${tokenId.toString(constants.BASE_16)}`, + ethUtil.toBuffer(receiverData || '0x'), + ), + ); }, decodeERC721AssetData(assetData: string): ERC721AssetData { const data = ethUtil.toBuffer(assetData); - if (data.byteLength < ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH) { + if (data.byteLength < constants.ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH) { throw new Error( - `Could not decode ERC721 Asset Data. Expected length of encoded data to be at least ${ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH}. Got ${data.byteLength}`, + `Could not decode ERC721 Asset Data. Expected length of encoded data to be at least ${ + constants.ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH + }. Got ${data.byteLength}`, ); } - const assetProxyId = ethUtil.bufferToHex(data.slice(0, 4)); + const assetProxyId = ethUtil.bufferToHex(data.slice(0, constants.SELECTOR_LENGTH)); if (assetProxyId !== AssetProxyId.ERC721) { throw new Error( - `Could not decode ERC721 Asset Data. Expected Asset Proxy Id to be ERC721 (${AssetProxyId.ERC721}), but got ${assetProxyId}`, + `Could not decode ERC721 Asset Data. Expected Asset Proxy Id to be ERC721 (${ + AssetProxyId.ERC721 + }), but got ${assetProxyId}`, ); } const [tokenAddress, tokenId, receiverData] = ethAbi.rawDecode( ['address', 'uint256', 'bytes'], - data.slice(4), + data.slice(constants.SELECTOR_LENGTH), ); return { assetProxyId, @@ -120,12 +123,14 @@ export const assetProxyUtils = { }, decodeAssetDataId(assetData: string): AssetProxyId { const encodedAssetData = ethUtil.toBuffer(assetData); - if (encodedAssetData.byteLength < 4) { + if (encodedAssetData.byteLength < constants.SELECTOR_LENGTH) { throw new Error( - `Could not decode Proxy Data. Expected length of encoded data to be at least 4. Got ${encodedAssetData.byteLength}`, + `Could not decode Proxy Data. Expected length of encoded data to be at least 4. Got ${ + encodedAssetData.byteLength + }`, ); } - const encodedAssetProxyId = encodedAssetData.slice(0, 4); + const encodedAssetProxyId = encodedAssetData.slice(0, constants.SELECTOR_LENGTH); const assetProxyId = assetProxyUtils.decodeAssetProxyId(encodedAssetProxyId); return assetProxyId; }, -- cgit v1.2.3