diff options
author | Fabio Berger <me@fabioberger.com> | 2018-05-25 07:30:46 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-25 07:30:46 +0800 |
commit | 895a9093aa5f204f9f7ad0fedb2934a8b6c40b17 (patch) | |
tree | 9f1e56a98683fbee69a6b5fc051dc81064643734 /packages/contracts/src | |
parent | c4a7574f7bd3939eac9417241144197bbb22edb1 (diff) | |
parent | 35121f0b780102f0235ab3d5f26441cbb3f090e9 (diff) | |
download | dexon-0x-contracts-895a9093aa5f204f9f7ad0fedb2934a8b6c40b17.tar.gz dexon-0x-contracts-895a9093aa5f204f9f7ad0fedb2934a8b6c40b17.tar.zst dexon-0x-contracts-895a9093aa5f204f9f7ad0fedb2934a8b6c40b17.zip |
Merge pull request #575 from 0xProject/feature/contracts/eip712-order-hash
Update order hash to match latest eip712
Diffstat (limited to 'packages/contracts/src')
3 files changed, 82 insertions, 38 deletions
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 7d8328c67..ed7f9391d 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibOrder.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibOrder.sol @@ -20,20 +20,25 @@ pragma solidity ^0.4.24; contract LibOrder { + bytes32 constant DOMAIN_SEPARATOR_SCHEMA_HASH = keccak256( + "DomainSeparator(address contract)" + ); + bytes32 constant ORDER_SCHEMA_HASH = keccak256( - "address exchangeAddress", - "address makerAddress", - "address takerAddress", - "address feeRecipientAddress", - "address senderAddress", - "uint256 makerAssetAmount", - "uint256 takerAssetAmount", - "uint256 makerFee", - "uint256 takerFee", - "uint256 expirationTimeSeconds", - "uint256 salt", - "bytes makerAssetData", - "bytes takerAssetData" + "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,", + ")" ); struct Order { @@ -71,9 +76,10 @@ contract LibOrder { // TODO: EIP712 is not finalized yet // Source: https://github.com/ethereum/EIPs/pull/712 orderHash = keccak256( + DOMAIN_SEPARATOR_SCHEMA_HASH, + keccak256(address(this)), ORDER_SCHEMA_HASH, keccak256( - address(this), order.makerAddress, order.takerAddress, order.feeRecipientAddress, @@ -84,8 +90,8 @@ contract LibOrder { order.takerFee, order.expirationTimeSeconds, order.salt, - order.makerAssetData, - order.takerAssetData + keccak256(order.makerAssetData), + keccak256(order.takerAssetData) ) ); return orderHash; diff --git a/packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol b/packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol index b8fc90af1..bd01277dd 100644 --- a/packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol +++ b/packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol @@ -69,6 +69,22 @@ contract TestLibs is return orderHash; } + function getOrderSchemaHash() + public + view + returns (bytes32) + { + return ORDER_SCHEMA_HASH; + } + + function getDomainSeparatorSchemaHash() + public + view + returns (bytes32) + { + return DOMAIN_SEPARATOR_SCHEMA_HASH; + } + function publicAddFillResults(FillResults memory totalFillResults, FillResults memory singleFillResults) public pure diff --git a/packages/contracts/src/utils/order_utils.ts b/packages/contracts/src/utils/order_utils.ts index 9542a3858..78ac934a1 100644 --- a/packages/contracts/src/utils/order_utils.ts +++ b/packages/contracts/src/utils/order_utils.ts @@ -1,11 +1,9 @@ import { Order, SignedOrder, UnsignedOrder } from '@0xproject/types'; 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 { CancelOrder, MatchOrder, SignatureType } from './types'; +import { CancelOrder, MatchOrder, OrderStruct, SignedOrder, UnsignedOrder } from './types'; export const orderUtils = { createFill: (signedOrder: SignedOrder, takerAssetFillAmount?: BigNumber) => { @@ -40,24 +38,41 @@ export const orderUtils = { }; return orderStruct; }, - getOrderHashBuff(order: SignedOrder | UnsignedOrder): Buffer { + 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; + }, + getOrderSchemaHex(): string { const orderSchemaHashBuff = crypto.solSHA3([ - 'address exchangeAddress', - 'address makerAddress', - 'address takerAddress', - 'address feeRecipientAddress', - 'address senderAddress', - 'uint256 makerAssetAmount', - 'uint256 takerAssetAmount', - 'uint256 makerFee', - 'uint256 takerFee', - 'uint256 expirationTimeSeconds', - 'uint256 salt', - 'bytes makerAssetData', - 'bytes takerAssetData', + '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; + }, + getOrderHashBuff(order: SignedOrder | UnsignedOrder): Buffer { + const makerAssetDataHash = crypto.solSHA3([ethUtil.toBuffer(order.makerAssetData)]); + const takerAssetDataHash = crypto.solSHA3([ethUtil.toBuffer(order.takerAssetData)]); + const orderParamsHashBuff = crypto.solSHA3([ - order.exchangeAddress, order.makerAddress, order.takerAddress, order.feeRecipientAddress, @@ -68,12 +83,19 @@ export const orderUtils = { order.takerFee, order.expirationTimeSeconds, order.salt, - ethUtil.toBuffer(order.makerAssetData), - ethUtil.toBuffer(order.takerAssetData), + makerAssetDataHash, + takerAssetDataHash, ]); - const orderSchemaHashHex = `0x${orderSchemaHashBuff.toString('hex')}`; const orderParamsHashHex = `0x${orderParamsHashBuff.toString('hex')}`; - const orderHashBuff = crypto.solSHA3([new BigNumber(orderSchemaHashHex), new BigNumber(orderParamsHashHex)]); + const orderSchemaHashHex = orderUtils.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), + ]); return orderHashBuff; }, getOrderHashHex(order: SignedOrder | UnsignedOrder): string { |