diff options
author | Jacob Evans <jacob@dekz.net> | 2018-06-05 09:40:07 +0800 |
---|---|---|
committer | Jacob Evans <jacob@dekz.net> | 2018-06-06 05:08:04 +0800 |
commit | a59e9f024e76122c76600d938b565ea47938b287 (patch) | |
tree | 749c1485efeca041aa319e64af64aedd579dd8d6 /packages | |
parent | 4670cc1a5f04d61045a82b4a3bbcf56e009afd56 (diff) | |
download | dexon-sol-tools-a59e9f024e76122c76600d938b565ea47938b287.tar.gz dexon-sol-tools-a59e9f024e76122c76600d938b565ea47938b287.tar.zst dexon-sol-tools-a59e9f024e76122c76600d938b565ea47938b287.zip |
Update Order utils to use eip712
Diffstat (limited to 'packages')
-rw-r--r-- | packages/contracts/src/contracts/current/protocol/Exchange/libs/LibEIP712.sol | 35 | ||||
-rw-r--r-- | packages/contracts/src/contracts/current/protocol/Exchange/libs/LibOrder.sol | 30 | ||||
-rw-r--r-- | packages/contracts/src/utils/order_factory.ts | 4 | ||||
-rw-r--r-- | packages/contracts/src/utils/order_utils.ts | 56 | ||||
-rw-r--r-- | packages/contracts/src/utils/transaction_factory.ts | 16 | ||||
-rw-r--r-- | packages/contracts/src/utils/types.ts | 10 | ||||
-rw-r--r-- | packages/contracts/test/exchange/core.ts | 5 | ||||
-rw-r--r-- | packages/contracts/test/exchange/libs.ts | 9 | ||||
-rw-r--r-- | packages/order-utils/src/eip712_utils.ts (renamed from packages/contracts/src/utils/eip712_utils.ts) | 51 | ||||
-rw-r--r-- | packages/order-utils/src/index.ts | 3 | ||||
-rw-r--r-- | packages/order-utils/src/order_hash.ts | 75 | ||||
-rw-r--r-- | packages/order-utils/src/types.ts | 10 |
12 files changed, 121 insertions, 183 deletions
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibEIP712.sol b/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibEIP712.sol index d28ef876f..7704e3db4 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibEIP712.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibEIP712.sol @@ -20,16 +20,19 @@ pragma solidity ^0.4.24; contract LibEIP712 { string public constant EIP191_HEADER = "\x19\x01"; + bytes32 public constant EIP712_DOMAIN_SEPARATOR_NAME_HASH = keccak256("0x Protocol"); - bytes32 public constant EIP712_DOMAIN_SEPARATOR_VERSION_HASH = keccak256("1"); + bytes32 public constant EIP712_DOMAIN_SEPARATOR_VERSION_HASH = keccak256("2"); bytes32 public constant EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH = keccak256( - "DomainSeparator(", - "string name,", - "string version,", - "address contract", - ")" + abi.encodePacked( + "DomainSeparator(", + "string name,", + "string version,", + "address contract", + ")" + ) ); function createEIP712Message(bytes32 hashStruct) @@ -41,14 +44,18 @@ contract LibEIP712 { // Source: https://github.com/ethereum/EIPs/pull/712 // TODO: Cache the Domain Separator message = keccak256( - EIP191_HEADER, - keccak256( - EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH, - EIP712_DOMAIN_SEPARATOR_NAME_HASH, - EIP712_DOMAIN_SEPARATOR_VERSION_HASH, - bytes32(address(this)) - ), - hashStruct + abi.encodePacked( + EIP191_HEADER, + keccak256( + abi.encodePacked( + EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH, + EIP712_DOMAIN_SEPARATOR_NAME_HASH, + EIP712_DOMAIN_SEPARATOR_VERSION_HASH, + bytes32(address(this)) + ) + ), + hashStruct + ) ); return message; } diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibOrder.sol b/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibOrder.sol index 6888918e2..ed532fd59 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibOrder.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibOrder.sol @@ -89,20 +89,22 @@ contract LibOrder is orderHash = createEIP712Message( keccak256( abi.encodePacked( - EIP712_ORDER_SCHEMA_HASH, - bytes32(order.makerAddress), - bytes32(order.takerAddress), - bytes32(order.feeRecipientAddress), - bytes32(order.senderAddress), - order.makerAssetAmount, - order.takerAssetAmount, - order.makerFee, - order.takerFee, - order.expirationTimeSeconds, - order.salt, - keccak256(abi.encodePacked(order.makerAssetData)), - keccak256(abi.encodePacked(order.takerAssetData)) - ))); + EIP712_ORDER_SCHEMA_HASH, + bytes32(order.makerAddress), + bytes32(order.takerAddress), + bytes32(order.feeRecipientAddress), + bytes32(order.senderAddress), + order.makerAssetAmount, + order.takerAssetAmount, + order.makerFee, + order.takerFee, + order.expirationTimeSeconds, + order.salt, + keccak256(abi.encodePacked(order.makerAssetData)), + keccak256(abi.encodePacked(order.takerAssetData)) + ) + ) + ); return orderHash; } } diff --git a/packages/contracts/src/utils/order_factory.ts b/packages/contracts/src/utils/order_factory.ts index bfe5013e8..dd02e1f5c 100644 --- a/packages/contracts/src/utils/order_factory.ts +++ b/packages/contracts/src/utils/order_factory.ts @@ -1,5 +1,5 @@ import { generatePseudoRandomSalt, orderHashUtils } from '@0xproject/order-utils'; -import { Order, SignatureType, SignedOrder, UnsignedOrder } from '@0xproject/types'; +import { Order, SignatureType, SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; @@ -26,7 +26,7 @@ export class OrderFactory { takerAddress: constants.NULL_ADDRESS, ...this._defaultOrderParams, ...customOrderParams, - } as any) as UnsignedOrder; + } as any) as Order; const orderHashBuff = orderHashUtils.getOrderHashBuffer(order); const signature = signingUtils.signMessage(orderHashBuff, this._privateKey, signatureType); const signedOrder = { diff --git a/packages/contracts/src/utils/order_utils.ts b/packages/contracts/src/utils/order_utils.ts index 316350ffb..180761f2c 100644 --- a/packages/contracts/src/utils/order_utils.ts +++ b/packages/contracts/src/utils/order_utils.ts @@ -1,29 +1,8 @@ -import { Order, OrderWithoutExchangeAddress, SignedOrder, UnsignedOrder } from '@0xproject/types'; +import { OrderWithoutExchangeAddress, SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import ethUtil = require('ethereumjs-util'); -import * as _ from 'lodash'; -import { crypto } from './crypto'; -import { EIP712Utils } from './eip712_utils'; -import { CancelOrder, EIP712Schema, MatchOrder } from './types'; - -const EIP712_ORDER_SCHEMA: EIP712Schema = { - name: 'Order', - parameters: [ - { name: 'makerAddress', type: 'address' }, - { name: 'takerAddress', type: 'address' }, - { name: 'feeRecipientAddress', type: 'address' }, - { name: 'senderAddress', type: 'address' }, - { name: 'makerAssetAmount', type: 'uint256' }, - { name: 'takerAssetAmount', type: 'uint256' }, - { name: 'makerFee', type: 'uint256' }, - { name: 'takerFee', type: 'uint256' }, - { name: 'expirationTimeSeconds', type: 'uint256' }, - { name: 'salt', type: 'uint256' }, - { name: 'makerAssetData', type: 'bytes' }, - { name: 'takerAssetData', type: 'bytes' }, - ], -}; +import { CancelOrder, MatchOrder } from './types'; export const orderUtils = { createFill: (signedOrder: SignedOrder, takerAssetFillAmount?: BigNumber) => { @@ -58,37 +37,6 @@ export const orderUtils = { }; return orderStruct; }, - getOrderSchemaBuffer(): Buffer { - return EIP712Utils.compileSchema(EIP712_ORDER_SCHEMA); - }, - getOrderHashBuffer(order: SignedOrder | UnsignedOrder): Buffer { - const makerAssetDataHash = crypto.solSHA3([ethUtil.toBuffer(order.makerAssetData)]); - const takerAssetDataHash = crypto.solSHA3([ethUtil.toBuffer(order.takerAssetData)]); - - const orderParamsHashBuff = crypto.solSHA3([ - orderUtils.getOrderSchemaBuffer(), - EIP712Utils.pad32Address(order.makerAddress), - EIP712Utils.pad32Address(order.takerAddress), - EIP712Utils.pad32Address(order.feeRecipientAddress), - EIP712Utils.pad32Address(order.senderAddress), - order.makerAssetAmount, - order.takerAssetAmount, - order.makerFee, - order.takerFee, - order.expirationTimeSeconds, - order.salt, - makerAssetDataHash, - takerAssetDataHash, - ]); - const orderParamsHashHex = `0x${orderParamsHashBuff.toString('hex')}`; - const orderHashBuff = EIP712Utils.createEIP712Message(orderParamsHashHex, order.exchangeAddress); - return orderHashBuff; - }, - getOrderHashHex(order: SignedOrder | UnsignedOrder): string { - const orderHashBuff = orderUtils.getOrderHashBuffer(order); - const orderHashHex = `0x${orderHashBuff.toString('hex')}`; - return orderHashHex; - }, createMatchOrders(signedOrderLeft: SignedOrder, signedOrderRight: SignedOrder): MatchOrder { const fill = { left: orderUtils.getOrderWithoutExchangeAddress(signedOrderLeft), diff --git a/packages/contracts/src/utils/transaction_factory.ts b/packages/contracts/src/utils/transaction_factory.ts index 1c1029d62..7aaf3691c 100644 --- a/packages/contracts/src/utils/transaction_factory.ts +++ b/packages/contracts/src/utils/transaction_factory.ts @@ -1,14 +1,9 @@ -import { crypto, generatePseudoRandomSalt } from '@0xproject/order-utils'; +import { crypto, EIP712Schema, EIP712Utils, generatePseudoRandomSalt } from '@0xproject/order-utils'; import { SignatureType } from '@0xproject/types'; -import { BigNumber } from '@0xproject/utils'; import * as ethUtil from 'ethereumjs-util'; -import * as _ from 'lodash'; -import { crypto } from './crypto'; -import { EIP712Utils } from './eip712_utils'; -import { orderUtils } from './order_utils'; import { signingUtils } from './signing_utils'; -import { EIP712Schema, SignatureType, SignedTransaction } from './types'; +import { SignedTransaction } from './types'; const EIP712_EXECUTE_TRANSACTION_SCHEMA: EIP712Schema = { name: 'ExecuteTransaction', @@ -30,20 +25,15 @@ export class TransactionFactory { } public newSignedTransaction(data: string, signatureType: SignatureType = SignatureType.EthSign): SignedTransaction { const executeTransactionSchemaHashBuff = EIP712Utils.compileSchema(EIP712_EXECUTE_TRANSACTION_SCHEMA); - const salt = generatePseudoRandomSalt(); const dataHash = crypto.solSHA3([ethUtil.toBuffer(data)]); - const executeTransactionDataHash = crypto.solSHA3([ executeTransactionSchemaHashBuff, salt, EIP712Utils.pad32Buffer(this._signerBuff), dataHash, ]); - - const executeTransactionMessageHex = `0x${executeTransactionDataHash.toString('hex')}`; - - const txHash = EIP712Utils.createEIP712Message(executeTransactionMessageHex, this._exchangeAddress); + const txHash = EIP712Utils.createEIP712Message(executeTransactionDataHash, this._exchangeAddress); const signature = signingUtils.signMessage(txHash, this._privateKey, signatureType); const signedTx = { exchangeAddress: this._exchangeAddress, diff --git a/packages/contracts/src/utils/types.ts b/packages/contracts/src/utils/types.ts index 5b7f1bcee..360e1fdbc 100644 --- a/packages/contracts/src/utils/types.ts +++ b/packages/contracts/src/utils/types.ts @@ -147,13 +147,3 @@ export interface MatchOrder { leftSignature: string; rightSignature: string; } - -export interface EIP712Parameter { - name: string; - type: string; -} - -export interface EIP712Schema { - name: string; - parameters: EIP712Parameter[]; -} diff --git a/packages/contracts/test/exchange/core.ts b/packages/contracts/test/exchange/core.ts index 91ead93f0..6d62c2b86 100644 --- a/packages/contracts/test/exchange/core.ts +++ b/packages/contracts/test/exchange/core.ts @@ -1,5 +1,5 @@ import { BlockchainLifecycle } from '@0xproject/dev-utils'; -import { assetProxyUtils, crypto, orderHashUtils } from '@0xproject/order-utils'; +import { assetProxyUtils, orderHashUtils } from '@0xproject/order-utils'; import { AssetProxyId, SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; @@ -24,8 +24,7 @@ import { ERC20Wrapper } from '../../src/utils/erc20_wrapper'; import { ERC721Wrapper } from '../../src/utils/erc721_wrapper'; import { ExchangeWrapper } from '../../src/utils/exchange_wrapper'; import { OrderFactory } from '../../src/utils/order_factory'; -import { orderUtils } from '../../src/utils/order_utils'; -import { ContractName, ERC20BalancesByOwner, OrderStatus } from '../../src/utils/types'; +import { ERC20BalancesByOwner } from '../../src/utils/types'; import { provider, txDefaults, web3Wrapper } from '../../src/utils/web3_wrapper'; chaiSetup.configure(); diff --git a/packages/contracts/test/exchange/libs.ts b/packages/contracts/test/exchange/libs.ts index b6de4d3b0..ac9020bce 100644 --- a/packages/contracts/test/exchange/libs.ts +++ b/packages/contracts/test/exchange/libs.ts @@ -1,5 +1,5 @@ import { BlockchainLifecycle } from '@0xproject/dev-utils'; -import { assetProxyUtils, orderHashUtils } from '@0xproject/order-utils'; +import { assetProxyUtils, EIP712Utils, orderHashUtils } from '@0xproject/order-utils'; import { SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import * as chai from 'chai'; @@ -10,7 +10,6 @@ import { addressUtils } from '../../src/utils/address_utils'; import { artifacts } from '../../src/utils/artifacts'; import { chaiSetup } from '../../src/utils/chai_setup'; import { constants } from '../../src/utils/constants'; -import { EIP712Utils } from '../../src/utils/eip712_utils'; import { OrderFactory } from '../../src/utils/order_factory'; import { provider, txDefaults, web3Wrapper } from '../../src/utils/web3_wrapper'; @@ -58,15 +57,15 @@ describe('Exchange libs', () => { describe('getOrderSchema', () => { it('should output the correct order schema hash', async () => { const orderSchema = await libs.getOrderSchemaHash.callAsync(); - const orderSchemaBuffer = orderHashUtils._getOrderSchemaHex(); - const schemaHashHex = `0x${orderSchemaBuffer.toString('hex')}`; + const schemaHashBuffer = orderHashUtils._getOrderSchemaBuffer(); + const schemaHashHex = `0x${schemaHashBuffer.toString('hex')}`; expect(schemaHashHex).to.be.equal(orderSchema); }); }); describe('getDomainSeparatorSchema', () => { it('should output the correct domain separator schema hash', async () => { const domainSeparatorSchema = await libs.getDomainSeparatorSchemaHash.callAsync(); - const domainSchemaBuffer = EIP712Utils.getDomainSeparatorSchemaBuffer(); + const domainSchemaBuffer = EIP712Utils._getDomainSeparatorSchemaBuffer(); const schemaHashHex = `0x${domainSchemaBuffer.toString('hex')}`; expect(schemaHashHex).to.be.equal(domainSeparatorSchema); }); diff --git a/packages/contracts/src/utils/eip712_utils.ts b/packages/order-utils/src/eip712_utils.ts index 07b45c8af..f7583f8d5 100644 --- a/packages/contracts/src/utils/eip712_utils.ts +++ b/packages/order-utils/src/eip712_utils.ts @@ -7,7 +7,8 @@ import { EIP712Schema } from './types'; const EIP191_PREFIX = '\x19\x01'; const EIP712_DOMAIN_NAME = '0x Protocol'; -const EIP712_DOMAIN_VERSION = '1'; +const EIP712_DOMAIN_VERSION = '2'; +const EIP712_VALUE_LENGTH = 32; const EIP712_DOMAIN_SCHEMA: EIP712Schema = { name: 'DomainSeparator', @@ -19,6 +20,11 @@ const EIP712_DOMAIN_SCHEMA: EIP712Schema = { }; export const EIP712Utils = { + /** + * Compiles the EIP712Schema and returns the hash of the schema. + * @param schema The EIP712 schema. + * @return The hash of the compiled schema + */ compileSchema(schema: EIP712Schema): Buffer { const namedTypes = _.map(schema.parameters, parameter => `${parameter.type} ${parameter.name}`); const namedTypesJoined = namedTypes.join(','); @@ -26,20 +32,31 @@ export const EIP712Utils = { const eip712SchemaHashBuffer = crypto.solSHA3([eip712Schema]); return eip712SchemaHashBuffer; }, - createEIP712Message(hashStruct: string, contractAddress: string): Buffer { - const domainSeparatorHashHex = EIP712Utils.getDomainSeparatorHashHex(contractAddress); - const messageBuff = crypto.solSHA3([ - EIP191_PREFIX, - new BigNumber(domainSeparatorHashHex), - new BigNumber(hashStruct), - ]); + /** + * Merges the EIP712 hash of a struct with the DomainSeparator for 0x v2. + * @param hashStruct the EIP712 hash of a struct + * @param contractAddress the exchange contract address + * @return The hash of an EIP712 message with domain separator prefixed + */ + createEIP712Message(hashStruct: Buffer, contractAddress: string): Buffer { + const domainSeparatorHashBuffer = EIP712Utils._getDomainSeparatorHashBuffer(contractAddress); + const messageBuff = crypto.solSHA3([EIP191_PREFIX, domainSeparatorHashBuffer, hashStruct]); return messageBuff; }, - getDomainSeparatorSchemaBuffer(): Buffer { + pad32Address(address: string): Buffer { + const addressBuffer = ethUtil.toBuffer(address); + const addressPadded = EIP712Utils.pad32Buffer(addressBuffer); + return addressPadded; + }, + pad32Buffer(buffer: Buffer): Buffer { + const bufferPadded = ethUtil.setLengthLeft(buffer, EIP712_VALUE_LENGTH); + return bufferPadded; + }, + _getDomainSeparatorSchemaBuffer(): Buffer { return EIP712Utils.compileSchema(EIP712_DOMAIN_SCHEMA); }, - getDomainSeparatorHashHex(exchangeAddress: string): string { - const domainSeparatorSchemaBuffer = EIP712Utils.getDomainSeparatorSchemaBuffer(); + _getDomainSeparatorHashBuffer(exchangeAddress: string): Buffer { + const domainSeparatorSchemaBuffer = EIP712Utils._getDomainSeparatorSchemaBuffer(); const nameHash = crypto.solSHA3([EIP712_DOMAIN_NAME]); const versionHash = crypto.solSHA3([EIP712_DOMAIN_VERSION]); const domainSeparatorHashBuff = crypto.solSHA3([ @@ -48,16 +65,6 @@ export const EIP712Utils = { versionHash, EIP712Utils.pad32Address(exchangeAddress), ]); - const domainSeparatorHashHex = `0x${domainSeparatorHashBuff.toString('hex')}`; - return domainSeparatorHashHex; - }, - pad32Address(address: string): Buffer { - const addressBuffer = ethUtil.toBuffer(address); - const addressPadded = EIP712Utils.pad32Buffer(addressBuffer); - return addressPadded; - }, - pad32Buffer(buffer: Buffer): Buffer { - const bufferPadded = ethUtil.setLengthLeft(buffer, 32); - return bufferPadded; + return domainSeparatorHashBuff; }, }; diff --git a/packages/order-utils/src/index.ts b/packages/order-utils/src/index.ts index b844fbfcb..caa996621 100644 --- a/packages/order-utils/src/index.ts +++ b/packages/order-utils/src/index.ts @@ -4,9 +4,10 @@ export { orderFactory } from './order_factory'; export { constants } from './constants'; export { crypto } from './crypto'; export { generatePseudoRandomSalt } from './salt'; -export { OrderError, MessagePrefixType, MessagePrefixOpts } from './types'; +export { OrderError, MessagePrefixType, MessagePrefixOpts, EIP712Parameter, EIP712Schema } from './types'; export { AbstractBalanceAndProxyAllowanceFetcher } from './abstract/abstract_balance_and_proxy_allowance_fetcher'; export { AbstractOrderFilledCancelledFetcher } from './abstract/abstract_order_filled_cancelled_fetcher'; export { RemainingFillableCalculator } from './remaining_fillable_calculator'; export { OrderStateUtils } from './order_state_utils'; export { assetProxyUtils } from './asset_proxy_utils'; +export { EIP712Utils } from './eip712_utils'; diff --git a/packages/order-utils/src/order_hash.ts b/packages/order-utils/src/order_hash.ts index 2ef746ef8..b2414febc 100644 --- a/packages/order-utils/src/order_hash.ts +++ b/packages/order-utils/src/order_hash.ts @@ -9,9 +9,29 @@ import * as _ from 'lodash'; import { assert } from './assert'; import { crypto } from './crypto'; +import { EIP712Utils } from './eip712_utils'; +import { EIP712Schema } from './types'; const INVALID_TAKER_FORMAT = 'instance.takerAddress is not of a type(s) string'; +const EIP712_ORDER_SCHEMA: EIP712Schema = { + name: 'Order', + parameters: [ + { name: 'makerAddress', type: 'address' }, + { name: 'takerAddress', type: 'address' }, + { name: 'feeRecipientAddress', type: 'address' }, + { name: 'senderAddress', type: 'address' }, + { name: 'makerAssetAmount', type: 'uint256' }, + { name: 'takerAssetAmount', type: 'uint256' }, + { name: 'makerFee', type: 'uint256' }, + { name: 'takerFee', type: 'uint256' }, + { name: 'expirationTimeSeconds', type: 'uint256' }, + { name: 'salt', type: 'uint256' }, + { name: 'makerAssetData', type: 'bytes' }, + { name: 'takerAssetData', type: 'bytes' }, + ], +}; + export const orderHashUtils = { /** * Checks if the supplied hex encoded order hash is valid. @@ -45,7 +65,7 @@ export const orderHashUtils = { throw error; } - const orderHashBuff = this.getOrderHashBuff(order); + const orderHashBuff = orderHashUtils.getOrderHashBuffer(order); const orderHashHex = `0x${orderHashBuff.toString('hex')}`; return orderHashHex; }, @@ -54,15 +74,16 @@ export const orderHashUtils = { * @param order An object that conforms to the Order or SignedOrder interface definitions. * @return The resulting orderHash from hashing the supplied order as a Buffer */ - getOrderHashBuff(order: SignedOrder | Order): Buffer { + getOrderHashBuffer(order: SignedOrder | Order): Buffer { const makerAssetDataHash = crypto.solSHA3([ethUtil.toBuffer(order.makerAssetData)]); const takerAssetDataHash = crypto.solSHA3([ethUtil.toBuffer(order.takerAssetData)]); const orderParamsHashBuff = crypto.solSHA3([ - order.makerAddress, - order.takerAddress, - order.feeRecipientAddress, - order.senderAddress, + orderHashUtils._getOrderSchemaBuffer(), + EIP712Utils.pad32Address(order.makerAddress), + EIP712Utils.pad32Address(order.takerAddress), + EIP712Utils.pad32Address(order.feeRecipientAddress), + EIP712Utils.pad32Address(order.senderAddress), order.makerAssetAmount, order.takerAssetAmount, order.makerFee, @@ -72,46 +93,10 @@ export const orderHashUtils = { makerAssetDataHash, takerAssetDataHash, ]); - const orderParamsHashHex = `0x${orderParamsHashBuff.toString('hex')}`; - const orderSchemaHashHex = this._getOrderSchemaHex(); - const domainSeparatorHashHex = this._getDomainSeparatorHashHex(order.exchangeAddress); - const domainSeparatorSchemaHex = this._getDomainSeparatorSchemaHex(); - const orderHashBuff = crypto.solSHA3([ - new BigNumber(domainSeparatorSchemaHex), - new BigNumber(domainSeparatorHashHex), - new BigNumber(orderSchemaHashHex), - new BigNumber(orderParamsHashHex), - ]); + const orderHashBuff = EIP712Utils.createEIP712Message(orderParamsHashBuff, order.exchangeAddress); return orderHashBuff; }, - _getOrderSchemaHex(): string { - const orderSchemaHashBuff = crypto.solSHA3([ - 'Order(', - 'address makerAddress,', - 'address takerAddress,', - 'address feeRecipientAddress,', - 'address senderAddress,', - 'uint256 makerAssetAmount,', - 'uint256 takerAssetAmount,', - 'uint256 makerFee,', - 'uint256 takerFee,', - 'uint256 expirationTimeSeconds,', - 'uint256 salt,', - 'bytes makerAssetData,', - 'bytes takerAssetData,', - ')', - ]); - const schemaHashHex = `0x${orderSchemaHashBuff.toString('hex')}`; - return schemaHashHex; - }, - _getDomainSeparatorSchemaHex(): string { - const domainSeparatorSchemaHashBuff = crypto.solSHA3(['DomainSeparator(address contract)']); - const schemaHashHex = `0x${domainSeparatorSchemaHashBuff.toString('hex')}`; - return schemaHashHex; - }, - _getDomainSeparatorHashHex(exchangeAddress: string): string { - const domainSeparatorHashBuff = crypto.solSHA3([exchangeAddress]); - const domainSeparatorHashHex = `0x${domainSeparatorHashBuff.toString('hex')}`; - return domainSeparatorHashHex; + _getOrderSchemaBuffer(): Buffer { + return EIP712Utils.compileSchema(EIP712_ORDER_SCHEMA); }, }; diff --git a/packages/order-utils/src/types.ts b/packages/order-utils/src/types.ts index db0bfb249..858c0e18b 100644 --- a/packages/order-utils/src/types.ts +++ b/packages/order-utils/src/types.ts @@ -23,3 +23,13 @@ export interface MessagePrefixOpts { prefixType: MessagePrefixType; shouldAddPrefixBeforeCallingEthSign: boolean; } + +export interface EIP712Parameter { + name: string; + type: string; +} + +export interface EIP712Schema { + name: string; + parameters: EIP712Parameter[]; +} |