Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Rename hashing functions in LibOrder and LibZeroExTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
abandeali1 committed Aug 14, 2019
1 parent 2da996f commit 1dae1d2
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 53 deletions.
26 changes: 13 additions & 13 deletions contracts/exchange-libs/contracts/src/LibOrder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ library LibOrder {
0xf80322eb8376aafb64eadf8f0d7623f22130fd9491a221e902b713cb984a7534;

// A valid order remains fillable until it is expired, fully filled, or cancelled.
// An order's state is unaffected by external factors, like account balances.
// An order's status is unaffected by external factors, like account balances.
enum OrderStatus {
INVALID, // Default value
INVALID_MAKER_ASSET_AMOUNT, // Order does not have a valid maker asset amount
Expand All @@ -67,42 +67,42 @@ library LibOrder {
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 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 makerFee; // Fee paid to feeRecipient by maker when order is filled.
uint256 takerFee; // Fee paid to feeRecipient by taker when order is filled.
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 leading bytes4 references the id of the asset proxy.
bytes takerAssetData; // Encoded data that can be decoded by a specified proxy contract when transferring takerAsset. The leading bytes4 references the id of the asset proxy.
bytes makerFeeAssetData; // Encoded data that can be decoded by a specified proxy contract when transferring makerAsset fees. The leading bytes4 references the id of the asset proxy.
bytes takerFeeAssetData; // Encoded data that can be decoded by a specified proxy contract when transferring takerAsset fees. The leading bytes4 references the id of the asset proxy.
bytes makerFeeAssetData; // Encoded data that can be decoded by a specified proxy contract when transferring makerFeeAsset. The leading bytes4 references the id of the asset proxy.
bytes takerFeeAssetData; // Encoded data that can be decoded by a specified proxy contract when transferring takerFeeAsset. The leading bytes4 references the id of the asset proxy.
}
// solhint-enable max-line-length

struct OrderInfo {
uint8 orderStatus; // Status that describes order's validity and fillability.
bytes32 orderHash; // EIP712 hash of the order (see LibOrder.getOrderHash).
bytes32 orderHash; // EIP712 typed data hash of the order (see LibOrder.getTypedDataHash).
uint256 orderTakerAssetFilledAmount; // Amount of order that has already been filled.
}

/// @dev Calculates Keccak-256 hash of the order.
/// @dev Calculates the EIP712 typed data hash of an order with a given domain separator.
/// @param order The order structure.
/// @return Keccak-256 EIP712 hash of the order.
function getOrderHash(Order memory order, bytes32 eip712ExchangeDomainHash)
/// @return EIP712 typed data hash of the order.
function getTypedDataHash(Order memory order, bytes32 eip712ExchangeDomainHash)
internal
pure
returns (bytes32 orderHash)
{
orderHash = LibEIP712.hashEIP712Message(
eip712ExchangeDomainHash,
order.hashOrder()
order.getStructHash()
);
return orderHash;
}

/// @dev Calculates EIP712 hash of the order.
/// @dev Calculates EIP712 hash of the order struct.
/// @param order The order structure.
/// @return EIP712 hash of the order.
function hashOrder(Order memory order)
/// @return EIP712 hash of the order struct.
function getStructHash(Order memory order)
internal
pure
returns (bytes32 result)
Expand Down
18 changes: 9 additions & 9 deletions contracts/exchange-libs/contracts/src/LibZeroExTransaction.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,26 @@ library LibZeroExTransaction {
bytes data; // AbiV2 encoded calldata.
}

/// @dev Calculates the EIP712 hash of a 0x transaction using the domain separator of the Exchange contract.
/// @param transaction 0x transaction containing salt, signerAddress, and data.
/// @return EIP712 hash of the transaction with the domain separator of this contract.
function getTransactionHash(ZeroExTransaction memory transaction, bytes32 eip712ExchangeDomainHash)
/// @dev Calculates the EIP712 typed data hash of a transaction with a given domain separator.
/// @param transaction 0x transaction structure.
/// @return EIP712 typed data hash of the transaction.
function getTypedDataHash(ZeroExTransaction memory transaction, bytes32 eip712ExchangeDomainHash)
internal
pure
returns (bytes32 transactionHash)
{
// Hash the transaction with the domain separator of the Exchange contract.
transactionHash = LibEIP712.hashEIP712Message(
eip712ExchangeDomainHash,
transaction.hashZeroExTransaction()
transaction.getStructHash()
);
return transactionHash;
}

/// @dev Calculates EIP712 hash of the 0x transaction with no domain separator.
/// @param transaction 0x transaction containing salt, signerAddress, and data.
/// @return EIP712 hash of the transaction with no domain separator.
function hashZeroExTransaction(ZeroExTransaction memory transaction)
/// @dev Calculates EIP712 hash of the 0x transaction struct.
/// @param transaction 0x transaction structure.
/// @return EIP712 hash of the transaction struct.
function getStructHash(ZeroExTransaction memory transaction)
internal
pure
returns (bytes32 result)
Expand Down
8 changes: 4 additions & 4 deletions contracts/exchange-libs/contracts/test/TestLibOrder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ import "../src/LibOrder.sol";

contract TestLibOrder {

function getOrderHash(LibOrder.Order memory order, bytes32 eip712ExchangeDomainHash)
function getTypedDataHash(LibOrder.Order memory order, bytes32 eip712ExchangeDomainHash)
public
pure
returns (bytes32 orderHash)
{
orderHash = LibOrder.getOrderHash(order, eip712ExchangeDomainHash);
orderHash = LibOrder.getTypedDataHash(order, eip712ExchangeDomainHash);
return orderHash;
}

function hashOrder(LibOrder.Order memory order)
function getStructHash(LibOrder.Order memory order)
public
pure
returns (bytes32 result)
{
result = LibOrder.hashOrder(order);
result = LibOrder.getStructHash(order);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ import "../src/LibZeroExTransaction.sol";

contract TestLibZeroExTransaction {

function getZeroExTransactionHash(LibZeroExTransaction.ZeroExTransaction memory transaction, bytes32 eip712ExchangeDomainHash)
function getTypedDataHash(LibZeroExTransaction.ZeroExTransaction memory transaction, bytes32 eip712ExchangeDomainHash)
public
pure
returns (bytes32 transactionHash)
{
transactionHash = LibZeroExTransaction.getTransactionHash(transaction, eip712ExchangeDomainHash);
transactionHash = LibZeroExTransaction.getTypedDataHash(transaction, eip712ExchangeDomainHash);
return transactionHash;
}

function hashZeroExTransaction(LibZeroExTransaction.ZeroExTransaction memory transaction)
function getStructHash(LibZeroExTransaction.ZeroExTransaction memory transaction)
public
pure
returns (bytes32 result)
{
result = LibZeroExTransaction.hashZeroExTransaction(transaction);
result = LibZeroExTransaction.getStructHash(transaction);
return result;
}
}
2 changes: 1 addition & 1 deletion contracts/exchange-libs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"lint-contracts": "solhint -c ../.solhint.json contracts/**/**/**/**/*.sol"
},
"config": {
"abis": "./generated-artifacts/@(LibEIP712ExchangeDomain|LibExchangeRichErrors|LibExchangeSelectors|LibFillResults|LibMath|LibMathRichErrors|LibOrder|LibZeroExTransaction|TestLibEIP712ExchangeDomain|TestLibFillResults|TestLibMath|TestLibOrder|TestLibZeroExTransaction).json",
"abis": "./generated-artifacts/@(LibEIP712ExchangeDomain|LibExchangeRichErrors|LibFillResults|LibMath|LibMathRichErrors|LibOrder|LibZeroExTransaction|TestLibEIP712ExchangeDomain|TestLibFillResults|TestLibMath|TestLibOrder|TestLibZeroExTransaction).json",
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually."
},
"repository": {
Expand Down
2 changes: 0 additions & 2 deletions contracts/exchange-libs/src/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { ContractArtifact } from 'ethereum-types';

import * as LibEIP712ExchangeDomain from '../generated-artifacts/LibEIP712ExchangeDomain.json';
import * as LibExchangeRichErrors from '../generated-artifacts/LibExchangeRichErrors.json';
import * as LibExchangeSelectors from '../generated-artifacts/LibExchangeSelectors.json';
import * as LibFillResults from '../generated-artifacts/LibFillResults.json';
import * as LibMath from '../generated-artifacts/LibMath.json';
import * as LibMathRichErrors from '../generated-artifacts/LibMathRichErrors.json';
Expand All @@ -21,7 +20,6 @@ import * as TestLibZeroExTransaction from '../generated-artifacts/TestLibZeroExT
export const artifacts = {
LibEIP712ExchangeDomain: LibEIP712ExchangeDomain as ContractArtifact,
LibExchangeRichErrors: LibExchangeRichErrors as ContractArtifact,
LibExchangeSelectors: LibExchangeSelectors as ContractArtifact,
LibFillResults: LibFillResults as ContractArtifact,
LibMath: LibMath as ContractArtifact,
LibMathRichErrors: LibMathRichErrors as ContractArtifact,
Expand Down
1 change: 0 additions & 1 deletion contracts/exchange-libs/src/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
export * from '../generated-wrappers/lib_e_i_p712_exchange_domain';
export * from '../generated-wrappers/lib_exchange_rich_errors';
export * from '../generated-wrappers/lib_exchange_selectors';
export * from '../generated-wrappers/lib_fill_results';
export * from '../generated-wrappers/lib_math';
export * from '../generated-wrappers/lib_math_rich_errors';
Expand Down
6 changes: 3 additions & 3 deletions contracts/exchange-libs/test/lib_order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ blockchainTests('LibOrder', env => {
version: constants.EIP712_DOMAIN_VERSION,
}),
);
const orderHashHex = await libOrderContract.getOrderHash.callAsync(order, domainHash);
const orderHashHex = await libOrderContract.getTypedDataHash.callAsync(order, domainHash);
expect(orderHashUtils.getOrderHashHex(order)).to.be.equal(orderHashHex);
});
it('orderHash should differ if the domain hash is different', async () => {
Expand All @@ -64,8 +64,8 @@ blockchainTests('LibOrder', env => {
chainId: 1337,
}),
);
const orderHashHex1 = await libOrderContract.getOrderHash.callAsync(order, domainHash1);
const orderHashHex2 = await libOrderContract.getOrderHash.callAsync(order, domainHash2);
const orderHashHex1 = await libOrderContract.getTypedDataHash.callAsync(order, domainHash1);
const orderHashHex2 = await libOrderContract.getTypedDataHash.callAsync(order, domainHash2);
expect(orderHashHex1).to.be.not.equal(orderHashHex2);
});
});
Expand Down
6 changes: 3 additions & 3 deletions contracts/exchange-libs/test/lib_zero_ex_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ blockchainTests('LibZeroExTransaction', env => {
version: constants.EIP712_DOMAIN_VERSION,
}),
);
const orderHashHex = await libZeroExTransactionContract.getZeroExTransactionHash.callAsync(
const orderHashHex = await libZeroExTransactionContract.getTypedDataHash.callAsync(
zeroExTransaction,
domainHash,
);
Expand All @@ -60,11 +60,11 @@ blockchainTests('LibZeroExTransaction', env => {
chainId: 1337,
}),
);
const transactionHashHex1 = await libZeroExTransactionContract.getZeroExTransactionHash.callAsync(
const transactionHashHex1 = await libZeroExTransactionContract.getTypedDataHash.callAsync(
zeroExTransaction,
domainHash1,
);
const transactionHashHex2 = await libZeroExTransactionContract.getZeroExTransactionHash.callAsync(
const transactionHashHex2 = await libZeroExTransactionContract.getTypedDataHash.callAsync(
zeroExTransaction,
domainHash2,
);
Expand Down
1 change: 0 additions & 1 deletion contracts/exchange-libs/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"files": [
"generated-artifacts/LibEIP712ExchangeDomain.json",
"generated-artifacts/LibExchangeRichErrors.json",
"generated-artifacts/LibExchangeSelectors.json",
"generated-artifacts/LibFillResults.json",
"generated-artifacts/LibMath.json",
"generated-artifacts/LibMathRichErrors.json",
Expand Down
2 changes: 1 addition & 1 deletion contracts/exchange/contracts/src/MixinExchangeCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ contract MixinExchangeCore is
returns (LibOrder.OrderInfo memory orderInfo)
{
// Compute the order hash
orderInfo.orderHash = order.getOrderHash(EIP712_EXCHANGE_DOMAIN_HASH);
orderInfo.orderHash = order.getTypedDataHash(EIP712_EXCHANGE_DOMAIN_HASH);

// Fetch filled amount
orderInfo.orderTakerAssetFilledAmount = filled[orderInfo.orderHash];
Expand Down
4 changes: 2 additions & 2 deletions contracts/exchange/contracts/src/MixinSignatureValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ contract MixinSignatureValidator is
view
returns (bool isValid)
{
bytes32 orderHash = order.getOrderHash(EIP712_EXCHANGE_DOMAIN_HASH);
bytes32 orderHash = order.getTypedDataHash(EIP712_EXCHANGE_DOMAIN_HASH);
return _isValidOrderWithHashSignature(
order,
orderHash,
Expand All @@ -157,7 +157,7 @@ contract MixinSignatureValidator is
view
returns (bool isValid)
{
bytes32 transactionHash = transaction.getTransactionHash(EIP712_EXCHANGE_DOMAIN_HASH);
bytes32 transactionHash = transaction.getTypedDataHash(EIP712_EXCHANGE_DOMAIN_HASH);
isValid = _isValidTransactionWithHashSignature(
transaction,
transactionHash,
Expand Down
2 changes: 1 addition & 1 deletion contracts/exchange/contracts/src/MixinTransactions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ contract MixinTransactions is
internal
returns (bytes memory)
{
bytes32 transactionHash = transaction.getTransactionHash(EIP712_EXCHANGE_DOMAIN_HASH);
bytes32 transactionHash = transaction.getTypedDataHash(EIP712_EXCHANGE_DOMAIN_HASH);

// Check transaction is not expired
// solhint-disable-next-line not-rely-on-time
Expand Down
6 changes: 3 additions & 3 deletions contracts/exchange/contracts/src/interfaces/IExchangeCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ contract IExchangeCore {
bytes makerAssetData, // Encoded data specific to makerAsset.
bytes takerAssetData, // Encoded data specific to takerAsset.
bytes makerFeeAssetData, // Encoded data specific to makerFeeAsset.
bytes takerFeeAssetData, // Encoded data specific to takerFeeAsset.
bytes takerFeeAssetData, // Encoded data specific to takerFeeAsset.
uint256 makerAssetFilledAmount, // Amount of makerAsset sold by maker and bought by taker.
uint256 takerAssetFilledAmount, // Amount of takerAsset sold by taker and bought by maker.
uint256 makerFeePaid, // Amount of makerFeeAssetData paid to feeRecipient by maker.
uint256 takerFeePaid, // Amount of takerFeeAssetData paid to feeRecipient by taker.
address takerAddress, // Address that filled the order.
address senderAddress, // Address that called the Exchange contract (msg.sender).
bytes32 indexed orderHash // EIP712 hash of order (see LibOrder.getOrderHash).
bytes32 indexed orderHash // EIP712 hash of order (see LibOrder.getTypedDataHash).
);

// Cancel event is emitted whenever an individual order is cancelled.
event Cancel(
address indexed makerAddress, // Address that created the order.
address indexed feeRecipientAddress, // Address that would have recieved fees if order was filled.
address senderAddress, // Address that called the Exchange contract (msg.sender).
bytes32 indexed orderHash, // EIP712 hash of order (see LibOrder.getOrderHash).
bytes32 indexed orderHash, // EIP712 hash of order (see LibOrder.getTypedDataHash).
bytes makerAssetData, // Encoded data specific to makerAsset.
bytes takerAssetData // Encoded data specific to takerAsset.
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract TestExchangeInternals is
)
public
{
filled[LibOrder.getOrderHash(order, EIP712_EXCHANGE_DOMAIN_HASH)] = orderTakerAssetFilledAmount;
filled[LibOrder.getTypedDataHash(order, EIP712_EXCHANGE_DOMAIN_HASH)] = orderTakerAssetFilledAmount;
_updateFilledState(
order,
takerAddress,
Expand Down
4 changes: 2 additions & 2 deletions contracts/exchange/contracts/test/TestValidatorWallet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ contract TestValidatorWallet is
// Use the Exchange to calculate the hash of the order and assert
// that it matches the one we extracted previously.
require(
LibOrder.getOrderHash(order, _exchange.EIP712_EXCHANGE_DOMAIN_HASH()) == hash,
LibOrder.getTypedDataHash(order, _exchange.EIP712_EXCHANGE_DOMAIN_HASH()) == hash,
"UNEXPECTED_ORDER_HASH"
);
} else {
Expand All @@ -238,7 +238,7 @@ contract TestValidatorWallet is
// Use the Exchange to calculate the hash of the transaction and assert
// that it matches the one we extracted previously.
require(
LibZeroExTransaction.getTransactionHash(transaction, _exchange.EIP712_EXCHANGE_DOMAIN_HASH()) == hash,
LibZeroExTransaction.getTypedDataHash(transaction, _exchange.EIP712_EXCHANGE_DOMAIN_HASH()) == hash,
"UNEXPECTED_TRANSACTION_HASH"
);
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/exchange/contracts/test/TestWrapperFunctions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ contract TestWrapperFunctions is
orderInfo.orderTakerAssetFilledAmount = uint128(order.salt);
// High byte of `order.salt` is the `orderStatus`.
orderInfo.orderStatus = uint8(order.salt >> 248) % (MAX_ORDER_STATUS + 1);
orderInfo.orderHash = _getOrderHash(order);
orderInfo.orderHash = _getTypedDataHash(order);
}

/// @dev Overridden to log arguments, be deterministic, and revert with certain inputs.
Expand Down Expand Up @@ -111,7 +111,7 @@ contract TestWrapperFunctions is
}

/// @dev Simplified order hashing.
function _getOrderHash(LibOrder.Order memory order)
function _getTypedDataHash(LibOrder.Order memory order)
internal
pure
returns (bytes32 hash)
Expand Down

0 comments on commit 1dae1d2

Please sign in to comment.