SignatureChecker.sol 1.2 KB

1234567891011121314151617181920212223242526272829
  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. library SignatureChecker {
  15. function isValidSignatureNow(address signer, bytes32 hash, bytes memory signature) internal view returns (bool) {
  16. if (Address.isContract(signer)) {
  17. try IERC1271(signer).isValidSignature(hash, signature) returns (bytes4 magicValue) {
  18. return magicValue == IERC1271(signer).isValidSignature.selector;
  19. } catch {
  20. return false;
  21. }
  22. } else {
  23. return ECDSA.recover(hash, signature) == signer;
  24. }
  25. }
  26. }