@metamask/snaps-controllers 0.24.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.
Files changed (111) hide show
  1. package/LICENSE +18 -0
  2. package/README.md +3 -0
  3. package/dist/cronjob/CronjobController.d.ts +115 -0
  4. package/dist/cronjob/CronjobController.js +231 -0
  5. package/dist/cronjob/CronjobController.js.map +1 -0
  6. package/dist/cronjob/index.d.ts +1 -0
  7. package/dist/cronjob/index.js +18 -0
  8. package/dist/cronjob/index.js.map +1 -0
  9. package/dist/fsm.d.ts +24 -0
  10. package/dist/fsm.js +75 -0
  11. package/dist/fsm.js.map +1 -0
  12. package/dist/index.d.ts +6 -0
  13. package/dist/index.js +22 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/multichain/MultiChainController.d.ts +135 -0
  16. package/dist/multichain/MultiChainController.js +348 -0
  17. package/dist/multichain/MultiChainController.js.map +1 -0
  18. package/dist/multichain/index.d.ts +3 -0
  19. package/dist/multichain/index.js +20 -0
  20. package/dist/multichain/index.js.map +1 -0
  21. package/dist/multichain/matching.d.ts +9 -0
  22. package/dist/multichain/matching.js +58 -0
  23. package/dist/multichain/matching.js.map +1 -0
  24. package/dist/multichain/middleware.d.ts +14 -0
  25. package/dist/multichain/middleware.js +42 -0
  26. package/dist/multichain/middleware.js.map +1 -0
  27. package/dist/services/AbstractExecutionService.d.ts +124 -0
  28. package/dist/services/AbstractExecutionService.js +316 -0
  29. package/dist/services/AbstractExecutionService.js.map +1 -0
  30. package/dist/services/ExecutionService.d.ts +68 -0
  31. package/dist/services/ExecutionService.js +4 -0
  32. package/dist/services/ExecutionService.js.map +1 -0
  33. package/dist/services/browser.d.ts +3 -0
  34. package/dist/services/browser.js +21 -0
  35. package/dist/services/browser.js.map +1 -0
  36. package/dist/services/iframe/IframeExecutionService.d.ts +25 -0
  37. package/dist/services/iframe/IframeExecutionService.js +82 -0
  38. package/dist/services/iframe/IframeExecutionService.js.map +1 -0
  39. package/dist/services/iframe/index.d.ts +1 -0
  40. package/dist/services/iframe/index.js +18 -0
  41. package/dist/services/iframe/index.js.map +1 -0
  42. package/dist/services/iframe/test/fixJSDOMPostMessageEventSource.d.ts +3 -0
  43. package/dist/services/iframe/test/fixJSDOMPostMessageEventSource.js +74 -0
  44. package/dist/services/iframe/test/fixJSDOMPostMessageEventSource.js.map +1 -0
  45. package/dist/services/iframe/test/server.d.ts +11 -0
  46. package/dist/services/iframe/test/server.js +71 -0
  47. package/dist/services/iframe/test/server.js.map +1 -0
  48. package/dist/services/index.d.ts +4 -0
  49. package/dist/services/index.js +21 -0
  50. package/dist/services/index.js.map +1 -0
  51. package/dist/services/node/NodeProcessExecutionService.d.ts +11 -0
  52. package/dist/services/node/NodeProcessExecutionService.js +18 -0
  53. package/dist/services/node/NodeProcessExecutionService.js.map +1 -0
  54. package/dist/services/node/NodeThreadExecutionService.d.ts +11 -0
  55. package/dist/services/node/NodeThreadExecutionService.js +18 -0
  56. package/dist/services/node/NodeThreadExecutionService.js.map +1 -0
  57. package/dist/services/node/index.d.ts +2 -0
  58. package/dist/services/node/index.js +19 -0
  59. package/dist/services/node/index.js.map +1 -0
  60. package/dist/snaps/RequestQueue.d.ts +24 -0
  61. package/dist/snaps/RequestQueue.js +47 -0
  62. package/dist/snaps/RequestQueue.js.map +1 -0
  63. package/dist/snaps/SnapController.d.ts +600 -0
  64. package/dist/snaps/SnapController.js +1314 -0
  65. package/dist/snaps/SnapController.js.map +1 -0
  66. package/dist/snaps/Timer.d.ts +39 -0
  67. package/dist/snaps/Timer.js +86 -0
  68. package/dist/snaps/Timer.js.map +1 -0
  69. package/dist/snaps/endowments/cronjob.d.ts +49 -0
  70. package/dist/snaps/endowments/cronjob.js +105 -0
  71. package/dist/snaps/endowments/cronjob.js.map +1 -0
  72. package/dist/snaps/endowments/enum.d.ts +8 -0
  73. package/dist/snaps/endowments/enum.js +13 -0
  74. package/dist/snaps/endowments/enum.js.map +1 -0
  75. package/dist/snaps/endowments/ethereum-provider.d.ts +13 -0
  76. package/dist/snaps/endowments/ethereum-provider.js +31 -0
  77. package/dist/snaps/endowments/ethereum-provider.js.map +1 -0
  78. package/dist/snaps/endowments/index.d.ts +67 -0
  79. package/dist/snaps/endowments/index.js +39 -0
  80. package/dist/snaps/endowments/index.js.map +1 -0
  81. package/dist/snaps/endowments/keyring.d.ts +39 -0
  82. package/dist/snaps/endowments/keyring.js +103 -0
  83. package/dist/snaps/endowments/keyring.js.map +1 -0
  84. package/dist/snaps/endowments/long-running.d.ts +13 -0
  85. package/dist/snaps/endowments/long-running.js +28 -0
  86. package/dist/snaps/endowments/long-running.js.map +1 -0
  87. package/dist/snaps/endowments/network-access.d.ts +13 -0
  88. package/dist/snaps/endowments/network-access.js +29 -0
  89. package/dist/snaps/endowments/network-access.js.map +1 -0
  90. package/dist/snaps/endowments/transaction-insight.d.ts +38 -0
  91. package/dist/snaps/endowments/transaction-insight.js +106 -0
  92. package/dist/snaps/endowments/transaction-insight.js.map +1 -0
  93. package/dist/snaps/index.d.ts +4 -0
  94. package/dist/snaps/index.js +21 -0
  95. package/dist/snaps/index.js.map +1 -0
  96. package/dist/snaps/selectors.d.ts +2 -0
  97. package/dist/snaps/selectors.js +6 -0
  98. package/dist/snaps/selectors.js.map +1 -0
  99. package/dist/snaps/utils/index.d.ts +2 -0
  100. package/dist/snaps/utils/index.js +19 -0
  101. package/dist/snaps/utils/index.js.map +1 -0
  102. package/dist/snaps/utils/npm.d.ts +14 -0
  103. package/dist/snaps/utils/npm.js +81 -0
  104. package/dist/snaps/utils/npm.js.map +1 -0
  105. package/dist/snaps/utils/stream.d.ts +30 -0
  106. package/dist/snaps/utils/stream.js +124 -0
  107. package/dist/snaps/utils/stream.js.map +1 -0
  108. package/dist/utils.d.ts +128 -0
  109. package/dist/utils.js +92 -0
  110. package/dist/utils.js.map +1 -0
  111. package/package.json +99 -0
@@ -0,0 +1,14 @@
1
+ import { JsonRpcMiddleware, JsonRpcRequest } from 'json-rpc-engine';
2
+ import { ConnectArguments, Session, MultiChainRequest } from '@metamask/snaps-utils';
3
+ /**
4
+ * Creates a middleware that handles requests to the multichain controller.
5
+ *
6
+ * @param hooks - The hooks required by the middleware.
7
+ * @param hooks.onConnect - The onConnect hook.
8
+ * @param hooks.onRequest - The onRequest hook.
9
+ * @returns The middleware.
10
+ */
11
+ export declare function createMultiChainMiddleware({ onConnect, onRequest, }: {
12
+ onConnect: (origin: string, requestedNamespaces: ConnectArguments) => Promise<Session>;
13
+ onRequest: (origin: string, data: MultiChainRequest) => Promise<unknown>;
14
+ }): JsonRpcMiddleware<Omit<JsonRpcRequest<unknown>, 'id' | 'jsonrpc'>, any>;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createMultiChainMiddleware = void 0;
4
+ const json_rpc_engine_1 = require("json-rpc-engine");
5
+ const utils_1 = require("@metamask/utils");
6
+ const snaps_utils_1 = require("@metamask/snaps-utils");
7
+ /**
8
+ * Creates a middleware that handles requests to the multichain controller.
9
+ *
10
+ * @param hooks - The hooks required by the middleware.
11
+ * @param hooks.onConnect - The onConnect hook.
12
+ * @param hooks.onRequest - The onRequest hook.
13
+ * @returns The middleware.
14
+ */
15
+ function createMultiChainMiddleware({ onConnect, onRequest, }) {
16
+ return (0, json_rpc_engine_1.createAsyncMiddleware)(async function middleware(req, res, next) {
17
+ // This is added by other middleware
18
+ const { origin, params: unwrapped } = req;
19
+ if (req.method !== 'wallet_multiChainRequestHack') {
20
+ await next();
21
+ return;
22
+ }
23
+ (0, utils_1.assert)(unwrapped !== undefined, `Invalid params for ${req.method}`);
24
+ switch (unwrapped.method) {
25
+ case 'caip_request': {
26
+ (0, snaps_utils_1.assertIsMultiChainRequest)(unwrapped.params);
27
+ res.result = await onRequest(origin, unwrapped.params);
28
+ return;
29
+ }
30
+ case 'metamask_handshake': {
31
+ (0, snaps_utils_1.assertIsConnectArguments)(unwrapped.params);
32
+ res.result = await onConnect(origin, unwrapped.params);
33
+ return;
34
+ }
35
+ default: {
36
+ await next();
37
+ }
38
+ }
39
+ });
40
+ }
41
+ exports.createMultiChainMiddleware = createMultiChainMiddleware;
42
+ //# sourceMappingURL=middleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../src/multichain/middleware.ts"],"names":[],"mappings":";;;AAAA,qDAIyB;AACzB,2CAAyC;AACzC,uDAM+B;AAE/B;;;;;;;GAOG;AACH,SAAgB,0BAA0B,CAAC,EACzC,SAAS,EACT,SAAS,GAOV;IACC,OAAO,IAAA,uCAAqB,EAAC,KAAK,UAAU,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI;QACnE,oCAAoC;QACpC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,GAEhB,CAAC;QACvB,IAAI,GAAG,CAAC,MAAM,KAAK,8BAA8B,EAAE;YACjD,MAAM,IAAI,EAAE,CAAC;YACb,OAAO;SACR;QAED,IAAA,cAAM,EAAC,SAAS,KAAK,SAAS,EAAE,sBAAsB,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;QAEpE,QAAQ,SAAS,CAAC,MAAM,EAAE;YACxB,KAAK,cAAc,CAAC,CAAC;gBACnB,IAAA,uCAAyB,EAAC,SAAS,CAAC,MAAM,CAAC,CAAC;gBAC5C,GAAG,CAAC,MAAM,GAAG,MAAM,SAAS,CAAC,MAAM,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;gBACvD,OAAO;aACR;YAED,KAAK,oBAAoB,CAAC,CAAC;gBACzB,IAAA,sCAAwB,EAAC,SAAS,CAAC,MAAM,CAAC,CAAC;gBAC3C,GAAG,CAAC,MAAM,GAAG,MAAM,SAAS,CAAC,MAAM,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;gBACvD,OAAO;aACR;YAED,OAAO,CAAC,CAAC;gBACP,MAAM,IAAI,EAAE,CAAC;aACd;SACF;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AAxCD,gEAwCC","sourcesContent":["import {\n createAsyncMiddleware,\n JsonRpcMiddleware,\n JsonRpcRequest,\n} from 'json-rpc-engine';\nimport { assert } from '@metamask/utils';\nimport {\n assertIsConnectArguments,\n assertIsMultiChainRequest,\n ConnectArguments,\n Session,\n MultiChainRequest,\n} from '@metamask/snaps-utils';\n\n/**\n * Creates a middleware that handles requests to the multichain controller.\n *\n * @param hooks - The hooks required by the middleware.\n * @param hooks.onConnect - The onConnect hook.\n * @param hooks.onRequest - The onRequest hook.\n * @returns The middleware.\n */\nexport function createMultiChainMiddleware({\n onConnect,\n onRequest,\n}: {\n onConnect: (\n origin: string,\n requestedNamespaces: ConnectArguments,\n ) => Promise<Session>;\n onRequest: (origin: string, data: MultiChainRequest) => Promise<unknown>;\n}): JsonRpcMiddleware<Omit<JsonRpcRequest<unknown>, 'id' | 'jsonrpc'>, any> {\n return createAsyncMiddleware(async function middleware(req, res, next) {\n // This is added by other middleware\n const { origin, params: unwrapped } = req as JsonRpcRequest<\n JsonRpcRequest<unknown>\n > & { origin: string };\n if (req.method !== 'wallet_multiChainRequestHack') {\n await next();\n return;\n }\n\n assert(unwrapped !== undefined, `Invalid params for ${req.method}`);\n\n switch (unwrapped.method) {\n case 'caip_request': {\n assertIsMultiChainRequest(unwrapped.params);\n res.result = await onRequest(origin, unwrapped.params);\n return;\n }\n\n case 'metamask_handshake': {\n assertIsConnectArguments(unwrapped.params);\n res.result = await onConnect(origin, unwrapped.params);\n return;\n }\n\n default: {\n await next();\n }\n }\n });\n}\n"]}
@@ -0,0 +1,124 @@
1
+ /// <reference types="node" />
2
+ import { Duplex } from 'stream';
3
+ import ObjectMultiplex from '@metamask/object-multiplex';
4
+ import { SnapRpcHookArgs } from '@metamask/snaps-utils';
5
+ import { BasePostMessageStream } from '@metamask/post-message-stream';
6
+ import { JsonRpcEngine } from 'json-rpc-engine';
7
+ import { ExecutionService, ExecutionServiceMessenger, SnapExecutionData } from './ExecutionService';
8
+ export declare type SetupSnapProvider = (snapId: string, stream: Duplex) => void;
9
+ export declare type ExecutionServiceArgs = {
10
+ setupSnapProvider: SetupSnapProvider;
11
+ messenger: ExecutionServiceMessenger;
12
+ terminationTimeout?: number;
13
+ };
14
+ export declare type JobStreams = {
15
+ command: Duplex;
16
+ rpc: Duplex;
17
+ _connection: BasePostMessageStream;
18
+ };
19
+ export declare type Job<WorkerType> = {
20
+ id: string;
21
+ streams: JobStreams;
22
+ rpcEngine: JsonRpcEngine;
23
+ worker: WorkerType;
24
+ };
25
+ export declare abstract class AbstractExecutionService<WorkerType> implements ExecutionService {
26
+ #private;
27
+ protected jobs: Map<string, Job<WorkerType>>;
28
+ private setupSnapProvider;
29
+ constructor({ setupSnapProvider, messenger, terminationTimeout, }: ExecutionServiceArgs);
30
+ /**
31
+ * Constructor helper for registering the controller's messaging system
32
+ * actions.
33
+ */
34
+ private registerMessageHandlers;
35
+ /**
36
+ * Performs additional necessary work during job termination. **MUST** be
37
+ * implemented by concrete implementations. See
38
+ * {@link AbstractExecutionService.terminate} for details.
39
+ *
40
+ * @param job - The object corresponding to the job to be terminated.
41
+ */
42
+ protected abstract terminateJob(job: Job<WorkerType>): void;
43
+ /**
44
+ * Terminates the job with the specified ID and deletes all its associated
45
+ * data. Any subsequent messages targeting the job will fail with an error.
46
+ * Throws an error if the specified job does not exist, or if termination
47
+ * fails unexpectedly.
48
+ *
49
+ * @param jobId - The id of the job to be terminated.
50
+ */
51
+ terminate(jobId: string): Promise<void>;
52
+ /**
53
+ * Initiates a job for a snap.
54
+ *
55
+ * 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.
56
+ *
57
+ * @returns Information regarding the created job.
58
+ */
59
+ protected initJob(): Promise<Job<WorkerType>>;
60
+ /**
61
+ * Sets up the streams for an initiated job.
62
+ *
63
+ * 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.
64
+ *
65
+ * @param jobId - The id of the job.
66
+ * @returns The streams to communicate with the worker and the worker itself.
67
+ */
68
+ protected initStreams(jobId: string): Promise<{
69
+ streams: JobStreams;
70
+ worker: WorkerType;
71
+ }>;
72
+ /**
73
+ * Abstract function implemented by implementing class that spins up a new worker for a job.
74
+ *
75
+ * 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.
76
+ */
77
+ protected abstract initEnvStream(jobId: string): Promise<{
78
+ worker: WorkerType;
79
+ stream: BasePostMessageStream;
80
+ }>;
81
+ /**
82
+ * Terminates the Snap with the specified ID. May throw an error if
83
+ * termination unexpectedly fails, but will not fail if no job for the snap
84
+ * with the specified ID is found.
85
+ *
86
+ * @param snapId - The ID of the snap to terminate.
87
+ */
88
+ terminateSnap(snapId: string): Promise<void>;
89
+ terminateAllSnaps(): Promise<void>;
90
+ /**
91
+ * Gets the RPC request handler for the given snap.
92
+ *
93
+ * @param snapId - The id of the Snap whose message handler to get.
94
+ * @returns The RPC request handler for the snap.
95
+ */
96
+ private getRpcRequestHandler;
97
+ /**
98
+ * Initializes and executes a snap, setting up the communication channels to the snap etc.
99
+ *
100
+ * 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.
101
+ *
102
+ * @param snapData - Data needed for Snap execution.
103
+ * @returns A string `OK` if execution succeeded.
104
+ * @throws If the execution service returns an error.
105
+ */
106
+ executeSnap(snapData: SnapExecutionData): Promise<string>;
107
+ private command;
108
+ /**
109
+ * Handle RPC request.
110
+ *
111
+ * @param snapId - The ID of the recipient snap.
112
+ * @param options - Bag of options to pass to the RPC handler.
113
+ * @returns Promise that can handle the request.
114
+ */
115
+ handleRpcRequest(snapId: string, options: SnapRpcHookArgs): Promise<unknown>;
116
+ }
117
+ /**
118
+ * Sets up stream multiplexing for the given stream.
119
+ *
120
+ * @param connectionStream - The stream to mux.
121
+ * @param streamName - The name of the stream, for identification in errors.
122
+ * @returns The multiplexed stream.
123
+ */
124
+ export declare function setupMultiplex(connectionStream: Duplex, streamName: string): ObjectMultiplex;
@@ -0,0 +1,316 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ var _AbstractExecutionService_instances, _AbstractExecutionService_snapRpcHooks, _AbstractExecutionService_snapToJobMap, _AbstractExecutionService_jobToSnapMap, _AbstractExecutionService_messenger, _AbstractExecutionService_terminationTimeout, _AbstractExecutionService_removeSnapHooks, _AbstractExecutionService_createSnapHooks, _AbstractExecutionService_getJobForSnap, _AbstractExecutionService_getSnapForJob, _AbstractExecutionService_mapSnapAndJob, _AbstractExecutionService_removeSnapAndJobMapping;
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.setupMultiplex = exports.AbstractExecutionService = void 0;
19
+ const object_multiplex_1 = __importDefault(require("@metamask/object-multiplex"));
20
+ const snaps_utils_1 = require("@metamask/snaps-utils");
21
+ const utils_1 = require("@metamask/utils");
22
+ const json_rpc_engine_1 = require("json-rpc-engine");
23
+ const json_rpc_middleware_stream_1 = require("json-rpc-middleware-stream");
24
+ const nanoid_1 = require("nanoid");
25
+ const pump_1 = __importDefault(require("pump"));
26
+ const utils_2 = require("../utils");
27
+ const controllerName = 'ExecutionService';
28
+ class AbstractExecutionService {
29
+ constructor({ setupSnapProvider, messenger, terminationTimeout = utils_1.Duration.Second, }) {
30
+ _AbstractExecutionService_instances.add(this);
31
+ _AbstractExecutionService_snapRpcHooks.set(this, void 0);
32
+ _AbstractExecutionService_snapToJobMap.set(this, void 0);
33
+ _AbstractExecutionService_jobToSnapMap.set(this, void 0);
34
+ _AbstractExecutionService_messenger.set(this, void 0);
35
+ _AbstractExecutionService_terminationTimeout.set(this, void 0);
36
+ __classPrivateFieldSet(this, _AbstractExecutionService_snapRpcHooks, new Map(), "f");
37
+ this.jobs = new Map();
38
+ this.setupSnapProvider = setupSnapProvider;
39
+ __classPrivateFieldSet(this, _AbstractExecutionService_snapToJobMap, new Map(), "f");
40
+ __classPrivateFieldSet(this, _AbstractExecutionService_jobToSnapMap, new Map(), "f");
41
+ __classPrivateFieldSet(this, _AbstractExecutionService_messenger, messenger, "f");
42
+ __classPrivateFieldSet(this, _AbstractExecutionService_terminationTimeout, terminationTimeout, "f");
43
+ this.registerMessageHandlers();
44
+ }
45
+ /**
46
+ * Constructor helper for registering the controller's messaging system
47
+ * actions.
48
+ */
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());
54
+ }
55
+ /**
56
+ * Terminates the job with the specified ID and deletes all its associated
57
+ * data. Any subsequent messages targeting the job will fail with an error.
58
+ * Throws an error if the specified job does not exist, or if termination
59
+ * fails unexpectedly.
60
+ *
61
+ * @param jobId - The id of the job to be terminated.
62
+ */
63
+ async terminate(jobId) {
64
+ const jobWrapper = this.jobs.get(jobId);
65
+ if (!jobWrapper) {
66
+ throw new Error(`Job with id "${jobId}" not found.`);
67
+ }
68
+ // Ping worker and tell it to run teardown, continue with termination if it takes too long
69
+ const result = await (0, utils_2.withTimeout)(this.command(jobId, {
70
+ jsonrpc: '2.0',
71
+ method: 'terminate',
72
+ params: [],
73
+ id: (0, nanoid_1.nanoid)(),
74
+ }), __classPrivateFieldGet(this, _AbstractExecutionService_terminationTimeout, "f"));
75
+ if (result === utils_2.hasTimedOut || result !== 'OK') {
76
+ // We tried to shutdown gracefully but failed. This probably means the Snap is in infite loop and
77
+ // hogging down the whole JS process.
78
+ // TODO(ritave): It might be doing weird things such as posting a lot of setTimeouts. Add a test to ensure that this behaviour
79
+ // doesn't leak into other workers. Especially important in IframeExecutionEnvironment since they all share the same
80
+ // JS process.
81
+ console.error(`Job "${jobId}" failed to terminate gracefully.`, result);
82
+ }
83
+ Object.values(jobWrapper.streams).forEach((stream) => {
84
+ try {
85
+ !stream.destroyed && stream.destroy();
86
+ stream.removeAllListeners();
87
+ }
88
+ catch (err) {
89
+ console.error('Error while destroying stream', err);
90
+ }
91
+ });
92
+ this.terminateJob(jobWrapper);
93
+ __classPrivateFieldGet(this, _AbstractExecutionService_instances, "m", _AbstractExecutionService_removeSnapAndJobMapping).call(this, jobId);
94
+ this.jobs.delete(jobId);
95
+ console.log(`Job "${jobId}" terminated.`);
96
+ }
97
+ /**
98
+ * Initiates a job for a snap.
99
+ *
100
+ * 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.
101
+ *
102
+ * @returns Information regarding the created job.
103
+ */
104
+ async initJob() {
105
+ const jobId = (0, nanoid_1.nanoid)();
106
+ const { streams, worker } = await this.initStreams(jobId);
107
+ const rpcEngine = new json_rpc_engine_1.JsonRpcEngine();
108
+ const jsonRpcConnection = (0, json_rpc_middleware_stream_1.createStreamMiddleware)();
109
+ (0, pump_1.default)(jsonRpcConnection.stream, streams.command, jsonRpcConnection.stream);
110
+ rpcEngine.push(jsonRpcConnection.middleware);
111
+ const envMetadata = {
112
+ id: jobId,
113
+ streams,
114
+ rpcEngine,
115
+ worker,
116
+ };
117
+ this.jobs.set(jobId, envMetadata);
118
+ return envMetadata;
119
+ }
120
+ /**
121
+ * Sets up the streams for an initiated job.
122
+ *
123
+ * 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.
124
+ *
125
+ * @param jobId - The id of the job.
126
+ * @returns The streams to communicate with the worker and the worker itself.
127
+ */
128
+ async initStreams(jobId) {
129
+ const { worker, stream: envStream } = await this.initEnvStream(jobId);
130
+ // Typecast justification: stream type mismatch
131
+ const mux = setupMultiplex(envStream, `Job: "${jobId}"`);
132
+ const commandStream = mux.createStream(snaps_utils_1.SNAP_STREAM_NAMES.COMMAND);
133
+ // Handle out-of-band errors, i.e. errors thrown from the snap outside of the req/res cycle.
134
+ // Also keep track of outbound request/responses
135
+ const notificationHandler = (message) => {
136
+ if (!(0, utils_1.isJsonRpcNotification)(message)) {
137
+ return;
138
+ }
139
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
140
+ const snapId = __classPrivateFieldGet(this, _AbstractExecutionService_jobToSnapMap, "f").get(jobId);
141
+ if (message.method === 'OutboundRequest') {
142
+ __classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").publish('ExecutionService:outboundRequest', snapId);
143
+ }
144
+ else if (message.method === 'OutboundResponse') {
145
+ __classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").publish('ExecutionService:outboundResponse', snapId);
146
+ }
147
+ else if (message.method === 'UnhandledError') {
148
+ if ((0, utils_1.isObject)(message.params) && message.params.error) {
149
+ __classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").publish('ExecutionService:unhandledError', snapId, message.params.error);
150
+ commandStream.removeListener('data', notificationHandler);
151
+ }
152
+ else {
153
+ console.error(new Error(`Received malformed "${message.method}" command stream notification.`));
154
+ }
155
+ }
156
+ else {
157
+ console.error(new Error(`Received unexpected command stream notification "${message.method}".`));
158
+ }
159
+ };
160
+ commandStream.on('data', notificationHandler);
161
+ const rpcStream = mux.createStream(snaps_utils_1.SNAP_STREAM_NAMES.JSON_RPC);
162
+ // Typecast: stream type mismatch
163
+ return {
164
+ streams: {
165
+ command: commandStream,
166
+ rpc: rpcStream,
167
+ _connection: envStream,
168
+ },
169
+ worker,
170
+ };
171
+ }
172
+ /**
173
+ * Terminates the Snap with the specified ID. May throw an error if
174
+ * termination unexpectedly fails, but will not fail if no job for the snap
175
+ * with the specified ID is found.
176
+ *
177
+ * @param snapId - The ID of the snap to terminate.
178
+ */
179
+ async terminateSnap(snapId) {
180
+ const jobId = __classPrivateFieldGet(this, _AbstractExecutionService_snapToJobMap, "f").get(snapId);
181
+ if (jobId) {
182
+ await this.terminate(jobId);
183
+ }
184
+ }
185
+ async terminateAllSnaps() {
186
+ await Promise.all([...this.jobs.keys()].map((jobId) => this.terminate(jobId)));
187
+ __classPrivateFieldGet(this, _AbstractExecutionService_snapRpcHooks, "f").clear();
188
+ }
189
+ /**
190
+ * Gets the RPC request handler for the given snap.
191
+ *
192
+ * @param snapId - The id of the Snap whose message handler to get.
193
+ * @returns The RPC request handler for the snap.
194
+ */
195
+ async getRpcRequestHandler(snapId) {
196
+ return __classPrivateFieldGet(this, _AbstractExecutionService_snapRpcHooks, "f").get(snapId);
197
+ }
198
+ /**
199
+ * Initializes and executes a snap, setting up the communication channels to the snap etc.
200
+ *
201
+ * 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.
202
+ *
203
+ * @param snapData - Data needed for Snap execution.
204
+ * @returns A string `OK` if execution succeeded.
205
+ * @throws If the execution service returns an error.
206
+ */
207
+ async executeSnap(snapData) {
208
+ if (__classPrivateFieldGet(this, _AbstractExecutionService_snapToJobMap, "f").has(snapData.snapId)) {
209
+ throw new Error(`Snap "${snapData.snapId}" is already being executed.`);
210
+ }
211
+ const job = await this.initJob();
212
+ __classPrivateFieldGet(this, _AbstractExecutionService_instances, "m", _AbstractExecutionService_mapSnapAndJob).call(this, snapData.snapId, job.id);
213
+ // Ping the worker to ensure that it started up
214
+ await this.command(job.id, {
215
+ jsonrpc: '2.0',
216
+ method: 'ping',
217
+ id: (0, nanoid_1.nanoid)(),
218
+ });
219
+ const rpcStream = job.streams.rpc;
220
+ this.setupSnapProvider(snapData.snapId, rpcStream);
221
+ const result = await this.command(job.id, {
222
+ jsonrpc: '2.0',
223
+ method: 'executeSnap',
224
+ params: snapData,
225
+ id: (0, nanoid_1.nanoid)(),
226
+ });
227
+ __classPrivateFieldGet(this, _AbstractExecutionService_instances, "m", _AbstractExecutionService_createSnapHooks).call(this, snapData.snapId, job.id);
228
+ return result;
229
+ }
230
+ // Cannot be hash private yet because of tests.
231
+ async command(jobId, message) {
232
+ if (typeof message !== 'object') {
233
+ throw new Error('Must send object.');
234
+ }
235
+ const job = this.jobs.get(jobId);
236
+ if (!job) {
237
+ throw new Error(`Job with id "${jobId}" not found.`);
238
+ }
239
+ console.log('Parent: Sending Command', message);
240
+ const response = await job.rpcEngine.handle(message);
241
+ if (response.error) {
242
+ throw new Error(response.error.message);
243
+ }
244
+ return response.result;
245
+ }
246
+ /**
247
+ * Handle RPC request.
248
+ *
249
+ * @param snapId - The ID of the recipient snap.
250
+ * @param options - Bag of options to pass to the RPC handler.
251
+ * @returns Promise that can handle the request.
252
+ */
253
+ async handleRpcRequest(snapId, options) {
254
+ const rpcRequestHandler = await this.getRpcRequestHandler(snapId);
255
+ if (!rpcRequestHandler) {
256
+ throw new Error(`Snap execution service returned no RPC handler for running snap "${snapId}".`);
257
+ }
258
+ return rpcRequestHandler(options);
259
+ }
260
+ }
261
+ exports.AbstractExecutionService = AbstractExecutionService;
262
+ _AbstractExecutionService_snapRpcHooks = new WeakMap(), _AbstractExecutionService_snapToJobMap = new WeakMap(), _AbstractExecutionService_jobToSnapMap = new WeakMap(), _AbstractExecutionService_messenger = new WeakMap(), _AbstractExecutionService_terminationTimeout = new WeakMap(), _AbstractExecutionService_instances = new WeakSet(), _AbstractExecutionService_removeSnapHooks = function _AbstractExecutionService_removeSnapHooks(snapId) {
263
+ __classPrivateFieldGet(this, _AbstractExecutionService_snapRpcHooks, "f").delete(snapId);
264
+ }, _AbstractExecutionService_createSnapHooks = function _AbstractExecutionService_createSnapHooks(snapId, workerId) {
265
+ const rpcHook = async ({ origin, handler, request }) => {
266
+ return await this.command(workerId, {
267
+ id: (0, nanoid_1.nanoid)(),
268
+ jsonrpc: '2.0',
269
+ method: 'snapRpc',
270
+ params: {
271
+ origin,
272
+ handler,
273
+ request,
274
+ target: snapId,
275
+ },
276
+ });
277
+ };
278
+ __classPrivateFieldGet(this, _AbstractExecutionService_snapRpcHooks, "f").set(snapId, rpcHook);
279
+ }, _AbstractExecutionService_getJobForSnap = function _AbstractExecutionService_getJobForSnap(snapId) {
280
+ return __classPrivateFieldGet(this, _AbstractExecutionService_snapToJobMap, "f").get(snapId);
281
+ }, _AbstractExecutionService_getSnapForJob = function _AbstractExecutionService_getSnapForJob(jobId) {
282
+ return __classPrivateFieldGet(this, _AbstractExecutionService_jobToSnapMap, "f").get(jobId);
283
+ }, _AbstractExecutionService_mapSnapAndJob = function _AbstractExecutionService_mapSnapAndJob(snapId, jobId) {
284
+ __classPrivateFieldGet(this, _AbstractExecutionService_snapToJobMap, "f").set(snapId, jobId);
285
+ __classPrivateFieldGet(this, _AbstractExecutionService_jobToSnapMap, "f").set(jobId, snapId);
286
+ }, _AbstractExecutionService_removeSnapAndJobMapping = function _AbstractExecutionService_removeSnapAndJobMapping(jobId) {
287
+ const snapId = __classPrivateFieldGet(this, _AbstractExecutionService_jobToSnapMap, "f").get(jobId);
288
+ if (!snapId) {
289
+ throw new Error(`job: "${jobId}" has no mapped snap.`);
290
+ }
291
+ __classPrivateFieldGet(this, _AbstractExecutionService_jobToSnapMap, "f").delete(jobId);
292
+ __classPrivateFieldGet(this, _AbstractExecutionService_snapToJobMap, "f").delete(snapId);
293
+ __classPrivateFieldGet(this, _AbstractExecutionService_instances, "m", _AbstractExecutionService_removeSnapHooks).call(this, snapId);
294
+ };
295
+ /**
296
+ * Sets up stream multiplexing for the given stream.
297
+ *
298
+ * @param connectionStream - The stream to mux.
299
+ * @param streamName - The name of the stream, for identification in errors.
300
+ * @returns The multiplexed stream.
301
+ */
302
+ function setupMultiplex(connectionStream, streamName) {
303
+ const mux = new object_multiplex_1.default();
304
+ (0, pump_1.default)(connectionStream,
305
+ // Typecast: stream type mismatch
306
+ mux, connectionStream, (err) => {
307
+ if (err) {
308
+ streamName
309
+ ? console.error(`"${streamName}" stream failure.`, err)
310
+ : console.error(err);
311
+ }
312
+ });
313
+ return mux;
314
+ }
315
+ exports.setupMultiplex = setupMultiplex;
316
+ //# sourceMappingURL=AbstractExecutionService.js.map
@@ -0,0 +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,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;QAC/B,IAAI,CAAC,IAAI,GAAG,IAAI,GAAG,EAAE,CAAC;QACtB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C,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"]}
@@ -0,0 +1,68 @@
1
+ import { RestrictedControllerMessenger } from '@metamask/controllers';
2
+ import { SnapId, SnapRpcHookArgs } from '@metamask/snaps-utils';
3
+ import { Json } from '@metamask/types';
4
+ declare type TerminateSnap = (snapId: string) => Promise<void>;
5
+ declare type TerminateAll = () => Promise<void>;
6
+ declare type ExecuteSnap = (snapData: SnapExecutionData) => Promise<unknown>;
7
+ declare type HandleRpcRequest = (snapId: string, options: SnapRpcHookArgs) => Promise<unknown>;
8
+ export interface ExecutionService {
9
+ terminateSnap: TerminateSnap;
10
+ terminateAllSnaps: TerminateAll;
11
+ executeSnap: ExecuteSnap;
12
+ handleRpcRequest: HandleRpcRequest;
13
+ }
14
+ export declare type SnapExecutionData = {
15
+ snapId: string;
16
+ sourceCode: string;
17
+ endowments?: Json;
18
+ };
19
+ export declare type SnapErrorJson = {
20
+ message: string;
21
+ code: number;
22
+ data?: Json;
23
+ };
24
+ declare const controllerName = "ExecutionService";
25
+ export declare type ErrorMessageEvent = {
26
+ type: 'ExecutionService:unhandledError';
27
+ payload: [SnapId, SnapErrorJson];
28
+ };
29
+ export declare type OutboundRequest = {
30
+ type: 'ExecutionService:outboundRequest';
31
+ payload: [SnapId];
32
+ };
33
+ export declare type OutboundResponse = {
34
+ type: 'ExecutionService:outboundResponse';
35
+ payload: [SnapId];
36
+ };
37
+ export declare type ExecutionServiceEvents = ErrorMessageEvent | OutboundRequest | OutboundResponse;
38
+ /**
39
+ * Handles RPC request.
40
+ */
41
+ export declare type HandleRpcRequestAction = {
42
+ type: `${typeof controllerName}:handleRpcRequest`;
43
+ handler: ExecutionService['handleRpcRequest'];
44
+ };
45
+ /**
46
+ * Executes a given snap.
47
+ */
48
+ export declare type ExecuteSnapAction = {
49
+ type: `${typeof controllerName}:executeSnap`;
50
+ handler: ExecutionService['executeSnap'];
51
+ };
52
+ /**
53
+ * Terminates a given snap.
54
+ */
55
+ export declare type TerminateSnapAction = {
56
+ type: `${typeof controllerName}:terminateSnap`;
57
+ handler: ExecutionService['terminateSnap'];
58
+ };
59
+ /**
60
+ * Terminates all snaps.
61
+ */
62
+ export declare type TerminateAllSnapsAction = {
63
+ type: `${typeof controllerName}:terminateAllSnaps`;
64
+ handler: ExecutionService['terminateAllSnaps'];
65
+ };
66
+ export declare type ExecutionServiceActions = HandleRpcRequestAction | ExecuteSnapAction | TerminateSnapAction | TerminateAllSnapsAction;
67
+ export declare type ExecutionServiceMessenger = RestrictedControllerMessenger<'ExecutionService', ExecutionServiceActions, ExecutionServiceEvents, ExecutionServiceActions['type'], ExecutionServiceEvents['type']>;
68
+ export {};
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const controllerName = 'ExecutionService';
4
+ //# sourceMappingURL=ExecutionService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ExecutionService.js","sourceRoot":"","sources":["../../src/services/ExecutionService.ts"],"names":[],"mappings":";;AAiCA,MAAM,cAAc,GAAG,kBAAkB,CAAC","sourcesContent":["import { RestrictedControllerMessenger } from '@metamask/controllers';\nimport { SnapId, SnapRpcHookArgs } from '@metamask/snaps-utils';\n\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"]}
@@ -0,0 +1,3 @@
1
+ export * from './AbstractExecutionService';
2
+ export * from './ExecutionService';
3
+ export * from './iframe';