aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--package.json2
-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
-rw-r--r--test/0x.js.ts37
6 files changed, 107 insertions, 1 deletions
diff --git a/package.json b/package.json
index 557d6eeff..e6281afae 100644
--- a/package.json
+++ b/package.json
@@ -35,7 +35,6 @@
"@types/chai": "^3.5.2",
"@types/mocha": "^2.2.41",
"@types/node": "^7.0.22",
- "@types/lodash": "^4.14.64",
"awesome-typescript-loader": "^3.1.3",
"bignumber.js": "^4.0.2",
"chai": "^3.5.0",
@@ -55,6 +54,7 @@
},
"dependencies": {
"bignumber.js": "^4.0.2",
+ "ethereumjs-abi": "^0.6.4",
"ethereumjs-util": "^5.1.1",
"jsonschema": "^1.1.1",
"lodash": "^4.17.4",
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',
+}
diff --git a/test/0x.js.ts b/test/0x.js.ts
index a913fd6b5..07c58f1a5 100644
--- a/test/0x.js.ts
+++ b/test/0x.js.ts
@@ -3,12 +3,49 @@ import * as chai from 'chai';
import 'mocha';
import * as BigNumber from 'bignumber.js';
import ChaiBigNumber = require('chai-bignumber');
+import {constants} from '../src/ts/utils/constants';
// Use BigNumber chai add-on
chai.use(ChaiBigNumber());
const expect = chai.expect;
describe('ZeroEx library', () => {
+ describe('#getOrderHash', () => {
+ it('defaults takerAddress to NULL address', () => {
+ const orderHash = ZeroEx.getOrderHash(
+ constants.NULL_ADDRESS,
+ constants.NULL_ADDRESS,
+ '',
+ constants.NULL_ADDRESS,
+ constants.NULL_ADDRESS,
+ constants.NULL_ADDRESS,
+ new BigNumber(0),
+ new BigNumber(0),
+ new BigNumber(0),
+ new BigNumber(0),
+ new BigNumber(0),
+ new BigNumber(0),
+ );
+ expect(orderHash).to.be.equal('0x103a5e97dab5dbeb8f385636f86a7d1e458a7ccbe1bd194727f0b2f85ab116c7');
+ });
+ it('calculates the order hash', () => {
+ const orderHash = ZeroEx.getOrderHash(
+ constants.NULL_ADDRESS,
+ constants.NULL_ADDRESS,
+ constants.NULL_ADDRESS,
+ constants.NULL_ADDRESS,
+ constants.NULL_ADDRESS,
+ constants.NULL_ADDRESS,
+ new BigNumber(0),
+ new BigNumber(0),
+ new BigNumber(0),
+ new BigNumber(0),
+ new BigNumber(0),
+ new BigNumber(0),
+ );
+ expect(orderHash).to.be.equal('0x103a5e97dab5dbeb8f385636f86a7d1e458a7ccbe1bd194727f0b2f85ab116c7');
+ });
+ });
describe('#isValidSignature', () => {
// This test data was borrowed from the JSON RPC documentation
// Source: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign