@metamask/snaps-controllers 0.24.1 → 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cronjob/CronjobController.d.ts +3 -2
- package/dist/cronjob/CronjobController.js +37 -23
- package/dist/cronjob/CronjobController.js.map +1 -1
- package/dist/fsm.js +1 -1
- package/dist/fsm.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js.map +1 -1
- package/dist/multichain/MultiChainController.d.ts +3 -1
- package/dist/multichain/MultiChainController.js +8 -8
- package/dist/multichain/MultiChainController.js.map +1 -1
- package/dist/multichain/middleware.d.ts +1 -1
- package/dist/multichain/middleware.js +2 -2
- package/dist/multichain/middleware.js.map +1 -1
- package/dist/services/AbstractExecutionService.d.ts +3 -3
- package/dist/services/AbstractExecutionService.js +13 -12
- package/dist/services/AbstractExecutionService.js.map +1 -1
- package/dist/services/ExecutionService.d.ts +1 -1
- package/dist/services/ExecutionService.js.map +1 -1
- package/dist/services/iframe/IframeExecutionService.js +1 -1
- package/dist/services/iframe/IframeExecutionService.js.map +1 -1
- package/dist/services/iframe/test/server.js +6 -6
- package/dist/services/iframe/test/server.js.map +1 -1
- package/dist/services/node/NodeProcessExecutionService.d.ts +1 -1
- package/dist/services/node/NodeProcessExecutionService.js +2 -2
- package/dist/services/node/NodeProcessExecutionService.js.map +1 -1
- package/dist/services/node/NodeThreadExecutionService.d.ts +2 -2
- package/dist/services/node/NodeThreadExecutionService.js +5 -4
- package/dist/services/node/NodeThreadExecutionService.js.map +1 -1
- package/dist/snaps/SnapController.d.ts +18 -16
- package/dist/snaps/SnapController.js +104 -87
- package/dist/snaps/SnapController.js.map +1 -1
- package/dist/snaps/endowments/cronjob.d.ts +2 -2
- package/dist/snaps/endowments/cronjob.js +3 -3
- package/dist/snaps/endowments/cronjob.js.map +1 -1
- package/dist/snaps/endowments/enum.d.ts +2 -1
- package/dist/snaps/endowments/enum.js +1 -0
- package/dist/snaps/endowments/enum.js.map +1 -1
- package/dist/snaps/endowments/ethereum-provider.d.ts +1 -1
- package/dist/snaps/endowments/ethereum-provider.js +2 -2
- package/dist/snaps/endowments/ethereum-provider.js.map +1 -1
- package/dist/snaps/endowments/index.d.ts +33 -19
- package/dist/snaps/endowments/index.js +16 -4
- package/dist/snaps/endowments/index.js.map +1 -1
- package/dist/snaps/endowments/keyring.d.ts +1 -1
- package/dist/snaps/endowments/keyring.js +2 -2
- package/dist/snaps/endowments/keyring.js.map +1 -1
- package/dist/snaps/endowments/long-running.d.ts +1 -1
- package/dist/snaps/endowments/long-running.js +2 -2
- package/dist/snaps/endowments/long-running.js.map +1 -1
- package/dist/snaps/endowments/network-access.d.ts +1 -1
- package/dist/snaps/endowments/network-access.js +2 -2
- package/dist/snaps/endowments/network-access.js.map +1 -1
- package/dist/snaps/endowments/rpc.d.ts +35 -0
- package/dist/snaps/endowments/rpc.js +91 -0
- package/dist/snaps/endowments/rpc.js.map +1 -0
- package/dist/snaps/endowments/transaction-insight.d.ts +3 -3
- package/dist/snaps/endowments/transaction-insight.js +3 -3
- package/dist/snaps/endowments/transaction-insight.js.map +1 -1
- package/dist/snaps/utils/npm.d.ts +2 -2
- package/dist/snaps/utils/npm.js +7 -3
- package/dist/snaps/utils/npm.js.map +1 -1
- package/dist/snaps/utils/stream.d.ts +1 -1
- package/dist/snaps/utils/stream.js +3 -3
- package/dist/snaps/utils/stream.js.map +1 -1
- package/dist/utils.d.ts +3 -3
- package/dist/utils.js.map +1 -1
- package/package.json +25 -20
|
@@ -47,10 +47,10 @@ class AbstractExecutionService {
|
|
|
47
47
|
* actions.
|
|
48
48
|
*/
|
|
49
49
|
registerMessageHandlers() {
|
|
50
|
-
__classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").registerActionHandler(`${controllerName}:handleRpcRequest`, (snapId, options) => this.handleRpcRequest(snapId, options));
|
|
51
|
-
__classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").registerActionHandler(`${controllerName}:executeSnap`, (snapData) => this.executeSnap(snapData));
|
|
52
|
-
__classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").registerActionHandler(`${controllerName}:terminateSnap`, (snapId) => this.terminateSnap(snapId));
|
|
53
|
-
__classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").registerActionHandler(`${controllerName}:terminateAllSnaps`, () => this.terminateAllSnaps());
|
|
50
|
+
__classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").registerActionHandler(`${controllerName}:handleRpcRequest`, async (snapId, options) => this.handleRpcRequest(snapId, options));
|
|
51
|
+
__classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").registerActionHandler(`${controllerName}:executeSnap`, async (snapData) => this.executeSnap(snapData));
|
|
52
|
+
__classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").registerActionHandler(`${controllerName}:terminateSnap`, async (snapId) => this.terminateSnap(snapId));
|
|
53
|
+
__classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").registerActionHandler(`${controllerName}:terminateAllSnaps`, async () => this.terminateAllSnaps());
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
56
56
|
* Terminates the job with the specified ID and deletes all its associated
|
|
@@ -85,8 +85,8 @@ class AbstractExecutionService {
|
|
|
85
85
|
!stream.destroyed && stream.destroy();
|
|
86
86
|
stream.removeAllListeners();
|
|
87
87
|
}
|
|
88
|
-
catch (
|
|
89
|
-
console.error('Error while destroying stream',
|
|
88
|
+
catch (error) {
|
|
89
|
+
console.error('Error while destroying stream', error);
|
|
90
90
|
}
|
|
91
91
|
});
|
|
92
92
|
this.terminateJob(jobWrapper);
|
|
@@ -164,6 +164,7 @@ class AbstractExecutionService {
|
|
|
164
164
|
streams: {
|
|
165
165
|
command: commandStream,
|
|
166
166
|
rpc: rpcStream,
|
|
167
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
167
168
|
_connection: envStream,
|
|
168
169
|
},
|
|
169
170
|
worker,
|
|
@@ -183,7 +184,7 @@ class AbstractExecutionService {
|
|
|
183
184
|
}
|
|
184
185
|
}
|
|
185
186
|
async terminateAllSnaps() {
|
|
186
|
-
await Promise.all([...this.jobs.keys()].map((jobId) => this.terminate(jobId)));
|
|
187
|
+
await Promise.all([...this.jobs.keys()].map(async (jobId) => this.terminate(jobId)));
|
|
187
188
|
__classPrivateFieldGet(this, _AbstractExecutionService_snapRpcHooks, "f").clear();
|
|
188
189
|
}
|
|
189
190
|
/**
|
|
@@ -192,7 +193,7 @@ class AbstractExecutionService {
|
|
|
192
193
|
* @param snapId - The id of the Snap whose message handler to get.
|
|
193
194
|
* @returns The RPC request handler for the snap.
|
|
194
195
|
*/
|
|
195
|
-
|
|
196
|
+
getRpcRequestHandler(snapId) {
|
|
196
197
|
return __classPrivateFieldGet(this, _AbstractExecutionService_snapRpcHooks, "f").get(snapId);
|
|
197
198
|
}
|
|
198
199
|
/**
|
|
@@ -303,11 +304,11 @@ function setupMultiplex(connectionStream, streamName) {
|
|
|
303
304
|
const mux = new object_multiplex_1.default();
|
|
304
305
|
(0, pump_1.default)(connectionStream,
|
|
305
306
|
// Typecast: stream type mismatch
|
|
306
|
-
mux, connectionStream, (
|
|
307
|
-
if (
|
|
307
|
+
mux, connectionStream, (error) => {
|
|
308
|
+
if (error) {
|
|
308
309
|
streamName
|
|
309
|
-
? console.error(`"${streamName}" stream failure.`,
|
|
310
|
-
: console.error(
|
|
310
|
+
? console.error(`"${streamName}" stream failure.`, error)
|
|
311
|
+
: console.error(error);
|
|
311
312
|
}
|
|
312
313
|
});
|
|
313
314
|
return mux;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AbstractExecutionService.js","sourceRoot":"","sources":["../../src/services/AbstractExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAEA,kFAAyD;AACzD,uDAI+B;AAG/B,2CAMyB;AACzB,qDAKyB;AACzB,2EAAoE;AACpE,mCAAgC;AAChC,gDAAwB;AACxB,oCAAoD;AAQpD,MAAM,cAAc,GA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I,8CAAc,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC7C,IAAI,CAAC,MAAM,EAAE;QACX,MAAM,IAAI,KAAK,CAAC,SAAS,KAAK,uBAAuB,CAAC,CAAC;KACxD;IAED,uBAAA,IAAI,8CAAc,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACjC,uBAAA,IAAI,8CAAc,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAClC,uBAAA,IAAI,sFAAiB,MAArB,IAAI,EAAkB,MAAM,CAAC,CAAC;AAChC,CAAC;AAyBH;;;;;;GAMG;AACH,SAAgB,cAAc,CAC5B,gBAAwB,EACxB,UAAkB;IAElB,MAAM,GAAG,GAAG,IAAI,0BAAe,EAAE,CAAC;IAClC,IAAA,cAAI,EACF,gBAAgB;IAChB,iCAAiC;IACjC,GAAwB,EACxB,gBAAgB,EAChB,CAAC,GAAG,EAAE,EAAE;QACN,IAAI,GAAG,EAAE;YACP,UAAU;gBACR,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,UAAU,mBAAmB,EAAE,GAAG,CAAC;gBACvD,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;SACxB;IACH,CAAC,CACF,CAAC;IACF,OAAO,GAAG,CAAC;AACb,CAAC;AAnBD,wCAmBC","sourcesContent":["import { Duplex } from 'stream';\n\nimport ObjectMultiplex from '@metamask/object-multiplex';\nimport {\n SnapRpcHook,\n SnapRpcHookArgs,\n SNAP_STREAM_NAMES,\n} from '@metamask/snaps-utils';\n\nimport { BasePostMessageStream } from '@metamask/post-message-stream';\nimport {\n Duration,\n isJsonRpcNotification,\n isObject,\n Json,\n JsonRpcNotification,\n} from '@metamask/utils';\nimport {\n JsonRpcEngine,\n // TODO: Replace with @metamask/utils version after bumping json-rpc-engine\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from 'json-rpc-engine';\nimport { createStreamMiddleware } from 'json-rpc-middleware-stream';\nimport { nanoid } from 'nanoid';\nimport pump from 'pump';\nimport { hasTimedOut, withTimeout } from '../utils';\nimport {\n ExecutionService,\n ExecutionServiceMessenger,\n SnapErrorJson,\n SnapExecutionData,\n} from './ExecutionService';\n\nconst controllerName = 'ExecutionService';\n\nexport type SetupSnapProvider = (snapId: string, stream: Duplex) => void;\n\nexport type ExecutionServiceArgs = {\n setupSnapProvider: SetupSnapProvider;\n messenger: ExecutionServiceMessenger;\n terminationTimeout?: number;\n};\n\nexport type JobStreams = {\n command: Duplex;\n rpc: Duplex;\n _connection: BasePostMessageStream;\n};\n\nexport type Job<WorkerType> = {\n id: string;\n streams: JobStreams;\n rpcEngine: JsonRpcEngine;\n worker: WorkerType;\n};\n\nexport abstract class AbstractExecutionService<WorkerType>\n implements ExecutionService\n{\n #snapRpcHooks: Map<string, SnapRpcHook>;\n\n // Cannot be hash private yet because of tests.\n protected jobs: Map<string, Job<WorkerType>>;\n\n // Cannot be hash private yet because of tests.\n private setupSnapProvider: SetupSnapProvider;\n\n #snapToJobMap: Map<string, string>;\n\n #jobToSnapMap: Map<string, string>;\n\n #messenger: ExecutionServiceMessenger;\n\n #terminationTimeout: number;\n\n constructor({\n setupSnapProvider,\n messenger,\n terminationTimeout = Duration.Second,\n }: ExecutionServiceArgs) {\n this.#snapRpcHooks = new Map();\n this.jobs = new Map();\n this.setupSnapProvider = setupSnapProvider;\n this.#snapToJobMap = new Map();\n this.#jobToSnapMap = new Map();\n this.#messenger = messenger;\n this.#terminationTimeout = terminationTimeout;\n\n this.registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering the controller's messaging system\n * actions.\n */\n private registerMessageHandlers(): void {\n this.#messenger.registerActionHandler(\n `${controllerName}:handleRpcRequest`,\n (snapId: string, options: SnapRpcHookArgs) =>\n this.handleRpcRequest(snapId, options),\n );\n\n this.#messenger.registerActionHandler(\n `${controllerName}:executeSnap`,\n (snapData: SnapExecutionData) => this.executeSnap(snapData),\n );\n\n this.#messenger.registerActionHandler(\n `${controllerName}:terminateSnap`,\n (snapId: string) => this.terminateSnap(snapId),\n );\n\n this.#messenger.registerActionHandler(\n `${controllerName}:terminateAllSnaps`,\n () => 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(job: Job<WorkerType>): void;\n\n /**\n * Terminates the job with the specified ID and deletes all its associated\n * data. Any subsequent messages targeting the job will fail with an error.\n * Throws an error if the specified job does not exist, or if termination\n * fails unexpectedly.\n *\n * @param jobId - The id of the job to be terminated.\n */\n public async terminate(jobId: string): Promise<void> {\n const jobWrapper = this.jobs.get(jobId);\n if (!jobWrapper) {\n throw new Error(`Job with id \"${jobId}\" not found.`);\n }\n\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(jobId, {\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 // We tried to shutdown gracefully but failed. This probably means the Snap is in infite loop and\n // hogging down the whole JS process.\n // TODO(ritave): It might be doing weird things such as posting a lot of setTimeouts. Add a test to ensure that this behaviour\n // doesn't leak into other workers. Especially important in IframeExecutionEnvironment since they all share the same\n // JS process.\n console.error(`Job \"${jobId}\" failed to terminate gracefully.`, result);\n }\n\n Object.values(jobWrapper.streams).forEach((stream) => {\n try {\n !stream.destroyed && stream.destroy();\n stream.removeAllListeners();\n } catch (err) {\n console.error('Error while destroying stream', err);\n }\n });\n\n this.terminateJob(jobWrapper);\n\n this.#removeSnapAndJobMapping(jobId);\n this.jobs.delete(jobId);\n console.log(`Job \"${jobId}\" terminated.`);\n }\n\n /**\n * Initiates a job for a snap.\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 * @returns Information regarding the created job.\n */\n protected async initJob(): Promise<Job<WorkerType>> {\n const jobId = nanoid();\n const { streams, worker } = await this.initStreams(jobId);\n const rpcEngine = new JsonRpcEngine();\n\n const jsonRpcConnection = createStreamMiddleware();\n\n pump(jsonRpcConnection.stream, streams.command, jsonRpcConnection.stream);\n\n rpcEngine.push(jsonRpcConnection.middleware);\n\n const envMetadata = {\n id: jobId,\n streams,\n rpcEngine,\n worker,\n };\n this.jobs.set(jobId, envMetadata);\n\n return envMetadata;\n }\n\n /**\n * Sets up the streams for an initiated 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 * @param jobId - The id of the job.\n * @returns The streams to communicate with the worker and the worker itself.\n */\n protected async initStreams(\n jobId: string,\n ): Promise<{ streams: JobStreams; worker: WorkerType }> {\n const { worker, stream: envStream } = await this.initEnvStream(jobId);\n // Typecast justification: stream type mismatch\n const mux = setupMultiplex(\n envStream as unknown as Duplex,\n `Job: \"${jobId}\"`,\n );\n\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<unknown>\n | JsonRpcNotification<Json[] | Record<string, Json>>,\n ) => {\n if (!isJsonRpcNotification(message)) {\n return;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const snapId = this.#jobToSnapMap.get(jobId)!;\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 if (isObject(message.params) && message.params.error) {\n this.#messenger.publish(\n 'ExecutionService:unhandledError',\n snapId,\n message.params.error as SnapErrorJson,\n );\n commandStream.removeListener('data', notificationHandler);\n } else {\n console.error(\n new Error(\n `Received malformed \"${message.method}\" command stream notification.`,\n ),\n );\n }\n } else {\n console.error(\n new Error(\n `Received unexpected command stream notification \"${message.method}\".`,\n ),\n );\n }\n };\n commandStream.on('data', notificationHandler);\n const rpcStream = mux.createStream(SNAP_STREAM_NAMES.JSON_RPC);\n\n // Typecast: stream type mismatch\n return {\n streams: {\n command: commandStream as unknown as Duplex,\n rpc: rpcStream,\n _connection: envStream,\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(jobId: string): Promise<{\n worker: WorkerType;\n stream: BasePostMessageStream;\n }>;\n\n /**\n * Terminates the Snap with the specified ID. May throw an error if\n * termination unexpectedly fails, but will not fail if no job for the snap\n * with the specified ID is found.\n *\n * @param snapId - The ID of the snap to terminate.\n */\n async terminateSnap(snapId: string) {\n const jobId = this.#snapToJobMap.get(snapId);\n if (jobId) {\n await this.terminate(jobId);\n }\n }\n\n async terminateAllSnaps() {\n await Promise.all(\n [...this.jobs.keys()].map((jobId) => this.terminate(jobId)),\n );\n this.#snapRpcHooks.clear();\n }\n\n /**\n * Gets the RPC request handler for the given snap.\n *\n * @param snapId - The id of the Snap whose message handler to get.\n * @returns The RPC request handler for the snap.\n */\n private async getRpcRequestHandler(snapId: string) {\n return this.#snapRpcHooks.get(snapId);\n }\n\n /**\n * Initializes and executes a snap, setting up the communication channels to the snap etc.\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 * @param snapData - Data needed for Snap execution.\n * @returns A string `OK` if execution succeeded.\n * @throws If the execution service returns an error.\n */\n async executeSnap(snapData: SnapExecutionData): Promise<string> {\n if (this.#snapToJobMap.has(snapData.snapId)) {\n throw new Error(`Snap \"${snapData.snapId}\" is already being executed.`);\n }\n\n const job = await this.initJob();\n this.#mapSnapAndJob(snapData.snapId, job.id);\n\n // Ping the worker to ensure that it started up\n await this.command(job.id, {\n jsonrpc: '2.0',\n method: 'ping',\n id: nanoid(),\n });\n\n const rpcStream = job.streams.rpc as unknown as Duplex;\n\n this.setupSnapProvider(snapData.snapId, rpcStream);\n\n const result = await this.command(job.id, {\n jsonrpc: '2.0',\n method: 'executeSnap',\n params: snapData,\n id: nanoid(),\n });\n this.#createSnapHooks(snapData.snapId, job.id);\n return result as string;\n }\n\n // Cannot be hash private yet because of tests.\n private async command(\n jobId: string,\n message: JsonRpcRequest<unknown>,\n ): Promise<unknown> {\n if (typeof message !== 'object') {\n throw new Error('Must send object.');\n }\n\n const job = this.jobs.get(jobId);\n if (!job) {\n throw new Error(`Job with id \"${jobId}\" not found.`);\n }\n\n console.log('Parent: Sending Command', message);\n const response: PendingJsonRpcResponse<unknown> =\n await job.rpcEngine.handle(message);\n if (response.error) {\n throw new Error(response.error.message);\n }\n return response.result;\n }\n\n #removeSnapHooks(snapId: string) {\n this.#snapRpcHooks.delete(snapId);\n }\n\n #createSnapHooks(snapId: string, workerId: string) {\n const rpcHook = async ({ origin, handler, request }: SnapRpcHookArgs) => {\n return await this.command(workerId, {\n id: nanoid(),\n jsonrpc: '2.0',\n method: 'snapRpc',\n params: {\n origin,\n handler,\n request,\n target: snapId,\n },\n });\n };\n\n this.#snapRpcHooks.set(snapId, rpcHook);\n }\n\n /**\n * Gets the job id for a given snap.\n *\n * @param snapId - A given snap id.\n * @returns The ID of the snap's job.\n */\n #getJobForSnap(snapId: string): string | undefined {\n return this.#snapToJobMap.get(snapId);\n }\n\n /**\n * Gets the snap id for a given job.\n *\n * @param jobId - A given job id.\n * @returns The ID of the snap that is running the job.\n */\n #getSnapForJob(jobId: string): string | undefined {\n return this.#jobToSnapMap.get(jobId);\n }\n\n #mapSnapAndJob(snapId: string, jobId: string): void {\n this.#snapToJobMap.set(snapId, jobId);\n this.#jobToSnapMap.set(jobId, snapId);\n }\n\n #removeSnapAndJobMapping(jobId: string): void {\n const snapId = this.#jobToSnapMap.get(jobId);\n if (!snapId) {\n throw new Error(`job: \"${jobId}\" has no mapped snap.`);\n }\n\n this.#jobToSnapMap.delete(jobId);\n this.#snapToJobMap.delete(snapId);\n this.#removeSnapHooks(snapId);\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 rpcRequestHandler = await this.getRpcRequestHandler(snapId);\n\n if (!rpcRequestHandler) {\n throw new Error(\n `Snap execution service returned no RPC handler for running snap \"${snapId}\".`,\n );\n }\n\n return rpcRequestHandler(options);\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 pump(\n connectionStream,\n // Typecast: stream type mismatch\n mux as unknown as Duplex,\n connectionStream,\n (err) => {\n if (err) {\n streamName\n ? console.error(`\"${streamName}\" stream failure.`, err)\n : console.error(err);\n }\n },\n );\n return mux;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"AbstractExecutionService.js","sourceRoot":"","sources":["../../src/services/AbstractExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,kFAAyD;AAEzD,uDAI+B;AAC/B,2CAMyB;AACzB,qDAKyB;AACzB,2EAAoE;AACpE,mCAAgC;AAChC,gDAAwB;AAGxB,oCAAoD;AAQpD,MAAM,cAAc,GAAG,kBAAkB,CAAC;AAuB1C,MAAsB,wBAAwB;IAmB5C,YAAY,EACV,iBAAiB,EACjB,SAAS,EACT,kBAAkB,GAAG,gBAAQ,CAAC,MAAM,GACf;;QApBvB,yDAAwC;QAQxC,yDAAmC;QAEnC,yDAAmC;QAEnC,sDAAsC;QAEtC,+DAA4B;QAO1B,uBAAA,IAAI,0CAAiB,IAAI,GAAG,EAAE,MAAA,CAAC;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MAAM,CAAC,MAAM,CAAC,CAAC;IAClC,uBAAA,IAAI,sFAAiB,MAArB,IAAI,EAAkB,MAAM,CAAC,CAAC;AAChC,CAAC;AAyBH;;;;;;GAMG;AACH,SAAgB,cAAc,CAC5B,gBAAwB,EACxB,UAAkB;IAElB,MAAM,GAAG,GAAG,IAAI,0BAAe,EAAE,CAAC;IAClC,IAAA,cAAI,EACF,gBAAgB;IAChB,iCAAiC;IACjC,GAAwB,EACxB,gBAAgB,EAChB,CAAC,KAAK,EAAE,EAAE;QACR,IAAI,KAAK,EAAE;YACT,UAAU;gBACR,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,UAAU,mBAAmB,EAAE,KAAK,CAAC;gBACzD,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;SAC1B;IACH,CAAC,CACF,CAAC;IACF,OAAO,GAAG,CAAC;AACb,CAAC;AAnBD,wCAmBC","sourcesContent":["import ObjectMultiplex from '@metamask/object-multiplex';\nimport { BasePostMessageStream } from '@metamask/post-message-stream';\nimport {\n SnapRpcHook,\n SnapRpcHookArgs,\n SNAP_STREAM_NAMES,\n} from '@metamask/snaps-utils';\nimport {\n Duration,\n isJsonRpcNotification,\n isObject,\n Json,\n JsonRpcNotification,\n} from '@metamask/utils';\nimport {\n JsonRpcEngine,\n // TODO: Replace with @metamask/utils version after bumping json-rpc-engine\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from 'json-rpc-engine';\nimport { createStreamMiddleware } from 'json-rpc-middleware-stream';\nimport { nanoid } from 'nanoid';\nimport pump from 'pump';\nimport { Duplex } from 'stream';\n\nimport { hasTimedOut, withTimeout } from '../utils';\nimport {\n ExecutionService,\n ExecutionServiceMessenger,\n SnapErrorJson,\n SnapExecutionData,\n} from './ExecutionService';\n\nconst controllerName = 'ExecutionService';\n\nexport type SetupSnapProvider = (snapId: string, stream: Duplex) => void;\n\nexport type ExecutionServiceArgs = {\n setupSnapProvider: SetupSnapProvider;\n messenger: ExecutionServiceMessenger;\n terminationTimeout?: number;\n};\n\nexport type JobStreams = {\n command: Duplex;\n rpc: Duplex;\n _connection: BasePostMessageStream;\n};\n\nexport type Job<WorkerType> = {\n id: string;\n streams: JobStreams;\n rpcEngine: JsonRpcEngine;\n worker: WorkerType;\n};\n\nexport abstract class AbstractExecutionService<WorkerType>\n implements ExecutionService\n{\n #snapRpcHooks: Map<string, SnapRpcHook>;\n\n // Cannot be hash private yet because of tests.\n protected jobs: Map<string, Job<WorkerType>>;\n\n // Cannot be hash private yet because of tests.\n private readonly setupSnapProvider: SetupSnapProvider;\n\n #snapToJobMap: Map<string, string>;\n\n #jobToSnapMap: Map<string, string>;\n\n #messenger: ExecutionServiceMessenger;\n\n #terminationTimeout: number;\n\n constructor({\n setupSnapProvider,\n messenger,\n terminationTimeout = Duration.Second,\n }: ExecutionServiceArgs) {\n this.#snapRpcHooks = new Map();\n this.jobs = new Map();\n this.setupSnapProvider = setupSnapProvider;\n this.#snapToJobMap = new Map();\n this.#jobToSnapMap = new Map();\n this.#messenger = messenger;\n this.#terminationTimeout = terminationTimeout;\n\n this.registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering the controller's messaging system\n * actions.\n */\n private 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 (snapData: SnapExecutionData) => this.executeSnap(snapData),\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(job: Job<WorkerType>): void;\n\n /**\n * Terminates the job with the specified ID and deletes all its associated\n * data. Any subsequent messages targeting the job will fail with an error.\n * Throws an error if the specified job does not exist, or if termination\n * fails unexpectedly.\n *\n * @param jobId - The id of the job to be terminated.\n */\n public async terminate(jobId: string): Promise<void> {\n const jobWrapper = this.jobs.get(jobId);\n if (!jobWrapper) {\n throw new Error(`Job with id \"${jobId}\" not found.`);\n }\n\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(jobId, {\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 // We tried to shutdown gracefully but failed. This probably means the Snap is in infite loop and\n // hogging down the whole JS process.\n // TODO(ritave): It might be doing weird things such as posting a lot of setTimeouts. Add a test to ensure that this behaviour\n // doesn't leak into other workers. Especially important in IframeExecutionEnvironment since they all share the same\n // JS process.\n console.error(`Job \"${jobId}\" failed to terminate gracefully.`, result);\n }\n\n Object.values(jobWrapper.streams).forEach((stream) => {\n try {\n !stream.destroyed && stream.destroy();\n stream.removeAllListeners();\n } catch (error) {\n console.error('Error while destroying stream', error);\n }\n });\n\n this.terminateJob(jobWrapper);\n\n this.#removeSnapAndJobMapping(jobId);\n this.jobs.delete(jobId);\n console.log(`Job \"${jobId}\" terminated.`);\n }\n\n /**\n * Initiates a job for a snap.\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 * @returns Information regarding the created job.\n */\n protected async initJob(): Promise<Job<WorkerType>> {\n const jobId = nanoid();\n const { streams, worker } = await this.initStreams(jobId);\n const rpcEngine = new JsonRpcEngine();\n\n const jsonRpcConnection = createStreamMiddleware();\n\n pump(jsonRpcConnection.stream, streams.command, jsonRpcConnection.stream);\n\n rpcEngine.push(jsonRpcConnection.middleware);\n\n const envMetadata = {\n id: jobId,\n streams,\n rpcEngine,\n worker,\n };\n this.jobs.set(jobId, envMetadata);\n\n return envMetadata;\n }\n\n /**\n * Sets up the streams for an initiated 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 * @param jobId - The id of the job.\n * @returns The streams to communicate with the worker and the worker itself.\n */\n protected async initStreams(\n jobId: string,\n ): Promise<{ streams: JobStreams; worker: WorkerType }> {\n const { worker, stream: envStream } = await this.initEnvStream(jobId);\n // Typecast justification: stream type mismatch\n const mux = setupMultiplex(\n envStream as unknown as Duplex,\n `Job: \"${jobId}\"`,\n );\n\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<unknown>\n | JsonRpcNotification<Json[] | Record<string, Json>>,\n ) => {\n if (!isJsonRpcNotification(message)) {\n return;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const snapId = this.#jobToSnapMap.get(jobId)!;\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 if (isObject(message.params) && message.params.error) {\n this.#messenger.publish(\n 'ExecutionService:unhandledError',\n snapId,\n message.params.error as SnapErrorJson,\n );\n commandStream.removeListener('data', notificationHandler);\n } else {\n console.error(\n new Error(\n `Received malformed \"${message.method}\" command stream notification.`,\n ),\n );\n }\n } else {\n console.error(\n new Error(\n `Received unexpected command stream notification \"${message.method}\".`,\n ),\n );\n }\n };\n commandStream.on('data', notificationHandler);\n const rpcStream = mux.createStream(SNAP_STREAM_NAMES.JSON_RPC);\n\n // Typecast: stream type mismatch\n return {\n streams: {\n command: commandStream as unknown as Duplex,\n rpc: rpcStream,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n _connection: envStream,\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(jobId: string): Promise<{\n worker: WorkerType;\n stream: BasePostMessageStream;\n }>;\n\n /**\n * Terminates the Snap with the specified ID. May throw an error if\n * termination unexpectedly fails, but will not fail if no job for the snap\n * with the specified ID is found.\n *\n * @param snapId - The ID of the snap to terminate.\n */\n async terminateSnap(snapId: string) {\n const jobId = this.#snapToJobMap.get(snapId);\n if (jobId) {\n await this.terminate(jobId);\n }\n }\n\n async terminateAllSnaps() {\n await Promise.all(\n [...this.jobs.keys()].map(async (jobId) => this.terminate(jobId)),\n );\n this.#snapRpcHooks.clear();\n }\n\n /**\n * Gets the RPC request handler for the given snap.\n *\n * @param snapId - The id of the Snap whose message handler to get.\n * @returns The RPC request handler for the snap.\n */\n private getRpcRequestHandler(snapId: string) {\n return this.#snapRpcHooks.get(snapId);\n }\n\n /**\n * Initializes and executes a snap, setting up the communication channels to the snap etc.\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 * @param snapData - Data needed for Snap execution.\n * @returns A string `OK` if execution succeeded.\n * @throws If the execution service returns an error.\n */\n async executeSnap(snapData: SnapExecutionData): Promise<string> {\n if (this.#snapToJobMap.has(snapData.snapId)) {\n throw new Error(`Snap \"${snapData.snapId}\" is already being executed.`);\n }\n\n const job = await this.initJob();\n this.#mapSnapAndJob(snapData.snapId, job.id);\n\n // Ping the worker to ensure that it started up\n await this.command(job.id, {\n jsonrpc: '2.0',\n method: 'ping',\n id: nanoid(),\n });\n\n const rpcStream = job.streams.rpc as unknown as Duplex;\n\n this.setupSnapProvider(snapData.snapId, rpcStream);\n\n const result = await this.command(job.id, {\n jsonrpc: '2.0',\n method: 'executeSnap',\n params: snapData,\n id: nanoid(),\n });\n this.#createSnapHooks(snapData.snapId, job.id);\n return result as string;\n }\n\n // Cannot be hash private yet because of tests.\n private async command(\n jobId: string,\n message: JsonRpcRequest<unknown>,\n ): Promise<unknown> {\n if (typeof message !== 'object') {\n throw new Error('Must send object.');\n }\n\n const job = this.jobs.get(jobId);\n if (!job) {\n throw new Error(`Job with id \"${jobId}\" not found.`);\n }\n\n console.log('Parent: Sending Command', message);\n const response: PendingJsonRpcResponse<unknown> =\n await job.rpcEngine.handle(message);\n if (response.error) {\n throw new Error(response.error.message);\n }\n return response.result;\n }\n\n #removeSnapHooks(snapId: string) {\n this.#snapRpcHooks.delete(snapId);\n }\n\n #createSnapHooks(snapId: string, workerId: string) {\n const rpcHook = async ({ origin, handler, request }: SnapRpcHookArgs) => {\n return await this.command(workerId, {\n id: nanoid(),\n jsonrpc: '2.0',\n method: 'snapRpc',\n params: {\n origin,\n handler,\n request,\n target: snapId,\n },\n });\n };\n\n this.#snapRpcHooks.set(snapId, rpcHook);\n }\n\n /**\n * Gets the job id for a given snap.\n *\n * @param snapId - A given snap id.\n * @returns The ID of the snap's job.\n */\n #getJobForSnap(snapId: string): string | undefined {\n return this.#snapToJobMap.get(snapId);\n }\n\n /**\n * Gets the snap id for a given job.\n *\n * @param jobId - A given job id.\n * @returns The ID of the snap that is running the job.\n */\n #getSnapForJob(jobId: string): string | undefined {\n return this.#jobToSnapMap.get(jobId);\n }\n\n #mapSnapAndJob(snapId: string, jobId: string): void {\n this.#snapToJobMap.set(snapId, jobId);\n this.#jobToSnapMap.set(jobId, snapId);\n }\n\n #removeSnapAndJobMapping(jobId: string): void {\n const snapId = this.#jobToSnapMap.get(jobId);\n if (!snapId) {\n throw new Error(`job: \"${jobId}\" has no mapped snap.`);\n }\n\n this.#jobToSnapMap.delete(jobId);\n this.#snapToJobMap.delete(snapId);\n this.#removeSnapHooks(snapId);\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 rpcRequestHandler = await this.getRpcRequestHandler(snapId);\n\n if (!rpcRequestHandler) {\n throw new Error(\n `Snap execution service returned no RPC handler for running snap \"${snapId}\".`,\n );\n }\n\n return rpcRequestHandler(options);\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 pump(\n connectionStream,\n // Typecast: stream type mismatch\n mux as unknown as Duplex,\n connectionStream,\n (error) => {\n if (error) {\n streamName\n ? console.error(`\"${streamName}\" stream failure.`, error)\n : console.error(error);\n }\n },\n );\n return mux;\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RestrictedControllerMessenger } from '@metamask/
|
|
1
|
+
import { RestrictedControllerMessenger } from '@metamask/base-controller';
|
|
2
2
|
import { SnapId, SnapRpcHookArgs } from '@metamask/snaps-utils';
|
|
3
3
|
import { Json } from '@metamask/types';
|
|
4
4
|
declare type TerminateSnap = (snapId: string) => Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExecutionService.js","sourceRoot":"","sources":["../../src/services/ExecutionService.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"ExecutionService.js","sourceRoot":"","sources":["../../src/services/ExecutionService.ts"],"names":[],"mappings":";;AAgCA,MAAM,cAAc,GAAG,kBAAkB,CAAC","sourcesContent":["import { RestrictedControllerMessenger } from '@metamask/base-controller';\nimport { SnapId, SnapRpcHookArgs } from '@metamask/snaps-utils';\nimport { Json } from '@metamask/types';\n\ntype TerminateSnap = (snapId: string) => Promise<void>;\ntype TerminateAll = () => Promise<void>;\ntype ExecuteSnap = (snapData: SnapExecutionData) => Promise<unknown>;\n\ntype HandleRpcRequest = (\n snapId: string,\n options: SnapRpcHookArgs,\n) => Promise<unknown>;\n\nexport interface ExecutionService {\n terminateSnap: TerminateSnap;\n terminateAllSnaps: TerminateAll;\n executeSnap: ExecuteSnap;\n handleRpcRequest: HandleRpcRequest;\n}\n\nexport type SnapExecutionData = {\n snapId: string;\n sourceCode: string;\n endowments?: Json;\n};\n\nexport type SnapErrorJson = {\n message: string;\n code: number;\n data?: Json;\n};\n\nconst controllerName = 'ExecutionService';\n\nexport type ErrorMessageEvent = {\n type: 'ExecutionService:unhandledError';\n payload: [SnapId, SnapErrorJson];\n};\n\nexport type OutboundRequest = {\n type: 'ExecutionService:outboundRequest';\n payload: [SnapId];\n};\n\nexport type OutboundResponse = {\n type: 'ExecutionService:outboundResponse';\n payload: [SnapId];\n};\n\nexport type ExecutionServiceEvents =\n | ErrorMessageEvent\n | OutboundRequest\n | OutboundResponse;\n\n/**\n * Handles RPC request.\n */\nexport type HandleRpcRequestAction = {\n type: `${typeof controllerName}:handleRpcRequest`;\n handler: ExecutionService['handleRpcRequest'];\n};\n\n/**\n * Executes a given snap.\n */\nexport type ExecuteSnapAction = {\n type: `${typeof controllerName}:executeSnap`;\n handler: ExecutionService['executeSnap'];\n};\n\n/**\n * Terminates a given snap.\n */\nexport type TerminateSnapAction = {\n type: `${typeof controllerName}:terminateSnap`;\n handler: ExecutionService['terminateSnap'];\n};\n\n/**\n * Terminates all snaps.\n */\nexport type TerminateAllSnapsAction = {\n type: `${typeof controllerName}:terminateAllSnaps`;\n handler: ExecutionService['terminateAllSnaps'];\n};\n\nexport type ExecutionServiceActions =\n | HandleRpcRequestAction\n | ExecuteSnapAction\n | TerminateSnapAction\n | TerminateAllSnapsAction;\n\nexport type ExecutionServiceMessenger = RestrictedControllerMessenger<\n 'ExecutionService',\n ExecutionServiceActions,\n ExecutionServiceEvents,\n ExecutionServiceActions['type'],\n ExecutionServiceEvents['type']\n>;\n"]}
|
|
@@ -34,7 +34,7 @@ class IframeExecutionService extends AbstractExecutionService_1.AbstractExecutio
|
|
|
34
34
|
* @param jobId - The job id.
|
|
35
35
|
* @returns A promise that resolves to the contentWindow of the iframe.
|
|
36
36
|
*/
|
|
37
|
-
createWindow(uri, jobId) {
|
|
37
|
+
async createWindow(uri, jobId) {
|
|
38
38
|
return new Promise((resolve, reject) => {
|
|
39
39
|
const iframe = document.createElement('iframe');
|
|
40
40
|
// The order of operations appears to matter for everything except this
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IframeExecutionService.js","sourceRoot":"","sources":["../../../src/services/iframe/IframeExecutionService.ts"],"names":[],"mappings":";;;AAAA,uEAGuC;
|
|
1
|
+
{"version":3,"file":"IframeExecutionService.js","sourceRoot":"","sources":["../../../src/services/iframe/IframeExecutionService.ts"],"names":[],"mappings":";;;AAAA,uEAGuC;AAEvC,0EAIqC;AAMrC,MAAa,sBAAuB,SAAQ,mDAAgC;IAG1E,YAAY,EACV,SAAS,EACT,SAAS,EACT,iBAAiB,GACqB;QACtC,KAAK,CAAC;YACJ,SAAS;YACT,iBAAiB;SAClB,CAAC,CAAC;QACH,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAES,YAAY,CAAC,UAAuB;;QAC5C,MAAA,QAAQ,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,CAAC,0CAAE,MAAM,EAAE,CAAC;IACnD,CAAC;IAES,KAAK,CAAC,aAAa,CAAC,KAAa;QAIzC,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,YAAY,CAC1C,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,EACzB,KAAK,CACN,CAAC;QACF,MAAM,MAAM,GAAG,IAAI,6CAAuB,CAAC;YACzC,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE,OAAO;YACf,YAAY,EAAE,YAAY;YAC1B,YAAY,EAAE,GAAG;SAClB,CAAC,CAAC;QAEH,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,CAAC;IAC1C,CAAC;IAED;;;;;;;;OAQG;IACK,KAAK,CAAC,YAAY,CAAC,GAAW,EAAE,KAAa;QACnD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,MAAM,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;YAChD,uEAAuE;YACvE,yCAAyC;YACzC,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YAEjC,uEAAuE;YACvE,yEAAyE;YACzE,uEAAuE;YACvE,wEAAwE;YACxE,uBAAuB;YACvB,EAAE;YACF,mEAAmE;YACnE,kCAAkC;YAClC,EAAE;YACF,mGAAmG;YACnG,iJAAiJ;YACjJ,MAAM,CAAC,YAAY,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;YAChC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;YAElC,MAAM,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;gBACnC,IAAI,MAAM,CAAC,aAAa,EAAE;oBACxB,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;iBAC/B;qBAAM;oBACL,qEAAqE;oBACrE,mBAAmB;oBACnB,MAAM,CACJ,IAAI,KAAK,CACP,qDAAqD,KAAK,IAAI,CAC/D,CACF,CAAC;iBACH;YACH,CAAC,CAAC,CAAC;YAEH,yEAAyE;YACzE,yEAAyE;YACzE,wEAAwE;YACxE,yCAAyC;YACzC,EAAE;YACF,mEAAmE;YACnE,wCAAwC;YACxC,EAAE;YACF,kEAAkE;YAClE,WAAW;YACX,qFAAqF;YACrF,MAAM,CAAC,YAAY,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC;QAClD,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA/FD,wDA+FC","sourcesContent":["import {\n WindowPostMessageStream,\n BasePostMessageStream,\n} from '@metamask/post-message-stream';\n\nimport {\n Job,\n AbstractExecutionService,\n ExecutionServiceArgs,\n} from '../AbstractExecutionService';\n\ntype IframeExecutionEnvironmentServiceArgs = {\n iframeUrl: URL;\n} & ExecutionServiceArgs;\n\nexport class IframeExecutionService extends AbstractExecutionService<Window> {\n public iframeUrl: URL;\n\n constructor({\n iframeUrl,\n messenger,\n setupSnapProvider,\n }: IframeExecutionEnvironmentServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n });\n this.iframeUrl = iframeUrl;\n }\n\n protected terminateJob(jobWrapper: Job<Window>): void {\n document.getElementById(jobWrapper.id)?.remove();\n }\n\n protected async initEnvStream(jobId: string): Promise<{\n worker: Window;\n stream: BasePostMessageStream;\n }> {\n const iframeWindow = await this.createWindow(\n this.iframeUrl.toString(),\n jobId,\n );\n const stream = new WindowPostMessageStream({\n name: 'parent',\n target: 'child',\n targetWindow: iframeWindow,\n targetOrigin: '*',\n });\n\n return { worker: iframeWindow, stream };\n }\n\n /**\n * Creates the iframe to be used as the execution environment. This may run\n * forever if the iframe never loads, but the promise should be wrapped in\n * an initialization timeout in the SnapController.\n *\n * @param uri - The iframe URI.\n * @param jobId - The job id.\n * @returns A promise that resolves to the contentWindow of the iframe.\n */\n private async createWindow(uri: string, jobId: string): Promise<Window> {\n return new Promise((resolve, reject) => {\n const iframe = document.createElement('iframe');\n // The order of operations appears to matter for everything except this\n // attribute. We may as well set it here.\n iframe.setAttribute('id', jobId);\n\n // In the past, we've had problems that appear to be symptomatic of the\n // iframe firing the `load` event before its scripts are actually loaded,\n // which has prevented snaps from executing properly. Therefore, we set\n // the `src` attribute and append the iframe to the DOM before attaching\n // the `load` listener.\n //\n // `load` should only fire when \"all dependent resources\" have been\n // loaded, which includes scripts.\n //\n // MDN article for `load` event: https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event\n // Re: `load` firing twice: https://stackoverflow.com/questions/10781880/dynamically-created-iframe-triggers-onload-event-twice/15880489#15880489\n iframe.setAttribute('src', uri);\n document.body.appendChild(iframe);\n\n iframe.addEventListener('load', () => {\n if (iframe.contentWindow) {\n resolve(iframe.contentWindow);\n } else {\n // We don't know of a case when this would happen, but better to fail\n // fast if it does.\n reject(\n new Error(\n `iframe.contentWindow not present on load for job \"${jobId}\".`,\n ),\n );\n }\n });\n\n // We need to set the sandbox attribute after appending the iframe to the\n // DOM, otherwise errors in the iframe will not be propagated via `error`\n // and `unhandledrejection` events, and we cannot catch and handle them.\n // We wish we knew why this was the case.\n //\n // We set this property after adding the `load` listener because it\n // appears to work dependably. ¯\\_(ツ)_/¯\n //\n // We apply this property as a principle of least authority (POLA)\n // measure.\n // Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attr-sandbox\n iframe.setAttribute('sandbox', 'allow-scripts');\n });\n }\n}\n"]}
|
|
@@ -21,8 +21,8 @@ async function start(port = exports.PORT) {
|
|
|
21
21
|
}
|
|
22
22
|
const bundlePath = require.resolve('@metamask/snaps-execution-environments/__test__/iframe-test/bundle.js');
|
|
23
23
|
const publicPath = path_1.default.resolve(bundlePath, '../');
|
|
24
|
-
server = http_1.default.createServer(
|
|
25
|
-
|
|
24
|
+
server = http_1.default.createServer((req, res) => {
|
|
25
|
+
(0, serve_handler_1.default)(req, res, {
|
|
26
26
|
public: publicPath,
|
|
27
27
|
headers: [
|
|
28
28
|
{
|
|
@@ -35,7 +35,7 @@ async function start(port = exports.PORT) {
|
|
|
35
35
|
],
|
|
36
36
|
},
|
|
37
37
|
],
|
|
38
|
-
});
|
|
38
|
+
}).catch(reject);
|
|
39
39
|
});
|
|
40
40
|
server.listen({ port }, () => {
|
|
41
41
|
console.log(`Server listening on: http://localhost:${port}`);
|
|
@@ -57,9 +57,9 @@ exports.start = start;
|
|
|
57
57
|
*/
|
|
58
58
|
async function stop() {
|
|
59
59
|
await new Promise((resolve, reject) => {
|
|
60
|
-
server.close((
|
|
61
|
-
if (
|
|
62
|
-
reject(
|
|
60
|
+
server.close((error) => {
|
|
61
|
+
if (error) {
|
|
62
|
+
reject(error);
|
|
63
63
|
}
|
|
64
64
|
else {
|
|
65
65
|
resolve();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../../src/services/iframe/test/server.ts"],"names":[],"mappings":";;;;;;AAAA,gDAAwB;AACxB,gDAAwB;AACxB,kEAAyC;AAE5B,QAAA,IAAI,GAAG,IAAI,CAAC;AAEzB,IAAI,MAAmB,CAAC;AACxB;;;;GAIG;AACI,KAAK,UAAU,KAAK,CAAC,IAAI,GAAG,YAAI;IACrC,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC3C,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,EAAE;YAC3C,MAAM,CAAC,IAAI,KAAK,CAAC,kBAAkB,IAAI,GAAG,CAAC,CAAC,CAAC;SAC9C;QAED,MAAM,UAAU,GAAG,OAAO,CAAC,OAAO,CAChC,uEAAuE,CACxE,CAAC;QACF,MAAM,UAAU,GAAG,cAAI,CAAC,OAAO,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QAEnD,MAAM,GAAG,cAAI,CAAC,YAAY,CAAC,
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../../src/services/iframe/test/server.ts"],"names":[],"mappings":";;;;;;AAAA,gDAAwB;AACxB,gDAAwB;AACxB,kEAAyC;AAE5B,QAAA,IAAI,GAAG,IAAI,CAAC;AAEzB,IAAI,MAAmB,CAAC;AACxB;;;;GAIG;AACI,KAAK,UAAU,KAAK,CAAC,IAAI,GAAG,YAAI;IACrC,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC3C,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,EAAE;YAC3C,MAAM,CAAC,IAAI,KAAK,CAAC,kBAAkB,IAAI,GAAG,CAAC,CAAC,CAAC;SAC9C;QAED,MAAM,UAAU,GAAG,OAAO,CAAC,OAAO,CACh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;YACrB,IAAI,KAAK,EAAE;gBACT,MAAM,CAAC,KAAK,CAAC,CAAC;aACf;iBAAM;gBACL,OAAO,EAAE,CAAC;aACX;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAVD,oBAUC","sourcesContent":["import http from 'http';\nimport path from 'path';\nimport serveHandler from 'serve-handler';\n\nexport const PORT = 6364;\n\nlet server: http.Server;\n/**\n * Starts a local server that serves the iframe execution environment.\n *\n * @param port - The port to start the server on.\n */\nexport async function start(port = PORT) {\n return new Promise<void>((resolve, reject) => {\n if (!Number.isSafeInteger(port) || port < 0) {\n reject(new Error(`Invalid port: \"${port}\"`));\n }\n\n const bundlePath = require.resolve(\n '@metamask/snaps-execution-environments/__test__/iframe-test/bundle.js',\n );\n const publicPath = path.resolve(bundlePath, '../');\n\n server = http.createServer((req, res) => {\n serveHandler(req, res, {\n public: publicPath,\n headers: [\n {\n source: '**/*',\n headers: [\n {\n key: 'Cache-Control',\n value: 'no-cache',\n },\n ],\n },\n ],\n }).catch(reject);\n });\n\n server.listen({ port }, () => {\n console.log(`Server listening on: http://localhost:${port}`);\n resolve();\n });\n\n server.on('error', (error) => {\n console.error('Server error', error);\n reject(error);\n });\n\n server.on('close', () => {\n console.log('Server closed');\n reject(new Error('Server closed'));\n });\n });\n}\n\n/**\n * Stops the local server.\n */\nexport async function stop() {\n await new Promise<void>((resolve, reject) => {\n server.close((error) => {\n if (error) {\n reject(error);\n } else {\n resolve();\n }\n });\n });\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { ChildProcess } from 'child_process';
|
|
3
2
|
import { BasePostMessageStream } from '@metamask/post-message-stream';
|
|
3
|
+
import { ChildProcess } from 'child_process';
|
|
4
4
|
import { AbstractExecutionService, Job } from '..';
|
|
5
5
|
export declare class NodeProcessExecutionService extends AbstractExecutionService<ChildProcess> {
|
|
6
6
|
protected initEnvStream(): Promise<{
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.NodeProcessExecutionService = void 0;
|
|
4
|
-
const child_process_1 = require("child_process");
|
|
5
4
|
const post_message_stream_1 = require("@metamask/post-message-stream");
|
|
5
|
+
const child_process_1 = require("child_process");
|
|
6
6
|
const __1 = require("..");
|
|
7
7
|
class NodeProcessExecutionService extends __1.AbstractExecutionService {
|
|
8
8
|
async initEnvStream() {
|
|
9
9
|
const worker = (0, child_process_1.fork)(require.resolve('@metamask/snaps-execution-environments/dist/webpack/node-process/bundle.js'));
|
|
10
10
|
const stream = new post_message_stream_1.ProcessParentMessageStream({ process: worker });
|
|
11
|
-
return { worker, stream };
|
|
11
|
+
return Promise.resolve({ worker, stream });
|
|
12
12
|
}
|
|
13
13
|
terminateJob(jobWrapper) {
|
|
14
14
|
jobWrapper.worker.kill();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NodeProcessExecutionService.js","sourceRoot":"","sources":["../../../src/services/node/NodeProcessExecutionService.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"NodeProcessExecutionService.js","sourceRoot":"","sources":["../../../src/services/node/NodeProcessExecutionService.ts"],"names":[],"mappings":";;;AAAA,uEAGuC;AACvC,iDAAmD;AAEnD,0BAAmD;AAEnD,MAAa,2BAA4B,SAAQ,4BAAsC;IAC3E,KAAK,CAAC,aAAa;QAI3B,MAAM,MAAM,GAAG,IAAA,oBAAI,EACjB,OAAO,CAAC,OAAO,CACb,4EAA4E,CAC7E,CACF,CAAC;QAEF,MAAM,MAAM,GAAG,IAAI,gDAA0B,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;QACnE,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IAC7C,CAAC;IAES,YAAY,CAAC,UAA6B;QAClD,UAAU,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC3B,CAAC;CACF;AAlBD,kEAkBC","sourcesContent":["import {\n ProcessParentMessageStream,\n BasePostMessageStream,\n} from '@metamask/post-message-stream';\nimport { ChildProcess, fork } from 'child_process';\n\nimport { AbstractExecutionService, Job } from '..';\n\nexport class NodeProcessExecutionService extends AbstractExecutionService<ChildProcess> {\n protected async initEnvStream(): Promise<{\n worker: ChildProcess;\n stream: BasePostMessageStream;\n }> {\n const worker = fork(\n require.resolve(\n '@metamask/snaps-execution-environments/dist/webpack/node-process/bundle.js',\n ),\n );\n\n const stream = new ProcessParentMessageStream({ process: worker });\n return Promise.resolve({ worker, stream });\n }\n\n protected terminateJob(jobWrapper: Job<ChildProcess>): void {\n jobWrapper.worker.kill();\n }\n}\n"]}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { Worker } from 'worker_threads';
|
|
3
2
|
import { BasePostMessageStream } from '@metamask/post-message-stream';
|
|
3
|
+
import { Worker } from 'worker_threads';
|
|
4
4
|
import { AbstractExecutionService, Job } from '..';
|
|
5
5
|
export declare class NodeThreadExecutionService extends AbstractExecutionService<Worker> {
|
|
6
6
|
protected initEnvStream(): Promise<{
|
|
7
7
|
worker: Worker;
|
|
8
8
|
stream: BasePostMessageStream;
|
|
9
9
|
}>;
|
|
10
|
-
protected terminateJob(jobWrapper: Job<Worker>): void
|
|
10
|
+
protected terminateJob(jobWrapper: Job<Worker>): Promise<void>;
|
|
11
11
|
}
|
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.NodeThreadExecutionService = void 0;
|
|
4
|
-
const worker_threads_1 = require("worker_threads");
|
|
5
4
|
const post_message_stream_1 = require("@metamask/post-message-stream");
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
6
|
+
const worker_threads_1 = require("worker_threads");
|
|
6
7
|
const __1 = require("..");
|
|
7
8
|
class NodeThreadExecutionService extends __1.AbstractExecutionService {
|
|
8
9
|
async initEnvStream() {
|
|
9
10
|
const worker = new worker_threads_1.Worker(require.resolve('@metamask/snaps-execution-environments/dist/webpack/node-thread/bundle.js'));
|
|
10
11
|
const stream = new post_message_stream_1.ThreadParentMessageStream({ thread: worker });
|
|
11
|
-
return { worker, stream };
|
|
12
|
+
return Promise.resolve({ worker, stream });
|
|
12
13
|
}
|
|
13
|
-
terminateJob(jobWrapper) {
|
|
14
|
-
jobWrapper.worker.terminate();
|
|
14
|
+
async terminateJob(jobWrapper) {
|
|
15
|
+
await jobWrapper.worker.terminate();
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
18
|
exports.NodeThreadExecutionService = NodeThreadExecutionService;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NodeThreadExecutionService.js","sourceRoot":"","sources":["../../../src/services/node/NodeThreadExecutionService.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"NodeThreadExecutionService.js","sourceRoot":"","sources":["../../../src/services/node/NodeThreadExecutionService.ts"],"names":[],"mappings":";;;AAAA,uEAGuC;AACvC,wDAAwD;AACxD,mDAAwC;AAExC,0BAAmD;AAEnD,MAAa,0BAA2B,SAAQ,4BAAgC;IACpE,KAAK,CAAC,aAAa;QAI3B,MAAM,MAAM,GAAG,IAAI,uBAAM,CACvB,OAAO,CAAC,OAAO,CACb,2EAA2E,CAC5E,CACF,CAAC;QACF,MAAM,MAAM,GAAG,IAAI,+CAAyB,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;QACjE,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IAC7C,CAAC;IAES,KAAK,CAAC,YAAY,CAAC,UAAuB;QAClD,MAAM,UAAU,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;IACtC,CAAC;CACF;AAjBD,gEAiBC","sourcesContent":["import {\n ThreadParentMessageStream,\n BasePostMessageStream,\n} from '@metamask/post-message-stream';\n// eslint-disable-next-line @typescript-eslint/no-shadow\nimport { Worker } from 'worker_threads';\n\nimport { AbstractExecutionService, Job } from '..';\n\nexport class NodeThreadExecutionService extends AbstractExecutionService<Worker> {\n protected async initEnvStream(): Promise<{\n worker: Worker;\n stream: BasePostMessageStream;\n }> {\n const worker = new Worker(\n require.resolve(\n '@metamask/snaps-execution-environments/dist/webpack/node-thread/bundle.js',\n ),\n );\n const stream = new ThreadParentMessageStream({ thread: worker });\n return Promise.resolve({ worker, stream });\n }\n\n protected async terminateJob(jobWrapper: Job<Worker>): Promise<void> {\n await jobWrapper.worker.terminate();\n }\n}\n"]}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import { AddApprovalRequest
|
|
1
|
+
import { AddApprovalRequest } from '@metamask/approval-controller';
|
|
2
|
+
import { BaseControllerV2 as BaseController, RestrictedControllerMessenger } from '@metamask/base-controller';
|
|
3
|
+
import { GetEndowments, GetPermissions, GrantPermissions, HasPermission, HasPermissions, RevokeAllPermissions, RevokePermissionForAllSubjects, RevokePermissions } from '@metamask/permission-controller';
|
|
2
4
|
import { BlockedSnapInfo, InstallSnapsResult, PersistedSnap, RequestedSnapPermissions, Snap, SnapId, SnapManifest, SnapRpcHook, SnapRpcHookArgs, SnapStatusEvents, StatusContext, StatusEvents, StatusStates, TruncatedSnap, ValidatedSnapId } from '@metamask/snaps-utils';
|
|
5
|
+
import { GetSubjectMetadata } from '@metamask/subject-metadata-controller';
|
|
3
6
|
import { Json } from '@metamask/utils';
|
|
4
7
|
import { StateMachine } from '@xstate/fsm';
|
|
5
8
|
import type { Patch } from 'immer';
|
|
6
|
-
import { ExecuteSnapAction, ExecutionServiceEvents, HandleRpcRequestAction, SnapErrorJson, TerminateAllSnapsAction, TerminateSnapAction } from '../services
|
|
9
|
+
import { ExecuteSnapAction, ExecutionServiceEvents, HandleRpcRequestAction, SnapErrorJson, TerminateAllSnapsAction, TerminateSnapAction } from '../services';
|
|
7
10
|
import { Timer } from './Timer';
|
|
8
11
|
export declare const controllerName = "SnapController";
|
|
9
12
|
export declare const SNAP_APPROVAL_INSTALL = "wallet_installSnap";
|
|
@@ -238,11 +241,11 @@ export declare type SnapTerminated = {
|
|
|
238
241
|
payload: [snap: TruncatedSnap];
|
|
239
242
|
};
|
|
240
243
|
export declare type SnapControllerEvents = SnapAdded | SnapBlocked | SnapInstalled | SnapRemoved | SnapStateChange | SnapUnblocked | SnapUpdated | SnapTerminated;
|
|
241
|
-
export declare type AllowedActions = GetEndowments | GetPermissions | HasPermission | HasPermissions | RevokePermissions | RevokeAllPermissions | RevokePermissionForAllSubjects | GrantPermissions | AddApprovalRequest | HandleRpcRequestAction | ExecuteSnapAction | TerminateAllSnapsAction | TerminateSnapAction;
|
|
244
|
+
export declare type AllowedActions = GetEndowments | GetPermissions | GetSubjectMetadata | HasPermission | HasPermissions | RevokePermissions | RevokeAllPermissions | RevokePermissionForAllSubjects | GrantPermissions | AddApprovalRequest | HandleRpcRequestAction | ExecuteSnapAction | TerminateAllSnapsAction | TerminateSnapAction;
|
|
242
245
|
export declare type AllowedEvents = ExecutionServiceEvents;
|
|
243
246
|
declare type SnapControllerMessenger = RestrictedControllerMessenger<typeof controllerName, SnapControllerActions | AllowedActions, SnapControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
244
247
|
export declare enum AppKeyType {
|
|
245
|
-
|
|
248
|
+
StateEncryption = "stateEncryption"
|
|
246
249
|
}
|
|
247
250
|
declare type GetAppKey = (subject: string, appKeyType: AppKeyType) => Promise<string>;
|
|
248
251
|
declare type FeatureFlags = {
|
|
@@ -329,8 +332,8 @@ declare type SnapControllerArgs = {
|
|
|
329
332
|
};
|
|
330
333
|
export declare class SnapController extends BaseController<string, SnapControllerState, SnapControllerMessenger> {
|
|
331
334
|
#private;
|
|
332
|
-
private maxRequestTime;
|
|
333
|
-
private snapsRuntimeData;
|
|
335
|
+
private readonly maxRequestTime;
|
|
336
|
+
private readonly snapsRuntimeData;
|
|
334
337
|
constructor({ closeAllConnections, messenger, state, getAppKey, environmentEndowmentPermissions, npmRegistryUrl, idleTimeCheckInterval, checkBlockList, maxIdleTime, maxRequestTime, fetchFunction, featureFlags, }: SnapControllerArgs);
|
|
335
338
|
/**
|
|
336
339
|
* Checks all installed snaps against the block list and
|
|
@@ -346,9 +349,9 @@ export declare class SnapController extends BaseController<string, SnapControlle
|
|
|
346
349
|
* @returns Whether the version of the snap is blocked or not.
|
|
347
350
|
*/
|
|
348
351
|
isBlocked(snapId: ValidatedSnapId, snapInfo: SnapInfo): Promise<boolean>;
|
|
349
|
-
_onUnhandledSnapError(snapId: SnapId, error: SnapErrorJson):
|
|
350
|
-
_onOutboundRequest(snapId: SnapId):
|
|
351
|
-
_onOutboundResponse(snapId: SnapId):
|
|
352
|
+
_onUnhandledSnapError(snapId: SnapId, error: SnapErrorJson): void;
|
|
353
|
+
_onOutboundRequest(snapId: SnapId): void;
|
|
354
|
+
_onOutboundResponse(snapId: SnapId): void;
|
|
352
355
|
/**
|
|
353
356
|
* Starts the given snap. Throws an error if no such snap exists
|
|
354
357
|
* or if it is already running.
|
|
@@ -444,7 +447,7 @@ export declare class SnapController extends BaseController<string, SnapControlle
|
|
|
444
447
|
*
|
|
445
448
|
* @param snapId - The id of the Snap whose state should be cleared.
|
|
446
449
|
*/
|
|
447
|
-
clearSnapState(snapId: SnapId):
|
|
450
|
+
clearSnapState(snapId: SnapId): void;
|
|
448
451
|
/**
|
|
449
452
|
* Adds error from a snap to the SnapController state.
|
|
450
453
|
*
|
|
@@ -452,16 +455,15 @@ export declare class SnapController extends BaseController<string, SnapControlle
|
|
|
452
455
|
*/
|
|
453
456
|
addSnapError(snapError: SnapError): void;
|
|
454
457
|
/**
|
|
455
|
-
* Removes an error by internalID from
|
|
458
|
+
* Removes an error by internalID from the SnapControllers state.
|
|
456
459
|
*
|
|
457
460
|
* @param internalID - The internal error ID to remove on the SnapController.
|
|
458
461
|
*/
|
|
459
|
-
removeSnapError(internalID: string):
|
|
462
|
+
removeSnapError(internalID: string): void;
|
|
460
463
|
/**
|
|
461
464
|
* Clears all errors from the SnapControllers state.
|
|
462
|
-
*
|
|
463
465
|
*/
|
|
464
|
-
clearSnapErrors():
|
|
466
|
+
clearSnapErrors(): void;
|
|
465
467
|
/**
|
|
466
468
|
* Gets the own state of the snap with the given id.
|
|
467
469
|
* This is distinct from the state MetaMask uses to manage snaps.
|
|
@@ -475,7 +477,7 @@ export declare class SnapController extends BaseController<string, SnapControlle
|
|
|
475
477
|
* Completely clear the controller's state: delete all associated data,
|
|
476
478
|
* handlers, event listeners, and permissions; tear down all snap providers.
|
|
477
479
|
*/
|
|
478
|
-
clearState(): void
|
|
480
|
+
clearState(): Promise<void>;
|
|
479
481
|
/**
|
|
480
482
|
* Removes the given snap from state, and clears all associated handlers
|
|
481
483
|
* and listeners.
|
|
@@ -521,7 +523,7 @@ export declare class SnapController extends BaseController<string, SnapControlle
|
|
|
521
523
|
* @param origin - The origin whose permitted snaps to retrieve.
|
|
522
524
|
* @returns The serialized permitted snaps for the origin.
|
|
523
525
|
*/
|
|
524
|
-
getPermittedSnaps(origin: string):
|
|
526
|
+
getPermittedSnaps(origin: string): InstallSnapsResult;
|
|
525
527
|
/**
|
|
526
528
|
* Installs the snaps requested by the given origin, returning the snap
|
|
527
529
|
* object if the origin is permitted to install it, and an authorization error
|