-
-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathSnapDataSource.ts
More file actions
758 lines (665 loc) · 24.3 KB
/
SnapDataSource.ts
File metadata and controls
758 lines (665 loc) · 24.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
import type { Balance, CaipAssetType } from '@metamask/keyring-api';
import { KeyringClient } from '@metamask/keyring-snap-client';
import type {
Caveat,
GetPermissions,
PermissionConstraint,
PermissionControllerStateChange,
SubjectPermissions,
} from '@metamask/permission-controller';
import type {
SnapControllerGetRunnableSnapsAction,
SnapControllerHandleRequestAction,
} from '@metamask/snaps-controllers';
import type { Snap, SnapId } from '@metamask/snaps-sdk';
import { HandlerType, SnapCaveatType } from '@metamask/snaps-utils';
import { parseCaipAssetType } from '@metamask/utils';
import type { Json, JsonRpcRequest } from '@metamask/utils';
import { AbstractDataSource } from './AbstractDataSource';
import type {
DataSourceState,
SubscriptionRequest,
} from './AbstractDataSource';
import type { AssetsControllerMessenger } from '../AssetsController';
import { projectLogger, createModuleLogger } from '../logger';
import type {
AssetBalance,
ChainId,
Caip19AssetId,
DataRequest,
DataResponse,
Middleware,
} from '../types';
// ============================================================================
// SNAP KEYRING EVENT TYPES
// ============================================================================
/**
* Payload for AccountsController:accountBalancesUpdated event.
* Re-published from SnapKeyring:accountBalancesUpdated.
*/
export type AccountBalancesUpdatedEventPayload = {
balances: {
[accountId: string]: {
[assetId: string]: {
amount: string;
unit: string;
};
};
};
};
/**
* Event from AccountsController when snap balances are updated.
*/
export type AccountsControllerAccountBalancesUpdatedEvent = {
type: 'AccountsController:accountBalancesUpdated';
payload: [AccountBalancesUpdatedEventPayload];
};
const log = createModuleLogger(projectLogger, 'SnapDataSource');
// ============================================================================
// CONSTANTS
// ============================================================================
export const SNAP_DATA_SOURCE_NAME = 'SnapDataSource';
/** The permission name for snap keyring endowment */
export const KEYRING_PERMISSION = 'endowment:keyring';
/** The permission name for snap assets endowment (contains chainIds) */
export const ASSETS_PERMISSION = 'endowment:assets';
// ============================================================================
// PERMISSION UTILITIES
// ============================================================================
/**
* Getter function to get the chainIds caveat from a permission.
*
* This does basic validation of the caveat, but does not validate the type or
* value of the namespaces object itself, as this is handled by the
* `PermissionsController` when the permission is requested.
*
* @param permission - The permission to get the `chainIds` caveat from.
* @returns An array of `chainIds` that the snap supports, or null if none.
*/
export function getChainIdsCaveat(
permission?: PermissionConstraint,
): ChainId[] | null {
if (!permission?.caveats) {
return null;
}
const caveat = permission.caveats.find(
(permCaveat) => permCaveat.type === SnapCaveatType.ChainIds,
) as Caveat<string, string[]> | undefined;
return caveat ? (caveat.value as ChainId[]) : null;
}
/**
* Extracts the CAIP-2 chain ID from a CAIP-19 asset ID.
* e.g., "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501" -> "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp"
* Uses @metamask/utils parseCaipAssetType for CAIP parsing.
*
* @param assetId - The CAIP-19 asset ID to extract chain from.
* @returns The CAIP-2 chain ID portion of the asset ID.
*/
export function extractChainFromAssetId(assetId: string): ChainId {
const parsed = parseCaipAssetType(assetId as CaipAssetType);
return parsed.chainId;
}
// ============================================================================
// STATE
// ============================================================================
/**
* State for the SnapDataSource.
* Uses dynamic snap discovery - chains are populated from PermissionController.
*/
export type SnapDataSourceState = {
/**
* Mapping of chain IDs to snap IDs that support them.
* Used to filter which accounts to process for a given chain request.
*/
chainToSnap: Record<ChainId, string>;
} & DataSourceState;
const defaultSnapState: SnapDataSourceState = {
activeChains: [],
chainToSnap: {},
};
// ============================================================================
// MESSENGER TYPES
// ============================================================================
/**
* Allowed events that SnapDataSource can subscribe to.
*/
export type SnapDataSourceAllowedEvents =
| AccountsControllerAccountBalancesUpdatedEvent
| PermissionControllerStateChange;
export type SnapDataSourceAllowedActions =
| SnapControllerGetRunnableSnapsAction
| SnapControllerHandleRequestAction
| GetPermissions;
// ============================================================================
// OPTIONS
// ============================================================================
export type SnapDataSourceOptions = {
/** The AssetsController messenger (shared by all data sources). */
messenger: AssetsControllerMessenger;
/** Called when this data source's active chains change. Pass dataSourceName so the controller knows the source. */
onActiveChainsUpdated: (
dataSourceName: string,
chains: ChainId[],
previousChains: ChainId[],
) => void;
/** Configured networks to support (defaults to all snap networks) */
configuredNetworks?: ChainId[];
/** Default polling interval in ms for subscriptions */
pollInterval?: number;
/** Initial state */
state?: Partial<SnapDataSourceState>;
};
// ============================================================================
// SNAP DATA SOURCE
// ============================================================================
/**
* Unified Snap data source that routes requests to the appropriate wallet snap
* based on the chain ID prefix.
*
* @example
* ```typescript
* const snapDataSource = new SnapDataSource({
* messenger,
* onActiveChainsUpdated: (chains) => { /* ... *\/ },
* });
*
* // Fetch will automatically route to the correct snap
* await snapDataSource.fetch({
* chainIds: ['solana:mainnet', 'bip122:000000000019d6689c085ae165831e93'],
* accountIds: ['account1'],
* });
* ```
*/
export class SnapDataSource extends AbstractDataSource<
typeof SNAP_DATA_SOURCE_NAME,
SnapDataSourceState
> {
readonly #messenger: AssetsControllerMessenger;
readonly #onActiveChainsUpdated: (
dataSourceName: string,
chains: ChainId[],
previousChains: ChainId[],
) => void;
/** Bound handler for snap keyring balance updates, stored for cleanup */
readonly #handleSnapBalancesUpdatedBound: (
payload: AccountBalancesUpdatedEventPayload,
) => void;
readonly #handlePermissionStateChangeBound: () => void;
/** Cache of KeyringClient instances per snap ID to avoid re-instantiation */
readonly #keyringClientCache: Map<string, KeyringClient> = new Map();
constructor(options: SnapDataSourceOptions) {
super(SNAP_DATA_SOURCE_NAME, {
...defaultSnapState,
...options.state,
});
this.#messenger = options.messenger;
this.#onActiveChainsUpdated = options.onActiveChainsUpdated;
// Bind handlers for cleanup in destroy()
this.#handleSnapBalancesUpdatedBound = this.#handleSnapBalancesUpdated.bind(
this,
) as (payload: AccountBalancesUpdatedEventPayload) => void;
this.#handlePermissionStateChangeBound =
this.#discoverKeyringSnaps.bind(this);
this.#subscribeToEvents();
// Discover keyring-capable snaps and populate activeChains dynamically
this.#discoverKeyringSnaps();
}
/**
* Subscribe to all events needed by SnapDataSource.
* Groups snap keyring events and permission change events.
*/
#subscribeToEvents(): void {
// Subscribe to snap keyring events and permission changes (not in AssetsControllerEvents).
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const messenger = this.#messenger as any;
messenger.subscribe(
'AccountsController:accountBalancesUpdated',
this.#handleSnapBalancesUpdatedBound,
);
messenger.subscribe(
'PermissionController:stateChange',
this.#handlePermissionStateChangeBound,
);
}
/**
* Handle snap balance updates from the keyring.
* Transforms the payload and publishes to AssetsController.
*
* @param payload - The balance update payload from AccountsController.
*/
#handleSnapBalancesUpdated(
payload: AccountBalancesUpdatedEventPayload,
): void {
// Transform the snap keyring payload to DataResponse format
let assetsBalance: NonNullable<DataResponse['assetsBalance']> | undefined;
for (const [accountId, assets] of Object.entries(payload?.balances ?? {})) {
let accountAssets: Record<Caip19AssetId, AssetBalance> | undefined;
for (const [assetId, balance] of Object.entries(assets ?? {})) {
let chainId: ChainId;
try {
chainId = extractChainFromAssetId(assetId);
} catch (error) {
log('Skipping snap balance for malformed asset ID', {
assetId,
error,
});
continue;
}
if (this.#isChainSupportedBySnap(chainId)) {
accountAssets ??= {};
accountAssets[assetId as Caip19AssetId] = {
amount: balance.amount,
};
}
}
if (accountAssets) {
assetsBalance ??= {};
assetsBalance[accountId] = accountAssets;
}
}
// Only report if we have snap-related updates
if (assetsBalance) {
const response: DataResponse = { assetsBalance, updateMode: 'merge' };
for (const subscription of this.activeSubscriptions.values()) {
subscription.onAssetsUpdate(response)?.catch(console.error);
}
}
}
/**
* Check if a chain ID is supported by any discovered snap.
*
* @param chainId - The CAIP-2 chain ID to check.
* @returns True if we have a snap that supports this chain.
*/
#isChainSupportedBySnap(chainId: ChainId): boolean {
return this.state.activeChains.includes(chainId);
}
// ============================================================================
// SNAP DISCOVERY (Dynamic via PermissionController)
// ============================================================================
/**
* Get all runnable snaps from SnapController.
* Runnable snaps are enabled and not blocked.
*
* @returns Array of runnable snaps.
*/
#getRunnableSnaps(): Snap[] {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (this.#messenger as any).call(
'SnapController:getRunnableSnaps',
) as Snap[];
} catch (error) {
log('Failed to get runnable snaps', error);
return [];
}
}
/**
* Get permissions for a snap from PermissionController.
*
* @param snapId - The snap ID to get permissions for.
* @returns The snap's permissions, or undefined if none.
*/
#getSnapPermissions(
snapId: string,
): SubjectPermissions<PermissionConstraint> | undefined {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (this.#messenger as any).call(
'PermissionController:getPermissions',
snapId,
) as SubjectPermissions<PermissionConstraint>;
} catch (error) {
log('Failed to get permissions for snap', { snapId, error });
return undefined;
}
}
/**
* Discover all snaps with keyring capabilities and their supported chains.
* Uses PermissionController to find snaps with endowment:keyring permission.
* Updates chainToSnap mapping and activeChains.
*
* Called on initialization and whenever PermissionController state changes
* (e.g., new snaps installed, permissions granted/revoked).
*
* @remarks
* **Known limitation:** If discovery fails (e.g., SnapController not ready),
* the data source continues with empty chainToSnap. This means no snap
* chains will be supported until a re-discovery is triggered by a permission
* change. Callers should be aware that initialization may complete with no
* active chains.
*/
#discoverKeyringSnaps(): void {
try {
const runnableSnaps = this.#getRunnableSnaps();
const chainToSnap: Record<ChainId, string> = {};
const supportedChains: ChainId[] = [];
for (const snap of runnableSnaps) {
const permissions = this.#getSnapPermissions(snap.id);
// Must have endowment:keyring permission to be a keyring snap
if (!permissions?.[KEYRING_PERMISSION]) {
continue;
}
// Get chainIds caveat from the assets permission (not keyring permission)
// The chainIds are stored in endowment:assets
const assetsPermission = permissions[ASSETS_PERMISSION];
const chainIds = getChainIdsCaveat(assetsPermission);
// Map each chain to this snap (first snap wins if multiple support same chain)
if (chainIds) {
for (const chainId of chainIds) {
if (!(chainId in chainToSnap)) {
chainToSnap[chainId] = snap.id;
supportedChains.push(chainId);
}
}
}
}
// Update chainToSnap mapping
this.state.chainToSnap = chainToSnap;
// Notify if chains changed
try {
const previous = [...this.state.activeChains];
this.updateActiveChains(supportedChains, (updatedChains) => {
this.#onActiveChainsUpdated(this.getName(), updatedChains, previous);
});
} catch {
// AssetsController not ready yet - expected during initialization
}
} catch (error) {
log('Keyring snap discovery failed', { error });
this.state.chainToSnap = {};
try {
const previous = [...this.state.activeChains];
this.updateActiveChains([], (updatedChains) => {
this.#onActiveChainsUpdated(this.getName(), updatedChains, previous);
});
} catch {
// AssetsController not ready yet - expected during initialization
}
}
}
// ============================================================================
// FETCH
// ============================================================================
async fetch(request: DataRequest): Promise<DataResponse> {
// Guard against undefined request
// Note: chainIds filtering is done by middleware/subscribe before calling fetch
if (!request?.chainIds?.length) {
return {};
}
if (!request?.accountsWithSupportedChains?.length) {
return { assetsBalance: {}, assetsInfo: {}, updateMode: 'full' };
}
const results: DataResponse = {
assetsBalance: {},
assetsInfo: {},
updateMode: 'full',
};
// Fetch balances for each account using its snap ID from metadata
for (const { account } of request.accountsWithSupportedChains) {
// Skip accounts without snap metadata (non-snap accounts)
const snapId = account.metadata.snap?.id;
if (!snapId) {
continue;
}
// Skip accounts whose snap doesn't support any of the requested chains
const snapSupportsRequestedChains = request.chainIds.some(
(chainId) => this.state.chainToSnap[chainId] === snapId,
);
if (!snapSupportsRequestedChains) {
continue;
}
const accountId = account.id;
try {
const client = this.#getKeyringClient(snapId);
// Step 1: Get the list of assets for this account
const accountAssets = await client.listAccountAssets(accountId);
// If no assets, skip to next account
if (!accountAssets || accountAssets.length === 0) {
continue;
}
// Step 2: Get balances for those specific assets
const balances: Record<CaipAssetType, Balance> =
await client.getAccountBalances(
accountId,
accountAssets as CaipAssetType[],
);
// Transform keyring response to DataResponse format
if (balances && typeof balances === 'object' && results.assetsBalance) {
for (const [assetId, balance] of Object.entries(balances)) {
results.assetsBalance[accountId] ??= {};
const accountBalances = results.assetsBalance[accountId];
if (accountBalances) {
(accountBalances as Record<string, unknown>)[assetId] = {
amount: balance.amount,
};
}
}
}
} catch {
// Expected when account doesn't belong to this snap
}
}
return results;
}
// ============================================================================
// MIDDLEWARE
// ============================================================================
/**
* Get the middleware for fetching balances via Snaps.
* This middleware:
* - Supports multiple accounts in a single request
* - Filters request to only chains this data source supports
* - Fetches balances for those chains for all accounts
* - Merges response into context
* - Removes handled chains from request for next middleware
*
* @returns The middleware function for the assets pipeline.
*/
get assetsMiddleware(): Middleware {
return async (context, next) => {
const { request } = context;
// Filter to chains this data source supports
const supportedChains = request.chainIds.filter((chainId) =>
this.state.activeChains.includes(chainId),
);
// If no supported chains, skip and pass to next middleware
if (supportedChains.length === 0) {
return next(context);
}
let successfullyHandledChains: ChainId[] = [];
try {
// Fetch for supported chains
const response = await this.fetch({
...request,
chainIds: supportedChains,
});
// Merge response into context
if (response.assetsBalance) {
context.response.assetsBalance ??= {};
for (const [accountId, accountBalances] of Object.entries(
response.assetsBalance,
)) {
context.response.assetsBalance[accountId] = {
...context.response.assetsBalance[accountId],
...accountBalances,
};
}
}
if (response.assetsInfo) {
context.response.assetsInfo = {
...context.response.assetsInfo,
...response.assetsInfo,
};
}
if (response.assetsPrice) {
context.response.assetsPrice = {
...context.response.assetsPrice,
...response.assetsPrice,
};
}
// Determine successfully handled chains (exclude errors)
const failedChains = new Set(Object.keys(response.errors ?? {}));
successfullyHandledChains = supportedChains.filter(
(chainId) => !failedChains.has(chainId),
);
} catch (error) {
log('Middleware fetch failed', { error });
successfullyHandledChains = [];
}
// Prepare context for next middleware
let nextContext = context;
if (successfullyHandledChains.length > 0) {
const remainingChains = request.chainIds.filter(
(chainId) => !successfullyHandledChains.includes(chainId),
);
nextContext = {
...context,
request: {
...request,
chainIds: remainingChains,
},
};
}
// Call next middleware
return next(nextContext);
};
}
// ============================================================================
// SUBSCRIBE - Routes to appropriate snap(s)
// ============================================================================
async subscribe(subscriptionRequest: SubscriptionRequest): Promise<void> {
const { request, subscriptionId, isUpdate } = subscriptionRequest;
// Guard against undefined request or chainIds
if (!request?.chainIds) {
return;
}
// Filter to chains we have a snap for
const supportedChains = request.chainIds.filter((chainId) =>
this.#isChainSupportedBySnap(chainId),
);
if (supportedChains.length === 0) {
return;
}
if (isUpdate) {
const existing = this.activeSubscriptions.get(subscriptionId);
if (existing) {
existing.chains = supportedChains;
// Do a fetch to get latest data on subscription update
this.fetch({
...request,
chainIds: supportedChains,
})
.then(async (fetchResponse) => {
if (Object.keys(fetchResponse.assetsBalance ?? {}).length > 0) {
await existing.onAssetsUpdate(fetchResponse);
}
return fetchResponse;
})
.catch((error) => {
log('Subscription update fetch failed', { subscriptionId, error });
});
return;
}
}
await this.unsubscribe(subscriptionId);
// Snaps provide real-time updates via AccountsController:accountBalancesUpdated
// We only need to track the subscription and do an initial fetch
// No polling needed - updates come through #handleSnapBalancesUpdated
this.activeSubscriptions.set(subscriptionId, {
cleanup: () => {
// No timer to clear - we use event-based updates
},
chains: supportedChains,
onAssetsUpdate: subscriptionRequest.onAssetsUpdate,
});
// Initial fetch to get current balances
try {
const fetchResponse = await this.fetch({
...request,
chainIds: supportedChains,
});
const subscription = this.activeSubscriptions.get(subscriptionId);
if (
Object.keys(fetchResponse.assetsBalance ?? {}).length > 0 &&
subscription
) {
await subscription.onAssetsUpdate(fetchResponse);
}
} catch (error) {
log('Initial fetch failed', { subscriptionId, error });
}
}
// ============================================================================
// KEYRING CLIENT
// ============================================================================
/**
* Gets a `KeyringClient` for a Snap.
* Caches clients per snap ID to avoid re-instantiation across multiple calls.
*
* @param snapId - ID of the Snap to get the client for.
* @returns A `KeyringClient` for the Snap.
*/
#getKeyringClient(snapId: string): KeyringClient {
const cachedClient = this.#keyringClientCache.get(snapId);
if (cachedClient) {
return cachedClient;
}
const client = new KeyringClient({
send: async (request: JsonRpcRequest): Promise<Json> =>
await (
this.#messenger as unknown as {
call: (action: string, ...args: unknown[]) => Promise<Json> | Json;
}
).call('SnapController:handleRequest', {
snapId: snapId as SnapId,
origin: 'metamask',
handler: HandlerType.OnKeyringRequest,
request,
}),
});
this.#keyringClientCache.set(snapId, client);
return client;
}
// ============================================================================
// CLEANUP
// ============================================================================
destroy(): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const messenger = this.#messenger as any;
// Unsubscribe from snap keyring events
try {
messenger.unsubscribe(
'AccountsController:accountBalancesUpdated',
this.#handleSnapBalancesUpdatedBound,
);
} catch (error) {
log('Failed to unsubscribe from snap keyring events', { error });
}
// Unsubscribe from permission changes
try {
messenger.unsubscribe(
'PermissionController:stateChange',
this.#handlePermissionStateChangeBound,
);
} catch (error) {
log('Failed to unsubscribe from permission changes', { error });
}
// Clean up active subscriptions
for (const [subscriptionId] of this.activeSubscriptions) {
this.unsubscribe(subscriptionId).catch(() => {
// Ignore cleanup errors
});
}
// Clear keyring client cache
this.#keyringClientCache.clear();
}
}
// ============================================================================
// FACTORY
// ============================================================================
export function createSnapDataSource(
options: SnapDataSourceOptions,
): SnapDataSource {
return new SnapDataSource(options);
}