This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Order Hash Algo to be EIP712 Compliant (#276)
* 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
1 parent
863da89
commit 78078e5
Showing
10 changed files
with
417 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.