@metamask/snaps-controllers 19.0.0 → 19.0.1

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.
Files changed (74) hide show
  1. package/CHANGELOG.md +13 -1
  2. package/dist/cronjob/CronjobController-method-action-types.cjs +1 -1
  3. package/dist/cronjob/CronjobController-method-action-types.cjs.map +1 -1
  4. package/dist/cronjob/CronjobController-method-action-types.d.cts +1 -1
  5. package/dist/cronjob/CronjobController-method-action-types.d.mts +1 -1
  6. package/dist/cronjob/CronjobController-method-action-types.mjs +1 -1
  7. package/dist/cronjob/CronjobController-method-action-types.mjs.map +1 -1
  8. package/dist/cronjob/CronjobController.cjs.map +1 -1
  9. package/dist/cronjob/CronjobController.d.cts +2 -2
  10. package/dist/cronjob/CronjobController.d.cts.map +1 -1
  11. package/dist/cronjob/CronjobController.d.mts +2 -2
  12. package/dist/cronjob/CronjobController.d.mts.map +1 -1
  13. package/dist/cronjob/CronjobController.mjs.map +1 -1
  14. package/dist/insights/SnapInsightsController.cjs.map +1 -1
  15. package/dist/insights/SnapInsightsController.d.cts +2 -2
  16. package/dist/insights/SnapInsightsController.d.cts.map +1 -1
  17. package/dist/insights/SnapInsightsController.d.mts +2 -2
  18. package/dist/insights/SnapInsightsController.d.mts.map +1 -1
  19. package/dist/insights/SnapInsightsController.mjs.map +1 -1
  20. package/dist/interface/SnapInterfaceController-method-action-types.cjs +1 -1
  21. package/dist/interface/SnapInterfaceController-method-action-types.cjs.map +1 -1
  22. package/dist/interface/SnapInterfaceController-method-action-types.d.cts +1 -1
  23. package/dist/interface/SnapInterfaceController-method-action-types.d.mts +1 -1
  24. package/dist/interface/SnapInterfaceController-method-action-types.mjs +1 -1
  25. package/dist/interface/SnapInterfaceController-method-action-types.mjs.map +1 -1
  26. package/dist/interface/SnapInterfaceController.cjs.map +1 -1
  27. package/dist/interface/SnapInterfaceController.d.cts +2 -2
  28. package/dist/interface/SnapInterfaceController.d.cts.map +1 -1
  29. package/dist/interface/SnapInterfaceController.d.mts +2 -2
  30. package/dist/interface/SnapInterfaceController.d.mts.map +1 -1
  31. package/dist/interface/SnapInterfaceController.mjs.map +1 -1
  32. package/dist/multichain/MultichainRoutingService-method-action-types.cjs +1 -1
  33. package/dist/multichain/MultichainRoutingService-method-action-types.cjs.map +1 -1
  34. package/dist/multichain/MultichainRoutingService-method-action-types.d.cts +1 -1
  35. package/dist/multichain/MultichainRoutingService-method-action-types.d.mts +1 -1
  36. package/dist/multichain/MultichainRoutingService-method-action-types.mjs +1 -1
  37. package/dist/multichain/MultichainRoutingService-method-action-types.mjs.map +1 -1
  38. package/dist/multichain/MultichainRoutingService.cjs.map +1 -1
  39. package/dist/multichain/MultichainRoutingService.d.cts +2 -2
  40. package/dist/multichain/MultichainRoutingService.d.cts.map +1 -1
  41. package/dist/multichain/MultichainRoutingService.d.mts +2 -2
  42. package/dist/multichain/MultichainRoutingService.d.mts.map +1 -1
  43. package/dist/multichain/MultichainRoutingService.mjs.map +1 -1
  44. package/dist/services/ExecutionService-method-action-types.cjs +1 -1
  45. package/dist/services/ExecutionService-method-action-types.cjs.map +1 -1
  46. package/dist/services/ExecutionService-method-action-types.d.cts +1 -1
  47. package/dist/services/ExecutionService-method-action-types.d.mts +1 -1
  48. package/dist/services/ExecutionService-method-action-types.mjs +1 -1
  49. package/dist/services/ExecutionService-method-action-types.mjs.map +1 -1
  50. package/dist/snaps/SnapController-method-action-types.cjs +1 -1
  51. package/dist/snaps/SnapController-method-action-types.cjs.map +1 -1
  52. package/dist/snaps/SnapController-method-action-types.d.cts +1 -1
  53. package/dist/snaps/SnapController-method-action-types.d.mts +1 -1
  54. package/dist/snaps/SnapController-method-action-types.mjs +1 -1
  55. package/dist/snaps/SnapController-method-action-types.mjs.map +1 -1
  56. package/dist/snaps/SnapController.cjs.map +1 -1
  57. package/dist/snaps/SnapController.d.cts +2 -2
  58. package/dist/snaps/SnapController.d.cts.map +1 -1
  59. package/dist/snaps/SnapController.d.mts +2 -2
  60. package/dist/snaps/SnapController.d.mts.map +1 -1
  61. package/dist/snaps/SnapController.mjs.map +1 -1
  62. package/dist/snaps/registry/SnapRegistryController-method-action-types.cjs +1 -1
  63. package/dist/snaps/registry/SnapRegistryController-method-action-types.cjs.map +1 -1
  64. package/dist/snaps/registry/SnapRegistryController-method-action-types.d.cts +1 -1
  65. package/dist/snaps/registry/SnapRegistryController-method-action-types.d.mts +1 -1
  66. package/dist/snaps/registry/SnapRegistryController-method-action-types.mjs +1 -1
  67. package/dist/snaps/registry/SnapRegistryController-method-action-types.mjs.map +1 -1
  68. package/dist/websocket/WebSocketService-method-action-types.cjs +1 -1
  69. package/dist/websocket/WebSocketService-method-action-types.cjs.map +1 -1
  70. package/dist/websocket/WebSocketService-method-action-types.d.cts +1 -1
  71. package/dist/websocket/WebSocketService-method-action-types.d.mts +1 -1
  72. package/dist/websocket/WebSocketService-method-action-types.mjs +1 -1
  73. package/dist/websocket/WebSocketService-method-action-types.mjs.map +1 -1
  74. package/package.json +10 -10
@@ -1 +1 @@
1
- {"version":3,"file":"MultichainRoutingService.cjs","sourceRoot":"","sources":["../../src/multichain/MultichainRoutingService.ts"],"names":[],"mappings":";;;AAEA,qDAAiD;AACjD,mEAGqC;AAGrC,uDAAoD;AAMpD,2CAMyB;AACzB,mCAAgC;AAsDhC,MAAM,IAAI,GAAG,0BAA0B,CAAC;AAExC,MAAM,yBAAyB,GAAG;IAChC,eAAe;IACf,qBAAqB;IACrB,sBAAsB;IACtB,kBAAkB;CACV,CAAC;AAEX,MAAa,wBAAwB;IACnC,IAAI,GAAgB,IAAI,CAAC;IAEzB,KAAK,GAAG,IAAI,CAAC;IAEJ,UAAU,CAAoC;IAE9C,gBAAgB,CAA0B;IAEnD,YAAY,EAAE,SAAS,EAAE,eAAe,EAAgC;QACtE,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;QAC5B,IAAI,CAAC,gBAAgB,GAAG,eAAe,CAAC;QAExC,IAAI,CAAC,UAAU,CAAC,4B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,KAAK,CAAC;aACxD,MAAM,CAAC,CAAC,OAAwB,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IAC1E,CAAC;IAED;;;;;;OAMG;IACH,mBAAmB,CAAC,KAAkB;QACpC,MAAM,cAAc,GAAG,IAAI,CAAC,6BAA6B,CAAC,KAAK,CAAC,CAAC,OAAO,CACtE,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAC7B,CAAC;QAEF,MAAM,eAAe,GAAG,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC,OAAO,CAC3D,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CACvB,CAAC;QAEF,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,cAAc,EAAE,GAAG,eAAe,CAAC,CAAC,CAAC,CAAC;IACtE,CAAC;IAED;;;;;OAKG;IACH,oBAAoB,CAAC,KAAkB;QACrC,OAAO,IAAI,CAAC,6BAA6B,CAAC,KAAK,CAAC,CAAC,GAAG,CAClD,CAAC,OAAO,EAAE,EAAE,CAAC,GAAG,KAAK,IAAI,OAAO,CAAC,OAAO,EAAE,CAC3C,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACH,gBAAgB,CAAC,KAAkB;QACjC,MAAM,cAAc,GAAG,IAAI,CAAC,UAAU;aACnC,IAAI,CAAC,2CAA2C,EAAE,KAAK,CAAC;aACxD,IAAI,CAAC,CAAC,OAAwB,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QACtE,+GAA+G;QAC/G,OAAO,cAAc,IAAI,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;IACpE,CAAC;CACF;AA9TD,4DA8TC","sourcesContent":["import type { Messenger } from '@metamask/messenger';\nimport type { GetPermissions } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport {\n getProtocolCaveatScopes,\n SnapEndowments,\n} from '@metamask/snaps-rpc-methods';\nimport type { Json, JsonRpcRequest, SnapId } from '@metamask/snaps-sdk';\nimport type { InternalAccount } from '@metamask/snaps-utils';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport type {\n CaipAccountId,\n CaipChainId,\n JsonRpcParams,\n} from '@metamask/utils';\nimport {\n assert,\n hasProperty,\n isObject,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\nimport { nanoid } from 'nanoid';\n\nimport type { MultichainRoutingServiceMethodActions } from './MultichainRoutingService-method-action-types';\nimport type {\n SnapControllerGetRunnableSnapsAction,\n SnapControllerHandleRequestAction,\n} from '../snaps';\n\ntype SnapKeyring = {\n submitRequest: (request: {\n origin: string;\n account: string;\n method: string;\n params?: Json[] | Record<string, Json>;\n scope: CaipChainId;\n }) => Promise<Json>;\n};\n\n// Expecting a bound function that calls KeyringController.withKeyring selecting the Snap keyring\nexport type WithSnapKeyringFunction = <ReturnType>(\n operation: ({ keyring }: { keyring: SnapKeyring }) => Promise<ReturnType>,\n) => Promise<ReturnType>;\n\nexport type AccountsControllerListMultichainAccountsAction = {\n type: `AccountsController:listMultichainAccounts`;\n handler: (chainId?: CaipChainId) => InternalAccount[];\n};\n\nexport type MultichainRoutingServiceActions =\n MultichainRoutingServiceMethodActions;\n\ntype AllowedActions =\n | SnapControllerGetRunnableSnapsAction\n | SnapControllerHandleRequestAction\n | GetPermissions\n | AccountsControllerListMultichainAccountsAction;\n\nexport type MultichainRoutingServiceEvents = never;\n\nexport type MultichainRoutingServiceMessenger = Messenger<\n typeof name,\n MultichainRoutingServiceActions | AllowedActions\n>;\n\nexport type MultichainRoutingServiceArgs = {\n messenger: MultichainRoutingServiceMessenger;\n withSnapKeyring: WithSnapKeyringFunction;\n};\n\ntype ProtocolSnap = {\n snapId: SnapId;\n methods: string[];\n};\n\nconst name = 'MultichainRoutingService';\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'handleRequest',\n 'getSupportedMethods',\n 'getSupportedAccounts',\n 'isSupportedScope',\n] as const;\n\nexport class MultichainRoutingService {\n name: typeof name = name;\n\n state = null;\n\n readonly #messenger: MultichainRoutingServiceMessenger;\n\n readonly #withSnapKeyring: WithSnapKeyringFunction;\n\n constructor({ messenger, withSnapKeyring }: MultichainRoutingServiceArgs) {\n this.#messenger = messenger;\n this.#withSnapKeyring = withSnapKeyring;\n\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Attempts to resolve the account address to use for a given request by inspecting the request itself.\n *\n * The request is sent to an account Snap that will attempt this resolution.\n *\n * We manually construct the request instead of using the SnapKeyring, as the keyring may not be available.\n *\n * @param snapId - The ID of the Snap to send the request to.\n * @param scope - The CAIP-2 scope for the request.\n * @param request - The JSON-RPC request.\n * @returns The resolved address if found, otherwise null.\n * @throws If the invocation of the SnapKeyring fails.\n */\n async #resolveRequestAddress(\n snapId: SnapId,\n scope: CaipChainId,\n request: JsonRpcRequest,\n ) {\n try {\n const result = await this.#messenger.call(\n 'SnapController:handleRequest',\n {\n snapId,\n origin: 'metamask',\n request: {\n method: 'keyring_resolveAccountAddress',\n params: {\n request,\n scope,\n },\n },\n handler: HandlerType.OnKeyringRequest,\n },\n );\n\n assert(result === null || isObject(result));\n\n const address = result?.address as CaipAccountId;\n return address ? parseCaipAccountId(address).address : null;\n } catch {\n throw rpcErrors.internal();\n }\n }\n\n /**\n * Get the account ID of the account that should service the RPC request via an account Snap.\n *\n * This function checks whether any accounts exist that can service a given request by\n * using a combination of the resolveAccountAddress functionality and the connected accounts.\n *\n * If an account is expected to service this request but none is found, the function will throw.\n *\n * @param connectedAddresses - The CAIP-10 addresses connected to the\n * requesting origin for the requested scope.\n * @param scope - The CAIP-2 scope for the request.\n * @param request - The JSON-RPC request.\n * @returns An account ID if found, otherwise null.\n * @throws If no account is found, but the accounts exist that could service the request.\n */\n async #getSnapAccountId(\n connectedAddresses: CaipAccountId[],\n scope: CaipChainId,\n request: JsonRpcRequest,\n ) {\n const accounts = this.#messenger\n .call('AccountsController:listMultichainAccounts', scope)\n .filter(\n (\n account: InternalAccount,\n ): account is InternalAccount & {\n metadata: Required<InternalAccount['metadata']>;\n } =>\n Boolean(account.metadata.snap?.enabled) &&\n account.methods.includes(request.method),\n );\n\n // If no accounts can service the request, return null.\n if (accounts.length === 0) {\n return null;\n }\n\n const parsedConnectedAddresses = connectedAddresses.map(\n (connectedAddress) => parseCaipAccountId(connectedAddress).address,\n );\n\n const connectedAccounts = accounts.filter((account) =>\n parsedConnectedAddresses.includes(account.address),\n );\n\n if (connectedAccounts.length === 0) {\n throw rpcErrors.invalidParams({\n message: 'No available account found for request.',\n });\n }\n\n const resolutionSnapId = connectedAccounts[0].metadata.snap.id;\n\n // Attempt to resolve the address that should be used for signing.\n const address = await this.#resolveRequestAddress(\n resolutionSnapId,\n scope,\n request,\n );\n\n // If we have a resolved address, try to find the selected account based on that\n // otherwise, default to one of the connected accounts.\n // TODO: Eventually let the user choose if we have more than one option for the account.\n const selectedAccount = address\n ? connectedAccounts.find((account) => account.address === address)\n : connectedAccounts[0];\n\n if (!selectedAccount) {\n throw rpcErrors.invalidParams({\n message: 'No available account found for request.',\n });\n }\n\n return selectedAccount.id;\n }\n\n /**\n * Get all protocol Snaps that can service a given CAIP-2 scope.\n *\n * Protocol Snaps are deemed fit to service a scope if they are runnable\n * and have the proper permissions set for the scope.\n *\n * @param scope - A CAIP-2 scope.\n * @returns A list of all the protocol Snaps available and their RPC methods.\n */\n #getProtocolSnaps(scope: CaipChainId) {\n const filteredSnaps = this.#messenger.call(\n 'SnapController:getRunnableSnaps',\n );\n\n return filteredSnaps.reduce<ProtocolSnap[]>((accumulator, snap) => {\n const permissions = this.#messenger.call(\n 'PermissionController:getPermissions',\n snap.id,\n );\n\n if (permissions && hasProperty(permissions, SnapEndowments.Protocol)) {\n const permission = permissions[SnapEndowments.Protocol];\n const scopes = getProtocolCaveatScopes(permission);\n if (scopes && hasProperty(scopes, scope)) {\n accumulator.push({\n snapId: snap.id,\n methods: scopes[scope].methods,\n });\n }\n }\n\n return accumulator;\n }, []);\n }\n\n /**\n * Handle an incoming JSON-RPC request tied to a specific scope by routing\n * to either a protocol Snap or an account Snap.\n *\n * Note: Addresses are considered case-sensitive by the MultichainRoutingService as\n * not all non-EVM chains are case-insensitive.\n *\n * @param options - An options bag.\n * @param options.connectedAddresses - Addresses currently connected to the\n * origin for the requested scope.\n * @param options.origin - The origin of the RPC request.\n * @param options.request - The JSON-RPC request.\n * @param options.scope - The CAIP-2 scope for the request.\n * @returns The response from the chosen Snap.\n * @throws If no handler was found.\n */\n async handleRequest({\n connectedAddresses,\n origin,\n scope,\n request: rawRequest,\n }: {\n connectedAddresses: CaipAccountId[];\n origin: string;\n scope: CaipChainId;\n request: JsonRpcRequest;\n }): Promise<Json> {\n // Explicitly block EVM scopes, just in case.\n assert(\n !scope.startsWith(KnownCaipNamespace.Eip155) &&\n !scope.startsWith('wallet:eip155'),\n );\n\n // Re-create the request to simplify and remove additional properties that may be present in MM middleware.\n const request = {\n jsonrpc: '2.0' as const,\n id: rawRequest.id ?? nanoid(),\n method: rawRequest.method,\n ...(rawRequest.params ? { params: rawRequest.params } : {}),\n };\n\n const { method, params } = request;\n\n // If the RPC request can be serviced by an account Snap, route it there.\n const accountId = await this.#getSnapAccountId(\n connectedAddresses,\n scope,\n request,\n );\n\n if (accountId) {\n return this.#withSnapKeyring(async ({ keyring }) =>\n keyring.submitRequest({\n origin,\n account: accountId,\n scope,\n method,\n params: params as JsonRpcParams,\n }),\n );\n }\n\n // If the RPC request cannot be serviced by an account Snap,\n // but has a protocol Snap available, route it there.\n const protocolSnaps = this.#getProtocolSnaps(scope);\n const protocolSnap = protocolSnaps.find((snap) =>\n snap.methods.includes(method),\n );\n\n if (protocolSnap) {\n return this.#messenger.call('SnapController:handleRequest', {\n snapId: protocolSnap.snapId,\n origin,\n request: {\n method: '',\n params: {\n request,\n scope,\n },\n },\n handler: HandlerType.OnProtocolRequest,\n }) as Promise<Json>;\n }\n\n // If no compatible account or protocol Snaps were found, throw.\n throw rpcErrors.methodNotFound();\n }\n\n /**\n * Get a list of metadata for supported accounts for a given scope from the client.\n *\n * @param scope - The CAIP-2 scope.\n * @returns A list of metadata for the supported accounts.\n */\n #getSupportedAccountsMetadata(scope: CaipChainId): InternalAccount[] {\n return this.#messenger\n .call('AccountsController:listMultichainAccounts', scope)\n .filter((account: InternalAccount) => account.metadata.snap?.enabled);\n }\n\n /**\n * Get a list of supported methods for a given scope.\n * This combines both protocol and account Snaps supported methods.\n *\n * @param scope - The CAIP-2 scope.\n * @returns A list of supported methods.\n */\n getSupportedMethods(scope: CaipChainId): string[] {\n const accountMethods = this.#getSupportedAccountsMetadata(scope).flatMap(\n (account) => account.methods,\n );\n\n const protocolMethods = this.#getProtocolSnaps(scope).flatMap(\n (snap) => snap.methods,\n );\n\n return Array.from(new Set([...accountMethods, ...protocolMethods]));\n }\n\n /**\n * Get a list of supported accounts for a given scope.\n *\n * @param scope - The CAIP-2 scope.\n * @returns A list of CAIP-10 addresses.\n */\n getSupportedAccounts(scope: CaipChainId): string[] {\n return this.#getSupportedAccountsMetadata(scope).map(\n (account) => `${scope}:${account.address}`,\n );\n }\n\n /**\n * Determine whether a given CAIP-2 scope is supported by the router.\n *\n * @param scope - The CAIP-2 scope.\n * @returns True if the router can service the scope, otherwise false.\n */\n isSupportedScope(scope: CaipChainId): boolean {\n const hasAccountSnap = this.#messenger\n .call('AccountsController:listMultichainAccounts', scope)\n .some((account: InternalAccount) => account.metadata.snap?.enabled);\n // We currently assume here that if one Snap exists that service the scope, we can service the scope generally.\n return hasAccountSnap || this.#getProtocolSnaps(scope).length > 0;\n }\n}\n"]}
1
+ {"version":3,"file":"MultichainRoutingService.cjs","sourceRoot":"","sources":["../../src/multichain/MultichainRoutingService.ts"],"names":[],"mappings":";;;AAEA,qDAAiD;AACjD,mEA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AO,EAAE,CAC3C,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACH,gBAAgB,CAAC,KAAkB;QACjC,MAAM,cAAc,GAAG,IAAI,CAAC,UAAU;aACnC,IAAI,CAAC,2CAA2C,EAAE,KAAK,CAAC;aACxD,IAAI,CAAC,CAAC,OAAwB,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QACtE,+GAA+G;QAC/G,OAAO,cAAc,IAAI,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;IACpE,CAAC;CACF;AA9TD,4DA8TC","sourcesContent":["import type { Messenger } from '@metamask/messenger';\nimport type { PermissionControllerGetPermissionsAction } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport {\n getProtocolCaveatScopes,\n SnapEndowments,\n} from '@metamask/snaps-rpc-methods';\nimport type { Json, JsonRpcRequest, SnapId } from '@metamask/snaps-sdk';\nimport type { InternalAccount } from '@metamask/snaps-utils';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport type {\n CaipAccountId,\n CaipChainId,\n JsonRpcParams,\n} from '@metamask/utils';\nimport {\n assert,\n hasProperty,\n isObject,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\nimport { nanoid } from 'nanoid';\n\nimport type { MultichainRoutingServiceMethodActions } from './MultichainRoutingService-method-action-types';\nimport type {\n SnapControllerGetRunnableSnapsAction,\n SnapControllerHandleRequestAction,\n} from '../snaps';\n\ntype SnapKeyring = {\n submitRequest: (request: {\n origin: string;\n account: string;\n method: string;\n params?: Json[] | Record<string, Json>;\n scope: CaipChainId;\n }) => Promise<Json>;\n};\n\n// Expecting a bound function that calls KeyringController.withKeyring selecting the Snap keyring\nexport type WithSnapKeyringFunction = <ReturnType>(\n operation: ({ keyring }: { keyring: SnapKeyring }) => Promise<ReturnType>,\n) => Promise<ReturnType>;\n\nexport type AccountsControllerListMultichainAccountsAction = {\n type: `AccountsController:listMultichainAccounts`;\n handler: (chainId?: CaipChainId) => InternalAccount[];\n};\n\nexport type MultichainRoutingServiceActions =\n MultichainRoutingServiceMethodActions;\n\ntype AllowedActions =\n | SnapControllerGetRunnableSnapsAction\n | SnapControllerHandleRequestAction\n | PermissionControllerGetPermissionsAction\n | AccountsControllerListMultichainAccountsAction;\n\nexport type MultichainRoutingServiceEvents = never;\n\nexport type MultichainRoutingServiceMessenger = Messenger<\n typeof name,\n MultichainRoutingServiceActions | AllowedActions\n>;\n\nexport type MultichainRoutingServiceArgs = {\n messenger: MultichainRoutingServiceMessenger;\n withSnapKeyring: WithSnapKeyringFunction;\n};\n\ntype ProtocolSnap = {\n snapId: SnapId;\n methods: string[];\n};\n\nconst name = 'MultichainRoutingService';\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'handleRequest',\n 'getSupportedMethods',\n 'getSupportedAccounts',\n 'isSupportedScope',\n] as const;\n\nexport class MultichainRoutingService {\n name: typeof name = name;\n\n state = null;\n\n readonly #messenger: MultichainRoutingServiceMessenger;\n\n readonly #withSnapKeyring: WithSnapKeyringFunction;\n\n constructor({ messenger, withSnapKeyring }: MultichainRoutingServiceArgs) {\n this.#messenger = messenger;\n this.#withSnapKeyring = withSnapKeyring;\n\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Attempts to resolve the account address to use for a given request by inspecting the request itself.\n *\n * The request is sent to an account Snap that will attempt this resolution.\n *\n * We manually construct the request instead of using the SnapKeyring, as the keyring may not be available.\n *\n * @param snapId - The ID of the Snap to send the request to.\n * @param scope - The CAIP-2 scope for the request.\n * @param request - The JSON-RPC request.\n * @returns The resolved address if found, otherwise null.\n * @throws If the invocation of the SnapKeyring fails.\n */\n async #resolveRequestAddress(\n snapId: SnapId,\n scope: CaipChainId,\n request: JsonRpcRequest,\n ) {\n try {\n const result = await this.#messenger.call(\n 'SnapController:handleRequest',\n {\n snapId,\n origin: 'metamask',\n request: {\n method: 'keyring_resolveAccountAddress',\n params: {\n request,\n scope,\n },\n },\n handler: HandlerType.OnKeyringRequest,\n },\n );\n\n assert(result === null || isObject(result));\n\n const address = result?.address as CaipAccountId;\n return address ? parseCaipAccountId(address).address : null;\n } catch {\n throw rpcErrors.internal();\n }\n }\n\n /**\n * Get the account ID of the account that should service the RPC request via an account Snap.\n *\n * This function checks whether any accounts exist that can service a given request by\n * using a combination of the resolveAccountAddress functionality and the connected accounts.\n *\n * If an account is expected to service this request but none is found, the function will throw.\n *\n * @param connectedAddresses - The CAIP-10 addresses connected to the\n * requesting origin for the requested scope.\n * @param scope - The CAIP-2 scope for the request.\n * @param request - The JSON-RPC request.\n * @returns An account ID if found, otherwise null.\n * @throws If no account is found, but the accounts exist that could service the request.\n */\n async #getSnapAccountId(\n connectedAddresses: CaipAccountId[],\n scope: CaipChainId,\n request: JsonRpcRequest,\n ) {\n const accounts = this.#messenger\n .call('AccountsController:listMultichainAccounts', scope)\n .filter(\n (\n account: InternalAccount,\n ): account is InternalAccount & {\n metadata: Required<InternalAccount['metadata']>;\n } =>\n Boolean(account.metadata.snap?.enabled) &&\n account.methods.includes(request.method),\n );\n\n // If no accounts can service the request, return null.\n if (accounts.length === 0) {\n return null;\n }\n\n const parsedConnectedAddresses = connectedAddresses.map(\n (connectedAddress) => parseCaipAccountId(connectedAddress).address,\n );\n\n const connectedAccounts = accounts.filter((account) =>\n parsedConnectedAddresses.includes(account.address),\n );\n\n if (connectedAccounts.length === 0) {\n throw rpcErrors.invalidParams({\n message: 'No available account found for request.',\n });\n }\n\n const resolutionSnapId = connectedAccounts[0].metadata.snap.id;\n\n // Attempt to resolve the address that should be used for signing.\n const address = await this.#resolveRequestAddress(\n resolutionSnapId,\n scope,\n request,\n );\n\n // If we have a resolved address, try to find the selected account based on that\n // otherwise, default to one of the connected accounts.\n // TODO: Eventually let the user choose if we have more than one option for the account.\n const selectedAccount = address\n ? connectedAccounts.find((account) => account.address === address)\n : connectedAccounts[0];\n\n if (!selectedAccount) {\n throw rpcErrors.invalidParams({\n message: 'No available account found for request.',\n });\n }\n\n return selectedAccount.id;\n }\n\n /**\n * Get all protocol Snaps that can service a given CAIP-2 scope.\n *\n * Protocol Snaps are deemed fit to service a scope if they are runnable\n * and have the proper permissions set for the scope.\n *\n * @param scope - A CAIP-2 scope.\n * @returns A list of all the protocol Snaps available and their RPC methods.\n */\n #getProtocolSnaps(scope: CaipChainId) {\n const filteredSnaps = this.#messenger.call(\n 'SnapController:getRunnableSnaps',\n );\n\n return filteredSnaps.reduce<ProtocolSnap[]>((accumulator, snap) => {\n const permissions = this.#messenger.call(\n 'PermissionController:getPermissions',\n snap.id,\n );\n\n if (permissions && hasProperty(permissions, SnapEndowments.Protocol)) {\n const permission = permissions[SnapEndowments.Protocol];\n const scopes = getProtocolCaveatScopes(permission);\n if (scopes && hasProperty(scopes, scope)) {\n accumulator.push({\n snapId: snap.id,\n methods: scopes[scope].methods,\n });\n }\n }\n\n return accumulator;\n }, []);\n }\n\n /**\n * Handle an incoming JSON-RPC request tied to a specific scope by routing\n * to either a protocol Snap or an account Snap.\n *\n * Note: Addresses are considered case-sensitive by the MultichainRoutingService as\n * not all non-EVM chains are case-insensitive.\n *\n * @param options - An options bag.\n * @param options.connectedAddresses - Addresses currently connected to the\n * origin for the requested scope.\n * @param options.origin - The origin of the RPC request.\n * @param options.request - The JSON-RPC request.\n * @param options.scope - The CAIP-2 scope for the request.\n * @returns The response from the chosen Snap.\n * @throws If no handler was found.\n */\n async handleRequest({\n connectedAddresses,\n origin,\n scope,\n request: rawRequest,\n }: {\n connectedAddresses: CaipAccountId[];\n origin: string;\n scope: CaipChainId;\n request: JsonRpcRequest;\n }): Promise<Json> {\n // Explicitly block EVM scopes, just in case.\n assert(\n !scope.startsWith(KnownCaipNamespace.Eip155) &&\n !scope.startsWith('wallet:eip155'),\n );\n\n // Re-create the request to simplify and remove additional properties that may be present in MM middleware.\n const request = {\n jsonrpc: '2.0' as const,\n id: rawRequest.id ?? nanoid(),\n method: rawRequest.method,\n ...(rawRequest.params ? { params: rawRequest.params } : {}),\n };\n\n const { method, params } = request;\n\n // If the RPC request can be serviced by an account Snap, route it there.\n const accountId = await this.#getSnapAccountId(\n connectedAddresses,\n scope,\n request,\n );\n\n if (accountId) {\n return this.#withSnapKeyring(async ({ keyring }) =>\n keyring.submitRequest({\n origin,\n account: accountId,\n scope,\n method,\n params: params as JsonRpcParams,\n }),\n );\n }\n\n // If the RPC request cannot be serviced by an account Snap,\n // but has a protocol Snap available, route it there.\n const protocolSnaps = this.#getProtocolSnaps(scope);\n const protocolSnap = protocolSnaps.find((snap) =>\n snap.methods.includes(method),\n );\n\n if (protocolSnap) {\n return this.#messenger.call('SnapController:handleRequest', {\n snapId: protocolSnap.snapId,\n origin,\n request: {\n method: '',\n params: {\n request,\n scope,\n },\n },\n handler: HandlerType.OnProtocolRequest,\n }) as Promise<Json>;\n }\n\n // If no compatible account or protocol Snaps were found, throw.\n throw rpcErrors.methodNotFound();\n }\n\n /**\n * Get a list of metadata for supported accounts for a given scope from the client.\n *\n * @param scope - The CAIP-2 scope.\n * @returns A list of metadata for the supported accounts.\n */\n #getSupportedAccountsMetadata(scope: CaipChainId): InternalAccount[] {\n return this.#messenger\n .call('AccountsController:listMultichainAccounts', scope)\n .filter((account: InternalAccount) => account.metadata.snap?.enabled);\n }\n\n /**\n * Get a list of supported methods for a given scope.\n * This combines both protocol and account Snaps supported methods.\n *\n * @param scope - The CAIP-2 scope.\n * @returns A list of supported methods.\n */\n getSupportedMethods(scope: CaipChainId): string[] {\n const accountMethods = this.#getSupportedAccountsMetadata(scope).flatMap(\n (account) => account.methods,\n );\n\n const protocolMethods = this.#getProtocolSnaps(scope).flatMap(\n (snap) => snap.methods,\n );\n\n return Array.from(new Set([...accountMethods, ...protocolMethods]));\n }\n\n /**\n * Get a list of supported accounts for a given scope.\n *\n * @param scope - The CAIP-2 scope.\n * @returns A list of CAIP-10 addresses.\n */\n getSupportedAccounts(scope: CaipChainId): string[] {\n return this.#getSupportedAccountsMetadata(scope).map(\n (account) => `${scope}:${account.address}`,\n );\n }\n\n /**\n * Determine whether a given CAIP-2 scope is supported by the router.\n *\n * @param scope - The CAIP-2 scope.\n * @returns True if the router can service the scope, otherwise false.\n */\n isSupportedScope(scope: CaipChainId): boolean {\n const hasAccountSnap = this.#messenger\n .call('AccountsController:listMultichainAccounts', scope)\n .some((account: InternalAccount) => account.metadata.snap?.enabled);\n // We currently assume here that if one Snap exists that service the scope, we can service the scope generally.\n return hasAccountSnap || this.#getProtocolSnaps(scope).length > 0;\n }\n}\n"]}
@@ -1,5 +1,5 @@
1
1
  import type { Messenger } from "@metamask/messenger";
2
- import type { GetPermissions } from "@metamask/permission-controller";
2
+ import type { PermissionControllerGetPermissionsAction } from "@metamask/permission-controller";
3
3
  import type { Json, JsonRpcRequest } from "@metamask/snaps-sdk";
4
4
  import type { InternalAccount } from "@metamask/snaps-utils";
5
5
  import type { CaipAccountId, CaipChainId } from "@metamask/utils";
@@ -22,7 +22,7 @@ export type AccountsControllerListMultichainAccountsAction = {
22
22
  handler: (chainId?: CaipChainId) => InternalAccount[];
23
23
  };
24
24
  export type MultichainRoutingServiceActions = MultichainRoutingServiceMethodActions;
25
- type AllowedActions = SnapControllerGetRunnableSnapsAction | SnapControllerHandleRequestAction | GetPermissions | AccountsControllerListMultichainAccountsAction;
25
+ type AllowedActions = SnapControllerGetRunnableSnapsAction | SnapControllerHandleRequestAction | PermissionControllerGetPermissionsAction | AccountsControllerListMultichainAccountsAction;
26
26
  export type MultichainRoutingServiceEvents = never;
27
27
  export type MultichainRoutingServiceMessenger = Messenger<typeof name, MultichainRoutingServiceActions | AllowedActions>;
28
28
  export type MultichainRoutingServiceArgs = {
@@ -1 +1 @@
1
- {"version":3,"file":"MultichainRoutingService.d.cts","sourceRoot":"","sources":["../../src/multichain/MultichainRoutingService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;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jD,CAAC;AAEF,MAAM,MAAM,4BAA4B,GAAG;IACzC,SAAS,EAAE,iCAAiC,CAAC;IAC7C,eAAe,EAAE,uBAAuB,CAAC;CAC1C,CAAC;AAOF,QAAA,MAAM,IAAI,6BAA6B,CAAC;AASxC,qBAAa,wBAAwB;;IACnC,IAAI,EAAE,OAAO,IAAI,CAAQ;IAEzB,KAAK,OAAQ;gBAMD,EAAE,SAAS,EAAE,eAAe,EAAE,EAAE,4BAA4B;IAqKxE;;;;;;;;;;;;;;;OAeG;IACG,aAAa,CAAC,EAClB,kBAAkB,EAClB,MAAM,EACN,KAAK,EACL,OAAO,EAAE,UAAU,GACpB,EAAE;QACD,kBAAkB,EAAE,aAAa,EAAE,CAAC;QACpC,MAAM,EAAE,MAAM,CAAC;QACf,KAAK,EAAE,WAAW,CAAC;QACnB,OAAO,EAAE,cAAc,CAAC;KACzB,GAAG,OAAO,CAAC,IAAI,CAAC;IA0EjB;;;;;;OAMG;IACH,mBAAmB,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,EAAE;IAYjD;;;;;OAKG;IACH,oBAAoB,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,EAAE;IAMlD;;;;;OAKG;IACH,gBAAgB,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO;CAO9C"}
1
+ {"version":3,"file":"MultichainRoutingService.d.cts","sourceRoot":"","sources":["../../src/multichain/MultichainRoutingService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,wCAAwC,EAAE,wCAAwC;AAMhG,OAAO,KAAK,EAAE,IAAI,EAAE,cAAc,EAAU,4BAA4B;AACxE,OAAO,KAAK,EAAE,eAAe,EAAE,8BAA8B;AAE7D,OAAO,KAAK,EACV,aAAa,EACb,WAAW,EAEZ,wBAAwB;AAUzB,OAAO,KAAK,EAAE,qCAAqC,EAAE,2DAAuD;AAC5G,OAAO,KAAK,EACV,oCAAoC,EACpC,iCA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B;IAqKxE;;;;;;;;;;;;;;;OAeG;IACG,aAAa,CAAC,EAClB,kBAAkB,EAClB,MAAM,EACN,KAAK,EACL,OAAO,EAAE,UAAU,GACpB,EAAE;QACD,kBAAkB,EAAE,aAAa,EAAE,CAAC;QACpC,MAAM,EAAE,MAAM,CAAC;QACf,KAAK,EAAE,WAAW,CAAC;QACnB,OAAO,EAAE,cAAc,CAAC;KACzB,GAAG,OAAO,CAAC,IAAI,CAAC;IA0EjB;;;;;;OAMG;IACH,mBAAmB,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,EAAE;IAYjD;;;;;OAKG;IACH,oBAAoB,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,EAAE;IAMlD;;;;;OAKG;IACH,gBAAgB,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO;CAO9C"}
@@ -1,5 +1,5 @@
1
1
  import type { Messenger } from "@metamask/messenger";
2
- import type { GetPermissions } from "@metamask/permission-controller";
2
+ import type { PermissionControllerGetPermissionsAction } from "@metamask/permission-controller";
3
3
  import type { Json, JsonRpcRequest } from "@metamask/snaps-sdk";
4
4
  import type { InternalAccount } from "@metamask/snaps-utils";
5
5
  import type { CaipAccountId, CaipChainId } from "@metamask/utils";
@@ -22,7 +22,7 @@ export type AccountsControllerListMultichainAccountsAction = {
22
22
  handler: (chainId?: CaipChainId) => InternalAccount[];
23
23
  };
24
24
  export type MultichainRoutingServiceActions = MultichainRoutingServiceMethodActions;
25
- type AllowedActions = SnapControllerGetRunnableSnapsAction | SnapControllerHandleRequestAction | GetPermissions | AccountsControllerListMultichainAccountsAction;
25
+ type AllowedActions = SnapControllerGetRunnableSnapsAction | SnapControllerHandleRequestAction | PermissionControllerGetPermissionsAction | AccountsControllerListMultichainAccountsAction;
26
26
  export type MultichainRoutingServiceEvents = never;
27
27
  export type MultichainRoutingServiceMessenger = Messenger<typeof name, MultichainRoutingServiceActions | AllowedActions>;
28
28
  export type MultichainRoutingServiceArgs = {
@@ -1 +1 @@
1
- {"version":3,"file":"MultichainRoutingService.d.mts","sourceRoot":"","sources":["../../src/multichain/MultichainRoutingService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,cAAc,EAAE,wCAAwC;AAMtE,OAAO,KAAK,EAAE,IAAI,EAAE,cAAc,EAAU,4BAA4B;AACxE,OAAO,KAAK,EAAE,eAAe,EAAE,8BAA8B;AAE7D,OAAO,KAAK,EACV,aAAa,EACb,WAAW,EAEZ,wBAAwB;AAUzB,OAAO,KAAK,EAAE,qCAAqC,EAAE,2DAAuD;AAC5G,OAAO,KAAK,EACV,oCAAoC,EACpC,iCAAiC,EAClC,2BAAiB;AAElB,KAAK,WAAW,GAAG;IACjB,aAAa,EAAE,CAAC,OAAO,EAAE;QACvB,MAAM,EAAE,MAAM,CAAC;QACf,OAAO,EAAE,MAAM,CAAC;QAChB,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,CAAC,EAAE,IAAI,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QACvC,KAAK,EAAE,WAAW,CAAC;KACpB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACrB,CAAC;AAGF,MAAM,MAAM,uBAAuB,GAAG,CAAC,UAAU,EAC/C,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE;IAAE,OAAO,EAAE,WAAW,CAAA;CAAE,KAAK,OAAO,CAAC,UAAU,CAAC,KACtE,OAAO,CAAC,UAAU,CAAC,CAAC;AAEzB,MAAM,MAAM,8CAA8C,GAAG;IAC3D,IAAI,EAAE,2CAA2C,CAAC;IAClD,OAAO,EAAE,CAAC,OAAO,CAAC,EAAE,WAAW,KAAK,eAAe,EAAE,CAAC;CACvD,CAAC;AAEF,MAAM,MAAM,+BAA+B,GACzC,qCAAqC,CAAC;AAExC,KAAK,cAAc,GACf,oCAAoC,GACpC,iCAAiC,GACjC,cAAc,GACd,8CAA8C,CAAC;AAEnD,MAAM,MAAM,8BAA8B,GAAG,KAAK,CAAC;AAEnD,MAAM,MAAM,iCAAiC,GAAG,SAAS,CACvD,OAAO,IAAI,EACX,+BAA+B,GAAG,cAAc,CACjD,CAAC;AAEF,MAAM,MAAM,4BAA4B,GAAG;IACzC,SAAS,EAAE,iCAAiC,CAAC;IAC7C,eAAe,EAAE,uBAAuB,CAAC;CAC1C,CAAC;AAOF,QAAA,MAAM,IAAI,6BAA6B,CAAC;AASxC,qBAAa,wBAAwB;;IACnC,IAAI,EAAE,OAAO,IAAI,CAAQ;IAEzB,KAAK,OAAQ;gBAMD,EAAE,SAAS,EAAE,eAAe,EAAE,EAAE,4BAA4B;IAqKxE;;;;;;;;;;;;;;;OAeG;IACG,aAAa,CAAC,EAClB,kBAAkB,EAClB,MAAM,EACN,KAAK,EACL,OAAO,EAAE,UAAU,GACpB,EAAE;QACD,kBAAkB,EAAE,aAAa,EAAE,CAAC;QACpC,MAAM,EAAE,MAAM,CAAC;QACf,KAAK,EAAE,WAAW,CAAC;QACnB,OAAO,EAAE,cAAc,CAAC;KACzB,GAAG,OAAO,CAAC,IAAI,CAAC;IA0EjB;;;;;;OAMG;IACH,mBAAmB,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,EAAE;IAYjD;;;;;OAKG;IACH,oBAAoB,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,EAAE;IAMlD;;;;;OAKG;IACH,gBAAgB,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO;CAO9C"}
1
+ {"version":3,"file":"MultichainRoutingService.d.mts","sourceRoot":"","sources":["../../src/multichain/MultichainRoutingService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,wCAAwC,EAAE,wCAAwC;AAMhG,OAAO,KAAK,EAAE,IAAI,EAAE,cAAc,EAAU,4BAA4B;AACxE,OAAO,KAAK,EAAE,eAAe,EAAE,8BAA8B;AAE7D,OAAO,KAAK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ASxC,qBAAa,wBAAwB;;IACnC,IAAI,EAAE,OAAO,IAAI,CAAQ;IAEzB,KAAK,OAAQ;gBAMD,EAAE,SAAS,EAAE,eAAe,EAAE,EAAE,4BAA4B;IAqKxE;;;;;;;;;;;;;;;OAeG;IACG,aAAa,CAAC,EAClB,kBAAkB,EAClB,MAAM,EACN,KAAK,EACL,OAAO,EAAE,UAAU,GACpB,EAAE;QACD,kBAAkB,EAAE,aAAa,EAAE,CAAC;QACpC,MAAM,EAAE,MAAM,CAAC;QACf,KAAK,EAAE,WAAW,CAAC;QACnB,OAAO,EAAE,cAAc,CAAC;KACzB,GAAG,OAAO,CAAC,IAAI,CAAC;IA0EjB;;;;;;OAMG;IACH,mBAAmB,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,EAAE;IAYjD;;;;;OAKG;IACH,oBAAoB,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,EAAE;IAMlD;;;;;OAKG;IACH,gBAAgB,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO;CAO9C"}
@@ -1 +1 @@
1
- {"version":3,"file":"MultichainRoutingService.mjs","sourceRoot":"","sources":["../../src/multichain/MultichainRoutingService.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,6BAA6B;AACjD,OAAO,EACL,uBAAuB,EACvB,cAAc,EACf,oCAAoC;AAGrC,OAAO,EAAE,WAAW,EAAE,8BAA8B;AAMpD,OAAO,EACL,MAAM,EACN,WAAW,EACX,QAAQ,EACR,kBAAkB,EAClB,kBAAkB,EACnB,wBAAwB;AACzB,OAAO,EAAE,MAAM,EAAE,eAAe;AAsDhC,MAAM,IAAI,GAAG,0BAA0B,CAAC;AAExC,MAAM,yBAAyB,GAAG;IAChC,eAAe;IACf,qBAAqB;IACrB,sBAAsB;IACtB,kBAAkB;CACV,CAAC;AAEX,MAAM,OAAO,wBAAwB;IACnC,IAAI,GAAgB,IAAI,CAAC;IAEzB,KAAK,GAAG,IAAI,CAAC;IAEJ,UAAU,C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AAC,MAAM,GAAG,CAAC,CAAC;IACpE,CAAC;CACF","sourcesContent":["import type { Messenger } from '@metamask/messenger';\nimport type { GetPermissions } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport {\n getProtocolCaveatScopes,\n SnapEndowments,\n} from '@metamask/snaps-rpc-methods';\nimport type { Json, JsonRpcRequest, SnapId } from '@metamask/snaps-sdk';\nimport type { InternalAccount } from '@metamask/snaps-utils';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport type {\n CaipAccountId,\n CaipChainId,\n JsonRpcParams,\n} from '@metamask/utils';\nimport {\n assert,\n hasProperty,\n isObject,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\nimport { nanoid } from 'nanoid';\n\nimport type { MultichainRoutingServiceMethodActions } from './MultichainRoutingService-method-action-types';\nimport type {\n SnapControllerGetRunnableSnapsAction,\n SnapControllerHandleRequestAction,\n} from '../snaps';\n\ntype SnapKeyring = {\n submitRequest: (request: {\n origin: string;\n account: string;\n method: string;\n params?: Json[] | Record<string, Json>;\n scope: CaipChainId;\n }) => Promise<Json>;\n};\n\n// Expecting a bound function that calls KeyringController.withKeyring selecting the Snap keyring\nexport type WithSnapKeyringFunction = <ReturnType>(\n operation: ({ keyring }: { keyring: SnapKeyring }) => Promise<ReturnType>,\n) => Promise<ReturnType>;\n\nexport type AccountsControllerListMultichainAccountsAction = {\n type: `AccountsController:listMultichainAccounts`;\n handler: (chainId?: CaipChainId) => InternalAccount[];\n};\n\nexport type MultichainRoutingServiceActions =\n MultichainRoutingServiceMethodActions;\n\ntype AllowedActions =\n | SnapControllerGetRunnableSnapsAction\n | SnapControllerHandleRequestAction\n | GetPermissions\n | AccountsControllerListMultichainAccountsAction;\n\nexport type MultichainRoutingServiceEvents = never;\n\nexport type MultichainRoutingServiceMessenger = Messenger<\n typeof name,\n MultichainRoutingServiceActions | AllowedActions\n>;\n\nexport type MultichainRoutingServiceArgs = {\n messenger: MultichainRoutingServiceMessenger;\n withSnapKeyring: WithSnapKeyringFunction;\n};\n\ntype ProtocolSnap = {\n snapId: SnapId;\n methods: string[];\n};\n\nconst name = 'MultichainRoutingService';\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'handleRequest',\n 'getSupportedMethods',\n 'getSupportedAccounts',\n 'isSupportedScope',\n] as const;\n\nexport class MultichainRoutingService {\n name: typeof name = name;\n\n state = null;\n\n readonly #messenger: MultichainRoutingServiceMessenger;\n\n readonly #withSnapKeyring: WithSnapKeyringFunction;\n\n constructor({ messenger, withSnapKeyring }: MultichainRoutingServiceArgs) {\n this.#messenger = messenger;\n this.#withSnapKeyring = withSnapKeyring;\n\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Attempts to resolve the account address to use for a given request by inspecting the request itself.\n *\n * The request is sent to an account Snap that will attempt this resolution.\n *\n * We manually construct the request instead of using the SnapKeyring, as the keyring may not be available.\n *\n * @param snapId - The ID of the Snap to send the request to.\n * @param scope - The CAIP-2 scope for the request.\n * @param request - The JSON-RPC request.\n * @returns The resolved address if found, otherwise null.\n * @throws If the invocation of the SnapKeyring fails.\n */\n async #resolveRequestAddress(\n snapId: SnapId,\n scope: CaipChainId,\n request: JsonRpcRequest,\n ) {\n try {\n const result = await this.#messenger.call(\n 'SnapController:handleRequest',\n {\n snapId,\n origin: 'metamask',\n request: {\n method: 'keyring_resolveAccountAddress',\n params: {\n request,\n scope,\n },\n },\n handler: HandlerType.OnKeyringRequest,\n },\n );\n\n assert(result === null || isObject(result));\n\n const address = result?.address as CaipAccountId;\n return address ? parseCaipAccountId(address).address : null;\n } catch {\n throw rpcErrors.internal();\n }\n }\n\n /**\n * Get the account ID of the account that should service the RPC request via an account Snap.\n *\n * This function checks whether any accounts exist that can service a given request by\n * using a combination of the resolveAccountAddress functionality and the connected accounts.\n *\n * If an account is expected to service this request but none is found, the function will throw.\n *\n * @param connectedAddresses - The CAIP-10 addresses connected to the\n * requesting origin for the requested scope.\n * @param scope - The CAIP-2 scope for the request.\n * @param request - The JSON-RPC request.\n * @returns An account ID if found, otherwise null.\n * @throws If no account is found, but the accounts exist that could service the request.\n */\n async #getSnapAccountId(\n connectedAddresses: CaipAccountId[],\n scope: CaipChainId,\n request: JsonRpcRequest,\n ) {\n const accounts = this.#messenger\n .call('AccountsController:listMultichainAccounts', scope)\n .filter(\n (\n account: InternalAccount,\n ): account is InternalAccount & {\n metadata: Required<InternalAccount['metadata']>;\n } =>\n Boolean(account.metadata.snap?.enabled) &&\n account.methods.includes(request.method),\n );\n\n // If no accounts can service the request, return null.\n if (accounts.length === 0) {\n return null;\n }\n\n const parsedConnectedAddresses = connectedAddresses.map(\n (connectedAddress) => parseCaipAccountId(connectedAddress).address,\n );\n\n const connectedAccounts = accounts.filter((account) =>\n parsedConnectedAddresses.includes(account.address),\n );\n\n if (connectedAccounts.length === 0) {\n throw rpcErrors.invalidParams({\n message: 'No available account found for request.',\n });\n }\n\n const resolutionSnapId = connectedAccounts[0].metadata.snap.id;\n\n // Attempt to resolve the address that should be used for signing.\n const address = await this.#resolveRequestAddress(\n resolutionSnapId,\n scope,\n request,\n );\n\n // If we have a resolved address, try to find the selected account based on that\n // otherwise, default to one of the connected accounts.\n // TODO: Eventually let the user choose if we have more than one option for the account.\n const selectedAccount = address\n ? connectedAccounts.find((account) => account.address === address)\n : connectedAccounts[0];\n\n if (!selectedAccount) {\n throw rpcErrors.invalidParams({\n message: 'No available account found for request.',\n });\n }\n\n return selectedAccount.id;\n }\n\n /**\n * Get all protocol Snaps that can service a given CAIP-2 scope.\n *\n * Protocol Snaps are deemed fit to service a scope if they are runnable\n * and have the proper permissions set for the scope.\n *\n * @param scope - A CAIP-2 scope.\n * @returns A list of all the protocol Snaps available and their RPC methods.\n */\n #getProtocolSnaps(scope: CaipChainId) {\n const filteredSnaps = this.#messenger.call(\n 'SnapController:getRunnableSnaps',\n );\n\n return filteredSnaps.reduce<ProtocolSnap[]>((accumulator, snap) => {\n const permissions = this.#messenger.call(\n 'PermissionController:getPermissions',\n snap.id,\n );\n\n if (permissions && hasProperty(permissions, SnapEndowments.Protocol)) {\n const permission = permissions[SnapEndowments.Protocol];\n const scopes = getProtocolCaveatScopes(permission);\n if (scopes && hasProperty(scopes, scope)) {\n accumulator.push({\n snapId: snap.id,\n methods: scopes[scope].methods,\n });\n }\n }\n\n return accumulator;\n }, []);\n }\n\n /**\n * Handle an incoming JSON-RPC request tied to a specific scope by routing\n * to either a protocol Snap or an account Snap.\n *\n * Note: Addresses are considered case-sensitive by the MultichainRoutingService as\n * not all non-EVM chains are case-insensitive.\n *\n * @param options - An options bag.\n * @param options.connectedAddresses - Addresses currently connected to the\n * origin for the requested scope.\n * @param options.origin - The origin of the RPC request.\n * @param options.request - The JSON-RPC request.\n * @param options.scope - The CAIP-2 scope for the request.\n * @returns The response from the chosen Snap.\n * @throws If no handler was found.\n */\n async handleRequest({\n connectedAddresses,\n origin,\n scope,\n request: rawRequest,\n }: {\n connectedAddresses: CaipAccountId[];\n origin: string;\n scope: CaipChainId;\n request: JsonRpcRequest;\n }): Promise<Json> {\n // Explicitly block EVM scopes, just in case.\n assert(\n !scope.startsWith(KnownCaipNamespace.Eip155) &&\n !scope.startsWith('wallet:eip155'),\n );\n\n // Re-create the request to simplify and remove additional properties that may be present in MM middleware.\n const request = {\n jsonrpc: '2.0' as const,\n id: rawRequest.id ?? nanoid(),\n method: rawRequest.method,\n ...(rawRequest.params ? { params: rawRequest.params } : {}),\n };\n\n const { method, params } = request;\n\n // If the RPC request can be serviced by an account Snap, route it there.\n const accountId = await this.#getSnapAccountId(\n connectedAddresses,\n scope,\n request,\n );\n\n if (accountId) {\n return this.#withSnapKeyring(async ({ keyring }) =>\n keyring.submitRequest({\n origin,\n account: accountId,\n scope,\n method,\n params: params as JsonRpcParams,\n }),\n );\n }\n\n // If the RPC request cannot be serviced by an account Snap,\n // but has a protocol Snap available, route it there.\n const protocolSnaps = this.#getProtocolSnaps(scope);\n const protocolSnap = protocolSnaps.find((snap) =>\n snap.methods.includes(method),\n );\n\n if (protocolSnap) {\n return this.#messenger.call('SnapController:handleRequest', {\n snapId: protocolSnap.snapId,\n origin,\n request: {\n method: '',\n params: {\n request,\n scope,\n },\n },\n handler: HandlerType.OnProtocolRequest,\n }) as Promise<Json>;\n }\n\n // If no compatible account or protocol Snaps were found, throw.\n throw rpcErrors.methodNotFound();\n }\n\n /**\n * Get a list of metadata for supported accounts for a given scope from the client.\n *\n * @param scope - The CAIP-2 scope.\n * @returns A list of metadata for the supported accounts.\n */\n #getSupportedAccountsMetadata(scope: CaipChainId): InternalAccount[] {\n return this.#messenger\n .call('AccountsController:listMultichainAccounts', scope)\n .filter((account: InternalAccount) => account.metadata.snap?.enabled);\n }\n\n /**\n * Get a list of supported methods for a given scope.\n * This combines both protocol and account Snaps supported methods.\n *\n * @param scope - The CAIP-2 scope.\n * @returns A list of supported methods.\n */\n getSupportedMethods(scope: CaipChainId): string[] {\n const accountMethods = this.#getSupportedAccountsMetadata(scope).flatMap(\n (account) => account.methods,\n );\n\n const protocolMethods = this.#getProtocolSnaps(scope).flatMap(\n (snap) => snap.methods,\n );\n\n return Array.from(new Set([...accountMethods, ...protocolMethods]));\n }\n\n /**\n * Get a list of supported accounts for a given scope.\n *\n * @param scope - The CAIP-2 scope.\n * @returns A list of CAIP-10 addresses.\n */\n getSupportedAccounts(scope: CaipChainId): string[] {\n return this.#getSupportedAccountsMetadata(scope).map(\n (account) => `${scope}:${account.address}`,\n );\n }\n\n /**\n * Determine whether a given CAIP-2 scope is supported by the router.\n *\n * @param scope - The CAIP-2 scope.\n * @returns True if the router can service the scope, otherwise false.\n */\n isSupportedScope(scope: CaipChainId): boolean {\n const hasAccountSnap = this.#messenger\n .call('AccountsController:listMultichainAccounts', scope)\n .some((account: InternalAccount) => account.metadata.snap?.enabled);\n // We currently assume here that if one Snap exists that service the scope, we can service the scope generally.\n return hasAccountSnap || this.#getProtocolSnaps(scope).length > 0;\n }\n}\n"]}
1
+ {"version":3,"file":"MultichainRoutingService.mjs","sourceRoot":"","sources":["../../src/multichain/MultichainRoutingService.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,6BAA6B;AACjD,OAAO,EACL,uBAAuB,EACvB,cAAc,EACf,oCAAoC;AAGrC,OAAO,EAAE,WAAW,EAAE,8BAA8B;AAMpD,OAAO,EACL,MAAM,EACN,WAAW,EACX,QAAQ,EACR,kBAAkB,EAClB,kBAAkB,EACnB,wBAAwB;AACzB,OAAO,EAAE,MAAM,EAAE,eAAe;AAsDhC,MAAM,IAAI,GAAG,0BAA0B,CAAC;AAExC,MAAM,yBAAyB,GAAG;IAChC,eAAe;IACf,qBAAqB;IACrB,sBAAsB;IACtB,kBAAkB;CACV,CAAC;AAEX,MAAM,OAAO,wBAAwB;IACnC,IAAI,GAAgB,IAAI,CAAC;IAEzB,KAAK,GAAG,IAAI,CAAC;IAEJ,UAAU,CAAoC;IAE9C,gBAAgB,CAA0B;IAEnD,YAAY,EAAE,SAAS,EAAE,eAAe,EAAgC;QACtE,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;QAC5B,IAAI,CAAC,gBAAgB,GAAG,eAAe,CAAC;QAExC,IAAI,CAAC,UAAU,CAAC,4BAA4B,CAC1C,IAAI,EACJ,yBAAyB,CAC1B,CAAC;IACJ,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,KAAK,CAAC,sBAAsB,CAC1B,MAAc,EACd,KAAkB,EAClB,OAAuB;QAEvB,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,IAAI,CACvC,8BAA8B,EAC9B;gBACE,MAAM;gBACN,MAAM,EAAE,UAAU;gBAClB,OAAO,EAAE;oBACP,MAAM,EAAE,+BAA+B;oBACvC,MAAM,EAAE;wBACN,OAAO;wBACP,KAAK;qBACN;iBACF;gBACD,OAAO,EAAE,WAAW,CAAC,gBAAgB;aACtC,CACF,CAAC;YAEF,MAAM,CAAC,MAAM,K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AAE,GAAG,eAAe,CAAC,CAAC,CAAC,CAAC;IACtE,CAAC;IAED;;;;;OAKG;IACH,oBAAoB,CAAC,KAAkB;QACrC,OAAO,IAAI,CAAC,6BAA6B,CAAC,KAAK,CAAC,CAAC,GAAG,CAClD,CAAC,OAAO,EAAE,EAAE,CAAC,GAAG,KAAK,IAAI,OAAO,CAAC,OAAO,EAAE,CAC3C,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACH,gBAAgB,CAAC,KAAkB;QACjC,MAAM,cAAc,GAAG,IAAI,CAAC,UAAU;aACnC,IAAI,CAAC,2CAA2C,EAAE,KAAK,CAAC;aACxD,IAAI,CAAC,CAAC,OAAwB,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QACtE,+GAA+G;QAC/G,OAAO,cAAc,IAAI,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;IACpE,CAAC;CACF","sourcesContent":["import type { Messenger } from '@metamask/messenger';\nimport type { PermissionControllerGetPermissionsAction } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport {\n getProtocolCaveatScopes,\n SnapEndowments,\n} from '@metamask/snaps-rpc-methods';\nimport type { Json, JsonRpcRequest, SnapId } from '@metamask/snaps-sdk';\nimport type { InternalAccount } from '@metamask/snaps-utils';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport type {\n CaipAccountId,\n CaipChainId,\n JsonRpcParams,\n} from '@metamask/utils';\nimport {\n assert,\n hasProperty,\n isObject,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\nimport { nanoid } from 'nanoid';\n\nimport type { MultichainRoutingServiceMethodActions } from './MultichainRoutingService-method-action-types';\nimport type {\n SnapControllerGetRunnableSnapsAction,\n SnapControllerHandleRequestAction,\n} from '../snaps';\n\ntype SnapKeyring = {\n submitRequest: (request: {\n origin: string;\n account: string;\n method: string;\n params?: Json[] | Record<string, Json>;\n scope: CaipChainId;\n }) => Promise<Json>;\n};\n\n// Expecting a bound function that calls KeyringController.withKeyring selecting the Snap keyring\nexport type WithSnapKeyringFunction = <ReturnType>(\n operation: ({ keyring }: { keyring: SnapKeyring }) => Promise<ReturnType>,\n) => Promise<ReturnType>;\n\nexport type AccountsControllerListMultichainAccountsAction = {\n type: `AccountsController:listMultichainAccounts`;\n handler: (chainId?: CaipChainId) => InternalAccount[];\n};\n\nexport type MultichainRoutingServiceActions =\n MultichainRoutingServiceMethodActions;\n\ntype AllowedActions =\n | SnapControllerGetRunnableSnapsAction\n | SnapControllerHandleRequestAction\n | PermissionControllerGetPermissionsAction\n | AccountsControllerListMultichainAccountsAction;\n\nexport type MultichainRoutingServiceEvents = never;\n\nexport type MultichainRoutingServiceMessenger = Messenger<\n typeof name,\n MultichainRoutingServiceActions | AllowedActions\n>;\n\nexport type MultichainRoutingServiceArgs = {\n messenger: MultichainRoutingServiceMessenger;\n withSnapKeyring: WithSnapKeyringFunction;\n};\n\ntype ProtocolSnap = {\n snapId: SnapId;\n methods: string[];\n};\n\nconst name = 'MultichainRoutingService';\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'handleRequest',\n 'getSupportedMethods',\n 'getSupportedAccounts',\n 'isSupportedScope',\n] as const;\n\nexport class MultichainRoutingService {\n name: typeof name = name;\n\n state = null;\n\n readonly #messenger: MultichainRoutingServiceMessenger;\n\n readonly #withSnapKeyring: WithSnapKeyringFunction;\n\n constructor({ messenger, withSnapKeyring }: MultichainRoutingServiceArgs) {\n this.#messenger = messenger;\n this.#withSnapKeyring = withSnapKeyring;\n\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Attempts to resolve the account address to use for a given request by inspecting the request itself.\n *\n * The request is sent to an account Snap that will attempt this resolution.\n *\n * We manually construct the request instead of using the SnapKeyring, as the keyring may not be available.\n *\n * @param snapId - The ID of the Snap to send the request to.\n * @param scope - The CAIP-2 scope for the request.\n * @param request - The JSON-RPC request.\n * @returns The resolved address if found, otherwise null.\n * @throws If the invocation of the SnapKeyring fails.\n */\n async #resolveRequestAddress(\n snapId: SnapId,\n scope: CaipChainId,\n request: JsonRpcRequest,\n ) {\n try {\n const result = await this.#messenger.call(\n 'SnapController:handleRequest',\n {\n snapId,\n origin: 'metamask',\n request: {\n method: 'keyring_resolveAccountAddress',\n params: {\n request,\n scope,\n },\n },\n handler: HandlerType.OnKeyringRequest,\n },\n );\n\n assert(result === null || isObject(result));\n\n const address = result?.address as CaipAccountId;\n return address ? parseCaipAccountId(address).address : null;\n } catch {\n throw rpcErrors.internal();\n }\n }\n\n /**\n * Get the account ID of the account that should service the RPC request via an account Snap.\n *\n * This function checks whether any accounts exist that can service a given request by\n * using a combination of the resolveAccountAddress functionality and the connected accounts.\n *\n * If an account is expected to service this request but none is found, the function will throw.\n *\n * @param connectedAddresses - The CAIP-10 addresses connected to the\n * requesting origin for the requested scope.\n * @param scope - The CAIP-2 scope for the request.\n * @param request - The JSON-RPC request.\n * @returns An account ID if found, otherwise null.\n * @throws If no account is found, but the accounts exist that could service the request.\n */\n async #getSnapAccountId(\n connectedAddresses: CaipAccountId[],\n scope: CaipChainId,\n request: JsonRpcRequest,\n ) {\n const accounts = this.#messenger\n .call('AccountsController:listMultichainAccounts', scope)\n .filter(\n (\n account: InternalAccount,\n ): account is InternalAccount & {\n metadata: Required<InternalAccount['metadata']>;\n } =>\n Boolean(account.metadata.snap?.enabled) &&\n account.methods.includes(request.method),\n );\n\n // If no accounts can service the request, return null.\n if (accounts.length === 0) {\n return null;\n }\n\n const parsedConnectedAddresses = connectedAddresses.map(\n (connectedAddress) => parseCaipAccountId(connectedAddress).address,\n );\n\n const connectedAccounts = accounts.filter((account) =>\n parsedConnectedAddresses.includes(account.address),\n );\n\n if (connectedAccounts.length === 0) {\n throw rpcErrors.invalidParams({\n message: 'No available account found for request.',\n });\n }\n\n const resolutionSnapId = connectedAccounts[0].metadata.snap.id;\n\n // Attempt to resolve the address that should be used for signing.\n const address = await this.#resolveRequestAddress(\n resolutionSnapId,\n scope,\n request,\n );\n\n // If we have a resolved address, try to find the selected account based on that\n // otherwise, default to one of the connected accounts.\n // TODO: Eventually let the user choose if we have more than one option for the account.\n const selectedAccount = address\n ? connectedAccounts.find((account) => account.address === address)\n : connectedAccounts[0];\n\n if (!selectedAccount) {\n throw rpcErrors.invalidParams({\n message: 'No available account found for request.',\n });\n }\n\n return selectedAccount.id;\n }\n\n /**\n * Get all protocol Snaps that can service a given CAIP-2 scope.\n *\n * Protocol Snaps are deemed fit to service a scope if they are runnable\n * and have the proper permissions set for the scope.\n *\n * @param scope - A CAIP-2 scope.\n * @returns A list of all the protocol Snaps available and their RPC methods.\n */\n #getProtocolSnaps(scope: CaipChainId) {\n const filteredSnaps = this.#messenger.call(\n 'SnapController:getRunnableSnaps',\n );\n\n return filteredSnaps.reduce<ProtocolSnap[]>((accumulator, snap) => {\n const permissions = this.#messenger.call(\n 'PermissionController:getPermissions',\n snap.id,\n );\n\n if (permissions && hasProperty(permissions, SnapEndowments.Protocol)) {\n const permission = permissions[SnapEndowments.Protocol];\n const scopes = getProtocolCaveatScopes(permission);\n if (scopes && hasProperty(scopes, scope)) {\n accumulator.push({\n snapId: snap.id,\n methods: scopes[scope].methods,\n });\n }\n }\n\n return accumulator;\n }, []);\n }\n\n /**\n * Handle an incoming JSON-RPC request tied to a specific scope by routing\n * to either a protocol Snap or an account Snap.\n *\n * Note: Addresses are considered case-sensitive by the MultichainRoutingService as\n * not all non-EVM chains are case-insensitive.\n *\n * @param options - An options bag.\n * @param options.connectedAddresses - Addresses currently connected to the\n * origin for the requested scope.\n * @param options.origin - The origin of the RPC request.\n * @param options.request - The JSON-RPC request.\n * @param options.scope - The CAIP-2 scope for the request.\n * @returns The response from the chosen Snap.\n * @throws If no handler was found.\n */\n async handleRequest({\n connectedAddresses,\n origin,\n scope,\n request: rawRequest,\n }: {\n connectedAddresses: CaipAccountId[];\n origin: string;\n scope: CaipChainId;\n request: JsonRpcRequest;\n }): Promise<Json> {\n // Explicitly block EVM scopes, just in case.\n assert(\n !scope.startsWith(KnownCaipNamespace.Eip155) &&\n !scope.startsWith('wallet:eip155'),\n );\n\n // Re-create the request to simplify and remove additional properties that may be present in MM middleware.\n const request = {\n jsonrpc: '2.0' as const,\n id: rawRequest.id ?? nanoid(),\n method: rawRequest.method,\n ...(rawRequest.params ? { params: rawRequest.params } : {}),\n };\n\n const { method, params } = request;\n\n // If the RPC request can be serviced by an account Snap, route it there.\n const accountId = await this.#getSnapAccountId(\n connectedAddresses,\n scope,\n request,\n );\n\n if (accountId) {\n return this.#withSnapKeyring(async ({ keyring }) =>\n keyring.submitRequest({\n origin,\n account: accountId,\n scope,\n method,\n params: params as JsonRpcParams,\n }),\n );\n }\n\n // If the RPC request cannot be serviced by an account Snap,\n // but has a protocol Snap available, route it there.\n const protocolSnaps = this.#getProtocolSnaps(scope);\n const protocolSnap = protocolSnaps.find((snap) =>\n snap.methods.includes(method),\n );\n\n if (protocolSnap) {\n return this.#messenger.call('SnapController:handleRequest', {\n snapId: protocolSnap.snapId,\n origin,\n request: {\n method: '',\n params: {\n request,\n scope,\n },\n },\n handler: HandlerType.OnProtocolRequest,\n }) as Promise<Json>;\n }\n\n // If no compatible account or protocol Snaps were found, throw.\n throw rpcErrors.methodNotFound();\n }\n\n /**\n * Get a list of metadata for supported accounts for a given scope from the client.\n *\n * @param scope - The CAIP-2 scope.\n * @returns A list of metadata for the supported accounts.\n */\n #getSupportedAccountsMetadata(scope: CaipChainId): InternalAccount[] {\n return this.#messenger\n .call('AccountsController:listMultichainAccounts', scope)\n .filter((account: InternalAccount) => account.metadata.snap?.enabled);\n }\n\n /**\n * Get a list of supported methods for a given scope.\n * This combines both protocol and account Snaps supported methods.\n *\n * @param scope - The CAIP-2 scope.\n * @returns A list of supported methods.\n */\n getSupportedMethods(scope: CaipChainId): string[] {\n const accountMethods = this.#getSupportedAccountsMetadata(scope).flatMap(\n (account) => account.methods,\n );\n\n const protocolMethods = this.#getProtocolSnaps(scope).flatMap(\n (snap) => snap.methods,\n );\n\n return Array.from(new Set([...accountMethods, ...protocolMethods]));\n }\n\n /**\n * Get a list of supported accounts for a given scope.\n *\n * @param scope - The CAIP-2 scope.\n * @returns A list of CAIP-10 addresses.\n */\n getSupportedAccounts(scope: CaipChainId): string[] {\n return this.#getSupportedAccountsMetadata(scope).map(\n (account) => `${scope}:${account.address}`,\n );\n }\n\n /**\n * Determine whether a given CAIP-2 scope is supported by the router.\n *\n * @param scope - The CAIP-2 scope.\n * @returns True if the router can service the scope, otherwise false.\n */\n isSupportedScope(scope: CaipChainId): boolean {\n const hasAccountSnap = this.#messenger\n .call('AccountsController:listMultichainAccounts', scope)\n .some((account: InternalAccount) => account.metadata.snap?.enabled);\n // We currently assume here that if one Snap exists that service the scope, we can service the scope generally.\n return hasAccountSnap || this.#getProtocolSnaps(scope).length > 0;\n }\n}\n"]}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /**
3
- * This file is auto generated by `scripts/generate-method-action-types.ts`.
3
+ * This file is auto generated.
4
4
  * Do not edit manually.
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -1 +1 @@
1
- {"version":3,"file":"ExecutionService-method-action-types.cjs","sourceRoot":"","sources":["../../src/services/ExecutionService-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { ExecutionService } from './ExecutionService';\n\n/**\n * Terminates the Snap with the specified ID and deletes all its associated\n * data. Any subsequent messages targeting the Snap will fail with an error.\n * Throws an error if termination fails unexpectedly.\n *\n * @param snapId - The id of the Snap to be terminated.\n */\nexport type ExecutionServiceTerminateSnapAction = {\n type: `ExecutionService:terminateSnap`;\n handler: ExecutionService['terminateSnap'];\n};\n\nexport type ExecutionServiceTerminateAllSnapsAction = {\n type: `ExecutionService:terminateAllSnaps`;\n handler: ExecutionService['terminateAllSnaps'];\n};\n\n/**\n * Initializes and executes a Snap, setting up the communication channels to the Snap etc.\n *\n * @param snapData - Data needed for Snap execution.\n * @param snapData.snapId - The ID of the Snap to execute.\n * @param snapData.sourceCode - The source code of the Snap to execute.\n * @param snapData.endowments - The endowments available to the executing Snap.\n * @returns A string `OK` if execution succeeded.\n * @throws If the execution service returns an error or execution times out.\n */\nexport type ExecutionServiceExecuteSnapAction = {\n type: `ExecutionService:executeSnap`;\n handler: ExecutionService['executeSnap'];\n};\n\n/**\n * Handle RPC request.\n *\n * @param snapId - The ID of the recipient Snap.\n * @param options - Bag of options to pass to the RPC handler.\n * @returns Promise that can handle the request.\n */\nexport type ExecutionServiceHandleRpcRequestAction = {\n type: `ExecutionService:handleRpcRequest`;\n handler: ExecutionService['handleRpcRequest'];\n};\n\n/**\n * Union of all ExecutionService action types.\n */\nexport type ExecutionServiceMethodActions =\n | ExecutionServiceTerminateSnapAction\n | ExecutionServiceTerminateAllSnapsAction\n | ExecutionServiceExecuteSnapAction\n | ExecutionServiceHandleRpcRequestAction;\n"]}
1
+ {"version":3,"file":"ExecutionService-method-action-types.cjs","sourceRoot":"","sources":["../../src/services/ExecutionService-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { ExecutionService } from './ExecutionService';\n\n/**\n * Terminates the Snap with the specified ID and deletes all its associated\n * data. Any subsequent messages targeting the Snap will fail with an error.\n * Throws an error if termination fails unexpectedly.\n *\n * @param snapId - The id of the Snap to be terminated.\n */\nexport type ExecutionServiceTerminateSnapAction = {\n type: `ExecutionService:terminateSnap`;\n handler: ExecutionService['terminateSnap'];\n};\n\nexport type ExecutionServiceTerminateAllSnapsAction = {\n type: `ExecutionService:terminateAllSnaps`;\n handler: ExecutionService['terminateAllSnaps'];\n};\n\n/**\n * Initializes and executes a Snap, setting up the communication channels to the Snap etc.\n *\n * @param snapData - Data needed for Snap execution.\n * @param snapData.snapId - The ID of the Snap to execute.\n * @param snapData.sourceCode - The source code of the Snap to execute.\n * @param snapData.endowments - The endowments available to the executing Snap.\n * @returns A string `OK` if execution succeeded.\n * @throws If the execution service returns an error or execution times out.\n */\nexport type ExecutionServiceExecuteSnapAction = {\n type: `ExecutionService:executeSnap`;\n handler: ExecutionService['executeSnap'];\n};\n\n/**\n * Handle RPC request.\n *\n * @param snapId - The ID of the recipient Snap.\n * @param options - Bag of options to pass to the RPC handler.\n * @returns Promise that can handle the request.\n */\nexport type ExecutionServiceHandleRpcRequestAction = {\n type: `ExecutionService:handleRpcRequest`;\n handler: ExecutionService['handleRpcRequest'];\n};\n\n/**\n * Union of all ExecutionService action types.\n */\nexport type ExecutionServiceMethodActions =\n | ExecutionServiceTerminateSnapAction\n | ExecutionServiceTerminateAllSnapsAction\n | ExecutionServiceExecuteSnapAction\n | ExecutionServiceHandleRpcRequestAction;\n"]}
@@ -1,5 +1,5 @@
1
1
  /**
2
- * This file is auto generated by `scripts/generate-method-action-types.ts`.
2
+ * This file is auto generated.
3
3
  * Do not edit manually.
4
4
  */
5
5
  import type { ExecutionService } from "./ExecutionService.cjs";
@@ -1,5 +1,5 @@
1
1
  /**
2
- * This file is auto generated by `scripts/generate-method-action-types.ts`.
2
+ * This file is auto generated.
3
3
  * Do not edit manually.
4
4
  */
5
5
  import type { ExecutionService } from "./ExecutionService.mjs";
@@ -1,5 +1,5 @@
1
1
  /**
2
- * This file is auto generated by `scripts/generate-method-action-types.ts`.
2
+ * This file is auto generated.
3
3
  * Do not edit manually.
4
4
  */
5
5
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"ExecutionService-method-action-types.mjs","sourceRoot":"","sources":["../../src/services/ExecutionService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { ExecutionService } from './ExecutionService';\n\n/**\n * Terminates the Snap with the specified ID and deletes all its associated\n * data. Any subsequent messages targeting the Snap will fail with an error.\n * Throws an error if termination fails unexpectedly.\n *\n * @param snapId - The id of the Snap to be terminated.\n */\nexport type ExecutionServiceTerminateSnapAction = {\n type: `ExecutionService:terminateSnap`;\n handler: ExecutionService['terminateSnap'];\n};\n\nexport type ExecutionServiceTerminateAllSnapsAction = {\n type: `ExecutionService:terminateAllSnaps`;\n handler: ExecutionService['terminateAllSnaps'];\n};\n\n/**\n * Initializes and executes a Snap, setting up the communication channels to the Snap etc.\n *\n * @param snapData - Data needed for Snap execution.\n * @param snapData.snapId - The ID of the Snap to execute.\n * @param snapData.sourceCode - The source code of the Snap to execute.\n * @param snapData.endowments - The endowments available to the executing Snap.\n * @returns A string `OK` if execution succeeded.\n * @throws If the execution service returns an error or execution times out.\n */\nexport type ExecutionServiceExecuteSnapAction = {\n type: `ExecutionService:executeSnap`;\n handler: ExecutionService['executeSnap'];\n};\n\n/**\n * Handle RPC request.\n *\n * @param snapId - The ID of the recipient Snap.\n * @param options - Bag of options to pass to the RPC handler.\n * @returns Promise that can handle the request.\n */\nexport type ExecutionServiceHandleRpcRequestAction = {\n type: `ExecutionService:handleRpcRequest`;\n handler: ExecutionService['handleRpcRequest'];\n};\n\n/**\n * Union of all ExecutionService action types.\n */\nexport type ExecutionServiceMethodActions =\n | ExecutionServiceTerminateSnapAction\n | ExecutionServiceTerminateAllSnapsAction\n | ExecutionServiceExecuteSnapAction\n | ExecutionServiceHandleRpcRequestAction;\n"]}
1
+ {"version":3,"file":"ExecutionService-method-action-types.mjs","sourceRoot":"","sources":["../../src/services/ExecutionService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { ExecutionService } from './ExecutionService';\n\n/**\n * Terminates the Snap with the specified ID and deletes all its associated\n * data. Any subsequent messages targeting the Snap will fail with an error.\n * Throws an error if termination fails unexpectedly.\n *\n * @param snapId - The id of the Snap to be terminated.\n */\nexport type ExecutionServiceTerminateSnapAction = {\n type: `ExecutionService:terminateSnap`;\n handler: ExecutionService['terminateSnap'];\n};\n\nexport type ExecutionServiceTerminateAllSnapsAction = {\n type: `ExecutionService:terminateAllSnaps`;\n handler: ExecutionService['terminateAllSnaps'];\n};\n\n/**\n * Initializes and executes a Snap, setting up the communication channels to the Snap etc.\n *\n * @param snapData - Data needed for Snap execution.\n * @param snapData.snapId - The ID of the Snap to execute.\n * @param snapData.sourceCode - The source code of the Snap to execute.\n * @param snapData.endowments - The endowments available to the executing Snap.\n * @returns A string `OK` if execution succeeded.\n * @throws If the execution service returns an error or execution times out.\n */\nexport type ExecutionServiceExecuteSnapAction = {\n type: `ExecutionService:executeSnap`;\n handler: ExecutionService['executeSnap'];\n};\n\n/**\n * Handle RPC request.\n *\n * @param snapId - The ID of the recipient Snap.\n * @param options - Bag of options to pass to the RPC handler.\n * @returns Promise that can handle the request.\n */\nexport type ExecutionServiceHandleRpcRequestAction = {\n type: `ExecutionService:handleRpcRequest`;\n handler: ExecutionService['handleRpcRequest'];\n};\n\n/**\n * Union of all ExecutionService action types.\n */\nexport type ExecutionServiceMethodActions =\n | ExecutionServiceTerminateSnapAction\n | ExecutionServiceTerminateAllSnapsAction\n | ExecutionServiceExecuteSnapAction\n | ExecutionServiceHandleRpcRequestAction;\n"]}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /**
3
- * This file is auto generated by `scripts/generate-method-action-types.ts`.
3
+ * This file is auto generated.
4
4
  * Do not edit manually.
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -1 +1 @@
1
- {"version":3,"file":"SnapController-method-action-types.cjs","sourceRoot":"","sources":["../../src/snaps/SnapController-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { SnapController } from './SnapController';\n\n/**\n * Initialise the SnapController.\n *\n * Currently this method sets up the controller and calls the `onStart` lifecycle hook for all\n * runnable Snaps.\n *\n * @param waitForPlatform - Whether to wait for the platform to be ready before returning.\n */\nexport type SnapControllerInitAction = {\n type: `SnapController:init`;\n handler: SnapController['init'];\n};\n\n/**\n * Trigger an update of the registry.\n *\n * As a side-effect of this, preinstalled Snaps may be updated and Snaps may be blocked/unblocked.\n */\nexport type SnapControllerUpdateRegistryAction = {\n type: `SnapController:updateRegistry`;\n handler: SnapController['updateRegistry'];\n};\n\n/**\n * Enables the given snap. A snap can only be started if it is enabled. A snap\n * can only be enabled if it isn't blocked.\n *\n * @param snapId - The id of the Snap to enable.\n */\nexport type SnapControllerEnableSnapAction = {\n type: `SnapController:enableSnap`;\n handler: SnapController['enableSnap'];\n};\n\n/**\n * Disables the given snap. A snap can only be started if it is enabled.\n *\n * @param snapId - The id of the Snap to disable.\n * @returns A promise that resolves once the snap has been disabled.\n */\nexport type SnapControllerDisableSnapAction = {\n type: `SnapController:disableSnap`;\n handler: SnapController['disableSnap'];\n};\n\n/**\n * Stops the given snap, removes all hooks, closes all connections, and\n * terminates its worker.\n *\n * @param snapId - The id of the Snap to stop.\n * @param statusEvent - The Snap status event that caused the snap to be\n * stopped.\n */\nexport type SnapControllerStopSnapAction = {\n type: `SnapController:stopSnap`;\n handler: SnapController['stopSnap'];\n};\n\n/**\n * Stops all running snaps, removes all hooks, closes all connections, and\n * terminates their workers.\n *\n * @param statusEvent - The Snap status event that caused the snap to be\n * stopped.\n */\nexport type SnapControllerStopAllSnapsAction = {\n type: `SnapController:stopAllSnaps`;\n handler: SnapController['stopAllSnaps'];\n};\n\n/**\n * Returns whether the given snap is running.\n * Throws an error if the snap doesn't exist.\n *\n * @param snapId - The id of the Snap to check.\n * @returns `true` if the snap is running, otherwise `false`.\n */\nexport type SnapControllerIsSnapRunningAction = {\n type: `SnapController:isSnapRunning`;\n handler: SnapController['isSnapRunning'];\n};\n\n/**\n * Returns whether the given snap has been added to state.\n *\n * @param snapId - The id of the Snap to check for.\n * @returns `true` if the snap exists in the controller state, otherwise `false`.\n */\nexport type SnapControllerHasSnapAction = {\n type: `SnapController:hasSnap`;\n handler: SnapController['hasSnap'];\n};\n\n/**\n * Gets the snap with the given id if it exists, including all data.\n * This should not be used if the snap is to be serializable, as e.g.\n * the snap sourceCode may be quite large.\n *\n * @param snapId - The id of the Snap to get.\n * @returns The entire snap object from the controller state.\n */\nexport type SnapControllerGetSnapAction = {\n type: `SnapController:getSnap`;\n handler: SnapController['getSnap'];\n};\n\n/**\n * Updates the own state of the snap with the given id.\n * This is distinct from the state MetaMask uses to manage snaps.\n *\n * @param snapId - The id of the Snap whose state should be updated.\n * @param newSnapState - The new state of the snap.\n * @param encrypted - A flag to indicate whether to use encrypted storage or not.\n */\nexport type SnapControllerUpdateSnapStateAction = {\n type: `SnapController:updateSnapState`;\n handler: SnapController['updateSnapState'];\n};\n\n/**\n * Clears the state of the snap with the given id.\n * This is distinct from the state MetaMask uses to manage snaps.\n *\n * @param snapId - The id of the Snap whose state should be cleared.\n * @param encrypted - A flag to indicate whether to use encrypted storage or not.\n */\nexport type SnapControllerClearSnapStateAction = {\n type: `SnapController:clearSnapState`;\n handler: SnapController['clearSnapState'];\n};\n\n/**\n * Gets the own state of the snap with the given id.\n * This is distinct from the state MetaMask uses to manage snaps.\n *\n * @param snapId - The id of the Snap whose state to get.\n * @param encrypted - A flag to indicate whether to use encrypted storage or not.\n * @returns The requested snap state or null if no state exists.\n */\nexport type SnapControllerGetSnapStateAction = {\n type: `SnapController:getSnapState`;\n handler: SnapController['getSnapState'];\n};\n\n/**\n * Gets a static auxiliary snap file in a chosen file encoding.\n *\n * @param snapId - The id of the Snap whose state to get.\n * @param path - The path to the requested file.\n * @param encoding - An optional requested file encoding.\n * @returns The file requested in the chosen file encoding or null if the file is not found.\n */\nexport type SnapControllerGetSnapFileAction = {\n type: `SnapController:getSnapFile`;\n handler: SnapController['getSnapFile'];\n};\n\n/**\n * Determine if a given Snap ID supports a given minimum version of the Snaps platform\n * by inspecting the platformVersion in the Snap manifest.\n *\n * @param snapId - The Snap ID.\n * @param version - The version.\n * @returns True if the platform version is equal or greater to the passed version, false otherwise.\n */\nexport type SnapControllerIsMinimumPlatformVersionAction = {\n type: `SnapController:isMinimumPlatformVersion`;\n handler: SnapController['isMinimumPlatformVersion'];\n};\n\n/**\n * Completely clear the controller's state: delete all associated data,\n * handlers, event listeners, and permissions; tear down all snap providers.\n * Also re-initializes the controller after clearing the state.\n */\nexport type SnapControllerClearStateAction = {\n type: `SnapController:clearState`;\n handler: SnapController['clearState'];\n};\n\n/**\n * Removes the given snap from state, and clears all associated handlers\n * and listeners.\n *\n * @param snapId - The id of the Snap.\n * @returns A promise that resolves once the snap has been removed.\n */\nexport type SnapControllerRemoveSnapAction = {\n type: `SnapController:removeSnap`;\n handler: SnapController['removeSnap'];\n};\n\n/**\n * Stops the given snaps, removes them from state, and clears all associated\n * permissions, handlers, and listeners.\n *\n * @param snapIds - The ids of the Snaps.\n */\nexport type SnapControllerRemoveSnapsAction = {\n type: `SnapController:removeSnaps`;\n handler: SnapController['removeSnaps'];\n};\n\n/**\n * Disconnect the Snap from the given origin, meaning the origin can no longer\n * interact with the Snap until it is reconnected.\n *\n * @param origin - The origin from which to remove the Snap.\n * @param snapId - The id of the snap to remove.\n */\nexport type SnapControllerDisconnectOriginAction = {\n type: `SnapController:disconnectOrigin`;\n handler: SnapController['disconnectOrigin'];\n};\n\n/**\n * Checks if a list of permissions are dynamic and allowed to be revoked, if they are they will all be revoked.\n *\n * @param snapId - The snap ID.\n * @param permissionNames - The names of the permissions.\n * @throws If non-dynamic permissions are passed.\n */\nexport type SnapControllerRevokeDynamicSnapPermissionsAction = {\n type: `SnapController:revokeDynamicSnapPermissions`;\n handler: SnapController['revokeDynamicSnapPermissions'];\n};\n\n/**\n * Gets all snaps in their truncated format.\n *\n * @returns All installed snaps in their truncated format.\n */\nexport type SnapControllerGetAllSnapsAction = {\n type: `SnapController:getAllSnaps`;\n handler: SnapController['getAllSnaps'];\n};\n\n/**\n * Gets all runnable snaps.\n *\n * @returns All runnable snaps.\n */\nexport type SnapControllerGetRunnableSnapsAction = {\n type: `SnapController:getRunnableSnaps`;\n handler: SnapController['getRunnableSnaps'];\n};\n\n/**\n * Gets the serialized permitted snaps of the given origin, if any.\n *\n * @param origin - The origin whose permitted snaps to retrieve.\n * @returns The serialized permitted snaps for the origin.\n */\nexport type SnapControllerGetPermittedSnapsAction = {\n type: `SnapController:getPermittedSnaps`;\n handler: SnapController['getPermittedSnaps'];\n};\n\n/**\n * Installs the snaps requested by the given origin, returning the snap\n * object if the origin is permitted to install it, and an authorization error\n * otherwise.\n *\n * @param origin - The origin that requested to install the snaps.\n * @param requestedSnaps - The snaps to install.\n * @returns An object of snap ids and snap objects, or errors if a\n * snap couldn't be installed.\n */\nexport type SnapControllerInstallSnapsAction = {\n type: `SnapController:installSnaps`;\n handler: SnapController['installSnaps'];\n};\n\n/**\n * Passes a JSON-RPC request object to the RPC handler function of a snap.\n *\n * @param options - A bag of options.\n * @param options.snapId - The ID of the recipient snap.\n * @param options.origin - The origin of the RPC request.\n * @param options.handler - The handler to trigger on the snap for the request.\n * @param options.request - The JSON-RPC request object.\n * @returns The result of the JSON-RPC request.\n */\nexport type SnapControllerHandleRequestAction = {\n type: `SnapController:handleRequest`;\n handler: SnapController['handleRequest'];\n};\n\n/**\n * Set the active state of the client. This will trigger the `onActive` or\n * `onInactive` lifecycle hooks for all Snaps.\n *\n * @param active - A boolean indicating whether the client is active or not.\n */\nexport type SnapControllerSetClientActiveAction = {\n type: `SnapController:setClientActive`;\n handler: SnapController['setClientActive'];\n};\n\n/**\n * Union of all SnapController action types.\n */\nexport type SnapControllerMethodActions =\n | SnapControllerInitAction\n | SnapControllerUpdateRegistryAction\n | SnapControllerEnableSnapAction\n | SnapControllerDisableSnapAction\n | SnapControllerStopSnapAction\n | SnapControllerStopAllSnapsAction\n | SnapControllerIsSnapRunningAction\n | SnapControllerHasSnapAction\n | SnapControllerGetSnapAction\n | SnapControllerUpdateSnapStateAction\n | SnapControllerClearSnapStateAction\n | SnapControllerGetSnapStateAction\n | SnapControllerGetSnapFileAction\n | SnapControllerIsMinimumPlatformVersionAction\n | SnapControllerClearStateAction\n | SnapControllerRemoveSnapAction\n | SnapControllerRemoveSnapsAction\n | SnapControllerDisconnectOriginAction\n | SnapControllerRevokeDynamicSnapPermissionsAction\n | SnapControllerGetAllSnapsAction\n | SnapControllerGetRunnableSnapsAction\n | SnapControllerGetPermittedSnapsAction\n | SnapControllerInstallSnapsAction\n | SnapControllerHandleRequestAction\n | SnapControllerSetClientActiveAction;\n"]}
1
+ {"version":3,"file":"SnapController-method-action-types.cjs","sourceRoot":"","sources":["../../src/snaps/SnapController-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { SnapController } from './SnapController';\n\n/**\n * Initialise the SnapController.\n *\n * Currently this method sets up the controller and calls the `onStart` lifecycle hook for all\n * runnable Snaps.\n *\n * @param waitForPlatform - Whether to wait for the platform to be ready before returning.\n */\nexport type SnapControllerInitAction = {\n type: `SnapController:init`;\n handler: SnapController['init'];\n};\n\n/**\n * Trigger an update of the registry.\n *\n * As a side-effect of this, preinstalled Snaps may be updated and Snaps may be blocked/unblocked.\n */\nexport type SnapControllerUpdateRegistryAction = {\n type: `SnapController:updateRegistry`;\n handler: SnapController['updateRegistry'];\n};\n\n/**\n * Enables the given snap. A snap can only be started if it is enabled. A snap\n * can only be enabled if it isn't blocked.\n *\n * @param snapId - The id of the Snap to enable.\n */\nexport type SnapControllerEnableSnapAction = {\n type: `SnapController:enableSnap`;\n handler: SnapController['enableSnap'];\n};\n\n/**\n * Disables the given snap. A snap can only be started if it is enabled.\n *\n * @param snapId - The id of the Snap to disable.\n * @returns A promise that resolves once the snap has been disabled.\n */\nexport type SnapControllerDisableSnapAction = {\n type: `SnapController:disableSnap`;\n handler: SnapController['disableSnap'];\n};\n\n/**\n * Stops the given snap, removes all hooks, closes all connections, and\n * terminates its worker.\n *\n * @param snapId - The id of the Snap to stop.\n * @param statusEvent - The Snap status event that caused the snap to be\n * stopped.\n */\nexport type SnapControllerStopSnapAction = {\n type: `SnapController:stopSnap`;\n handler: SnapController['stopSnap'];\n};\n\n/**\n * Stops all running snaps, removes all hooks, closes all connections, and\n * terminates their workers.\n *\n * @param statusEvent - The Snap status event that caused the snap to be\n * stopped.\n */\nexport type SnapControllerStopAllSnapsAction = {\n type: `SnapController:stopAllSnaps`;\n handler: SnapController['stopAllSnaps'];\n};\n\n/**\n * Returns whether the given snap is running.\n * Throws an error if the snap doesn't exist.\n *\n * @param snapId - The id of the Snap to check.\n * @returns `true` if the snap is running, otherwise `false`.\n */\nexport type SnapControllerIsSnapRunningAction = {\n type: `SnapController:isSnapRunning`;\n handler: SnapController['isSnapRunning'];\n};\n\n/**\n * Returns whether the given snap has been added to state.\n *\n * @param snapId - The id of the Snap to check for.\n * @returns `true` if the snap exists in the controller state, otherwise `false`.\n */\nexport type SnapControllerHasSnapAction = {\n type: `SnapController:hasSnap`;\n handler: SnapController['hasSnap'];\n};\n\n/**\n * Gets the snap with the given id if it exists, including all data.\n * This should not be used if the snap is to be serializable, as e.g.\n * the snap sourceCode may be quite large.\n *\n * @param snapId - The id of the Snap to get.\n * @returns The entire snap object from the controller state.\n */\nexport type SnapControllerGetSnapAction = {\n type: `SnapController:getSnap`;\n handler: SnapController['getSnap'];\n};\n\n/**\n * Updates the own state of the snap with the given id.\n * This is distinct from the state MetaMask uses to manage snaps.\n *\n * @param snapId - The id of the Snap whose state should be updated.\n * @param newSnapState - The new state of the snap.\n * @param encrypted - A flag to indicate whether to use encrypted storage or not.\n */\nexport type SnapControllerUpdateSnapStateAction = {\n type: `SnapController:updateSnapState`;\n handler: SnapController['updateSnapState'];\n};\n\n/**\n * Clears the state of the snap with the given id.\n * This is distinct from the state MetaMask uses to manage snaps.\n *\n * @param snapId - The id of the Snap whose state should be cleared.\n * @param encrypted - A flag to indicate whether to use encrypted storage or not.\n */\nexport type SnapControllerClearSnapStateAction = {\n type: `SnapController:clearSnapState`;\n handler: SnapController['clearSnapState'];\n};\n\n/**\n * Gets the own state of the snap with the given id.\n * This is distinct from the state MetaMask uses to manage snaps.\n *\n * @param snapId - The id of the Snap whose state to get.\n * @param encrypted - A flag to indicate whether to use encrypted storage or not.\n * @returns The requested snap state or null if no state exists.\n */\nexport type SnapControllerGetSnapStateAction = {\n type: `SnapController:getSnapState`;\n handler: SnapController['getSnapState'];\n};\n\n/**\n * Gets a static auxiliary snap file in a chosen file encoding.\n *\n * @param snapId - The id of the Snap whose state to get.\n * @param path - The path to the requested file.\n * @param encoding - An optional requested file encoding.\n * @returns The file requested in the chosen file encoding or null if the file is not found.\n */\nexport type SnapControllerGetSnapFileAction = {\n type: `SnapController:getSnapFile`;\n handler: SnapController['getSnapFile'];\n};\n\n/**\n * Determine if a given Snap ID supports a given minimum version of the Snaps platform\n * by inspecting the platformVersion in the Snap manifest.\n *\n * @param snapId - The Snap ID.\n * @param version - The version.\n * @returns True if the platform version is equal or greater to the passed version, false otherwise.\n */\nexport type SnapControllerIsMinimumPlatformVersionAction = {\n type: `SnapController:isMinimumPlatformVersion`;\n handler: SnapController['isMinimumPlatformVersion'];\n};\n\n/**\n * Completely clear the controller's state: delete all associated data,\n * handlers, event listeners, and permissions; tear down all snap providers.\n * Also re-initializes the controller after clearing the state.\n */\nexport type SnapControllerClearStateAction = {\n type: `SnapController:clearState`;\n handler: SnapController['clearState'];\n};\n\n/**\n * Removes the given snap from state, and clears all associated handlers\n * and listeners.\n *\n * @param snapId - The id of the Snap.\n * @returns A promise that resolves once the snap has been removed.\n */\nexport type SnapControllerRemoveSnapAction = {\n type: `SnapController:removeSnap`;\n handler: SnapController['removeSnap'];\n};\n\n/**\n * Stops the given snaps, removes them from state, and clears all associated\n * permissions, handlers, and listeners.\n *\n * @param snapIds - The ids of the Snaps.\n */\nexport type SnapControllerRemoveSnapsAction = {\n type: `SnapController:removeSnaps`;\n handler: SnapController['removeSnaps'];\n};\n\n/**\n * Disconnect the Snap from the given origin, meaning the origin can no longer\n * interact with the Snap until it is reconnected.\n *\n * @param origin - The origin from which to remove the Snap.\n * @param snapId - The id of the snap to remove.\n */\nexport type SnapControllerDisconnectOriginAction = {\n type: `SnapController:disconnectOrigin`;\n handler: SnapController['disconnectOrigin'];\n};\n\n/**\n * Checks if a list of permissions are dynamic and allowed to be revoked, if they are they will all be revoked.\n *\n * @param snapId - The snap ID.\n * @param permissionNames - The names of the permissions.\n * @throws If non-dynamic permissions are passed.\n */\nexport type SnapControllerRevokeDynamicSnapPermissionsAction = {\n type: `SnapController:revokeDynamicSnapPermissions`;\n handler: SnapController['revokeDynamicSnapPermissions'];\n};\n\n/**\n * Gets all snaps in their truncated format.\n *\n * @returns All installed snaps in their truncated format.\n */\nexport type SnapControllerGetAllSnapsAction = {\n type: `SnapController:getAllSnaps`;\n handler: SnapController['getAllSnaps'];\n};\n\n/**\n * Gets all runnable snaps.\n *\n * @returns All runnable snaps.\n */\nexport type SnapControllerGetRunnableSnapsAction = {\n type: `SnapController:getRunnableSnaps`;\n handler: SnapController['getRunnableSnaps'];\n};\n\n/**\n * Gets the serialized permitted snaps of the given origin, if any.\n *\n * @param origin - The origin whose permitted snaps to retrieve.\n * @returns The serialized permitted snaps for the origin.\n */\nexport type SnapControllerGetPermittedSnapsAction = {\n type: `SnapController:getPermittedSnaps`;\n handler: SnapController['getPermittedSnaps'];\n};\n\n/**\n * Installs the snaps requested by the given origin, returning the snap\n * object if the origin is permitted to install it, and an authorization error\n * otherwise.\n *\n * @param origin - The origin that requested to install the snaps.\n * @param requestedSnaps - The snaps to install.\n * @returns An object of snap ids and snap objects, or errors if a\n * snap couldn't be installed.\n */\nexport type SnapControllerInstallSnapsAction = {\n type: `SnapController:installSnaps`;\n handler: SnapController['installSnaps'];\n};\n\n/**\n * Passes a JSON-RPC request object to the RPC handler function of a snap.\n *\n * @param options - A bag of options.\n * @param options.snapId - The ID of the recipient snap.\n * @param options.origin - The origin of the RPC request.\n * @param options.handler - The handler to trigger on the snap for the request.\n * @param options.request - The JSON-RPC request object.\n * @returns The result of the JSON-RPC request.\n */\nexport type SnapControllerHandleRequestAction = {\n type: `SnapController:handleRequest`;\n handler: SnapController['handleRequest'];\n};\n\n/**\n * Set the active state of the client. This will trigger the `onActive` or\n * `onInactive` lifecycle hooks for all Snaps.\n *\n * @param active - A boolean indicating whether the client is active or not.\n */\nexport type SnapControllerSetClientActiveAction = {\n type: `SnapController:setClientActive`;\n handler: SnapController['setClientActive'];\n};\n\n/**\n * Union of all SnapController action types.\n */\nexport type SnapControllerMethodActions =\n | SnapControllerInitAction\n | SnapControllerUpdateRegistryAction\n | SnapControllerEnableSnapAction\n | SnapControllerDisableSnapAction\n | SnapControllerStopSnapAction\n | SnapControllerStopAllSnapsAction\n | SnapControllerIsSnapRunningAction\n | SnapControllerHasSnapAction\n | SnapControllerGetSnapAction\n | SnapControllerUpdateSnapStateAction\n | SnapControllerClearSnapStateAction\n | SnapControllerGetSnapStateAction\n | SnapControllerGetSnapFileAction\n | SnapControllerIsMinimumPlatformVersionAction\n | SnapControllerClearStateAction\n | SnapControllerRemoveSnapAction\n | SnapControllerRemoveSnapsAction\n | SnapControllerDisconnectOriginAction\n | SnapControllerRevokeDynamicSnapPermissionsAction\n | SnapControllerGetAllSnapsAction\n | SnapControllerGetRunnableSnapsAction\n | SnapControllerGetPermittedSnapsAction\n | SnapControllerInstallSnapsAction\n | SnapControllerHandleRequestAction\n | SnapControllerSetClientActiveAction;\n"]}
@@ -1,5 +1,5 @@
1
1
  /**
2
- * This file is auto generated by `scripts/generate-method-action-types.ts`.
2
+ * This file is auto generated.
3
3
  * Do not edit manually.
4
4
  */
5
5
  import type { SnapController } from "./SnapController.cjs";
@@ -1,5 +1,5 @@
1
1
  /**
2
- * This file is auto generated by `scripts/generate-method-action-types.ts`.
2
+ * This file is auto generated.
3
3
  * Do not edit manually.
4
4
  */
5
5
  import type { SnapController } from "./SnapController.mjs";
@@ -1,5 +1,5 @@
1
1
  /**
2
- * This file is auto generated by `scripts/generate-method-action-types.ts`.
2
+ * This file is auto generated.
3
3
  * Do not edit manually.
4
4
  */
5
5
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"SnapController-method-action-types.mjs","sourceRoot":"","sources":["../../src/snaps/SnapController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { SnapController } from './SnapController';\n\n/**\n * Initialise the SnapController.\n *\n * Currently this method sets up the controller and calls the `onStart` lifecycle hook for all\n * runnable Snaps.\n *\n * @param waitForPlatform - Whether to wait for the platform to be ready before returning.\n */\nexport type SnapControllerInitAction = {\n type: `SnapController:init`;\n handler: SnapController['init'];\n};\n\n/**\n * Trigger an update of the registry.\n *\n * As a side-effect of this, preinstalled Snaps may be updated and Snaps may be blocked/unblocked.\n */\nexport type SnapControllerUpdateRegistryAction = {\n type: `SnapController:updateRegistry`;\n handler: SnapController['updateRegistry'];\n};\n\n/**\n * Enables the given snap. A snap can only be started if it is enabled. A snap\n * can only be enabled if it isn't blocked.\n *\n * @param snapId - The id of the Snap to enable.\n */\nexport type SnapControllerEnableSnapAction = {\n type: `SnapController:enableSnap`;\n handler: SnapController['enableSnap'];\n};\n\n/**\n * Disables the given snap. A snap can only be started if it is enabled.\n *\n * @param snapId - The id of the Snap to disable.\n * @returns A promise that resolves once the snap has been disabled.\n */\nexport type SnapControllerDisableSnapAction = {\n type: `SnapController:disableSnap`;\n handler: SnapController['disableSnap'];\n};\n\n/**\n * Stops the given snap, removes all hooks, closes all connections, and\n * terminates its worker.\n *\n * @param snapId - The id of the Snap to stop.\n * @param statusEvent - The Snap status event that caused the snap to be\n * stopped.\n */\nexport type SnapControllerStopSnapAction = {\n type: `SnapController:stopSnap`;\n handler: SnapController['stopSnap'];\n};\n\n/**\n * Stops all running snaps, removes all hooks, closes all connections, and\n * terminates their workers.\n *\n * @param statusEvent - The Snap status event that caused the snap to be\n * stopped.\n */\nexport type SnapControllerStopAllSnapsAction = {\n type: `SnapController:stopAllSnaps`;\n handler: SnapController['stopAllSnaps'];\n};\n\n/**\n * Returns whether the given snap is running.\n * Throws an error if the snap doesn't exist.\n *\n * @param snapId - The id of the Snap to check.\n * @returns `true` if the snap is running, otherwise `false`.\n */\nexport type SnapControllerIsSnapRunningAction = {\n type: `SnapController:isSnapRunning`;\n handler: SnapController['isSnapRunning'];\n};\n\n/**\n * Returns whether the given snap has been added to state.\n *\n * @param snapId - The id of the Snap to check for.\n * @returns `true` if the snap exists in the controller state, otherwise `false`.\n */\nexport type SnapControllerHasSnapAction = {\n type: `SnapController:hasSnap`;\n handler: SnapController['hasSnap'];\n};\n\n/**\n * Gets the snap with the given id if it exists, including all data.\n * This should not be used if the snap is to be serializable, as e.g.\n * the snap sourceCode may be quite large.\n *\n * @param snapId - The id of the Snap to get.\n * @returns The entire snap object from the controller state.\n */\nexport type SnapControllerGetSnapAction = {\n type: `SnapController:getSnap`;\n handler: SnapController['getSnap'];\n};\n\n/**\n * Updates the own state of the snap with the given id.\n * This is distinct from the state MetaMask uses to manage snaps.\n *\n * @param snapId - The id of the Snap whose state should be updated.\n * @param newSnapState - The new state of the snap.\n * @param encrypted - A flag to indicate whether to use encrypted storage or not.\n */\nexport type SnapControllerUpdateSnapStateAction = {\n type: `SnapController:updateSnapState`;\n handler: SnapController['updateSnapState'];\n};\n\n/**\n * Clears the state of the snap with the given id.\n * This is distinct from the state MetaMask uses to manage snaps.\n *\n * @param snapId - The id of the Snap whose state should be cleared.\n * @param encrypted - A flag to indicate whether to use encrypted storage or not.\n */\nexport type SnapControllerClearSnapStateAction = {\n type: `SnapController:clearSnapState`;\n handler: SnapController['clearSnapState'];\n};\n\n/**\n * Gets the own state of the snap with the given id.\n * This is distinct from the state MetaMask uses to manage snaps.\n *\n * @param snapId - The id of the Snap whose state to get.\n * @param encrypted - A flag to indicate whether to use encrypted storage or not.\n * @returns The requested snap state or null if no state exists.\n */\nexport type SnapControllerGetSnapStateAction = {\n type: `SnapController:getSnapState`;\n handler: SnapController['getSnapState'];\n};\n\n/**\n * Gets a static auxiliary snap file in a chosen file encoding.\n *\n * @param snapId - The id of the Snap whose state to get.\n * @param path - The path to the requested file.\n * @param encoding - An optional requested file encoding.\n * @returns The file requested in the chosen file encoding or null if the file is not found.\n */\nexport type SnapControllerGetSnapFileAction = {\n type: `SnapController:getSnapFile`;\n handler: SnapController['getSnapFile'];\n};\n\n/**\n * Determine if a given Snap ID supports a given minimum version of the Snaps platform\n * by inspecting the platformVersion in the Snap manifest.\n *\n * @param snapId - The Snap ID.\n * @param version - The version.\n * @returns True if the platform version is equal or greater to the passed version, false otherwise.\n */\nexport type SnapControllerIsMinimumPlatformVersionAction = {\n type: `SnapController:isMinimumPlatformVersion`;\n handler: SnapController['isMinimumPlatformVersion'];\n};\n\n/**\n * Completely clear the controller's state: delete all associated data,\n * handlers, event listeners, and permissions; tear down all snap providers.\n * Also re-initializes the controller after clearing the state.\n */\nexport type SnapControllerClearStateAction = {\n type: `SnapController:clearState`;\n handler: SnapController['clearState'];\n};\n\n/**\n * Removes the given snap from state, and clears all associated handlers\n * and listeners.\n *\n * @param snapId - The id of the Snap.\n * @returns A promise that resolves once the snap has been removed.\n */\nexport type SnapControllerRemoveSnapAction = {\n type: `SnapController:removeSnap`;\n handler: SnapController['removeSnap'];\n};\n\n/**\n * Stops the given snaps, removes them from state, and clears all associated\n * permissions, handlers, and listeners.\n *\n * @param snapIds - The ids of the Snaps.\n */\nexport type SnapControllerRemoveSnapsAction = {\n type: `SnapController:removeSnaps`;\n handler: SnapController['removeSnaps'];\n};\n\n/**\n * Disconnect the Snap from the given origin, meaning the origin can no longer\n * interact with the Snap until it is reconnected.\n *\n * @param origin - The origin from which to remove the Snap.\n * @param snapId - The id of the snap to remove.\n */\nexport type SnapControllerDisconnectOriginAction = {\n type: `SnapController:disconnectOrigin`;\n handler: SnapController['disconnectOrigin'];\n};\n\n/**\n * Checks if a list of permissions are dynamic and allowed to be revoked, if they are they will all be revoked.\n *\n * @param snapId - The snap ID.\n * @param permissionNames - The names of the permissions.\n * @throws If non-dynamic permissions are passed.\n */\nexport type SnapControllerRevokeDynamicSnapPermissionsAction = {\n type: `SnapController:revokeDynamicSnapPermissions`;\n handler: SnapController['revokeDynamicSnapPermissions'];\n};\n\n/**\n * Gets all snaps in their truncated format.\n *\n * @returns All installed snaps in their truncated format.\n */\nexport type SnapControllerGetAllSnapsAction = {\n type: `SnapController:getAllSnaps`;\n handler: SnapController['getAllSnaps'];\n};\n\n/**\n * Gets all runnable snaps.\n *\n * @returns All runnable snaps.\n */\nexport type SnapControllerGetRunnableSnapsAction = {\n type: `SnapController:getRunnableSnaps`;\n handler: SnapController['getRunnableSnaps'];\n};\n\n/**\n * Gets the serialized permitted snaps of the given origin, if any.\n *\n * @param origin - The origin whose permitted snaps to retrieve.\n * @returns The serialized permitted snaps for the origin.\n */\nexport type SnapControllerGetPermittedSnapsAction = {\n type: `SnapController:getPermittedSnaps`;\n handler: SnapController['getPermittedSnaps'];\n};\n\n/**\n * Installs the snaps requested by the given origin, returning the snap\n * object if the origin is permitted to install it, and an authorization error\n * otherwise.\n *\n * @param origin - The origin that requested to install the snaps.\n * @param requestedSnaps - The snaps to install.\n * @returns An object of snap ids and snap objects, or errors if a\n * snap couldn't be installed.\n */\nexport type SnapControllerInstallSnapsAction = {\n type: `SnapController:installSnaps`;\n handler: SnapController['installSnaps'];\n};\n\n/**\n * Passes a JSON-RPC request object to the RPC handler function of a snap.\n *\n * @param options - A bag of options.\n * @param options.snapId - The ID of the recipient snap.\n * @param options.origin - The origin of the RPC request.\n * @param options.handler - The handler to trigger on the snap for the request.\n * @param options.request - The JSON-RPC request object.\n * @returns The result of the JSON-RPC request.\n */\nexport type SnapControllerHandleRequestAction = {\n type: `SnapController:handleRequest`;\n handler: SnapController['handleRequest'];\n};\n\n/**\n * Set the active state of the client. This will trigger the `onActive` or\n * `onInactive` lifecycle hooks for all Snaps.\n *\n * @param active - A boolean indicating whether the client is active or not.\n */\nexport type SnapControllerSetClientActiveAction = {\n type: `SnapController:setClientActive`;\n handler: SnapController['setClientActive'];\n};\n\n/**\n * Union of all SnapController action types.\n */\nexport type SnapControllerMethodActions =\n | SnapControllerInitAction\n | SnapControllerUpdateRegistryAction\n | SnapControllerEnableSnapAction\n | SnapControllerDisableSnapAction\n | SnapControllerStopSnapAction\n | SnapControllerStopAllSnapsAction\n | SnapControllerIsSnapRunningAction\n | SnapControllerHasSnapAction\n | SnapControllerGetSnapAction\n | SnapControllerUpdateSnapStateAction\n | SnapControllerClearSnapStateAction\n | SnapControllerGetSnapStateAction\n | SnapControllerGetSnapFileAction\n | SnapControllerIsMinimumPlatformVersionAction\n | SnapControllerClearStateAction\n | SnapControllerRemoveSnapAction\n | SnapControllerRemoveSnapsAction\n | SnapControllerDisconnectOriginAction\n | SnapControllerRevokeDynamicSnapPermissionsAction\n | SnapControllerGetAllSnapsAction\n | SnapControllerGetRunnableSnapsAction\n | SnapControllerGetPermittedSnapsAction\n | SnapControllerInstallSnapsAction\n | SnapControllerHandleRequestAction\n | SnapControllerSetClientActiveAction;\n"]}
1
+ {"version":3,"file":"SnapController-method-action-types.mjs","sourceRoot":"","sources":["../../src/snaps/SnapController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { SnapController } from './SnapController';\n\n/**\n * Initialise the SnapController.\n *\n * Currently this method sets up the controller and calls the `onStart` lifecycle hook for all\n * runnable Snaps.\n *\n * @param waitForPlatform - Whether to wait for the platform to be ready before returning.\n */\nexport type SnapControllerInitAction = {\n type: `SnapController:init`;\n handler: SnapController['init'];\n};\n\n/**\n * Trigger an update of the registry.\n *\n * As a side-effect of this, preinstalled Snaps may be updated and Snaps may be blocked/unblocked.\n */\nexport type SnapControllerUpdateRegistryAction = {\n type: `SnapController:updateRegistry`;\n handler: SnapController['updateRegistry'];\n};\n\n/**\n * Enables the given snap. A snap can only be started if it is enabled. A snap\n * can only be enabled if it isn't blocked.\n *\n * @param snapId - The id of the Snap to enable.\n */\nexport type SnapControllerEnableSnapAction = {\n type: `SnapController:enableSnap`;\n handler: SnapController['enableSnap'];\n};\n\n/**\n * Disables the given snap. A snap can only be started if it is enabled.\n *\n * @param snapId - The id of the Snap to disable.\n * @returns A promise that resolves once the snap has been disabled.\n */\nexport type SnapControllerDisableSnapAction = {\n type: `SnapController:disableSnap`;\n handler: SnapController['disableSnap'];\n};\n\n/**\n * Stops the given snap, removes all hooks, closes all connections, and\n * terminates its worker.\n *\n * @param snapId - The id of the Snap to stop.\n * @param statusEvent - The Snap status event that caused the snap to be\n * stopped.\n */\nexport type SnapControllerStopSnapAction = {\n type: `SnapController:stopSnap`;\n handler: SnapController['stopSnap'];\n};\n\n/**\n * Stops all running snaps, removes all hooks, closes all connections, and\n * terminates their workers.\n *\n * @param statusEvent - The Snap status event that caused the snap to be\n * stopped.\n */\nexport type SnapControllerStopAllSnapsAction = {\n type: `SnapController:stopAllSnaps`;\n handler: SnapController['stopAllSnaps'];\n};\n\n/**\n * Returns whether the given snap is running.\n * Throws an error if the snap doesn't exist.\n *\n * @param snapId - The id of the Snap to check.\n * @returns `true` if the snap is running, otherwise `false`.\n */\nexport type SnapControllerIsSnapRunningAction = {\n type: `SnapController:isSnapRunning`;\n handler: SnapController['isSnapRunning'];\n};\n\n/**\n * Returns whether the given snap has been added to state.\n *\n * @param snapId - The id of the Snap to check for.\n * @returns `true` if the snap exists in the controller state, otherwise `false`.\n */\nexport type SnapControllerHasSnapAction = {\n type: `SnapController:hasSnap`;\n handler: SnapController['hasSnap'];\n};\n\n/**\n * Gets the snap with the given id if it exists, including all data.\n * This should not be used if the snap is to be serializable, as e.g.\n * the snap sourceCode may be quite large.\n *\n * @param snapId - The id of the Snap to get.\n * @returns The entire snap object from the controller state.\n */\nexport type SnapControllerGetSnapAction = {\n type: `SnapController:getSnap`;\n handler: SnapController['getSnap'];\n};\n\n/**\n * Updates the own state of the snap with the given id.\n * This is distinct from the state MetaMask uses to manage snaps.\n *\n * @param snapId - The id of the Snap whose state should be updated.\n * @param newSnapState - The new state of the snap.\n * @param encrypted - A flag to indicate whether to use encrypted storage or not.\n */\nexport type SnapControllerUpdateSnapStateAction = {\n type: `SnapController:updateSnapState`;\n handler: SnapController['updateSnapState'];\n};\n\n/**\n * Clears the state of the snap with the given id.\n * This is distinct from the state MetaMask uses to manage snaps.\n *\n * @param snapId - The id of the Snap whose state should be cleared.\n * @param encrypted - A flag to indicate whether to use encrypted storage or not.\n */\nexport type SnapControllerClearSnapStateAction = {\n type: `SnapController:clearSnapState`;\n handler: SnapController['clearSnapState'];\n};\n\n/**\n * Gets the own state of the snap with the given id.\n * This is distinct from the state MetaMask uses to manage snaps.\n *\n * @param snapId - The id of the Snap whose state to get.\n * @param encrypted - A flag to indicate whether to use encrypted storage or not.\n * @returns The requested snap state or null if no state exists.\n */\nexport type SnapControllerGetSnapStateAction = {\n type: `SnapController:getSnapState`;\n handler: SnapController['getSnapState'];\n};\n\n/**\n * Gets a static auxiliary snap file in a chosen file encoding.\n *\n * @param snapId - The id of the Snap whose state to get.\n * @param path - The path to the requested file.\n * @param encoding - An optional requested file encoding.\n * @returns The file requested in the chosen file encoding or null if the file is not found.\n */\nexport type SnapControllerGetSnapFileAction = {\n type: `SnapController:getSnapFile`;\n handler: SnapController['getSnapFile'];\n};\n\n/**\n * Determine if a given Snap ID supports a given minimum version of the Snaps platform\n * by inspecting the platformVersion in the Snap manifest.\n *\n * @param snapId - The Snap ID.\n * @param version - The version.\n * @returns True if the platform version is equal or greater to the passed version, false otherwise.\n */\nexport type SnapControllerIsMinimumPlatformVersionAction = {\n type: `SnapController:isMinimumPlatformVersion`;\n handler: SnapController['isMinimumPlatformVersion'];\n};\n\n/**\n * Completely clear the controller's state: delete all associated data,\n * handlers, event listeners, and permissions; tear down all snap providers.\n * Also re-initializes the controller after clearing the state.\n */\nexport type SnapControllerClearStateAction = {\n type: `SnapController:clearState`;\n handler: SnapController['clearState'];\n};\n\n/**\n * Removes the given snap from state, and clears all associated handlers\n * and listeners.\n *\n * @param snapId - The id of the Snap.\n * @returns A promise that resolves once the snap has been removed.\n */\nexport type SnapControllerRemoveSnapAction = {\n type: `SnapController:removeSnap`;\n handler: SnapController['removeSnap'];\n};\n\n/**\n * Stops the given snaps, removes them from state, and clears all associated\n * permissions, handlers, and listeners.\n *\n * @param snapIds - The ids of the Snaps.\n */\nexport type SnapControllerRemoveSnapsAction = {\n type: `SnapController:removeSnaps`;\n handler: SnapController['removeSnaps'];\n};\n\n/**\n * Disconnect the Snap from the given origin, meaning the origin can no longer\n * interact with the Snap until it is reconnected.\n *\n * @param origin - The origin from which to remove the Snap.\n * @param snapId - The id of the snap to remove.\n */\nexport type SnapControllerDisconnectOriginAction = {\n type: `SnapController:disconnectOrigin`;\n handler: SnapController['disconnectOrigin'];\n};\n\n/**\n * Checks if a list of permissions are dynamic and allowed to be revoked, if they are they will all be revoked.\n *\n * @param snapId - The snap ID.\n * @param permissionNames - The names of the permissions.\n * @throws If non-dynamic permissions are passed.\n */\nexport type SnapControllerRevokeDynamicSnapPermissionsAction = {\n type: `SnapController:revokeDynamicSnapPermissions`;\n handler: SnapController['revokeDynamicSnapPermissions'];\n};\n\n/**\n * Gets all snaps in their truncated format.\n *\n * @returns All installed snaps in their truncated format.\n */\nexport type SnapControllerGetAllSnapsAction = {\n type: `SnapController:getAllSnaps`;\n handler: SnapController['getAllSnaps'];\n};\n\n/**\n * Gets all runnable snaps.\n *\n * @returns All runnable snaps.\n */\nexport type SnapControllerGetRunnableSnapsAction = {\n type: `SnapController:getRunnableSnaps`;\n handler: SnapController['getRunnableSnaps'];\n};\n\n/**\n * Gets the serialized permitted snaps of the given origin, if any.\n *\n * @param origin - The origin whose permitted snaps to retrieve.\n * @returns The serialized permitted snaps for the origin.\n */\nexport type SnapControllerGetPermittedSnapsAction = {\n type: `SnapController:getPermittedSnaps`;\n handler: SnapController['getPermittedSnaps'];\n};\n\n/**\n * Installs the snaps requested by the given origin, returning the snap\n * object if the origin is permitted to install it, and an authorization error\n * otherwise.\n *\n * @param origin - The origin that requested to install the snaps.\n * @param requestedSnaps - The snaps to install.\n * @returns An object of snap ids and snap objects, or errors if a\n * snap couldn't be installed.\n */\nexport type SnapControllerInstallSnapsAction = {\n type: `SnapController:installSnaps`;\n handler: SnapController['installSnaps'];\n};\n\n/**\n * Passes a JSON-RPC request object to the RPC handler function of a snap.\n *\n * @param options - A bag of options.\n * @param options.snapId - The ID of the recipient snap.\n * @param options.origin - The origin of the RPC request.\n * @param options.handler - The handler to trigger on the snap for the request.\n * @param options.request - The JSON-RPC request object.\n * @returns The result of the JSON-RPC request.\n */\nexport type SnapControllerHandleRequestAction = {\n type: `SnapController:handleRequest`;\n handler: SnapController['handleRequest'];\n};\n\n/**\n * Set the active state of the client. This will trigger the `onActive` or\n * `onInactive` lifecycle hooks for all Snaps.\n *\n * @param active - A boolean indicating whether the client is active or not.\n */\nexport type SnapControllerSetClientActiveAction = {\n type: `SnapController:setClientActive`;\n handler: SnapController['setClientActive'];\n};\n\n/**\n * Union of all SnapController action types.\n */\nexport type SnapControllerMethodActions =\n | SnapControllerInitAction\n | SnapControllerUpdateRegistryAction\n | SnapControllerEnableSnapAction\n | SnapControllerDisableSnapAction\n | SnapControllerStopSnapAction\n | SnapControllerStopAllSnapsAction\n | SnapControllerIsSnapRunningAction\n | SnapControllerHasSnapAction\n | SnapControllerGetSnapAction\n | SnapControllerUpdateSnapStateAction\n | SnapControllerClearSnapStateAction\n | SnapControllerGetSnapStateAction\n | SnapControllerGetSnapFileAction\n | SnapControllerIsMinimumPlatformVersionAction\n | SnapControllerClearStateAction\n | SnapControllerRemoveSnapAction\n | SnapControllerRemoveSnapsAction\n | SnapControllerDisconnectOriginAction\n | SnapControllerRevokeDynamicSnapPermissionsAction\n | SnapControllerGetAllSnapsAction\n | SnapControllerGetRunnableSnapsAction\n | SnapControllerGetPermittedSnapsAction\n | SnapControllerInstallSnapsAction\n | SnapControllerHandleRequestAction\n | SnapControllerSetClientActiveAction;\n"]}