AccountMock.sol 7.5 KB

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