diff options
Diffstat (limited to 'packages/order-utils/src')
-rw-r--r-- | packages/order-utils/src/index.ts | 10 | ||||
-rw-r--r-- | packages/order-utils/src/order_factory.ts | 25 | ||||
-rw-r--r-- | packages/order-utils/src/signature_utils.ts | 109 | ||||
-rw-r--r-- | packages/order-utils/src/types.ts | 22 |
4 files changed, 79 insertions, 87 deletions
diff --git a/packages/order-utils/src/index.ts b/packages/order-utils/src/index.ts index 858f500c6..681fbc904 100644 --- a/packages/order-utils/src/index.ts +++ b/packages/order-utils/src/index.ts @@ -13,15 +13,7 @@ export { orderFactory } from './order_factory'; export { constants } from './constants'; export { crypto } from './crypto'; export { generatePseudoRandomSalt } from './salt'; -export { - CreateOrderOpts, - OrderError, - MessagePrefixType, - MessagePrefixOpts, - EIP712Parameter, - EIP712Schema, - EIP712Types, -} from './types'; +export { CreateOrderOpts, OrderError, EIP712Parameter, EIP712Schema, EIP712Types } from './types'; export { AbstractBalanceAndProxyAllowanceFetcher } from './abstract/abstract_balance_and_proxy_allowance_fetcher'; export { AbstractOrderFilledCancelledFetcher } from './abstract/abstract_order_filled_cancelled_fetcher'; export { BalanceAndProxyAllowanceLazyStore } from './store/balance_and_proxy_allowance_lazy_store'; diff --git a/packages/order-utils/src/order_factory.ts b/packages/order-utils/src/order_factory.ts index 14727fd97..4a6f3924b 100644 --- a/packages/order-utils/src/order_factory.ts +++ b/packages/order-utils/src/order_factory.ts @@ -1,14 +1,13 @@ -import { ECSignature, Order, SignedOrder } from '@0xproject/types'; +import { Order, SignedOrder, SignerType } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import { Provider } from 'ethereum-types'; -import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; import { constants } from './constants'; import { orderHashUtils } from './order_hash'; import { generatePseudoRandomSalt } from './salt'; import { ecSignOrderHashAsync } from './signature_utils'; -import { CreateOrderOpts, MessagePrefixType } from './types'; +import { CreateOrderOpts } from './types'; export const orderFactory = { createOrder( @@ -59,12 +58,7 @@ export const orderFactory = { createOrderOpts, ); const orderHash = orderHashUtils.getOrderHashHex(order); - const messagePrefixOpts = { - prefixType: MessagePrefixType.EthSign, - shouldAddPrefixBeforeCallingEthSign: false, - }; - const ecSignature = await ecSignOrderHashAsync(provider, orderHash, makerAddress, messagePrefixOpts); - const signature = getVRSHexString(ecSignature); + const signature = await ecSignOrderHashAsync(provider, orderHash, makerAddress, SignerType.Default); const signedOrder: SignedOrder = _.assign(order, { signature }); return signedOrder; }, @@ -89,16 +83,3 @@ function generateDefaultCreateOrderOpts(): { expirationTimeSeconds: constants.INFINITE_TIMESTAMP_SEC, }; } - -function getVRSHexString(ecSignature: ECSignature): string { - const ETH_SIGN_SIGNATURE_TYPE = '03'; - const vrs = `${intToHex(ecSignature.v)}${ethUtil.stripHexPrefix(ecSignature.r)}${ethUtil.stripHexPrefix( - ecSignature.s, - )}${ETH_SIGN_SIGNATURE_TYPE}`; - return vrs; -} - -function intToHex(i: number): string { - const hex = ethUtil.bufferToHex(ethUtil.toBuffer(i)); - return hex; -} diff --git a/packages/order-utils/src/signature_utils.ts b/packages/order-utils/src/signature_utils.ts index 26fb24705..870aef2ed 100644 --- a/packages/order-utils/src/signature_utils.ts +++ b/packages/order-utils/src/signature_utils.ts @@ -1,5 +1,5 @@ import { schemas } from '@0xproject/json-schemas'; -import { ECSignature, SignatureType, ValidatorSignature } from '@0xproject/types'; +import { ECSignature, SignatureType, SignerType, ValidatorSignature } from '@0xproject/types'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { Provider } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; @@ -10,7 +10,7 @@ import { assert } from './assert'; import { ExchangeContract } from './generated_contract_wrappers/exchange'; import { IValidatorContract } from './generated_contract_wrappers/i_validator'; import { IWalletContract } from './generated_contract_wrappers/i_wallet'; -import { MessagePrefixOpts, MessagePrefixType, OrderError } from './types'; +import { OrderError } from './types'; import { utils } from './utils'; /** @@ -48,7 +48,7 @@ export async function isValidSignatureAsync( case SignatureType.EthSign: { const ecSignature = parseECSignature(signature); - const prefixedMessageHex = addSignedMessagePrefix(data, MessagePrefixType.EthSign); + const prefixedMessageHex = addSignedMessagePrefix(data, SignerType.Default); return isValidECSignature(prefixedMessageHex, ecSignature, signerAddress); } @@ -72,7 +72,7 @@ export async function isValidSignatureAsync( } case SignatureType.Trezor: { - const prefixedMessageHex = addSignedMessagePrefix(data, MessagePrefixType.Trezor); + const prefixedMessageHex = addSignedMessagePrefix(data, SignerType.Trezor); const ecSignature = parseECSignature(signature); return isValidECSignature(prefixedMessageHex, ecSignature, signerAddress); } @@ -191,22 +191,21 @@ export function isValidECSignature(data: string, signature: ECSignature, signerA } /** - * Signs an orderHash and returns it's elliptic curve signature. + * Signs an orderHash and returns it's elliptic curve signature and signature type. * This method currently supports TestRPC, Geth and Parity above and below V1.6.6 * @param orderHash Hex encoded orderHash to sign. * @param signerAddress The hex encoded Ethereum address you wish to sign it with. This address * must be available via the Provider supplied to 0x.js. - * @param hashPrefixOpts Different signers add/require different prefixes be appended to the message being signed. - * Since we cannot know ahead of time which signer you are using, you must supply both a prefixType and - * whether it must be added before calling `eth_sign` (some signers add it themselves) - * @return An object containing the Elliptic curve signature parameters generated by signing the orderHash. + * @param signerType Different signers add/require different prefixes to be prepended to the message being signed. + * Since we cannot know ahead of time which signer you are using, you must supply a SignerType. + * @return A hex encoded string containing the Elliptic curve signature generated by signing the orderHash and the Signature Type. */ export async function ecSignOrderHashAsync( provider: Provider, orderHash: string, signerAddress: string, - messagePrefixOpts: MessagePrefixOpts, -): Promise<ECSignature> { + signerType: SignerType, +): Promise<string> { assert.isWeb3Provider('provider', provider); assert.isHexString('orderHash', orderHash); assert.isETHAddressHex('signerAddress', signerAddress); @@ -215,8 +214,10 @@ export async function ecSignOrderHashAsync( const normalizedSignerAddress = signerAddress.toLowerCase(); let msgHashHex = orderHash; - const prefixedMsgHashHex = addSignedMessagePrefix(orderHash, messagePrefixOpts.prefixType); - if (messagePrefixOpts.shouldAddPrefixBeforeCallingEthSign) { + const prefixedMsgHashHex = addSignedMessagePrefix(orderHash, signerType); + // Metamask incorrectly implements eth_sign and does not prefix the message as per the spec + // Source: https://github.com/MetaMask/metamask-extension/commit/a9d36860bec424dcee8db043d3e7da6a5ff5672e + if (signerType === SignerType.Metamask) { msgHashHex = prefixedMsgHashHex; } const signature = await web3Wrapper.signMessageAsync(normalizedSignerAddress, msgHashHex); @@ -225,57 +226,97 @@ export async function ecSignOrderHashAsync( // v + r + s OR r + s + v, and different clients (even different versions of the same client) // return the signature params in different orders. In order to support all client implementations, // we parse the signature in both ways, and evaluate if either one is a valid signature. + // r + s + v is the most prevalent format from eth_sign, so we attempt this first. // tslint:disable-next-line:custom-no-magic-numbers const validVParamValues = [27, 28]; + const ecSignatureRSV = parseSignatureHexAsRSV(signature); + if (_.includes(validVParamValues, ecSignatureRSV.v)) { + const isValidRSVSignature = isValidECSignature(prefixedMsgHashHex, ecSignatureRSV, normalizedSignerAddress); + if (isValidRSVSignature) { + const convertedSignatureHex = convertECSignatureToSignatureHex(ecSignatureRSV, signerType); + return convertedSignatureHex; + } + } const ecSignatureVRS = parseSignatureHexAsVRS(signature); if (_.includes(validVParamValues, ecSignatureVRS.v)) { const isValidVRSSignature = isValidECSignature(prefixedMsgHashHex, ecSignatureVRS, normalizedSignerAddress); if (isValidVRSSignature) { - return ecSignatureVRS; + const convertedSignatureHex = convertECSignatureToSignatureHex(ecSignatureVRS, signerType); + return convertedSignatureHex; } } - const ecSignatureRSV = parseSignatureHexAsRSV(signature); - if (_.includes(validVParamValues, ecSignatureRSV.v)) { - const isValidRSVSignature = isValidECSignature(prefixedMsgHashHex, ecSignatureRSV, normalizedSignerAddress); - if (isValidRSVSignature) { - return ecSignatureRSV; + throw new Error(OrderError.InvalidSignature); +} +/** + * Combines ECSignature with V,R,S and the relevant signature type for use in 0x protocol + * @param ecSignature The ECSignature of the signed data + * @param signerType The SignerType of the signed data + * @return Hex encoded string of signature (v,r,s) with Signature Type + */ +export function convertECSignatureToSignatureHex(ecSignature: ECSignature, signerType: SignerType): string { + const signatureBuffer = Buffer.concat([ + ethUtil.toBuffer(ecSignature.v), + ethUtil.toBuffer(ecSignature.r), + ethUtil.toBuffer(ecSignature.s), + ]); + const signatureHex = `0x${signatureBuffer.toString('hex')}`; + let signatureType; + switch (signerType) { + case SignerType.Metamask: + case SignerType.Ledger: + case SignerType.Default: { + signatureType = SignatureType.EthSign; + break; + } + case SignerType.Trezor: { + signatureType = SignatureType.Trezor; + break; } + default: + throw new Error(`Unrecognized SignerType: ${signerType}`); } - - throw new Error(OrderError.InvalidSignature); + const signatureWithType = convertToSignatureWithType(signatureHex, signatureType); + return signatureWithType; +} +/** + * Combines the signature proof and the Signature Type. + * @param signature The hex encoded signature proof + * @param signatureType The signature type, i.e EthSign, Trezor, Wallet etc. + * @return Hex encoded string of signature proof with Signature Type + */ +export function convertToSignatureWithType(signature: string, signatureType: SignatureType): string { + const signatureBuffer = Buffer.concat([ethUtil.toBuffer(signature), ethUtil.toBuffer(signatureType)]); + const signatureHex = `0x${signatureBuffer.toString('hex')}`; + return signatureHex; } - /** * Adds the relevant prefix to the message being signed. * @param message Message to sign - * @param messagePrefixType The type of message prefix to add. Different signers expect + * @param signerType The type of message prefix to add for a given SignerType. Different signers expect * specific message prefixes. * @return Prefixed message */ -export function addSignedMessagePrefix(message: string, messagePrefixType: MessagePrefixType): string { +export function addSignedMessagePrefix(message: string, signerType: SignerType = SignerType.Default): string { assert.isString('message', message); - assert.doesBelongToStringEnum('messagePrefixType', messagePrefixType, MessagePrefixType); - switch (messagePrefixType) { - case MessagePrefixType.None: - return message; - - case MessagePrefixType.EthSign: { + assert.doesBelongToStringEnum('signerType', signerType, SignerType); + switch (signerType) { + case SignerType.Metamask: + case SignerType.Ledger: + case SignerType.Default: { const msgBuff = ethUtil.toBuffer(message); const prefixedMsgBuff = ethUtil.hashPersonalMessage(msgBuff); const prefixedMsgHex = ethUtil.bufferToHex(prefixedMsgBuff); return prefixedMsgHex; } - - case MessagePrefixType.Trezor: { + case SignerType.Trezor: { const msgBuff = ethUtil.toBuffer(message); const prefixedMsgBuff = hashTrezorPersonalMessage(msgBuff); const prefixedMsgHex = ethUtil.bufferToHex(prefixedMsgBuff); return prefixedMsgHex; } - default: - throw new Error(`Unrecognized MessagePrefixType: ${messagePrefixType}`); + throw new Error(`Unrecognized SignerType: ${signerType}`); } } diff --git a/packages/order-utils/src/types.ts b/packages/order-utils/src/types.ts index f44e94349..1fbd8cf7b 100644 --- a/packages/order-utils/src/types.ts +++ b/packages/order-utils/src/types.ts @@ -4,28 +4,6 @@ export enum OrderError { InvalidSignature = 'INVALID_SIGNATURE', } -/** - * The requisite message prefix (is any) to add to an `eth_sign` request. - */ -export enum MessagePrefixType { - None = 'NONE', - EthSign = 'ETH_SIGN', - Trezor = 'TREZOR', -} - -/** - * Options related to message prefixing of messages sent to `eth_sign` - * Some signers prepend a message prefix (e.g Parity Signer, Ledger, TestRPC), while - * others require it already be prepended (e.g Metamask). In addition, different signers - * expect slightly different prefixes (See: https://github.com/ethereum/go-ethereum/issues/14794). - * Depending on the signer that will receive your signing request, you must specify the - * desired prefix and whether it should be added before making the `eth_sign` request. - */ -export interface MessagePrefixOpts { - prefixType: MessagePrefixType; - shouldAddPrefixBeforeCallingEthSign: boolean; -} - export enum TradeSide { Maker = 'maker', Taker = 'taker', |