SignatureChecker.sol 1.2 KB

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