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 | |
parent | 4b6324050da4862f4d8bdb7d6dbd66107929a2b5 (diff) | |
download | dexon-sol-tools-fd004032cb23998184a78ac4a0a486ef1bd04c25.tar.gz dexon-sol-tools-fd004032cb23998184a78ac4a0a486ef1bd04c25.tar.zst dexon-sol-tools-fd004032cb23998184a78ac4a0a486ef1bd04c25.zip |
Introduce SignedOrder class and remove type assertions
Diffstat (limited to 'packages/contracts/util')
-rw-r--r-- | packages/contracts/util/exchange_wrapper.ts | 50 | ||||
-rw-r--r-- | packages/contracts/util/formatters.ts | 20 | ||||
-rw-r--r-- | packages/contracts/util/order.ts | 68 | ||||
-rw-r--r-- | packages/contracts/util/order_factory.ts | 7 | ||||
-rw-r--r-- | packages/contracts/util/signed_order.ts | 92 | ||||
-rw-r--r-- | packages/contracts/util/types.ts | 9 |
6 files changed, 142 insertions, 104 deletions
diff --git a/packages/contracts/util/exchange_wrapper.ts b/packages/contracts/util/exchange_wrapper.ts index e44a0eab6..ff263246f 100644 --- a/packages/contracts/util/exchange_wrapper.ts +++ b/packages/contracts/util/exchange_wrapper.ts @@ -6,7 +6,7 @@ import * as Web3 from 'web3'; import { ExchangeContract } from '../src/contract_wrappers/generated/exchange'; import { formatters } from './formatters'; -import { Order } from './order'; +import { SignedOrder } from './signed_order'; export class ExchangeWrapper { private _exchange: ExchangeContract; @@ -16,7 +16,7 @@ export class ExchangeWrapper { this._zeroEx = zeroEx; } public async fillOrderAsync( - order: Order, + signedOrder: SignedOrder, from: string, opts: { fillTakerTokenAmount?: BigNumber; @@ -24,15 +24,15 @@ export class ExchangeWrapper { } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { const shouldThrowOnInsufficientBalanceOrAllowance = !!opts.shouldThrowOnInsufficientBalanceOrAllowance; - const params = order.createFill(shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmount); + const params = signedOrder.createFill(shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmount); const txHash = await this._exchange.fillOrder.sendTransactionAsync( params.orderAddresses, params.orderValues, params.fillTakerTokenAmount, params.shouldThrowOnInsufficientBalanceOrAllowance, - params.v as number, - params.r as string, - params.s as string, + params.v, + params.r, + params.s, { from }, ); const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); @@ -41,11 +41,11 @@ export class ExchangeWrapper { return tx; } public async cancelOrderAsync( - order: Order, + signedOrder: SignedOrder, from: string, opts: { cancelTakerTokenAmount?: BigNumber } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = order.createCancel(opts.cancelTakerTokenAmount); + const params = signedOrder.createCancel(opts.cancelTakerTokenAmount); const txHash = await this._exchange.cancelOrder.sendTransactionAsync( params.orderAddresses, params.orderValues, @@ -58,19 +58,19 @@ export class ExchangeWrapper { return tx; } public async fillOrKillOrderAsync( - order: Order, + signedOrder: SignedOrder, from: string, opts: { fillTakerTokenAmount?: BigNumber } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { const shouldThrowOnInsufficientBalanceOrAllowance = true; - const params = order.createFill(shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmount); + const params = signedOrder.createFill(shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmount); const txHash = await this._exchange.fillOrKillOrder.sendTransactionAsync( params.orderAddresses, params.orderValues, params.fillTakerTokenAmount, - params.v as number, - params.r as string, - params.s as string, + params.v, + params.r, + params.s, { from }, ); const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); @@ -79,7 +79,7 @@ export class ExchangeWrapper { return tx; } public async batchFillOrdersAsync( - orders: Order[], + orders: SignedOrder[], from: string, opts: { fillTakerTokenAmounts?: BigNumber[]; @@ -108,7 +108,7 @@ export class ExchangeWrapper { return tx; } public async batchFillOrKillOrdersAsync( - orders: Order[], + orders: SignedOrder[], from: string, opts: { fillTakerTokenAmounts?: BigNumber[]; shouldThrowOnInsufficientBalanceOrAllowance?: boolean } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { @@ -133,7 +133,7 @@ export class ExchangeWrapper { return tx; } public async fillOrdersUpToAsync( - orders: Order[], + orders: SignedOrder[], from: string, opts: { fillTakerTokenAmount: BigNumber; shouldThrowOnInsufficientBalanceOrAllowance?: boolean }, ): Promise<TransactionReceiptWithDecodedLogs> { @@ -159,7 +159,7 @@ export class ExchangeWrapper { return tx; } public async batchCancelOrdersAsync( - orders: Order[], + orders: SignedOrder[], from: string, opts: { cancelTakerTokenAmounts?: BigNumber[] } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { @@ -175,19 +175,19 @@ export class ExchangeWrapper { _.each(tx.logs, log => wrapLogBigNumbers(log)); return tx; } - public async getOrderHashAsync(order: Order): Promise<string> { + public async getOrderHashAsync(signedOrder: SignedOrder): Promise<string> { const shouldThrowOnInsufficientBalanceOrAllowance = false; - const params = order.createFill(shouldThrowOnInsufficientBalanceOrAllowance); + const params = signedOrder.createFill(shouldThrowOnInsufficientBalanceOrAllowance); const orderHash = await this._exchange.getOrderHash(params.orderAddresses, params.orderValues); return orderHash; } - public async isValidSignatureAsync(order: Order): Promise<boolean> { + public async isValidSignatureAsync(signedOrder: SignedOrder): Promise<boolean> { const isValidSignature = await this._exchange.isValidSignature( - order.params.maker, - order.getOrderHashHex(), - order.params.v as number, - order.params.r as string, - order.params.s as string, + signedOrder.params.maker, + signedOrder.getOrderHashHex(), + signedOrder.params.v, + signedOrder.params.r, + signedOrder.params.s, ); return isValidSignature; } diff --git a/packages/contracts/util/formatters.ts b/packages/contracts/util/formatters.ts index e16fe8d45..6c225268e 100644 --- a/packages/contracts/util/formatters.ts +++ b/packages/contracts/util/formatters.ts @@ -1,12 +1,12 @@ import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; -import { Order } from './order'; +import { SignedOrder } from './signed_order'; import { BatchCancelOrders, BatchFillOrders, FillOrdersUpTo } from './types'; export const formatters = { createBatchFill( - orders: Order[], + orders: SignedOrder[], shouldThrowOnInsufficientBalanceOrAllowance: boolean, fillTakerTokenAmounts: BigNumber[] = [], ) { @@ -35,9 +35,9 @@ export const formatters = { order.params.expirationTimestampInSec, order.params.salt, ]); - batchFill.v.push(order.params.v as number); - batchFill.r.push(order.params.r as string); - batchFill.s.push(order.params.s as string); + batchFill.v.push(order.params.v); + batchFill.r.push(order.params.r); + batchFill.s.push(order.params.s); if (fillTakerTokenAmounts.length < orders.length) { batchFill.fillTakerTokenAmounts.push(order.params.takerTokenAmount); } @@ -45,7 +45,7 @@ export const formatters = { return batchFill; }, createFillUpTo( - orders: Order[], + orders: SignedOrder[], shouldThrowOnInsufficientBalanceOrAllowance: boolean, fillTakerTokenAmount: BigNumber, ) { @@ -74,13 +74,13 @@ export const formatters = { order.params.expirationTimestampInSec, order.params.salt, ]); - fillUpTo.v.push(order.params.v as number); - fillUpTo.r.push(order.params.r as string); - fillUpTo.s.push(order.params.s as string); + fillUpTo.v.push(order.params.v); + fillUpTo.r.push(order.params.r); + fillUpTo.s.push(order.params.s); }); return fillUpTo; }, - createBatchCancel(orders: Order[], cancelTakerTokenAmounts: BigNumber[] = []) { + createBatchCancel(orders: SignedOrder[], cancelTakerTokenAmounts: BigNumber[] = []) { const batchCancel: BatchCancelOrders = { orderAddresses: [], orderValues: [], diff --git a/packages/contracts/util/order.ts b/packages/contracts/util/order.ts index 12f10fa4e..6d412e2af 100644 --- a/packages/contracts/util/order.ts +++ b/packages/contracts/util/order.ts @@ -4,6 +4,7 @@ import ethUtil = require('ethereumjs-util'); import * as _ from 'lodash'; import { crypto } from './crypto'; +import { SignedOrder } from './signed_order'; import { OrderParams } from './types'; export class Order { @@ -13,76 +14,17 @@ export class Order { this.params = params; this._web3Wrapper = web3Wrapper; } - public isValidSignature() { - const { v, r, s } = this.params; - if (_.isUndefined(v) || _.isUndefined(r) || _.isUndefined(s)) { - throw new Error('Cannot call isValidSignature on unsigned order'); - } - 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 async signAsync() { + public async signAsync(): Promise<SignedOrder> { const orderHash = this.getOrderHashHex(); const signature = await this._web3Wrapper.signTransactionAsync(this.params.maker, orderHash); const { v, r, s } = ethUtil.fromRpcSig(signature); - this.params = _.assign(this.params, { + const signedOrderParams = _.assign(this.params, { v, r: ethUtil.bufferToHex(r), s: ethUtil.bufferToHex(s), }); - } - 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; + const signedOrder = new SignedOrder(this._web3Wrapper, signedOrderParams); + return signedOrder; } public getOrderHashHex(): string { const orderHash = crypto.solSHA3([ diff --git a/packages/contracts/util/order_factory.ts b/packages/contracts/util/order_factory.ts index 2b50f13e8..788f60a88 100644 --- a/packages/contracts/util/order_factory.ts +++ b/packages/contracts/util/order_factory.ts @@ -4,6 +4,7 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; import { Order } from './order'; +import { SignedOrder } from './signed_order'; import { DefaultOrderParams, OptionalOrderParams, OrderParams } from './types'; export class OrderFactory { @@ -13,7 +14,7 @@ export class OrderFactory { this._defaultOrderParams = defaultOrderParams; this._web3Wrapper = web3Wrapper; } - public async newSignedOrderAsync(customOrderParams: OptionalOrderParams = {}): Promise<Order> { + public async newSignedOrderAsync(customOrderParams: OptionalOrderParams = {}): Promise<SignedOrder> { const randomExpiration = new BigNumber(Math.floor((Date.now() + Math.random() * 100000000000) / 1000)); const orderParams: OrderParams = _.assign( {}, @@ -26,7 +27,7 @@ export class OrderFactory { customOrderParams, ); const order = new Order(this._web3Wrapper, orderParams); - await order.signAsync(); - return order; + const signedOrder = await order.signAsync(); + return signedOrder; } } 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; + } +} diff --git a/packages/contracts/util/types.ts b/packages/contracts/util/types.ts index 5d474b596..1bd8940ad 100644 --- a/packages/contracts/util/types.ts +++ b/packages/contracts/util/types.ts @@ -76,9 +76,12 @@ export interface OrderParams { takerFee: BigNumber; expirationTimestampInSec: BigNumber; salt: BigNumber; - v?: number; - r?: string; - s?: string; +} + +export interface SignedOrderParams extends OrderParams { + v: number; + r: string; + s: string; } export interface TransactionDataParams { |