@walletwright/core 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { a as wallets, c as extensionIdFromPath, d as placeApprovalWindow, f as profileKey, i as walletKindsByEcosystem, l as findNotificationPopup, m as waitUntil, n as extensionContextOptions, o as DEFAULT_CACHE_DIR, p as formatTimeout, s as DEFAULT_NOTIFICATION_MATCH, t as buildCache, u as hasNotificationPopup } from "./cache-Bqz3Mchl.mjs";
1
+ import { a as walletKindsByEcosystem, c as DEFAULT_NOTIFICATION_MATCH, d as placeApprovalWindow, f as profileKey, i as isWalletKind, l as extensionIdFromPath, m as waitUntil, n as restorePreviousProfile, o as wallets, p as formatTimeout, r as extensionContextOptions, s as DEFAULT_CACHE_DIR, t as buildCache, u as findNotificationPopup } from "./cache-CHP-6eU0.mjs";
2
2
  import { cp, mkdtemp, rm } from "node:fs/promises";
3
3
  import path from "node:path";
4
4
  import { chromium, test } from "@playwright/test";
@@ -11,6 +11,22 @@ const POPUP_CLOSE_TIMEOUT_MS = 15e3;
11
11
  const OPTIONAL_POPUP_TIMEOUT_MS = 1e4;
12
12
  /** The MV3 service worker spawns a popup slowly once the wallet's own UI has been driven. */
13
13
  const REQUIRED_POPUP_TIMEOUT_MS = 3e4;
14
+ /**
15
+ * Drive `popup` with `settle` and return the page that actually took the approval. A popup that
16
+ * closes mid-settle is the wallet respawning the request, so re-acquire once and drive the new one.
17
+ */
18
+ const settleApproval = async (settle, popup, acquire) => {
19
+ try {
20
+ await settle(popup);
21
+ return popup;
22
+ } catch (error) {
23
+ if (!popup.isClosed()) throw error;
24
+ const fresh = await acquire();
25
+ if (!fresh) throw error;
26
+ await settle(fresh);
27
+ return fresh;
28
+ }
29
+ };
14
30
  /** Build the runtime controller that drives an unlocked wallet against the dapp under test. */
15
31
  const createWallet = ({ context, definition, extensionId, home, password }) => {
16
32
  const match = definition.notificationMatch ?? "notification.html";
@@ -20,60 +36,39 @@ const createWallet = ({ context, definition, extensionId, home, password }) => {
20
36
  home,
21
37
  password
22
38
  };
23
- /**
24
- * Make the dapp the active tab. While an extension page holds that spot, MetaMask renders a new
25
- * approval inline in it rather than opening a notification window, and the window that does open
26
- * shows the wallet home instead of the request. The wallet's own page can end up fronted both
27
- * after an action and after a previous approval settles.
28
- */
39
+ /** MetaMask opens requests inline while its extension page owns focus. */
29
40
  const frontDapp = async () => {
30
- await context.pages().find((page) => (/* @__PURE__ */ new RegExp("^https?:", "v")).test(page.url()) && !page.isClosed())?.bringToFront().catch(() => {});
41
+ await context.pages().find((page) => new RegExp("^https?:", "v").test(page.url()) && !page.isClosed())?.bringToFront().catch(() => {});
31
42
  };
32
- /** Wait for the approval window the wallet opens for a pending request. */
33
- const findApproval = (optional) => findNotificationPopup({
34
- approvalControls: definition.approvalControls,
35
- context,
36
- extensionId,
37
- match,
38
- timeoutMs: optional ? OPTIONAL_POPUP_TIMEOUT_MS : REQUIRED_POPUP_TIMEOUT_MS
39
- });
40
- /** Drive the pending approval popup with `settle`, then wait for it to close. */
43
+ /** Find and place the approval window before its controls are driven. */
44
+ const acquireApproval = async (optional) => {
45
+ const popup = await findNotificationPopup({
46
+ approvalControls: definition.approvalControls,
47
+ context,
48
+ extensionId,
49
+ match,
50
+ timeoutMs: optional ? OPTIONAL_POPUP_TIMEOUT_MS : REQUIRED_POPUP_TIMEOUT_MS
51
+ });
52
+ if (popup) await placeApprovalWindow(popup);
53
+ return popup;
54
+ };
55
+ /** Drive the pending approval popup with `settle`, then wait for that popup to close. */
41
56
  const resolvePopup = async (settle, { optional = false }) => {
42
57
  await frontDapp();
43
- const find = () => findApproval(optional);
44
- const popup = await find();
58
+ const acquire = () => acquireApproval(optional);
59
+ const popup = await acquire();
45
60
  if (!popup) {
46
61
  if (optional) return;
47
62
  const open = context.pages().map((page) => page.url());
48
63
  throw new Error(`[walletwright] approval popup did not appear. Open pages: ${open.join(" | ") || "none"}`);
49
64
  }
50
- await placeApprovalWindow(popup);
51
- try {
52
- await settle(popup);
53
- } catch (error) {
54
- if (!popup.isClosed()) throw error;
55
- const fresh = await find();
56
- if (!fresh) throw error;
57
- await settle(fresh);
58
- }
59
- const closed = await waitUntil(() => !hasNotificationPopup(context, extensionId, match), { timeoutMs: POPUP_CLOSE_TIMEOUT_MS });
60
- if (!optional && closed === void 0) throw new Error(`[walletwright] approval popup did not close after ${formatTimeout(POPUP_CLOSE_TIMEOUT_MS)} (the approval may not have registered)`);
65
+ const driven = await settleApproval(settle, popup, acquire);
66
+ if (await waitUntil(() => driven.isClosed() || void 0, { timeoutMs: POPUP_CLOSE_TIMEOUT_MS }) === void 0) throw new Error(`[walletwright] approval popup did not close after ${formatTimeout(POPUP_CLOSE_TIMEOUT_MS)} (the approval may not have registered)`);
61
67
  };
62
68
  const unsupported = (name) => /* @__PURE__ */ new Error(`[walletwright] ${definition.extensionName} does not support ${name}()`);
63
69
  const approve = (options = {}) => resolvePopup((popup) => definition.approve(popup, password), options);
64
- const reject = async (options = {}) => {
65
- const { reject: rejectPopup } = definition;
66
- if (!rejectPopup) throw unsupported("reject");
67
- await resolvePopup((popup) => rejectPopup(popup), options);
68
- };
69
- /**
70
- * Bind an optional capability, or fail loudly naming the wallet and action. A wallet declares only
71
- * what has been driven against the real extension, so an undeclared action is a real gap rather
72
- * than something to swallow. Extra args (e.g. a network config) forward after the context.
73
- *
74
- * After the action, focus returns to the dapp (see `frontDapp`), since actions drive the wallet's
75
- * own page via `bringToFront`.
76
- */
70
+ const reject = (options = {}) => resolvePopup((popup) => definition.reject(popup), options);
71
+ /** Bind a verified wallet action and restore focus to the dapp when it finishes. */
77
72
  const action = (fn, name) => async (...args) => {
78
73
  if (!fn) throw unsupported(name);
79
74
  if (home.isClosed()) throw new Error(`[walletwright] wallet home page is closed; cannot run ${name}()`);
@@ -111,50 +106,64 @@ const createWallet = ({ context, definition, extensionId, home, password }) => {
111
106
 
112
107
  //#endregion
113
108
  //#region src/internal/launch.ts
114
- /**
115
- * Drop the tabs nobody drives: Chromium's initial `about:blank` and the extension's own auto-opened
116
- * tab. We navigate a tab of our own to the wallet instead of adopting that one (it's unreliable, see
117
- * `reachUnlockScreen`), which otherwise leaves a second, still-locked wallet tab open all run.
118
- * Approval popups are unaffected: they're matched by URL, not by being the only extension page.
119
- */
109
+ const closeLaunch = async (context, runDir) => {
110
+ const closed = await Promise.allSettled([context.close()]);
111
+ const removed = await Promise.allSettled([rm(runDir, {
112
+ force: true,
113
+ recursive: true
114
+ })]);
115
+ const closeResult = closed[0];
116
+ const removeResult = removed[0];
117
+ if (closeResult?.status === "rejected" && removeResult?.status === "rejected") throw new AggregateError([closeResult.reason, removeResult.reason], `[walletwright] failed to close the browser and remove ${runDir}`);
118
+ if (closeResult?.status === "rejected") throw closeResult.reason;
119
+ if (removeResult?.status === "rejected") throw removeResult.reason;
120
+ };
121
+ /** Close Chromium's blank tab and the extension's auto-opened tab. */
120
122
  const closeStrayPages = async (context, home) => {
121
123
  const isStray = (page) => page !== home && !page.isClosed() && (page.url() === "about:blank" || page.url().startsWith("chrome-extension://"));
122
124
  const closing = [];
123
125
  for (const page of context.pages()) if (isStray(page)) closing.push(page.close());
124
126
  await Promise.allSettled(closing);
125
127
  };
126
- /**
127
- * Launch a fresh persistent context from the onboarded cache and return an unlocked wallet
128
- * controller. Headed by default; `headless` needs the wallet to declare `headlessApprovals`, since
129
- * some wallets create an approval window headless that is never exposed as a page. Outside
130
- * Playwright fixtures, `context.close()` is yours.
131
- */
128
+ /** Launch and unlock a disposable profile copy. Headless mode requires verified wallet support. */
132
129
  const launchWallet = async (setup, { headless = false } = {}) => {
133
130
  const definition = wallets[setup.wallet];
134
131
  if (headless && definition.headlessApprovals !== true) throw new Error(`[walletwright] ${definition.extensionName} has no verified headless approval flow; run this suite headed (\`use: { headless: false }\`, or \`--headed\`).`);
135
132
  const cacheDir = path.resolve(setup.cacheDir ?? ".walletwright");
136
133
  const profileDir = path.join(cacheDir, profileKey(setup));
134
+ await restorePreviousProfile(profileDir);
137
135
  if (!existsSync(profileDir)) throw new Error(`[walletwright] no cache for this setup at ${profileDir}. Build it first with buildCache() or \`walletwright cache\`.`);
138
136
  const extensionPath = await definition.prepareExtension(cacheDir, setup.version);
137
+ const extensionId = extensionIdFromPath(extensionPath);
138
+ /** Cleanup must cover copy and launch failures that occur before a context exists. */
139
139
  const runDir = await mkdtemp(path.join(os.tmpdir(), "walletwright-"));
140
- await cp(profileDir, runDir, { recursive: true });
141
- const context = await chromium.launchPersistentContext(runDir, extensionContextOptions(extensionPath, headless));
142
- context.on("close", async () => {
143
- await rm(runDir, {
144
- force: true,
145
- recursive: true
146
- }).catch(() => {});
140
+ const removeRunDir = () => rm(runDir, {
141
+ force: true,
142
+ recursive: true
147
143
  });
148
- const extensionId = extensionIdFromPath(extensionPath);
144
+ const handleContextClose = async () => {
145
+ try {
146
+ await removeRunDir();
147
+ } catch {}
148
+ };
149
+ let context;
149
150
  try {
150
- await definition.prepareContext?.(context);
151
- const home = await definition.reachUnlockScreen(context, extensionId);
151
+ await cp(profileDir, runDir, { recursive: true });
152
+ context = await chromium.launchPersistentContext(runDir, extensionContextOptions(extensionPath, headless));
153
+ const launched = context;
154
+ launched.on("close", handleContextClose);
155
+ await definition.prepareContext?.(launched);
156
+ const home = await definition.reachUnlockScreen(launched, extensionId);
152
157
  await definition.unlock(home, setup.password);
153
- await closeStrayPages(context, home);
158
+ await closeStrayPages(launched, home);
154
159
  return {
155
- context,
160
+ close: async () => {
161
+ launched.off("close", handleContextClose);
162
+ await closeLaunch(launched, runDir);
163
+ },
164
+ context: launched,
156
165
  wallet: createWallet({
157
- context,
166
+ context: launched,
158
167
  definition,
159
168
  extensionId,
160
169
  home,
@@ -162,26 +171,16 @@ const launchWallet = async (setup, { headless = false } = {}) => {
162
171
  })
163
172
  };
164
173
  } catch (error) {
165
- await context.close().catch(() => {});
174
+ context?.off("close", handleContextClose);
175
+ await context?.close().catch(() => {});
176
+ await removeRunDir().catch(() => {});
166
177
  throw error;
167
178
  }
168
179
  };
169
180
 
170
181
  //#endregion
171
182
  //#region src/fixtures.ts
172
- /**
173
- * Build a Playwright `test` whose `context`/`page` run inside a persistent Chromium with the wallet
174
- * loaded and unlocked, exposing a `wallet` fixture to approve connect/sign popups.
175
- *
176
- * ```ts
177
- * const test = createWalletFixtures(setup);
178
- * test("connect", async ({ page, wallet }) => {
179
- * await page.goto("/");
180
- * await page.getByRole("button", { name: "Connect" }).click();
181
- * await wallet.connectToDapp();
182
- * });
183
- * ```
184
- */
183
+ /** Create Playwright fixtures backed by one unlocked wallet launch per test. */
185
184
  const createWalletFixtures = (setup) => test.extend({
186
185
  context: async ({ walletLaunch }, use) => {
187
186
  await use(walletLaunch.context);
@@ -192,10 +191,10 @@ const createWalletFixtures = (setup) => test.extend({
192
191
  walletLaunch: async ({ headless }, use) => {
193
192
  const launched = await launchWallet(setup, { headless });
194
193
  await use(launched);
195
- await launched.context.close();
194
+ await launched.close();
196
195
  }
197
196
  });
198
197
 
199
198
  //#endregion
200
- export { buildCache, createWalletFixtures, launchWallet, walletKindsByEcosystem, wallets };
199
+ export { buildCache, createWalletFixtures, isWalletKind, launchWallet, walletKindsByEcosystem, wallets };
201
200
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":["base"],"sources":["../src/internal/controller.ts","../src/internal/launch.ts","../src/fixtures.ts"],"sourcesContent":["import type { BrowserContext, Page } from \"@playwright/test\";\n\nimport type { Wallet, WalletActionContext, WalletDefinition } from \"../types\";\n\nimport {\n DEFAULT_NOTIFICATION_MATCH,\n findNotificationPopup,\n hasNotificationPopup,\n placeApprovalWindow,\n} from \"./utils\";\nimport { formatTimeout, waitUntil } from \"./wait\";\n\nconst POPUP_CLOSE_TIMEOUT_MS = 15_000;\n/** Optional approvals keep a short wait: \"no popup\" is a normal outcome, and the wait is latency. */\nconst OPTIONAL_POPUP_TIMEOUT_MS = 10_000;\n/** The MV3 service worker spawns a popup slowly once the wallet's own UI has been driven. */\nconst REQUIRED_POPUP_TIMEOUT_MS = 30_000;\ntype ResolveOptions = { optional?: boolean };\n\ntype CreateWalletOptions = {\n context: BrowserContext;\n definition: WalletDefinition;\n extensionId: string;\n /** The wallet's own page, left open after unlock, that the actions drive. */\n home: Page;\n password: string;\n};\n\n/** Build the runtime controller that drives an unlocked wallet against the dapp under test. */\nexport const createWallet = ({\n context,\n definition,\n extensionId,\n home,\n password,\n}: CreateWalletOptions): Wallet => {\n const match = definition.notificationMatch ?? DEFAULT_NOTIFICATION_MATCH;\n const ctx: WalletActionContext = { context, extensionId, home, password };\n\n /**\n * Make the dapp the active tab. While an extension page holds that spot, MetaMask renders a new\n * approval inline in it rather than opening a notification window, and the window that does open\n * shows the wallet home instead of the request. The wallet's own page can end up fronted both\n * after an action and after a previous approval settles.\n */\n const frontDapp = async (): Promise<void> => {\n const dapp = context.pages().find((page) => /^https?:/v.test(page.url()) && !page.isClosed());\n await dapp?.bringToFront().catch(() => {});\n };\n\n /** Wait for the approval window the wallet opens for a pending request. */\n const findApproval = (optional: boolean): Promise<Page | undefined> =>\n findNotificationPopup({\n approvalControls: definition.approvalControls,\n context,\n extensionId,\n match,\n timeoutMs: optional ? OPTIONAL_POPUP_TIMEOUT_MS : REQUIRED_POPUP_TIMEOUT_MS,\n });\n\n /** Drive the pending approval popup with `settle`, then wait for it to close. */\n const resolvePopup = async (\n settle: (popup: Page) => Promise<void>,\n { optional = false }: ResolveOptions,\n ): Promise<void> => {\n await frontDapp();\n const find = () => findApproval(optional);\n const popup = await find();\n if (!popup) {\n if (optional) {\n return; // e.g. Phantom auto-approves an already-trusted site (no popup)\n }\n // Naming the open pages separates \"the wallet never opened one\" from \"it opened one showing\n // something other than the request\", which look identical from the timeout alone.\n const open = context.pages().map((page) => page.url());\n throw new Error(\n `[walletwright] approval popup did not appear. Open pages: ${open.join(\" | \") || \"none\"}`,\n );\n }\n await placeApprovalWindow(popup); // it can open partly off a small or virtual display\n try {\n await settle(popup);\n } catch (error) {\n // The finder can grab the previous popup in its final moments (the window closes right after\n // its own approval resolves). If ours died under us, one fresh find gets the real popup.\n if (!popup.isClosed()) {\n throw error;\n }\n const fresh = await find();\n if (!fresh) {\n throw error;\n }\n await settle(fresh);\n }\n\n // Wait for the popup to close so the next approval doesn't grab a stale page.\n const closed = await waitUntil(() => !hasNotificationPopup(context, extensionId, match), {\n timeoutMs: POPUP_CLOSE_TIMEOUT_MS,\n });\n if (!optional && closed === undefined) {\n throw new Error(\n `[walletwright] approval popup did not close after ${formatTimeout(POPUP_CLOSE_TIMEOUT_MS)} (the approval may not have registered)`,\n );\n }\n };\n\n const unsupported = (name: string): Error =>\n new Error(`[walletwright] ${definition.extensionName} does not support ${name}()`);\n\n const approve = (options: ResolveOptions = {}) =>\n resolvePopup((popup) => definition.approve(popup, password), options);\n\n const reject = async (options: ResolveOptions = {}): Promise<void> => {\n const { reject: rejectPopup } = definition;\n if (!rejectPopup) {\n throw unsupported(\"reject\");\n }\n await resolvePopup((popup) => rejectPopup(popup), options);\n };\n\n /**\n * Bind an optional capability, or fail loudly naming the wallet and action. A wallet declares only\n * what has been driven against the real extension, so an undeclared action is a real gap rather\n * than something to swallow. Extra args (e.g. a network config) forward after the context.\n *\n * After the action, focus returns to the dapp (see `frontDapp`), since actions drive the wallet's\n * own page via `bringToFront`.\n */\n const action =\n <A extends ReadonlyArray<unknown>>(\n fn: ((ctx: WalletActionContext, ...args: A) => Promise<void>) | undefined,\n name: string,\n ) =>\n async (...args: A): Promise<void> => {\n if (!fn) {\n throw unsupported(name);\n }\n if (home.isClosed()) {\n throw new Error(`[walletwright] wallet home page is closed; cannot run ${name}()`);\n }\n // Actions drive the wallet's own page; front it first so clicks land on a visible tab, then\n // hand focus back to the dapp below so new approvals open as popups instead of inline.\n await home.bringToFront().catch(() => {});\n await fn(ctx, ...args);\n await frontDapp();\n };\n\n // A capability is declared once, on the *Api types in types.ts: WalletActions derives from those,\n // so a new method makes this object fail to compile until it is bound. Only the fn-to-slot pairing\n // below can still be wrong, and capability-wiring.test.ts guards that.\n return {\n accounts: {\n add: action(definition.actions?.accounts?.add, \"accounts.add\"),\n importPrivateKey: action(\n definition.actions?.accounts?.importPrivateKey,\n \"accounts.importPrivateKey\",\n ),\n rename: action(definition.actions?.accounts?.rename, \"accounts.rename\"),\n switch: action(definition.actions?.accounts?.switch, \"accounts.switch\"),\n },\n approve,\n confirmSignature: () => approve(),\n confirmTransaction: () => approve(),\n // Connect may auto-approve on some wallets, so a missing popup is not an error here.\n connectToDapp: () => approve({ optional: true }),\n extensionId,\n home,\n network: {\n add: action(definition.actions?.network?.add, \"network.add\"),\n switch: action(definition.actions?.network?.switch, \"network.switch\"),\n },\n reject,\n rejectConnection: () => reject(),\n rejectSignature: () => reject(),\n rejectTransaction: () => reject(),\n settings: {\n lock: action(definition.actions?.settings?.lock, \"settings.lock\"),\n unlock: action(definition.actions?.settings?.unlock, \"settings.unlock\"),\n },\n };\n};\n","import { existsSync } from \"node:fs\";\nimport { cp, mkdtemp, rm } from \"node:fs/promises\";\nimport os from \"node:os\";\nimport path from \"node:path\";\n\nimport { type BrowserContext, chromium, type Page } from \"@playwright/test\";\n\nimport type { Wallet, WalletSetup } from \"../types\";\nimport { wallets } from \"../wallets/index\";\n\nimport { extensionContextOptions } from \"./chromium\";\nimport { createWallet } from \"./controller\";\nimport { DEFAULT_CACHE_DIR, extensionIdFromPath, profileKey } from \"./utils\";\n\nexport type LaunchedWallet = {\n context: BrowserContext;\n wallet: Wallet;\n};\n\n/**\n * Drop the tabs nobody drives: Chromium's initial `about:blank` and the extension's own auto-opened\n * tab. We navigate a tab of our own to the wallet instead of adopting that one (it's unreliable, see\n * `reachUnlockScreen`), which otherwise leaves a second, still-locked wallet tab open all run.\n * Approval popups are unaffected: they're matched by URL, not by being the only extension page.\n */\nconst closeStrayPages = async (context: BrowserContext, home: Page): Promise<void> => {\n const isStray = (page: Page): boolean =>\n page !== home &&\n !page.isClosed() &&\n (page.url() === \"about:blank\" || page.url().startsWith(\"chrome-extension://\"));\n\n const closing: Array<Promise<void>> = [];\n for (const page of context.pages()) {\n if (isStray(page)) {\n closing.push(page.close());\n }\n }\n await Promise.allSettled(closing);\n};\n\n/**\n * Launch a fresh persistent context from the onboarded cache and return an unlocked wallet\n * controller. Headed by default; `headless` needs the wallet to declare `headlessApprovals`, since\n * some wallets create an approval window headless that is never exposed as a page. Outside\n * Playwright fixtures, `context.close()` is yours.\n */\nexport const launchWallet = async (\n setup: WalletSetup,\n { headless = false }: { headless?: boolean } = {},\n): Promise<LaunchedWallet> => {\n const definition = wallets[setup.wallet];\n // A wallet whose approval window never surfaces headless reaches no approval at all, and would\n // fail 30s later at the first connect with nothing pointing back at the mode as the cause.\n if (headless && definition.headlessApprovals !== true) {\n throw new Error(\n `[walletwright] ${definition.extensionName} has no verified headless approval flow; run this suite headed (\\`use: { headless: false }\\`, or \\`--headed\\`).`,\n );\n }\n const cacheDir = path.resolve(setup.cacheDir ?? DEFAULT_CACHE_DIR);\n const profileDir = path.join(cacheDir, profileKey(setup));\n if (!existsSync(profileDir)) {\n throw new Error(\n `[walletwright] no cache for this setup at ${profileDir}. Build it first with buildCache() or \\`walletwright cache\\`.`,\n );\n }\n\n const extensionPath = await definition.prepareExtension(cacheDir, setup.version);\n\n // Run from a throwaway copy so the cache stays pristine and parallel runs don't share a profile.\n const runDir = await mkdtemp(path.join(os.tmpdir(), \"walletwright-\"));\n await cp(profileDir, runDir, { recursive: true });\n\n const context = await chromium.launchPersistentContext(\n runDir,\n extensionContextOptions(extensionPath, headless),\n );\n\n // The throwaway profile copy is only needed while the context is live; drop it on close so a long\n // suite (workers x specs x retries) doesn't fill the temp dir with profile copies.\n context.on(\"close\", async () => {\n await rm(runDir, { force: true, recursive: true }).catch(() => {});\n });\n\n const extensionId = extensionIdFromPath(extensionPath);\n\n try {\n await definition.prepareContext?.(context);\n // Kept open (not closed after unlock): the settings/network/account actions drive this page.\n const home = await definition.reachUnlockScreen(context, extensionId);\n await definition.unlock(home, setup.password);\n await closeStrayPages(context, home);\n\n return {\n context,\n wallet: createWallet({\n context,\n definition,\n extensionId,\n home,\n password: setup.password,\n }),\n };\n } catch (error) {\n // Fires the context.on(\"close\") handler above, which removes the throwaway runDir.\n await context.close().catch(() => {});\n throw error;\n }\n};\n","import { test as base } from \"@playwright/test\";\n\nimport { type LaunchedWallet, launchWallet } from \"./internal/launch\";\nimport type { Wallet, WalletSetup } from \"./types\";\n\nexport type WalletFixtures = {\n wallet: Wallet;\n};\n\n/** One launch per test, which the `context` and `wallet` fixtures then read from. */\ntype LaunchFixture = {\n walletLaunch: LaunchedWallet;\n};\n\n/**\n * Build a Playwright `test` whose `context`/`page` run inside a persistent Chromium with the wallet\n * loaded and unlocked, exposing a `wallet` fixture to approve connect/sign popups.\n *\n * ```ts\n * const test = createWalletFixtures(setup);\n * test(\"connect\", async ({ page, wallet }) => {\n * await page.goto(\"/\");\n * await page.getByRole(\"button\", { name: \"Connect\" }).click();\n * await wallet.connectToDapp();\n * });\n * ```\n */\nexport const createWalletFixtures = (setup: WalletSetup) =>\n base.extend<LaunchFixture & WalletFixtures>({\n context: async ({ walletLaunch }, use) => {\n await use(walletLaunch.context);\n },\n wallet: async ({ walletLaunch }, use) => {\n await use(walletLaunch.wallet);\n },\n // Depends only on Playwright's `headless` option (so `--headed` and `use: { headless }` reach\n // the wallet): its own `browser` fixture would launch a second Chromium that no test ever\n // drives, since the wallet needs a persistent context of its own.\n walletLaunch: async ({ headless }, use) => {\n const launched = await launchWallet(setup, { headless });\n await use(launched);\n await launched.context.close();\n },\n });\n"],"mappings":";;;;;;;;AAYA,MAAM,yBAAyB;;AAE/B,MAAM,4BAA4B;;AAElC,MAAM,4BAA4B;;AAalC,MAAa,gBAAgB,EAC3B,SACA,YACA,aACA,MACA,eACiC;CACjC,MAAM,QAAQ,WAAW;CACzB,MAAM,MAA2B;EAAE;EAAS;EAAa;EAAM;CAAS;;;;;;;CAQxE,MAAM,YAA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AO;EAEd,MAAM,QAAQ,MAAM,CAAC,CAAC,YAAY,CAAC,CAAC;EACpC,MAAM;CACR;AACF;;;;;;;;;;;;;;;;;AChFA,MAAa,wBAAwB,UACnCA,KAAK,OAAuC;CAC1C,SAAS,OAAO,EAAE,gBAAgB,QAAQ;EACxC,MAAM,IAAI,aAAa,OAAO;CAChC;CACA,QAAQ,OAAO,EAAE,gBAAgB,QAAQ;EACvC,MAAM,IAAI,aAAa,MAAM;CAC/B;CAIA,cAAc,OAAO,EAAE,YAAY,QAAQ;EACzC,MAAM,WAAW,MAAM,aAAa,OAAO,EAAE,SAAS,CAAC;EACvD,MAAM,IAAI,QAAQ;EAClB,MAAM,SAAS,QAAQ,MAAM;CAC/B;AACF,CAAC"}
1
+ {"version":3,"file":"index.mjs","names":["base"],"sources":["../src/internal/controller.ts","../src/internal/launch.ts","../src/fixtures.ts"],"sourcesContent":["import type { BrowserContext, Page } from \"@playwright/test\";\n\nimport type { Wallet, WalletActionContext, WalletDefinition } from \"../types\";\n\nimport { DEFAULT_NOTIFICATION_MATCH, findNotificationPopup, placeApprovalWindow } from \"./utils\";\nimport { formatTimeout, waitUntil } from \"./wait\";\n\nconst POPUP_CLOSE_TIMEOUT_MS = 15_000;\n/** Optional approvals keep a short wait: \"no popup\" is a normal outcome, and the wait is latency. */\nconst OPTIONAL_POPUP_TIMEOUT_MS = 10_000;\n/** The MV3 service worker spawns a popup slowly once the wallet's own UI has been driven. */\nconst REQUIRED_POPUP_TIMEOUT_MS = 30_000;\ntype ResolveOptions = { optional?: boolean };\n\n/**\n * Drive `popup` with `settle` and return the page that actually took the approval. A popup that\n * closes mid-settle is the wallet respawning the request, so re-acquire once and drive the new one.\n */\nconst settleApproval = async (\n settle: (popup: Page) => Promise<void>,\n popup: Page,\n acquire: () => Promise<Page | undefined>,\n): Promise<Page> => {\n try {\n await settle(popup);\n return popup;\n } catch (error) {\n if (!popup.isClosed()) {\n throw error;\n }\n const fresh = await acquire();\n if (!fresh) {\n throw error;\n }\n await settle(fresh);\n return fresh;\n }\n};\n\ntype CreateWalletOptions = {\n context: BrowserContext;\n definition: WalletDefinition;\n extensionId: string;\n /** The wallet's own page, left open after unlock, that the actions drive. */\n home: Page;\n password: string;\n};\n\n/** Build the runtime controller that drives an unlocked wallet against the dapp under test. */\nexport const createWallet = ({\n context,\n definition,\n extensionId,\n home,\n password,\n}: CreateWalletOptions): Wallet => {\n const match = definition.notificationMatch ?? DEFAULT_NOTIFICATION_MATCH;\n const ctx: WalletActionContext = { context, extensionId, home, password };\n\n /** MetaMask opens requests inline while its extension page owns focus. */\n const frontDapp = async (): Promise<void> => {\n const dapp = context.pages().find((page) => /^https?:/v.test(page.url()) && !page.isClosed());\n await dapp?.bringToFront().catch(() => {});\n };\n\n /** Find and place the approval window before its controls are driven. */\n const acquireApproval = async (optional: boolean): Promise<Page | undefined> => {\n const popup = await findNotificationPopup({\n approvalControls: definition.approvalControls,\n context,\n extensionId,\n match,\n timeoutMs: optional ? OPTIONAL_POPUP_TIMEOUT_MS : REQUIRED_POPUP_TIMEOUT_MS,\n });\n if (popup) {\n await placeApprovalWindow(popup);\n }\n return popup;\n };\n\n /** Drive the pending approval popup with `settle`, then wait for that popup to close. */\n const resolvePopup = async (\n settle: (popup: Page) => Promise<void>,\n { optional = false }: ResolveOptions,\n ): Promise<void> => {\n await frontDapp();\n const acquire = () => acquireApproval(optional);\n const popup = await acquire();\n if (!popup) {\n if (optional) {\n return; // e.g. Phantom auto-approves an already-trusted site (no popup)\n }\n const open = context.pages().map((page) => page.url());\n throw new Error(\n `[walletwright] approval popup did not appear. Open pages: ${open.join(\" | \") || \"none\"}`,\n );\n }\n\n const driven = await settleApproval(settle, popup, acquire);\n\n /** Wait for the exact popup we drove; chained popups may reuse the same URL. */\n const closed = await waitUntil(() => driven.isClosed() || undefined, {\n timeoutMs: POPUP_CLOSE_TIMEOUT_MS,\n });\n if (closed === undefined) {\n throw new Error(\n `[walletwright] approval popup did not close after ${formatTimeout(POPUP_CLOSE_TIMEOUT_MS)} (the approval may not have registered)`,\n );\n }\n };\n\n const unsupported = (name: string): Error =>\n new Error(`[walletwright] ${definition.extensionName} does not support ${name}()`);\n\n const approve = (options: ResolveOptions = {}) =>\n resolvePopup((popup) => definition.approve(popup, password), options);\n\n const reject = (options: ResolveOptions = {}) =>\n resolvePopup((popup) => definition.reject(popup), options);\n\n /** Bind a verified wallet action and restore focus to the dapp when it finishes. */\n const action =\n <A extends ReadonlyArray<unknown>>(\n fn: ((ctx: WalletActionContext, ...args: A) => Promise<void>) | undefined,\n name: string,\n ) =>\n async (...args: A): Promise<void> => {\n if (!fn) {\n throw unsupported(name);\n }\n if (home.isClosed()) {\n throw new Error(`[walletwright] wallet home page is closed; cannot run ${name}()`);\n }\n await home.bringToFront().catch(() => {});\n await fn(ctx, ...args);\n await frontDapp();\n };\n\n return {\n accounts: {\n add: action(definition.actions?.accounts?.add, \"accounts.add\"),\n importPrivateKey: action(\n definition.actions?.accounts?.importPrivateKey,\n \"accounts.importPrivateKey\",\n ),\n rename: action(definition.actions?.accounts?.rename, \"accounts.rename\"),\n switch: action(definition.actions?.accounts?.switch, \"accounts.switch\"),\n },\n approve,\n confirmSignature: () => approve(),\n confirmTransaction: () => approve(),\n connectToDapp: () => approve({ optional: true }),\n extensionId,\n home,\n network: {\n add: action(definition.actions?.network?.add, \"network.add\"),\n switch: action(definition.actions?.network?.switch, \"network.switch\"),\n },\n reject,\n rejectConnection: () => reject(),\n rejectSignature: () => reject(),\n rejectTransaction: () => reject(),\n settings: {\n lock: action(definition.actions?.settings?.lock, \"settings.lock\"),\n unlock: action(definition.actions?.settings?.unlock, \"settings.unlock\"),\n },\n };\n};\n","import { existsSync } from \"node:fs\";\nimport { cp, mkdtemp, rm } from \"node:fs/promises\";\nimport os from \"node:os\";\nimport path from \"node:path\";\n\nimport { type BrowserContext, chromium, type Page } from \"@playwright/test\";\n\nimport type { Wallet, WalletSetup } from \"../types\";\nimport { wallets } from \"../wallets/index\";\n\nimport { restorePreviousProfile } from \"./cache\";\nimport { extensionContextOptions } from \"./chromium\";\nimport { createWallet } from \"./controller\";\nimport { DEFAULT_CACHE_DIR, extensionIdFromPath, profileKey } from \"./utils\";\n\nexport type LaunchedWallet = {\n /**\n * Close the browser and remove the throwaway profile copy, in that order. Prefer this over\n * `context.close()`: the copy is a full onboarded profile (tens of MB), and awaiting its removal is\n * the only way to know it is gone before the process exits.\n */\n close: () => Promise<void>;\n context: BrowserContext;\n wallet: Wallet;\n};\n\nconst closeLaunch = async (context: BrowserContext, runDir: string): Promise<void> => {\n const closed = await Promise.allSettled([context.close()]);\n const removed = await Promise.allSettled([rm(runDir, { force: true, recursive: true })]);\n const closeResult = closed[0];\n const removeResult = removed[0];\n\n if (closeResult?.status === \"rejected\" && removeResult?.status === \"rejected\") {\n throw new AggregateError(\n [closeResult.reason, removeResult.reason],\n `[walletwright] failed to close the browser and remove ${runDir}`,\n );\n }\n if (closeResult?.status === \"rejected\") {\n throw closeResult.reason;\n }\n if (removeResult?.status === \"rejected\") {\n throw removeResult.reason;\n }\n};\n\n/** Close Chromium's blank tab and the extension's auto-opened tab. */\nconst closeStrayPages = async (context: BrowserContext, home: Page): Promise<void> => {\n const isStray = (page: Page): boolean =>\n page !== home &&\n !page.isClosed() &&\n (page.url() === \"about:blank\" || page.url().startsWith(\"chrome-extension://\"));\n\n const closing: Array<Promise<void>> = [];\n for (const page of context.pages()) {\n if (isStray(page)) {\n closing.push(page.close());\n }\n }\n await Promise.allSettled(closing);\n};\n\n/** Launch and unlock a disposable profile copy. Headless mode requires verified wallet support. */\nexport const launchWallet = async (\n setup: WalletSetup,\n { headless = false }: { headless?: boolean } = {},\n): Promise<LaunchedWallet> => {\n const definition = wallets[setup.wallet];\n if (headless && definition.headlessApprovals !== true) {\n throw new Error(\n `[walletwright] ${definition.extensionName} has no verified headless approval flow; run this suite headed (\\`use: { headless: false }\\`, or \\`--headed\\`).`,\n );\n }\n const cacheDir = path.resolve(setup.cacheDir ?? DEFAULT_CACHE_DIR);\n const profileDir = path.join(cacheDir, profileKey(setup));\n await restorePreviousProfile(profileDir);\n if (!existsSync(profileDir)) {\n throw new Error(\n `[walletwright] no cache for this setup at ${profileDir}. Build it first with buildCache() or \\`walletwright cache\\`.`,\n );\n }\n\n const extensionPath = await definition.prepareExtension(cacheDir, setup.version);\n\n const extensionId = extensionIdFromPath(extensionPath);\n\n /** Cleanup must cover copy and launch failures that occur before a context exists. */\n const runDir = await mkdtemp(path.join(os.tmpdir(), \"walletwright-\"));\n const removeRunDir = (): Promise<void> => rm(runDir, { force: true, recursive: true });\n const handleContextClose = async (): Promise<void> => {\n try {\n await removeRunDir();\n } catch {\n // Playwright does not await close-event listeners; `close()` owns strict cleanup.\n }\n };\n\n let context: BrowserContext | undefined;\n try {\n await cp(profileDir, runDir, { recursive: true });\n\n context = await chromium.launchPersistentContext(\n runDir,\n extensionContextOptions(extensionPath, headless),\n );\n const launched = context;\n launched.on(\"close\", handleContextClose);\n\n await definition.prepareContext?.(launched);\n const home = await definition.reachUnlockScreen(launched, extensionId);\n await definition.unlock(home, setup.password);\n await closeStrayPages(launched, home);\n\n return {\n close: async () => {\n launched.off(\"close\", handleContextClose);\n await closeLaunch(launched, runDir);\n },\n context: launched,\n wallet: createWallet({\n context: launched,\n definition,\n extensionId,\n home,\n password: setup.password,\n }),\n };\n } catch (error) {\n context?.off(\"close\", handleContextClose);\n await context?.close().catch(() => {});\n await removeRunDir().catch(() => {});\n throw error;\n }\n};\n\nexport { closeLaunch };\n","import { test as base } from \"@playwright/test\";\n\nimport { type LaunchedWallet, launchWallet } from \"./internal/launch\";\nimport type { Wallet, WalletSetup } from \"./types\";\n\nexport type WalletFixtures = {\n wallet: Wallet;\n};\n\n/** One launch per test, which the `context` and `wallet` fixtures then read from. */\ntype LaunchFixture = {\n walletLaunch: LaunchedWallet;\n};\n\n/** Create Playwright fixtures backed by one unlocked wallet launch per test. */\nexport const createWalletFixtures = (setup: WalletSetup) =>\n base.extend<LaunchFixture & WalletFixtures>({\n context: async ({ walletLaunch }, use) => {\n await use(walletLaunch.context);\n },\n wallet: async ({ walletLaunch }, use) => {\n await use(walletLaunch.wallet);\n },\n walletLaunch: async ({ headless }, use) => {\n const launched = await launchWallet(setup, { headless });\n await use(launched);\n await launched.close();\n },\n });\n"],"mappings":";;;;;;;;AAOA,MAAM,yBAAyB;;AAE/B,MAAM,4BAA4B;;AAElC,MAAM,4BAA4B;;;;;AAOlC,MAAM,iBAAiB,OACrB,QACA,OACA,YACkB;CAClB,IAAI;EACF,MAAM,OAAO,KAAK;EAClB,OAAO;CACT,SAAS,OAAO;EACd,IAAI,CAAC,MAAM,SAAS,GAClB,MAAM;EAER,MAAM,QAAQ,MAAM,QAAQ;EAC5B,IAAI,CAAC,OACH,MAAM;EAER,MAAM,OAAO,KAAK;EAClB,OAAO;CACT;AACF;;AAYA,MAAa,gBAAgB,EAC3B,SACA,YACA,aACA,MACA,eACiC;CACjC,MAAM,QAAQ,WAAW;CACzB,MAAM,MAA2B;EAAE;EAAS;EAAa;EAAM;CAAS;;CAGxE,MAAM,YAAY,YAA2B;EAE3C,MADa,QAAQ,MAAM,CAAC,CAAC,MAAM,SAAS,0BAAU,CAAC,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK,CAAC,KAAK,SAAS,CAClF,CAAC,EAAE,aAAa,CAAC,CAAC,YAAY,CAAC,CAAC;CAC3C;;CAGA,MAAM,kBAAkB,OAAO,aAAiD;EAC9E,MAAM,QAAQ,MAAM,sBAAsB;GACxC,kBAAkB,WAAW;GAC7B;GACA;GACA;GACA,WAAW,WAAW,4BAA4B;EACpD,CAAC;EACD,IAAI,OACF,MAAM,oBAAoB,KAAK;EAEjC,OAAO;CACT;;CAGA,MAAM,eAAe,OACnB,QACA,EAAE,WAAW,YACK;EAClB,MAAM,UAAU;EAChB,MAAM,gBAAgB,gBAAgB,QAAQ;EAC9C,MAAM,QAAQ,MAAM,QAAQ;EAC5B,IAAI,CAAC,OAAO;GACV,IAAI,UACF;GA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C,YAAY,CAAC,CAAC;EACnC,MAAM;CACR;AACF;;;;;ACtHA,MAAa,wBAAwB,UACnCA,KAAK,OAAuC;CAC1C,SAAS,OAAO,EAAE,gBAAgB,QAAQ;EACxC,MAAM,IAAI,aAAa,OAAO;CAChC;CACA,QAAQ,OAAO,EAAE,gBAAgB,QAAQ;EACvC,MAAM,IAAI,aAAa,MAAM;CAC/B;CACA,cAAc,OAAO,EAAE,YAAY,QAAQ;EACzC,MAAM,WAAW,MAAM,aAAa,OAAO,EAAE,SAAS,CAAC;EACvD,MAAM,IAAI,QAAQ;EAClB,MAAM,SAAS,MAAM;CACvB;AACF,CAAC"}
@@ -1,18 +1,7 @@
1
1
  import { BrowserContext, Page } from "@playwright/test";
2
2
  //#region src/mock-standard.d.ts
3
- /**
4
- * A headless Wallet-Standard fake for Solana dapp tests that don't need a real extension. It
5
- * hand-rolls the Wallet-Standard registration protocol (no browser-side library) and answers
6
- * `standard:connect` plus `solana:signMessage`, signing with a real ed25519 key so signatures
7
- * verify. Isolated from the EVM `mock.ts`; `@walletwright/core/mock-standard` is its own entry
8
- * point.
9
- *
10
- * ed25519 signing runs Node-side (viem is secp256k1-only), reusing the same exposeFunction bridge
11
- * pattern as the EVM mock. No extra dependency: Node's built-in `crypto` signs ed25519 natively.
12
- */
13
- type MockEcosystem = "svm";
3
+ /** Headless Wallet-Standard Solana mock that signs messages with Node's ed25519 implementation. */
14
4
  type MockStandardWalletOptions = {
15
- ecosystem?: MockEcosystem;
16
5
  /** Wallet name announced over Wallet Standard. */
17
6
  name?: string;
18
7
  /** 32-byte ed25519 seed as hex. Defaults to a fixed test seed so the address is stable. */
@@ -25,17 +14,13 @@ type MockStandardAccount = {
25
14
  };
26
15
  declare const encodeBase58: (bytes: Uint8Array) => string;
27
16
  declare const privateKeyFromSeed: (seed: Buffer) => import("node:crypto").KeyObject;
17
+ /** `message` is required: defaulting it would sign the empty message instead of rejecting. */
28
18
  type BridgeRequest = {
29
- message?: Array<number>;
19
+ message: Array<number>;
30
20
  method: string;
31
21
  };
32
22
  declare const createStandardHandler: (seed: Buffer) => ({ message, method }: BridgeRequest) => Promise<Array<number>>;
33
- /**
34
- * Install the headless Wallet-Standard mock for Solana connect + sign on a context (every page) or a
35
- * single page. Call before `goto`, so the wallet is registered when the dapp discovers wallets.
36
- * Returns the account it announces so a test can verify signatures against the public key.
37
- */
38
23
  declare const installMockStandardWallet: (target: BrowserContext | Page, options?: MockStandardWalletOptions) => Promise<MockStandardAccount>;
39
24
  //#endregion
40
- export { type MockEcosystem, type MockStandardAccount, type MockStandardWalletOptions, createStandardHandler, encodeBase58, installMockStandardWallet, privateKeyFromSeed };
25
+ export { type MockStandardAccount, type MockStandardWalletOptions, createStandardHandler, encodeBase58, installMockStandardWallet, privateKeyFromSeed };
41
26
  //# sourceMappingURL=mock-standard.d.mts.map
@@ -35,7 +35,7 @@ const rawPublicKey = (seed) => {
35
35
  };
36
36
  const signEd25519 = (seed, message) => [...sign(null, Buffer.from(message), privateKeyFromSeed(seed))];
37
37
  const SVM_CHAINS = ["solana:mainnet", "solana:devnet"];
38
- const createStandardHandler = (seed) => ({ message = [], method }) => {
38
+ const createStandardHandler = (seed) => ({ message, method }) => {
39
39
  switch (method) {
40
40
  case "solana:signMessage": return Promise.resolve(signEd25519(seed, Uint8Array.from(message)));
41
41
  default: return Promise.reject(/* @__PURE__ */ new Error(`[walletwright/mock-standard] unsupported method: ${method}`));
@@ -46,24 +46,21 @@ const createStandardHandler = (seed) => ({ message = [], method }) => {
46
46
  * single page. Call before `goto`, so the wallet is registered when the dapp discovers wallets.
47
47
  * Returns the account it announces so a test can verify signatures against the public key.
48
48
  */
49
+ /** Per install, for the same reason as the EVM mock: see the note on `installCount` in `mock.ts`. */
50
+ let installCount = 0;
49
51
  const installMockStandardWallet = async (target, options = {}) => {
50
- const { ecosystem = "svm", name = "Walletwright Mock", seedHex = DEFAULT_SEED_HEX } = options;
51
- if (ecosystem !== "svm") throw new Error(`[walletwright/mock-standard] prototype supports only svm, got ${String(ecosystem)}`);
52
+ const { name = "Walletwright Mock", seedHex = DEFAULT_SEED_HEX } = options;
52
53
  const seed = Buffer.from(seedHex, "hex");
53
54
  if (seed.length !== 32) throw new Error("[walletwright/mock-standard] seedHex must be 32 bytes");
54
55
  const publicKey = rawPublicKey(seed);
55
56
  const address = encodeBase58(publicKey);
56
57
  const publicKeyHex = publicKey.toString("hex");
57
58
  const handle = createStandardHandler(seed);
58
- const bindingName = "__walletwrightMockStandardSign";
59
- try {
60
- await target.exposeFunction(bindingName, (rpc) => handle(rpc));
61
- } catch (error) {
62
- if (!(error instanceof Error && error.message.includes("already registered"))) throw error;
63
- }
59
+ const bindingName = `__walletwrightMockStandardSign_${installCount++}`;
60
+ await target.exposeFunction(bindingName, (rpc) => handle(rpc));
64
61
  await target.addInitScript(([binding, info]) => {
65
62
  const call = window[binding];
66
- const publicKeyBytes = Uint8Array.from((info.publicKeyHex.match(/* @__PURE__ */ new RegExp(".{2}", "gv")) ?? []).map((byte) => Number.parseInt(byte, 16)));
63
+ const publicKeyBytes = Uint8Array.from((info.publicKeyHex.match(new RegExp(".{2}", "gv")) ?? []).map((byte) => Number.parseInt(byte, 16)));
67
64
  const account = {
68
65
  address: info.address,
69
66
  chains: info.chains,
@@ -1 +1 @@
1
- {"version":3,"file":"mock-standard.mjs","names":[],"sources":["../src/mock-standard.ts"],"sourcesContent":["import { createPrivateKey, createPublicKey, sign } from \"node:crypto\";\n\nimport type { BrowserContext, Page } from \"@playwright/test\";\n\n/**\n * A headless Wallet-Standard fake for Solana dapp tests that don't need a real extension. It\n * hand-rolls the Wallet-Standard registration protocol (no browser-side library) and answers\n * `standard:connect` plus `solana:signMessage`, signing with a real ed25519 key so signatures\n * verify. Isolated from the EVM `mock.ts`; `@walletwright/core/mock-standard` is its own entry\n * point.\n *\n * ed25519 signing runs Node-side (viem is secp256k1-only), reusing the same exposeFunction bridge\n * pattern as the EVM mock. No extra dependency: Node's built-in `crypto` signs ed25519 natively.\n */\ntype MockEcosystem = \"svm\";\n\ntype MockStandardWalletOptions = {\n ecosystem?: MockEcosystem;\n /** Wallet name announced over Wallet Standard. */\n name?: string;\n /** 32-byte ed25519 seed as hex. Defaults to a fixed test seed so the address is stable. */\n seedHex?: string;\n};\n\ntype MockStandardAccount = {\n address: string;\n /** Raw 32-byte ed25519 public key, hex-encoded. */\n publicKeyHex: string;\n};\n\n// A fixed 32-byte seed so the derived Solana address is stable across runs.\nconst DEFAULT_SEED_HEX = \"0101010101010101010101010101010101010101010101010101010101010101\";\n\nconst PKCS8_ED25519_PREFIX = Buffer.from(\"302e020100300506032b657004220420\", \"hex\");\n\nconst BASE58_ALPHABET = \"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz\";\n\nconst encodeBase58 = (bytes: Uint8Array): string => {\n let leadingZeros = 0;\n while (leadingZeros < bytes.length && bytes[leadingZeros] === 0) {\n leadingZeros += 1;\n }\n const digits: Array<number> = [];\n for (const byte of bytes) {\n let carry = byte;\n for (let index = 0; index < digits.length; index += 1) {\n carry += digits[index] * 256;\n digits[index] = carry % 58;\n carry = Math.floor(carry / 58);\n }\n while (carry > 0) {\n digits.push(carry % 58);\n carry = Math.floor(carry / 58);\n }\n }\n const prefix = \"1\".repeat(leadingZeros);\n const body = digits\n .toReversed()\n .map((digit) => BASE58_ALPHABET[digit])\n .join(\"\");\n return prefix + body;\n};\n\nconst privateKeyFromSeed = (seed: Buffer) =>\n createPrivateKey({\n format: \"der\",\n key: Buffer.concat([PKCS8_ED25519_PREFIX, seed]),\n type: \"pkcs8\",\n });\n\nconst rawPublicKey = (seed: Buffer): Buffer => {\n const spki = createPublicKey(privateKeyFromSeed(seed)).export({ format: \"der\", type: \"spki\" });\n return spki.subarray(-32);\n};\n\nconst signEd25519 = (seed: Buffer, message: Uint8Array): Array<number> => [\n ...sign(null, Buffer.from(message), privateKeyFromSeed(seed)),\n];\n\nconst SVM_CHAINS = [\"solana:mainnet\", \"solana:devnet\"] as const;\n\ntype BridgeRequest = { message?: Array<number>; method: string };\n\nconst createStandardHandler =\n (seed: Buffer) =>\n ({ message = [], method }: BridgeRequest): Promise<Array<number>> => {\n switch (method) {\n case \"solana:signMessage\": {\n return Promise.resolve(signEd25519(seed, Uint8Array.from(message)));\n }\n default: {\n return Promise.reject(\n new Error(`[walletwright/mock-standard] unsupported method: ${method}`),\n );\n }\n }\n };\n\n/**\n * Install the headless Wallet-Standard mock for Solana connect + sign on a context (every page) or a\n * single page. Call before `goto`, so the wallet is registered when the dapp discovers wallets.\n * Returns the account it announces so a test can verify signatures against the public key.\n */\nconst installMockStandardWallet = async (\n target: BrowserContext | Page,\n options: MockStandardWalletOptions = {},\n): Promise<MockStandardAccount> => {\n const { ecosystem = \"svm\", name = \"Walletwright Mock\", seedHex = DEFAULT_SEED_HEX } = options;\n if (ecosystem !== \"svm\") {\n throw new Error(\n `[walletwright/mock-standard] prototype supports only svm, got ${String(ecosystem)}`,\n );\n }\n const seed = Buffer.from(seedHex, \"hex\");\n if (seed.length !== 32) {\n throw new Error(\"[walletwright/mock-standard] seedHex must be 32 bytes\");\n }\n const publicKey = rawPublicKey(seed);\n const address = encodeBase58(publicKey);\n const publicKeyHex = publicKey.toString(\"hex\");\n const handle = createStandardHandler(seed);\n\n const bindingName = \"__walletwrightMockStandardSign\";\n try {\n await target.exposeFunction(bindingName, (rpc: BridgeRequest) => handle(rpc));\n } catch (error) {\n if (!(error instanceof Error && error.message.includes(\"already registered\"))) {\n throw error;\n }\n }\n\n await target.addInitScript(\n ([binding, info]) => {\n const call = (\n window as unknown as Record<string, (rpc: BridgeRequest) => Promise<Array<number>>>\n )[binding];\n const publicKeyBytes = Uint8Array.from(\n (info.publicKeyHex.match(/.{2}/gv) ?? []).map((byte) => Number.parseInt(byte, 16)),\n );\n const account = {\n address: info.address,\n chains: info.chains,\n features: [\"solana:signMessage\"],\n label: info.name,\n publicKey: publicKeyBytes,\n };\n const wallet = {\n accounts: [account],\n chains: info.chains,\n features: {\n \"solana:signMessage\": {\n signMessage: (...inputs: Array<{ message: Uint8Array }>) =>\n Promise.all(\n inputs.map(async ({ message }) => ({\n signature: Uint8Array.from(\n await call({ message: [...message], method: \"solana:signMessage\" }),\n ),\n signedMessage: message,\n })),\n ),\n version: \"1.0.0\",\n },\n \"standard:connect\": {\n connect: () => Promise.resolve({ accounts: [account] }),\n version: \"1.0.0\",\n },\n \"standard:events\": {\n on: () => () => {},\n version: \"1.0.0\",\n },\n },\n icon: info.icon,\n name: info.name,\n version: \"1.0.0\" as const,\n };\n\n const callback = (api: { register: (w: typeof wallet) => void }) => {\n api.register(wallet);\n };\n try {\n window.dispatchEvent(\n new CustomEvent(\"wallet-standard:register-wallet\", { detail: callback }),\n );\n } catch {\n // The app may not be listening yet; the app-ready handler below covers that case.\n }\n window.addEventListener(\"wallet-standard:app-ready\", (event) => {\n callback((event as CustomEvent<{ register: (w: typeof wallet) => void }>).detail);\n });\n },\n [\n bindingName,\n {\n address,\n chains: [...SVM_CHAINS],\n icon: \"data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'/>\",\n name,\n publicKeyHex,\n },\n ] as const,\n );\n\n return { address, publicKeyHex };\n};\n\nexport { createStandardHandler, encodeBase58, installMockStandardWallet, privateKeyFromSeed };\nexport type { MockEcosystem, MockStandardAccount, MockStandardWalletOptions };\n"],"mappings":";;;AA+BA,MAAM,mBAAmB;AAEzB,MAAM,uBAAuB,OAAO,KAAK,oCAAoC,KAAK;AAElF,MAAM,kBAAkB;AAExB,MAAM,gBAAgB,UAA8B;CAClD,IAAI,eAAe;CACnB,OAAO,eAAe,MAAM,UAAU,MAAM,kBAAkB,GAC5D,gBAAgB;CAElB,MAAM,SAAwB,CAAC;CAC/B,KAAK,MAAM,QAAQ,OAAO;EACxB,IAAI,QAAQ;EACZ,KAAK,IAAI,QAAQ,GAAG,QAAQ,OAAO,QAAQ,SAAS,GAAG;GACrD,SAAS,OAAO,SAAS;GACzB,OAAO,SAAS,QAAQ;GACxB,QAAQ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iBAAiB,8BAA8B,UAAU;GAC9D,SAAU,MAAgE,MAAM;EAClF,CAAC;CACH,GACA,CACE,aACA;EACE;EACA,QAAQ,CAAC,GAAG,UAAU;EACtB,MAAM;EACN;EACA;CACF,CACF,CACF;CAEA,OAAO;EAAE;EAAS;CAAa;AACjC"}
1
+ {"version":3,"file":"mock-standard.mjs","names":[],"sources":["../src/mock-standard.ts"],"sourcesContent":["import { createPrivateKey, createPublicKey, sign } from \"node:crypto\";\n\nimport type { BrowserContext, Page } from \"@playwright/test\";\n\n/** Headless Wallet-Standard Solana mock that signs messages with Node's ed25519 implementation. */\ntype MockStandardWalletOptions = {\n /** Wallet name announced over Wallet Standard. */\n name?: string;\n /** 32-byte ed25519 seed as hex. Defaults to a fixed test seed so the address is stable. */\n seedHex?: string;\n};\n\ntype MockStandardAccount = {\n address: string;\n /** Raw 32-byte ed25519 public key, hex-encoded. */\n publicKeyHex: string;\n};\n\nconst DEFAULT_SEED_HEX = \"0101010101010101010101010101010101010101010101010101010101010101\";\n\nconst PKCS8_ED25519_PREFIX = Buffer.from(\"302e020100300506032b657004220420\", \"hex\");\n\nconst BASE58_ALPHABET = \"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz\";\n\nconst encodeBase58 = (bytes: Uint8Array): string => {\n let leadingZeros = 0;\n while (leadingZeros < bytes.length && bytes[leadingZeros] === 0) {\n leadingZeros += 1;\n }\n const digits: Array<number> = [];\n for (const byte of bytes) {\n let carry = byte;\n for (let index = 0; index < digits.length; index += 1) {\n carry += digits[index] * 256;\n digits[index] = carry % 58;\n carry = Math.floor(carry / 58);\n }\n while (carry > 0) {\n digits.push(carry % 58);\n carry = Math.floor(carry / 58);\n }\n }\n const prefix = \"1\".repeat(leadingZeros);\n const body = digits\n .toReversed()\n .map((digit) => BASE58_ALPHABET[digit])\n .join(\"\");\n return prefix + body;\n};\n\nconst privateKeyFromSeed = (seed: Buffer) =>\n createPrivateKey({\n format: \"der\",\n key: Buffer.concat([PKCS8_ED25519_PREFIX, seed]),\n type: \"pkcs8\",\n });\n\nconst rawPublicKey = (seed: Buffer): Buffer => {\n const spki = createPublicKey(privateKeyFromSeed(seed)).export({ format: \"der\", type: \"spki\" });\n return spki.subarray(-32);\n};\n\nconst signEd25519 = (seed: Buffer, message: Uint8Array): Array<number> => [\n ...sign(null, Buffer.from(message), privateKeyFromSeed(seed)),\n];\n\nconst SVM_CHAINS = [\"solana:mainnet\", \"solana:devnet\"] as const;\n\n/** `message` is required: defaulting it would sign the empty message instead of rejecting. */\ntype BridgeRequest = { message: Array<number>; method: string };\n\nconst createStandardHandler =\n (seed: Buffer) =>\n ({ message, method }: BridgeRequest): Promise<Array<number>> => {\n switch (method) {\n case \"solana:signMessage\": {\n return Promise.resolve(signEd25519(seed, Uint8Array.from(message)));\n }\n default: {\n return Promise.reject(\n new Error(`[walletwright/mock-standard] unsupported method: ${method}`),\n );\n }\n }\n };\n\n/**\n * Install the headless Wallet-Standard mock for Solana connect + sign on a context (every page) or a\n * single page. Call before `goto`, so the wallet is registered when the dapp discovers wallets.\n * Returns the account it announces so a test can verify signatures against the public key.\n */\n/** Per install, for the same reason as the EVM mock: see the note on `installCount` in `mock.ts`. */\nlet installCount = 0;\n\nconst installMockStandardWallet = async (\n target: BrowserContext | Page,\n options: MockStandardWalletOptions = {},\n): Promise<MockStandardAccount> => {\n const { name = \"Walletwright Mock\", seedHex = DEFAULT_SEED_HEX } = options;\n const seed = Buffer.from(seedHex, \"hex\");\n if (seed.length !== 32) {\n throw new Error(\"[walletwright/mock-standard] seedHex must be 32 bytes\");\n }\n const publicKey = rawPublicKey(seed);\n const address = encodeBase58(publicKey);\n const publicKeyHex = publicKey.toString(\"hex\");\n const handle = createStandardHandler(seed);\n\n const bindingName = `__walletwrightMockStandardSign_${installCount++}`;\n await target.exposeFunction(bindingName, (rpc: BridgeRequest) => handle(rpc));\n\n await target.addInitScript(\n ([binding, info]) => {\n const call =\n // oxlint-disable-next-line anti-slop/no-chained-type-assertions -- Playwright's exposeBinding installs `binding` on the page's window after this script's types are fixed\n (window as unknown as Record<string, (rpc: BridgeRequest) => Promise<Array<number>>>)[\n binding\n ];\n const publicKeyBytes = Uint8Array.from(\n (info.publicKeyHex.match(/.{2}/gv) ?? []).map((byte) => Number.parseInt(byte, 16)),\n );\n const account = {\n address: info.address,\n chains: info.chains,\n features: [\"solana:signMessage\"],\n label: info.name,\n publicKey: publicKeyBytes,\n };\n const wallet = {\n accounts: [account],\n chains: info.chains,\n features: {\n \"solana:signMessage\": {\n signMessage: (...inputs: Array<{ message: Uint8Array }>) =>\n Promise.all(\n inputs.map(async ({ message }) => ({\n signature: Uint8Array.from(\n await call({ message: [...message], method: \"solana:signMessage\" }),\n ),\n signedMessage: message,\n })),\n ),\n version: \"1.0.0\",\n },\n \"standard:connect\": {\n connect: () => Promise.resolve({ accounts: [account] }),\n version: \"1.0.0\",\n },\n \"standard:events\": {\n on: () => () => {},\n version: \"1.0.0\",\n },\n },\n icon: info.icon,\n name: info.name,\n version: \"1.0.0\" as const,\n };\n\n const callback = (api: { register: (w: typeof wallet) => void }) => {\n api.register(wallet);\n };\n try {\n window.dispatchEvent(\n new CustomEvent(\"wallet-standard:register-wallet\", { detail: callback }),\n );\n } catch {\n // The app may not be listening yet; the app-ready handler below covers that case.\n }\n window.addEventListener(\"wallet-standard:app-ready\", (event) => {\n callback((event as CustomEvent<{ register: (w: typeof wallet) => void }>).detail);\n });\n },\n [\n bindingName,\n {\n address,\n chains: [...SVM_CHAINS],\n icon: \"data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'/>\",\n name,\n publicKeyHex,\n },\n ] as const,\n );\n\n return { address, publicKeyHex };\n};\n\nexport { createStandardHandler, encodeBase58, installMockStandardWallet, privateKeyFromSeed };\nexport type { MockStandardAccount, MockStandardWalletOptions };\n"],"mappings":";;;AAkBA,MAAM,mBAAmB;AAEzB,MAAM,uBAAuB,OAAO,KAAK,oCAAoC,KAAK;AAElF,MAAM,kBAA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,QAAkD;GAClE,IAAI,SAAS,MAAM;EACrB;EACA,IAAI;GACF,OAAO,cACL,IAAI,YAAY,mCAAmC,EAAE,QAAQ,SAAS,CAAC,CACzE;EACF,QAAQ,CAER;EACA,OAAO,iBAAiB,8BAA8B,UAAU;GAC9D,SAAU,MAAgE,MAAM;EAClF,CAAC;CACH,GACA,CACE,aACA;EACE;EACA,QAAQ,CAAC,GAAG,UAAU;EACtB,MAAM;EACN;EACA;CACF,CACF,CACF;CAEA,OAAO;EAAE;EAAS;CAAa;AACjC"}
package/dist/mock.d.mts CHANGED
@@ -1,12 +1,7 @@
1
1
  import { BrowserContext, Page } from "@playwright/test";
2
2
  import { PrivateKeyAccount } from "viem/accounts";
3
3
  //#region src/mock.d.ts
4
- /**
5
- * A headless injected-wallet fake for dapp tests that don't need a real extension. It announces
6
- * itself over EIP-6963 and answers the EIP-1193 requests a connect/sign flow makes, signing with a
7
- * real key so signatures verify. `@walletwright/core/mock` is a separate entry point that needs
8
- * `viem` (optional peer); the extension-driving core doesn't.
9
- */
4
+ /** Headless EIP-6963 and EIP-1193 mock that signs with a real key. */
10
5
  type MockWalletOptions = {
11
6
  chainId?: number;
12
7
  /** Wallet name announced over EIP-6963. */
package/dist/mock.mjs CHANGED
@@ -3,7 +3,7 @@ import { privateKeyToAccount } from "viem/accounts";
3
3
  //#region src/mock.ts
4
4
  const DEFAULT_KEY = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
5
5
  const toHex = (value) => `0x${value.toString(16)}`;
6
- const isHexMessage = (value) => (/* @__PURE__ */ new RegExp("^0x[\\da-fA-F]*$", "v")).test(value);
6
+ const isHexMessage = (value) => new RegExp("^0x[\\da-fA-F]*$", "v").test(value);
7
7
  /** The EIP-1193 handler a mock provider answers with. Exported for direct, browser-free testing. */
8
8
  const createRpcHandler = (account, chainIdHex) => ({ method, params = [] }) => {
9
9
  switch (method) {
@@ -20,6 +20,8 @@ const createRpcHandler = (account, chainIdHex) => ({ method, params = [] }) => {
20
20
  default: return Promise.reject(/* @__PURE__ */ new Error(`[walletwright/mock] unsupported method: ${method}`));
21
21
  }
22
22
  };
23
+ /** Each install needs its own Playwright binding and EIP-6963 identity. */
24
+ let installCount = 0;
23
25
  /**
24
26
  * Install the mock on a context (every page) or a single page. Call before `goto`, so the provider
25
27
  * exists when the dapp looks for it. Returns the account address it announces.
@@ -29,19 +31,16 @@ const installMockWallet = async (target, options = {}) => {
29
31
  const account = privateKeyToAccount(privateKey);
30
32
  const chainIdHex = toHex(chainId);
31
33
  const handle = createRpcHandler(account, chainIdHex);
32
- const bindingName = "__walletwrightMockRpc";
33
- try {
34
- await target.exposeFunction(bindingName, (rpc) => handle(rpc));
35
- } catch (error) {
36
- if (!(error instanceof Error && error.message.includes("already registered"))) throw error;
37
- }
34
+ const install = installCount++;
35
+ const bindingName = `__walletwrightMockRpc_${install}`;
36
+ await target.exposeFunction(bindingName, (rpc) => handle(rpc));
38
37
  await target.addInitScript(([binding, info]) => {
39
38
  const call = window[binding];
40
39
  const provider = {
41
40
  isMetaMask: true,
42
- request: (rpc) => call(rpc),
43
41
  on: () => provider,
44
- removeListener: () => provider
42
+ removeListener: () => provider,
43
+ request: (rpc) => call(rpc)
45
44
  };
46
45
  window.ethereum = provider;
47
46
  const announce = () => {
@@ -56,7 +55,7 @@ const installMockWallet = async (target, options = {}) => {
56
55
  icon: "data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'/>",
57
56
  name,
58
57
  rdns: "sh.walletwright.mock",
59
- uuid: "00000000-0000-0000-0000-000000000000"
58
+ uuid: `00000000-0000-0000-0000-${String(install).padStart(12, "0")}`
60
59
  }]);
61
60
  return account.address;
62
61
  };
package/dist/mock.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mock.mjs","names":[],"sources":["../src/mock.ts"],"sourcesContent":["import type { BrowserContext, Page } from \"@playwright/test\";\nimport type { PrivateKeyAccount } from \"viem/accounts\";\nimport { privateKeyToAccount } from \"viem/accounts\";\n\n/**\n * A headless injected-wallet fake for dapp tests that don't need a real extension. It announces\n * itself over EIP-6963 and answers the EIP-1193 requests a connect/sign flow makes, signing with a\n * real key so signatures verify. `@walletwright/core/mock` is a separate entry point that needs\n * `viem` (optional peer); the extension-driving core doesn't.\n */\nexport type MockWalletOptions = {\n chainId?: number;\n /** Wallet name announced over EIP-6963. */\n name?: string;\n /** The account's private key. Defaults to anvil/hardhat account #0. */\n privateKey?: `0x${string}`;\n};\n\n// Anvil/Hardhat account #0, address 0xf39F…92266.\nconst DEFAULT_KEY = \"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80\";\n\ntype Rpc = { method: string; params?: ReadonlyArray<unknown> };\n\nconst toHex = (value: number): string => `0x${value.toString(16)}`;\n\nconst isHexMessage = (value: string): value is `0x${string}` => /^0x[\\da-fA-F]*$/v.test(value);\n\n/** The EIP-1193 handler a mock provider answers with. Exported for direct, browser-free testing. */\nconst createRpcHandler =\n (account: PrivateKeyAccount, chainIdHex: string) =>\n ({ method, params = [] }: Rpc): Promise<unknown> => {\n switch (method) {\n case \"eth_requestAccounts\":\n case \"eth_accounts\": {\n return Promise.resolve([account.address]);\n }\n case \"eth_chainId\": {\n return Promise.resolve(chainIdHex);\n }\n case \"personal_sign\": {\n // params: [message, address]. Dapps pass either a 0x-hex message or a plain UTF-8 string.\n const [message] = params;\n if (typeof message !== \"string\") {\n return Promise.reject(\n new Error(\"[walletwright/mock] personal_sign expects a string message\"),\n );\n }\n return account.signMessage({\n message: isHexMessage(message) ? { raw: message } : message,\n });\n }\n case \"wallet_switchEthereumChain\":\n case \"wallet_addEthereumChain\": {\n return Promise.resolve(null);\n }\n default: {\n return Promise.reject(new Error(`[walletwright/mock] unsupported method: ${method}`));\n }\n }\n };\n\n/**\n * Install the mock on a context (every page) or a single page. Call before `goto`, so the provider\n * exists when the dapp looks for it. Returns the account address it announces.\n */\nconst installMockWallet = async (\n target: BrowserContext | Page,\n options: MockWalletOptions = {},\n): Promise<string> => {\n const { chainId = 31_337, name = \"Walletwright Mock\", privateKey = DEFAULT_KEY } = options;\n const account = privateKeyToAccount(privateKey);\n const chainIdHex = toHex(chainId);\n const handle = createRpcHandler(account, chainIdHex);\n\n const bindingName = \"__walletwrightMockRpc\";\n try {\n await target.exposeFunction(bindingName, (rpc: Rpc) => handle(rpc));\n } catch (error) {\n // Playwright rejects a second exposeFunction with the same name; the bridge is already there,\n // so a repeat install (e.g. in a per-test hook) is fine. Any other error is real.\n if (!(error instanceof Error && error.message.includes(\"already registered\"))) {\n throw error;\n }\n }\n\n await target.addInitScript(\n ([binding, info]) => {\n const call = (window as unknown as Record<string, (rpc: Rpc) => Promise<unknown>>)[binding];\n const provider = {\n isMetaMask: true,\n request: (rpc: Rpc) => call(rpc),\n // Minimal event surface so wagmi/web3-onboard don't throw wiring listeners.\n on: () => provider,\n removeListener: () => provider,\n };\n (window as unknown as { ethereum?: unknown }).ethereum = provider;\n\n const announce = () => {\n window.dispatchEvent(\n new CustomEvent(\"eip6963:announceProvider\", {\n detail: Object.freeze({ info, provider }),\n }),\n );\n };\n window.addEventListener(\"eip6963:requestProvider\", announce);\n announce();\n },\n [\n bindingName,\n {\n icon: \"data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'/>\",\n name,\n rdns: \"sh.walletwright.mock\",\n uuid: \"00000000-0000-0000-0000-000000000000\",\n },\n ] as const,\n );\n\n return account.address;\n};\n\nexport { createRpcHandler, installMockWallet };\n"],"mappings":";;;AAmBA,MAAM,cAAc;AAIpB,MAAM,SAAS,UAA0B,KAAK,MAAM,SAAS,EAAE;AAE/D,MAAM,gBAAgB,2BAA0C,kCAAiB,EAAC,CAAC,KAAK,KAAK;;AAG7F,MAAM,oBACH,SAA4B,gBAC5B,EAAE,QAAQ,SAAS,CAAC,QAA+B;CAClD,QAAQ,QAAR;EACE,KAAK;EACL,KAAK,gBACH,OAAO,QAAQ,QAAQ,CAAC,QAAQ,OAAO,CAAC;EAE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AO,QAAQ;AACjB"}
1
+ {"version":3,"file":"mock.mjs","names":[],"sources":["../src/mock.ts"],"sourcesContent":["import type { BrowserContext, Page } from \"@playwright/test\";\nimport type { PrivateKeyAccount } from \"viem/accounts\";\nimport { privateKeyToAccount } from \"viem/accounts\";\n\n/** Headless EIP-6963 and EIP-1193 mock that signs with a real key. */\nexport type MockWalletOptions = {\n chainId?: number;\n /** Wallet name announced over EIP-6963. */\n name?: string;\n /** The account's private key. Defaults to anvil/hardhat account #0. */\n privateKey?: `0x${string}`;\n};\n\nconst DEFAULT_KEY = \"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80\";\n\ntype Rpc = { method: string; params?: ReadonlyArray<unknown> };\n\nconst toHex = (value: number): string => `0x${value.toString(16)}`;\n\nconst isHexMessage = (value: string): value is `0x${string}` => /^0x[\\da-fA-F]*$/v.test(value);\n\n/** The EIP-1193 handler a mock provider answers with. Exported for direct, browser-free testing. */\nconst createRpcHandler =\n (account: PrivateKeyAccount, chainIdHex: string) =>\n ({ method, params = [] }: Rpc): Promise<unknown> => {\n switch (method) {\n case \"eth_requestAccounts\":\n case \"eth_accounts\": {\n return Promise.resolve([account.address]);\n }\n case \"eth_chainId\": {\n return Promise.resolve(chainIdHex);\n }\n case \"personal_sign\": {\n const [message] = params;\n if (typeof message !== \"string\") {\n return Promise.reject(\n new Error(\"[walletwright/mock] personal_sign expects a string message\"),\n );\n }\n return account.signMessage({\n message: isHexMessage(message) ? { raw: message } : message,\n });\n }\n case \"wallet_switchEthereumChain\":\n case \"wallet_addEthereumChain\": {\n return Promise.resolve(null);\n }\n default: {\n return Promise.reject(new Error(`[walletwright/mock] unsupported method: ${method}`));\n }\n }\n };\n\n/** Each install needs its own Playwright binding and EIP-6963 identity. */\nlet installCount = 0;\n\n/**\n * Install the mock on a context (every page) or a single page. Call before `goto`, so the provider\n * exists when the dapp looks for it. Returns the account address it announces.\n */\nconst installMockWallet = async (\n target: BrowserContext | Page,\n options: MockWalletOptions = {},\n): Promise<string> => {\n const { chainId = 31_337, name = \"Walletwright Mock\", privateKey = DEFAULT_KEY } = options;\n const account = privateKeyToAccount(privateKey);\n const chainIdHex = toHex(chainId);\n const handle = createRpcHandler(account, chainIdHex);\n\n const install = installCount++;\n const bindingName = `__walletwrightMockRpc_${install}`;\n await target.exposeFunction(bindingName, (rpc: Rpc) => handle(rpc));\n\n await target.addInitScript(\n ([binding, info]) => {\n // oxlint-disable-next-line anti-slop/no-chained-type-assertions -- Playwright's exposeBinding installs `binding` on the page's window after this script's types are fixed\n const call = (window as unknown as Record<string, (rpc: Rpc) => Promise<unknown>>)[binding];\n const provider = {\n isMetaMask: true,\n on: () => provider,\n removeListener: () => provider,\n request: (rpc: Rpc) => call(rpc),\n };\n // oxlint-disable-next-line anti-slop/no-chained-type-assertions -- injecting the EIP-1193 provider is the point of this script; the DOM lib has no `ethereum` slot\n (window as unknown as { ethereum?: unknown }).ethereum = provider;\n\n const announce = () => {\n window.dispatchEvent(\n new CustomEvent(\"eip6963:announceProvider\", {\n detail: Object.freeze({ info, provider }),\n }),\n );\n };\n window.addEventListener(\"eip6963:requestProvider\", announce);\n announce();\n },\n [\n bindingName,\n {\n icon: \"data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'/>\",\n name,\n rdns: \"sh.walletwright.mock\",\n // Per install, so two providers stay distinguishable in a dapp's uuid-keyed provider map.\n uuid: `00000000-0000-0000-0000-${String(install).padStart(12, \"0\")}`,\n },\n ] as const,\n );\n\n return account.address;\n};\n\nexport { createRpcHandler, installMockWallet };\n"],"mappings":";;;AAaA,MAAM,cAAc;AAIpB,MAAM,SAAS,UAA0B,KAAK,MAAM,SAAS,EAAE;AAE/D,MAAM,gBAAgB,UAA0C,kCAAiB,CAAC,CAAC,KAAK,KAAK;;AAG7F,MAAM,oBACH,SAA4B,gBAC5B,EAAE,QAAQ,SAAS,CAAC,QAA+B;CAClD,QAAQ,QAAR;EACE,KAAK;EACL,KAAK,gBACH,OA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