|
1 | 1 | import { startRegistration } from "@simplewebauthn/browser"; |
2 | 2 | import type { Address, Hex } from "viem"; |
3 | | -import { concat, decodeErrorResult, encodeFunctionData } from "viem"; |
4 | | -import { entryPoint07Address } from "viem/account-abstraction"; |
5 | | -import { prepareDeploySmartAccount } from "zksync-sso-4337/client"; |
| 3 | +import { createWalletClient, http, parseEther } from "viem"; |
| 4 | +import { privateKeyToAccount } from "viem/accounts"; |
| 5 | +import type { Chain } from "viem/chains"; |
| 6 | +import { getAccountAddressFromLogs, prepareDeploySmartAccount } from "zksync-sso-4337/client"; |
6 | 7 |
|
7 | 8 | import contractsConfig from "../contracts-anvil.json"; |
8 | 9 | import { useAccountStore } from "./account"; |
9 | 10 | import { useClientStore } from "./client"; |
10 | 11 |
|
| 12 | +// Anvil's default funded account (first account) |
| 13 | +const DEPLOYER_PRIVATE_KEY = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" as Hex; |
| 14 | + |
| 15 | +// Anvil chain configuration (chain ID 1337 to match erc4337-contracts setup) |
| 16 | +const anvilChain: Chain = { |
| 17 | + id: 1337, |
| 18 | + name: "Anvil", |
| 19 | + nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 }, |
| 20 | + rpcUrls: { |
| 21 | + default: { http: ["http://127.0.0.1:8545"] }, |
| 22 | + }, |
| 23 | +}; |
| 24 | + |
11 | 25 | export const useConnectorStore = defineStore("connector", () => { |
12 | 26 | const accountStore = useAccountStore(); |
13 | 27 | const { address, isConnected } = storeToRefs(accountStore); |
@@ -135,41 +149,49 @@ export const useConnectorStore = defineStore("connector", () => { |
135 | 149 | }], |
136 | 150 | }); |
137 | 151 |
|
138 | | - const initCode = concat([transaction.to, transaction.data]); |
139 | | - const entryPoint = entryPoint07Address; |
140 | | - |
141 | | - let senderAddress: Address; |
142 | | - try { |
143 | | - await publicClient.call({ |
144 | | - to: entryPoint, |
145 | | - data: encodeFunctionData({ |
146 | | - abi: [{ |
147 | | - name: "getSenderAddress", |
148 | | - type: "function", |
149 | | - stateMutability: "view", |
150 | | - inputs: [{ name: "initCode", type: "bytes" }], |
151 | | - outputs: [], |
152 | | - }], |
153 | | - functionName: "getSenderAddress", |
154 | | - args: [initCode], |
155 | | - }), |
156 | | - }); |
157 | | - throw new Error("getSenderAddress did not revert"); |
158 | | - } catch (e) { |
159 | | - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
160 | | - const data = (e as any).data || (e as any).cause?.data || (e as any).cause?.cause?.data; |
161 | | - if (!data) throw e; |
162 | | - |
163 | | - const error = decodeErrorResult({ |
164 | | - abi: [{ |
165 | | - name: "SenderAddressResult", |
166 | | - type: "error", |
167 | | - inputs: [{ name: "sender", type: "address" }], |
168 | | - }], |
169 | | - data, |
170 | | - }); |
171 | | - senderAddress = error.args[0]; |
172 | | - } |
| 152 | + // eslint-disable-next-line no-console |
| 153 | + console.log("Prepared deployment transaction to factory:", transaction.to); |
| 154 | + |
| 155 | + // Deploy the account using a funded deployer wallet |
| 156 | + const deployerAccount = privateKeyToAccount(DEPLOYER_PRIVATE_KEY); |
| 157 | + const walletClient = createWalletClient({ |
| 158 | + account: deployerAccount, |
| 159 | + chain: anvilChain, |
| 160 | + transport: http(), |
| 161 | + }); |
| 162 | + |
| 163 | + // Send the deployment transaction |
| 164 | + // eslint-disable-next-line no-console |
| 165 | + console.log("Deploying account..."); |
| 166 | + const deployTxHash = await walletClient.sendTransaction({ |
| 167 | + to: transaction.to, |
| 168 | + data: transaction.data, |
| 169 | + }); |
| 170 | + |
| 171 | + // eslint-disable-next-line no-console |
| 172 | + console.log("Deploy tx hash:", deployTxHash); |
| 173 | + |
| 174 | + // Wait for deployment to complete and get the receipt with logs |
| 175 | + const receipt = await publicClient.waitForTransactionReceipt({ hash: deployTxHash }); |
| 176 | + |
| 177 | + // eslint-disable-next-line no-console |
| 178 | + console.log("Deployment receipt received, logs count:", receipt.logs.length); |
| 179 | + |
| 180 | + // Get the deployed account address from the AccountCreated event |
| 181 | + const senderAddress = getAccountAddressFromLogs(receipt.logs); |
| 182 | + |
| 183 | + // eslint-disable-next-line no-console |
| 184 | + console.log("Account deployed at:", senderAddress); |
| 185 | + |
| 186 | + // Fund the smart account with some ETH for gas |
| 187 | + const fundTxHash = await walletClient.sendTransaction({ |
| 188 | + to: senderAddress, |
| 189 | + value: parseEther("1"), |
| 190 | + }); |
| 191 | + await publicClient.waitForTransactionReceipt({ hash: fundTxHash }); |
| 192 | + |
| 193 | + // eslint-disable-next-line no-console |
| 194 | + console.log("Account funded!"); |
173 | 195 |
|
174 | 196 | accountStore.setAccount(senderAddress, credIdHex); |
175 | 197 | return { address: senderAddress, credentialId: credIdHex }; |
|
0 commit comments