From 65676c22f92996feb0e3cb98daa3d8ce7b3854e9 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Tue, 8 May 2018 15:31:56 +1000 Subject: Update order hash to match latest eip712 --- .../current/protocol/Exchange/libs/LibOrder.sol | 32 +++++++-------- .../contracts/current/test/TestLibs/TestLibs.sol | 8 ++++ packages/contracts/src/utils/order_utils.ts | 45 +++++++++++++--------- 3 files changed, 51 insertions(+), 34 deletions(-) (limited to 'packages/contracts/src') 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..34ea62ef1 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibOrder.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibOrder.sol @@ -21,19 +21,21 @@ pragma solidity ^0.4.24; contract LibOrder { 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 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,", + ")" ); struct Order { @@ -84,8 +86,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..ed0c282af 100644 --- a/packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol +++ b/packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol @@ -69,6 +69,14 @@ contract TestLibs is return orderHash; } + function publicGetOrderSchemaHash() + public + view + returns (bytes32) + { + return ORDER_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 8adc6b735..688259b6c 100644 --- a/packages/contracts/src/utils/order_utils.ts +++ b/packages/contracts/src/utils/order_utils.ts @@ -1,7 +1,5 @@ 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, OrderStruct, SignatureType, SignedOrder, UnsignedOrder } from './types'; @@ -39,22 +37,31 @@ export const orderUtils = { }; return orderStruct; }, - getOrderHashBuff(order: SignedOrder | UnsignedOrder): Buffer { + 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 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,', + ')', ]); + 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, @@ -67,11 +74,11 @@ 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 orderSchemaHashHex = orderUtils.getOrderSchemaHex(); const orderHashBuff = crypto.solSHA3([new BigNumber(orderSchemaHashHex), new BigNumber(orderParamsHashHex)]); return orderHashBuff; }, -- cgit v1.2.3 From fcf4a958c3daa4dd7911de48001a8e235c9eb5ac Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Wed, 9 May 2018 10:03:30 +1000 Subject: publicGetOrderSchemaHash -> getOrderSchemaHash --- packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/contracts/src') diff --git a/packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol b/packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol index ed0c282af..c791fa0f9 100644 --- a/packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol +++ b/packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol @@ -69,7 +69,7 @@ contract TestLibs is return orderHash; } - function publicGetOrderSchemaHash() + function getOrderSchemaHash() public view returns (bytes32) -- cgit v1.2.3 From 3cc8af819c9f62f42e7cb3d03f896b35cfc10d05 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Wed, 23 May 2018 17:27:49 -0700 Subject: Use domain separator for exchange address --- .../current/protocol/Exchange/libs/LibOrder.sol | 8 ++++++-- .../contracts/current/test/TestLibs/TestLibs.sol | 8 ++++++++ packages/contracts/src/utils/order_utils.ts | 23 ++++++++++++++++++---- 3 files changed, 33 insertions(+), 6 deletions(-) (limited to 'packages/contracts/src') 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 34ea62ef1..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,9 +20,12 @@ pragma solidity ^0.4.24; contract LibOrder { + bytes32 constant DOMAIN_SEPARATOR_SCHEMA_HASH = keccak256( + "DomainSeparator(address contract)" + ); + bytes32 constant ORDER_SCHEMA_HASH = keccak256( "Order(", - "address exchangeAddress,", "address makerAddress,", "address takerAddress,", "address feeRecipientAddress,", @@ -73,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, diff --git a/packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol b/packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol index c791fa0f9..bd01277dd 100644 --- a/packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol +++ b/packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol @@ -77,6 +77,14 @@ contract TestLibs is 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 688259b6c..969d43dd1 100644 --- a/packages/contracts/src/utils/order_utils.ts +++ b/packages/contracts/src/utils/order_utils.ts @@ -2,7 +2,7 @@ import { BigNumber } from '@0xproject/utils'; import ethUtil = require('ethereumjs-util'); import { crypto } from './crypto'; -import { CancelOrder, MatchOrder, OrderStruct, SignatureType, SignedOrder, UnsignedOrder } from './types'; +import { CancelOrder, MatchOrder, OrderStruct, SignedOrder, UnsignedOrder } from './types'; export const orderUtils = { createFill: (signedOrder: SignedOrder, takerAssetFillAmount?: BigNumber) => { @@ -37,10 +37,19 @@ export const orderUtils = { }; return orderStruct; }, + 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([ 'Order(', - 'address exchangeAddress,', 'address makerAddress,', 'address takerAddress,', 'address feeRecipientAddress,', @@ -63,7 +72,6 @@ export const orderUtils = { const takerAssetDataHash = crypto.solSHA3([ethUtil.toBuffer(order.takerAssetData)]); const orderParamsHashBuff = crypto.solSHA3([ - order.exchangeAddress, order.makerAddress, order.takerAddress, order.feeRecipientAddress, @@ -79,7 +87,14 @@ export const orderUtils = { ]); const orderParamsHashHex = `0x${orderParamsHashBuff.toString('hex')}`; const orderSchemaHashHex = orderUtils.getOrderSchemaHex(); - const orderHashBuff = crypto.solSHA3([new BigNumber(orderSchemaHashHex), new BigNumber(orderParamsHashHex)]); + 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 { -- cgit v1.2.3