SignatureChecker.sol 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "./ECDSA.sol";
  4. import "../Address.sol";
  5. import "../../interfaces/IERC1271.sol";
  6. /**
  7. * @dev Signature verification helper: Provide a single mechanism to verify both private-key (EOA) ECDSA signature and
  8. * ERC1271 contract sigantures. Using this instead of ECDSA.recover in your contract will make them compatible with
  9. * smart contract wallets such as Argent and Gnosis.
  10. *
  11. * Note: unlike ECDSA signatures, contract signature's are revocable, and the outcome of this function can thus change
  12. * through time. It could return true at block N and false at block N+1 (or the opposite).
  13. *
  14. * _Available since v4.1._
  15. */
  16. library SignatureChecker {
  17. function isValidSignatureNow(
  18. address signer,
  19. bytes32 hash,
  20. bytes memory signature
  21. ) 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 && result.length == 32 && abi.decode(result, (bytes4)) == IERC1271.isValidSignature.selector);
  30. }
  31. }