Skip to content

Commit fafb0fd

Browse files
committed
feat: move dynamic imports in lib
also update tests to better reject and check for errors
1 parent 1ab5cd8 commit fafb0fd

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

examples/demo-app/tests/web-sdk-test.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ test("Deploy, fund, and transfer from smart account", async ({ page }) => {
9797
// Wait for transaction to complete - look for transaction hash in the send section
9898
// We need to find the second occurrence of "Transaction Hash:" since fund also has one
9999
await expect(page.locator("strong:has-text(\"Transaction Hash:\")").nth(1)).toBeVisible({ timeout: 30000 });
100+
await expect(page.getByText("Transaction failed: Failed to submit UserOperation:")).not.toBeVisible();
100101

101102
// Verify we have a transaction hash for the send
102103
const sendTxHash = page.locator("code").filter({ hasText: /^0x[a-fA-F0-9]{64}/ }).nth(1);
@@ -196,7 +197,8 @@ test("Deploy with passkey and send transaction using passkey", async ({ page })
196197
await page.getByRole("button", { name: "Send with Passkey" }).click();
197198

198199
// Wait for transaction to complete
199-
await expect(page.locator("strong:has-text(\"Transaction Hash:\")").nth(1)).toBeVisible({ timeout: 60000 });
200+
await expect(page.getByText("Transaction confirmed! UserOp hash:")).toBeVisible({ timeout: 60000 });
201+
await expect(page.getByText("Transaction failed: Failed to submit UserOperation:")).not.toBeVisible();
200202

201203
// Verify we have a transaction hash for the send
202204
const sendTxHash = page.locator("code").filter({ hasText: /^0x[a-fA-F0-9]{64}/ }).nth(1);

packages/sdk-platforms/web/src/webauthn-helpers.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55

66
import { startAuthentication } from "@simplewebauthn/browser";
77

8+
import {
9+
encode_passkey_signature,
10+
prepare_passkey_user_operation,
11+
SendTransactionConfig,
12+
submit_passkey_user_operation,
13+
} from "../pkg-bundler/zksync_sso_erc4337_web_ffi";
14+
815
/**
916
* Convert hex string to Uint8Array
1017
*
@@ -191,7 +198,6 @@ export async function signWithPasskey(
191198

192199
// ABI encode the signature using Rust function (now fixed to match ethers.js)
193200
// Format: (bytes authenticatorData, string clientDataJSON, bytes32[2] rs, bytes credentialId)
194-
const { encode_passkey_signature } = await import("../pkg-bundler/zksync_sso_erc4337_web_ffi");
195201
const signature = encode_passkey_signature(
196202
authenticatorData,
197203
clientDataJSON,
@@ -255,13 +261,6 @@ export async function sendTransactionWithPasskey(options: {
255261
origin,
256262
} = options;
257263

258-
// Import WASM functions
259-
const {
260-
prepare_passkey_user_operation,
261-
submit_passkey_user_operation,
262-
SendTransactionConfig,
263-
} = await import("../pkg-bundler/zksync_sso_erc4337_web_ffi");
264-
265264
// Step 1: Prepare UserOperation to get hash
266265
const prepareConfig = new SendTransactionConfig(
267266
rpcUrl,

packages/sdk-platforms/web/src/webauthn.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@
88
* @module webauthn
99
*/
1010

11+
// Lazy-loaded SimpleWebAuthn - only loaded when needed
12+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
13+
let simpleWebAuthnModule: any = null;
14+
async function getSimpleWebAuthn() {
15+
if (!simpleWebAuthnModule) {
16+
try {
17+
simpleWebAuthnModule = await import("@simplewebauthn/browser");
18+
} catch {
19+
throw new Error(
20+
"SimpleWebAuthn is not installed. Please install @simplewebauthn/browser: npm install @simplewebauthn/browser",
21+
);
22+
}
23+
}
24+
return simpleWebAuthnModule;
25+
}
26+
1127
/**
1228
* Configuration options for creating a WebAuthn credential
1329
*/
@@ -128,17 +144,9 @@ function base64urlToBuffer(base64url: string): Uint8Array {
128144
export async function createWebAuthnCredential(
129145
options: CreateCredentialOptions = {},
130146
): Promise<WebAuthnCredential> {
131-
// Try to import SimpleWebAuthn (optional peer dependency)
132-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
133-
let startRegistration: any;
134-
try {
135-
const simpleWebAuthn = await import("@simplewebauthn/browser");
136-
startRegistration = simpleWebAuthn.startRegistration;
137-
} catch {
138-
throw new Error(
139-
"SimpleWebAuthn is not installed. Please install @simplewebauthn/browser: npm install @simplewebauthn/browser",
140-
);
141-
}
147+
// Get SimpleWebAuthn (lazy loaded and cached)
148+
const simpleWebAuthn = await getSimpleWebAuthn();
149+
const startRegistration = simpleWebAuthn.startRegistration;
142150

143151
// Check if WebAuthn is supported
144152
if (!window.PublicKeyCredential) {

0 commit comments

Comments
 (0)