@metamask-previews/assets-controller 0.2.0-preview-c3cd77f → 0.2.0-preview-d01b2f93d
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 +4 -0
- package/dist/AssetsController-method-action-types.cjs +7 -0
- package/dist/AssetsController-method-action-types.cjs.map +1 -0
- package/dist/AssetsController-method-action-types.d.cts +78 -0
- package/dist/AssetsController-method-action-types.d.cts.map +1 -0
- package/dist/AssetsController-method-action-types.d.mts +78 -0
- package/dist/AssetsController-method-action-types.d.mts.map +1 -0
- package/dist/AssetsController-method-action-types.mjs +6 -0
- package/dist/AssetsController-method-action-types.mjs.map +1 -0
- package/dist/AssetsController.cjs +161 -116
- package/dist/AssetsController.cjs.map +1 -1
- package/dist/AssetsController.d.cts +23 -80
- package/dist/AssetsController.d.cts.map +1 -1
- package/dist/AssetsController.d.mts +23 -80
- package/dist/AssetsController.d.mts.map +1 -1
- package/dist/AssetsController.mjs +161 -116
- package/dist/AssetsController.mjs.map +1 -1
- package/dist/data-sources/AbstractDataSource.cjs.map +1 -1
- package/dist/data-sources/AbstractDataSource.d.cts +10 -1
- package/dist/data-sources/AbstractDataSource.d.cts.map +1 -1
- package/dist/data-sources/AbstractDataSource.d.mts +10 -1
- package/dist/data-sources/AbstractDataSource.d.mts.map +1 -1
- package/dist/data-sources/AbstractDataSource.mjs.map +1 -1
- package/dist/data-sources/AccountsApiDataSource.cjs +23 -99
- package/dist/data-sources/AccountsApiDataSource.cjs.map +1 -1
- package/dist/data-sources/AccountsApiDataSource.d.cts +5 -67
- package/dist/data-sources/AccountsApiDataSource.d.cts.map +1 -1
- package/dist/data-sources/AccountsApiDataSource.d.mts +5 -67
- package/dist/data-sources/AccountsApiDataSource.d.mts.map +1 -1
- package/dist/data-sources/AccountsApiDataSource.mjs +22 -97
- package/dist/data-sources/AccountsApiDataSource.mjs.map +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.cjs +135 -45
- package/dist/data-sources/BackendWebsocketDataSource.cjs.map +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.d.cts +19 -66
- package/dist/data-sources/BackendWebsocketDataSource.d.cts.map +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.d.mts +19 -66
- package/dist/data-sources/BackendWebsocketDataSource.d.mts.map +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.mjs +135 -45
- package/dist/data-sources/BackendWebsocketDataSource.mjs.map +1 -1
- package/dist/data-sources/PriceDataSource.cjs +22 -44
- package/dist/data-sources/PriceDataSource.cjs.map +1 -1
- package/dist/data-sources/PriceDataSource.d.cts +6 -89
- package/dist/data-sources/PriceDataSource.d.cts.map +1 -1
- package/dist/data-sources/PriceDataSource.d.mts +6 -89
- package/dist/data-sources/PriceDataSource.d.mts.map +1 -1
- package/dist/data-sources/PriceDataSource.mjs +22 -44
- package/dist/data-sources/PriceDataSource.mjs.map +1 -1
- package/dist/data-sources/RpcDataSource.cjs +57 -98
- package/dist/data-sources/RpcDataSource.cjs.map +1 -1
- package/dist/data-sources/RpcDataSource.d.cts +16 -55
- package/dist/data-sources/RpcDataSource.d.cts.map +1 -1
- package/dist/data-sources/RpcDataSource.d.mts +16 -55
- package/dist/data-sources/RpcDataSource.d.mts.map +1 -1
- package/dist/data-sources/RpcDataSource.mjs +57 -98
- package/dist/data-sources/RpcDataSource.mjs.map +1 -1
- package/dist/data-sources/SnapDataSource.cjs +30 -30
- package/dist/data-sources/SnapDataSource.cjs.map +1 -1
- package/dist/data-sources/SnapDataSource.d.cts +7 -44
- package/dist/data-sources/SnapDataSource.d.cts.map +1 -1
- package/dist/data-sources/SnapDataSource.d.mts +7 -44
- package/dist/data-sources/SnapDataSource.d.mts.map +1 -1
- package/dist/data-sources/SnapDataSource.mjs +30 -30
- package/dist/data-sources/SnapDataSource.mjs.map +1 -1
- package/dist/data-sources/TokenDataSource.cjs +3 -16
- package/dist/data-sources/TokenDataSource.cjs.map +1 -1
- package/dist/data-sources/TokenDataSource.d.cts +2 -25
- package/dist/data-sources/TokenDataSource.d.cts.map +1 -1
- package/dist/data-sources/TokenDataSource.d.mts +2 -25
- package/dist/data-sources/TokenDataSource.d.mts.map +1 -1
- package/dist/data-sources/TokenDataSource.mjs +3 -16
- package/dist/data-sources/TokenDataSource.mjs.map +1 -1
- package/dist/data-sources/index.cjs +1 -6
- package/dist/data-sources/index.cjs.map +1 -1
- package/dist/data-sources/index.d.cts +6 -7
- package/dist/data-sources/index.d.cts.map +1 -1
- package/dist/data-sources/index.d.mts +6 -7
- package/dist/data-sources/index.d.mts.map +1 -1
- package/dist/data-sources/index.mjs +1 -3
- package/dist/data-sources/index.mjs.map +1 -1
- package/dist/index.cjs +1 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -11
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +9 -11
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -3
- package/dist/index.mjs.map +1 -1
- package/dist/middlewares/DetectionMiddleware.cjs +4 -27
- package/dist/middlewares/DetectionMiddleware.cjs.map +1 -1
- package/dist/middlewares/DetectionMiddleware.d.cts +3 -26
- package/dist/middlewares/DetectionMiddleware.d.cts.map +1 -1
- package/dist/middlewares/DetectionMiddleware.d.mts +3 -26
- package/dist/middlewares/DetectionMiddleware.d.mts.map +1 -1
- package/dist/middlewares/DetectionMiddleware.mjs +4 -27
- package/dist/middlewares/DetectionMiddleware.mjs.map +1 -1
- package/dist/middlewares/index.cjs.map +1 -1
- package/dist/middlewares/index.d.cts +1 -1
- package/dist/middlewares/index.d.cts.map +1 -1
- package/dist/middlewares/index.d.mts +1 -1
- package/dist/middlewares/index.d.mts.map +1 -1
- package/dist/middlewares/index.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +52 -3
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +52 -3
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/data-sources/initDataSources.cjs +0 -215
- package/dist/data-sources/initDataSources.cjs.map +0 -1
- package/dist/data-sources/initDataSources.d.cts +0 -140
- package/dist/data-sources/initDataSources.d.cts.map +0 -1
- package/dist/data-sources/initDataSources.d.mts +0 -140
- package/dist/data-sources/initDataSources.d.mts.map +0 -1
- package/dist/data-sources/initDataSources.mjs +0 -210
- package/dist/data-sources/initDataSources.mjs.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapDataSource.cjs","sourceRoot":"","sources":["../../src/data-sources/SnapDataSource.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,uEAA8D;AAc9D,uDAAoE;AAGpE,iEAA0D;AAK1D,0CAA8D;AAqC9D,MAAM,GAAG,GAAG,IAAA,2BAAkB,EAAC,sBAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE,+EAA+E;AAC/E,YAAY;AACZ,+EAA+E;AAElE,QAAA,qBAAqB,GAAG,gBAAgB,CAAC;AAEtD,qDAAqD;AACxC,QAAA,kBAAkB,GAAG,mBAAmB,CAAC;AAEtD,wEAAwE;AAC3D,QAAA,iBAAiB,GAAG,kBAAkB,CAAC;AAEpD,+EAA+E;AAC/E,uBAAuB;AACvB,+EAA+E;AAE/E;;;;;;;;;GASG;AACH,SAAgB,iBAAiB,CAC/B,UAAiC;IAEjC,IAAI,CAAC,UAAU,EAAE,OAAO,EAAE,CAAC;QACzB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,MAAM,GAAG,UAAU,CAAC,OAAO,CAAC,IAAI,CACpC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,KAAK,4BAAc,CAAC,QAAQ,CACpB,CAAC;IAE1C,OAAO,MAAM,CAAC,CAAC,CAAE,MAAM,CAAC,KAAmB,CAAC,CAAC,CAAC,IAAI,CAAC;AACrD,CAAC;AAZD,8CAYC;AAED;;;;;;GAMG;AACH,SAAgB,uBAAuB,CAAC,OAAe;IACrD,MAAM,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d,CAAC;YACJ,CAAC,CAAC,CAAC;QACL,CAAC;QAAC,MAAM,CAAC;YACP,kEAAkE;QACpE,CAAC;IACH,CAAC;AACH,CAAC,+EAgQiB,MAAc;IAC9B,MAAM,YAAY,GAAG,uBAAA,IAAI,0CAAoB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC1D,IAAI,YAAY,EAAE,CAAC;QACjB,OAAO,YAAY,CAAC;IACtB,CAAC;IAED,MAAM,MAAM,GAAG,IAAI,mCAAa,CAAC;QAC/B,IAAI,EAAE,KAAK,EAAE,OAAuB,EAAiB,EAAE,CACrD,CAAC,MAAM,uBAAA,IAAI,iCAAW,CAAC,IAAI,CAAC,8BAA8B,EAAE;YAC1D,MAAM,EAAE,MAAgB;YACxB,MAAM,EAAE,UAAU;YAClB,OAAO,EAAE,yBAAW,CAAC,gBAAgB;YACrC,OAAO;SACR,CAAC,CAAS;KACd,CAAC,CAAC;IAEH,uBAAA,IAAI,0CAAoB,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC7C,OAAO,MAAM,CAAC;AAChB,CAAC;AA0CH,+EAA+E;AAC/E,UAAU;AACV,+EAA+E;AAE/E,SAAgB,oBAAoB,CAClC,OAA8B;IAE9B,OAAO,IAAI,cAAc,CAAC,OAAO,CAAC,CAAC;AACrC,CAAC;AAJD,oDAIC","sourcesContent":["import type { Balance, CaipAssetType } from '@metamask/keyring-api';\nimport { KeyringClient } from '@metamask/keyring-snap-client';\nimport type { Messenger } from '@metamask/messenger';\nimport type {\n Caveat,\n GetPermissions,\n PermissionConstraint,\n PermissionControllerStateChange,\n SubjectPermissions,\n} from '@metamask/permission-controller';\nimport type {\n GetRunnableSnaps,\n HandleSnapRequest,\n} from '@metamask/snaps-controllers';\nimport type { Snap, SnapId } from '@metamask/snaps-sdk';\nimport { HandlerType, SnapCaveatType } from '@metamask/snaps-utils';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { AbstractDataSource } from './AbstractDataSource';\nimport type {\n DataSourceState,\n SubscriptionRequest,\n} from './AbstractDataSource';\nimport { projectLogger, createModuleLogger } from '../logger';\nimport type {\n AssetBalance,\n ChainId,\n Caip19AssetId,\n DataRequest,\n DataResponse,\n Middleware,\n} from '../types';\n\n// ============================================================================\n// SNAP KEYRING EVENT TYPES\n// ============================================================================\n\n/**\n * Payload for AccountsController:accountBalancesUpdated event.\n * Re-published from SnapKeyring:accountBalancesUpdated.\n */\nexport type AccountBalancesUpdatedEventPayload = {\n balances: {\n [accountId: string]: {\n [assetId: string]: {\n amount: string;\n unit: string;\n };\n };\n };\n};\n\n/**\n * Event from AccountsController when snap balances are updated.\n */\nexport type AccountsControllerAccountBalancesUpdatedEvent = {\n type: 'AccountsController:accountBalancesUpdated';\n payload: [AccountBalancesUpdatedEventPayload];\n};\n\nconst log = createModuleLogger(projectLogger, 'SnapDataSource');\n\n// ============================================================================\n// CONSTANTS\n// ============================================================================\n\nexport const SNAP_DATA_SOURCE_NAME = 'SnapDataSource';\n\n/** The permission name for snap keyring endowment */\nexport const KEYRING_PERMISSION = 'endowment:keyring';\n\n/** The permission name for snap assets endowment (contains chainIds) */\nexport const ASSETS_PERMISSION = 'endowment:assets';\n\n// ============================================================================\n// PERMISSION UTILITIES\n// ============================================================================\n\n/**\n * Getter function to get the chainIds caveat from a permission.\n *\n * This does basic validation of the caveat, but does not validate the type or\n * value of the namespaces object itself, as this is handled by the\n * `PermissionsController` when the permission is requested.\n *\n * @param permission - The permission to get the `chainIds` caveat from.\n * @returns An array of `chainIds` that the snap supports, or null if none.\n */\nexport function getChainIdsCaveat(\n permission?: PermissionConstraint,\n): ChainId[] | null {\n if (!permission?.caveats) {\n return null;\n }\n\n const caveat = permission.caveats.find(\n (permCaveat) => permCaveat.type === SnapCaveatType.ChainIds,\n ) as Caveat<string, string[]> | undefined;\n\n return caveat ? (caveat.value as ChainId[]) : null;\n}\n\n/**\n * Extract chain ID from a CAIP-19 asset ID.\n * e.g., \"solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501\" -> \"solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp\"\n *\n * @param assetId - The CAIP-19 asset ID to extract chain from.\n * @returns The CAIP-2 chain ID portion of the asset ID.\n */\nexport function extractChainFromAssetId(assetId: string): ChainId {\n const parts = assetId.split('/');\n return parts[0] as ChainId;\n}\n\n// ============================================================================\n// STATE\n// ============================================================================\n\n/**\n * State for the SnapDataSource.\n * Uses dynamic snap discovery - chains are populated from PermissionController.\n */\nexport type SnapDataSourceState = {\n /**\n * Mapping of chain IDs to snap IDs that support them.\n * Used to filter which accounts to process for a given chain request.\n */\n chainToSnap: Record<ChainId, string>;\n} & DataSourceState;\n\nconst defaultSnapState: SnapDataSourceState = {\n activeChains: [],\n chainToSnap: {},\n};\n\n// ============================================================================\n// MESSENGER TYPES\n// ============================================================================\n\nexport type SnapDataSourceGetAssetsMiddlewareAction = {\n type: 'SnapDataSource:getAssetsMiddleware';\n handler: () => Middleware;\n};\n\nexport type SnapDataSourceGetActiveChainsAction = {\n type: 'SnapDataSource:getActiveChains';\n handler: () => Promise<ChainId[]>;\n};\n\nexport type SnapDataSourceFetchAction = {\n type: 'SnapDataSource:fetch';\n handler: (request: DataRequest) => Promise<DataResponse>;\n};\n\nexport type SnapDataSourceSubscribeAction = {\n type: 'SnapDataSource:subscribe';\n handler: (request: SubscriptionRequest) => Promise<void>;\n};\n\nexport type SnapDataSourceUnsubscribeAction = {\n type: 'SnapDataSource:unsubscribe';\n handler: (subscriptionId: string) => Promise<void>;\n};\n\nexport type SnapDataSourceActions =\n | SnapDataSourceGetAssetsMiddlewareAction\n | SnapDataSourceGetActiveChainsAction\n | SnapDataSourceFetchAction\n | SnapDataSourceSubscribeAction\n | SnapDataSourceUnsubscribeAction;\n\nexport type SnapDataSourceActiveChainsChangedEvent = {\n type: 'SnapDataSource:activeChainsUpdated';\n payload: [ChainId[]];\n};\n\nexport type SnapDataSourceAssetsUpdatedEvent = {\n type: 'SnapDataSource:assetsUpdated';\n payload: [DataResponse, string | undefined];\n};\n\nexport type SnapDataSourceEvents =\n | SnapDataSourceActiveChainsChangedEvent\n | SnapDataSourceAssetsUpdatedEvent;\n\n/**\n * Allowed events that SnapDataSource can subscribe to.\n */\nexport type SnapDataSourceAllowedEvents =\n | AccountsControllerAccountBalancesUpdatedEvent\n | PermissionControllerStateChange;\n\n// Actions to report to AssetsController\ntype AssetsControllerActiveChainsUpdateAction = {\n type: 'AssetsController:activeChainsUpdate';\n handler: (dataSourceId: string, activeChains: ChainId[]) => void;\n};\n\ntype AssetsControllerAssetsUpdateAction = {\n type: 'AssetsController:assetsUpdate';\n handler: (response: DataResponse, sourceId: string) => Promise<void>;\n};\n\nexport type SnapDataSourceAllowedActions =\n | AssetsControllerActiveChainsUpdateAction\n | AssetsControllerAssetsUpdateAction\n | GetRunnableSnaps\n | HandleSnapRequest\n | GetPermissions;\n\nexport type SnapDataSourceMessenger = Messenger<\n typeof SNAP_DATA_SOURCE_NAME,\n SnapDataSourceActions | SnapDataSourceAllowedActions,\n SnapDataSourceEvents | SnapDataSourceAllowedEvents\n>;\n\n// ============================================================================\n// OPTIONS\n// ============================================================================\n\nexport type SnapDataSourceOptions = {\n /** Messenger for this data source */\n messenger: SnapDataSourceMessenger;\n /** Configured networks to support (defaults to all snap networks) */\n configuredNetworks?: ChainId[];\n /** Default polling interval in ms for subscriptions */\n pollInterval?: number;\n /** Initial state */\n state?: Partial<SnapDataSourceState>;\n};\n\n// ============================================================================\n// SNAP DATA SOURCE\n// ============================================================================\n\n/**\n * Unified Snap data source that routes requests to the appropriate wallet snap\n * based on the chain ID prefix.\n *\n * @example\n * ```typescript\n * const snapDataSource = new SnapDataSource({\n * messenger,\n * });\n *\n * // Fetch will automatically route to the correct snap\n * await snapDataSource.fetch({\n * chainIds: ['solana:mainnet', 'bip122:000000000019d6689c085ae165831e93'],\n * accountIds: ['account1'],\n * });\n * ```\n */\nexport class SnapDataSource extends AbstractDataSource<\n typeof SNAP_DATA_SOURCE_NAME,\n SnapDataSourceState\n> {\n readonly #messenger: SnapDataSourceMessenger;\n\n /** Bound handler for snap keyring balance updates, stored for cleanup */\n readonly #handleSnapBalancesUpdatedBound: (\n payload: AccountBalancesUpdatedEventPayload,\n ) => void;\n\n readonly #handlePermissionStateChangeBound: () => void;\n\n /** Cache of KeyringClient instances per snap ID to avoid re-instantiation */\n readonly #keyringClientCache: Map<string, KeyringClient> = new Map();\n\n constructor(options: SnapDataSourceOptions) {\n super(SNAP_DATA_SOURCE_NAME, {\n ...defaultSnapState,\n ...options.state,\n });\n\n this.#messenger = options.messenger;\n\n // Bind handlers for cleanup in destroy()\n this.#handleSnapBalancesUpdatedBound = this.#handleSnapBalancesUpdated.bind(\n this,\n ) as (payload: AccountBalancesUpdatedEventPayload) => void;\n this.#handlePermissionStateChangeBound =\n this.#discoverKeyringSnaps.bind(this);\n\n this.#registerActionHandlers();\n this.#subscribeToEvents();\n\n // Discover keyring-capable snaps and populate activeChains dynamically\n this.#discoverKeyringSnaps();\n }\n\n /**\n * Subscribe to all events needed by SnapDataSource.\n * Groups snap keyring events and permission change events.\n */\n #subscribeToEvents(): void {\n // Subscribe to snap keyring events for real-time balance updates.\n // The snaps emit AccountBalancesUpdated events when balances change,\n // which are re-published by AccountsController.\n this.#messenger.subscribe(\n 'AccountsController:accountBalancesUpdated',\n this.#handleSnapBalancesUpdatedBound,\n );\n\n // Subscribe to permission changes to detect new keyring snaps at runtime.\n // Re-runs snap discovery when permissions change.\n this.#messenger.subscribe(\n 'PermissionController:stateChange',\n this.#handlePermissionStateChangeBound,\n );\n }\n\n /**\n * Handle snap balance updates from the keyring.\n * Transforms the payload and publishes to AssetsController.\n *\n * @param payload - The balance update payload from AccountsController.\n */\n #handleSnapBalancesUpdated(\n payload: AccountBalancesUpdatedEventPayload,\n ): void {\n // Transform the snap keyring payload to DataResponse format\n let assetsBalance: NonNullable<DataResponse['assetsBalance']> | undefined;\n\n for (const [accountId, assets] of Object.entries(payload.balances)) {\n let accountAssets: Record<Caip19AssetId, AssetBalance> | undefined;\n\n for (const [assetId, balance] of Object.entries(assets)) {\n const chainId = extractChainFromAssetId(assetId);\n if (this.#isChainSupportedBySnap(chainId)) {\n accountAssets ??= {};\n accountAssets[assetId as Caip19AssetId] = {\n amount: balance.amount,\n };\n }\n }\n\n if (accountAssets) {\n assetsBalance ??= {};\n assetsBalance[accountId] = accountAssets;\n }\n }\n\n // Only report if we have snap-related updates\n if (assetsBalance) {\n const response: DataResponse = { assetsBalance };\n this.#messenger\n .call('AssetsController:assetsUpdate', response, SNAP_DATA_SOURCE_NAME)\n .catch(console.error);\n }\n }\n\n /**\n * Check if a chain ID is supported by any discovered snap.\n *\n * @param chainId - The CAIP-2 chain ID to check.\n * @returns True if we have a snap that supports this chain.\n */\n #isChainSupportedBySnap(chainId: ChainId): boolean {\n return this.state.activeChains.includes(chainId);\n }\n\n #registerActionHandlers(): void {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const messenger = this.#messenger as any;\n\n messenger.registerActionHandler(\n 'SnapDataSource:getAssetsMiddleware',\n () => this.assetsMiddleware,\n );\n\n messenger.registerActionHandler(\n 'SnapDataSource:getActiveChains',\n async () => this.getActiveChains(),\n );\n\n messenger.registerActionHandler(\n 'SnapDataSource:fetch',\n async (request: DataRequest) => this.fetch(request),\n );\n\n messenger.registerActionHandler(\n 'SnapDataSource:subscribe',\n async (request: SubscriptionRequest) => this.subscribe(request),\n );\n\n messenger.registerActionHandler(\n 'SnapDataSource:unsubscribe',\n async (subscriptionId: string) => this.unsubscribe(subscriptionId),\n );\n }\n\n // ============================================================================\n // SNAP DISCOVERY (Dynamic via PermissionController)\n // ============================================================================\n\n /**\n * Get all runnable snaps from SnapController.\n * Runnable snaps are enabled and not blocked.\n *\n * @returns Array of runnable snaps.\n */\n #getRunnableSnaps(): Snap[] {\n try {\n return this.#messenger.call('SnapController:getRunnableSnaps');\n } catch (error) {\n log('Failed to get runnable snaps', error);\n return [];\n }\n }\n\n /**\n * Get permissions for a snap from PermissionController.\n *\n * @param snapId - The snap ID to get permissions for.\n * @returns The snap's permissions, or undefined if none.\n */\n #getSnapPermissions(\n snapId: string,\n ): SubjectPermissions<PermissionConstraint> | undefined {\n try {\n return this.#messenger.call(\n 'PermissionController:getPermissions',\n snapId,\n ) as SubjectPermissions<PermissionConstraint>;\n } catch (error) {\n log('Failed to get permissions for snap', { snapId, error });\n return undefined;\n }\n }\n\n /**\n * Discover all snaps with keyring capabilities and their supported chains.\n * Uses PermissionController to find snaps with endowment:keyring permission.\n * Updates chainToSnap mapping and activeChains.\n *\n * Called on initialization and whenever PermissionController state changes\n * (e.g., new snaps installed, permissions granted/revoked).\n *\n * @remarks\n * **Known limitation:** If discovery fails (e.g., SnapController not ready),\n * the data source continues with empty chainToSnap. This means no snap\n * chains will be supported until a re-discovery is triggered by a permission\n * change. Callers should be aware that initialization may complete with no\n * active chains.\n */\n #discoverKeyringSnaps(): void {\n try {\n const runnableSnaps = this.#getRunnableSnaps();\n const chainToSnap: Record<ChainId, string> = {};\n const supportedChains: ChainId[] = [];\n\n for (const snap of runnableSnaps) {\n const permissions = this.#getSnapPermissions(snap.id);\n // Must have endowment:keyring permission to be a keyring snap\n if (!permissions?.[KEYRING_PERMISSION]) {\n continue;\n }\n\n // Get chainIds caveat from the assets permission (not keyring permission)\n // The chainIds are stored in endowment:assets\n const assetsPermission = permissions[ASSETS_PERMISSION];\n const chainIds = getChainIdsCaveat(assetsPermission);\n\n // Map each chain to this snap (first snap wins if multiple support same chain)\n if (chainIds) {\n for (const chainId of chainIds) {\n if (!(chainId in chainToSnap)) {\n chainToSnap[chainId] = snap.id;\n supportedChains.push(chainId);\n }\n }\n }\n }\n\n // Update chainToSnap mapping\n this.state.chainToSnap = chainToSnap;\n\n // Notify if chains changed\n try {\n this.updateActiveChains(supportedChains, (updatedChains) => {\n this.#messenger.call(\n 'AssetsController:activeChainsUpdate',\n SNAP_DATA_SOURCE_NAME,\n updatedChains,\n );\n });\n } catch {\n // AssetsController not ready yet - expected during initialization\n }\n } catch (error) {\n log('Keyring snap discovery failed', { error });\n this.state.chainToSnap = {};\n try {\n this.updateActiveChains([], (updatedChains) => {\n this.#messenger.call(\n 'AssetsController:activeChainsUpdate',\n SNAP_DATA_SOURCE_NAME,\n updatedChains,\n );\n });\n } catch {\n // AssetsController not ready yet - expected during initialization\n }\n }\n }\n\n // ============================================================================\n // FETCH\n // ============================================================================\n\n async fetch(request: DataRequest): Promise<DataResponse> {\n // Guard against undefined request\n // Note: chainIds filtering is done by middleware/subscribe before calling fetch\n if (!request?.accounts || !request?.chainIds?.length) {\n return {};\n }\n\n const results: DataResponse = {\n assetsBalance: {},\n assetsMetadata: {},\n };\n\n // Fetch balances for each account using its snap ID from metadata\n for (const account of request.accounts) {\n // Skip accounts without snap metadata (non-snap accounts)\n const snapId = account.metadata.snap?.id;\n if (!snapId) {\n continue;\n }\n\n // Skip accounts whose snap doesn't support any of the requested chains\n const snapSupportsRequestedChains = request.chainIds.some(\n (chainId) => this.state.chainToSnap[chainId] === snapId,\n );\n if (!snapSupportsRequestedChains) {\n continue;\n }\n\n const accountId = account.id;\n try {\n const client = this.#getKeyringClient(snapId);\n\n // Step 1: Get the list of assets for this account\n const accountAssets = await client.listAccountAssets(accountId);\n\n // If no assets, skip to next account\n if (!accountAssets || accountAssets.length === 0) {\n continue;\n }\n\n // Step 2: Get balances for those specific assets\n const balances: Record<CaipAssetType, Balance> =\n await client.getAccountBalances(\n accountId,\n accountAssets as CaipAssetType[],\n );\n\n // Transform keyring response to DataResponse format\n if (balances && typeof balances === 'object' && results.assetsBalance) {\n for (const [assetId, balance] of Object.entries(balances)) {\n results.assetsBalance[accountId] ??= {};\n const accountBalances = results.assetsBalance[accountId];\n if (accountBalances) {\n (accountBalances as Record<string, unknown>)[assetId] = {\n amount: balance.amount,\n };\n }\n }\n }\n } catch {\n // Expected when account doesn't belong to this snap\n }\n }\n\n return results;\n }\n\n // ============================================================================\n // MIDDLEWARE\n // ============================================================================\n\n /**\n * Get the middleware for fetching balances via Snaps.\n * This middleware:\n * - Supports multiple accounts in a single request\n * - Filters request to only chains this data source supports\n * - Fetches balances for those chains for all accounts\n * - Merges response into context\n * - Removes handled chains from request for next middleware\n *\n * @returns The middleware function for the assets pipeline.\n */\n get assetsMiddleware(): Middleware {\n return async (context, next) => {\n const { request } = context;\n\n // Filter to chains this data source supports\n const supportedChains = request.chainIds.filter((chainId) =>\n this.state.activeChains.includes(chainId),\n );\n\n // If no supported chains, skip and pass to next middleware\n if (supportedChains.length === 0) {\n return next(context);\n }\n\n let successfullyHandledChains: ChainId[] = [];\n\n try {\n // Fetch for supported chains\n const response = await this.fetch({\n ...request,\n chainIds: supportedChains,\n });\n\n // Merge response into context\n if (response.assetsBalance) {\n context.response.assetsBalance ??= {};\n for (const [accountId, accountBalances] of Object.entries(\n response.assetsBalance,\n )) {\n context.response.assetsBalance[accountId] = {\n ...context.response.assetsBalance[accountId],\n ...accountBalances,\n };\n }\n }\n if (response.assetsMetadata) {\n context.response.assetsMetadata = {\n ...context.response.assetsMetadata,\n ...response.assetsMetadata,\n };\n }\n if (response.assetsPrice) {\n context.response.assetsPrice = {\n ...context.response.assetsPrice,\n ...response.assetsPrice,\n };\n }\n\n // Determine successfully handled chains (exclude errors)\n const failedChains = new Set(Object.keys(response.errors ?? {}));\n successfullyHandledChains = supportedChains.filter(\n (chainId) => !failedChains.has(chainId),\n );\n } catch (error) {\n log('Middleware fetch failed', { error });\n successfullyHandledChains = [];\n }\n\n // Prepare context for next middleware\n let nextContext = context;\n if (successfullyHandledChains.length > 0) {\n const remainingChains = request.chainIds.filter(\n (chainId) => !successfullyHandledChains.includes(chainId),\n );\n nextContext = {\n ...context,\n request: {\n ...request,\n chainIds: remainingChains,\n },\n };\n }\n\n // Call next middleware\n return next(nextContext);\n };\n }\n\n // ============================================================================\n // SUBSCRIBE - Routes to appropriate snap(s)\n // ============================================================================\n\n async subscribe(subscriptionRequest: SubscriptionRequest): Promise<void> {\n const { request, subscriptionId, isUpdate } = subscriptionRequest;\n\n // Guard against undefined request or chainIds\n if (!request?.chainIds) {\n return;\n }\n\n // Filter to chains we have a snap for\n const supportedChains = request.chainIds.filter((chainId) =>\n this.#isChainSupportedBySnap(chainId),\n );\n\n if (supportedChains.length === 0) {\n return;\n }\n\n if (isUpdate) {\n const existing = this.activeSubscriptions.get(subscriptionId);\n if (existing) {\n existing.chains = supportedChains;\n // Do a fetch to get latest data on subscription update\n this.fetch({\n ...request,\n chainIds: supportedChains,\n })\n .then(async (fetchResponse) => {\n if (Object.keys(fetchResponse.assetsBalance ?? {}).length > 0) {\n await this.#messenger.call(\n 'AssetsController:assetsUpdate',\n fetchResponse,\n SNAP_DATA_SOURCE_NAME,\n );\n }\n return fetchResponse;\n })\n .catch((error) => {\n log('Subscription update fetch failed', { subscriptionId, error });\n });\n return;\n }\n }\n\n await this.unsubscribe(subscriptionId);\n\n // Snaps provide real-time updates via AccountsController:accountBalancesUpdated\n // We only need to track the subscription and do an initial fetch\n // No polling needed - updates come through #handleSnapBalancesUpdated\n\n this.activeSubscriptions.set(subscriptionId, {\n cleanup: () => {\n // No timer to clear - we use event-based updates\n },\n chains: supportedChains,\n });\n\n // Initial fetch to get current balances\n try {\n const fetchResponse = await this.fetch({\n ...request,\n chainIds: supportedChains,\n });\n\n if (Object.keys(fetchResponse.assetsBalance ?? {}).length > 0) {\n await this.#messenger.call(\n 'AssetsController:assetsUpdate',\n fetchResponse,\n SNAP_DATA_SOURCE_NAME,\n );\n }\n } catch (error) {\n log('Initial fetch failed', { subscriptionId, error });\n }\n }\n\n // ============================================================================\n // KEYRING CLIENT\n // ============================================================================\n\n /**\n * Gets a `KeyringClient` for a Snap.\n * Caches clients per snap ID to avoid re-instantiation across multiple calls.\n *\n * @param snapId - ID of the Snap to get the client for.\n * @returns A `KeyringClient` for the Snap.\n */\n #getKeyringClient(snapId: string): KeyringClient {\n const cachedClient = this.#keyringClientCache.get(snapId);\n if (cachedClient) {\n return cachedClient;\n }\n\n const client = new KeyringClient({\n send: async (request: JsonRpcRequest): Promise<Json> =>\n (await this.#messenger.call('SnapController:handleRequest', {\n snapId: snapId as SnapId,\n origin: 'metamask',\n handler: HandlerType.OnKeyringRequest,\n request,\n })) as Json,\n });\n\n this.#keyringClientCache.set(snapId, client);\n return client;\n }\n\n // ============================================================================\n // CLEANUP\n // ============================================================================\n\n destroy(): void {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const messenger = this.#messenger as any;\n\n // Unsubscribe from snap keyring events\n try {\n messenger.unsubscribe(\n 'AccountsController:accountBalancesUpdated',\n this.#handleSnapBalancesUpdatedBound,\n );\n } catch (error) {\n log('Failed to unsubscribe from snap keyring events', { error });\n }\n\n // Unsubscribe from permission changes\n try {\n messenger.unsubscribe(\n 'PermissionController:stateChange',\n this.#handlePermissionStateChangeBound,\n );\n } catch (error) {\n log('Failed to unsubscribe from permission changes', { error });\n }\n\n // Clean up active subscriptions\n for (const [subscriptionId] of this.activeSubscriptions) {\n this.unsubscribe(subscriptionId).catch(() => {\n // Ignore cleanup errors\n });\n }\n\n // Clear keyring client cache\n this.#keyringClientCache.clear();\n }\n}\n\n// ============================================================================\n// FACTORY\n// ============================================================================\n\nexport function createSnapDataSource(\n options: SnapDataSourceOptions,\n): SnapDataSource {\n return new SnapDataSource(options);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"SnapDataSource.cjs","sourceRoot":"","sources":["../../src/data-sources/SnapDataSource.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,uEAA8D;AAa9D,uDAAoE;AAGpE,iEAA0D;AAM1D,0CAA8D;AAqC9D,MAAM,GAAG,GAAG,IAAA,2BAAkB,EAAC,sBAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE,+EAA+E;AAC/E,YAAY;AACZ,+EAA+E;AAElE,QAAA,qBAAqB,GAAG,gBAAgB,CAAC;AAEtD,qDAAqD;AACxC,QAAA,kBAAkB,GAAG,mBAAmB,CAAC;AAEtD,wEAAwE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E,EAAE;gBAC5C,uBAAA,IAAI,6CAAuB,MAA3B,IAAI,EAAwB,aAAa,CAAC,CAAC;YAC7C,CAAC,CAAC,CAAC;QACL,CAAC;QAAC,MAAM,CAAC;YACP,kEAAkE;QACpE,CAAC;IACH,CAAC;AACH,CAAC,+EAgQiB,MAAc;IAC9B,MAAM,YAAY,GAAG,uBAAA,IAAI,0CAAoB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC1D,IAAI,YAAY,EAAE,CAAC;QACjB,OAAO,YAAY,CAAC;IACtB,CAAC;IAED,MAAM,MAAM,GAAG,IAAI,mCAAa,CAAC;QAC/B,IAAI,EAAE,KAAK,EAAE,OAAuB,EAAiB,EAAE,CACrD,MACE,uBAAA,IAAI,iCAGL,CAAC,IAAI,CAAC,8BAA8B,EAAE;YACrC,MAAM,EAAE,MAAgB;YACxB,MAAM,EAAE,UAAU;YAClB,OAAO,EAAE,yBAAW,CAAC,gBAAgB;YACrC,OAAO;SACR,CAAC;KACL,CAAC,CAAC;IAEH,uBAAA,IAAI,0CAAoB,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC7C,OAAO,MAAM,CAAC;AAChB,CAAC;AA0CH,+EAA+E;AAC/E,UAAU;AACV,+EAA+E;AAE/E,SAAgB,oBAAoB,CAClC,OAA8B;IAE9B,OAAO,IAAI,cAAc,CAAC,OAAO,CAAC,CAAC;AACrC,CAAC;AAJD,oDAIC","sourcesContent":["import type { Balance, CaipAssetType } from '@metamask/keyring-api';\nimport { KeyringClient } from '@metamask/keyring-snap-client';\nimport type {\n Caveat,\n GetPermissions,\n PermissionConstraint,\n PermissionControllerStateChange,\n SubjectPermissions,\n} from '@metamask/permission-controller';\nimport type {\n GetRunnableSnaps,\n HandleSnapRequest,\n} from '@metamask/snaps-controllers';\nimport type { Snap, SnapId } from '@metamask/snaps-sdk';\nimport { HandlerType, SnapCaveatType } from '@metamask/snaps-utils';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { AbstractDataSource } from './AbstractDataSource';\nimport type {\n DataSourceState,\n SubscriptionRequest,\n} from './AbstractDataSource';\nimport type { AssetsControllerMessenger } from '../AssetsController';\nimport { projectLogger, createModuleLogger } from '../logger';\nimport type {\n AssetBalance,\n ChainId,\n Caip19AssetId,\n DataRequest,\n DataResponse,\n Middleware,\n} from '../types';\n\n// ============================================================================\n// SNAP KEYRING EVENT TYPES\n// ============================================================================\n\n/**\n * Payload for AccountsController:accountBalancesUpdated event.\n * Re-published from SnapKeyring:accountBalancesUpdated.\n */\nexport type AccountBalancesUpdatedEventPayload = {\n balances: {\n [accountId: string]: {\n [assetId: string]: {\n amount: string;\n unit: string;\n };\n };\n };\n};\n\n/**\n * Event from AccountsController when snap balances are updated.\n */\nexport type AccountsControllerAccountBalancesUpdatedEvent = {\n type: 'AccountsController:accountBalancesUpdated';\n payload: [AccountBalancesUpdatedEventPayload];\n};\n\nconst log = createModuleLogger(projectLogger, 'SnapDataSource');\n\n// ============================================================================\n// CONSTANTS\n// ============================================================================\n\nexport const SNAP_DATA_SOURCE_NAME = 'SnapDataSource';\n\n/** The permission name for snap keyring endowment */\nexport const KEYRING_PERMISSION = 'endowment:keyring';\n\n/** The permission name for snap assets endowment (contains chainIds) */\nexport const ASSETS_PERMISSION = 'endowment:assets';\n\n// ============================================================================\n// PERMISSION UTILITIES\n// ============================================================================\n\n/**\n * Getter function to get the chainIds caveat from a permission.\n *\n * This does basic validation of the caveat, but does not validate the type or\n * value of the namespaces object itself, as this is handled by the\n * `PermissionsController` when the permission is requested.\n *\n * @param permission - The permission to get the `chainIds` caveat from.\n * @returns An array of `chainIds` that the snap supports, or null if none.\n */\nexport function getChainIdsCaveat(\n permission?: PermissionConstraint,\n): ChainId[] | null {\n if (!permission?.caveats) {\n return null;\n }\n\n const caveat = permission.caveats.find(\n (permCaveat) => permCaveat.type === SnapCaveatType.ChainIds,\n ) as Caveat<string, string[]> | undefined;\n\n return caveat ? (caveat.value as ChainId[]) : null;\n}\n\n/**\n * Extract chain ID from a CAIP-19 asset ID.\n * e.g., \"solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501\" -> \"solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp\"\n *\n * @param assetId - The CAIP-19 asset ID to extract chain from.\n * @returns The CAIP-2 chain ID portion of the asset ID.\n */\nexport function extractChainFromAssetId(assetId: string): ChainId {\n const parts = assetId.split('/');\n return parts[0] as ChainId;\n}\n\n// ============================================================================\n// STATE\n// ============================================================================\n\n/**\n * State for the SnapDataSource.\n * Uses dynamic snap discovery - chains are populated from PermissionController.\n */\nexport type SnapDataSourceState = {\n /**\n * Mapping of chain IDs to snap IDs that support them.\n * Used to filter which accounts to process for a given chain request.\n */\n chainToSnap: Record<ChainId, string>;\n} & DataSourceState;\n\nconst defaultSnapState: SnapDataSourceState = {\n activeChains: [],\n chainToSnap: {},\n};\n\n// ============================================================================\n// MESSENGER TYPES\n// ============================================================================\n\n/**\n * Allowed events that SnapDataSource can subscribe to.\n */\nexport type SnapDataSourceAllowedEvents =\n | AccountsControllerAccountBalancesUpdatedEvent\n | PermissionControllerStateChange;\n\nexport type SnapDataSourceAllowedActions =\n | GetRunnableSnaps\n | HandleSnapRequest\n | GetPermissions;\n\n// ============================================================================\n// OPTIONS\n// ============================================================================\n\nexport type SnapDataSourceOptions = {\n /** The AssetsController messenger (shared by all data sources). */\n messenger: AssetsControllerMessenger;\n /** Called when this data source's active chains change. */\n onActiveChainsUpdated: (chains: ChainId[]) => void;\n /** Configured networks to support (defaults to all snap networks) */\n configuredNetworks?: ChainId[];\n /** Default polling interval in ms for subscriptions */\n pollInterval?: number;\n /** Initial state */\n state?: Partial<SnapDataSourceState>;\n};\n\n// ============================================================================\n// SNAP DATA SOURCE\n// ============================================================================\n\n/**\n * Unified Snap data source that routes requests to the appropriate wallet snap\n * based on the chain ID prefix.\n *\n * @example\n * ```typescript\n * const snapDataSource = new SnapDataSource({\n * messenger,\n * onActiveChainsUpdated: (chains) => { /* ... *\\/ },\n * });\n *\n * // Fetch will automatically route to the correct snap\n * await snapDataSource.fetch({\n * chainIds: ['solana:mainnet', 'bip122:000000000019d6689c085ae165831e93'],\n * accountIds: ['account1'],\n * });\n * ```\n */\nexport class SnapDataSource extends AbstractDataSource<\n typeof SNAP_DATA_SOURCE_NAME,\n SnapDataSourceState\n> {\n readonly #messenger: AssetsControllerMessenger;\n\n readonly #onActiveChainsUpdated: (chains: ChainId[]) => void;\n\n /** Bound handler for snap keyring balance updates, stored for cleanup */\n readonly #handleSnapBalancesUpdatedBound: (\n payload: AccountBalancesUpdatedEventPayload,\n ) => void;\n\n readonly #handlePermissionStateChangeBound: () => void;\n\n /** Cache of KeyringClient instances per snap ID to avoid re-instantiation */\n readonly #keyringClientCache: Map<string, KeyringClient> = new Map();\n\n constructor(options: SnapDataSourceOptions) {\n super(SNAP_DATA_SOURCE_NAME, {\n ...defaultSnapState,\n ...options.state,\n });\n\n this.#messenger = options.messenger;\n this.#onActiveChainsUpdated = options.onActiveChainsUpdated;\n\n // Bind handlers for cleanup in destroy()\n this.#handleSnapBalancesUpdatedBound = this.#handleSnapBalancesUpdated.bind(\n this,\n ) as (payload: AccountBalancesUpdatedEventPayload) => void;\n this.#handlePermissionStateChangeBound =\n this.#discoverKeyringSnaps.bind(this);\n\n this.#subscribeToEvents();\n\n // Discover keyring-capable snaps and populate activeChains dynamically\n this.#discoverKeyringSnaps();\n }\n\n /**\n * Subscribe to all events needed by SnapDataSource.\n * Groups snap keyring events and permission change events.\n */\n #subscribeToEvents(): void {\n // Subscribe to snap keyring events and permission changes (not in AssetsControllerEvents).\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const messenger = this.#messenger as any;\n messenger.subscribe(\n 'AccountsController:accountBalancesUpdated',\n this.#handleSnapBalancesUpdatedBound,\n );\n messenger.subscribe(\n 'PermissionController:stateChange',\n this.#handlePermissionStateChangeBound,\n );\n }\n\n /**\n * Handle snap balance updates from the keyring.\n * Transforms the payload and publishes to AssetsController.\n *\n * @param payload - The balance update payload from AccountsController.\n */\n #handleSnapBalancesUpdated(\n payload: AccountBalancesUpdatedEventPayload,\n ): void {\n // Transform the snap keyring payload to DataResponse format\n let assetsBalance: NonNullable<DataResponse['assetsBalance']> | undefined;\n\n for (const [accountId, assets] of Object.entries(payload.balances)) {\n let accountAssets: Record<Caip19AssetId, AssetBalance> | undefined;\n\n for (const [assetId, balance] of Object.entries(assets)) {\n const chainId = extractChainFromAssetId(assetId);\n if (this.#isChainSupportedBySnap(chainId)) {\n accountAssets ??= {};\n accountAssets[assetId as Caip19AssetId] = {\n amount: balance.amount,\n };\n }\n }\n\n if (accountAssets) {\n assetsBalance ??= {};\n assetsBalance[accountId] = accountAssets;\n }\n }\n\n // Only report if we have snap-related updates\n if (assetsBalance) {\n const response: DataResponse = { assetsBalance };\n for (const subscription of this.activeSubscriptions.values()) {\n subscription.onAssetsUpdate(response)?.catch(console.error);\n }\n }\n }\n\n /**\n * Check if a chain ID is supported by any discovered snap.\n *\n * @param chainId - The CAIP-2 chain ID to check.\n * @returns True if we have a snap that supports this chain.\n */\n #isChainSupportedBySnap(chainId: ChainId): boolean {\n return this.state.activeChains.includes(chainId);\n }\n\n // ============================================================================\n // SNAP DISCOVERY (Dynamic via PermissionController)\n // ============================================================================\n\n /**\n * Get all runnable snaps from SnapController.\n * Runnable snaps are enabled and not blocked.\n *\n * @returns Array of runnable snaps.\n */\n #getRunnableSnaps(): Snap[] {\n try {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (this.#messenger as any).call(\n 'SnapController:getRunnableSnaps',\n ) as Snap[];\n } catch (error) {\n log('Failed to get runnable snaps', error);\n return [];\n }\n }\n\n /**\n * Get permissions for a snap from PermissionController.\n *\n * @param snapId - The snap ID to get permissions for.\n * @returns The snap's permissions, or undefined if none.\n */\n #getSnapPermissions(\n snapId: string,\n ): SubjectPermissions<PermissionConstraint> | undefined {\n try {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (this.#messenger as any).call(\n 'PermissionController:getPermissions',\n snapId,\n ) as SubjectPermissions<PermissionConstraint>;\n } catch (error) {\n log('Failed to get permissions for snap', { snapId, error });\n return undefined;\n }\n }\n\n /**\n * Discover all snaps with keyring capabilities and their supported chains.\n * Uses PermissionController to find snaps with endowment:keyring permission.\n * Updates chainToSnap mapping and activeChains.\n *\n * Called on initialization and whenever PermissionController state changes\n * (e.g., new snaps installed, permissions granted/revoked).\n *\n * @remarks\n * **Known limitation:** If discovery fails (e.g., SnapController not ready),\n * the data source continues with empty chainToSnap. This means no snap\n * chains will be supported until a re-discovery is triggered by a permission\n * change. Callers should be aware that initialization may complete with no\n * active chains.\n */\n #discoverKeyringSnaps(): void {\n try {\n const runnableSnaps = this.#getRunnableSnaps();\n const chainToSnap: Record<ChainId, string> = {};\n const supportedChains: ChainId[] = [];\n\n for (const snap of runnableSnaps) {\n const permissions = this.#getSnapPermissions(snap.id);\n // Must have endowment:keyring permission to be a keyring snap\n if (!permissions?.[KEYRING_PERMISSION]) {\n continue;\n }\n\n // Get chainIds caveat from the assets permission (not keyring permission)\n // The chainIds are stored in endowment:assets\n const assetsPermission = permissions[ASSETS_PERMISSION];\n const chainIds = getChainIdsCaveat(assetsPermission);\n\n // Map each chain to this snap (first snap wins if multiple support same chain)\n if (chainIds) {\n for (const chainId of chainIds) {\n if (!(chainId in chainToSnap)) {\n chainToSnap[chainId] = snap.id;\n supportedChains.push(chainId);\n }\n }\n }\n }\n\n // Update chainToSnap mapping\n this.state.chainToSnap = chainToSnap;\n\n // Notify if chains changed\n try {\n this.updateActiveChains(supportedChains, (updatedChains) => {\n this.#onActiveChainsUpdated(updatedChains);\n });\n } catch {\n // AssetsController not ready yet - expected during initialization\n }\n } catch (error) {\n log('Keyring snap discovery failed', { error });\n this.state.chainToSnap = {};\n try {\n this.updateActiveChains([], (updatedChains) => {\n this.#onActiveChainsUpdated(updatedChains);\n });\n } catch {\n // AssetsController not ready yet - expected during initialization\n }\n }\n }\n\n // ============================================================================\n // FETCH\n // ============================================================================\n\n async fetch(request: DataRequest): Promise<DataResponse> {\n // Guard against undefined request\n // Note: chainIds filtering is done by middleware/subscribe before calling fetch\n if (!request?.chainIds?.length) {\n return {};\n }\n if (!request?.accountsWithSupportedChains?.length) {\n return { assetsBalance: {}, assetsMetadata: {} };\n }\n\n const results: DataResponse = {\n assetsBalance: {},\n assetsMetadata: {},\n };\n\n // Fetch balances for each account using its snap ID from metadata\n for (const { account } of request.accountsWithSupportedChains) {\n // Skip accounts without snap metadata (non-snap accounts)\n const snapId = account.metadata.snap?.id;\n if (!snapId) {\n continue;\n }\n\n // Skip accounts whose snap doesn't support any of the requested chains\n const snapSupportsRequestedChains = request.chainIds.some(\n (chainId) => this.state.chainToSnap[chainId] === snapId,\n );\n if (!snapSupportsRequestedChains) {\n continue;\n }\n\n const accountId = account.id;\n try {\n const client = this.#getKeyringClient(snapId);\n\n // Step 1: Get the list of assets for this account\n const accountAssets = await client.listAccountAssets(accountId);\n\n // If no assets, skip to next account\n if (!accountAssets || accountAssets.length === 0) {\n continue;\n }\n\n // Step 2: Get balances for those specific assets\n const balances: Record<CaipAssetType, Balance> =\n await client.getAccountBalances(\n accountId,\n accountAssets as CaipAssetType[],\n );\n\n // Transform keyring response to DataResponse format\n if (balances && typeof balances === 'object' && results.assetsBalance) {\n for (const [assetId, balance] of Object.entries(balances)) {\n results.assetsBalance[accountId] ??= {};\n const accountBalances = results.assetsBalance[accountId];\n if (accountBalances) {\n (accountBalances as Record<string, unknown>)[assetId] = {\n amount: balance.amount,\n };\n }\n }\n }\n } catch {\n // Expected when account doesn't belong to this snap\n }\n }\n\n return results;\n }\n\n // ============================================================================\n // MIDDLEWARE\n // ============================================================================\n\n /**\n * Get the middleware for fetching balances via Snaps.\n * This middleware:\n * - Supports multiple accounts in a single request\n * - Filters request to only chains this data source supports\n * - Fetches balances for those chains for all accounts\n * - Merges response into context\n * - Removes handled chains from request for next middleware\n *\n * @returns The middleware function for the assets pipeline.\n */\n get assetsMiddleware(): Middleware {\n return async (context, next) => {\n const { request } = context;\n\n // Filter to chains this data source supports\n const supportedChains = request.chainIds.filter((chainId) =>\n this.state.activeChains.includes(chainId),\n );\n\n // If no supported chains, skip and pass to next middleware\n if (supportedChains.length === 0) {\n return next(context);\n }\n\n let successfullyHandledChains: ChainId[] = [];\n\n try {\n // Fetch for supported chains\n const response = await this.fetch({\n ...request,\n chainIds: supportedChains,\n });\n\n // Merge response into context\n if (response.assetsBalance) {\n context.response.assetsBalance ??= {};\n for (const [accountId, accountBalances] of Object.entries(\n response.assetsBalance,\n )) {\n context.response.assetsBalance[accountId] = {\n ...context.response.assetsBalance[accountId],\n ...accountBalances,\n };\n }\n }\n if (response.assetsMetadata) {\n context.response.assetsMetadata = {\n ...context.response.assetsMetadata,\n ...response.assetsMetadata,\n };\n }\n if (response.assetsPrice) {\n context.response.assetsPrice = {\n ...context.response.assetsPrice,\n ...response.assetsPrice,\n };\n }\n\n // Determine successfully handled chains (exclude errors)\n const failedChains = new Set(Object.keys(response.errors ?? {}));\n successfullyHandledChains = supportedChains.filter(\n (chainId) => !failedChains.has(chainId),\n );\n } catch (error) {\n log('Middleware fetch failed', { error });\n successfullyHandledChains = [];\n }\n\n // Prepare context for next middleware\n let nextContext = context;\n if (successfullyHandledChains.length > 0) {\n const remainingChains = request.chainIds.filter(\n (chainId) => !successfullyHandledChains.includes(chainId),\n );\n nextContext = {\n ...context,\n request: {\n ...request,\n chainIds: remainingChains,\n },\n };\n }\n\n // Call next middleware\n return next(nextContext);\n };\n }\n\n // ============================================================================\n // SUBSCRIBE - Routes to appropriate snap(s)\n // ============================================================================\n\n async subscribe(subscriptionRequest: SubscriptionRequest): Promise<void> {\n const { request, subscriptionId, isUpdate } = subscriptionRequest;\n\n // Guard against undefined request or chainIds\n if (!request?.chainIds) {\n return;\n }\n\n // Filter to chains we have a snap for\n const supportedChains = request.chainIds.filter((chainId) =>\n this.#isChainSupportedBySnap(chainId),\n );\n\n if (supportedChains.length === 0) {\n return;\n }\n\n if (isUpdate) {\n const existing = this.activeSubscriptions.get(subscriptionId);\n if (existing) {\n existing.chains = supportedChains;\n // Do a fetch to get latest data on subscription update\n this.fetch({\n ...request,\n chainIds: supportedChains,\n })\n .then(async (fetchResponse) => {\n if (Object.keys(fetchResponse.assetsBalance ?? {}).length > 0) {\n await existing.onAssetsUpdate(fetchResponse);\n }\n return fetchResponse;\n })\n .catch((error) => {\n log('Subscription update fetch failed', { subscriptionId, error });\n });\n return;\n }\n }\n\n await this.unsubscribe(subscriptionId);\n\n // Snaps provide real-time updates via AccountsController:accountBalancesUpdated\n // We only need to track the subscription and do an initial fetch\n // No polling needed - updates come through #handleSnapBalancesUpdated\n\n this.activeSubscriptions.set(subscriptionId, {\n cleanup: () => {\n // No timer to clear - we use event-based updates\n },\n chains: supportedChains,\n onAssetsUpdate: subscriptionRequest.onAssetsUpdate,\n });\n\n // Initial fetch to get current balances\n try {\n const fetchResponse = await this.fetch({\n ...request,\n chainIds: supportedChains,\n });\n\n const subscription = this.activeSubscriptions.get(subscriptionId);\n if (\n Object.keys(fetchResponse.assetsBalance ?? {}).length > 0 &&\n subscription\n ) {\n await subscription.onAssetsUpdate(fetchResponse);\n }\n } catch (error) {\n log('Initial fetch failed', { subscriptionId, error });\n }\n }\n\n // ============================================================================\n // KEYRING CLIENT\n // ============================================================================\n\n /**\n * Gets a `KeyringClient` for a Snap.\n * Caches clients per snap ID to avoid re-instantiation across multiple calls.\n *\n * @param snapId - ID of the Snap to get the client for.\n * @returns A `KeyringClient` for the Snap.\n */\n #getKeyringClient(snapId: string): KeyringClient {\n const cachedClient = this.#keyringClientCache.get(snapId);\n if (cachedClient) {\n return cachedClient;\n }\n\n const client = new KeyringClient({\n send: async (request: JsonRpcRequest): Promise<Json> =>\n await (\n this.#messenger as unknown as {\n call: (action: string, ...args: unknown[]) => Promise<Json> | Json;\n }\n ).call('SnapController:handleRequest', {\n snapId: snapId as SnapId,\n origin: 'metamask',\n handler: HandlerType.OnKeyringRequest,\n request,\n }),\n });\n\n this.#keyringClientCache.set(snapId, client);\n return client;\n }\n\n // ============================================================================\n // CLEANUP\n // ============================================================================\n\n destroy(): void {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const messenger = this.#messenger as any;\n\n // Unsubscribe from snap keyring events\n try {\n messenger.unsubscribe(\n 'AccountsController:accountBalancesUpdated',\n this.#handleSnapBalancesUpdatedBound,\n );\n } catch (error) {\n log('Failed to unsubscribe from snap keyring events', { error });\n }\n\n // Unsubscribe from permission changes\n try {\n messenger.unsubscribe(\n 'PermissionController:stateChange',\n this.#handlePermissionStateChangeBound,\n );\n } catch (error) {\n log('Failed to unsubscribe from permission changes', { error });\n }\n\n // Clean up active subscriptions\n for (const [subscriptionId] of this.activeSubscriptions) {\n this.unsubscribe(subscriptionId).catch(() => {\n // Ignore cleanup errors\n });\n }\n\n // Clear keyring client cache\n this.#keyringClientCache.clear();\n }\n}\n\n// ============================================================================\n// FACTORY\n// ============================================================================\n\nexport function createSnapDataSource(\n options: SnapDataSourceOptions,\n): SnapDataSource {\n return new SnapDataSource(options);\n}\n"]}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { Messenger } from "@metamask/messenger";
|
|
2
1
|
import type { GetPermissions, PermissionConstraint, PermissionControllerStateChange } from "@metamask/permission-controller";
|
|
3
2
|
import type { GetRunnableSnaps, HandleSnapRequest } from "@metamask/snaps-controllers";
|
|
4
3
|
import { AbstractDataSource } from "./AbstractDataSource.cjs";
|
|
5
4
|
import type { DataSourceState, SubscriptionRequest } from "./AbstractDataSource.cjs";
|
|
5
|
+
import type { AssetsControllerMessenger } from "../AssetsController.cjs";
|
|
6
6
|
import type { ChainId, DataRequest, DataResponse, Middleware } from "../types.cjs";
|
|
7
7
|
/**
|
|
8
8
|
* Payload for AccountsController:accountBalancesUpdated event.
|
|
@@ -60,53 +60,16 @@ export type SnapDataSourceState = {
|
|
|
60
60
|
*/
|
|
61
61
|
chainToSnap: Record<ChainId, string>;
|
|
62
62
|
} & DataSourceState;
|
|
63
|
-
export type SnapDataSourceGetAssetsMiddlewareAction = {
|
|
64
|
-
type: 'SnapDataSource:getAssetsMiddleware';
|
|
65
|
-
handler: () => Middleware;
|
|
66
|
-
};
|
|
67
|
-
export type SnapDataSourceGetActiveChainsAction = {
|
|
68
|
-
type: 'SnapDataSource:getActiveChains';
|
|
69
|
-
handler: () => Promise<ChainId[]>;
|
|
70
|
-
};
|
|
71
|
-
export type SnapDataSourceFetchAction = {
|
|
72
|
-
type: 'SnapDataSource:fetch';
|
|
73
|
-
handler: (request: DataRequest) => Promise<DataResponse>;
|
|
74
|
-
};
|
|
75
|
-
export type SnapDataSourceSubscribeAction = {
|
|
76
|
-
type: 'SnapDataSource:subscribe';
|
|
77
|
-
handler: (request: SubscriptionRequest) => Promise<void>;
|
|
78
|
-
};
|
|
79
|
-
export type SnapDataSourceUnsubscribeAction = {
|
|
80
|
-
type: 'SnapDataSource:unsubscribe';
|
|
81
|
-
handler: (subscriptionId: string) => Promise<void>;
|
|
82
|
-
};
|
|
83
|
-
export type SnapDataSourceActions = SnapDataSourceGetAssetsMiddlewareAction | SnapDataSourceGetActiveChainsAction | SnapDataSourceFetchAction | SnapDataSourceSubscribeAction | SnapDataSourceUnsubscribeAction;
|
|
84
|
-
export type SnapDataSourceActiveChainsChangedEvent = {
|
|
85
|
-
type: 'SnapDataSource:activeChainsUpdated';
|
|
86
|
-
payload: [ChainId[]];
|
|
87
|
-
};
|
|
88
|
-
export type SnapDataSourceAssetsUpdatedEvent = {
|
|
89
|
-
type: 'SnapDataSource:assetsUpdated';
|
|
90
|
-
payload: [DataResponse, string | undefined];
|
|
91
|
-
};
|
|
92
|
-
export type SnapDataSourceEvents = SnapDataSourceActiveChainsChangedEvent | SnapDataSourceAssetsUpdatedEvent;
|
|
93
63
|
/**
|
|
94
64
|
* Allowed events that SnapDataSource can subscribe to.
|
|
95
65
|
*/
|
|
96
66
|
export type SnapDataSourceAllowedEvents = AccountsControllerAccountBalancesUpdatedEvent | PermissionControllerStateChange;
|
|
97
|
-
type
|
|
98
|
-
type: 'AssetsController:activeChainsUpdate';
|
|
99
|
-
handler: (dataSourceId: string, activeChains: ChainId[]) => void;
|
|
100
|
-
};
|
|
101
|
-
type AssetsControllerAssetsUpdateAction = {
|
|
102
|
-
type: 'AssetsController:assetsUpdate';
|
|
103
|
-
handler: (response: DataResponse, sourceId: string) => Promise<void>;
|
|
104
|
-
};
|
|
105
|
-
export type SnapDataSourceAllowedActions = AssetsControllerActiveChainsUpdateAction | AssetsControllerAssetsUpdateAction | GetRunnableSnaps | HandleSnapRequest | GetPermissions;
|
|
106
|
-
export type SnapDataSourceMessenger = Messenger<typeof SNAP_DATA_SOURCE_NAME, SnapDataSourceActions | SnapDataSourceAllowedActions, SnapDataSourceEvents | SnapDataSourceAllowedEvents>;
|
|
67
|
+
export type SnapDataSourceAllowedActions = GetRunnableSnaps | HandleSnapRequest | GetPermissions;
|
|
107
68
|
export type SnapDataSourceOptions = {
|
|
108
|
-
/**
|
|
109
|
-
messenger:
|
|
69
|
+
/** The AssetsController messenger (shared by all data sources). */
|
|
70
|
+
messenger: AssetsControllerMessenger;
|
|
71
|
+
/** Called when this data source's active chains change. */
|
|
72
|
+
onActiveChainsUpdated: (chains: ChainId[]) => void;
|
|
110
73
|
/** Configured networks to support (defaults to all snap networks) */
|
|
111
74
|
configuredNetworks?: ChainId[];
|
|
112
75
|
/** Default polling interval in ms for subscriptions */
|
|
@@ -122,6 +85,7 @@ export type SnapDataSourceOptions = {
|
|
|
122
85
|
* ```typescript
|
|
123
86
|
* const snapDataSource = new SnapDataSource({
|
|
124
87
|
* messenger,
|
|
88
|
+
* onActiveChainsUpdated: (chains) => { /* ... *\/ },
|
|
125
89
|
* });
|
|
126
90
|
*
|
|
127
91
|
* // Fetch will automatically route to the correct snap
|
|
@@ -151,5 +115,4 @@ export declare class SnapDataSource extends AbstractDataSource<typeof SNAP_DATA_
|
|
|
151
115
|
destroy(): void;
|
|
152
116
|
}
|
|
153
117
|
export declare function createSnapDataSource(options: SnapDataSourceOptions): SnapDataSource;
|
|
154
|
-
export {};
|
|
155
118
|
//# sourceMappingURL=SnapDataSource.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapDataSource.d.cts","sourceRoot":"","sources":["../../src/data-sources/SnapDataSource.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"SnapDataSource.d.cts","sourceRoot":"","sources":["../../src/data-sources/SnapDataSource.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAEV,cAAc,EACd,oBAAoB,EACpB,+BAA+B,EAEhC,wCAAwC;AACzC,OAAO,KAAK,EACV,gBAAgB,EAChB,iBAAiB,EAClB,oCAAoC;AAKrC,OAAO,EAAE,kBAAkB,EAAE,iCAA6B;AAC1D,OAAO,KAAK,EACV,eAAe,EACf,mBAAmB,EACpB,iCAA6B;AAC9B,OAAO,KAAK,EAAE,yBAAyB,EAAE,gCAA4B;AAErE,OAAO,KAAK,EAEV,OAAO,EAEP,WAAW,EACX,YAAY,EACZ,UAAU,EACX,qBAAiB;AAMlB;;;GAGG;AACH,MAAM,MAAM,kCAAkC,GAAG;IAC/C,QAAQ,EAAE;QACR,CAAC,SAAS,EAAE,MAAM,GAAG;YACnB,CAAC,OAAO,EAAE,MAAM,GAAG;gBACjB,MAAM,EAAE,MAAM,CAAC;gBACf,IAAI,EAAE,MAAM,CAAC;aACd,CAAC;SACH,CAAC;KACH,CAAC;CACH,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,6CAA6C,GAAG;IAC1D,IAAI,EAAE,2CAA2C,CAAC;IAClD,OAAO,EAAE,CAAC,kCAAkC,CAAC,CAAC;CAC/C,CAAC;AAQF,eAAO,MAAM,qBAAqB,mBAAmB,CAAC;AAEtD,qDAAqD;AACrD,eAAO,MAAM,kBAAkB,sBAAsB,CAAC;AAEtD,wEAAwE;AACxE,eAAO,MAAM,iBAAiB,qBAAq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AMD,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,qBAAqB,GAC7B,cAAc,CAEhB"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { Messenger } from "@metamask/messenger";
|
|
2
1
|
import type { GetPermissions, PermissionConstraint, PermissionControllerStateChange } from "@metamask/permission-controller";
|
|
3
2
|
import type { GetRunnableSnaps, HandleSnapRequest } from "@metamask/snaps-controllers";
|
|
4
3
|
import { AbstractDataSource } from "./AbstractDataSource.mjs";
|
|
5
4
|
import type { DataSourceState, SubscriptionRequest } from "./AbstractDataSource.mjs";
|
|
5
|
+
import type { AssetsControllerMessenger } from "../AssetsController.mjs";
|
|
6
6
|
import type { ChainId, DataRequest, DataResponse, Middleware } from "../types.mjs";
|
|
7
7
|
/**
|
|
8
8
|
* Payload for AccountsController:accountBalancesUpdated event.
|
|
@@ -60,53 +60,16 @@ export type SnapDataSourceState = {
|
|
|
60
60
|
*/
|
|
61
61
|
chainToSnap: Record<ChainId, string>;
|
|
62
62
|
} & DataSourceState;
|
|
63
|
-
export type SnapDataSourceGetAssetsMiddlewareAction = {
|
|
64
|
-
type: 'SnapDataSource:getAssetsMiddleware';
|
|
65
|
-
handler: () => Middleware;
|
|
66
|
-
};
|
|
67
|
-
export type SnapDataSourceGetActiveChainsAction = {
|
|
68
|
-
type: 'SnapDataSource:getActiveChains';
|
|
69
|
-
handler: () => Promise<ChainId[]>;
|
|
70
|
-
};
|
|
71
|
-
export type SnapDataSourceFetchAction = {
|
|
72
|
-
type: 'SnapDataSource:fetch';
|
|
73
|
-
handler: (request: DataRequest) => Promise<DataResponse>;
|
|
74
|
-
};
|
|
75
|
-
export type SnapDataSourceSubscribeAction = {
|
|
76
|
-
type: 'SnapDataSource:subscribe';
|
|
77
|
-
handler: (request: SubscriptionRequest) => Promise<void>;
|
|
78
|
-
};
|
|
79
|
-
export type SnapDataSourceUnsubscribeAction = {
|
|
80
|
-
type: 'SnapDataSource:unsubscribe';
|
|
81
|
-
handler: (subscriptionId: string) => Promise<void>;
|
|
82
|
-
};
|
|
83
|
-
export type SnapDataSourceActions = SnapDataSourceGetAssetsMiddlewareAction | SnapDataSourceGetActiveChainsAction | SnapDataSourceFetchAction | SnapDataSourceSubscribeAction | SnapDataSourceUnsubscribeAction;
|
|
84
|
-
export type SnapDataSourceActiveChainsChangedEvent = {
|
|
85
|
-
type: 'SnapDataSource:activeChainsUpdated';
|
|
86
|
-
payload: [ChainId[]];
|
|
87
|
-
};
|
|
88
|
-
export type SnapDataSourceAssetsUpdatedEvent = {
|
|
89
|
-
type: 'SnapDataSource:assetsUpdated';
|
|
90
|
-
payload: [DataResponse, string | undefined];
|
|
91
|
-
};
|
|
92
|
-
export type SnapDataSourceEvents = SnapDataSourceActiveChainsChangedEvent | SnapDataSourceAssetsUpdatedEvent;
|
|
93
63
|
/**
|
|
94
64
|
* Allowed events that SnapDataSource can subscribe to.
|
|
95
65
|
*/
|
|
96
66
|
export type SnapDataSourceAllowedEvents = AccountsControllerAccountBalancesUpdatedEvent | PermissionControllerStateChange;
|
|
97
|
-
type
|
|
98
|
-
type: 'AssetsController:activeChainsUpdate';
|
|
99
|
-
handler: (dataSourceId: string, activeChains: ChainId[]) => void;
|
|
100
|
-
};
|
|
101
|
-
type AssetsControllerAssetsUpdateAction = {
|
|
102
|
-
type: 'AssetsController:assetsUpdate';
|
|
103
|
-
handler: (response: DataResponse, sourceId: string) => Promise<void>;
|
|
104
|
-
};
|
|
105
|
-
export type SnapDataSourceAllowedActions = AssetsControllerActiveChainsUpdateAction | AssetsControllerAssetsUpdateAction | GetRunnableSnaps | HandleSnapRequest | GetPermissions;
|
|
106
|
-
export type SnapDataSourceMessenger = Messenger<typeof SNAP_DATA_SOURCE_NAME, SnapDataSourceActions | SnapDataSourceAllowedActions, SnapDataSourceEvents | SnapDataSourceAllowedEvents>;
|
|
67
|
+
export type SnapDataSourceAllowedActions = GetRunnableSnaps | HandleSnapRequest | GetPermissions;
|
|
107
68
|
export type SnapDataSourceOptions = {
|
|
108
|
-
/**
|
|
109
|
-
messenger:
|
|
69
|
+
/** The AssetsController messenger (shared by all data sources). */
|
|
70
|
+
messenger: AssetsControllerMessenger;
|
|
71
|
+
/** Called when this data source's active chains change. */
|
|
72
|
+
onActiveChainsUpdated: (chains: ChainId[]) => void;
|
|
110
73
|
/** Configured networks to support (defaults to all snap networks) */
|
|
111
74
|
configuredNetworks?: ChainId[];
|
|
112
75
|
/** Default polling interval in ms for subscriptions */
|
|
@@ -122,6 +85,7 @@ export type SnapDataSourceOptions = {
|
|
|
122
85
|
* ```typescript
|
|
123
86
|
* const snapDataSource = new SnapDataSource({
|
|
124
87
|
* messenger,
|
|
88
|
+
* onActiveChainsUpdated: (chains) => { /* ... *\/ },
|
|
125
89
|
* });
|
|
126
90
|
*
|
|
127
91
|
* // Fetch will automatically route to the correct snap
|
|
@@ -151,5 +115,4 @@ export declare class SnapDataSource extends AbstractDataSource<typeof SNAP_DATA_
|
|
|
151
115
|
destroy(): void;
|
|
152
116
|
}
|
|
153
117
|
export declare function createSnapDataSource(options: SnapDataSourceOptions): SnapDataSource;
|
|
154
|
-
export {};
|
|
155
118
|
//# sourceMappingURL=SnapDataSource.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapDataSource.d.mts","sourceRoot":"","sources":["../../src/data-sources/SnapDataSource.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"SnapDataSource.d.mts","sourceRoot":"","sources":["../../src/data-sources/SnapDataSource.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAEV,cAAc,EACd,oBAAoB,EACpB,+BAA+B,EAEhC,wCAAwC;AACzC,OAAO,KAAK,EACV,gBAAgB,EAChB,iBAAiB,EAClB,oCAAoC;AAKrC,OAAO,EAAE,kBAAkB,EAAE,iCAA6B;AAC1D,OAAO,KAAK,EACV,eAAe,EACf,mBAAmB,EACpB,iCAA6B;AAC9B,OAAO,KAAK,EAAE,yBAAyB,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CAAC,MAAM,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC;IACnD,qEAAqE;IACrE,kBAAkB,CAAC,EAAE,OAAO,EAAE,CAAC;IAC/B,uDAAuD;IACvD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,oBAAoB;IACpB,KAAK,CAAC,EAAE,OAAO,CAAC,mBAAmB,CAAC,CAAC;CACtC,CAAC;AAMF;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,cAAe,SAAQ,kBAAkB,CACpD,OAAO,qBAAqB,EAC5B,mBAAmB,CACpB;;gBAea,OAAO,EAAE,qBAAqB;IA6MpC,KAAK,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;IA0ExD;;;;;;;;;;OAUG;IACH,IAAI,gBAAgB,IAAI,UAAU,CA4EjC;IAMK,SAAS,CAAC,mBAAmB,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IA+GxE,OAAO,IAAI,IAAI;CAkChB;AAMD,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,qBAAqB,GAC7B,cAAc,CAEhB"}
|
|
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _SnapDataSource_instances, _SnapDataSource_messenger, _SnapDataSource_handleSnapBalancesUpdatedBound, _SnapDataSource_handlePermissionStateChangeBound, _SnapDataSource_keyringClientCache, _SnapDataSource_subscribeToEvents, _SnapDataSource_handleSnapBalancesUpdated, _SnapDataSource_isChainSupportedBySnap,
|
|
12
|
+
var _SnapDataSource_instances, _SnapDataSource_messenger, _SnapDataSource_onActiveChainsUpdated, _SnapDataSource_handleSnapBalancesUpdatedBound, _SnapDataSource_handlePermissionStateChangeBound, _SnapDataSource_keyringClientCache, _SnapDataSource_subscribeToEvents, _SnapDataSource_handleSnapBalancesUpdated, _SnapDataSource_isChainSupportedBySnap, _SnapDataSource_getRunnableSnaps, _SnapDataSource_getSnapPermissions, _SnapDataSource_discoverKeyringSnaps, _SnapDataSource_getKeyringClient;
|
|
13
13
|
import { KeyringClient } from "@metamask/keyring-snap-client";
|
|
14
14
|
import { HandlerType, SnapCaveatType } from "@metamask/snaps-utils";
|
|
15
15
|
import { AbstractDataSource } from "./AbstractDataSource.mjs";
|
|
@@ -69,6 +69,7 @@ const defaultSnapState = {
|
|
|
69
69
|
* ```typescript
|
|
70
70
|
* const snapDataSource = new SnapDataSource({
|
|
71
71
|
* messenger,
|
|
72
|
+
* onActiveChainsUpdated: (chains) => { /* ... *\/ },
|
|
72
73
|
* });
|
|
73
74
|
*
|
|
74
75
|
* // Fetch will automatically route to the correct snap
|
|
@@ -86,16 +87,17 @@ export class SnapDataSource extends AbstractDataSource {
|
|
|
86
87
|
});
|
|
87
88
|
_SnapDataSource_instances.add(this);
|
|
88
89
|
_SnapDataSource_messenger.set(this, void 0);
|
|
90
|
+
_SnapDataSource_onActiveChainsUpdated.set(this, void 0);
|
|
89
91
|
/** Bound handler for snap keyring balance updates, stored for cleanup */
|
|
90
92
|
_SnapDataSource_handleSnapBalancesUpdatedBound.set(this, void 0);
|
|
91
93
|
_SnapDataSource_handlePermissionStateChangeBound.set(this, void 0);
|
|
92
94
|
/** Cache of KeyringClient instances per snap ID to avoid re-instantiation */
|
|
93
95
|
_SnapDataSource_keyringClientCache.set(this, new Map());
|
|
94
96
|
__classPrivateFieldSet(this, _SnapDataSource_messenger, options.messenger, "f");
|
|
97
|
+
__classPrivateFieldSet(this, _SnapDataSource_onActiveChainsUpdated, options.onActiveChainsUpdated, "f");
|
|
95
98
|
// Bind handlers for cleanup in destroy()
|
|
96
99
|
__classPrivateFieldSet(this, _SnapDataSource_handleSnapBalancesUpdatedBound, __classPrivateFieldGet(this, _SnapDataSource_instances, "m", _SnapDataSource_handleSnapBalancesUpdated).bind(this), "f");
|
|
97
100
|
__classPrivateFieldSet(this, _SnapDataSource_handlePermissionStateChangeBound, __classPrivateFieldGet(this, _SnapDataSource_instances, "m", _SnapDataSource_discoverKeyringSnaps).bind(this), "f");
|
|
98
|
-
__classPrivateFieldGet(this, _SnapDataSource_instances, "m", _SnapDataSource_registerActionHandlers).call(this);
|
|
99
101
|
__classPrivateFieldGet(this, _SnapDataSource_instances, "m", _SnapDataSource_subscribeToEvents).call(this);
|
|
100
102
|
// Discover keyring-capable snaps and populate activeChains dynamically
|
|
101
103
|
__classPrivateFieldGet(this, _SnapDataSource_instances, "m", _SnapDataSource_discoverKeyringSnaps).call(this);
|
|
@@ -107,15 +109,18 @@ export class SnapDataSource extends AbstractDataSource {
|
|
|
107
109
|
var _a;
|
|
108
110
|
// Guard against undefined request
|
|
109
111
|
// Note: chainIds filtering is done by middleware/subscribe before calling fetch
|
|
110
|
-
if (!request?.
|
|
112
|
+
if (!request?.chainIds?.length) {
|
|
111
113
|
return {};
|
|
112
114
|
}
|
|
115
|
+
if (!request?.accountsWithSupportedChains?.length) {
|
|
116
|
+
return { assetsBalance: {}, assetsMetadata: {} };
|
|
117
|
+
}
|
|
113
118
|
const results = {
|
|
114
119
|
assetsBalance: {},
|
|
115
120
|
assetsMetadata: {},
|
|
116
121
|
};
|
|
117
122
|
// Fetch balances for each account using its snap ID from metadata
|
|
118
|
-
for (const account of request.
|
|
123
|
+
for (const { account } of request.accountsWithSupportedChains) {
|
|
119
124
|
// Skip accounts without snap metadata (non-snap accounts)
|
|
120
125
|
const snapId = account.metadata.snap?.id;
|
|
121
126
|
if (!snapId) {
|
|
@@ -258,7 +263,7 @@ export class SnapDataSource extends AbstractDataSource {
|
|
|
258
263
|
})
|
|
259
264
|
.then(async (fetchResponse) => {
|
|
260
265
|
if (Object.keys(fetchResponse.assetsBalance ?? {}).length > 0) {
|
|
261
|
-
await
|
|
266
|
+
await existing.onAssetsUpdate(fetchResponse);
|
|
262
267
|
}
|
|
263
268
|
return fetchResponse;
|
|
264
269
|
})
|
|
@@ -277,6 +282,7 @@ export class SnapDataSource extends AbstractDataSource {
|
|
|
277
282
|
// No timer to clear - we use event-based updates
|
|
278
283
|
},
|
|
279
284
|
chains: supportedChains,
|
|
285
|
+
onAssetsUpdate: subscriptionRequest.onAssetsUpdate,
|
|
280
286
|
});
|
|
281
287
|
// Initial fetch to get current balances
|
|
282
288
|
try {
|
|
@@ -284,8 +290,10 @@ export class SnapDataSource extends AbstractDataSource {
|
|
|
284
290
|
...request,
|
|
285
291
|
chainIds: supportedChains,
|
|
286
292
|
});
|
|
287
|
-
|
|
288
|
-
|
|
293
|
+
const subscription = this.activeSubscriptions.get(subscriptionId);
|
|
294
|
+
if (Object.keys(fetchResponse.assetsBalance ?? {}).length > 0 &&
|
|
295
|
+
subscription) {
|
|
296
|
+
await subscription.onAssetsUpdate(fetchResponse);
|
|
289
297
|
}
|
|
290
298
|
}
|
|
291
299
|
catch (error) {
|
|
@@ -322,14 +330,12 @@ export class SnapDataSource extends AbstractDataSource {
|
|
|
322
330
|
__classPrivateFieldGet(this, _SnapDataSource_keyringClientCache, "f").clear();
|
|
323
331
|
}
|
|
324
332
|
}
|
|
325
|
-
_SnapDataSource_messenger = new WeakMap(), _SnapDataSource_handleSnapBalancesUpdatedBound = new WeakMap(), _SnapDataSource_handlePermissionStateChangeBound = new WeakMap(), _SnapDataSource_keyringClientCache = new WeakMap(), _SnapDataSource_instances = new WeakSet(), _SnapDataSource_subscribeToEvents = function _SnapDataSource_subscribeToEvents() {
|
|
326
|
-
// Subscribe to snap keyring events
|
|
327
|
-
//
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
// Re-runs snap discovery when permissions change.
|
|
332
|
-
__classPrivateFieldGet(this, _SnapDataSource_messenger, "f").subscribe('PermissionController:stateChange', __classPrivateFieldGet(this, _SnapDataSource_handlePermissionStateChangeBound, "f"));
|
|
333
|
+
_SnapDataSource_messenger = new WeakMap(), _SnapDataSource_onActiveChainsUpdated = new WeakMap(), _SnapDataSource_handleSnapBalancesUpdatedBound = new WeakMap(), _SnapDataSource_handlePermissionStateChangeBound = new WeakMap(), _SnapDataSource_keyringClientCache = new WeakMap(), _SnapDataSource_instances = new WeakSet(), _SnapDataSource_subscribeToEvents = function _SnapDataSource_subscribeToEvents() {
|
|
334
|
+
// Subscribe to snap keyring events and permission changes (not in AssetsControllerEvents).
|
|
335
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
336
|
+
const messenger = __classPrivateFieldGet(this, _SnapDataSource_messenger, "f");
|
|
337
|
+
messenger.subscribe('AccountsController:accountBalancesUpdated', __classPrivateFieldGet(this, _SnapDataSource_handleSnapBalancesUpdatedBound, "f"));
|
|
338
|
+
messenger.subscribe('PermissionController:stateChange', __classPrivateFieldGet(this, _SnapDataSource_handlePermissionStateChangeBound, "f"));
|
|
333
339
|
}, _SnapDataSource_handleSnapBalancesUpdated = function _SnapDataSource_handleSnapBalancesUpdated(payload) {
|
|
334
340
|
// Transform the snap keyring payload to DataResponse format
|
|
335
341
|
let assetsBalance;
|
|
@@ -352,22 +358,15 @@ _SnapDataSource_messenger = new WeakMap(), _SnapDataSource_handleSnapBalancesUpd
|
|
|
352
358
|
// Only report if we have snap-related updates
|
|
353
359
|
if (assetsBalance) {
|
|
354
360
|
const response = { assetsBalance };
|
|
355
|
-
|
|
356
|
-
.
|
|
357
|
-
|
|
361
|
+
for (const subscription of this.activeSubscriptions.values()) {
|
|
362
|
+
subscription.onAssetsUpdate(response)?.catch(console.error);
|
|
363
|
+
}
|
|
358
364
|
}
|
|
359
365
|
}, _SnapDataSource_isChainSupportedBySnap = function _SnapDataSource_isChainSupportedBySnap(chainId) {
|
|
360
366
|
return this.state.activeChains.includes(chainId);
|
|
361
|
-
}, _SnapDataSource_registerActionHandlers = function _SnapDataSource_registerActionHandlers() {
|
|
362
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
363
|
-
const messenger = __classPrivateFieldGet(this, _SnapDataSource_messenger, "f");
|
|
364
|
-
messenger.registerActionHandler('SnapDataSource:getAssetsMiddleware', () => this.assetsMiddleware);
|
|
365
|
-
messenger.registerActionHandler('SnapDataSource:getActiveChains', async () => this.getActiveChains());
|
|
366
|
-
messenger.registerActionHandler('SnapDataSource:fetch', async (request) => this.fetch(request));
|
|
367
|
-
messenger.registerActionHandler('SnapDataSource:subscribe', async (request) => this.subscribe(request));
|
|
368
|
-
messenger.registerActionHandler('SnapDataSource:unsubscribe', async (subscriptionId) => this.unsubscribe(subscriptionId));
|
|
369
367
|
}, _SnapDataSource_getRunnableSnaps = function _SnapDataSource_getRunnableSnaps() {
|
|
370
368
|
try {
|
|
369
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
371
370
|
return __classPrivateFieldGet(this, _SnapDataSource_messenger, "f").call('SnapController:getRunnableSnaps');
|
|
372
371
|
}
|
|
373
372
|
catch (error) {
|
|
@@ -376,6 +375,7 @@ _SnapDataSource_messenger = new WeakMap(), _SnapDataSource_handleSnapBalancesUpd
|
|
|
376
375
|
}
|
|
377
376
|
}, _SnapDataSource_getSnapPermissions = function _SnapDataSource_getSnapPermissions(snapId) {
|
|
378
377
|
try {
|
|
378
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
379
379
|
return __classPrivateFieldGet(this, _SnapDataSource_messenger, "f").call('PermissionController:getPermissions', snapId);
|
|
380
380
|
}
|
|
381
381
|
catch (error) {
|
|
@@ -412,7 +412,7 @@ _SnapDataSource_messenger = new WeakMap(), _SnapDataSource_handleSnapBalancesUpd
|
|
|
412
412
|
// Notify if chains changed
|
|
413
413
|
try {
|
|
414
414
|
this.updateActiveChains(supportedChains, (updatedChains) => {
|
|
415
|
-
__classPrivateFieldGet(this,
|
|
415
|
+
__classPrivateFieldGet(this, _SnapDataSource_onActiveChainsUpdated, "f").call(this, updatedChains);
|
|
416
416
|
});
|
|
417
417
|
}
|
|
418
418
|
catch {
|
|
@@ -424,7 +424,7 @@ _SnapDataSource_messenger = new WeakMap(), _SnapDataSource_handleSnapBalancesUpd
|
|
|
424
424
|
this.state.chainToSnap = {};
|
|
425
425
|
try {
|
|
426
426
|
this.updateActiveChains([], (updatedChains) => {
|
|
427
|
-
__classPrivateFieldGet(this,
|
|
427
|
+
__classPrivateFieldGet(this, _SnapDataSource_onActiveChainsUpdated, "f").call(this, updatedChains);
|
|
428
428
|
});
|
|
429
429
|
}
|
|
430
430
|
catch {
|
|
@@ -437,12 +437,12 @@ _SnapDataSource_messenger = new WeakMap(), _SnapDataSource_handleSnapBalancesUpd
|
|
|
437
437
|
return cachedClient;
|
|
438
438
|
}
|
|
439
439
|
const client = new KeyringClient({
|
|
440
|
-
send: async (request) =>
|
|
440
|
+
send: async (request) => await __classPrivateFieldGet(this, _SnapDataSource_messenger, "f").call('SnapController:handleRequest', {
|
|
441
441
|
snapId: snapId,
|
|
442
442
|
origin: 'metamask',
|
|
443
443
|
handler: HandlerType.OnKeyringRequest,
|
|
444
444
|
request,
|
|
445
|
-
})
|
|
445
|
+
}),
|
|
446
446
|
});
|
|
447
447
|
__classPrivateFieldGet(this, _SnapDataSource_keyringClientCache, "f").set(snapId, client);
|
|
448
448
|
return client;
|