aboutsummaryrefslogtreecommitdiffstats
path: root/src/ts/0x.js.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/ts/0x.js.ts')
-rw-r--r--src/ts/0x.js.ts57
1 files changed, 56 insertions, 1 deletions
diff --git a/src/ts/0x.js.ts b/src/ts/0x.js.ts
index cc3c43e79..7c6557b5b 100644
--- a/src/ts/0x.js.ts
+++ b/src/ts/0x.js.ts
@@ -1,12 +1,16 @@
import * as BigNumber from 'bignumber.js';
+import * as BN from 'bn.js';
import * as ethUtil from 'ethereumjs-util';
import * as Web3 from 'web3';
+import * as ethABI from 'ethereumjs-abi';
+import * as _ from 'lodash';
+import {constants} from './utils/constants';
import {assert} from './utils/assert';
import {Web3Wrapper} from './web3_wrapper';
import {ExchangeWrapper} from './contract_wrappers/exchange_wrapper';
import contract = require('truffle-contract');
import {ECSignatureSchema} from './schemas/ec_signature_schema';
-import {ECSignature} from './types';
+import {SolidityTypes, ECSignature} from './types';
const MAX_DIGITS_IN_UNSIGNED_256_INT = 78;
@@ -14,6 +18,48 @@ export class ZeroEx {
public web3Wrapper: Web3Wrapper;
public exchange: ExchangeWrapper;
/**
+ * Computes the orderHash given the order parameters and returns it as a hex encoded string.
+ */
+ public static getOrderHashHex(exchangeContractAddr: string, makerAddr: string, takerAddr: string,
+ tokenMAddress: string, tokenTAddress: string, feeRecipient: string,
+ valueM: BigNumber.BigNumber, valueT: BigNumber.BigNumber,
+ makerFee: BigNumber.BigNumber, takerFee: BigNumber.BigNumber,
+ expiration: BigNumber.BigNumber, salt: BigNumber.BigNumber): string {
+ takerAddr = _.isEmpty(takerAddr) ? constants.NULL_ADDRESS : takerAddr ;
+ assert.isETHAddressHex('exchangeContractAddr', exchangeContractAddr);
+ assert.isETHAddressHex('makerAddr', makerAddr);
+ assert.isETHAddressHex('takerAddr', takerAddr);
+ assert.isETHAddressHex('tokenMAddress', tokenMAddress);
+ assert.isETHAddressHex('tokenTAddress', tokenTAddress);
+ assert.isETHAddressHex('feeRecipient', feeRecipient);
+ assert.isBigNumber('valueM', valueM);
+ assert.isBigNumber('valueT', valueT);
+ 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: tokenMAddress, type: SolidityTypes.address},
+ {value: tokenTAddress, type: SolidityTypes.address},
+ {value: feeRecipient, type: SolidityTypes.address},
+ {value: this.bigNumberToBN(valueM), type: SolidityTypes.uint256},
+ {value: this.bigNumberToBN(valueT), type: SolidityTypes.uint256},
+ {value: this.bigNumberToBN(makerFee), type: SolidityTypes.uint256},
+ {value: this.bigNumberToBN(takerFee), type: SolidityTypes.uint256},
+ {value: this.bigNumberToBN(expiration), type: SolidityTypes.uint256},
+ {value: this.bigNumberToBN(salt), type: SolidityTypes.uint256},
+ ];
+ const types = _.map(orderParts, o => o.type);
+ const values = _.map(orderParts, o => o.value);
+ const hashBuff = ethABI.soliditySHA3(types, values);
+ const hashHex = ethUtil.bufferToHex(hashBuff);
+ return hashHex;
+ }
+ /**
* Verifies that the elliptic curve signature `signature` was generated
* by signing `data` with the private key corresponding to the `signerAddressHex` address.
*/
@@ -80,6 +126,15 @@ export class ZeroEx {
const baseUnitAmount = amount.times(unit);
return baseUnitAmount;
}
+ /**
+ * Converts BigNumber instance to BN
+ * The only we convert to BN is to remain compatible with `ethABI. soliditySHA3 ` that
+ * expects values of Solidity type `uint` to be of type `BN`.
+ * We do not use BN anywhere else in the codebase.
+ */
+ private static bigNumberToBN(value: BigNumber.BigNumber) {
+ return new BN(value.toString(), 10);
+ }
constructor(web3: Web3) {
this.web3Wrapper = new Web3Wrapper(web3);
this.exchange = new ExchangeWrapper(this.web3Wrapper);