SignatureChecker.sol 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0) (utils/cryptography/SignatureChecker.sol)
  3. pragma solidity ^0.8.0;
  4. import "./ECDSA.sol";
  5. import "../../interfaces/IERC1271.sol";
  6. /**
  7. * @dev Signature verification helper that can be used instead of `ECDSA.recover` to seamlessly support both ECDSA
  8. * signatures from externally owned accounts (EOAs) as well as ERC1271 signatures from smart contract wallets like
  9. * Argent and Gnosis Safe.
  10. *
  11. * _Available since v4.1._
  12. */
  13. library SignatureChecker {
  14. /**
  15. * @dev Checks if a signature is valid for a given signer and data hash. If the signer is a smart contract, the
  16. * signature is validated against that smart contract using ERC1271, otherwise it's validated using `ECDSA.recover`.
  17. *
  18. * NOTE: Unlike ECDSA signatures, contract signatures are revocable, and the outcome of this function can thus
  19. * change through time. It could return true at block N and false at block N+1 (or the opposite).
  20. */
  21. function isValidSignatureNow(address signer, bytes32 hash, bytes memory signature) internal view returns (bool) {
  22. (address recovered, ECDSA.RecoverError error) = ECDSA.tryRecover(hash, signature);
  23. if (error == ECDSA.RecoverError.NoError && recovered == signer) {
  24. return true;
  25. }
  26. (bool success, bytes memory result) = signer.staticcall(
  27. abi.encodeWithSelector(IERC1271.isValidSignature.selector, hash, signature)
  28. );
  29. return (success &&
  30. result.length == 32 &&
  31. abi.decode(result, (bytes32)) == bytes32(IERC1271.isValidSignature.selector));
  32. }
  33. }