aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2017-05-25 23:24:29 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2017-05-25 23:24:29 +0800
commitf2f39d9eb2822b71aa7c5cf6f3b2ef006a83ad03 (patch)
treec2a474aebcbdc93834afc1374908fbf4677d2381 /src
parentf7b8378a6eb4b4c6c3461ff677723869c67a4753 (diff)
downloaddexon-sol-tools-f2f39d9eb2822b71aa7c5cf6f3b2ef006a83ad03.tar
dexon-sol-tools-f2f39d9eb2822b71aa7c5cf6f3b2ef006a83ad03.tar.gz
dexon-sol-tools-f2f39d9eb2822b71aa7c5cf6f3b2ef006a83ad03.tar.bz2
dexon-sol-tools-f2f39d9eb2822b71aa7c5cf6f3b2ef006a83ad03.tar.lz
dexon-sol-tools-f2f39d9eb2822b71aa7c5cf6f3b2ef006a83ad03.tar.xz
dexon-sol-tools-f2f39d9eb2822b71aa7c5cf6f3b2ef006a83ad03.tar.zst
dexon-sol-tools-f2f39d9eb2822b71aa7c5cf6f3b2ef006a83ad03.zip
Port getOrderHash
Diffstat (limited to 'src')
-rw-r--r--src/ts/0x.js.ts40
-rw-r--r--src/ts/globals.d.ts5
-rw-r--r--src/ts/types.ts21
-rw-r--r--src/ts/utils/constants.ts3
4 files changed, 69 insertions, 0 deletions
diff --git a/src/ts/0x.js.ts b/src/ts/0x.js.ts
index ead1f56df..82dbce22d 100644
--- a/src/ts/0x.js.ts
+++ b/src/ts/0x.js.ts
@@ -1,8 +1,12 @@
import * as BigNumber from 'bignumber.js';
+import * as BN from 'bn.js';
import * as ethUtil from 'ethereumjs-util';
+import * as ethABI from 'ethereumjs-abi';
import * as _ from 'lodash';
+import {constants} from './utils/constants';
import {assert} from './utils/assert';
import {ECSignatureSchema} from './schemas/ec_signature_schema';
+import {SolidityTypes} from './types';
/**
* Elliptic Curve signature
@@ -16,6 +20,42 @@ export interface ECSignature {
const MAX_DIGITS_IN_UNSIGNED_256_INT = 78;
export class ZeroEx {
+ public static getOrderHash(exchangeContractAddr: string, makerAddr: string, takerAddr: string,
+ depositTokenAddr: string, receiveTokenAddr: string, feeRecipient: string,
+ depositAmt: BigNumber.BigNumber, receiveAmt: BigNumber.BigNumber,
+ makerFee: BigNumber.BigNumber, takerFee: BigNumber.BigNumber,
+ expiration: BigNumber.BigNumber, salt: BigNumber.BigNumber): string {
+ takerAddr = takerAddr !== '' ? takerAddr : constants.NULL_ADDRESS;
+ assert.isETHAddressHex('exchangeContractAddr', exchangeContractAddr);
+ assert.isETHAddressHex('makerAddr', makerAddr);
+ assert.isETHAddressHex('takerAddr', takerAddr);
+ assert.isETHAddressHex('depositTokenAddr', depositTokenAddr);
+ assert.isETHAddressHex('receiveTokenAddr', receiveTokenAddr);
+ assert.isETHAddressHex('feeRecipient', feeRecipient);
+ assert.isBigNumber('depositAmt', depositAmt);
+ assert.isBigNumber('receiveAmt', receiveAmt);
+ assert.isBigNumber('makerFee', makerFee);
+ assert.isBigNumber('takerFee', takerFee);
+ assert.isBigNumber('expiration', expiration);
+ assert.isBigNumber('salt', salt);
+ const orderParts = [
+ {value: exchangeContractAddr, type: SolidityTypes.address},
+ {value: makerAddr, type: SolidityTypes.address},
+ {value: takerAddr, type: SolidityTypes.address},
+ {value: depositTokenAddr, type: SolidityTypes.address},
+ {value: receiveTokenAddr, type: SolidityTypes.address},
+ {value: feeRecipient, type: SolidityTypes.address},
+ {value: new BN(depositAmt.toString(), 10), type: SolidityTypes.uint256},
+ {value: new BN(receiveAmt.toString(), 10), type: SolidityTypes.uint256},
+ {value: new BN(makerFee.toString(), 10), type: SolidityTypes.uint256},
+ {value: new BN(takerFee.toString(), 10), type: SolidityTypes.uint256},
+ {value: new BN(expiration.toString(), 10), type: SolidityTypes.uint256},
+ {value: new BN(salt.toString(), 10), type: SolidityTypes.uint256},
+ ];
+ const hashBuff = ethABI.soliditySHA3(_.map(orderParts, 'type'), _.map(orderParts, 'value'));
+ const buffHashHex = ethUtil.bufferToHex(hashBuff);
+ return buffHashHex;
+ }
/**
* Verifies that the elliptic curve signature `signature` was generated
* by signing `data` with the private key corresponding to the `signerAddressHex` address.
diff --git a/src/ts/globals.d.ts b/src/ts/globals.d.ts
index 796812c87..a50c635fb 100644
--- a/src/ts/globals.d.ts
+++ b/src/ts/globals.d.ts
@@ -1,4 +1,5 @@
declare module 'chai-bignumber';
+declare module 'bn.js';
declare interface Schema {
id: string;
@@ -23,3 +24,7 @@ declare module 'ethereumjs-util' {
const pubToAddress: (pubKey: string) => Buffer;
const isValidAddress: (address: string) => boolean;
}
+
+declare module 'ethereumjs-abi' {
+ const soliditySHA3: (argTypes: string[], args: any) => Buffer;
+}
diff --git a/src/ts/types.ts b/src/ts/types.ts
new file mode 100644
index 000000000..c3da8d81e
--- /dev/null
+++ b/src/ts/types.ts
@@ -0,0 +1,21 @@
+import * as _ from 'lodash';
+import * as BigNumber from 'bignumber.js';
+
+// Utility function to create a K:V from a list of strings
+// Adapted from: https://basarat.gitbooks.io/typescript/content/docs/types/literal-types.html
+function strEnum(values: string[]): {[key: string]: string} {
+ return _.reduce(values, (result, key) => {
+ result[key] = key;
+ return result;
+ }, Object.create(null));
+}
+
+export const SolidityTypes = strEnum([
+ 'address',
+ 'uint256',
+ 'uint8',
+ 'string',
+ 'bool',
+]);
+
+export type SolidityTypes = keyof typeof SolidityTypes;
diff --git a/src/ts/utils/constants.ts b/src/ts/utils/constants.ts
new file mode 100644
index 000000000..60af7b674
--- /dev/null
+++ b/src/ts/utils/constants.ts
@@ -0,0 +1,3 @@
+export const constants = {
+ NULL_ADDRESS: '0x0000000000000000000000000000000000000000',
+}