diff options
author | Remco Bloemen <remco@wicked.ventures> | 2018-06-22 13:03:42 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2018-06-26 07:19:07 +0800 |
commit | 7b0c13c1219fbe2d47b540476eaebb1f6358213d (patch) | |
tree | 7b81f105b09ed2ea458513c94297486e7efc3319 | |
parent | a98ecc05af5b264f981087005bb3fa041d5c8756 (diff) | |
download | dexon-sol-tools-7b0c13c1219fbe2d47b540476eaebb1f6358213d.tar dexon-sol-tools-7b0c13c1219fbe2d47b540476eaebb1f6358213d.tar.gz dexon-sol-tools-7b0c13c1219fbe2d47b540476eaebb1f6358213d.tar.bz2 dexon-sol-tools-7b0c13c1219fbe2d47b540476eaebb1f6358213d.tar.lz dexon-sol-tools-7b0c13c1219fbe2d47b540476eaebb1f6358213d.tar.xz dexon-sol-tools-7b0c13c1219fbe2d47b540476eaebb1f6358213d.tar.zst dexon-sol-tools-7b0c13c1219fbe2d47b540476eaebb1f6358213d.zip |
WIP
5 files changed, 36 insertions, 23 deletions
diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxy/ERC20Proxy.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/ERC20Proxy.sol index 69e2dc913..002e8b662 100644 --- a/packages/contracts/src/contracts/current/protocol/AssetProxy/ERC20Proxy.sol +++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/ERC20Proxy.sol @@ -30,7 +30,7 @@ contract ERC20Proxy is MixinERC20Transfer { // Id of this proxy. - bytes4 constant PROXY_ID = 1; + bytes4 constant PROXY_ID = 0xf47261b0; /// @dev Gets the proxy id associated with the proxy address. /// @return Proxy id. diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxy/ERC721Proxy.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/ERC721Proxy.sol index 7a1b13d80..f6bdf2a5f 100644 --- a/packages/contracts/src/contracts/current/protocol/AssetProxy/ERC721Proxy.sol +++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/ERC721Proxy.sol @@ -30,7 +30,7 @@ contract ERC721Proxy is MixinERC721Transfer { // Id of this proxy. - bytes4 constant PROXY_ID = 2; + bytes4 constant PROXY_ID = 0xf47261b0; /// @dev Gets the proxy id associated with the proxy address. /// @return Proxy id. diff --git a/packages/contracts/test/asset_proxy/decoder.ts b/packages/contracts/test/asset_proxy/decoder.ts index 98d18aa38..f671f495e 100644 --- a/packages/contracts/test/asset_proxy/decoder.ts +++ b/packages/contracts/test/asset_proxy/decoder.ts @@ -44,6 +44,7 @@ describe('TestAssetDataDecoders', () => { it('should correctly decode ERC721 asset data', async () => { const tokenId = generatePseudoRandomSalt(); const encodedAssetData = assetProxyUtils.encodeERC721AssetData(testAddress, tokenId); + console.log(encodedAssetData); const encodedAssetDataWithoutProxyId = encodedAssetData.slice(0, -2); const expectedDecodedAssetData = assetProxyUtils.decodeERC721AssetData(encodedAssetData); let decodedTokenAddress: string; 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; }, diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index e5a0f41a9..88abdc1e7 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -152,18 +152,18 @@ export interface ECSignature { } export enum AssetProxyId { - INVALID, - ERC20, - ERC721, + INVALID = '0x00000000', + ERC20 = '0xf47261b0', + ERC721 = '0x08e937fa', } export interface ERC20AssetData { - assetProxyId: AssetProxyId; + assetProxyId: string; tokenAddress: string; } export interface ERC721AssetData { - assetProxyId: AssetProxyId; + assetProxyId: string; tokenAddress: string; tokenId: BigNumber; receiverData: string; |