SignerERC7913.sol 1.8 KB

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