Created
December 13, 2018 05:47
-
-
Save amiller/91fa78b6d66043eea74fbf13abb8ab32 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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