From 12e2bfc794831c0008bdbfc27dad9509f2578057 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Mon, 4 Jun 2018 18:14:44 -0700 Subject: Fixes after rebasing --- packages/order-utils/src/asset_proxy_utils.ts | 78 ++++++++++++++------------- 1 file changed, 42 insertions(+), 36 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 a12be83a3..61a9b12e9 100644 --- a/packages/order-utils/src/asset_proxy_utils.ts +++ b/packages/order-utils/src/asset_proxy_utils.ts @@ -1,11 +1,11 @@ -import { AssetProxyId, ERC20ProxyData, ERC721ProxyData, ProxyData } from '@0xproject/types'; +import { AssetData, AssetProxyId, ERC20AssetData, ERC721AssetData } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import BN = require('bn.js'); import ethUtil = require('ethereumjs-util'); import * as _ from 'lodash'; -const ERC20_PROXY_METADATA_BYTE_LENGTH = 21; -const ERC721_PROXY_METADATA_BYTE_LENGTH = 53; +const ERC20_ASSET_DATA_BYTE_LENGTH = 21; +const ERC721_ASSET_DATA_BYTE_LENGTH = 53; export const assetProxyUtils = { encodeAssetProxyId(assetProxyId: AssetProxyId): Buffer { @@ -44,13 +44,13 @@ export const assetProxyUtils = { encodeERC20AssetData(tokenAddress: string): string { const encodedAssetProxyId = assetProxyUtils.encodeAssetProxyId(AssetProxyId.ERC20); const encodedAddress = assetProxyUtils.encodeAddress(tokenAddress); - const encodedMetadata = Buffer.concat([encodedAddress, encodedAssetProxyId]); - const encodedMetadataHex = ethUtil.bufferToHex(encodedMetadata); - return encodedMetadataHex; + const encodedAssetData = Buffer.concat([encodedAddress, encodedAssetProxyId]); + const encodedAssetDataHex = ethUtil.bufferToHex(encodedAssetData); + return encodedAssetDataHex; }, - decodeERC20ProxyData(proxyData: string): ERC20ProxyData { - const encodedProxyMetadata = ethUtil.toBuffer(proxyData); - if (encodedProxyMetadata.byteLength !== ERC20_PROXY_METADATA_BYTE_LENGTH) { + decodeERC20AssetData(proxyData: string): ERC20AssetData { + 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 ${ encodedAssetData.byteLength @@ -66,8 +66,8 @@ export const assetProxyUtils = { }), but got ${assetProxyId}`, ); } - const addressOffset = ERC20_PROXY_METADATA_BYTE_LENGTH - 1; - const encodedTokenAddress = encodedProxyMetadata.slice(0, addressOffset); + const addressOffset = ERC20_ASSET_DATA_BYTE_LENGTH - 1; + const encodedTokenAddress = encodedAssetData.slice(0, addressOffset); const tokenAddress = assetProxyUtils.decodeAddress(encodedTokenAddress); const erc20AssetData = { assetProxyId, @@ -75,30 +75,36 @@ export const assetProxyUtils = { }; return erc20AssetData; }, - encodeERC721AssetData(tokenAddress: string, tokenId: BigNumber, data?: string): string { + 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 encodedMetadata = Buffer.concat([encodedAddress, encodedTokenId]); - if (!_.isUndefined(data)) { - const encodedData = ethUtil.toBuffer(data); - const dataLength = new BigNumber(encodedData.byteLength); - const encodedDataLength = assetProxyUtils.encodeUint256(dataLength); - encodedMetadata = Buffer.concat([encodedMetadata, encodedDataLength, encodedData]); + 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]); } - encodedMetadata = Buffer.concat([encodedMetadata, encodedAssetProxyId]); - const encodedMetadataHex = ethUtil.bufferToHex(encodedMetadata); - return encodedMetadataHex; + encodedAssetData = Buffer.concat([encodedAssetData, encodedAssetProxyId]); + const encodedAssetDataHex = ethUtil.bufferToHex(encodedAssetData); + return encodedAssetDataHex; }, decodeERC721AssetData(assetData: string): ERC721AssetData { const encodedAssetData = ethUtil.toBuffer(assetData); - if (encodedAssetData.byteLength < ERC721_PROXY_METADATA_BYTE_LENGTH) { + if (encodedAssetData.byteLength < ERC721_ASSET_DATA_BYTE_LENGTH) { throw new Error( `Could not decode ERC20 Proxy Data. Expected length of encoded data to be at least 53. Got ${ encodedAssetData.byteLength }`, ); } + const addressOffset = 0; + const tokenIdOffset = 20; + const receiverDataLengthOffset = 52; + const receiverDataOffset = 84; + const encodedTokenAddress = encodedAssetData.slice(addressOffset, tokenIdOffset); + const proxyIdOffset = encodedAssetData.byteLength - 1; const encodedAssetProxyId = encodedAssetData.slice(-1); const assetProxyId = assetProxyUtils.decodeAssetProxyId(encodedAssetProxyId); if (assetProxyId !== AssetProxyId.ERC721) { @@ -108,31 +114,31 @@ export const assetProxyUtils = { }), but got ${assetProxyId}`, ); } - const addressOffset = ERC20_PROXY_METADATA_BYTE_LENGTH - 1; - const encodedTokenAddress = encodedProxyMetadata.slice(0, addressOffset); const tokenAddress = assetProxyUtils.decodeAddress(encodedTokenAddress); - const tokenIdOffset = ERC721_PROXY_METADATA_BYTE_LENGTH - 1; - const encodedTokenId = encodedProxyMetadata.slice(addressOffset, tokenIdOffset); + const encodedTokenId = encodedAssetData.slice(tokenIdOffset, receiverDataLengthOffset); const tokenId = assetProxyUtils.decodeUint256(encodedTokenId); const nullData = '0x'; - let data = nullData; - if (encodedAssetData.byteLength > 53) { - const encodedDataLength = encodedAssetData.slice(52, 84); - const dataLength = assetProxyUtils.decodeUint256(encodedDataLength); - const expectedDataLength = new BigNumber(encodedAssetData.byteLength - 85); - if (!dataLength.equals(expectedDataLength)) { + let receiverData = nullData; + if (encodedAssetData.byteLength > receiverDataLengthOffset + 1) { + const encodedReceiverDataLength = encodedAssetData.slice(receiverDataLengthOffset, receiverDataOffset); + const receiverDataLength = assetProxyUtils.decodeUint256(encodedReceiverDataLength); + const expectedReceiverDataLength = new BigNumber(encodedAssetData.byteLength - (receiverDataOffset + 1)); + if (!receiverDataLength.equals(expectedReceiverDataLength)) { throw new Error( - `Data length (${dataLength}) does not match actual length of data (${expectedDataLength})`, + `Data length (${receiverDataLength}) does not match actual length of data (${expectedReceiverDataLength})`, ); } - const encodedData = encodedAssetData.slice(84, expectedDataLength.toNumber() + 84); - data = ethUtil.bufferToHex(encodedData); + const encodedReceiverData = encodedAssetData.slice( + receiverDataOffset, + receiverDataOffset + receiverDataLength.toNumber(), + ); + receiverData = ethUtil.bufferToHex(encodedReceiverData); } const erc721AssetData: ERC721AssetData = { assetProxyId, tokenAddress, tokenId, - data, + receiverData, }; return erc721AssetData; }, -- cgit