SignatureChecker.sol 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 Variant of {isValidSignatureNow} that takes a signature in calldata
  38. */
  39. function isValidSignatureNowCalldata(
  40. address signer,
  41. bytes32 hash,
  42. bytes calldata signature
  43. ) internal view returns (bool) {
  44. if (signer.code.length == 0) {
  45. (address recovered, ECDSA.RecoverError err, ) = ECDSA.tryRecoverCalldata(hash, signature);
  46. return err == ECDSA.RecoverError.NoError && recovered == signer;
  47. } else {
  48. return isValidERC1271SignatureNow(signer, hash, signature);
  49. }
  50. }
  51. /**
  52. * @dev Checks if a signature is valid for a given signer and data hash. The signature is validated
  53. * against the signer smart contract using ERC-1271.
  54. *
  55. * NOTE: Unlike ECDSA signatures, contract signatures are revocable, and the outcome of this function can thus
  56. * change through time. It could return true at block N and false at block N+1 (or the opposite).
  57. */
  58. function isValidERC1271SignatureNow(
  59. address signer,
  60. bytes32 hash,
  61. bytes memory signature
  62. ) internal view returns (bool result) {
  63. bytes4 selector = IERC1271.isValidSignature.selector;
  64. uint256 length = signature.length;
  65. assembly ("memory-safe") {
  66. // Encoded calldata is :
  67. // [ 0x00 - 0x03 ] <selector>
  68. // [ 0x04 - 0x23 ] <hash>
  69. // [ 0x24 - 0x44 ] <signature offset> (0x40)
  70. // [ 0x44 - 0x64 ] <signature length>
  71. // [ 0x64 - ... ] <signature data>
  72. let ptr := mload(0x40)
  73. mstore(ptr, selector)
  74. mstore(add(ptr, 0x04), hash)
  75. mstore(add(ptr, 0x24), 0x40)
  76. mcopy(add(ptr, 0x44), signature, add(length, 0x20))
  77. let success := staticcall(gas(), signer, ptr, add(length, 0x64), 0, 0x20)
  78. result := and(success, and(gt(returndatasize(), 0x19), eq(mload(0x00), selector)))
  79. }
  80. }
  81. /**
  82. * @dev Verifies a signature for a given ERC-7913 signer and hash.
  83. *
  84. * The signer is a `bytes` object that is the concatenation of an address and optionally a key:
  85. * `verifier || key`. A signer must be at least 20 bytes long.
  86. *
  87. * Verification is done as follows:
  88. *
  89. * * If `signer.length < 20`: verification fails
  90. * * If `signer.length == 20`: verification is done using {isValidSignatureNow}
  91. * * Otherwise: verification is done using {IERC7913SignatureVerifier}
  92. *
  93. * NOTE: Unlike ECDSA signatures, contract signatures are revocable, and the outcome of this function can thus
  94. * change through time. It could return true at block N and false at block N+1 (or the opposite).
  95. */
  96. function isValidSignatureNow(
  97. bytes memory signer,
  98. bytes32 hash,
  99. bytes memory signature
  100. ) internal view returns (bool) {
  101. if (signer.length < 20) {
  102. return false;
  103. } else if (signer.length == 20) {
  104. return isValidSignatureNow(address(bytes20(signer)), hash, signature);
  105. } else {
  106. (bool success, bytes memory result) = address(bytes20(signer)).staticcall(
  107. abi.encodeCall(IERC7913SignatureVerifier.verify, (signer.slice(20), hash, signature))
  108. );
  109. return (success &&
  110. result.length >= 32 &&
  111. abi.decode(result, (bytes32)) == bytes32(IERC7913SignatureVerifier.verify.selector));
  112. }
  113. }
  114. /**
  115. * @dev Verifies multiple ERC-7913 `signatures` for a given `hash` using a set of `signers`.
  116. * Returns `false` if the number of signers and signatures is not the same.
  117. *
  118. * The signers should be ordered by their `keccak256` hash to ensure efficient duplication check. Unordered
  119. * signers are supported, but the uniqueness check will be more expensive.
  120. *
  121. * NOTE: Unlike ECDSA signatures, contract signatures are revocable, and the outcome of this function can thus
  122. * change through time. It could return true at block N and false at block N+1 (or the opposite).
  123. */
  124. function areValidSignaturesNow(
  125. bytes32 hash,
  126. bytes[] memory signers,
  127. bytes[] memory signatures
  128. ) internal view returns (bool) {
  129. if (signers.length != signatures.length) return false;
  130. bytes32 lastId = bytes32(0);
  131. for (uint256 i = 0; i < signers.length; ++i) {
  132. bytes memory signer = signers[i];
  133. // If one of the signatures is invalid, reject the batch
  134. if (!isValidSignatureNow(signer, hash, signatures[i])) return false;
  135. bytes32 id = keccak256(signer);
  136. // If the current signer ID is greater than all previous IDs, then this is a new signer.
  137. if (lastId < id) {
  138. lastId = id;
  139. } else {
  140. // If this signer id is not greater than all the previous ones, verify that it is not a duplicate of a previous one
  141. // This loop is never executed if the signers are ordered by id.
  142. for (uint256 j = 0; j < i; ++j) {
  143. if (id == keccak256(signers[j])) return false;
  144. }
  145. }
  146. }
  147. return true;
  148. }
  149. }