SignerERC7913.sol 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. * function setSigner(bytes memory signer_) public onlyEntryPointOrSelf {
  23. * _setSigner(signer_);
  24. * }
  25. * }
  26. * ```
  27. *
  28. * IMPORTANT: Failing to call {_setSigner} either during construction (if used standalone)
  29. * or during initialization (if used as a clone) may leave the signer either front-runnable or unusable.
  30. */
  31. abstract contract SignerERC7913 is AbstractSigner {
  32. bytes private _signer;
  33. constructor(bytes memory signer_) {
  34. _setSigner(signer_);
  35. }
  36. /// @dev Return the ERC-7913 signer (i.e. `verifier || key`).
  37. function signer() public view virtual returns (bytes memory) {
  38. return _signer;
  39. }
  40. /// @dev Sets the signer (i.e. `verifier || key`) with an ERC-7913 formatted signer.
  41. function _setSigner(bytes memory signer_) internal {
  42. _signer = signer_;
  43. }
  44. /**
  45. * @dev Verifies a signature using {SignatureChecker-isValidSignatureNow-bytes-bytes32-bytes-}
  46. * with {signer}, `hash` and `signature`.
  47. */
  48. function _rawSignatureValidation(
  49. bytes32 hash,
  50. bytes calldata signature
  51. ) internal view virtual override returns (bool) {
  52. return SignatureChecker.isValidSignatureNow(signer(), hash, signature);
  53. }
  54. }