Skip to content

Instantly share code, notes, and snippets.

@amiller
Created December 13, 2018 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amiller/91fa78b6d66043eea74fbf13abb8ab32 to your computer and use it in GitHub Desktop.
Save amiller/91fa78b6d66043eea74fbf13abb8ab32 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.22;
contract ECVerify {
function ecrecovery(bytes32 hash, bytes sig) public pure returns (address) {
bytes32 r;
bytes32 s;
uint8 v;
if (sig.length != 65) {
return 0;
}
assembly {
r := mload(add(sig, 32))
s := mload(add(sig, 64))
v := and(mload(add(sig, 65)), 255)
}
// https://github.com/ethereum/go-ethereum/issues/2053
if (v < 27) v += 27;
if (v != 27 && v != 28) return 0;
return ecrecover(hash, v, r, s);
}
function verifySignature(bytes32 hash, bytes sig, address signer) public pure returns (bool) {
return signer == ecrecovery(hash, sig);
}
}
contract Test is ECVerify {
address alice = 0x1B326Ad348e19ecFd1406C43D3bF7a95547AC55c;
function hashHello() public pure returns(bytes32) {
return keccak256("hello");
}
function checkSignature(bytes sig) public view returns(bool) {
bytes32 hash = hashHello();
return verifySignature( hash, sig, alice );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment