ECDSA.sol 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. pragma solidity ^0.5.7;
  2. /**
  3. * @title Elliptic curve signature operations
  4. * @dev Based on https://gist.github.com/axic/5b33912c6f61ae6fd96d6c4a47afde6d
  5. * TODO Remove this library once solidity supports passing a signature to ecrecover.
  6. * See https://github.com/ethereum/solidity/issues/864
  7. */
  8. library ECDSA {
  9. /**
  10. * @dev Recover signer address from a message by using their signature.
  11. * @param hash bytes32 message, the hash is the signed message. What is recovered is the signer address.
  12. * @param signature bytes signature, the signature is generated using web3.eth.sign()
  13. */
  14. function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
  15. // Check the signature length
  16. if (signature.length != 65) {
  17. return (address(0));
  18. }
  19. // Divide the signature in r, s and v variables
  20. bytes32 r;
  21. bytes32 s;
  22. uint8 v;
  23. // ecrecover takes the signature parameters, and the only way to get them
  24. // currently is to use assembly.
  25. // solhint-disable-next-line no-inline-assembly
  26. assembly {
  27. r := mload(add(signature, 0x20))
  28. s := mload(add(signature, 0x40))
  29. v := byte(0, mload(add(signature, 0x60)))
  30. }
  31. // EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
  32. // unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
  33. // the valid range for s in (281): 0 < s < secp256k1n ÷ 2 + 1, and for v in (282): v ∈ {27, 28}. Most
  34. // signatures from current libraries generate a unique signature with an s-value in the lower half order.
  35. //
  36. // If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
  37. // with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
  38. // vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
  39. // these malleable signatures as well.
  40. if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
  41. return address(0);
  42. }
  43. if (v != 27 && v != 28) {
  44. return address(0);
  45. }
  46. // If the signature is valid (and not malleable), return the signer address
  47. return ecrecover(hash, v, r, s);
  48. }
  49. /**
  50. * toEthSignedMessageHash
  51. * @dev Prefix a bytes32 value with "\x19Ethereum Signed Message:"
  52. * and hash the result.
  53. */
  54. function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) {
  55. // 32 is the length in bytes of hash,
  56. // enforced by the type signature above
  57. return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
  58. }
  59. }