Add tooling to verify signatures with support for ERC1271 (#2532)
Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
This commit is contained in:
29
contracts/utils/cryptography/SignatureChecker.sol
Normal file
29
contracts/utils/cryptography/SignatureChecker.sol
Normal file
@ -0,0 +1,29 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "./ECDSA.sol";
|
||||
import "../Address.sol";
|
||||
import "../../interfaces/IERC1271.sol";
|
||||
|
||||
/**
|
||||
* @dev Signature verification helper: Provide a single mechanism to verify both private-key (EOA) ECDSA signature and
|
||||
* ERC1271 contract sigantures. Using this instead of ECDSA.recover in your contract will make them compatible with
|
||||
* smart contract wallets such as Argent and Gnosis.
|
||||
*
|
||||
* Note: unlike ECDSA signatures, contract signature's 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).
|
||||
*/
|
||||
library SignatureChecker {
|
||||
function isValidSignatureNow(address signer, bytes32 hash, bytes memory signature) internal view returns (bool) {
|
||||
if (Address.isContract(signer)) {
|
||||
try IERC1271(signer).isValidSignature(hash, signature) returns (bytes4 magicValue) {
|
||||
return magicValue == IERC1271(signer).isValidSignature.selector;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return ECDSA.recover(hash, signature) == signer;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user