aboutsummaryrefslogtreecommitdiffstats
path: root/packages
diff options
context:
space:
mode:
authorGreg Hysen <greg.hysen@gmail.com>2018-04-12 03:21:14 +0800
committerAmir Bandeali <abandeali1@gmail.com>2018-04-21 04:56:17 +0800
commita0dfdefd165ce8b8eb0e76707391f5efb8485040 (patch)
tree2713783b77fa21dde0d365f8c36e228b5e2af95a /packages
parente7b0c24d9d905b4544caaf274beca97f5c7967b0 (diff)
downloaddexon-0x-contracts-a0dfdefd165ce8b8eb0e76707391f5efb8485040.tar
dexon-0x-contracts-a0dfdefd165ce8b8eb0e76707391f5efb8485040.tar.gz
dexon-0x-contracts-a0dfdefd165ce8b8eb0e76707391f5efb8485040.tar.bz2
dexon-0x-contracts-a0dfdefd165ce8b8eb0e76707391f5efb8485040.tar.lz
dexon-0x-contracts-a0dfdefd165ce8b8eb0e76707391f5efb8485040.tar.xz
dexon-0x-contracts-a0dfdefd165ce8b8eb0e76707391f5efb8485040.tar.zst
dexon-0x-contracts-a0dfdefd165ce8b8eb0e76707391f5efb8485040.zip
ethereum-js for encoding/decoding asset proxy data
Diffstat (limited to 'packages')
-rw-r--r--packages/contracts/src/utils/asset_proxy_utils.ts73
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;
}