69 lines
2.2 KiB
Solidity
69 lines
2.2 KiB
Solidity
|
pragma solidity ^0.5.0;
|
||
|
|
||
|
/**
|
||
|
* @title Elliptic curve signature operations
|
||
|
* @dev Based on https://gist.github.com/axic/5b33912c6f61ae6fd96d6c4a47afde6d
|
||
|
* TODO Remove this library once solidity supports passing a signature to ecrecover.
|
||
|
* See https://github.com/ethereum/solidity/issues/864
|
||
|
* Source: https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/ECRecovery.sol
|
||
|
*/
|
||
|
library 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 _signature bytes signature, the signature is generated using web3.eth.sign()
|
||
|
*/
|
||
|
function recover(bytes32 _hash, bytes memory _signature)
|
||
|
internal
|
||
|
pure
|
||
|
returns (address)
|
||
|
{
|
||
|
bytes32 r;
|
||
|
bytes32 s;
|
||
|
uint8 v;
|
||
|
|
||
|
// Check the signature length
|
||
|
if (_signature.length != 65) {
|
||
|
return (address(0));
|
||
|
}
|
||
|
|
||
|
// Divide the signature in r, s and v variables
|
||
|
// ecrecover takes the signature parameters, and the only way to get them
|
||
|
// currently is to use assembly.
|
||
|
// solium-disable-next-line security/no-inline-assembly
|
||
|
assembly {
|
||
|
r := mload(add(_signature, 32))
|
||
|
s := mload(add(_signature, 64))
|
||
|
v := byte(0, mload(add(_signature, 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 {
|
||
|
// solium-disable-next-line arg-overflow
|
||
|
return ecrecover(_hash, v, r, s);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @dev prefix a bytes32 value with "\x19Ethereum Signed Message:" and hash the result
|
||
|
*/
|
||
|
function toEthSignedMessageHash(bytes32 _hash)
|
||
|
internal
|
||
|
pure
|
||
|
returns (bytes32)
|
||
|
{
|
||
|
// 32 is the length in bytes of hash,
|
||
|
// enforced by the type signature above
|
||
|
return keccak256(
|
||
|
abi.encodePacked("\x19Ethereum Signed Message:\n32", _hash)
|
||
|
);
|
||
|
}
|
||
|
}
|