SignerERC7913.sol 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. constructor(bytes memory signer_) {
  30. _setSigner(signer_);
  31. }
  32. /// @dev Return the ERC-7913 signer (i.e. `verifier || key`).
  33. function signer() public view virtual returns (bytes memory) {
  34. return _signer;
  35. }
  36. /// @dev Sets the signer (i.e. `verifier || key`) with an ERC-7913 formatted signer.
  37. function _setSigner(bytes memory signer_) internal {
  38. _signer = signer_;
  39. }
  40. /**
  41. * @dev Verifies a signature using {SignatureChecker-isValidSignatureNow-bytes-bytes32-bytes-}
  42. * with {signer}, `hash` and `signature`.
  43. */
  44. function _rawSignatureValidation(
  45. bytes32 hash,
  46. bytes calldata signature
  47. ) internal view virtual override returns (bool) {
  48. return SignatureChecker.isValidSignatureNow(signer(), hash, signature);
  49. }
  50. }