ECDSA.sol 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (utils/cryptography/ECDSA.sol)
  3. pragma solidity ^0.8.0;
  4. import "../Strings.sol";
  5. /**
  6. * @dev Elliptic Curve Digital Signature Algorithm (ECDSA) operations.
  7. *
  8. * These functions can be used to verify that a message was signed by the holder
  9. * of the private keys of a given address.
  10. */
  11. library ECDSA {
  12. enum RecoverError {
  13. NoError,
  14. InvalidSignature,
  15. InvalidSignatureLength,
  16. InvalidSignatureS,
  17. InvalidSignatureV
  18. }
  19. function _throwError(RecoverError error) private pure {
  20. if (error == RecoverError.NoError) {
  21. return; // no error: do nothing
  22. } else if (error == RecoverError.InvalidSignature) {
  23. revert("ECDSA: invalid signature");
  24. } else if (error == RecoverError.InvalidSignatureLength) {
  25. revert("ECDSA: invalid signature length");
  26. } else if (error == RecoverError.InvalidSignatureS) {
  27. revert("ECDSA: invalid signature 's' value");
  28. } else if (error == RecoverError.InvalidSignatureV) {
  29. revert("ECDSA: invalid signature 'v' value");
  30. }
  31. }
  32. /**
  33. * @dev Returns the address that signed a hashed message (`hash`) with
  34. * `signature` or error string. This address can then be used for verification purposes.
  35. *
  36. * The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
  37. * this function rejects them by requiring the `s` value to be in the lower
  38. * half order, and the `v` value to be either 27 or 28.
  39. *
  40. * IMPORTANT: `hash` _must_ be the result of a hash operation for the
  41. * verification to be secure: it is possible to craft signatures that
  42. * recover to arbitrary addresses for non-hashed data. A safe way to ensure
  43. * this is by receiving a hash of the original message (which may otherwise
  44. * be too long), and then calling {toEthSignedMessageHash} on it.
  45. *
  46. * Documentation for signature generation:
  47. * - with https://web3js.readthedocs.io/en/v1.3.4/web3-eth-accounts.html#sign[Web3.js]
  48. * - with https://docs.ethers.io/v5/api/signer/#Signer-signMessage[ethers]
  49. *
  50. * _Available since v4.3._
  51. */
  52. function tryRecover(bytes32 hash, bytes memory signature) internal pure returns (address, RecoverError) {
  53. // Check the signature length
  54. // - case 65: r,s,v signature (standard)
  55. // - case 64: r,vs signature (cf https://eips.ethereum.org/EIPS/eip-2098) _Available since v4.1._
  56. if (signature.length == 65) {
  57. bytes32 r;
  58. bytes32 s;
  59. uint8 v;
  60. // ecrecover takes the signature parameters, and the only way to get them
  61. // currently is to use assembly.
  62. assembly {
  63. r := mload(add(signature, 0x20))
  64. s := mload(add(signature, 0x40))
  65. v := byte(0, mload(add(signature, 0x60)))
  66. }
  67. return tryRecover(hash, v, r, s);
  68. } else if (signature.length == 64) {
  69. bytes32 r;
  70. bytes32 vs;
  71. // ecrecover takes the signature parameters, and the only way to get them
  72. // currently is to use assembly.
  73. assembly {
  74. r := mload(add(signature, 0x20))
  75. vs := mload(add(signature, 0x40))
  76. }
  77. return tryRecover(hash, r, vs);
  78. } else {
  79. return (address(0), RecoverError.InvalidSignatureLength);
  80. }
  81. }
  82. /**
  83. * @dev Returns the address that signed a hashed message (`hash`) with
  84. * `signature`. This address can then be used for verification purposes.
  85. *
  86. * The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
  87. * this function rejects them by requiring the `s` value to be in the lower
  88. * half order, and the `v` value to be either 27 or 28.
  89. *
  90. * IMPORTANT: `hash` _must_ be the result of a hash operation for the
  91. * verification to be secure: it is possible to craft signatures that
  92. * recover to arbitrary addresses for non-hashed data. A safe way to ensure
  93. * this is by receiving a hash of the original message (which may otherwise
  94. * be too long), and then calling {toEthSignedMessageHash} on it.
  95. */
  96. function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
  97. (address recovered, RecoverError error) = tryRecover(hash, signature);
  98. _throwError(error);
  99. return recovered;
  100. }
  101. /**
  102. * @dev Overload of {ECDSA-tryRecover} that receives the `r` and `vs` short-signature fields separately.
  103. *
  104. * See https://eips.ethereum.org/EIPS/eip-2098[EIP-2098 short signatures]
  105. *
  106. * _Available since v4.3._
  107. */
  108. function tryRecover(
  109. bytes32 hash,
  110. bytes32 r,
  111. bytes32 vs
  112. ) internal pure returns (address, RecoverError) {
  113. bytes32 s;
  114. uint8 v;
  115. assembly {
  116. s := and(vs, 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)
  117. v := add(shr(255, vs), 27)
  118. }
  119. return tryRecover(hash, v, r, s);
  120. }
  121. /**
  122. * @dev Overload of {ECDSA-recover} that receives the `r and `vs` short-signature fields separately.
  123. *
  124. * _Available since v4.2._
  125. */
  126. function recover(
  127. bytes32 hash,
  128. bytes32 r,
  129. bytes32 vs
  130. ) internal pure returns (address) {
  131. (address recovered, RecoverError error) = tryRecover(hash, r, vs);
  132. _throwError(error);
  133. return recovered;
  134. }
  135. /**
  136. * @dev Overload of {ECDSA-tryRecover} that receives the `v`,
  137. * `r` and `s` signature fields separately.
  138. *
  139. * _Available since v4.3._
  140. */
  141. function tryRecover(
  142. bytes32 hash,
  143. uint8 v,
  144. bytes32 r,
  145. bytes32 s
  146. ) internal pure returns (address, RecoverError) {
  147. // EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
  148. // unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
  149. // the valid range for s in (301): 0 < s < secp256k1n ÷ 2 + 1, and for v in (302): v ∈ {27, 28}. Most
  150. // signatures from current libraries generate a unique signature with an s-value in the lower half order.
  151. //
  152. // If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
  153. // with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
  154. // vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
  155. // these malleable signatures as well.
  156. if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
  157. return (address(0), RecoverError.InvalidSignatureS);
  158. }
  159. if (v != 27 && v != 28) {
  160. return (address(0), RecoverError.InvalidSignatureV);
  161. }
  162. // If the signature is valid (and not malleable), return the signer address
  163. address signer = ecrecover(hash, v, r, s);
  164. if (signer == address(0)) {
  165. return (address(0), RecoverError.InvalidSignature);
  166. }
  167. return (signer, RecoverError.NoError);
  168. }
  169. /**
  170. * @dev Overload of {ECDSA-recover} that receives the `v`,
  171. * `r` and `s` signature fields separately.
  172. */
  173. function recover(
  174. bytes32 hash,
  175. uint8 v,
  176. bytes32 r,
  177. bytes32 s
  178. ) internal pure returns (address) {
  179. (address recovered, RecoverError error) = tryRecover(hash, v, r, s);
  180. _throwError(error);
  181. return recovered;
  182. }
  183. /**
  184. * @dev Returns an Ethereum Signed Message, created from a `hash`. This
  185. * produces hash corresponding to the one signed with the
  186. * https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`]
  187. * JSON-RPC method as part of EIP-191.
  188. *
  189. * See {recover}.
  190. */
  191. function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) {
  192. // 32 is the length in bytes of hash,
  193. // enforced by the type signature above
  194. return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
  195. }
  196. /**
  197. * @dev Returns an Ethereum Signed Message, created from `s`. This
  198. * produces hash corresponding to the one signed with the
  199. * https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`]
  200. * JSON-RPC method as part of EIP-191.
  201. *
  202. * See {recover}.
  203. */
  204. function toEthSignedMessageHash(bytes memory s) internal pure returns (bytes32) {
  205. return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n", Strings.toString(s.length), s));
  206. }
  207. /**
  208. * @dev Returns an Ethereum Signed Typed Data, created from a
  209. * `domainSeparator` and a `structHash`. This produces hash corresponding
  210. * to the one signed with the
  211. * https://eips.ethereum.org/EIPS/eip-712[`eth_signTypedData`]
  212. * JSON-RPC method as part of EIP-712.
  213. *
  214. * See {recover}.
  215. */
  216. function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32) {
  217. return keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
  218. }
  219. }