|
| 1 | +import { AbiParameters, Hash, Hex } from "ox"; |
| 2 | +import { ZERO_ADDRESS } from "../../../constants/addresses.js"; |
| 3 | +import { getContract } from "../../../contract/contract.js"; |
| 4 | +import { getNonce } from "../../../extensions/erc4337/__generated__/IEntryPoint/read/getNonce.js"; |
| 5 | +import { encodeSingleInitMSA } from "../../../extensions/erc7579/__generated__/Bootstrap/write/singleInitMSA.js"; |
| 6 | +import { execute } from "../../../extensions/erc7579/__generated__/IERC7579Account/write/execute.js"; |
| 7 | +import { prepareContractCall } from "../../../transaction/prepare-contract-call.js"; |
| 8 | +import { readContract } from "../../../transaction/read-contract.js"; |
| 9 | +import { getAddress } from "../../../utils/address.js"; |
| 10 | +import { toHex } from "../../../utils/encoding/hex.js"; |
| 11 | +import { ENTRYPOINT_ADDRESS_v0_7 } from "../lib/constants.js"; |
| 12 | +import { generateRandomUint192 } from "../lib/utils.js"; |
| 13 | +import type { SmartWalletOptions } from "../types.js"; |
| 14 | + |
| 15 | +export type ModuleType = "validator" | "executor" | "fallback" | "hook"; |
| 16 | +export type Module = { |
| 17 | + address: string; |
| 18 | + installData?: Hex.Hex; |
| 19 | + uninstallData?: Hex.Hex; |
| 20 | +}; |
| 21 | + |
| 22 | +export type Erc7579Options = SmartWalletOptions & { |
| 23 | + defaultModules?: { |
| 24 | + validator: (admin: string) => Promise<Module>; |
| 25 | + }; |
| 26 | +}; |
| 27 | + |
| 28 | +// TODO (msa) - actual addresses here instead of test ones |
| 29 | +const REFERENCE_BOOTSTRAP_ADDRESS = |
| 30 | + "0xedd4503de72bac321dfeb65f1373d2def17403fc"; |
| 31 | +const REFERENCE_FACTORY_ADDRESS = "0xC7c2a0aC7334f84bAe0EB1c4e42526FB6ea2e661"; //"0xa951A1179bA8bd08b8140aB9dc7910AF08AE7181"; |
| 32 | +const REFERENCE_VALIDATOR_ADDRESS = |
| 33 | + "0x6DF8ea6FF6Ca55f367CDA45510CA40dC78993DEC"; |
| 34 | + |
| 35 | +export function erc7579Config(options: Erc7579Options): SmartWalletOptions { |
| 36 | + return { |
| 37 | + ...options, |
| 38 | + factoryAddress: options.factoryAddress || REFERENCE_FACTORY_ADDRESS, |
| 39 | + overrides: { |
| 40 | + entrypointAddress: ENTRYPOINT_ADDRESS_v0_7, |
| 41 | + createAccount(factoryContract, admin) { |
| 42 | + return prepareContractCall({ |
| 43 | + contract: factoryContract, |
| 44 | + // TODO (msa) - adapt to our own 7579 factory |
| 45 | + method: "function createAccount(bytes32 salt, bytes initCode)", |
| 46 | + async params() { |
| 47 | + return createAccountParams({ |
| 48 | + defaultModules: options.defaultModules, |
| 49 | + admin, |
| 50 | + }); |
| 51 | + }, |
| 52 | + }); |
| 53 | + }, |
| 54 | + async predictAddress(factoryContract, admin) { |
| 55 | + return readContract({ |
| 56 | + contract: factoryContract, |
| 57 | + method: |
| 58 | + "function getAddress(bytes32 salt, bytes initCode) returns (address)", |
| 59 | + // TODO (msa) - adapt to our own 7579 factory |
| 60 | + async params() { |
| 61 | + return createAccountParams({ |
| 62 | + defaultModules: options.defaultModules, |
| 63 | + admin, |
| 64 | + }); |
| 65 | + }, |
| 66 | + }); |
| 67 | + }, |
| 68 | + execute(accountContract, transaction) { |
| 69 | + return execute({ |
| 70 | + contract: accountContract, |
| 71 | + async asyncParams() { |
| 72 | + return { |
| 73 | + mode: Hex.padRight("0x00", 32), // single execution |
| 74 | + executionCalldata: AbiParameters.encodePacked( |
| 75 | + ["address", "uint256", "bytes"], |
| 76 | + [ |
| 77 | + transaction.to || ZERO_ADDRESS, |
| 78 | + transaction.value || 0n, |
| 79 | + transaction.data || "0x", |
| 80 | + ], |
| 81 | + ), |
| 82 | + }; |
| 83 | + }, |
| 84 | + }); |
| 85 | + }, |
| 86 | + executeBatch(accountContract, transactions) { |
| 87 | + return execute({ |
| 88 | + contract: accountContract, |
| 89 | + async asyncParams() { |
| 90 | + return { |
| 91 | + mode: Hex.padRight("0x01", 32), // batch execution |
| 92 | + executionCalldata: AbiParameters.encode( |
| 93 | + [ |
| 94 | + { |
| 95 | + type: "tuple[]", |
| 96 | + components: [ |
| 97 | + { type: "address", name: "to" }, |
| 98 | + { type: "uint256", name: "value" }, |
| 99 | + { type: "bytes", name: "data" }, |
| 100 | + ], |
| 101 | + }, |
| 102 | + ], |
| 103 | + [ |
| 104 | + transactions.map((t) => ({ |
| 105 | + to: t.to || ZERO_ADDRESS, |
| 106 | + value: t.value || 0n, |
| 107 | + data: t.data || "0x", |
| 108 | + })), |
| 109 | + ], |
| 110 | + ), |
| 111 | + }; |
| 112 | + }, |
| 113 | + }); |
| 114 | + }, |
| 115 | + async getAccountNonce(accountContract) { |
| 116 | + const nonce = await getNonce({ |
| 117 | + contract: getContract({ |
| 118 | + address: ENTRYPOINT_ADDRESS_v0_7, |
| 119 | + chain: accountContract.chain, |
| 120 | + client: accountContract.client, |
| 121 | + }), |
| 122 | + key: generateRandomUint192(), |
| 123 | + sender: accountContract.address, |
| 124 | + }); |
| 125 | + const withValidator = `${REFERENCE_VALIDATOR_ADDRESS}${toHex(nonce).slice(42)}`; |
| 126 | + return Hex.toBigInt(withValidator as Hex.Hex); |
| 127 | + }, |
| 128 | + ...options.overrides, |
| 129 | + }, |
| 130 | + }; |
| 131 | +} |
| 132 | + |
| 133 | +async function createAccountParams(args: { |
| 134 | + defaultModules: Erc7579Options["defaultModules"]; |
| 135 | + admin: string; |
| 136 | +}): Promise<[Hex.Hex, Hex.Hex]> { |
| 137 | + const { defaultModules, admin } = args; |
| 138 | + const customValidator = defaultModules |
| 139 | + ? await defaultModules.validator(admin) |
| 140 | + : undefined; |
| 141 | + return [ |
| 142 | + Hash.keccak256(getAddress(admin)), |
| 143 | + AbiParameters.encode( |
| 144 | + [{ type: "address" }, { type: "bytes" }], |
| 145 | + [ |
| 146 | + REFERENCE_BOOTSTRAP_ADDRESS, // bootstrap |
| 147 | + encodeSingleInitMSA( |
| 148 | + customValidator |
| 149 | + ? { |
| 150 | + validator: getAddress(customValidator.address), |
| 151 | + data: customValidator.installData || "0x", |
| 152 | + } |
| 153 | + : { |
| 154 | + validator: REFERENCE_VALIDATOR_ADDRESS, |
| 155 | + data: "0x", |
| 156 | + }, |
| 157 | + ), |
| 158 | + ], |
| 159 | + ), |
| 160 | + ]; |
| 161 | +} |
0 commit comments