AccountMock.sol 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.27;
  3. import {Account} from "../../account/Account.sol";
  4. import {AccountERC7579} from "../../account/extensions/AccountERC7579.sol";
  5. import {AccountERC7579Hooked} from "../../account/extensions/AccountERC7579Hooked.sol";
  6. import {ERC721Holder} from "../../token/ERC721/utils/ERC721Holder.sol";
  7. import {ERC1155Holder} from "../../token/ERC1155/utils/ERC1155Holder.sol";
  8. import {ERC4337Utils} from "../../account/utils/draft-ERC4337Utils.sol";
  9. import {ERC7739} from "../../utils/cryptography/ERC7739.sol";
  10. import {ERC7821} from "../../account/extensions/ERC7821.sol";
  11. import {MODULE_TYPE_VALIDATOR} from "../../interfaces/draft-IERC7579.sol";
  12. import {PackedUserOperation} from "../../interfaces/draft-IERC4337.sol";
  13. import {AbstractSigner} from "../../utils/cryptography/AbstractSigner.sol";
  14. import {SignerECDSA} from "../../utils/cryptography/SignerECDSA.sol";
  15. import {SignerP256} from "../../utils/cryptography/SignerP256.sol";
  16. import {SignerRSA} from "../../utils/cryptography/SignerRSA.sol";
  17. import {SignerERC7702} from "../../utils/cryptography/SignerERC7702.sol";
  18. abstract contract AccountMock is Account, ERC7739, ERC7821, ERC721Holder, ERC1155Holder {
  19. /// Validates a user operation with a boolean signature.
  20. function _rawSignatureValidation(bytes32 hash, bytes calldata signature) internal pure override returns (bool) {
  21. return signature.length >= 32 && bytes32(signature) == hash;
  22. }
  23. /// @inheritdoc ERC7821
  24. function _erc7821AuthorizedExecutor(
  25. address caller,
  26. bytes32 mode,
  27. bytes calldata executionData
  28. ) internal view virtual override returns (bool) {
  29. return caller == address(entryPoint()) || super._erc7821AuthorizedExecutor(caller, mode, executionData);
  30. }
  31. }
  32. abstract contract AccountECDSAMock is Account, SignerECDSA, ERC7739, ERC7821, ERC721Holder, ERC1155Holder {
  33. constructor(address signerAddr) {
  34. _setSigner(signerAddr);
  35. }
  36. /// @inheritdoc ERC7821
  37. function _erc7821AuthorizedExecutor(
  38. address caller,
  39. bytes32 mode,
  40. bytes calldata executionData
  41. ) internal view virtual override returns (bool) {
  42. return caller == address(entryPoint()) || super._erc7821AuthorizedExecutor(caller, mode, executionData);
  43. }
  44. }
  45. abstract contract AccountP256Mock is Account, SignerP256, ERC7739, ERC7821, ERC721Holder, ERC1155Holder {
  46. constructor(bytes32 qx, bytes32 qy) {
  47. _setSigner(qx, qy);
  48. }
  49. /// @inheritdoc ERC7821
  50. function _erc7821AuthorizedExecutor(
  51. address caller,
  52. bytes32 mode,
  53. bytes calldata executionData
  54. ) internal view virtual override returns (bool) {
  55. return caller == address(entryPoint()) || super._erc7821AuthorizedExecutor(caller, mode, executionData);
  56. }
  57. }
  58. abstract contract AccountRSAMock is Account, SignerRSA, ERC7739, ERC7821, ERC721Holder, ERC1155Holder {
  59. constructor(bytes memory e, bytes memory n) {
  60. _setSigner(e, n);
  61. }
  62. /// @inheritdoc ERC7821
  63. function _erc7821AuthorizedExecutor(
  64. address caller,
  65. bytes32 mode,
  66. bytes calldata executionData
  67. ) internal view virtual override returns (bool) {
  68. return caller == address(entryPoint()) || super._erc7821AuthorizedExecutor(caller, mode, executionData);
  69. }
  70. }
  71. abstract contract AccountERC7702Mock is Account, SignerERC7702, ERC7739, ERC7821, ERC721Holder, ERC1155Holder {
  72. /// @inheritdoc ERC7821
  73. function _erc7821AuthorizedExecutor(
  74. address caller,
  75. bytes32 mode,
  76. bytes calldata executionData
  77. ) internal view virtual override returns (bool) {
  78. return caller == address(entryPoint()) || super._erc7821AuthorizedExecutor(caller, mode, executionData);
  79. }
  80. }
  81. abstract contract AccountERC7702WithModulesMock is
  82. Account,
  83. AccountERC7579,
  84. SignerERC7702,
  85. ERC7739,
  86. ERC721Holder,
  87. ERC1155Holder
  88. {
  89. function _validateUserOp(
  90. PackedUserOperation calldata userOp,
  91. bytes32 userOpHash
  92. ) internal virtual override(Account, AccountERC7579) returns (uint256) {
  93. return super._validateUserOp(userOp, userOpHash);
  94. }
  95. /// @dev Resolve implementation of ERC-1271 by both ERC7739 and AccountERC7579 to support both schemes.
  96. function isValidSignature(
  97. bytes32 hash,
  98. bytes calldata signature
  99. ) public view virtual override(ERC7739, AccountERC7579) returns (bytes4) {
  100. // ERC-7739 can return the fn selector (success), 0xffffffff (invalid) or 0x77390001 (detection).
  101. // If the return is 0xffffffff, we fallback to validation using ERC-7579 modules.
  102. bytes4 erc7739magic = ERC7739.isValidSignature(hash, signature);
  103. return erc7739magic == bytes4(0xffffffff) ? AccountERC7579.isValidSignature(hash, signature) : erc7739magic;
  104. }
  105. /// @dev Enable signature using the ERC-7702 signer.
  106. function _rawSignatureValidation(
  107. bytes32 hash,
  108. bytes calldata signature
  109. ) internal view virtual override(AbstractSigner, AccountERC7579, SignerERC7702) returns (bool) {
  110. return SignerERC7702._rawSignatureValidation(hash, signature);
  111. }
  112. }
  113. abstract contract AccountERC7579Mock is AccountERC7579 {
  114. constructor(address validator, bytes memory initData) {
  115. _installModule(MODULE_TYPE_VALIDATOR, validator, initData);
  116. }
  117. }
  118. abstract contract AccountERC7579HookedMock is AccountERC7579Hooked {
  119. constructor(address validator, bytes memory initData) {
  120. _installModule(MODULE_TYPE_VALIDATOR, validator, initData);
  121. }
  122. }