From 74a9a135648f0ad307ee98dbaf3cea8518bcd3a2 Mon Sep 17 00:00:00 2001 From: Lawrence Forman Date: Fri, 22 Mar 2019 15:43:17 -0400 Subject: [PATCH] Unpin `@0x/contracts-exchange` dependency in `/contracts/coordinator`. Split up EIP712 constants and functionality in `/contracts/exchange-libs` across 3, modular contracts. Make coordinator inherit from the modular EIP712 contracts in `@0x\contracts-exchange`. --- .../contracts/src/libs/LibEIP712Domain.sol | 14 ++++- .../exchange-libs/contracts/src/LibEIP712.sol | 16 ------ .../contracts/src/LibEIP712ExchangeDomain.sol | 55 +++++++++++++++++++ .../src/LibEIP712ExchangeDomainConstants.sol | 29 ++++++++++ .../exchange-libs/contracts/src/LibOrder.sol | 6 +- .../contracts/src/MixinTransactions.sol | 26 +-------- 6 files changed, 102 insertions(+), 44 deletions(-) create mode 100644 contracts/exchange-libs/contracts/src/LibEIP712ExchangeDomain.sol create mode 100644 contracts/exchange-libs/contracts/src/LibEIP712ExchangeDomainConstants.sol diff --git a/contracts/coordinator/contracts/src/libs/LibEIP712Domain.sol b/contracts/coordinator/contracts/src/libs/LibEIP712Domain.sol index a17452c25d..d8f7f50271 100644 --- a/contracts/coordinator/contracts/src/libs/LibEIP712Domain.sol +++ b/contracts/coordinator/contracts/src/libs/LibEIP712Domain.sol @@ -19,24 +19,29 @@ pragma solidity ^0.5.5; import "@0x/contracts-exchange-libs/contracts/src/LibEIP712.sol"; +import "@0x/contracts-exchange-libs/contracts/src/LibEIP712ExchangeDomainConstants.sol"; import "./LibConstants.sol"; // solhint-disable var-name-mixedcase contract LibEIP712Domain is LibConstants, - LibEIP712 + LibEIP712, + LibEIP712ExchangeDomainConstants { // EIP712 Domain Name value for the Coordinator string constant internal EIP712_COORDINATOR_DOMAIN_NAME = "0x Protocol Coordinator"; // EIP712 Domain Version value for the Coordinator - string constant internal EIP712_COORDINATOR_DOMAIN_VERSION = "1.0.0"; + string constant internal EIP712_COORDINATOR_DOMAIN_VERSION = "2.0.0"; // Hash of the EIP712 Domain Separator data for the Coordinator bytes32 public EIP712_COORDINATOR_DOMAIN_HASH; + // Hash of the EIP712 Domain Separator data for the Exchange + bytes32 public EIP712_EXCHANGE_DOMAIN_HASH; + constructor () public { @@ -45,6 +50,11 @@ contract LibEIP712Domain is EIP712_COORDINATOR_DOMAIN_VERSION, address(this) ); + EIP712_EXCHANGE_DOMAIN_HASH = hashEIP712Domain( + EIP712_EXCHANGE_DOMAIN_NAME, + EIP712_EXCHANGE_DOMAIN_VERSION, + address(this) + ); } /// @dev Calculates EIP712 encoding for a hash struct in the EIP712 domain diff --git a/contracts/exchange-libs/contracts/src/LibEIP712.sol b/contracts/exchange-libs/contracts/src/LibEIP712.sol index 9d83cc7530..4e4928f10b 100644 --- a/contracts/exchange-libs/contracts/src/LibEIP712.sol +++ b/contracts/exchange-libs/contracts/src/LibEIP712.sol @@ -31,17 +31,6 @@ contract LibEIP712 { ")" )); - // EIP712 Exchange Domain Name value - string constant internal EIP712_EXCHANGE_DOMAIN_NAME = "0x Protocol"; - - // EIP712 Exchange Domain Version value - string constant internal EIP712_EXCHANGE_DOMAIN_VERSION = "3.0.0"; - - // Hash of the EIP712 Domain Separator data - // solhint-disable-next-line var-name-mixedcase - bytes32 public EIP712_EXCHANGE_DOMAIN_HASH; - - // Chain ID of the network this contract is deployed on. // solhint-disable-next-line var-name-mixedcase uint256 internal CHAIN_ID; @@ -51,11 +40,6 @@ contract LibEIP712 { public { CHAIN_ID = chainId; - EIP712_EXCHANGE_DOMAIN_HASH = hashEIP712Domain( - EIP712_EXCHANGE_DOMAIN_NAME, - EIP712_EXCHANGE_DOMAIN_VERSION, - address(this) - ); } /// @dev Calculates a EIP712 domain separator. diff --git a/contracts/exchange-libs/contracts/src/LibEIP712ExchangeDomain.sol b/contracts/exchange-libs/contracts/src/LibEIP712ExchangeDomain.sol new file mode 100644 index 0000000000..219d41a35c --- /dev/null +++ b/contracts/exchange-libs/contracts/src/LibEIP712ExchangeDomain.sol @@ -0,0 +1,55 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity ^0.5.5; + +import "./LibEIP712.sol"; +import "./LibEIP712ExchangeDomainConstants.sol"; + + +contract LibEIP712ExchangeDomain is + LibEIP712, + LibEIP712ExchangeDomainConstants +{ + // Hash of the EIP712 Domain Separator data + // solhint-disable-next-line var-name-mixedcase + bytes32 internal EIP712_EXCHANGE_DOMAIN_HASH; + + constructor () + public + { + EIP712_EXCHANGE_DOMAIN_HASH = hashEIP712Domain( + EIP712_EXCHANGE_DOMAIN_NAME, + EIP712_EXCHANGE_DOMAIN_VERSION, + address(this) + ); + } + + + /// @dev Calculates EIP712 encoding for a hash struct in the EIP712 domain + /// of the Exchange contract. + /// @param hashStruct The EIP712 hash struct. + /// @return EIP712 hash applied to the Exchange EIP712 Domain. + function hashEIP712ExchangeMessage(bytes32 hashStruct) + internal + view + returns (bytes32 result) + { + return hashEIP712Message(EIP712_EXCHANGE_DOMAIN_HASH, hashStruct); + } +} diff --git a/contracts/exchange-libs/contracts/src/LibEIP712ExchangeDomainConstants.sol b/contracts/exchange-libs/contracts/src/LibEIP712ExchangeDomainConstants.sol new file mode 100644 index 0000000000..6ff616c7d9 --- /dev/null +++ b/contracts/exchange-libs/contracts/src/LibEIP712ExchangeDomainConstants.sol @@ -0,0 +1,29 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity ^0.5.5; + + +contract LibEIP712ExchangeDomainConstants { + + // EIP712 Exchange Domain Name value + string constant internal EIP712_EXCHANGE_DOMAIN_NAME = "0x Protocol"; + + // EIP712 Exchange Domain Version value + string constant internal EIP712_EXCHANGE_DOMAIN_VERSION = "3.0.0"; +} diff --git a/contracts/exchange-libs/contracts/src/LibOrder.sol b/contracts/exchange-libs/contracts/src/LibOrder.sol index 8fbfe879db..a0ca1abd18 100644 --- a/contracts/exchange-libs/contracts/src/LibOrder.sol +++ b/contracts/exchange-libs/contracts/src/LibOrder.sol @@ -18,11 +18,11 @@ pragma solidity ^0.5.5; -import "./LibEIP712.sol"; +import "./LibEIP712ExchangeDomain.sol"; contract LibOrder is - LibEIP712 + LibEIP712ExchangeDomain { // Hash for the EIP712 Order Schema bytes32 constant internal EIP712_ORDER_SCHEMA_HASH = keccak256(abi.encodePacked( @@ -85,7 +85,7 @@ contract LibOrder is view returns (bytes32 orderHash) { - orderHash = hashEIP712Message(EIP712_EXCHANGE_DOMAIN_HASH, hashOrder(order)); + orderHash = hashEIP712ExchangeMessage(hashOrder(order)); return orderHash; } diff --git a/contracts/exchange/contracts/src/MixinTransactions.sol b/contracts/exchange/contracts/src/MixinTransactions.sol index 3491fc55f9..dc050a8bd9 100644 --- a/contracts/exchange/contracts/src/MixinTransactions.sol +++ b/contracts/exchange/contracts/src/MixinTransactions.sol @@ -18,28 +18,18 @@ pragma solidity ^0.5.5; -import "@0x/contracts-exchange-libs/contracts/src/LibEIP712.sol"; +import "@0x/contracts-exchange-libs/contracts/src/LibEIP712ExchangeDomain.sol"; import "@0x/contracts-exchange-libs/contracts/src/LibExchangeErrors.sol"; import "./mixins/MSignatureValidator.sol"; import "./mixins/MTransactions.sol"; contract MixinTransactions is - LibEIP712, + LibEIP712ExchangeDomain, MSignatureValidator, MTransactions { - // EIP712 Domain Name value - string constant internal EIP712_DOMAIN_NAME = "0x Protocol"; - - // EIP712 Domain Version value - string constant internal EIP712_DOMAIN_VERSION = "3.0.0"; - - // Hash of the EIP712 Domain Separator data - // solhint-disable-next-line var-name-mixedcase - bytes32 public EIP712_DOMAIN_HASH; - // Mapping of transaction hash => executed // This prevents transactions from being executed more than once. mapping (bytes32 => bool) public transactions; @@ -47,16 +37,6 @@ contract MixinTransactions is // Address of current transaction signer address public currentContextAddress; - constructor () - public - { - EIP712_DOMAIN_HASH = hashEIP712Domain( - EIP712_DOMAIN_NAME, - EIP712_DOMAIN_VERSION, - address(this) - ); - } - /// @dev Executes an exchange method call in the context of signer. /// @param salt Arbitrary number to ensure uniqueness of transaction hash. /// @param signerAddress Address of transaction signer. @@ -77,7 +57,7 @@ contract MixinTransactions is ); bytes32 transactionHash = hashEIP712Message( - EIP712_DOMAIN_HASH, + EIP712_EXCHANGE_DOMAIN_HASH, hashZeroExTransaction( salt, signerAddress,