@metamask/snaps-controllers 17.2.1 → 18.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 (36) hide show
  1. package/CHANGELOG.md +38 -1
  2. package/dist/interface/SnapInterfaceController.cjs.map +1 -1
  3. package/dist/interface/SnapInterfaceController.d.cts +8 -2
  4. package/dist/interface/SnapInterfaceController.d.cts.map +1 -1
  5. package/dist/interface/SnapInterfaceController.d.mts +8 -2
  6. package/dist/interface/SnapInterfaceController.d.mts.map +1 -1
  7. package/dist/interface/SnapInterfaceController.mjs.map +1 -1
  8. package/dist/multichain/MultichainRouter.cjs +33 -10
  9. package/dist/multichain/MultichainRouter.cjs.map +1 -1
  10. package/dist/multichain/MultichainRouter.d.cts +5 -7
  11. package/dist/multichain/MultichainRouter.d.cts.map +1 -1
  12. package/dist/multichain/MultichainRouter.d.mts +5 -7
  13. package/dist/multichain/MultichainRouter.d.mts.map +1 -1
  14. package/dist/multichain/MultichainRouter.mjs +34 -11
  15. package/dist/multichain/MultichainRouter.mjs.map +1 -1
  16. package/dist/services/AbstractExecutionService.cjs +9 -13
  17. package/dist/services/AbstractExecutionService.cjs.map +1 -1
  18. package/dist/services/AbstractExecutionService.d.cts +3 -2
  19. package/dist/services/AbstractExecutionService.d.cts.map +1 -1
  20. package/dist/services/AbstractExecutionService.d.mts +3 -2
  21. package/dist/services/AbstractExecutionService.d.mts.map +1 -1
  22. package/dist/services/AbstractExecutionService.mjs +10 -14
  23. package/dist/services/AbstractExecutionService.mjs.map +1 -1
  24. package/dist/snaps/SnapController.cjs +121 -29
  25. package/dist/snaps/SnapController.cjs.map +1 -1
  26. package/dist/snaps/SnapController.d.cts +13 -17
  27. package/dist/snaps/SnapController.d.cts.map +1 -1
  28. package/dist/snaps/SnapController.d.mts +13 -17
  29. package/dist/snaps/SnapController.d.mts.map +1 -1
  30. package/dist/snaps/SnapController.mjs +121 -29
  31. package/dist/snaps/SnapController.mjs.map +1 -1
  32. package/dist/utils.d.cts +1 -0
  33. package/dist/utils.d.cts.map +1 -1
  34. package/dist/utils.d.mts +1 -0
  35. package/dist/utils.d.mts.map +1 -1
  36. package/package.json +11 -11
@@ -4,11 +4,11 @@ function $importDefault(module) {
4
4
  }
5
5
  return module;
6
6
  }
7
- import { JsonRpcEngine } from "@metamask/json-rpc-engine";
7
+ import { asV2Middleware } from "@metamask/json-rpc-engine";
8
+ import { JsonRpcEngineV2 as JsonRpcEngine } from "@metamask/json-rpc-engine/v2";
8
9
  import { createStreamMiddleware } from "@metamask/json-rpc-middleware-stream";
9
10
  import $ObjectMultiplex from "@metamask/object-multiplex";
10
11
  const ObjectMultiplex = $importDefault($ObjectMultiplex);
11
- import { JsonRpcError } from "@metamask/rpc-errors";
12
12
  import { SNAP_STREAM_NAMES, logError, logWarning } from "@metamask/snaps-utils";
13
13
  import { Duration, assertIsJsonRpcRequest, hasProperty, inMilliseconds } from "@metamask/utils";
14
14
  import { nanoid } from "nanoid";
@@ -66,7 +66,6 @@ export class AbstractExecutionService {
66
66
  const result = await withTimeout(this.#command(snapId, {
67
67
  jsonrpc: '2.0',
68
68
  method: 'terminate',
69
- params: [],
70
69
  id: nanoid(),
71
70
  }), this.#terminationTimeout);
72
71
  if (result === hasTimedOut || result !== 'OK') {
@@ -101,14 +100,15 @@ export class AbstractExecutionService {
101
100
  */
102
101
  async #initJob(snapId, timer) {
103
102
  const { streams, worker } = await this.#initStreams(snapId, timer);
104
- const rpcEngine = new JsonRpcEngine();
105
103
  const jsonRpcConnection = createStreamMiddleware();
106
104
  pipeline(jsonRpcConnection.stream, streams.command, jsonRpcConnection.stream, (error) => {
107
105
  if (error && !error.message?.match('Premature close')) {
108
106
  logError(`Command stream failure.`, error);
109
107
  }
110
108
  });
111
- rpcEngine.push(jsonRpcConnection.middleware);
109
+ const rpcEngine = JsonRpcEngine.create({
110
+ middleware: [asV2Middleware(jsonRpcConnection.middleware)],
111
+ });
112
112
  const envMetadata = {
113
113
  id: snapId,
114
114
  streams,
@@ -162,7 +162,9 @@ export class AbstractExecutionService {
162
162
  }
163
163
  };
164
164
  commandStream.on('data', notificationHandler);
165
- const rpcStream = mux.createStream(SNAP_STREAM_NAMES.JSON_RPC);
165
+ const rpcStream = mux
166
+ .createStream(SNAP_STREAM_NAMES.JSON_RPC)
167
+ .setMaxListeners(20);
166
168
  rpcStream.on('data', (chunk) => {
167
169
  if (chunk?.data && hasProperty(chunk?.data, 'id')) {
168
170
  this.#messenger.publish('ExecutionService:outboundRequest', snapId);
@@ -261,13 +263,7 @@ export class AbstractExecutionService {
261
263
  throw new Error(`"${snapId}" is not currently running.`);
262
264
  }
263
265
  log('Parent: Sending Command', message);
264
- const response = await job.rpcEngine.handle(message);
265
- // We don't need full validation of the response here because we control it.
266
- if (hasProperty(response, 'error')) {
267
- const error = response.error;
268
- throw new JsonRpcError(error.code, error.message, error.data);
269
- }
270
- return response.result;
266
+ return await job.rpcEngine.handle(message);
271
267
  }
272
268
  /**
273
269
  * Handle RPC request.
@@ -283,10 +279,10 @@ export class AbstractExecutionService {
283
279
  jsonrpc: '2.0',
284
280
  method: 'snapRpc',
285
281
  params: {
282
+ snapId,
286
283
  origin,
287
284
  handler,
288
285
  request: request,
289
- target: snapId,
290
286
  },
291
287
  });
292
288
  }
@@ -1 +1 @@
1
- {"version":3,"file":"AbstractExecutionService.mjs","sourceRoot":"","sources":["../../src/services/AbstractExecutionService.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,aAAa,EAAE,kCAAkC;AAC1D,OAAO,EAAE,sBAAsB,EAAE,6CAA6C;AAC9E,OAAO,gBAAe,mCAAmC;;AAEzD,OAAO,EAAE,YAAY,EAAE,6BAA6B;AAEpD,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,UAAU,EAAE,8BAA8B;AAOhF,OAAO,EACL,QAAQ,EACR,sBAAsB,EACtB,WAAW,EACX,cAAc,EACf,wBAAwB;AACzB,OAAO,EAAE,MAAM,EAAE,eAAe;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d,MAAM,EAAE,SAAS;YACjB,MAAM,EAAE;gBACN,MAAM;gBACN,OAAO;gBACP,OAAO,EAAE,OAAyB;gBAClC,MAAM,EAAE,MAAM;aACf;SACF,CAAC,CAAC;IACL,CAAC;CACF;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAC5B,gBAAwB,EACxB,UAAkB;IAElB,MAAM,GAAG,GAAG,IAAI,eAAe,EAAE,CAAC;IAClC,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE,gBAAgB,EAAE,CAAC,KAAK,EAAE,EAAE;QAC1D,IAAI,KAAK,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,iBAAiB,CAAC,EAAE,CAAC;YACtD,QAAQ,CAAC,IAAI,UAAU,mBAAmB,EAAE,KAAK,CAAC,CAAC;QACrD,CAAC;IACH,CAAC,CAAC,CAAC;IACH,OAAO,GAAG,CAAC;AACb,CAAC","sourcesContent":["import { JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport { createStreamMiddleware } from '@metamask/json-rpc-middleware-stream';\nimport ObjectMultiplex from '@metamask/object-multiplex';\nimport type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { JsonRpcError } from '@metamask/rpc-errors';\nimport type { SnapRpcHookArgs } from '@metamask/snaps-utils';\nimport { SNAP_STREAM_NAMES, logError, logWarning } from '@metamask/snaps-utils';\nimport type {\n Json,\n JsonRpcError as JsonRpcErrorType,\n JsonRpcNotification,\n JsonRpcRequest,\n} from '@metamask/utils';\nimport {\n Duration,\n assertIsJsonRpcRequest,\n hasProperty,\n inMilliseconds,\n} from '@metamask/utils';\nimport { nanoid } from 'nanoid';\nimport { pipeline } from 'readable-stream';\nimport type { Duplex } from 'readable-stream';\n\nimport type {\n ExecutionService,\n ExecutionServiceMessenger,\n SnapErrorJson,\n SnapExecutionData,\n} from './ExecutionService';\nimport { log } from '../logging';\nimport { Timer } from '../snaps/Timer';\nimport { hasTimedOut, withTimeout } from '../utils';\n\nconst controllerName = 'ExecutionService';\n\nexport type SetupSnapProvider = (snapId: string, stream: Duplex) => void;\n\nexport type ExecutionServiceArgs = {\n setupSnapProvider: SetupSnapProvider;\n messenger: ExecutionServiceMessenger;\n initTimeout?: number;\n pingTimeout?: number;\n terminationTimeout?: number;\n usePing?: boolean;\n};\n\nexport type JobStreams = {\n command: Duplex;\n rpc: Duplex;\n connection: BasePostMessageStream;\n mux: ObjectMultiplex;\n};\n\nexport type Job<WorkerType> = {\n id: string;\n streams: JobStreams;\n rpcEngine: JsonRpcEngine;\n worker: WorkerType;\n};\n\nexport type TerminateJobArgs<WorkerType> = Partial<Job<WorkerType>> &\n Pick<Job<WorkerType>, 'id'>;\n\n/** \n Statuses used for diagnostic purposes\n - created: The initial state, no initialization has started \n - initializing: Snap execution environment is initializing\n - initialized: Snap execution environment has initialized\n - executing: Snap source code is being executed\n - running: Snap executed and ready for RPC requests\n */\ntype ExecutionStatus =\n | 'created'\n | 'initializing'\n | 'initialized'\n | 'executing'\n | 'running';\n\nexport abstract class AbstractExecutionService<WorkerType>\n implements ExecutionService\n{\n name: typeof controllerName = controllerName;\n\n state = null;\n\n readonly #jobs: Map<string, Job<WorkerType>>;\n\n readonly #status: Map<string, ExecutionStatus>;\n\n readonly #setupSnapProvider: SetupSnapProvider;\n\n readonly #messenger: ExecutionServiceMessenger;\n\n readonly #initTimeout: number;\n\n readonly #pingTimeout: number;\n\n readonly #terminationTimeout: number;\n\n readonly #usePing: boolean;\n\n constructor({\n setupSnapProvider,\n messenger,\n initTimeout = inMilliseconds(60, Duration.Second),\n pingTimeout = inMilliseconds(10, Duration.Second),\n terminationTimeout = inMilliseconds(1, Duration.Second),\n usePing = true,\n }: ExecutionServiceArgs) {\n this.#jobs = new Map();\n this.#status = new Map();\n this.#setupSnapProvider = setupSnapProvider;\n this.#messenger = messenger;\n this.#initTimeout = initTimeout;\n this.#pingTimeout = pingTimeout;\n this.#terminationTimeout = terminationTimeout;\n this.#usePing = usePing;\n\n this.#registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering the controller's messaging system\n * actions.\n */\n #registerMessageHandlers(): void {\n this.#messenger.registerActionHandler(\n `${controllerName}:handleRpcRequest`,\n async (snapId: string, options: SnapRpcHookArgs) =>\n this.handleRpcRequest(snapId, options),\n );\n\n this.#messenger.registerActionHandler(\n `${controllerName}:executeSnap`,\n async (data: SnapExecutionData) => this.executeSnap(data),\n );\n\n this.#messenger.registerActionHandler(\n `${controllerName}:terminateSnap`,\n async (snapId: string) => this.terminateSnap(snapId),\n );\n\n this.#messenger.registerActionHandler(\n `${controllerName}:terminateAllSnaps`,\n async () => this.terminateAllSnaps(),\n );\n }\n\n /**\n * Performs additional necessary work during job termination. **MUST** be\n * implemented by concrete implementations. See\n * {@link AbstractExecutionService.terminate} for details.\n *\n * @param job - The object corresponding to the job to be terminated.\n */\n protected abstract terminateJob(\n job: TerminateJobArgs<WorkerType>,\n ): Promise<void>;\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 */\n public async terminateSnap(snapId: string): Promise<void> {\n const job = this.#jobs.get(snapId);\n if (!job) {\n return;\n }\n\n try {\n // Ping worker and tell it to run teardown, continue with termination if it takes too long\n const result = await withTimeout(\n this.#command(snapId, {\n jsonrpc: '2.0',\n method: 'terminate',\n params: [],\n id: nanoid(),\n }),\n this.#terminationTimeout,\n );\n\n if (result === hasTimedOut || result !== 'OK') {\n logWarning(`Snap \"${snapId}\" failed to terminate gracefully.`);\n }\n } catch {\n // Ignore\n }\n\n Object.values(job.streams).forEach((stream) => {\n try {\n if (!stream.destroyed) {\n stream.destroy();\n }\n } catch (error) {\n logError('Error while destroying stream', error);\n }\n });\n\n await this.terminateJob(job);\n\n this.#jobs.delete(snapId);\n this.#status.delete(snapId);\n log(`Snap \"${snapId}\" terminated.`);\n }\n\n /**\n * Initiates a job for a Snap.\n *\n * @param snapId - The ID of the Snap to initiate a job for.\n * @param timer - The timer to use for timeouts.\n * @returns Information regarding the created job.\n * @throws If the execution service returns an error or execution times out.\n */\n async #initJob(snapId: string, timer: Timer): Promise<Job<WorkerType>> {\n const { streams, worker } = await this.#initStreams(snapId, timer);\n const rpcEngine = new JsonRpcEngine();\n\n const jsonRpcConnection = createStreamMiddleware();\n\n pipeline(\n jsonRpcConnection.stream,\n streams.command,\n jsonRpcConnection.stream,\n (error) => {\n if (error && !error.message?.match('Premature close')) {\n logError(`Command stream failure.`, error);\n }\n },\n );\n\n rpcEngine.push(jsonRpcConnection.middleware);\n\n const envMetadata = {\n id: snapId,\n streams,\n rpcEngine,\n worker,\n };\n this.#jobs.set(snapId, envMetadata);\n\n return envMetadata;\n }\n\n /**\n * Sets up the streams for an initiated job.\n *\n * @param snapId - The Snap ID.\n * @param timer - The timer to use for timeouts.\n * @returns The streams to communicate with the worker and the worker itself.\n * @throws If the execution service returns an error or execution times out.\n */\n async #initStreams(\n snapId: string,\n timer: Timer,\n ): Promise<{ streams: JobStreams; worker: WorkerType }> {\n const result = await withTimeout(this.initEnvStream(snapId), timer);\n\n if (result === hasTimedOut) {\n // For certain environments, such as the iframe we may have already created the worker and wish to terminate it.\n await this.terminateJob({ id: snapId });\n\n const status = this.#status.get(snapId);\n if (status === 'created') {\n // Currently this error can only be thrown by OffscreenExecutionService.\n throw new Error(\n `The executor for \"${snapId}\" couldn't start initialization. The offscreen document may not exist.`,\n );\n }\n throw new Error(\n `The executor for \"${snapId}\" failed to initialize. The iframe/webview/worker failed to load.`,\n );\n }\n\n const { worker, stream: envStream } = result;\n const mux = setupMultiplex(envStream, `Snap: \"${snapId}\"`);\n const commandStream = mux.createStream(SNAP_STREAM_NAMES.COMMAND);\n\n // Handle out-of-band errors, i.e. errors thrown from the Snap outside of the req/res cycle.\n // Also keep track of outbound request/responses\n const notificationHandler = (\n message:\n | JsonRpcRequest\n | JsonRpcNotification<Json[] | Record<string, Json>>,\n ) => {\n if (hasProperty(message, 'id')) {\n return;\n }\n\n if (message.method === 'OutboundRequest') {\n this.#messenger.publish('ExecutionService:outboundRequest', snapId);\n } else if (message.method === 'OutboundResponse') {\n this.#messenger.publish('ExecutionService:outboundResponse', snapId);\n } else if (message.method === 'UnhandledError') {\n this.#messenger.publish(\n 'ExecutionService:unhandledError',\n snapId,\n (message.params as { error: SnapErrorJson }).error,\n );\n commandStream.removeListener('data', notificationHandler);\n } else {\n logError(\n new Error(\n `Received unexpected command stream notification \"${message.method}\".`,\n ),\n );\n }\n };\n\n commandStream.on('data', notificationHandler);\n\n const rpcStream = mux.createStream(SNAP_STREAM_NAMES.JSON_RPC);\n\n rpcStream.on('data', (chunk) => {\n if (chunk?.data && hasProperty(chunk?.data, 'id')) {\n this.#messenger.publish('ExecutionService:outboundRequest', snapId);\n }\n });\n\n const originalWrite = rpcStream.write.bind(rpcStream);\n\n // @ts-expect-error Hack to inspect the messages being written to the stream.\n rpcStream.write = (chunk, encoding, callback) => {\n // Ignore chain switching notifications as it doesn't matter for the SnapProvider.\n if (chunk?.data?.method === 'metamask_chainChanged') {\n return true;\n }\n\n if (chunk?.data && hasProperty(chunk?.data, 'id')) {\n this.#messenger.publish('ExecutionService:outboundResponse', snapId);\n }\n\n return originalWrite(chunk, encoding, callback);\n };\n\n return {\n streams: {\n command: commandStream,\n rpc: rpcStream,\n connection: envStream,\n mux,\n },\n worker,\n };\n }\n\n /**\n * Abstract function implemented by implementing class that spins up a new worker for a job.\n *\n * Depending on the execution environment, this may run forever if the Snap fails to start up properly, therefore any call to this function should be wrapped in a timeout.\n */\n protected abstract initEnvStream(snapId: string): Promise<{\n worker: WorkerType;\n stream: BasePostMessageStream;\n }>;\n\n /**\n * Set the execution status of the Snap.\n *\n * @param snapId - The Snap ID.\n * @param status - The current execution status.\n */\n protected setSnapStatus(snapId: string, status: ExecutionStatus) {\n this.#status.set(snapId, status);\n }\n\n async terminateAllSnaps() {\n await Promise.all(\n [...this.#jobs.keys()].map(async (snapId) => this.terminateSnap(snapId)),\n );\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 */\n async executeSnap({\n snapId,\n sourceCode,\n endowments,\n }: SnapExecutionData): Promise<string> {\n if (this.#jobs.has(snapId)) {\n throw new Error(`\"${snapId}\" is already running.`);\n }\n\n this.setSnapStatus(snapId, 'created');\n\n const timer = new Timer(this.#initTimeout);\n\n // This may resolve even if the environment has failed to start up fully\n const job = await this.#initJob(snapId, timer);\n\n // Certain environments use ping as part of their initialization and thus can skip it here\n if (this.#usePing) {\n // Ping the worker to ensure that it started up\n const pingResult = await withTimeout(\n this.#command(job.id, {\n jsonrpc: '2.0',\n method: 'ping',\n id: nanoid(),\n }),\n this.#pingTimeout,\n );\n\n if (pingResult === hasTimedOut) {\n throw new Error(\n `The executor for \"${snapId}\" was unreachable. The executor did not respond in time.`,\n );\n }\n }\n\n const rpcStream = job.streams.rpc;\n\n this.#setupSnapProvider(snapId, rpcStream);\n\n // Use the remaining time as the timer, but ensure that the\n // Snap gets at least half the init timeout.\n const remainingTime = Math.max(timer.remaining, this.#initTimeout / 2);\n\n this.setSnapStatus(snapId, 'initialized');\n\n const request = {\n jsonrpc: '2.0',\n method: 'executeSnap',\n params: { snapId, sourceCode, endowments },\n id: nanoid(),\n };\n\n assertIsJsonRpcRequest(request);\n\n this.setSnapStatus(snapId, 'executing');\n\n const result = await withTimeout(\n this.#command(job.id, request),\n remainingTime,\n );\n\n if (result === hasTimedOut) {\n throw new Error(`${snapId} failed to start.`);\n }\n\n if (result === 'OK') {\n this.setSnapStatus(snapId, 'running');\n }\n\n return result as string;\n }\n\n async #command(\n snapId: string,\n message: JsonRpcRequest,\n ): Promise<Json | undefined> {\n const job = this.#jobs.get(snapId);\n if (!job) {\n throw new Error(`\"${snapId}\" is not currently running.`);\n }\n\n log('Parent: Sending Command', message);\n const response = await job.rpcEngine.handle(message);\n\n // We don't need full validation of the response here because we control it.\n if (hasProperty(response, 'error')) {\n const error = response.error as JsonRpcErrorType;\n throw new JsonRpcError(error.code, error.message, error.data);\n }\n\n return response.result;\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 */\n public async handleRpcRequest(\n snapId: string,\n options: SnapRpcHookArgs,\n ): Promise<unknown> {\n const { handler, request, origin } = options;\n\n return await this.#command(snapId, {\n id: nanoid(),\n jsonrpc: '2.0',\n method: 'snapRpc',\n params: {\n origin,\n handler,\n request: request as JsonRpcRequest,\n target: snapId,\n },\n });\n }\n}\n\n/**\n * Sets up stream multiplexing for the given stream.\n *\n * @param connectionStream - The stream to mux.\n * @param streamName - The name of the stream, for identification in errors.\n * @returns The multiplexed stream.\n */\nexport function setupMultiplex(\n connectionStream: Duplex,\n streamName: string,\n): ObjectMultiplex {\n const mux = new ObjectMultiplex();\n pipeline(connectionStream, mux, connectionStream, (error) => {\n if (error && !error.message?.match('Premature close')) {\n logError(`\"${streamName}\" stream failure.`, error);\n }\n });\n return mux;\n}\n"]}
1
+ {"version":3,"file":"AbstractExecutionService.mjs","sourceRoot":"","sources":["../../src/services/AbstractExecutionService.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,EAAE,eAAe,IAAI,aAAa,EAAE,qCAAqC;AAChF,OAAO,EAAE,sBAAsB,EAAE,6CAA6C;AAC9E,OAAO,gBAAe,mCAAmC;;AAGzD,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,UAAU,EAAE,8BAA8B;AAMhF,OAAO,EACL,QAAQ,EACR,sBAAsB,EACtB,WAAW,EACX,cAAc,EACf,wBAAwB;AACzB,OAAO,EAAE,M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CAAC;IAClC,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE,gBAAgB,EAAE,CAAC,KAAK,EAAE,EAAE;QAC1D,IAAI,KAAK,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,iBAAiB,CAAC,EAAE,CAAC;YACtD,QAAQ,CAAC,IAAI,UAAU,mBAAmB,EAAE,KAAK,CAAC,CAAC;QACrD,CAAC;IACH,CAAC,CAAC,CAAC;IACH,OAAO,GAAG,CAAC;AACb,CAAC","sourcesContent":["import { asV2Middleware } from '@metamask/json-rpc-engine';\nimport { JsonRpcEngineV2 as JsonRpcEngine } from '@metamask/json-rpc-engine/v2';\nimport { createStreamMiddleware } from '@metamask/json-rpc-middleware-stream';\nimport ObjectMultiplex from '@metamask/object-multiplex';\nimport type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport type { SnapRpcHookArgs } from '@metamask/snaps-utils';\nimport { SNAP_STREAM_NAMES, logError, logWarning } from '@metamask/snaps-utils';\nimport type {\n Json,\n JsonRpcNotification,\n JsonRpcRequest,\n} from '@metamask/utils';\nimport {\n Duration,\n assertIsJsonRpcRequest,\n hasProperty,\n inMilliseconds,\n} from '@metamask/utils';\nimport { nanoid } from 'nanoid';\nimport { pipeline } from 'readable-stream';\nimport type { Duplex } from 'readable-stream';\n\nimport type {\n ExecutionService,\n ExecutionServiceMessenger,\n SnapErrorJson,\n SnapExecutionData,\n} from './ExecutionService';\nimport { log } from '../logging';\nimport { Timer } from '../snaps/Timer';\nimport { hasTimedOut, withTimeout } from '../utils';\n\nconst controllerName = 'ExecutionService';\n\nexport type SetupSnapProvider = (snapId: string, stream: Duplex) => void;\n\nexport type ExecutionServiceArgs = {\n setupSnapProvider: SetupSnapProvider;\n messenger: ExecutionServiceMessenger;\n initTimeout?: number;\n pingTimeout?: number;\n terminationTimeout?: number;\n usePing?: boolean;\n};\n\nexport type JobStreams = {\n command: Duplex;\n rpc: Duplex;\n connection: BasePostMessageStream;\n mux: ObjectMultiplex;\n};\n\nexport type Job<WorkerType> = {\n id: string;\n streams: JobStreams;\n rpcEngine: JsonRpcEngine<JsonRpcRequest>;\n worker: WorkerType;\n};\n\nexport type TerminateJobArgs<WorkerType> = Partial<Job<WorkerType>> &\n Pick<Job<WorkerType>, 'id'>;\n\n/** \n Statuses used for diagnostic purposes\n - created: The initial state, no initialization has started \n - initializing: Snap execution environment is initializing\n - initialized: Snap execution environment has initialized\n - executing: Snap source code is being executed\n - running: Snap executed and ready for RPC requests\n */\ntype ExecutionStatus =\n | 'created'\n | 'initializing'\n | 'initialized'\n | 'executing'\n | 'running';\n\nexport abstract class AbstractExecutionService<WorkerType>\n implements ExecutionService\n{\n name: typeof controllerName = controllerName;\n\n state = null;\n\n readonly #jobs: Map<string, Job<WorkerType>>;\n\n readonly #status: Map<string, ExecutionStatus>;\n\n readonly #setupSnapProvider: SetupSnapProvider;\n\n readonly #messenger: ExecutionServiceMessenger;\n\n readonly #initTimeout: number;\n\n readonly #pingTimeout: number;\n\n readonly #terminationTimeout: number;\n\n readonly #usePing: boolean;\n\n constructor({\n setupSnapProvider,\n messenger,\n initTimeout = inMilliseconds(60, Duration.Second),\n pingTimeout = inMilliseconds(10, Duration.Second),\n terminationTimeout = inMilliseconds(1, Duration.Second),\n usePing = true,\n }: ExecutionServiceArgs) {\n this.#jobs = new Map();\n this.#status = new Map();\n this.#setupSnapProvider = setupSnapProvider;\n this.#messenger = messenger;\n this.#initTimeout = initTimeout;\n this.#pingTimeout = pingTimeout;\n this.#terminationTimeout = terminationTimeout;\n this.#usePing = usePing;\n\n this.#registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering the controller's messaging system\n * actions.\n */\n #registerMessageHandlers(): void {\n this.#messenger.registerActionHandler(\n `${controllerName}:handleRpcRequest`,\n async (snapId: string, options: SnapRpcHookArgs) =>\n this.handleRpcRequest(snapId, options),\n );\n\n this.#messenger.registerActionHandler(\n `${controllerName}:executeSnap`,\n async (data: SnapExecutionData) => this.executeSnap(data),\n );\n\n this.#messenger.registerActionHandler(\n `${controllerName}:terminateSnap`,\n async (snapId: string) => this.terminateSnap(snapId),\n );\n\n this.#messenger.registerActionHandler(\n `${controllerName}:terminateAllSnaps`,\n async () => this.terminateAllSnaps(),\n );\n }\n\n /**\n * Performs additional necessary work during job termination. **MUST** be\n * implemented by concrete implementations. See\n * {@link AbstractExecutionService.terminate} for details.\n *\n * @param job - The object corresponding to the job to be terminated.\n */\n protected abstract terminateJob(\n job: TerminateJobArgs<WorkerType>,\n ): Promise<void>;\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 */\n public async terminateSnap(snapId: string): Promise<void> {\n const job = this.#jobs.get(snapId);\n if (!job) {\n return;\n }\n\n try {\n // Ping worker and tell it to run teardown, continue with termination if it takes too long\n const result = await withTimeout(\n this.#command(snapId, {\n jsonrpc: '2.0',\n method: 'terminate',\n id: nanoid(),\n }),\n this.#terminationTimeout,\n );\n\n if (result === hasTimedOut || result !== 'OK') {\n logWarning(`Snap \"${snapId}\" failed to terminate gracefully.`);\n }\n } catch {\n // Ignore\n }\n\n Object.values(job.streams).forEach((stream) => {\n try {\n if (!stream.destroyed) {\n stream.destroy();\n }\n } catch (error) {\n logError('Error while destroying stream', error);\n }\n });\n\n await this.terminateJob(job);\n\n this.#jobs.delete(snapId);\n this.#status.delete(snapId);\n log(`Snap \"${snapId}\" terminated.`);\n }\n\n /**\n * Initiates a job for a Snap.\n *\n * @param snapId - The ID of the Snap to initiate a job for.\n * @param timer - The timer to use for timeouts.\n * @returns Information regarding the created job.\n * @throws If the execution service returns an error or execution times out.\n */\n async #initJob(snapId: string, timer: Timer): Promise<Job<WorkerType>> {\n const { streams, worker } = await this.#initStreams(snapId, timer);\n\n const jsonRpcConnection = createStreamMiddleware();\n\n pipeline(\n jsonRpcConnection.stream,\n streams.command,\n jsonRpcConnection.stream,\n (error) => {\n if (error && !error.message?.match('Premature close')) {\n logError(`Command stream failure.`, error);\n }\n },\n );\n\n const rpcEngine = JsonRpcEngine.create({\n middleware: [asV2Middleware(jsonRpcConnection.middleware)],\n });\n\n const envMetadata = {\n id: snapId,\n streams,\n rpcEngine,\n worker,\n };\n this.#jobs.set(snapId, envMetadata);\n\n return envMetadata;\n }\n\n /**\n * Sets up the streams for an initiated job.\n *\n * @param snapId - The Snap ID.\n * @param timer - The timer to use for timeouts.\n * @returns The streams to communicate with the worker and the worker itself.\n * @throws If the execution service returns an error or execution times out.\n */\n async #initStreams(\n snapId: string,\n timer: Timer,\n ): Promise<{ streams: JobStreams; worker: WorkerType }> {\n const result = await withTimeout(this.initEnvStream(snapId), timer);\n\n if (result === hasTimedOut) {\n // For certain environments, such as the iframe we may have already created the worker and wish to terminate it.\n await this.terminateJob({ id: snapId });\n\n const status = this.#status.get(snapId);\n if (status === 'created') {\n // Currently this error can only be thrown by OffscreenExecutionService.\n throw new Error(\n `The executor for \"${snapId}\" couldn't start initialization. The offscreen document may not exist.`,\n );\n }\n throw new Error(\n `The executor for \"${snapId}\" failed to initialize. The iframe/webview/worker failed to load.`,\n );\n }\n\n const { worker, stream: envStream } = result;\n const mux = setupMultiplex(envStream, `Snap: \"${snapId}\"`);\n const commandStream = mux.createStream(SNAP_STREAM_NAMES.COMMAND);\n\n // Handle out-of-band errors, i.e. errors thrown from the Snap outside of the req/res cycle.\n // Also keep track of outbound request/responses\n const notificationHandler = (\n message:\n | JsonRpcRequest\n | JsonRpcNotification<Json[] | Record<string, Json>>,\n ) => {\n if (hasProperty(message, 'id')) {\n return;\n }\n\n if (message.method === 'OutboundRequest') {\n this.#messenger.publish('ExecutionService:outboundRequest', snapId);\n } else if (message.method === 'OutboundResponse') {\n this.#messenger.publish('ExecutionService:outboundResponse', snapId);\n } else if (message.method === 'UnhandledError') {\n this.#messenger.publish(\n 'ExecutionService:unhandledError',\n snapId,\n (message.params as { error: SnapErrorJson }).error,\n );\n commandStream.removeListener('data', notificationHandler);\n } else {\n logError(\n new Error(\n `Received unexpected command stream notification \"${message.method}\".`,\n ),\n );\n }\n };\n\n commandStream.on('data', notificationHandler);\n\n const rpcStream = mux\n .createStream(SNAP_STREAM_NAMES.JSON_RPC)\n .setMaxListeners(20);\n\n rpcStream.on('data', (chunk) => {\n if (chunk?.data && hasProperty(chunk?.data, 'id')) {\n this.#messenger.publish('ExecutionService:outboundRequest', snapId);\n }\n });\n\n const originalWrite = rpcStream.write.bind(rpcStream);\n\n // @ts-expect-error Hack to inspect the messages being written to the stream.\n rpcStream.write = (chunk, encoding, callback) => {\n // Ignore chain switching notifications as it doesn't matter for the SnapProvider.\n if (chunk?.data?.method === 'metamask_chainChanged') {\n return true;\n }\n\n if (chunk?.data && hasProperty(chunk?.data, 'id')) {\n this.#messenger.publish('ExecutionService:outboundResponse', snapId);\n }\n\n return originalWrite(chunk, encoding, callback);\n };\n\n return {\n streams: {\n command: commandStream,\n rpc: rpcStream,\n connection: envStream,\n mux,\n },\n worker,\n };\n }\n\n /**\n * Abstract function implemented by implementing class that spins up a new worker for a job.\n *\n * Depending on the execution environment, this may run forever if the Snap fails to start up properly, therefore any call to this function should be wrapped in a timeout.\n */\n protected abstract initEnvStream(snapId: string): Promise<{\n worker: WorkerType;\n stream: BasePostMessageStream;\n }>;\n\n /**\n * Set the execution status of the Snap.\n *\n * @param snapId - The Snap ID.\n * @param status - The current execution status.\n */\n protected setSnapStatus(snapId: string, status: ExecutionStatus) {\n this.#status.set(snapId, status);\n }\n\n async terminateAllSnaps() {\n await Promise.all(\n [...this.#jobs.keys()].map(async (snapId) => this.terminateSnap(snapId)),\n );\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 */\n async executeSnap({\n snapId,\n sourceCode,\n endowments,\n }: SnapExecutionData): Promise<string> {\n if (this.#jobs.has(snapId)) {\n throw new Error(`\"${snapId}\" is already running.`);\n }\n\n this.setSnapStatus(snapId, 'created');\n\n const timer = new Timer(this.#initTimeout);\n\n // This may resolve even if the environment has failed to start up fully\n const job = await this.#initJob(snapId, timer);\n\n // Certain environments use ping as part of their initialization and thus can skip it here\n if (this.#usePing) {\n // Ping the worker to ensure that it started up\n const pingResult = await withTimeout(\n this.#command(job.id, {\n jsonrpc: '2.0',\n method: 'ping',\n id: nanoid(),\n }),\n this.#pingTimeout,\n );\n\n if (pingResult === hasTimedOut) {\n throw new Error(\n `The executor for \"${snapId}\" was unreachable. The executor did not respond in time.`,\n );\n }\n }\n\n const rpcStream = job.streams.rpc;\n\n this.#setupSnapProvider(snapId, rpcStream);\n\n // Use the remaining time as the timer, but ensure that the\n // Snap gets at least half the init timeout.\n const remainingTime = Math.max(timer.remaining, this.#initTimeout / 2);\n\n this.setSnapStatus(snapId, 'initialized');\n\n const request = {\n jsonrpc: '2.0',\n method: 'executeSnap',\n params: { snapId, sourceCode, endowments },\n id: nanoid(),\n };\n\n assertIsJsonRpcRequest(request);\n\n this.setSnapStatus(snapId, 'executing');\n\n const result = await withTimeout(\n this.#command(job.id, request),\n remainingTime,\n );\n\n if (result === hasTimedOut) {\n throw new Error(`${snapId} failed to start.`);\n }\n\n if (result === 'OK') {\n this.setSnapStatus(snapId, 'running');\n }\n\n return result as string;\n }\n\n async #command(\n snapId: string,\n message: JsonRpcRequest,\n ): Promise<Json | undefined> {\n const job = this.#jobs.get(snapId);\n if (!job) {\n throw new Error(`\"${snapId}\" is not currently running.`);\n }\n\n log('Parent: Sending Command', message);\n return await job.rpcEngine.handle(message);\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 */\n public async handleRpcRequest(\n snapId: string,\n options: SnapRpcHookArgs,\n ): Promise<unknown> {\n const { handler, request, origin } = options;\n\n return await this.#command(snapId, {\n id: nanoid(),\n jsonrpc: '2.0',\n method: 'snapRpc',\n params: {\n snapId,\n origin,\n handler,\n request: request as JsonRpcRequest,\n },\n });\n }\n}\n\n/**\n * Sets up stream multiplexing for the given stream.\n *\n * @param connectionStream - The stream to mux.\n * @param streamName - The name of the stream, for identification in errors.\n * @returns The multiplexed stream.\n */\nexport function setupMultiplex(\n connectionStream: Duplex,\n streamName: string,\n): ObjectMultiplex {\n const mux = new ObjectMultiplex();\n pipeline(connectionStream, mux, connectionStream, (error) => {\n if (error && !error.message?.match('Premature close')) {\n logError(`\"${streamName}\" stream failure.`, error);\n }\n });\n return mux;\n}\n"]}
@@ -61,7 +61,6 @@ function truncateSnap(snap) {
61
61
  * - Start: Initializes the snap in its SES realm with the authorized permissions.
62
62
  */
63
63
  class SnapController extends base_controller_1.BaseController {
64
- #closeAllConnections;
65
64
  #dynamicPermissions;
66
65
  #environmentEndowmentPermissions;
67
66
  #excludedPermissions;
@@ -85,7 +84,12 @@ class SnapController extends base_controller_1.BaseController {
85
84
  #trackEvent;
86
85
  #trackSnapExport;
87
86
  #ensureOnboardingComplete;
88
- constructor({ closeAllConnections, messenger, state, dynamicPermissions = ['endowment:caip25', 'wallet_snap'], environmentEndowmentPermissions = [], excludedPermissions = {}, idleTimeCheckInterval = (0, utils_1.inMilliseconds)(5, utils_1.Duration.Second), maxIdleTime = (0, utils_1.inMilliseconds)(30, utils_1.Duration.Second), maxRequestTime = (0, utils_1.inMilliseconds)(60, utils_1.Duration.Second), fetchFunction = globalThis.fetch.bind(undefined), featureFlags = {}, detectSnapLocation: detectSnapLocationFunction = location_1.detectSnapLocation, preinstalledSnaps = null, encryptor, getMnemonicSeed, getFeatureFlags = () => ({}), clientCryptography, trackEvent, ensureOnboardingComplete, }) {
87
+ // A promise that resolves when the controller has finished setting up.
88
+ // This is used to ensure that the controller is ready to be used.
89
+ // It is resolved when the controller has finished setting up and the platform is ready.
90
+ // It is rejected if there is an error during setup.
91
+ #controllerSetup = (0, utils_1.createDeferredPromise)();
92
+ constructor({ messenger, state, dynamicPermissions = ['endowment:caip25', 'wallet_snap'], environmentEndowmentPermissions = [], excludedPermissions = {}, idleTimeCheckInterval = (0, utils_1.inMilliseconds)(5, utils_1.Duration.Second), maxIdleTime = (0, utils_1.inMilliseconds)(30, utils_1.Duration.Second), maxRequestTime = (0, utils_1.inMilliseconds)(60, utils_1.Duration.Second), fetchFunction = globalThis.fetch.bind(undefined), featureFlags = {}, detectSnapLocation: detectSnapLocationFunction = location_1.detectSnapLocation, preinstalledSnaps = null, encryptor, getMnemonicSeed, getFeatureFlags = () => ({}), clientCryptography, trackEvent, ensureOnboardingComplete, }) {
89
93
  super({
90
94
  messenger,
91
95
  metadata: {
@@ -112,7 +116,6 @@ class SnapController extends base_controller_1.BaseController {
112
116
  // Delete larger snap properties
113
117
  return Object.values(snaps).reduce((acc, snap) => {
114
118
  const snapCopy = { ...snap };
115
- delete snapCopy.sourceCode;
116
119
  delete snapCopy.auxiliaryFiles;
117
120
  acc[snap.id] = snapCopy;
118
121
  return acc;
@@ -147,7 +150,6 @@ class SnapController extends base_controller_1.BaseController {
147
150
  ...state,
148
151
  },
149
152
  });
150
- this.#closeAllConnections = closeAllConnections;
151
153
  this.#dynamicPermissions = dynamicPermissions;
152
154
  this.#environmentEndowmentPermissions = environmentEndowmentPermissions;
153
155
  this.#excludedPermissions = excludedPermissions;
@@ -186,12 +188,14 @@ class SnapController extends base_controller_1.BaseController {
186
188
  });
187
189
  });
188
190
  this.messenger.subscribe('KeyringController:lock', this.#handleLock.bind(this));
191
+ this.messenger.subscribe('SnapsRegistry:stateChange', () => {
192
+ this.#handleRegistryUpdate().catch((error) => {
193
+ (0, snaps_utils_1.logError)(`Error when processing Snaps registry update: ${(0, snaps_sdk_1.getErrorMessage)(error)}`);
194
+ });
195
+ }, ({ database }) => database);
189
196
  this.#initializeStateMachine();
190
197
  this.#registerMessageHandlers();
191
198
  Object.values(this.state?.snaps ?? {}).forEach((snap) => this.#setupRuntime(snap.id));
192
- if (this.#preinstalledSnaps) {
193
- this.#handlePreinstalledSnaps(this.#preinstalledSnaps);
194
- }
195
199
  this.#trackSnapExport = (0, utils_2.throttleTracking)((snapId, handler, success, origin) => {
196
200
  const snapMetadata = this.messenger.call('SnapsRegistry:getMetadata', snapId);
197
201
  this.#trackEvent({
@@ -276,7 +280,7 @@ class SnapController extends base_controller_1.BaseController {
276
280
  * actions.
277
281
  */
278
282
  #registerMessageHandlers() {
279
- this.messenger.registerActionHandler(`${exports.controllerName}:init`, (...args) => this.init(...args));
283
+ this.messenger.registerActionHandler(`${exports.controllerName}:init`, async (...args) => this.init(...args));
280
284
  this.messenger.registerActionHandler(`${exports.controllerName}:clearSnapState`, (...args) => this.clearSnapState(...args));
281
285
  this.messenger.registerActionHandler(`${exports.controllerName}:get`, (...args) => this.get(...args));
282
286
  this.messenger.registerActionHandler(`${exports.controllerName}:getSnapState`, async (...args) => this.getSnapState(...args));
@@ -303,15 +307,32 @@ class SnapController extends base_controller_1.BaseController {
303
307
  /**
304
308
  * Initialise the SnapController.
305
309
  *
306
- * Currently this method calls the `onStart` lifecycle hook for all
310
+ * Currently this method sets up the controller and calls the `onStart` lifecycle hook for all
307
311
  * runnable Snaps.
312
+ *
313
+ * @param waitForPlatform - Whether to wait for the platform to be ready before returning.
308
314
  */
309
- init() {
310
- // Lazily populate the `isReady` state.
311
- this.#ensureCanUsePlatform().catch(snaps_utils_1.logWarning);
312
- this.#callLifecycleHooks(constants_1.METAMASK_ORIGIN, snaps_utils_1.HandlerType.OnStart);
315
+ async init(waitForPlatform = true) {
316
+ try {
317
+ if (this.#preinstalledSnaps) {
318
+ await this.#handlePreinstalledSnaps(this.#preinstalledSnaps);
319
+ }
320
+ this.#controllerSetup.resolve();
321
+ // Populate the `isReady` state.
322
+ if (waitForPlatform) {
323
+ await this.#ensureCanUsePlatform();
324
+ }
325
+ else {
326
+ this.#ensureCanUsePlatform().catch(snaps_utils_1.logError);
327
+ }
328
+ this.#callLifecycleHooks(constants_1.METAMASK_ORIGIN, snaps_utils_1.HandlerType.OnStart);
329
+ }
330
+ catch (error) {
331
+ this.#controllerSetup.reject(error);
332
+ (0, snaps_utils_1.logWarning)('Error during SnapController initialization.', error);
333
+ }
313
334
  }
314
- #handlePreinstalledSnaps(preinstalledSnaps) {
335
+ async #handlePreinstalledSnaps(preinstalledSnaps) {
315
336
  for (const { snapId, manifest, files, removable, hidden, hideSnapBranding, } of preinstalledSnaps) {
316
337
  const existingSnap = this.get(snapId);
317
338
  const isAlreadyInstalled = existingSnap !== undefined;
@@ -346,7 +367,7 @@ class SnapController extends base_controller_1.BaseController {
346
367
  localizationFiles: validatedLocalizationFiles,
347
368
  };
348
369
  // Add snap to the SnapController state
349
- this.#set({
370
+ await this.#set({
350
371
  id: snapId,
351
372
  origin: constants_1.METAMASK_ORIGIN,
352
373
  files: filesObject,
@@ -419,15 +440,22 @@ class SnapController extends base_controller_1.BaseController {
419
440
  }, this.#idleTimeCheckInterval);
420
441
  }
421
442
  /**
422
- * Checks all installed snaps against the block list and
423
- * blocks/unblocks snaps as appropriate. See {@link SnapController.blockSnap}
424
- * for more information.
443
+ * Trigger an update of the registry.
425
444
  *
426
- * Also updates any preinstalled Snaps to the latest allowlisted version.
445
+ * As a side-effect of this, preinstalled Snaps may be updated and Snaps may be blocked/unblocked.
427
446
  */
428
447
  async updateRegistry() {
429
448
  await this.#ensureCanUsePlatform();
430
449
  await this.messenger.call('SnapsRegistry:update');
450
+ }
451
+ /**
452
+ * Checks all installed Snaps against the blocklist and
453
+ * blocks/unblocks Snaps as appropriate. See {@link SnapController.blockSnap}
454
+ * for more information.
455
+ *
456
+ * Also updates any preinstalled Snaps to the latest allowlisted version.
457
+ */
458
+ async #handleRegistryUpdate() {
431
459
  const blockedSnaps = await this.messenger.call('SnapsRegistry:get', Object.values(this.state.snaps).reduce((blockListArg, snap) => {
432
460
  blockListArg[snap.id] = {
433
461
  version: snap.version,
@@ -535,6 +563,8 @@ class SnapController extends base_controller_1.BaseController {
535
563
  * Waits for onboarding and then asserts whether the Snaps platform is allowed to run.
536
564
  */
537
565
  async #ensureCanUsePlatform() {
566
+ // Ensure the controller has finished setting up.
567
+ await this.#controllerSetup.promise;
538
568
  // Ensure the user has onboarded before allowing access to Snaps.
539
569
  await this.#ensureOnboardingComplete();
540
570
  const flags = this.#getFeatureFlags();
@@ -616,9 +646,10 @@ class SnapController extends base_controller_1.BaseController {
616
646
  if (!snap.enabled) {
617
647
  throw new Error(`Snap "${snapId}" is disabled.`);
618
648
  }
649
+ const sourceCode = await this.#getSourceCode(snapId);
619
650
  await this.#startSnap({
620
651
  snapId,
621
- sourceCode: snap.sourceCode,
652
+ sourceCode,
622
653
  });
623
654
  }
624
655
  /**
@@ -679,7 +710,6 @@ class SnapController extends base_controller_1.BaseController {
679
710
  runtime.stopPromise = promise;
680
711
  try {
681
712
  if (this.isRunning(snapId)) {
682
- this.#closeAllConnections?.(snapId);
683
713
  await this.#terminateSnap(snapId);
684
714
  }
685
715
  }
@@ -1050,6 +1080,7 @@ class SnapController extends base_controller_1.BaseController {
1050
1080
  /**
1051
1081
  * Completely clear the controller's state: delete all associated data,
1052
1082
  * handlers, event listeners, and permissions; tear down all snap providers.
1083
+ * Also re-initializes the controller after clearing the state.
1053
1084
  */
1054
1085
  async clearState() {
1055
1086
  const snapIds = Object.keys(this.state.snaps);
@@ -1063,10 +1094,10 @@ class SnapController extends base_controller_1.BaseController {
1063
1094
  });
1064
1095
  this.#snapsRuntimeData.clear();
1065
1096
  this.#rollbackSnapshots.clear();
1066
- // We want to remove all snaps & permissions, except for preinstalled snaps
1067
- if (this.#preinstalledSnaps) {
1068
- this.#handlePreinstalledSnaps(this.#preinstalledSnaps);
1069
- }
1097
+ await this.#clearStorageService();
1098
+ this.#controllerSetup = (0, utils_1.createDeferredPromise)();
1099
+ // Re-initialize the controller after clearing the state, re-installing preinstalled Snaps etc.
1100
+ await this.init(false);
1070
1101
  }
1071
1102
  /**
1072
1103
  * Removes the given snap from state, and clears all associated handlers
@@ -1107,6 +1138,7 @@ class SnapController extends base_controller_1.BaseController {
1107
1138
  delete state.snapStates[snapId];
1108
1139
  delete state.unencryptedSnapStates[snapId];
1109
1140
  });
1141
+ await this.#removeSourceCode(snapId);
1110
1142
  // If the snap has been fully installed before, also emit snapUninstalled.
1111
1143
  if (snap.status !== snaps_utils_1.SnapStatus.Installing) {
1112
1144
  this.messenger.publish(`SnapController:snapUninstalled`, truncated);
@@ -1532,7 +1564,7 @@ class SnapController extends base_controller_1.BaseController {
1532
1564
  await this.stopSnap(snapId, snaps_utils_1.SnapStatusEvents.Stop);
1533
1565
  }
1534
1566
  this.#transition(snapId, snaps_utils_1.SnapStatusEvents.Update);
1535
- this.#set({
1567
+ await this.#set({
1536
1568
  origin,
1537
1569
  id: snapId,
1538
1570
  files: newSnap,
@@ -1719,7 +1751,7 @@ class SnapController extends base_controller_1.BaseController {
1719
1751
  * @param args - The add snap args.
1720
1752
  * @returns The resulting snap object.
1721
1753
  */
1722
- #set(args) {
1754
+ async #set(args) {
1723
1755
  const { id: snapId, origin, files, isUpdate = false, removable, preinstalled, hidden, hideSnapBranding, } = args;
1724
1756
  const { manifest, sourceCode: sourceCodeFile, svgIcon, auxiliaryFiles: rawAuxiliaryFiles, localizationFiles, } = files;
1725
1757
  (0, snaps_utils_1.assertIsSnapManifest)(manifest.result);
@@ -1761,7 +1793,6 @@ class SnapController extends base_controller_1.BaseController {
1761
1793
  initialPermissions: manifest.result.initialPermissions,
1762
1794
  manifest: manifest.result,
1763
1795
  status: this.#statusMachine.config.initial,
1764
- sourceCode,
1765
1796
  version,
1766
1797
  versionHistory,
1767
1798
  auxiliaryFiles,
@@ -1778,9 +1809,14 @@ class SnapController extends base_controller_1.BaseController {
1778
1809
  if (isUpdate) {
1779
1810
  const rollbackSnapshot = this.#getRollbackSnapshot(snapId);
1780
1811
  if (rollbackSnapshot !== undefined) {
1812
+ // Save statePatches first, before any async operations that might throw.
1813
+ // This ensures rollback can revert state changes even if subsequent operations fail.
1781
1814
  rollbackSnapshot.statePatches = inversePatches;
1815
+ const previousSourceCode = await this.#getSourceCode(snapId);
1816
+ rollbackSnapshot.previousSourceCode = previousSourceCode;
1782
1817
  }
1783
1818
  }
1819
+ await this.#setSourceCode(snapId, sourceCode);
1784
1820
  // In case the Snap uses a localized manifest, we need to get the
1785
1821
  // proposed name from the localized manifest.
1786
1822
  const { proposedName } = (0, snaps_utils_1.getLocalizedSnapManifest)(manifest.result, 'en', localizedFiles);
@@ -2291,10 +2327,15 @@ class SnapController extends base_controller_1.BaseController {
2291
2327
  if (this.get(snapId)?.status !== snaps_utils_1.SnapStatus.Stopped) {
2292
2328
  this.#transition(snapId, snaps_utils_1.SnapStatusEvents.Stop);
2293
2329
  }
2294
- const { statePatches, permissions, previousInitialConnections, newInitialConnections, } = rollbackSnapshot;
2330
+ const { statePatches, permissions, previousInitialConnections, newInitialConnections, previousSourceCode, } = rollbackSnapshot;
2295
2331
  if (statePatches?.length) {
2296
2332
  this.applyPatches(statePatches);
2297
2333
  }
2334
+ // If the snap has a previous source code, set it back to the previous source code.
2335
+ // If it doesn't, we don't need to set it back to the previous source code because it means we haven't updated the source code.
2336
+ if (previousSourceCode) {
2337
+ await this.#setSourceCode(snapId, previousSourceCode);
2338
+ }
2298
2339
  // Reset snap status, as we may have been in another state when we stored state patches
2299
2340
  // But now we are 100% in a stopped state
2300
2341
  if (this.get(snapId)?.status !== snaps_utils_1.SnapStatus.Stopped) {
@@ -2531,6 +2572,57 @@ class SnapController extends base_controller_1.BaseController {
2531
2572
  runtime.state = undefined;
2532
2573
  }
2533
2574
  }
2575
+ /**
2576
+ * Retrieve the source code for a Snap from storage.
2577
+ *
2578
+ * @param snapId - The Snap ID.
2579
+ * @returns The source code for the Snap.
2580
+ */
2581
+ async #getSourceCode(snapId) {
2582
+ const storage = await this.#getStorage(snapId);
2583
+ (0, utils_1.assert)(storage?.sourceCode, `Source code for Snap "${snapId}" not found.`);
2584
+ return storage.sourceCode;
2585
+ }
2586
+ /**
2587
+ * Retrieve the storage for a Snap from the StorageService.
2588
+ *
2589
+ * @param snapId - The Snap ID.
2590
+ * @returns The storage for the Snap.
2591
+ */
2592
+ async #getStorage(snapId) {
2593
+ const { result, error } = await this.messenger.call('StorageService:getItem', this.name, snapId);
2594
+ (0, utils_1.assert)(!error, `Error retrieving storage for snap "${snapId}": ${error}`);
2595
+ return result;
2596
+ }
2597
+ /**
2598
+ * Store the source code for a Snap in storage.
2599
+ * For now we call the StorageService with just the source code
2600
+ * since that's all we store.
2601
+ *
2602
+ * @param snapId - The Snap ID.
2603
+ * @param sourceCode - The source code for the Snap.
2604
+ */
2605
+ async #setSourceCode(snapId, sourceCode) {
2606
+ await this.messenger.call('StorageService:setItem', this.name, snapId, {
2607
+ sourceCode,
2608
+ });
2609
+ }
2610
+ /**
2611
+ * Remove the source code for a Snap from storage.
2612
+ * Since we only store source code, this effectively
2613
+ * removes all data for the Snap for now.
2614
+ *
2615
+ * @param snapId - The Snap ID.
2616
+ */
2617
+ async #removeSourceCode(snapId) {
2618
+ await this.messenger.call('StorageService:removeItem', this.name, snapId);
2619
+ }
2620
+ /**
2621
+ * Clear all Snap data from the StorageService.
2622
+ */
2623
+ async #clearStorageService() {
2624
+ await this.messenger.call('StorageService:clear', this.name);
2625
+ }
2534
2626
  }
2535
2627
  exports.SnapController = SnapController;
2536
2628
  //# sourceMappingURL=SnapController.cjs.map