|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.24; |
| 4 | + |
| 5 | +import {AbstractSigner} from "./AbstractSigner.sol"; |
| 6 | +import {ERC7913Utils} from "./ERC7913Utils.sol"; |
| 7 | + |
| 8 | +/** |
| 9 | + * @dev Implementation of {AbstractSigner} using |
| 10 | + * https://eips.ethereum.org/EIPS/eip-7913[ERC-7913] signature verification. |
| 11 | + * |
| 12 | + * For {Account} usage, a {_setSigner} function is provided to set the ERC-7913 formatted {signer}. |
| 13 | + * Doing so is easier for a factory, who is likely to use initializable clones of this contract. |
| 14 | + * |
| 15 | + * The signer is a `bytes` object that concatenates a verifier address and a key: `verifier || key`. |
| 16 | + * |
| 17 | + * Example of usage: |
| 18 | + * |
| 19 | + * ```solidity |
| 20 | + * contract MyAccountERC7913 is Account, SignerERC7913, Initializable { |
| 21 | + * constructor() EIP712("MyAccountERC7913", "1") {} |
| 22 | + * |
| 23 | + * function initialize(bytes memory signer_) public initializer { |
| 24 | + * _setSigner(signer_); |
| 25 | + * } |
| 26 | + * } |
| 27 | + * ``` |
| 28 | + * |
| 29 | + * IMPORTANT: Failing to call {_setSigner} either during construction (if used standalone) |
| 30 | + * or during initialization (if used as a clone) may leave the signer either front-runnable or unusable. |
| 31 | + */ |
| 32 | + |
| 33 | +abstract contract SignerERC7913 is AbstractSigner { |
| 34 | + bytes private _signer; |
| 35 | + |
| 36 | + /// @dev Sets the signer (i.e. `verifier || key`) with an ERC-7913 formatted signer. |
| 37 | + function _setSigner(bytes memory signer_) internal { |
| 38 | + _signer = signer_; |
| 39 | + } |
| 40 | + |
| 41 | + /// @dev Return the ERC-7913 signer (i.e. `verifier || key`). |
| 42 | + function signer() public view virtual returns (bytes memory) { |
| 43 | + return _signer; |
| 44 | + } |
| 45 | + |
| 46 | + /// @dev Verifies a signature using {ERC7913Utils.isValidSignatureNow} with {signer}, `hash` and `signature`. |
| 47 | + function _rawSignatureValidation( |
| 48 | + bytes32 hash, |
| 49 | + bytes calldata signature |
| 50 | + ) internal view virtual override returns (bool) { |
| 51 | + return ERC7913Utils.isValidSignatureNow(signer(), hash, signature); |
| 52 | + } |
| 53 | +} |
0 commit comments