Refactor EIP712 contracts to reduce code duplication.

This commit is contained in:
Lawrence Forman
2019-03-21 13:49:12 -04:00
committed by Amir Bandeali
parent 4bbaa6b41c
commit 64b4158bad
11 changed files with 212 additions and 127 deletions

View File

@@ -38,16 +38,12 @@ contract LibConstants {
bytes public ZRX_ASSET_DATA;
// The chain ID of the network this contract is deployed on.
uint256 public CHAIN_ID;
/// @param zrxAssetData Asset data for ZRX token. Used for fee transfers.
/// @param chainId Chain ID of the network this contract is deployed on.
constructor (bytes memory zrxAssetData, uint256 chainId)
constructor (bytes memory zrxAssetData)
public
{
ZRX_ASSET_DATA = zrxAssetData;
CHAIN_ID = chainId;
}
}
// solhint-enable max-line-length

View File

@@ -18,20 +18,8 @@
pragma solidity ^0.5.5;
import "./LibConstants.sol";
contract LibEIP712 is
LibConstants
{
// EIP191 header for EIP712 prefix
string constant internal EIP191_HEADER = "\x19\x01";
// 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";
contract LibEIP712 {
// Hash of the EIP712 Domain Separator Schema
bytes32 constant internal EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH = keccak256(abi.encodePacked(
@@ -43,32 +31,66 @@ contract LibEIP712 is
")"
));
// 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_DOMAIN_HASH;
bytes32 public EIP712_EXCHANGE_DOMAIN_HASH;
constructor ()
// Chain ID of the network this contract is deployed on.
// solhint-disable-next-line var-name-mixedcase
uint256 internal CHAIN_ID;
/// @param chainId Chain ID of the network this contract is deployed on.
constructor (uint256 chainId)
public
{
EIP712_DOMAIN_HASH = keccak256(abi.encodePacked(
EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH,
keccak256(bytes(EIP712_DOMAIN_NAME)),
keccak256(bytes(EIP712_DOMAIN_VERSION)),
CHAIN_ID,
uint256(address(this))
));
CHAIN_ID = chainId;
EIP712_EXCHANGE_DOMAIN_HASH = hashEIP712Domain(
EIP712_EXCHANGE_DOMAIN_NAME,
EIP712_EXCHANGE_DOMAIN_VERSION,
address(this)
);
}
/// @dev Calculates EIP712 encoding for a hash struct in this EIP712 Domain.
/// @param hashStruct The EIP712 hash struct.
/// @return EIP712 hash applied to this EIP712 Domain.
function hashEIP712Message(bytes32 hashStruct)
/// @dev Calculates a EIP712 domain separator.
/// @param name The EIP712 domain name.
/// @param version The EIP712 domain version.
/// @param verifyingContract The EIP712 verifying contract.
/// @return EIP712 domain separator.
function hashEIP712Domain(
string memory name,
string memory version,
address verifyingContract
)
internal
view
returns (bytes32 result)
{
bytes32 eip712DomainHash = EIP712_DOMAIN_HASH;
return keccak256(abi.encodePacked(
EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH,
keccak256(bytes(name)),
keccak256(bytes(version)),
CHAIN_ID,
uint256(verifyingContract)
));
}
/// @dev Calculates EIP712 encoding for a hash struct with a given domain hash.
/// @param eip712DomainHash Hash of the domain domain separator data, computed
/// with getDomainHash().
/// @param hashStruct The EIP712 hash struct.
/// @return EIP712 hash applied to the given EIP712 Domain.
function hashEIP712Message(bytes32 eip712DomainHash, bytes32 hashStruct)
internal
pure
returns (bytes32 result)
{
// Assembly for more efficient computing:
// keccak256(abi.encodePacked(
// EIP191_HEADER,

View File

@@ -56,16 +56,16 @@ contract LibOrder is
// solhint-disable max-line-length
struct Order {
address makerAddress; // Address that created the order.
address takerAddress; // Address that is allowed to fill the order. If set to 0, any address is allowed to fill the order.
address feeRecipientAddress; // Address that will recieve fees when order is filled.
address makerAddress; // Address that created the order.
address takerAddress; // Address that is allowed to fill the order. If set to 0, any address is allowed to fill the order.
address feeRecipientAddress; // Address that will recieve fees when order is filled.
address senderAddress; // Address that is allowed to call Exchange contract methods that affect this order. If set to 0, any address is allowed to call these methods.
uint256 makerAssetAmount; // Amount of makerAsset being offered by maker. Must be greater than 0.
uint256 takerAssetAmount; // Amount of takerAsset being bid on by maker. Must be greater than 0.
uint256 makerAssetAmount; // Amount of makerAsset being offered by maker. Must be greater than 0.
uint256 takerAssetAmount; // Amount of takerAsset being bid on by maker. Must be greater than 0.
uint256 makerFee; // Amount of ZRX paid to feeRecipient by maker when order is filled. If set to 0, no transfer of ZRX from maker to feeRecipient will be attempted.
uint256 takerFee; // Amount of ZRX paid to feeRecipient by taker when order is filled. If set to 0, no transfer of ZRX from taker to feeRecipient will be attempted.
uint256 expirationTimeSeconds; // Timestamp in seconds at which order expires.
uint256 salt; // Arbitrary number to facilitate uniqueness of the order's hash.
uint256 expirationTimeSeconds; // Timestamp in seconds at which order expires.
uint256 salt; // Arbitrary number to facilitate uniqueness of the order's hash.
bytes makerAssetData; // Encoded data that can be decoded by a specified proxy contract when transferring makerAsset. The last byte references the id of this proxy.
bytes takerAssetData; // Encoded data that can be decoded by a specified proxy contract when transferring takerAsset. The last byte references the id of this proxy.
}
@@ -85,7 +85,7 @@ contract LibOrder is
view
returns (bytes32 orderHash)
{
orderHash = hashEIP712Message(hashOrder(order));
orderHash = hashEIP712Message(EIP712_EXCHANGE_DOMAIN_HASH, hashOrder(order));
return orderHash;
}
@@ -128,13 +128,13 @@ contract LibOrder is
let temp1 := mload(pos1)
let temp2 := mload(pos2)
let temp3 := mload(pos3)
// Hash in place
mstore(pos1, schemaHash)
mstore(pos2, makerAssetDataHash)
mstore(pos3, takerAssetDataHash)
result := keccak256(pos1, 416)
// Restore
mstore(pos1, temp1)
mstore(pos2, temp2)