SignatureChecker.sol 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0) (utils/cryptography/SignatureChecker.sol)
  3. pragma solidity ^0.8.24;
  4. import {ECDSA} from "./ECDSA.sol";
  5. import {IERC1271} from "../../interfaces/IERC1271.sol";
  6. import {IERC7913SignatureVerifier} from "../../interfaces/IERC7913.sol";
  7. import {Bytes} from "../../utils/Bytes.sol";
  8. /**
  9. * @dev Signature verification helper that can be used instead of `ECDSA.recover` to seamlessly support:
  10. *
  11. * * ECDSA signatures from externally owned accounts (EOAs)
  12. * * ERC-1271 signatures from smart contract wallets like Argent and Safe Wallet (previously Gnosis Safe)
  13. * * ERC-7913 signatures from keys that do not have an Ethereum address of their own
  14. *
  15. * See https://eips.ethereum.org/EIPS/eip-1271[ERC-1271] and https://eips.ethereum.org/EIPS/eip-7913[ERC-7913].
  16. */
  17. library SignatureChecker {
  18. using Bytes for bytes;
  19. /**
  20. * @dev Checks if a signature is valid for a given signer and data hash. If the signer has code, the
  21. * signature is validated against it using ERC-1271, otherwise it's validated using `ECDSA.recover`.
  22. *
  23. * NOTE: Unlike ECDSA signatures, contract signatures are revocable, and the outcome of this function can thus
  24. * change through time. It could return true at block N and false at block N+1 (or the opposite).
  25. *
  26. * NOTE: For an extended version of this function that supports ERC-7913 signatures, see {isValidSignatureNow-bytes-bytes32-bytes-}.
  27. */
  28. function isValidSignatureNow(address signer, bytes32 hash, bytes memory signature) internal view returns (bool) {
  29. if (signer.code.length == 0) {
  30. (address recovered, ECDSA.RecoverError err, ) = ECDSA.tryRecover(hash, signature);
  31. return err == ECDSA.RecoverError.NoError && recovered == signer;
  32. } else {
  33. return isValidERC1271SignatureNow(signer, hash, signature);
  34. }
  35. }
  36. /**
  37. * @dev Checks if a signature is valid for a given signer and data hash. The signature is validated
  38. * against the signer smart contract using ERC-1271.
  39. *
  40. * NOTE: Unlike ECDSA signatures, contract signatures are revocable, and the outcome of this function can thus
  41. * change through time. It could return true at block N and false at block N+1 (or the opposite).
  42. */
  43. function isValidERC1271SignatureNow(
  44. address signer,
  45. bytes32 hash,
  46. bytes memory signature
  47. ) internal view returns (bool) {
  48. (bool success, bytes memory result) = signer.staticcall(
  49. abi.encodeCall(IERC1271.isValidSignature, (hash, signature))
  50. );
  51. return (success &&
  52. result.length >= 32 &&
  53. abi.decode(result, (bytes32)) == bytes32(IERC1271.isValidSignature.selector));
  54. }
  55. /**
  56. * @dev Verifies a signature for a given ERC-7913 signer and hash.
  57. *
  58. * The signer is a `bytes` object that is the concatenation of an address and optionally a key:
  59. * `verifier || key`. A signer must be at least 20 bytes long.
  60. *
  61. * Verification is done as follows:
  62. *
  63. * * If `signer.length < 20`: verification fails
  64. * * If `signer.length == 20`: verification is done using {isValidSignatureNow}
  65. * * Otherwise: verification is done using {IERC7913SignatureVerifier}
  66. *
  67. * NOTE: Unlike ECDSA signatures, contract signatures are revocable, and the outcome of this function can thus
  68. * change through time. It could return true at block N and false at block N+1 (or the opposite).
  69. */
  70. function isValidSignatureNow(
  71. bytes memory signer,
  72. bytes32 hash,
  73. bytes memory signature
  74. ) internal view returns (bool) {
  75. if (signer.length < 20) {
  76. return false;
  77. } else if (signer.length == 20) {
  78. return isValidSignatureNow(address(bytes20(signer)), hash, signature);
  79. } else {
  80. (bool success, bytes memory result) = address(bytes20(signer)).staticcall(
  81. abi.encodeCall(IERC7913SignatureVerifier.verify, (signer.slice(20), hash, signature))
  82. );
  83. return (success &&
  84. result.length >= 32 &&
  85. abi.decode(result, (bytes32)) == bytes32(IERC7913SignatureVerifier.verify.selector));
  86. }
  87. }
  88. /**
  89. * @dev Verifies multiple ERC-7913 `signatures` for a given `hash` using a set of `signers`.
  90. * Returns `false` if the number of signers and signatures is not the same.
  91. *
  92. * The signers should be ordered by their `keccak256` hash to ensure efficient duplication check. Unordered
  93. * signers are supported, but the uniqueness check will be more expensive.
  94. *
  95. * NOTE: Unlike ECDSA signatures, contract signatures are revocable, and the outcome of this function can thus
  96. * change through time. It could return true at block N and false at block N+1 (or the opposite).
  97. */
  98. function areValidSignaturesNow(
  99. bytes32 hash,
  100. bytes[] memory signers,
  101. bytes[] memory signatures
  102. ) internal view returns (bool) {
  103. if (signers.length != signatures.length) return false;
  104. bytes32 lastId = bytes32(0);
  105. for (uint256 i = 0; i < signers.length; ++i) {
  106. bytes memory signer = signers[i];
  107. // If one of the signatures is invalid, reject the batch
  108. if (!isValidSignatureNow(signer, hash, signatures[i])) return false;
  109. bytes32 id = keccak256(signer);
  110. // If the current signer ID is greater than all previous IDs, then this is a new signer.
  111. if (lastId < id) {
  112. lastId = id;
  113. } else {
  114. // If this signer id is not greater than all the previous ones, verify that it is not a duplicate of a previous one
  115. // This loop is never executed if the signers are ordered by id.
  116. for (uint256 j = 0; j < i; ++j) {
  117. if (id == keccak256(signers[j])) return false;
  118. }
  119. }
  120. }
  121. return true;
  122. }
  123. }