ECDSA.sol 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. pragma solidity ^0.5.0;
  2. /**
  3. * @dev Elliptic Curve Digital Signature Algorithm (ECDSA) operations.
  4. *
  5. * These functions can be used to verify that a message was signed by the holder
  6. * of the private keys of a given address.
  7. */
  8. library ECDSA {
  9. /**
  10. * @dev Returns the address that signed a hashed message (`hash`) with
  11. * `signature`. This address can then be used for verification purposes.
  12. *
  13. * The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
  14. * this function rejects them by requiring the `s` value to be in the lower
  15. * half order, and the `v` value to be either 27 or 28.
  16. *
  17. * (.note) This call _does not revert_ if the signature is invalid, or
  18. * if the signer is otherwise unable to be retrieved. In those scenarios,
  19. * the zero address is returned.
  20. *
  21. * (.warning) `hash` _must_ be the result of a hash operation for the
  22. * verification to be secure: it is possible to craft signatures that
  23. * recover to arbitrary addresses for non-hashed data. A safe way to ensure
  24. * this is by receiving a hash of the original message (which may otherwise)
  25. * be too long), and then calling `toEthSignedMessageHash` on it.
  26. */
  27. function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
  28. // Check the signature length
  29. if (signature.length != 65) {
  30. return (address(0));
  31. }
  32. // Divide the signature in r, s and v variables
  33. bytes32 r;
  34. bytes32 s;
  35. uint8 v;
  36. // ecrecover takes the signature parameters, and the only way to get them
  37. // currently is to use assembly.
  38. // solhint-disable-next-line no-inline-assembly
  39. assembly {
  40. r := mload(add(signature, 0x20))
  41. s := mload(add(signature, 0x40))
  42. v := byte(0, mload(add(signature, 0x60)))
  43. }
  44. // EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
  45. // unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
  46. // the valid range for s in (281): 0 < s < secp256k1n ÷ 2 + 1, and for v in (282): v ∈ {27, 28}. Most
  47. // signatures from current libraries generate a unique signature with an s-value in the lower half order.
  48. //
  49. // If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
  50. // with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
  51. // vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
  52. // these malleable signatures as well.
  53. if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
  54. return address(0);
  55. }
  56. if (v != 27 && v != 28) {
  57. return address(0);
  58. }
  59. // If the signature is valid (and not malleable), return the signer address
  60. return ecrecover(hash, v, r, s);
  61. }
  62. /**
  63. * @dev Returns an Ethereum Signed Message, created from a `hash`. This
  64. * replicates the behavior of the
  65. * [`eth_sign`](https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign)
  66. * JSON-RPC method.
  67. *
  68. * See `recover`.
  69. */
  70. function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) {
  71. // 32 is the length in bytes of hash,
  72. // enforced by the type signature above
  73. return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
  74. }
  75. }