diff options
Diffstat (limited to 'packages')
-rw-r--r-- | packages/contracts/test/asset_proxy/proxies.ts | 5 | ||||
-rw-r--r-- | packages/contracts/test/libraries/lib_bytes.ts | 9 | ||||
-rw-r--r-- | packages/contracts/test/utils/constants.ts | 2 | ||||
-rw-r--r-- | packages/contracts/test/utils/type_encoding_utils.ts | 21 | ||||
-rw-r--r-- | packages/order-utils/src/asset_data_utils.ts | 36 | ||||
-rw-r--r-- | packages/order-utils/src/constants.ts | 1 |
6 files changed, 43 insertions, 31 deletions
diff --git a/packages/contracts/test/asset_proxy/proxies.ts b/packages/contracts/test/asset_proxy/proxies.ts index 42493bc3a..39674a030 100644 --- a/packages/contracts/test/asset_proxy/proxies.ts +++ b/packages/contracts/test/asset_proxy/proxies.ts @@ -23,6 +23,7 @@ import { constants } from '../utils/constants'; import { ERC20Wrapper } from '../utils/erc20_wrapper'; import { ERC721Wrapper } from '../utils/erc721_wrapper'; import { LogDecoder } from '../utils/log_decoder'; +import { typeEncodingUtils } from '../utils/type_encoding_utils'; import { provider, txDefaults, web3Wrapper } from '../utils/web3_wrapper'; chaiSetup.configure(); @@ -288,7 +289,7 @@ describe('Asset Transfer Proxies', () => { it('should call onERC721Received when transferring to a smart contract with receiver data', async () => { // Construct ERC721 asset data - const receiverData = ethUtil.bufferToHex(assetDataUtils.encodeUint256(generatePseudoRandomSalt())); + const receiverData = ethUtil.bufferToHex(typeEncodingUtils.encodeUint256(generatePseudoRandomSalt())); const encodedAssetData = assetDataUtils.encodeERC721AssetData( erc721Token.address, erc721MakerTokenId, @@ -327,7 +328,7 @@ describe('Asset Transfer Proxies', () => { it('should throw if there is receiver data but contract does not have onERC721Received', async () => { // Construct ERC721 asset data - const receiverData = ethUtil.bufferToHex(assetDataUtils.encodeUint256(generatePseudoRandomSalt())); + const receiverData = ethUtil.bufferToHex(typeEncodingUtils.encodeUint256(generatePseudoRandomSalt())); const encodedAssetData = assetDataUtils.encodeERC721AssetData( erc721Token.address, erc721MakerTokenId, diff --git a/packages/contracts/test/libraries/lib_bytes.ts b/packages/contracts/test/libraries/lib_bytes.ts index 3489564c0..95e05046b 100644 --- a/packages/contracts/test/libraries/lib_bytes.ts +++ b/packages/contracts/test/libraries/lib_bytes.ts @@ -12,6 +12,7 @@ import { artifacts } from '../utils/artifacts'; import { expectContractCallFailed } from '../utils/assertions'; import { chaiSetup } from '../utils/chai_setup'; import { constants } from '../utils/constants'; +import { typeEncodingUtils } from '../utils/type_encoding_utils'; import { provider, txDefaults, web3Wrapper } from '../utils/web3_wrapper'; chaiSetup.configure(); @@ -74,20 +75,20 @@ describe('LibBytes', () => { shortData = '0xffffaa'; const encodedShortData = ethUtil.toBuffer(shortData); const shortDataLength = new BigNumber(encodedShortData.byteLength); - const encodedShortDataLength = assetDataUtils.encodeUint256(shortDataLength); + const encodedShortDataLength = typeEncodingUtils.encodeUint256(shortDataLength); shortTestBytesAsBuffer = Buffer.concat([encodedShortDataLength, encodedShortData]); shortTestBytes = ethUtil.bufferToHex(shortTestBytesAsBuffer); // Create test bytes one word in length - wordOfData = ethUtil.bufferToHex(assetDataUtils.encodeUint256(generatePseudoRandomSalt())); + wordOfData = ethUtil.bufferToHex(typeEncodingUtils.encodeUint256(generatePseudoRandomSalt())); const encodedWordOfData = ethUtil.toBuffer(wordOfData); const wordOfDataLength = new BigNumber(encodedWordOfData.byteLength); - const encodedWordOfDataLength = assetDataUtils.encodeUint256(wordOfDataLength); + const encodedWordOfDataLength = typeEncodingUtils.encodeUint256(wordOfDataLength); wordOfTestBytesAsBuffer = Buffer.concat([encodedWordOfDataLength, encodedWordOfData]); wordOfTestBytes = ethUtil.bufferToHex(wordOfTestBytesAsBuffer); // Create long test bytes (combines short test bytes with word of test bytes) longData = ethUtil.bufferToHex(Buffer.concat([encodedShortData, encodedWordOfData])); const longDataLength = new BigNumber(encodedShortData.byteLength + encodedWordOfData.byteLength); - const encodedLongDataLength = assetDataUtils.encodeUint256(longDataLength); + const encodedLongDataLength = typeEncodingUtils.encodeUint256(longDataLength); longTestBytesAsBuffer = Buffer.concat([encodedLongDataLength, encodedShortData, encodedWordOfData]); longTestBytes = ethUtil.bufferToHex(longTestBytesAsBuffer); }); diff --git a/packages/contracts/test/utils/constants.ts b/packages/contracts/test/utils/constants.ts index e8995f9d6..7dac38f56 100644 --- a/packages/contracts/test/utils/constants.ts +++ b/packages/contracts/test/utils/constants.ts @@ -17,6 +17,7 @@ const TESTRPC_PRIVATE_KEYS_STRINGS = [ ]; export const constants = { + BASE_16: 16, INVALID_OPCODE: 'invalid opcode', TESTRPC_NETWORK_ID: 50, // Note(albrow): In practice V8 and most other engines limit the minimum @@ -47,4 +48,5 @@ export const constants = { makerFee: Web3Wrapper.toBaseUnitAmount(new BigNumber(1), 18), takerFee: Web3Wrapper.toBaseUnitAmount(new BigNumber(1), 18), }, + WORD_LENGTH: 32, }; diff --git a/packages/contracts/test/utils/type_encoding_utils.ts b/packages/contracts/test/utils/type_encoding_utils.ts new file mode 100644 index 000000000..75307b9bd --- /dev/null +++ b/packages/contracts/test/utils/type_encoding_utils.ts @@ -0,0 +1,21 @@ +import { BigNumber } from '@0xproject/utils'; +import BN = require('bn.js'); +import ethUtil = require('ethereumjs-util'); + +import { constants } from './constants'; + +export const typeEncodingUtils = { + encodeUint256(value: BigNumber): Buffer { + const base = 10; + 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, constants.WORD_LENGTH); + return paddedValue; + }, + decodeUint256(encodedValue: Buffer): BigNumber { + const formattedValue = ethUtil.bufferToHex(encodedValue); + const value = new BigNumber(formattedValue, constants.BASE_16); + return value; + }, +}; diff --git a/packages/order-utils/src/asset_data_utils.ts b/packages/order-utils/src/asset_data_utils.ts index 6a4e7600d..ae4da4bb0 100644 --- a/packages/order-utils/src/asset_data_utils.ts +++ b/packages/order-utils/src/asset_data_utils.ts @@ -7,19 +7,6 @@ import ethUtil = require('ethereumjs-util'); import { constants } from './constants'; export const assetDataUtils = { - encodeUint256(value: BigNumber): Buffer { - const base = 10; - 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, constants.WORD_LENGTH); - return paddedValue; - }, - decodeUint256(encodedValue: Buffer): BigNumber { - const formattedValue = ethUtil.bufferToHex(encodedValue); - const value = new BigNumber(formattedValue, constants.BASE_16); - return value; - }, encodeERC20AssetData(tokenAddress: string): string { return ethUtil.bufferToHex(ethAbi.simpleEncode('ERC20Token(address)', tokenAddress)); }, @@ -95,7 +82,7 @@ export const assetDataUtils = { ); } const encodedAssetProxyId = encodedAssetData.slice(0, constants.SELECTOR_LENGTH); - const assetProxyId = assetDataUtils._decodeAssetProxyId(encodedAssetProxyId); + const assetProxyId = decodeAssetProxyId(encodedAssetProxyId); return assetProxyId; }, decodeAssetData(assetData: string): ERC20AssetData | ERC721AssetData { @@ -111,14 +98,15 @@ export const assetDataUtils = { throw new Error(`Unrecognized asset proxy id: ${assetProxyId}`); } }, - _decodeAssetProxyId(encodedAssetProxyId: Buffer): AssetProxyId { - const hexString = ethUtil.bufferToHex(encodedAssetProxyId); - if (hexString === AssetProxyId.ERC20) { - return AssetProxyId.ERC20; - } - if (hexString === AssetProxyId.ERC721) { - return AssetProxyId.ERC721; - } - throw new Error(`Invalid ProxyId: ${hexString}`); - }, }; + +function decodeAssetProxyId(encodedAssetProxyId: Buffer): AssetProxyId { + const hexString = ethUtil.bufferToHex(encodedAssetProxyId); + if (hexString === AssetProxyId.ERC20) { + return AssetProxyId.ERC20; + } + if (hexString === AssetProxyId.ERC721) { + return AssetProxyId.ERC721; + } + throw new Error(`Invalid ProxyId: ${hexString}`); +} diff --git a/packages/order-utils/src/constants.ts b/packages/order-utils/src/constants.ts index 383a657b8..bb7482184 100644 --- a/packages/order-utils/src/constants.ts +++ b/packages/order-utils/src/constants.ts @@ -6,7 +6,6 @@ export const constants = { UNLIMITED_ALLOWANCE_IN_BASE_UNITS: new BigNumber(2).pow(256).minus(1), TESTRPC_NETWORK_ID: 50, ADDRESS_LENGTH: 20, - WORD_LENGTH: 32, ERC20_ASSET_DATA_BYTE_LENGTH: 36, ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH: 53, SELECTOR_LENGTH: 4, |