Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
Update Order Hash Algo to be EIP712 Compliant (#276)
Browse files Browse the repository at this point in the history
* FIrst stab

* Write unit test

* Complete updating order hash generation

* Build in eip712

* Update core wrapper

* Add additional comments

* Fix comment and formatting knits

* Fix compilation

* Standardize functions
  • Loading branch information
felix2feng committed Nov 9, 2018
1 parent 863da89 commit 78078e5
Show file tree
Hide file tree
Showing 10 changed files with 417 additions and 113 deletions.
117 changes: 117 additions & 0 deletions contracts/core/lib/EIP712Library.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
Copyright 2018 Set Labs Inc.
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.4.25;


/**
* @title EIP712Library
* @author Set Protocol
*
* The EIP712 Library contains functions for hashing EIP712 Messages
*
*/
library EIP712Library {
/* ============ Constants ============ */

// EIP191 header for EIP712 prefix
string constant internal EIP191_HEADER = "\x19\x01";

// EIP712 Domain Name value
string constant internal EIP712_DOMAIN_NAME = "Set Protocol";

// EIP712 Domain Version value
string constant internal EIP712_DOMAIN_VERSION = "1";

// Hash of the EIP712 Domain Separator Schema
bytes32 constant internal EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH = keccak256(
abi.encodePacked(
"EIP712Domain(",
"string name,",
"string version,",
")"
)
);

bytes32 constant internal EIP712_DOMAIN_HASH = keccak256(
abi.encodePacked(
EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH,
keccak256(
bytes(EIP712_DOMAIN_NAME)
),
keccak256(
bytes(EIP712_DOMAIN_VERSION)
)
)
);

/* ============ Internal Functions ============ */

/**
* 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
pure
returns (bytes32)
{
bytes32 eip712DomainHash = EIP712_DOMAIN_HASH;

bytes32 result;

assembly {
// Load free memory pointer
let memPtr := mload(64)

mstore(memPtr, 0x1901000000000000000000000000000000000000000000000000000000000000) // EIP191 header
mstore(add(memPtr, 2), eip712DomainHash) // EIP712 domain hash
mstore(add(memPtr, 34), hashStruct) // Hash of struct

// Compute hash
result := keccak256(memPtr, 66)
}
return result;
}

/**
* Returns the EIP712 Domain Hash
*
* @return bytes32 Hash of the EIP712 Set Protocol Domain
*/
function getEIP712DomainHash()
internal
view
returns (bytes32)
{
return EIP712_DOMAIN_HASH;
}
/**
* Returns the EIP712 Domain Separator Schema Hash
*
* @return bytes32 Hash of the EIP712 Domain Separator Schema
*/
function getEIP712DomainSeparatorSchemaHash()
internal
view
returns (bytes32)
{
return EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH;
}
}
71 changes: 67 additions & 4 deletions contracts/core/lib/OrderLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
pragma solidity 0.4.25;

import { SafeMath } from "openzeppelin-solidity/contracts/math/SafeMath.sol";
import { EIP712Library } from "./EIP712Library.sol";


/**
Expand All @@ -30,6 +31,29 @@ import { SafeMath } from "openzeppelin-solidity/contracts/math/SafeMath.sol";
library OrderLibrary {
using SafeMath for uint256;

/* ============ Constants ============ */

// Hash for the EIP712 Order Schema
bytes32 constant public EIP712_ORDER_SCHEMA_HASH = keccak256(
abi.encodePacked(
"IssuanceOrder(",
"address setAddress",
"address makerAddress",
"address makerToken",
"address relayerAddress",
"address relayerToken",
"uint256 quantity",
"uint256 makerTokenAmount",
"uint256 expiration",
"uint256 makerRelayerFee",
"uint256 takerRelayerFee",
"uint256 salt",
"address[] requiredComponents",
"uint256[] requiredComponentAmounts",
")"
)
);

/* ============ Structs ============ */

/**
Expand Down Expand Up @@ -70,27 +94,57 @@ library OrderLibrary {
/* ============ Internal Functions ============ */

/**
* Create hash of order parameters
* Calculates Keccak-256 hash of the order with the EIP712 Domain.
*
* @param _addresses [setAddress, makerAddress, makerToken, relayerAddress, relayerToken]
* @param _values [quantity, makerTokenAmount, expiration, makerRelayerFee, takerRelayerFee, salt]
* @param _requiredComponents Components to be acquired by exchange order
* @param _requiredComponentAmounts Amounts of each component to be acquired by exchange order
* @return bytes32 EIP712 Hash of IssuanceOrder applied to the EIP712 Domain
*/
function generateOrderHash(
address[5] _addresses,
uint256[6] _values,
address[] _requiredComponents,
uint256[] _requiredComponentAmounts
)
internal
pure
returns (bytes32)
{
return EIP712Library.hashEIP712Message(
hashOrder(
_addresses,
_values,
_requiredComponents,
_requiredComponentAmounts
)
);
}

/**
* Create an EIP712 hash of order parameters
*
* @param _addresses [setAddress, makerAddress, makerToken, relayerAddress, relayerToken]
* @param _values [quantity, makerTokenAmount, expiration, makerRelayerFee, takerRelayerFee, salt]
* @param _requiredComponents Components to be acquired by exchange order
* @param _requiredComponentAmounts Amounts of each component to be acquired by exchange order
* @return bytes32 Hash of IssuanceOrder
* @return bytes32 EIP712 Hash of IssuanceOrder
*/
function generateOrderHash(
function hashOrder(
address[5] _addresses,
uint256[6] _values,
address[] _requiredComponents,
uint256[] _requiredComponentAmounts
)
internal
private
pure
returns(bytes32)
{
// Hash the order parameters
return keccak256(
abi.encodePacked(
EIP712_ORDER_SCHEMA_HASH, // EIP 712 order schema hash
_addresses[0], // setAddress
_addresses[1], // makerAddress
_addresses[2], // makerToken
Expand Down Expand Up @@ -229,4 +283,13 @@ library OrderLibrary {

return _principal.mul(_numerator).div(_denominator);
}

/**
* Returns the EIP712 Issuance Order Schema Hash
*
* @return bytes32 Hash of the Issuance Order Schema
*/
function getEIP712OrderSchemaHash() internal view returns (bytes32) {
return EIP712_ORDER_SCHEMA_HASH;
}
}
33 changes: 33 additions & 0 deletions contracts/mocks/core/lib/EIP712LibraryMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
pragma solidity 0.4.25;
pragma experimental "ABIEncoderV2";

import { EIP712Library } from "../../../core/lib/EIP712Library.sol";

// Mock contract implementation of EIP712Library functions
contract EIP712LibraryMock {
function testGetEIP712DomainHash()
public
view
returns (bytes32)
{
return EIP712Library.getEIP712DomainHash();
}

function testGetEIP712DomainSeparatorSchemaHash()
public
view
returns (bytes32)
{
return EIP712Library.getEIP712DomainSeparatorSchemaHash();
}

function testHashEIP712Message(
bytes32 hashStruct
)
public
view
returns (bytes32)
{
return EIP712Library.hashEIP712Message(hashStruct);
}
}
4 changes: 4 additions & 0 deletions contracts/mocks/core/lib/OrderLibraryMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { OrderLibrary } from "../../../core/lib/OrderLibrary.sol";

// Mock contract implementation of OrderLibrary functions
contract OrderLibraryMock {
function testGetEIP712OrderSchemaHash() public view returns (bytes32) {
return OrderLibrary.getEIP712OrderSchemaHash();
}

function testGenerateOrderHash(
address[5] _addresses,
uint[6] _values,
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@
"husky": "^0.14.3",
"lint-staged": "^7.2.0",
"module-alias": "^2.1.0",
"set-protocol-utils": "^1.0.0-beta.13",
"openzeppelin-solidity": "^2.0.0",
"set-protocol-utils": "^1.0.0-beta.17",
"sol-trace-set": "^0.0.1",
"solium": "^1.1.7",
"tiny-promisify": "^1.0.0",
"truffle-hdwallet-provider": "^1.0.0-web3one.0",
"tslint-eslint-rules": "^5.3.1",
"web3": "1.0.0-beta.36",
"web3-utils": "1.0.0-beta.36",
"openzeppelin-solidity": "^2.0.0"
"web3-utils": "1.0.0-beta.36"
},
"husky": {
"hooks": {
Expand Down
81 changes: 81 additions & 0 deletions test/contracts/core/lib/eip712Library.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require('module-alias/register');

import * as chai from 'chai';
import { Bytes } from 'set-protocol-utils';

import { EIP712LibraryMockContract } from '@utils/contracts';
import { CoreWrapper } from '@utils/coreWrapper';
import { Blockchain } from '@utils/blockchain';
import { BigNumberSetup } from '@utils/bigNumberSetup';
import ChaiSetup from '@utils/chaiSetup';
import { getWeb3 } from '@utils/web3Helper';

BigNumberSetup.configure();
ChaiSetup.configure();
const web3 = getWeb3();
const { expect } = chai;
const blockchain = new Blockchain(web3);


contract('EIP712Library', accounts => {
const [
ownerAccount,
] = accounts;

let eip712Lib: EIP712LibraryMockContract;

const coreWrapper = new CoreWrapper(ownerAccount, ownerAccount);

beforeEach(async () => {
await blockchain.saveSnapshotAsync();

eip712Lib = await coreWrapper.deployMockEIP712LibAsync();
});

afterEach(async () => {
await blockchain.revertAsync();
});

describe('#getEIP712DomainHash', async () => {
async function subject(): Promise<string> {
return eip712Lib.testGetEIP712DomainHash.callAsync();
}

it('should return the correct hash', async () => {
const returnedHash = await subject();

const expectedEIP712Hash: Bytes = '0xa8dcc602486c63f3c678c9b3c5d615c4d6ab4b7d51868af6881272b5d8bb31ff';
expect(returnedHash).to.equal(expectedEIP712Hash);
});
});

describe('#getEIP712DomainSeparatorSchemaHash', async () => {
async function subject(): Promise<string> {
return eip712Lib.testGetEIP712DomainSeparatorSchemaHash.callAsync();
}

it('should return the correct hash', async () => {
const returnedHash = await subject();

const expectedEIP712Hash: Bytes = '0x4c2212af4ffd7e170315f531795cee6c22f874d8f5fab37dfa8ed65e616773d2';
expect(returnedHash).to.equal(expectedEIP712Hash);
});
});

describe('#hashEIP712Message', async () => {
const subjectHashStruct: Bytes = '0x0000000000000000000000000000000000000000000000000000000000000000';

async function subject(): Promise<string> {
return eip712Lib.testHashEIP712Message.callAsync(
subjectHashStruct,
);
}

it('should return the correct hash', async () => {
const returnedHash = await subject();

const expectedHash = '0x5686079a65f95107943e531f6f7f755044148600233246c75fdce6e59c85cae5';
expect(returnedHash).to.equal(expectedHash);
});
});
});
Loading

0 comments on commit 78078e5

Please sign in to comment.