SignatureChecker.sol 1.2 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. if (Address.isContract(signer)) {
  23. try IERC1271(signer).isValidSignature(hash, signature) returns (bytes4 magicValue) {
  24. return magicValue == IERC1271.isValidSignature.selector;
  25. } catch {
  26. return false;
  27. }
  28. } else {
  29. return ECDSA.recover(hash, signature) == signer;
  30. }
  31. }
  32. }