From 560a55bfd1a05bda0d4fb4b97d76e9ea0d9301a7 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Tue, 19 Jun 2018 13:30:13 +1000 Subject: Optimize and remove redundant encodePacked --- .../current/protocol/Exchange/MixinTransactions.sol | 8 ++++---- .../current/protocol/Exchange/libs/LibEIP712.sol | 19 ++++++++++++++----- .../current/protocol/Exchange/libs/LibOrder.sol | 6 ++++-- packages/contracts/src/utils/transaction_factory.ts | 3 +-- packages/order-utils/src/eip712_utils.ts | 2 -- packages/order-utils/src/order_hash.ts | 3 --- 6 files changed, 23 insertions(+), 18 deletions(-) (limited to 'packages') diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinTransactions.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinTransactions.sol index 9bdaa0f8f..f1332363c 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/MixinTransactions.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinTransactions.sol @@ -53,14 +53,14 @@ contract MixinTransactions is /// @return EIP712 hash of the Transaction. function hashZeroExTransaction(uint256 salt, address signer, bytes data) internal - view + pure returns (bytes32) { - return keccak256(abi.encodePacked( + return keccak256(abi.encode( EIP712_ZEROEX_TRANSACTION_SCHEMA_HASH, salt, - bytes32(signer), - keccak256(abi.encodePacked(data)) + signer, + keccak256(data) )); } diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibEIP712.sol b/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibEIP712.sol index 991137f32..b983347a4 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibEIP712.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibEIP712.sol @@ -22,6 +22,12 @@ contract LibEIP712 { // EIP191 header for EIP712 prefix string constant EIP191_HEADER = "\x19\x01"; + // EIP712 Domain Name value + string constant EIP712_DOMAIN_NAME = "0x Protocol"; + + // EIP712 Domain Version value + string constant EIP712_DOMAIN_VERSION = "2"; + // Hash of the EIP712 Domain Separator Schema bytes32 public constant EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH = keccak256(abi.encodePacked( "EIP712Domain(", @@ -35,16 +41,19 @@ contract LibEIP712 { bytes32 public EIP712_DOMAIN_HASH; constructor () - public + public { - EIP712_DOMAIN_HASH = keccak256(abi.encodePacked( + EIP712_DOMAIN_HASH = keccak256(abi.encode( EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH, - keccak256(abi.encodePacked("0x Protocol")), - keccak256(abi.encodePacked("2")), - bytes32(address(this)) + keccak256(bytes(EIP712_DOMAIN_NAME)), + keccak256(bytes(EIP712_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) internal view 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 04374f1d0..b21de6927 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibOrder.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibOrder.sol @@ -99,6 +99,8 @@ contract LibOrder is pure returns (bytes32) { + // Note: changing this to abi.encode results in an error on 0.4.24 + // UnimplementedFeatureError: Variable inaccessible, too deep inside stack (17) return keccak256(abi.encodePacked( EIP712_ORDER_SCHEMA_HASH, bytes32(order.makerAddress), @@ -111,8 +113,8 @@ contract LibOrder is order.takerFee, order.expirationTimeSeconds, order.salt, - keccak256(abi.encodePacked(order.makerAssetData)), - keccak256(abi.encodePacked(order.takerAssetData)) + keccak256(order.makerAssetData), + keccak256(order.takerAssetData) )); } } diff --git a/packages/contracts/src/utils/transaction_factory.ts b/packages/contracts/src/utils/transaction_factory.ts index c0a32b83a..19ef4e1bf 100644 --- a/packages/contracts/src/utils/transaction_factory.ts +++ b/packages/contracts/src/utils/transaction_factory.ts @@ -1,4 +1,4 @@ -import { crypto, EIP712Schema, EIP712Types, EIP712Utils, generatePseudoRandomSalt } from '@0xproject/order-utils'; +import { EIP712Schema, EIP712Types, EIP712Utils, generatePseudoRandomSalt } from '@0xproject/order-utils'; import { SignatureType } from '@0xproject/types'; import * as ethUtil from 'ethereumjs-util'; @@ -24,7 +24,6 @@ export class TransactionFactory { this._signerBuff = ethUtil.privateToAddress(this._privateKey); } public newSignedTransaction(data: string, signatureType: SignatureType = SignatureType.EthSign): SignedTransaction { - const executeTransactionSchemaHashBuff = EIP712Utils.compileSchema(EIP712_ZEROEX_TRANSACTION_SCHEMA); const salt = generatePseudoRandomSalt(); const signer = `0x${this._signerBuff.toString('hex')}`; const executeTransactionData = { diff --git a/packages/order-utils/src/eip712_utils.ts b/packages/order-utils/src/eip712_utils.ts index 00edd2531..2594e6d6d 100644 --- a/packages/order-utils/src/eip712_utils.ts +++ b/packages/order-utils/src/eip712_utils.ts @@ -1,8 +1,6 @@ -import { BigNumber } from '@0xproject/utils'; import ethUtil = require('ethereumjs-util'); import * as _ from 'lodash'; -import { assetProxyUtils } from './asset_proxy_utils'; import { crypto } from './crypto'; import { EIP712Schema, EIP712Types } from './types'; diff --git a/packages/order-utils/src/order_hash.ts b/packages/order-utils/src/order_hash.ts index 074624e9e..54c500653 100644 --- a/packages/order-utils/src/order_hash.ts +++ b/packages/order-utils/src/order_hash.ts @@ -1,11 +1,8 @@ import { schemas, SchemaValidator } from '@0xproject/json-schemas'; import { Order, SignedOrder } from '@0xproject/types'; -import { BigNumber } from '@0xproject/utils'; -import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; import { assert } from './assert'; -import { crypto } from './crypto'; import { EIP712Utils } from './eip712_utils'; import { EIP712Schema, EIP712Types } from './types'; -- cgit v1.2.3