Skip to content

Instantly share code, notes, and snippets.

@JTraversa
Last active March 6, 2021 01:14
Show Gist options
  • Save JTraversa/30469db5f39c7e3cee3fc4a778ca0094 to your computer and use it in GitHub Desktop.
Save JTraversa/30469db5f39c7e3cee3fc4a778ca0094 to your computer and use it in GitHub Desktop.
Sig test
pragma solidity ^0.5.9;
pragma experimental ABIEncoderV2;
contract Swivel {
struct RPCSig{
uint8 v;
bytes32 r;
bytes32 s;
}
struct EIP712Domain {
string name;
string version;
uint256 chainId;
address verifyingContract;
}
struct Order {
bytes32 key;
address maker;
address underlying;
bool floating;
uint256 principal;
uint256 interest;
uint256 duration;
uint256 expiry;
}
constructor () public {
DOMAIN_SEPARATOR = hashDomain(EIP712Domain({
name: "Swivel Finance",
version: '1.0.0',
chainId: 4,
verifyingContract: address(this)
}));
}
bytes32 DOMAIN_SEPARATOR;
// Offer + EIP Domain Hash Schema
bytes32 constant EIP712DOMAIN_TYPEHASH = keccak256(
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
);
bytes32 constant OFFER_TYPEHASH = keccak256(
"Order(bytes32 key,address maker,address underlying,bool floating,uint256 principal,uint256 interest,uint256 duration,uint256 expiry)"
);
function hashDomain(EIP712Domain memory eip712Domain) internal pure returns (bytes32) {
return keccak256(abi.encode(
EIP712DOMAIN_TYPEHASH,
keccak256(bytes(eip712Domain.name)),
keccak256(bytes(eip712Domain.version)),
eip712Domain.chainId,
eip712Domain.verifyingContract
));
}
function hashOrder(Order memory _order)private pure returns(bytes32){
return keccak256(abi.encode(
OFFER_TYPEHASH,
_order.key,
_order.maker,
_order.underlying,
_order.floating,
_order.principal,
_order.interest,
_order.duration,
_order.expiry
));
}
function signatureTest(Order memory _order, RPCSig memory _signature) public returns(bool) {
// Validate offer signature & ensure it was created by maker
require(_order.maker == ecrecover(
keccak256(abi.encodePacked(
"\x19\x01",
DOMAIN_SEPARATOR,
hashOrder(_order)
)),
_signature.v,
_signature.r,
_signature.s), "Invalid Signature");
return (true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment