SignerERC7913.sol 2.0 KB

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