SignatureChecker.sol 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (utils/cryptography/SignatureChecker.sol)
  3. pragma solidity ^0.8.0;
  4. import "./ECDSA.sol";
  5. import "../Address.sol";
  6. import "../../interfaces/IERC1271.sol";
  7. /**
  8. * @dev Signature verification helper: Provide a single mechanism to verify both private-key (EOA) ECDSA signature and
  9. * ERC1271 contract signatures. Using this instead of ECDSA.recover in your contract will make them compatible with
  10. * smart contract wallets such as Argent and Gnosis.
  11. *
  12. * Note: unlike ECDSA signatures, contract signature's are revocable, and the outcome of this function can thus change
  13. * through time. It could return true at block N and false at block N+1 (or the opposite).
  14. *
  15. * _Available since v4.1._
  16. */
  17. library SignatureChecker {
  18. function isValidSignatureNow(
  19. address signer,
  20. bytes32 hash,
  21. bytes memory signature
  22. ) internal view returns (bool) {
  23. (address recovered, ECDSA.RecoverError error) = ECDSA.tryRecover(hash, signature);
  24. if (error == ECDSA.RecoverError.NoError && recovered == signer) {
  25. return true;
  26. }
  27. (bool success, bytes memory result) = signer.staticcall(
  28. abi.encodeWithSelector(IERC1271.isValidSignature.selector, hash, signature)
  29. );
  30. return (success && result.length == 32 && abi.decode(result, (bytes4)) == IERC1271.isValidSignature.selector);
  31. }
  32. }