|
| 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?: string; |
| 19 | + uninstallData?: string; |
| 20 | + type: ModuleType; |
| 21 | +}; |
| 22 | + |
| 23 | +export type Erc7579Options = SmartWalletOptions & { |
| 24 | + defaultModules?: Module[]; |
| 25 | +}; |
| 26 | + |
| 27 | +// TODO (msa) - actual addresses here instead of test ones |
| 28 | +const REFERENCE_BOOTSTRAP_ADDRESS = |
| 29 | + "0xedd4503de72bac321dfeb65f1373d2def17403fc"; |
| 30 | +const REFERENCE_FACTORY_ADDRESS = "0xC7c2a0aC7334f84bAe0EB1c4e42526FB6ea2e661"; //"0xa951A1179bA8bd08b8140aB9dc7910AF08AE7181"; |
| 31 | +const REFERENCE_VALIDATOR_ADDRESS = |
| 32 | + "0x6DF8ea6FF6Ca55f367CDA45510CA40dC78993DEC"; |
| 33 | + |
| 34 | +export function erc7579Config(options: Erc7579Options): SmartWalletOptions { |
| 35 | + return { |
| 36 | + ...options, |
| 37 | + factoryAddress: options.factoryAddress || REFERENCE_FACTORY_ADDRESS, |
| 38 | + overrides: { |
| 39 | + entrypointAddress: ENTRYPOINT_ADDRESS_v0_7, |
| 40 | + createAccount(factoryContract, admin) { |
| 41 | + return prepareContractCall({ |
| 42 | + contract: factoryContract, |
| 43 | + // TODO (msa) - adapt to our own 7579 factory |
| 44 | + method: "function createAccount(bytes32 salt, bytes initCode)", |
| 45 | + params: [ |
| 46 | + Hash.keccak256(admin ? getAddress(admin) : ZERO_ADDRESS), |
| 47 | + AbiParameters.encode( |
| 48 | + [{ type: "address" }, { type: "bytes" }], |
| 49 | + [ |
| 50 | + REFERENCE_BOOTSTRAP_ADDRESS, // bootstrap |
| 51 | + encodeSingleInitMSA({ |
| 52 | + validator: REFERENCE_VALIDATOR_ADDRESS, |
| 53 | + data: "0x", |
| 54 | + }), |
| 55 | + ], |
| 56 | + ), |
| 57 | + ], |
| 58 | + }); |
| 59 | + }, |
| 60 | + async predictAddress(factoryContract, admin) { |
| 61 | + return readContract({ |
| 62 | + contract: factoryContract, |
| 63 | + method: |
| 64 | + "function getAddress(bytes32 salt, bytes initCode) returns (address)", |
| 65 | + // TODO (msa) - adapt to our own 7579 factory |
| 66 | + params: [ |
| 67 | + Hash.keccak256(admin ? getAddress(admin) : ZERO_ADDRESS), |
| 68 | + AbiParameters.encode( |
| 69 | + [{ type: "address" }, { type: "bytes" }], |
| 70 | + [ |
| 71 | + REFERENCE_BOOTSTRAP_ADDRESS, // bootstrap |
| 72 | + encodeSingleInitMSA({ |
| 73 | + validator: REFERENCE_VALIDATOR_ADDRESS, |
| 74 | + data: "0x", |
| 75 | + }), |
| 76 | + ], |
| 77 | + ), |
| 78 | + ], |
| 79 | + }); |
| 80 | + }, |
| 81 | + execute(accountContract, transaction) { |
| 82 | + return execute({ |
| 83 | + contract: accountContract, |
| 84 | + async asyncParams() { |
| 85 | + return { |
| 86 | + mode: Hex.padRight("0x00", 32), // single execution |
| 87 | + executionCalldata: AbiParameters.encodePacked( |
| 88 | + ["address", "uint256", "bytes"], |
| 89 | + [ |
| 90 | + transaction.to || ZERO_ADDRESS, |
| 91 | + transaction.value || 0n, |
| 92 | + transaction.data || "0x", |
| 93 | + ], |
| 94 | + ), |
| 95 | + }; |
| 96 | + }, |
| 97 | + }); |
| 98 | + }, |
| 99 | + executeBatch(accountContract, transactions) { |
| 100 | + return execute({ |
| 101 | + contract: accountContract, |
| 102 | + async asyncParams() { |
| 103 | + return { |
| 104 | + mode: Hex.padRight("0x01", 32), // batch execution |
| 105 | + executionCalldata: AbiParameters.encode( |
| 106 | + [ |
| 107 | + { |
| 108 | + type: "tuple[]", |
| 109 | + components: [ |
| 110 | + { type: "address", name: "to" }, |
| 111 | + { type: "uint256", name: "value" }, |
| 112 | + { type: "bytes", name: "data" }, |
| 113 | + ], |
| 114 | + }, |
| 115 | + ], |
| 116 | + [ |
| 117 | + transactions.map((t) => ({ |
| 118 | + to: t.to || ZERO_ADDRESS, |
| 119 | + value: t.value || 0n, |
| 120 | + data: t.data || "0x", |
| 121 | + })), |
| 122 | + ], |
| 123 | + ), |
| 124 | + }; |
| 125 | + }, |
| 126 | + }); |
| 127 | + }, |
| 128 | + async getAccountNonce(accountContract) { |
| 129 | + const nonce = await getNonce({ |
| 130 | + contract: getContract({ |
| 131 | + address: ENTRYPOINT_ADDRESS_v0_7, |
| 132 | + chain: accountContract.chain, |
| 133 | + client: accountContract.client, |
| 134 | + }), |
| 135 | + key: generateRandomUint192(), |
| 136 | + sender: accountContract.address, |
| 137 | + }); |
| 138 | + const withValidator = `${REFERENCE_VALIDATOR_ADDRESS}${toHex(nonce).slice(42)}`; |
| 139 | + return Hex.toBigInt(withValidator as Hex.Hex); |
| 140 | + }, |
| 141 | + ...options.overrides, |
| 142 | + }, |
| 143 | + }; |
| 144 | +} |
0 commit comments