|
| 1 | +/** |
| 2 | + * Page-side wallet injection. |
| 3 | + * |
| 4 | + * The babylon-wallet-connector's injectable BTC adapter reads from |
| 5 | + * `window.btcwallet` and treats whatever it finds there as an |
| 6 | + * `IBTCProvider`. To drive the connect flow in e2e we install a |
| 7 | + * deterministic provider on that global *before* the dApp loads, via |
| 8 | + * `page.addInitScript`. |
| 9 | + * |
| 10 | + * The mock provider lives entirely in page context: `addInitScript` |
| 11 | + * serialises its callback to a string and re-evaluates it in the |
| 12 | + * browser, so any function the callback references must be defined |
| 13 | + * inside the callback body. Closures captured Node-side do not |
| 14 | + * survive the boundary. Anything the test needs to vary across runs |
| 15 | + * therefore travels as a plain-JSON `config` arg. |
| 16 | + * |
| 17 | + * ETH provider injection is a separate problem: the dApp uses Reown's |
| 18 | + * AppKit, which has its own React state machine. Mocking that lands |
| 19 | + * with a follow-up ticket (the per-flow deposit specs that need a |
| 20 | + * fully-connected wallet pair will block on it). |
| 21 | + */ |
| 22 | + |
| 23 | +import type { Page } from "@playwright/test"; |
| 24 | + |
| 25 | +import type { SeededBtcWallet } from "./seededWallets"; |
| 26 | + |
| 27 | +export interface BtcWalletPageConfig { |
| 28 | + /** Bech32 address `getAddress` returns. */ |
| 29 | + address: string; |
| 30 | + /** 33-byte compressed pubkey hex `getPublicKeyHex` returns. */ |
| 31 | + publicKeyHex: string; |
| 32 | + /** Network string ("mainnet" | "signet" | "testnet"). */ |
| 33 | + network: string; |
| 34 | + /** Display name for the wallet menu. */ |
| 35 | + providerName: string; |
| 36 | + /** Data-URI icon. */ |
| 37 | + providerIcon: string; |
| 38 | + /** Marker so the page-side instance is recognisable from devtools. */ |
| 39 | + e2e: true; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Install a deterministic BTC provider on `window.btcwallet`. Must be |
| 44 | + * called BEFORE the first `page.goto` so the injectable adapter |
| 45 | + * discovers it during module evaluation. |
| 46 | + */ |
| 47 | +export async function injectBtcWalletProvider( |
| 48 | + page: Page, |
| 49 | + config: BtcWalletPageConfig, |
| 50 | +): Promise<void> { |
| 51 | + await page.addInitScript((cfg) => { |
| 52 | + const SIGNED_MESSAGE_HEX = "cd".repeat(64); |
| 53 | + |
| 54 | + // sha256 of `${appName}:${context}` rendered as lowercase hex. The |
| 55 | + // deriveContextHash output must be deterministic across runs but |
| 56 | + // not collide with real key material - the mock prefixes the |
| 57 | + // appName with a marker via the chosen input. |
| 58 | + async function sha256Hex(input: string): Promise<string> { |
| 59 | + const data = new TextEncoder().encode(input); |
| 60 | + const buffer = await crypto.subtle.digest("SHA-256", data); |
| 61 | + return Array.from(new Uint8Array(buffer)) |
| 62 | + .map((b) => b.toString(16).padStart(2, "0")) |
| 63 | + .join(""); |
| 64 | + } |
| 65 | + |
| 66 | + const listeners = new Map<string, Set<(...args: unknown[]) => void>>(); |
| 67 | + |
| 68 | + const provider = { |
| 69 | + connectWallet: async () => undefined, |
| 70 | + getAddress: async () => cfg.address, |
| 71 | + getPublicKeyHex: async () => cfg.publicKeyHex, |
| 72 | + getNetwork: async () => cfg.network, |
| 73 | + getInscriptions: async () => [], |
| 74 | + getWalletProviderName: async () => cfg.providerName, |
| 75 | + getWalletProviderIcon: async () => cfg.providerIcon, |
| 76 | + // Echo the PSBT back so callers that just decode-and-re-encode keep |
| 77 | + // working. Tests that need a real partial signature must reach |
| 78 | + // into the provider via `window.btcwallet` and overwrite the |
| 79 | + // method per-test. |
| 80 | + signPsbt: async (psbtHex: string) => psbtHex, |
| 81 | + signPsbts: async (psbtsHexes: string[]) => [...psbtsHexes], |
| 82 | + signMessage: async () => SIGNED_MESSAGE_HEX, |
| 83 | + deriveContextHash: async (appName: string, context: string) => |
| 84 | + sha256Hex(`${appName}:${context}`), |
| 85 | + on: (event: string, cb: (...args: unknown[]) => void) => { |
| 86 | + if (!listeners.has(event)) listeners.set(event, new Set()); |
| 87 | + listeners.get(event)!.add(cb); |
| 88 | + }, |
| 89 | + off: (event: string, cb: (...args: unknown[]) => void) => { |
| 90 | + listeners.get(event)?.delete(cb); |
| 91 | + }, |
| 92 | + // Tests dispatch events by reading `window.btcwallet.__emit`. |
| 93 | + __emit: (event: string, ...args: unknown[]) => { |
| 94 | + listeners.get(event)?.forEach((cb) => cb(...args)); |
| 95 | + }, |
| 96 | + __e2eConfig: cfg, |
| 97 | + }; |
| 98 | + |
| 99 | + (window as unknown as { btcwallet?: unknown }).btcwallet = provider; |
| 100 | + }, config); |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Convenience adapter for the seeded wallets in `seededWallets.ts`. |
| 105 | + * Builds the page-side config from a SeededBtcWallet so tests can |
| 106 | + * pass the same value to mempool route helpers and the injector. |
| 107 | + */ |
| 108 | +export function btcWalletConfigFromSeeded( |
| 109 | + wallet: SeededBtcWallet, |
| 110 | + overrides: Partial<BtcWalletPageConfig> = {}, |
| 111 | +): BtcWalletPageConfig { |
| 112 | + return { |
| 113 | + address: wallet.address, |
| 114 | + publicKeyHex: `02${"ab".repeat(32)}`, |
| 115 | + network: "signet", |
| 116 | + providerName: "E2E Mock BTC", |
| 117 | + providerIcon: "data:image/svg+xml;base64,PHN2Zy8+", |
| 118 | + e2e: true, |
| 119 | + ...overrides, |
| 120 | + }; |
| 121 | +} |
0 commit comments