SignerERC7913.sol 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.4.0) (utils/cryptography/signers/SignerERC7913.sol)
  3. pragma solidity ^0.8.24;
  4. import {AbstractSigner} from "./AbstractSigner.sol";
  5. import {SignatureChecker} from "../SignatureChecker.sol";
  6. /**
  7. * @dev Implementation of {AbstractSigner} using
  8. * https://eips.ethereum.org/EIPS/eip-7913[ERC-7913] signature verification.
  9. *
  10. * For {Account} usage, a {_setSigner} function is provided to set the ERC-7913 formatted {signer}.
  11. * Doing so is easier for a factory, who is likely to use initializable clones of this contract.
  12. *
  13. * The signer is a `bytes` object that concatenates a verifier address and a key: `verifier || key`.
  14. *
  15. * Example of usage:
  16. *
  17. * ```solidity
  18. * contract MyAccountERC7913 is Account, SignerERC7913, Initializable {
  19. * function initialize(bytes memory signer_) public initializer {
  20. * _setSigner(signer_);
  21. * }
  22. *
  23. * function setSigner(bytes memory signer_) public onlyEntryPointOrSelf {
  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. abstract contract SignerERC7913 is AbstractSigner {
  33. bytes private _signer;
  34. constructor(bytes memory signer_) {
  35. _setSigner(signer_);
  36. }
  37. /// @dev Return the ERC-7913 signer (i.e. `verifier || key`).
  38. function signer() public view virtual returns (bytes memory) {
  39. return _signer;
  40. }
  41. /// @dev Sets the signer (i.e. `verifier || key`) with an ERC-7913 formatted signer.
  42. function _setSigner(bytes memory signer_) internal {
  43. _signer = signer_;
  44. }
  45. /**
  46. * @dev Verifies a signature using {SignatureChecker-isValidSignatureNow-bytes-bytes32-bytes-}
  47. * with {signer}, `hash` and `signature`.
  48. */
  49. function _rawSignatureValidation(
  50. bytes32 hash,
  51. bytes calldata signature
  52. ) internal view virtual override returns (bool) {
  53. return SignatureChecker.isValidSignatureNow(signer(), hash, signature);
  54. }
  55. }