diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-02-07 19:22:22 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-02-07 19:22:22 +0800 |
commit | fd004032cb23998184a78ac4a0a486ef1bd04c25 (patch) | |
tree | 90f7e64996ccc0fee1e5428b2ced9b2c2b1c3d4a /packages/contracts/util/signed_order.ts | |
parent | 4b6324050da4862f4d8bdb7d6dbd66107929a2b5 (diff) | |
download | dexon-0x-contracts-fd004032cb23998184a78ac4a0a486ef1bd04c25.tar dexon-0x-contracts-fd004032cb23998184a78ac4a0a486ef1bd04c25.tar.gz dexon-0x-contracts-fd004032cb23998184a78ac4a0a486ef1bd04c25.tar.bz2 dexon-0x-contracts-fd004032cb23998184a78ac4a0a486ef1bd04c25.tar.lz dexon-0x-contracts-fd004032cb23998184a78ac4a0a486ef1bd04c25.tar.xz dexon-0x-contracts-fd004032cb23998184a78ac4a0a486ef1bd04c25.tar.zst dexon-0x-contracts-fd004032cb23998184a78ac4a0a486ef1bd04c25.zip |
Introduce SignedOrder class and remove type assertions
Diffstat (limited to 'packages/contracts/util/signed_order.ts')
-rw-r--r-- | packages/contracts/util/signed_order.ts | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/packages/contracts/util/signed_order.ts b/packages/contracts/util/signed_order.ts new file mode 100644 index 000000000..a84e0686c --- /dev/null +++ b/packages/contracts/util/signed_order.ts @@ -0,0 +1,92 @@ +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 { SignedOrderParams } from './types'; + +export class SignedOrder { + public params: SignedOrderParams; + private _web3Wrapper: Web3Wrapper; + constructor(web3Wrapper: Web3Wrapper, params: SignedOrderParams) { + this.params = params; + this._web3Wrapper = web3Wrapper; + } + public isValidSignature() { + const { v, r, s } = this.params; + const orderHash = this.getOrderHashHex(); + const msgHash = ethUtil.hashPersonalMessage(ethUtil.toBuffer(orderHash)); + try { + const pubKey = ethUtil.ecrecover(msgHash, v, ethUtil.toBuffer(r), ethUtil.toBuffer(s)); + const recoveredAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey)); + return recoveredAddress === this.params.maker; + } catch (err) { + return false; + } + } + public createFill(shouldThrowOnInsufficientBalanceOrAllowance?: boolean, fillTakerTokenAmount?: BigNumber) { + const fill = { + orderAddresses: [ + this.params.maker, + this.params.taker, + this.params.makerToken, + this.params.takerToken, + this.params.feeRecipient, + ], + orderValues: [ + this.params.makerTokenAmount, + this.params.takerTokenAmount, + this.params.makerFee, + this.params.takerFee, + this.params.expirationTimestampInSec, + this.params.salt, + ], + fillTakerTokenAmount: fillTakerTokenAmount || this.params.takerTokenAmount, + shouldThrowOnInsufficientBalanceOrAllowance: !!shouldThrowOnInsufficientBalanceOrAllowance, + v: this.params.v, + r: this.params.r, + s: this.params.s, + }; + return fill; + } + public createCancel(cancelTakerTokenAmount?: BigNumber) { + const cancel = { + orderAddresses: [ + this.params.maker, + this.params.taker, + this.params.makerToken, + this.params.takerToken, + this.params.feeRecipient, + ], + orderValues: [ + this.params.makerTokenAmount, + this.params.takerTokenAmount, + this.params.makerFee, + this.params.takerFee, + this.params.expirationTimestampInSec, + this.params.salt, + ], + cancelTakerTokenAmount: cancelTakerTokenAmount || this.params.takerTokenAmount, + }; + return cancel; + } + public getOrderHashHex(): string { + const orderHash = crypto.solSHA3([ + this.params.exchangeContractAddress, + this.params.maker, + this.params.taker, + this.params.makerToken, + this.params.takerToken, + this.params.feeRecipient, + this.params.makerTokenAmount, + this.params.takerTokenAmount, + this.params.makerFee, + this.params.takerFee, + this.params.expirationTimestampInSec, + this.params.salt, + ]); + const orderHashHex = ethUtil.bufferToHex(orderHash); + return orderHashHex; + } +} |