AccountMock.sol 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/signers/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/signers/AbstractSigner.sol";
  14. import {SignerECDSA} from "../../utils/cryptography/signers/SignerECDSA.sol";
  15. import {SignerP256} from "../../utils/cryptography/signers/SignerP256.sol";
  16. import {SignerRSA} from "../../utils/cryptography/signers/SignerRSA.sol";
  17. import {SignerERC7702} from "../../utils/cryptography/signers/SignerERC7702.sol";
  18. import {SignerERC7913} from "../../utils/cryptography/signers/SignerERC7913.sol";
  19. import {MultiSignerERC7913} from "../../utils/cryptography/signers/MultiSignerERC7913.sol";
  20. abstract contract AccountMock is Account, ERC7739, ERC7821, ERC721Holder, ERC1155Holder {
  21. /// Validates a user operation with a boolean signature.
  22. function _rawSignatureValidation(bytes32 hash, bytes calldata signature) internal pure override returns (bool) {
  23. return signature.length >= 32 && bytes32(signature) == hash;
  24. }
  25. /// @inheritdoc ERC7821
  26. function _erc7821AuthorizedExecutor(
  27. address caller,
  28. bytes32 mode,
  29. bytes calldata executionData
  30. ) internal view virtual override returns (bool) {
  31. return caller == address(entryPoint()) || super._erc7821AuthorizedExecutor(caller, mode, executionData);
  32. }
  33. }
  34. abstract contract AccountECDSAMock is Account, SignerECDSA, ERC7739, ERC7821, ERC721Holder, ERC1155Holder {
  35. constructor(address signerAddr) {
  36. _setSigner(signerAddr);
  37. }
  38. /// @inheritdoc ERC7821
  39. function _erc7821AuthorizedExecutor(
  40. address caller,
  41. bytes32 mode,
  42. bytes calldata executionData
  43. ) internal view virtual override returns (bool) {
  44. return caller == address(entryPoint()) || super._erc7821AuthorizedExecutor(caller, mode, executionData);
  45. }
  46. }
  47. abstract contract AccountP256Mock is Account, SignerP256, ERC7739, ERC7821, ERC721Holder, ERC1155Holder {
  48. constructor(bytes32 qx, bytes32 qy) {
  49. _setSigner(qx, qy);
  50. }
  51. /// @inheritdoc ERC7821
  52. function _erc7821AuthorizedExecutor(
  53. address caller,
  54. bytes32 mode,
  55. bytes calldata executionData
  56. ) internal view virtual override returns (bool) {
  57. return caller == address(entryPoint()) || super._erc7821AuthorizedExecutor(caller, mode, executionData);
  58. }
  59. }
  60. abstract contract AccountRSAMock is Account, SignerRSA, ERC7739, ERC7821, ERC721Holder, ERC1155Holder {
  61. constructor(bytes memory e, bytes memory n) {
  62. _setSigner(e, n);
  63. }
  64. /// @inheritdoc ERC7821
  65. function _erc7821AuthorizedExecutor(
  66. address caller,
  67. bytes32 mode,
  68. bytes calldata executionData
  69. ) internal view virtual override returns (bool) {
  70. return caller == address(entryPoint()) || super._erc7821AuthorizedExecutor(caller, mode, executionData);
  71. }
  72. }
  73. abstract contract AccountERC7702Mock is Account, SignerERC7702, ERC7739, ERC7821, ERC721Holder, ERC1155Holder {
  74. /// @inheritdoc ERC7821
  75. function _erc7821AuthorizedExecutor(
  76. address caller,
  77. bytes32 mode,
  78. bytes calldata executionData
  79. ) internal view virtual override returns (bool) {
  80. return caller == address(entryPoint()) || super._erc7821AuthorizedExecutor(caller, mode, executionData);
  81. }
  82. }
  83. abstract contract AccountERC7702WithModulesMock is
  84. Account,
  85. AccountERC7579,
  86. SignerERC7702,
  87. ERC7739,
  88. ERC721Holder,
  89. ERC1155Holder
  90. {
  91. function _validateUserOp(
  92. PackedUserOperation calldata userOp,
  93. bytes32 userOpHash
  94. ) internal virtual override(Account, AccountERC7579) returns (uint256) {
  95. return super._validateUserOp(userOp, userOpHash);
  96. }
  97. /// @dev Resolve implementation of ERC-1271 by both ERC7739 and AccountERC7579 to support both schemes.
  98. function isValidSignature(
  99. bytes32 hash,
  100. bytes calldata signature
  101. ) public view virtual override(ERC7739, AccountERC7579) returns (bytes4) {
  102. // ERC-7739 can return the fn selector (success), 0xffffffff (invalid) or 0x77390001 (detection).
  103. // If the return is 0xffffffff, we fallback to validation using ERC-7579 modules.
  104. bytes4 erc7739magic = ERC7739.isValidSignature(hash, signature);
  105. return erc7739magic == bytes4(0xffffffff) ? AccountERC7579.isValidSignature(hash, signature) : erc7739magic;
  106. }
  107. /// @dev Enable signature using the ERC-7702 signer.
  108. function _rawSignatureValidation(
  109. bytes32 hash,
  110. bytes calldata signature
  111. ) internal view virtual override(AbstractSigner, AccountERC7579, SignerERC7702) returns (bool) {
  112. return SignerERC7702._rawSignatureValidation(hash, signature);
  113. }
  114. }
  115. abstract contract AccountERC7579Mock is AccountERC7579 {
  116. constructor(address validator, bytes memory initData) {
  117. _installModule(MODULE_TYPE_VALIDATOR, validator, initData);
  118. }
  119. }
  120. abstract contract AccountERC7579HookedMock is AccountERC7579Hooked {
  121. constructor(address validator, bytes memory initData) {
  122. _installModule(MODULE_TYPE_VALIDATOR, validator, initData);
  123. }
  124. }
  125. abstract contract AccountMultiSignerMock is Account, MultiSignerERC7913, ERC7739, ERC7821, ERC721Holder, ERC1155Holder {
  126. constructor(bytes[] memory signers, uint64 threshold) {
  127. _addSigners(signers);
  128. _setThreshold(threshold);
  129. }
  130. /// @inheritdoc ERC7821
  131. function _erc7821AuthorizedExecutor(
  132. address caller,
  133. bytes32 mode,
  134. bytes calldata executionData
  135. ) internal view virtual override returns (bool) {
  136. return caller == address(entryPoint()) || super._erc7821AuthorizedExecutor(caller, mode, executionData);
  137. }
  138. }
  139. abstract contract AccountERC7913Mock is Account, SignerERC7913, ERC7739, ERC7821, ERC721Holder, ERC1155Holder {
  140. constructor(bytes memory _signer) {
  141. _setSigner(_signer);
  142. }
  143. /// @inheritdoc ERC7821
  144. function _erc7821AuthorizedExecutor(
  145. address caller,
  146. bytes32 mode,
  147. bytes calldata executionData
  148. ) internal view virtual override returns (bool) {
  149. return caller == address(entryPoint()) || super._erc7821AuthorizedExecutor(caller, mode, executionData);
  150. }
  151. }