diff options
Diffstat (limited to 'packages/order-utils/src/order_factory.ts')
-rw-r--r-- | packages/order-utils/src/order_factory.ts | 76 |
1 files changed, 48 insertions, 28 deletions
diff --git a/packages/order-utils/src/order_factory.ts b/packages/order-utils/src/order_factory.ts index 2759aac81..678336ac5 100644 --- a/packages/order-utils/src/order_factory.ts +++ b/packages/order-utils/src/order_factory.ts @@ -1,49 +1,69 @@ -import { Provider, SignedOrder } from '@0xproject/types'; +import { ECSignature, SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; +import { Provider } from 'ethereum-types'; +import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; -import { getOrderHashHex } from './order_hash'; +import { orderHashUtils } from './order_hash'; import { generatePseudoRandomSalt } from './salt'; -import { signOrderHashAsync } from './signature_utils'; - -const SHOULD_ADD_PERSONAL_MESSAGE_PREFIX = false; +import { ecSignOrderHashAsync } from './signature_utils'; +import { MessagePrefixType } from './types'; export const orderFactory = { async createSignedOrderAsync( provider: Provider, - maker: string, - taker: string, + makerAddress: string, + takerAddress: string, + senderAddress: string, makerFee: BigNumber, takerFee: BigNumber, - makerTokenAmount: BigNumber, - makerTokenAddress: string, - takerTokenAmount: BigNumber, - takerTokenAddress: string, - exchangeContractAddress: string, - feeRecipient: string, - expirationUnixTimestampSecIfExists?: BigNumber, + makerAssetAmount: BigNumber, + makerAssetData: string, + takerAssetAmount: BigNumber, + takerAssetData: string, + exchangeAddress: string, + feeRecipientAddress: string, + expirationTimeSecondsIfExists?: BigNumber, ): Promise<SignedOrder> { const defaultExpirationUnixTimestampSec = new BigNumber(2524604400); // Close to infinite - const expirationUnixTimestampSec = _.isUndefined(expirationUnixTimestampSecIfExists) + const expirationTimeSeconds = _.isUndefined(expirationTimeSecondsIfExists) ? defaultExpirationUnixTimestampSec - : expirationUnixTimestampSecIfExists; + : expirationTimeSecondsIfExists; const order = { - maker, - taker, + makerAddress, + takerAddress, + senderAddress, makerFee, takerFee, - makerTokenAmount, - takerTokenAmount, - makerTokenAddress, - takerTokenAddress, + makerAssetAmount, + takerAssetAmount, + makerAssetData, + takerAssetData, salt: generatePseudoRandomSalt(), - exchangeContractAddress, - feeRecipient, - expirationUnixTimestampSec, + exchangeAddress, + feeRecipientAddress, + expirationTimeSeconds, + }; + const orderHash = orderHashUtils.getOrderHashHex(order); + const messagePrefixOpts = { + prefixType: MessagePrefixType.EthSign, + shouldAddPrefixBeforeCallingEthSign: false, }; - const orderHash = getOrderHashHex(order); - const ecSignature = await signOrderHashAsync(provider, orderHash, maker, SHOULD_ADD_PERSONAL_MESSAGE_PREFIX); - const signedOrder: SignedOrder = _.assign(order, { ecSignature }); + const ecSignature = await ecSignOrderHashAsync(provider, orderHash, makerAddress, messagePrefixOpts); + const signature = getVRSHexString(ecSignature); + const signedOrder: SignedOrder = _.assign(order, { signature }); return signedOrder; }, }; + +function getVRSHexString(ecSignature: ECSignature): string { + const vrs = `0x${intToHex(ecSignature.v)}${ethUtil.stripHexPrefix(ecSignature.r)}${ethUtil.stripHexPrefix( + ecSignature.s, + )}`; + return vrs; +} + +function intToHex(i: number): string { + const hex = ethUtil.bufferToHex(ethUtil.toBuffer(i)); + return hex; +} |