|
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"; |
| 6 | + |
| 7 | +import contractsConfig from "../contracts-anvil.json"; |
| 8 | +import { useAccountStore } from "./account"; |
| 9 | +import { useClientStore } from "./client"; |
3 | 10 |
|
4 | 11 | export const useConnectorStore = defineStore("connector", () => { |
5 | 12 | const accountStore = useAccountStore(); |
@@ -113,19 +120,61 @@ export const useConnectorStore = defineStore("connector", () => { |
113 | 120 | const toHex = (arr: Uint8Array) => `0x${Array.from(arr).map((b) => b.toString(16).padStart(2, "0")).join("")}` as Hex; |
114 | 121 | const x = toHex(xBytes); |
115 | 122 | const y = toHex(yBytes); |
116 | | - // WORKAROUND: Use Anvil rich account #1 for testing |
117 | | - // This bypasses smart account deployment which requires factory contracts |
118 | | - // TODO: Deploy factory contracts once forge deployment issue is resolved |
119 | | - const richAccountAddress = "0x70997970C51812dc3A010C7d01b50e0d17dc79C8" as Address; |
120 | | - |
121 | | - console.log("WORKAROUND: Using rich account", { |
122 | | - address: richAccountAddress, |
123 | | - credentialId: credIdHex, |
124 | | - passkeyCoords: { x, y }, |
| 123 | + const clientStore = useClientStore(); |
| 124 | + const publicClient = clientStore.getPublicClient(); |
| 125 | + |
| 126 | + const { transaction } = prepareDeploySmartAccount({ |
| 127 | + contracts: { |
| 128 | + factory: contractsConfig.factory as Address, |
| 129 | + webauthnValidator: contractsConfig.webauthnValidator as Address, |
| 130 | + }, |
| 131 | + passkeySigners: [{ |
| 132 | + credentialId: credIdHex, |
| 133 | + publicKey: { x, y }, |
| 134 | + originDomain: window.location.origin, |
| 135 | + }], |
125 | 136 | }); |
126 | | - accountStore.setAccount(richAccountAddress, credIdHex); |
127 | | - return { address: richAccountAddress, credentialId: credIdHex }; |
| 137 | + |
| 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 | + } |
| 173 | + |
| 174 | + accountStore.setAccount(senderAddress, credIdHex); |
| 175 | + return { address: senderAddress, credentialId: credIdHex }; |
128 | 176 | } catch (error) { |
| 177 | + // eslint-disable-next-line no-console |
129 | 178 | console.error("Failed to connect account:", error); |
130 | 179 | throw error; |
131 | 180 | } |
|
0 commit comments