@farmslot/adapter-web 0.19.31 → 0.19.32

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/CHANGELOG.md CHANGED
@@ -6,6 +6,10 @@ All notable changes to `@farmslot/adapter-web` are tracked here.
6
6
 
7
7
  - Active-development baseline; add user-facing changes here before release or package publication.
8
8
 
9
+ ## 0.19.32 - 2026-11-24
10
+
11
+ - The injected wallet presented as MetaMask also answers the extension's window streams (`metamask-provider` and the `metamask-multichain-provider` session API), so apps that log in through `@metamask/connect-multichain`, such as the Web Terminal's Web3Auth login, finish starting and can connect.
12
+
9
13
  ## 0.19.31 - 2026-11-23
10
14
 
11
15
  - Pin the Recipe Runner 0.52.2 cohort.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farmslot/adapter-web",
3
- "version": "0.19.31",
3
+ "version": "0.19.32",
4
4
  "description": "Web platform adapter for Farmslot recipes: resolve and launch an isolated Chromium, own its CDP port, load unpacked extensions, and keep macOS focus.",
5
5
  "type": "commonjs",
6
6
  "exports": {
@@ -44,6 +44,16 @@ const ERROR_CATEGORIES = Object.freeze({
44
44
  [-32603]: 'internal',
45
45
  });
46
46
 
47
+ // The MetaMask extension's window-message streams, which an injected wallet
48
+ // presented as MetaMask also answers.
49
+ const EXTENSION_STREAMS = Object.freeze({
50
+ contentscript: 'metamask-contentscript',
51
+ inpage: 'metamask-inpage',
52
+ provider: 'metamask-provider',
53
+ multichain: 'metamask-multichain-provider',
54
+ sessionKey: '__farmslotMultichainSession',
55
+ });
56
+
47
57
  function pageMain(config, refusalReason) {
48
58
  if (window.top !== window || location.origin !== config.appOrigin) return;
49
59
  if (window[config.marker]) return;
@@ -249,9 +259,127 @@ function pageMain(config, refusalReason) {
249
259
  );
250
260
  window.addEventListener('eip6963:requestProvider', announce);
251
261
  announce();
262
+ if (config.injectedWallet.isMetaMask) serveExtensionStreams(provider);
252
263
  return;
253
264
  }
254
265
 
266
+ // A wallet presented as MetaMask is taken for the extension, so apps also
267
+ // reach it over the extension's window streams (as @metamask/connect-multichain
268
+ // does): `metamask-provider` (EIP-1193 JSON-RPC) and `metamask-multichain-provider`
269
+ // (CAIP-25 sessions). Both are answered from the same provider, so every
270
+ // request still goes to the host and is logged. The session's scopes persist
271
+ // per profile, as the extension's permissions do.
272
+ function serveExtensionStreams(provider) {
273
+ const streams = config.extensionStreams;
274
+ const reply = (name, data) =>
275
+ window.postMessage({ target: streams.inpage, data: { name, data } }, location.origin);
276
+ const chainOf = (scope) => Number.parseInt(String(scope).split(':')[1], 10);
277
+ let saved = [];
278
+ const readScopes = () => {
279
+ try {
280
+ const stored = JSON.parse(window.localStorage.getItem(streams.sessionKey) ?? '[]');
281
+ return Array.isArray(stored) ? stored : [];
282
+ } catch {
283
+ // Corrupt or blocked storage reads as no session.
284
+ return [];
285
+ }
286
+ };
287
+ const writeScopes = (scopes) => {
288
+ try {
289
+ if (scopes.length) window.localStorage.setItem(streams.sessionKey, JSON.stringify(scopes));
290
+ else window.localStorage.removeItem(streams.sessionKey);
291
+ } catch {
292
+ // No storage (opaque origin): the session lasts for this document.
293
+ }
294
+ saved = scopes;
295
+ };
296
+ saved = readScopes();
297
+ const session = async () => {
298
+ if (!saved.length) return { sessionScopes: {} };
299
+ const accounts = await provider.request({ method: 'eth_accounts' });
300
+ const sessionScopes = {};
301
+ for (const scope of saved) {
302
+ sessionScopes[scope] = {
303
+ accounts: accounts.map((address) => `${scope}:${address}`),
304
+ // The wallet methods the host signs and logs.
305
+ methods: config.loggedMethods,
306
+ notifications: [],
307
+ };
308
+ }
309
+ return { sessionScopes };
310
+ };
311
+ const changed = async () =>
312
+ reply(streams.multichain, { method: 'wallet_sessionChanged', params: await session() });
313
+ const multichain = {
314
+ wallet_getSession: session,
315
+ async wallet_createSession(params) {
316
+ const requested = Object.keys({ ...params?.requiredScopes, ...params?.optionalScopes });
317
+ const scopes = requested.filter((scope) => /^eip155:\d+$/u.test(scope));
318
+ if (!scopes.length) {
319
+ scopes.push(
320
+ `eip155:${Number.parseInt(await provider.request({ method: 'eth_chainId' }), 16)}`,
321
+ );
322
+ }
323
+ await provider.request({ method: 'eth_requestAccounts' });
324
+ writeScopes(scopes);
325
+ const result = await session();
326
+ await changed();
327
+ return result;
328
+ },
329
+ async wallet_revokeSession() {
330
+ writeScopes([]);
331
+ await changed();
332
+ return true;
333
+ },
334
+ async wallet_invokeMethod(params) {
335
+ const chain = chainOf(params?.scope);
336
+ if (!saved.includes(params?.scope)) {
337
+ throw Object.assign(new Error(`${params?.scope} is not in the session.`), { code: 4100 });
338
+ }
339
+ if (Number.parseInt(await provider.request({ method: 'eth_chainId' }), 16) !== chain) {
340
+ await provider.request({
341
+ method: 'wallet_switchEthereumChain',
342
+ params: [{ chainId: `0x${chain.toString(16)}` }],
343
+ });
344
+ }
345
+ return provider.request(params.request);
346
+ },
347
+ };
348
+ const answer = async (name, message, handle) => {
349
+ try {
350
+ reply(name, { jsonrpc: '2.0', id: message.id, result: await handle() });
351
+ } catch (error) {
352
+ reply(name, {
353
+ jsonrpc: '2.0',
354
+ id: message.id,
355
+ error: {
356
+ code: Number.isInteger(error?.code) ? error.code : -32603,
357
+ message: String(error?.message ?? error),
358
+ },
359
+ });
360
+ }
361
+ };
362
+ window.addEventListener('message', (event) => {
363
+ if (event.source !== window || event.origin !== location.origin) return;
364
+ if (event.data?.target !== streams.contentscript) return;
365
+ const name = event.data.data?.name;
366
+ const message = event.data.data?.data;
367
+ if (!message || message.id === undefined || typeof message.method !== 'string') return;
368
+ if (name === streams.provider) {
369
+ answer(name, message, () =>
370
+ provider.request({ method: message.method, params: message.params }),
371
+ );
372
+ } else if (name === streams.multichain) {
373
+ const handle = multichain[message.method];
374
+ answer(name, message, () => {
375
+ if (!handle)
376
+ throw Object.assign(new Error(`${message.method} is not supported`), { code: 4200 });
377
+ return handle(message.params);
378
+ });
379
+ }
380
+ });
381
+ }
382
+
255
383
  // Extension mode: wrap whatever provider the wallet extension injects,
256
384
  // however the app reaches it (window.ethereum or an EIP-6963 announcement).
257
385
  let current = window.ethereum ? wrap(window.ethereum, 'window.ethereum') : undefined;
@@ -359,6 +487,7 @@ function pageScriptSource({ signer, appOrigin, refuseTypedData = null, injectedW
359
487
  ? { info: injectedWallet.info, isMetaMask: injectedWallet.isMetaMask === true }
360
488
  : null,
361
489
  errorCategories: ERROR_CATEGORIES,
490
+ extensionStreams: EXTENSION_STREAMS,
362
491
  loggedMethods: LOGGED_METHODS,
363
492
  marker: PAGE_MARKER,
364
493
  logBinding: LOG_BINDING,