SignerERC7913.sol 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.4.0-rc.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. * ```
  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. /// @dev Return the ERC-7913 signer (i.e. `verifier || key`).
  31. function signer() public view virtual returns (bytes memory) {
  32. return _signer;
  33. }
  34. /// @dev Sets the signer (i.e. `verifier || key`) with an ERC-7913 formatted signer.
  35. function _setSigner(bytes memory signer_) internal {
  36. _signer = signer_;
  37. }
  38. /**
  39. * @dev Verifies a signature using {SignatureChecker-isValidSignatureNow-bytes-bytes32-bytes-}
  40. * with {signer}, `hash` and `signature`.
  41. */
  42. function _rawSignatureValidation(
  43. bytes32 hash,
  44. bytes calldata signature
  45. ) internal view virtual override returns (bool) {
  46. return SignatureChecker.isValidSignatureNow(signer(), hash, signature);
  47. }
  48. }