From 3ff8a319c5460caaf9edc1bea68e807fe0611aaa Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Wed, 7 Mar 2018 16:05:43 -0800 Subject: Add utils for hashing and signing orders, update wrappers --- packages/contracts/src/utils/order_factory.ts | 36 +++++++++++++-------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'packages/contracts/src/utils/order_factory.ts') diff --git a/packages/contracts/src/utils/order_factory.ts b/packages/contracts/src/utils/order_factory.ts index d5c3a9544..3f09cedf3 100644 --- a/packages/contracts/src/utils/order_factory.ts +++ b/packages/contracts/src/utils/order_factory.ts @@ -1,37 +1,35 @@ -import { Order, ZeroEx } from '0x.js'; +import { ZeroEx } from '0x.js'; import { BigNumber } from '@0xproject/utils'; -import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; -import { signedOrderUtils } from './signed_order_utils'; -import { DefaultOrderParams, SignedOrder } from './types'; +import { orderUtils } from './order_utils'; +import { signingUtils } from './signing_utils'; +import { DefaultOrderParams, SignatureType, SignedOrder, UnsignedOrder } from './types'; export class OrderFactory { - private _defaultOrderParams: Partial; - private _zeroEx: ZeroEx; - constructor(zeroEx: ZeroEx, defaultOrderParams: Partial) { + private _defaultOrderParams: Partial; + private _secretKey: Buffer; + constructor(secretKey: Buffer, defaultOrderParams: Partial) { this._defaultOrderParams = defaultOrderParams; - this._zeroEx = zeroEx; + this._secretKey = secretKey; } - public async newSignedOrderAsync(customOrderParams: Partial = {}): Promise { + public newSignedOrder( + customOrderParams: Partial = {}, + signatureType: SignatureType = SignatureType.Ecrecover, + ): SignedOrder { const randomExpiration = new BigNumber(Math.floor((Date.now() + Math.random() * 100000000000) / 1000)); const order = ({ - expirationTimestampSeconds: randomExpiration, + expirationTimeSeconds: randomExpiration, salt: ZeroEx.generatePseudoRandomSalt(), takerAddress: ZeroEx.NULL_ADDRESS, ...this._defaultOrderParams, ...customOrderParams, - } as any) as SignedOrder; - const orderHashHex = signedOrderUtils.getOrderHashHex(order); - const shouldAddPersonalMessagePrefix = false; - const ecSignature = await this._zeroEx.signOrderHashAsync( - orderHashHex, - order.makerAddress, - shouldAddPersonalMessagePrefix, - ); + } as any) as UnsignedOrder; + const orderHashBuff = orderUtils.getOrderHashBuff(order); + const signature = signingUtils.signMessage(orderHashBuff, this._secretKey, signatureType); const signedOrder = { ...order, - ecSignature, + signature: `0x${signature.toString('hex')}`, }; return signedOrder; } -- cgit v1.2.3