-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from danfinlay/add-entrypoints
Add entrypoints and 1271 support
- Loading branch information
Showing
14 changed files
with
34,719 additions
and
732 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
node_modules/ | ||
.DS_Store | ||
cache/ | ||
artifacts/ |
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 @@ | ||
v16.14.2 |
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 |
---|---|---|
@@ -1,11 +1,35 @@ | ||
#!/usr/bin/env node | ||
|
||
const yargs = require('yargs'); | ||
const typesToCode = require('./index'); | ||
const path = require('path'); | ||
|
||
const targetPath = path.join(process.cwd(), process.argv[2]); | ||
const types = require(targetPath); | ||
const shouldLog = process.argv[3] === 'log'; | ||
const argv = yargs | ||
.option('input', { | ||
alias: 'i', | ||
describe: 'Input file path', | ||
demandOption: true, | ||
type: 'string', | ||
}) | ||
.option('entryPoints', { | ||
alias: 'e', | ||
describe: 'Type names to be used as entry points', | ||
demandOption: true, | ||
array: true, | ||
type: 'string', | ||
}) | ||
.option('log', { | ||
alias: 'l', | ||
describe: 'Enable logging', | ||
type: 'boolean', | ||
}) | ||
.help() | ||
.alias('help', 'h') | ||
.argv; | ||
|
||
console.log(typesToCode.generateSolidity(types, shouldLog)); | ||
const targetPath = argv.input; | ||
const types = require(targetPath); | ||
const entryPoints = argv.entryPoints; | ||
const shouldLog = argv.log; | ||
|
||
console.log(typesToCode.generateSolidity(types, shouldLog, entryPoints)); |
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,43 @@ | ||
pragma solidity 0.8.19; | ||
|
||
// SPDX-License-Identifier: MIT | ||
|
||
contract ECRecovery { | ||
/** | ||
* @dev Recover signer address from a message by using their signature | ||
* @param hash bytes32 message, the hash is the signed message. What is recovered is the signer address. | ||
* @param sig bytes signature, the signature is generated using web3.eth.sign() | ||
*/ | ||
function recover(bytes32 hash, bytes memory sig) | ||
internal | ||
pure | ||
returns (address) | ||
{ | ||
bytes32 r; | ||
bytes32 s; | ||
uint8 v; | ||
|
||
//Check the signature length | ||
if (sig.length != 65) { | ||
return (address(0)); | ||
} | ||
|
||
// Divide the signature in r, s and v variables | ||
assembly { | ||
r := mload(add(sig, 32)) | ||
s := mload(add(sig, 64)) | ||
v := byte(0, mload(add(sig, 96))) | ||
} | ||
// Version of signature should be 27 or 28, but 0 and 1 are also possible versions | ||
if (v < 27) { | ||
v += 27; | ||
} | ||
|
||
// If the version is correct return the signer address | ||
if (v != 27 && v != 28) { | ||
return (address(0)); | ||
} else { | ||
return ecrecover(hash, v, r, s); | ||
} | ||
} | ||
} |
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,31 @@ | ||
pragma solidity 0.8.19; | ||
import "./ECRecovery.sol"; | ||
|
||
//SPDX-License-Identifier: MIT | ||
|
||
contract EIP1271 is ECRecovery { | ||
mapping(address => bool) isOwner; | ||
|
||
constructor() { | ||
isOwner[msg.sender] = true; | ||
} | ||
|
||
function addOwner(address _owner) public { | ||
isOwner[_owner] = true; | ||
} | ||
|
||
/** | ||
* @notice Verifies that the signer is the owner of the signing contract. | ||
*/ | ||
function isValidSignature(bytes32 _hash, bytes calldata _signature) | ||
external | ||
view | ||
returns (bytes4) | ||
{ | ||
if (isOwner[recover(_hash, _signature)]) { | ||
return 0x1626ba7e; | ||
} else { | ||
return 0xffffffff; | ||
} | ||
} | ||
} |
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,144 @@ | ||
pragma solidity ^0.8.13; | ||
// SPDX-License-Identifier: MIT | ||
|
||
|
||
abstract contract ERC1271Contract { | ||
/** | ||
* @dev Should return whether the signature provided is valid for the provided hash | ||
* @param _hash Hash of the data to be signed | ||
* @param _signature Signature byte array associated with _hash | ||
* | ||
* MUST return the bytes4 magic value 0x1626ba7e when function passes. | ||
* MUST NOT modify state (using STATICCALL for solc < 0.5, view modifier for solc > 0.5) | ||
* MUST allow external calls | ||
*/ | ||
function isValidSignature( | ||
bytes32 _hash, | ||
bytes memory _signature) | ||
public | ||
view | ||
virtual | ||
returns (bytes4 magicValue); | ||
} | ||
|
||
abstract contract EIP712Decoder { | ||
function getDomainHash () public view virtual returns (bytes32); | ||
|
||
struct EIP712Domain { | ||
string name; | ||
string version; | ||
uint256 chainId; | ||
address verifyingContract; | ||
} | ||
|
||
bytes32 constant public EIP712DOMAIN_TYPEHASH = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"); | ||
|
||
|
||
struct SignedPerson { | ||
bytes signature; | ||
address signer; | ||
Person message; | ||
} | ||
|
||
|
||
struct Person { | ||
string name; | ||
uint256 age; | ||
} | ||
|
||
bytes32 constant public PERSON_TYPEHASH = keccak256("Person(string name,uint256 age)"); | ||
|
||
|
||
/** | ||
* @dev Recover signer address from a message by using their signature | ||
* @param hash bytes32 message, the hash is the signed message. What is recovered is the signer address. | ||
* @param sig bytes signature, the signature is generated using web3.eth.sign() | ||
*/ | ||
function recover(bytes32 hash, bytes memory sig) internal pure returns (address) { | ||
bytes32 r; | ||
bytes32 s; | ||
uint8 v; | ||
|
||
// Check the signature length | ||
if (sig.length != 65) { | ||
return (address(0)); | ||
} | ||
|
||
// Divide the signature in r, s and v variables | ||
assembly { | ||
r := mload(add(sig, 32)) | ||
s := mload(add(sig, 64)) | ||
v := byte(0, mload(add(sig, 96))) | ||
} | ||
// Version of signature should be 27 or 28, but 0 and 1 are also possible versions | ||
if (v < 27) { | ||
v += 27; | ||
} | ||
|
||
// If the version is correct return the signer address | ||
if (v != 27 && v != 28) { | ||
return (address(0)); | ||
} else { | ||
return ecrecover(hash, v, r, s); | ||
} | ||
} | ||
|
||
function GET_EIP712DOMAIN_PACKETHASH (EIP712Domain memory _input) public pure returns (bytes32) { | ||
bytes memory encoded = GET_EIP712DOMAIN_PACKET(_input); | ||
return keccak256(encoded); | ||
} | ||
|
||
function GET_EIP712DOMAIN_PACKET (EIP712Domain memory _input) public pure returns (bytes memory) { | ||
bytes memory encoded = abi.encode( | ||
EIP712DOMAIN_TYPEHASH, | ||
keccak256(bytes(_input.name)), | ||
keccak256(bytes(_input.version)), | ||
_input.chainId, | ||
_input.verifyingContract | ||
); | ||
return encoded; | ||
} | ||
|
||
|
||
function GET_PERSON_PACKETHASH (Person memory _input) public pure returns (bytes32) { | ||
bytes memory encoded = GET_PERSON_PACKET(_input); | ||
return keccak256(encoded); | ||
} | ||
|
||
function GET_PERSON_PACKET (Person memory _input) public pure returns (bytes memory) { | ||
bytes memory encoded = abi.encode( | ||
PERSON_TYPEHASH, | ||
keccak256(bytes(_input.name)), | ||
_input.age | ||
); | ||
return encoded; | ||
} | ||
|
||
|
||
function verifySignedPerson(SignedPerson memory _input) public view returns (address) { | ||
bytes32 packetHash = GET_PERSON_PACKETHASH(_input.message); | ||
bytes32 digest = keccak256( | ||
abi.encodePacked( | ||
"\x19\x01", | ||
getDomainHash(), | ||
packetHash | ||
) | ||
); | ||
|
||
if (_input.signer == 0x0000000000000000000000000000000000000000) { | ||
address recoveredSigner = recover( | ||
digest, | ||
_input.signature | ||
); | ||
return recoveredSigner; | ||
} else { | ||
// EIP-1271 signature verification | ||
bytes4 result = ERC1271Contract(_input.signer).isValidSignature(digest, _input.signature); | ||
require(result == 0x1626ba7e, "INVALID_SIGNATURE"); | ||
return _input.signer; | ||
} | ||
} | ||
|
||
|
||
} | ||
|
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,21 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.19; | ||
|
||
import "./EIP712Decoder.sol"; | ||
|
||
contract MockEIP712Decoder is EIP712Decoder { | ||
bytes32 domainHash; | ||
constructor(uint256 chainId) { | ||
domainHash = keccak256(abi.encode( | ||
EIP712DOMAIN_TYPEHASH, | ||
keccak256(bytes("MockEIP712Decoder")), | ||
keccak256(bytes("1")), | ||
chainId, | ||
address(this) | ||
)); | ||
} | ||
|
||
function getDomainHash () public view override returns (bytes32) { | ||
return domainHash; | ||
} | ||
} |
Oops, something went wrong.