diff options
Diffstat (limited to 'packages/contracts/src/utils/signed_order_utils.ts')
-rw-r--r-- | packages/contracts/src/utils/signed_order_utils.ts | 82 |
1 files changed, 61 insertions, 21 deletions
diff --git a/packages/contracts/src/utils/signed_order_utils.ts b/packages/contracts/src/utils/signed_order_utils.ts index 6122748b4..1d9096ee4 100644 --- a/packages/contracts/src/utils/signed_order_utils.ts +++ b/packages/contracts/src/utils/signed_order_utils.ts @@ -1,44 +1,84 @@ -import { SignedOrder } from '0x.js'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import ethUtil = require('ethereumjs-util'); import * as _ from 'lodash'; import { crypto } from './crypto'; +import { OrderStruct, SignatureType, SignedOrder } from './types'; export const signedOrderUtils = { createFill: (signedOrder: SignedOrder, takerTokenFillAmount?: BigNumber) => { const fill = { - ...signedOrderUtils.getOrderAddressesAndValues(signedOrder), + order: signedOrderUtils.getOrderStruct(signedOrder), takerTokenFillAmount: takerTokenFillAmount || signedOrder.takerTokenAmount, - ...signedOrder.ecSignature, + signature: signedOrder.signature, }; return fill; }, createCancel(signedOrder: SignedOrder, takerTokenCancelAmount?: BigNumber) { const cancel = { - ...signedOrderUtils.getOrderAddressesAndValues(signedOrder), + order: signedOrderUtils.getOrderStruct(signedOrder), takerTokenCancelAmount: takerTokenCancelAmount || signedOrder.takerTokenAmount, }; return cancel; }, - getOrderAddressesAndValues(signedOrder: SignedOrder) { - return { - orderAddresses: [ - signedOrder.maker, - signedOrder.taker, - signedOrder.makerTokenAddress, - signedOrder.takerTokenAddress, - signedOrder.feeRecipient, - ], - orderValues: [ - signedOrder.makerTokenAmount, - signedOrder.takerTokenAmount, - signedOrder.makerFee, - signedOrder.takerFee, - signedOrder.expirationUnixTimestampSec, - signedOrder.salt, - ], + getOrderStruct(signedOrder: SignedOrder): OrderStruct { + const orderStruct = { + makerAddress: signedOrder.makerAddress, + takerAddress: signedOrder.takerAddress, + makerTokenAddress: signedOrder.makerTokenAddress, + takerTokenAddress: signedOrder.takerTokenAddress, + feeRecipientAddress: signedOrder.feeRecipientAddress, + makerTokenAmount: signedOrder.makerTokenAmount, + takerTokenAmount: signedOrder.takerTokenAmount, + makerFeeAmount: signedOrder.makerFeeAmount, + takerFeeAmount: signedOrder.takerFeeAmount, + expirationTimeSeconds: signedOrder.expirationTimeSeconds, + salt: signedOrder.salt, }; + return orderStruct; + }, + getOrderHashHex(signedOrder: SignedOrder): string { + const orderSchemaHashBuff = crypto.solSHA3([ + 'address exchangeAddress', + 'address makerAddress', + 'address takerAddress', + 'address makerTokenAddress', + 'address takerTokenAddress', + 'address feeRecipientAddress', + 'uint256 makerTokenAmount', + 'uint256 takerTokenAmount', + 'uint256 makerFeeAmount', + 'uint256 takerFeeAmount', + 'uint256 expirationTimestampSeconds', + 'uint256 salt', + ]); + const orderSchemaHashHex = `0x${orderSchemaHashBuff.toString('hex')}`; + const orderHashBuff = crypto.solSHA3([ + signedOrder.exchangeAddress, + signedOrder.makerAddress, + signedOrder.takerAddress, + signedOrder.makerTokenAddress, + signedOrder.takerTokenAddress, + signedOrder.feeRecipientAddress, + signedOrder.makerTokenAmount, + signedOrder.takerTokenAmount, + signedOrder.makerFeeAmount, + signedOrder.takerFeeAmount, + signedOrder.expirationTimeSeconds, + signedOrder.salt, + ]); + const orderHashHex = `0x${orderHashBuff.toString('hex')}`; + const prefixedOrderHashBuff = crypto.solSHA3([new BigNumber(orderSchemaHashHex), new BigNumber(orderHashHex)]); + const prefixedOrderHashHex = `0x${prefixedOrderHashBuff.toString('hex')}`; + return prefixedOrderHashHex; + }, + getSignatureType(signature: string): SignatureType { + const signatureBuff = new Buffer(signature); + const signatureType = signatureBuff[0]; + if (!_.has(SignatureType, signatureType)) { + throw new Error(`${signatureType} is not a valid signature type`); + } + return signatureType; }, }; |