1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v4.8.0) (utils/cryptography/SignatureChecker.sol)
- pragma solidity ^0.8.0;
- import "./ECDSA.sol";
- import "../../interfaces/IERC1271.sol";
- /**
- * @dev Signature verification helper that can be used instead of `ECDSA.recover` to seamlessly support both ECDSA
- * signatures from externally owned accounts (EOAs) as well as ERC1271 signatures from smart contract wallets like
- * Argent and Gnosis Safe.
- *
- * _Available since v4.1._
- */
- library SignatureChecker {
- /**
- * @dev Checks if a signature is valid for a given signer and data hash. If the signer is a smart contract, the
- * signature is validated against that smart contract using ERC1271, otherwise it's validated using `ECDSA.recover`.
- *
- * NOTE: Unlike ECDSA signatures, contract signatures are revocable, and the outcome of this function can thus
- * change through time. It could return true at block N and false at block N+1 (or the opposite).
- */
- function isValidSignatureNow(address signer, bytes32 hash, bytes memory signature) internal view returns (bool) {
- (address recovered, ECDSA.RecoverError error) = ECDSA.tryRecover(hash, signature);
- return
- (error == ECDSA.RecoverError.NoError && recovered == signer) ||
- isValidERC1271SignatureNow(signer, hash, signature);
- }
- /**
- * @dev Checks if a signature is valid for a given signer and data hash. The signature is validated
- * against the signer smart contract using ERC1271.
- *
- * NOTE: Unlike ECDSA signatures, contract signatures are revocable, and the outcome of this function can thus
- * change through time. It could return true at block N and false at block N+1 (or the opposite).
- */
- function isValidERC1271SignatureNow(
- address signer,
- bytes32 hash,
- bytes memory signature
- ) internal view returns (bool) {
- (bool success, bytes memory result) = signer.staticcall(
- abi.encodeWithSelector(IERC1271.isValidSignature.selector, hash, signature)
- );
- return (success &&
- result.length >= 32 &&
- abi.decode(result, (bytes32)) == bytes32(IERC1271.isValidSignature.selector));
- }
- }
|