@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,600 @@
1
+ import { AddApprovalRequest, BaseControllerV2 as BaseController, GetEndowments, GetPermissions, GrantPermissions, HasPermission, HasPermissions, RestrictedControllerMessenger, RevokeAllPermissions, RevokePermissionForAllSubjects, RevokePermissions } from '@metamask/controllers';
2
+ import { BlockedSnapInfo, InstallSnapsResult, PersistedSnap, RequestedSnapPermissions, Snap, SnapId, SnapManifest, SnapRpcHook, SnapRpcHookArgs, SnapStatusEvents, StatusContext, StatusEvents, StatusStates, TruncatedSnap, ValidatedSnapId } from '@metamask/snaps-utils';
3
+ import { Json } from '@metamask/utils';
4
+ import { StateMachine } from '@xstate/fsm';
5
+ import type { Patch } from 'immer';
6
+ import { ExecuteSnapAction, ExecutionServiceEvents, HandleRpcRequestAction, SnapErrorJson, TerminateAllSnapsAction, TerminateSnapAction } from '../services/ExecutionService';
7
+ import { Timer } from './Timer';
8
+ export declare const controllerName = "SnapController";
9
+ export declare const SNAP_APPROVAL_INSTALL = "wallet_installSnap";
10
+ export declare const SNAP_APPROVAL_UPDATE = "wallet_updateSnap";
11
+ export declare type PendingRequest = {
12
+ requestId: unknown;
13
+ timer: Timer;
14
+ };
15
+ /**
16
+ * A wrapper type for any data stored during runtime of Snaps.
17
+ * It is not persisted in state as it contains non-serializable data and is only relevant for the
18
+ * current session.
19
+ */
20
+ export interface SnapRuntimeData {
21
+ /**
22
+ * A promise that resolves when the Snap has finished installing
23
+ */
24
+ installPromise: null | Promise<PersistedSnap>;
25
+ /**
26
+ * A Unix timestamp for the last time the Snap received an RPC request
27
+ */
28
+ lastRequest: null | number;
29
+ /**
30
+ * The current number of active references where this Snap is being used
31
+ */
32
+ activeReferences: number;
33
+ /**
34
+ * The current pending inbound requests, meaning requests that are processed by snaps.
35
+ */
36
+ pendingInboundRequests: PendingRequest[];
37
+ /**
38
+ * The current pending outbound requests, meaning requests made from snaps towards the MetaMask
39
+ * extension.
40
+ */
41
+ pendingOutboundRequests: number;
42
+ /**
43
+ * RPC handler designated for the Snap
44
+ */
45
+ rpcHandler: null | SnapRpcHook;
46
+ /**
47
+ * The finite state machine interpreter for possible states that the Snap can be in such as
48
+ * stopped, running, blocked
49
+ *
50
+ * @see {@link SnapController:constructor}
51
+ */
52
+ interpreter: StateMachine.Service<StatusContext, StatusEvents, StatusStates>;
53
+ /**
54
+ * The snap source code
55
+ */
56
+ sourceCode: null | string;
57
+ /**
58
+ * The snap state (encrypted)
59
+ */
60
+ state: null | string;
61
+ }
62
+ export declare type SnapError = {
63
+ message: string;
64
+ code: number;
65
+ data?: Json;
66
+ };
67
+ /**
68
+ * The return type of {@link SnapController.fetchSnap} and its sibling methods.
69
+ */
70
+ declare type FetchSnapResult = {
71
+ /**
72
+ * The manifest of the fetched Snap.
73
+ */
74
+ manifest: SnapManifest;
75
+ /**
76
+ * The source code of the fetched Snap.
77
+ */
78
+ sourceCode: string;
79
+ /**
80
+ * The raw XML content of the Snap's SVG icon, if any.
81
+ */
82
+ svgIcon?: string;
83
+ };
84
+ declare type CloseAllConnectionsFunction = (origin: string) => void;
85
+ declare type StoredSnaps = Record<SnapId, Snap>;
86
+ export declare type SnapControllerState = {
87
+ snaps: StoredSnaps;
88
+ snapStates: {};
89
+ snapErrors: {
90
+ [internalID: string]: SnapError & {
91
+ internalID: string;
92
+ };
93
+ };
94
+ };
95
+ export declare type PersistedSnapControllerState = SnapControllerState & {
96
+ snaps: Record<SnapId, PersistedSnap>;
97
+ snapStates: Record<SnapId, string>;
98
+ };
99
+ /**
100
+ * Gets the specified Snap from state.
101
+ */
102
+ export declare type GetSnap = {
103
+ type: `${typeof controllerName}:get`;
104
+ handler: SnapController['get'];
105
+ };
106
+ /**
107
+ * Handles sending an inbound request to a snap and returns its result.
108
+ */
109
+ export declare type HandleSnapRequest = {
110
+ type: `${typeof controllerName}:handleRequest`;
111
+ handler: SnapController['handleRequest'];
112
+ };
113
+ /**
114
+ * Gets the specified Snap's persisted state.
115
+ */
116
+ export declare type GetSnapState = {
117
+ type: `${typeof controllerName}:getSnapState`;
118
+ handler: SnapController['getSnapState'];
119
+ };
120
+ /**
121
+ * Checks if the specified snap exists in state.
122
+ */
123
+ export declare type HasSnap = {
124
+ type: `${typeof controllerName}:has`;
125
+ handler: SnapController['has'];
126
+ };
127
+ /**
128
+ * Updates the specified Snap's persisted state.
129
+ */
130
+ export declare type UpdateSnapState = {
131
+ type: `${typeof controllerName}:updateSnapState`;
132
+ handler: SnapController['updateSnapState'];
133
+ };
134
+ /**
135
+ * Clears the specified Snap's persisted state.
136
+ */
137
+ export declare type ClearSnapState = {
138
+ type: `${typeof controllerName}:clearSnapState`;
139
+ handler: SnapController['clearSnapState'];
140
+ };
141
+ /**
142
+ * Checks all installed snaps against the blocklist.
143
+ */
144
+ export declare type UpdateBlockedSnaps = {
145
+ type: `${typeof controllerName}:updateBlockedSnaps`;
146
+ handler: SnapController['updateBlockedSnaps'];
147
+ };
148
+ export declare type EnableSnap = {
149
+ type: `${typeof controllerName}:enable`;
150
+ handler: SnapController['enableSnap'];
151
+ };
152
+ export declare type DisableSnap = {
153
+ type: `${typeof controllerName}:disable`;
154
+ handler: SnapController['disableSnap'];
155
+ };
156
+ export declare type RemoveSnap = {
157
+ type: `${typeof controllerName}:remove`;
158
+ handler: SnapController['removeSnap'];
159
+ };
160
+ export declare type GetPermittedSnaps = {
161
+ type: `${typeof controllerName}:getPermitted`;
162
+ handler: SnapController['getPermittedSnaps'];
163
+ };
164
+ export declare type GetAllSnaps = {
165
+ type: `${typeof controllerName}:getAll`;
166
+ handler: SnapController['getAllSnaps'];
167
+ };
168
+ export declare type IncrementActiveReferences = {
169
+ type: `${typeof controllerName}:incrementActiveReferences`;
170
+ handler: SnapController['incrementActiveReferences'];
171
+ };
172
+ export declare type DecrementActiveReferences = {
173
+ type: `${typeof controllerName}:decrementActiveReferences`;
174
+ handler: SnapController['decrementActiveReferences'];
175
+ };
176
+ export declare type InstallSnaps = {
177
+ type: `${typeof controllerName}:install`;
178
+ handler: SnapController['installSnaps'];
179
+ };
180
+ export declare type RemoveSnapError = {
181
+ type: `${typeof controllerName}:removeSnapError`;
182
+ handler: SnapController['removeSnapError'];
183
+ };
184
+ export declare type SnapControllerActions = ClearSnapState | GetSnap | GetSnapState | HandleSnapRequest | HasSnap | UpdateBlockedSnaps | UpdateSnapState | EnableSnap | DisableSnap | RemoveSnap | GetPermittedSnaps | InstallSnaps | RemoveSnapError | GetAllSnaps | IncrementActiveReferences | DecrementActiveReferences;
185
+ export declare type SnapStateChange = {
186
+ type: `${typeof controllerName}:stateChange`;
187
+ payload: [SnapControllerState, Patch[]];
188
+ };
189
+ /**
190
+ * Emitted when a Snap has been added to state during installation.
191
+ */
192
+ export declare type SnapAdded = {
193
+ type: `${typeof controllerName}:snapAdded`;
194
+ payload: [snap: Snap, svgIcon: string | undefined];
195
+ };
196
+ /**
197
+ * Emitted when an installed snap has been blocked.
198
+ */
199
+ export declare type SnapBlocked = {
200
+ type: `${typeof controllerName}:snapBlocked`;
201
+ payload: [snapId: string, blockedSnapInfo: BlockedSnapInfo];
202
+ };
203
+ /**
204
+ * Emitted when a snap has been started after being added and authorized during
205
+ * installation.
206
+ */
207
+ export declare type SnapInstalled = {
208
+ type: `${typeof controllerName}:snapInstalled`;
209
+ payload: [snap: TruncatedSnap];
210
+ };
211
+ /**
212
+ * Emitted when a snap is removed.
213
+ */
214
+ export declare type SnapRemoved = {
215
+ type: `${typeof controllerName}:snapRemoved`;
216
+ payload: [snap: TruncatedSnap];
217
+ };
218
+ /**
219
+ * Emitted when an installed snap has been unblocked.
220
+ */
221
+ export declare type SnapUnblocked = {
222
+ type: `${typeof controllerName}:snapUnblocked`;
223
+ payload: [snapId: string];
224
+ };
225
+ /**
226
+ * Emitted when a snap is updated.
227
+ */
228
+ export declare type SnapUpdated = {
229
+ type: `${typeof controllerName}:snapUpdated`;
230
+ payload: [snap: TruncatedSnap, oldVersion: string];
231
+ };
232
+ /**
233
+ * Emitted when a Snap is terminated. This is different from the snap being
234
+ * stopped as it can also be triggered when a snap fails initialization.
235
+ */
236
+ export declare type SnapTerminated = {
237
+ type: `${typeof controllerName}:snapTerminated`;
238
+ payload: [snap: TruncatedSnap];
239
+ };
240
+ 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;
242
+ export declare type AllowedEvents = ExecutionServiceEvents;
243
+ declare type SnapControllerMessenger = RestrictedControllerMessenger<typeof controllerName, SnapControllerActions | AllowedActions, SnapControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
244
+ export declare enum AppKeyType {
245
+ stateEncryption = "stateEncryption"
246
+ }
247
+ declare type GetAppKey = (subject: string, appKeyType: AppKeyType) => Promise<string>;
248
+ declare type FeatureFlags = {
249
+ /**
250
+ * We still need to implement new UI approval page in metamask-extension before we can allow
251
+ * DApps to update Snaps. After it's added, this flag can be removed.
252
+ *
253
+ * @see {SNAP_APPROVAL_UPDATE}
254
+ * @see {SnapController.processRequestedSnap}
255
+ */
256
+ dappsCanUpdateSnaps?: true;
257
+ };
258
+ declare type SemVerVersion = string;
259
+ declare type SnapInfo = {
260
+ version: SemVerVersion;
261
+ shasum: string;
262
+ };
263
+ export declare type CheckSnapBlockListArg = Record<SnapId, SnapInfo>;
264
+ export declare type CheckSnapBlockListResult = Record<SnapId, {
265
+ blocked: true;
266
+ reason?: string;
267
+ infoUrl?: string;
268
+ } | {
269
+ blocked: false;
270
+ }>;
271
+ /**
272
+ * Checks whether a version of a snap is blocked.
273
+ */
274
+ export declare type CheckSnapBlockList = (snapsToCheck: CheckSnapBlockListArg) => Promise<CheckSnapBlockListResult>;
275
+ declare type SnapControllerArgs = {
276
+ /**
277
+ * A teardown function that allows the host to clean up its instrumentation
278
+ * for a running snap.
279
+ */
280
+ closeAllConnections: CloseAllConnectionsFunction;
281
+ /**
282
+ * The names of endowment permissions whose values are the names of JavaScript
283
+ * APIs that will be added to the snap execution environment at runtime.
284
+ */
285
+ environmentEndowmentPermissions: string[];
286
+ /**
287
+ * The function that will be used by the controller fo make network requests.
288
+ * Should be compatible with {@link fetch}.
289
+ */
290
+ fetchFunction?: typeof fetch;
291
+ /**
292
+ * Flags that enable or disable features in the controller.
293
+ * See {@link FeatureFlags}.
294
+ */
295
+ featureFlags: FeatureFlags;
296
+ /**
297
+ * A function to get an "app key" for a specific subject.
298
+ */
299
+ getAppKey: GetAppKey;
300
+ /**
301
+ * How frequently to check whether a snap is idle.
302
+ */
303
+ idleTimeCheckInterval?: number;
304
+ /**
305
+ * A function that checks whether the specified snap and version is blocked.
306
+ */
307
+ checkBlockList: CheckSnapBlockList;
308
+ /**
309
+ * The maximum amount of time that a snap may be idle.
310
+ */
311
+ maxIdleTime?: number;
312
+ /**
313
+ * The controller messenger.
314
+ */
315
+ messenger: SnapControllerMessenger;
316
+ /**
317
+ * The maximum amount of time a snap may take to process an RPC request,
318
+ * unless it is permitted to take longer.
319
+ */
320
+ maxRequestTime?: number;
321
+ /**
322
+ * The npm registry URL that will be used to fetch published snaps.
323
+ */
324
+ npmRegistryUrl?: string;
325
+ /**
326
+ * Persisted state that will be used for rehydration.
327
+ */
328
+ state?: PersistedSnapControllerState;
329
+ };
330
+ export declare class SnapController extends BaseController<string, SnapControllerState, SnapControllerMessenger> {
331
+ #private;
332
+ private maxRequestTime;
333
+ private snapsRuntimeData;
334
+ constructor({ closeAllConnections, messenger, state, getAppKey, environmentEndowmentPermissions, npmRegistryUrl, idleTimeCheckInterval, checkBlockList, maxIdleTime, maxRequestTime, fetchFunction, featureFlags, }: SnapControllerArgs);
335
+ /**
336
+ * Checks all installed snaps against the block list and
337
+ * blocks/unblocks snaps as appropriate. See {@link SnapController.blockSnap}
338
+ * for more information.
339
+ */
340
+ updateBlockedSnaps(): Promise<void>;
341
+ /**
342
+ * Checks the block list to determine whether a version of a snap is blocked.
343
+ *
344
+ * @param snapId - The snap id to check.
345
+ * @param snapInfo - Snap information containing version and shasum.
346
+ * @returns Whether the version of the snap is blocked or not.
347
+ */
348
+ isBlocked(snapId: ValidatedSnapId, snapInfo: SnapInfo): Promise<boolean>;
349
+ _onUnhandledSnapError(snapId: SnapId, error: SnapErrorJson): Promise<void>;
350
+ _onOutboundRequest(snapId: SnapId): Promise<void>;
351
+ _onOutboundResponse(snapId: SnapId): Promise<void>;
352
+ /**
353
+ * Starts the given snap. Throws an error if no such snap exists
354
+ * or if it is already running.
355
+ *
356
+ * @param snapId - The id of the Snap to start.
357
+ */
358
+ startSnap(snapId: SnapId): Promise<void>;
359
+ /**
360
+ * Enables the given snap. A snap can only be started if it is enabled. A snap
361
+ * can only be enabled if it isn't blocked.
362
+ *
363
+ * @param snapId - The id of the Snap to enable.
364
+ */
365
+ enableSnap(snapId: SnapId): void;
366
+ /**
367
+ * Disables the given snap. A snap can only be started if it is enabled.
368
+ *
369
+ * @param snapId - The id of the Snap to disable.
370
+ * @returns A promise that resolves once the snap has been disabled.
371
+ */
372
+ disableSnap(snapId: SnapId): Promise<void>;
373
+ /**
374
+ * Stops the given snap, removes all hooks, closes all connections, and
375
+ * terminates its worker.
376
+ *
377
+ * @param snapId - The id of the Snap to stop.
378
+ * @param statusEvent - The Snap status event that caused the snap to be
379
+ * stopped.
380
+ */
381
+ stopSnap(snapId: SnapId, statusEvent?: SnapStatusEvents.Stop | SnapStatusEvents.Crash): Promise<void>;
382
+ /**
383
+ * Returns whether the given snap is running.
384
+ * Throws an error if the snap doesn't exist.
385
+ *
386
+ * @param snapId - The id of the Snap to check.
387
+ * @returns `true` if the snap is running, otherwise `false`.
388
+ */
389
+ isRunning(snapId: SnapId): boolean;
390
+ /**
391
+ * Returns whether the given snap has been added to state.
392
+ *
393
+ * @param snapId - The id of the Snap to check for.
394
+ * @returns `true` if the snap exists in the controller state, otherwise `false`.
395
+ */
396
+ has(snapId: SnapId): boolean;
397
+ /**
398
+ * Gets the snap with the given id if it exists, including all data.
399
+ * This should not be used if the snap is to be serializable, as e.g.
400
+ * the snap sourceCode may be quite large.
401
+ *
402
+ * @param snapId - The id of the Snap to get.
403
+ * @returns The entire snap object from the controller state.
404
+ */
405
+ get(snapId: SnapId): Snap | undefined;
406
+ /**
407
+ * Gets the snap with the given id, throws if doesn't.
408
+ * This should not be used if the snap is to be serializable, as e.g.
409
+ * the snap sourceCode may be quite large.
410
+ *
411
+ * @see {@link SnapController.get}
412
+ * @throws {@link Error}. If the snap doesn't exist
413
+ * @param snapId - The id of the snap to get.
414
+ * @returns The entire snap object.
415
+ */
416
+ getExpect(snapId: SnapId): Snap;
417
+ /**
418
+ * Gets the snap with the given id if it exists, excluding any
419
+ * non-serializable or expensive-to-serialize data.
420
+ *
421
+ * @param snapId - The id of the Snap to get.
422
+ * @returns A truncated version of the snap state, that is less expensive to serialize.
423
+ */
424
+ getTruncated(snapId: SnapId): TruncatedSnap | null;
425
+ /**
426
+ * Gets the snap with the given id, throw if it doesn't exist.
427
+ *
428
+ * @throws {@link Error}. If snap doesn't exist
429
+ * @param snapId - The id of the snap to get.
430
+ * @returns A truncated version of the snap state, that is less expensive to serialize.
431
+ */
432
+ getTruncatedExpect(snapId: SnapId): TruncatedSnap;
433
+ /**
434
+ * Updates the own state of the snap with the given id.
435
+ * This is distinct from the state MetaMask uses to manage snaps.
436
+ *
437
+ * @param snapId - The id of the Snap whose state should be updated.
438
+ * @param newSnapState - The new state of the snap.
439
+ */
440
+ updateSnapState(snapId: SnapId, newSnapState: Json): Promise<void>;
441
+ /**
442
+ * Clears the state of the snap with the given id.
443
+ * This is distinct from the state MetaMask uses to manage snaps.
444
+ *
445
+ * @param snapId - The id of the Snap whose state should be cleared.
446
+ */
447
+ clearSnapState(snapId: SnapId): Promise<void>;
448
+ /**
449
+ * Adds error from a snap to the SnapController state.
450
+ *
451
+ * @param snapError - The error to store on the SnapController.
452
+ */
453
+ addSnapError(snapError: SnapError): void;
454
+ /**
455
+ * Removes an error by internalID from a the SnapControllers state.
456
+ *
457
+ * @param internalID - The internal error ID to remove on the SnapController.
458
+ */
459
+ removeSnapError(internalID: string): Promise<void>;
460
+ /**
461
+ * Clears all errors from the SnapControllers state.
462
+ *
463
+ */
464
+ clearSnapErrors(): Promise<void>;
465
+ /**
466
+ * Gets the own state of the snap with the given id.
467
+ * This is distinct from the state MetaMask uses to manage snaps.
468
+ *
469
+ * @param snapId - The id of the Snap whose state to get.
470
+ * @returns A promise that resolves with the decrypted snap state or null if no state exists.
471
+ * @throws If the snap state decryption fails.
472
+ */
473
+ getSnapState(snapId: SnapId): Promise<Json>;
474
+ /**
475
+ * Completely clear the controller's state: delete all associated data,
476
+ * handlers, event listeners, and permissions; tear down all snap providers.
477
+ */
478
+ clearState(): void;
479
+ /**
480
+ * Removes the given snap from state, and clears all associated handlers
481
+ * and listeners.
482
+ *
483
+ * @param snapId - The id of the Snap.
484
+ * @returns A promise that resolves once the snap has been removed.
485
+ */
486
+ removeSnap(snapId: SnapId): Promise<void>;
487
+ /**
488
+ * Stops the given snaps, removes them from state, and clears all associated
489
+ * permissions, handlers, and listeners.
490
+ *
491
+ * @param snapIds - The ids of the Snaps.
492
+ */
493
+ removeSnaps(snapIds: string[]): Promise<void>;
494
+ /**
495
+ * Safely revokes all permissions granted to a Snap.
496
+ *
497
+ * @param snapId - The snap ID.
498
+ */
499
+ private revokeAllSnapPermissions;
500
+ /**
501
+ * Handles incrementing the activeReferences counter.
502
+ *
503
+ * @param snapId - The snap id of the snap that was referenced.
504
+ */
505
+ incrementActiveReferences(snapId: SnapId): void;
506
+ /**
507
+ * Handles decrement the activeReferences counter.
508
+ *
509
+ * @param snapId - The snap id of the snap that was referenced..
510
+ */
511
+ decrementActiveReferences(snapId: SnapId): void;
512
+ /**
513
+ * Gets all snaps in their truncated format.
514
+ *
515
+ * @returns All installed snaps in their truncated format.
516
+ */
517
+ getAllSnaps(): TruncatedSnap[];
518
+ /**
519
+ * Gets the serialized permitted snaps of the given origin, if any.
520
+ *
521
+ * @param origin - The origin whose permitted snaps to retrieve.
522
+ * @returns The serialized permitted snaps for the origin.
523
+ */
524
+ getPermittedSnaps(origin: string): Promise<InstallSnapsResult>;
525
+ /**
526
+ * Installs the snaps requested by the given origin, returning the snap
527
+ * object if the origin is permitted to install it, and an authorization error
528
+ * otherwise.
529
+ *
530
+ * @param origin - The origin that requested to install the snaps.
531
+ * @param requestedSnaps - The snaps to install.
532
+ * @returns An object of snap ids and snap objects, or errors if a
533
+ * snap couldn't be installed.
534
+ */
535
+ installSnaps(origin: string, requestedSnaps: RequestedSnapPermissions): Promise<InstallSnapsResult>;
536
+ /**
537
+ * Adds, authorizes, and runs the given snap with a snap provider.
538
+ * Results from this method should be efficiently serializable.
539
+ *
540
+ * @param origin - The origin requesting the snap.
541
+ * @param snapId - The id of the snap.
542
+ * @param versionRange - The semver range of the snap to install.
543
+ * @returns The resulting snap object, or an error if something went wrong.
544
+ */
545
+ private processRequestedSnap;
546
+ /**
547
+ * Updates an installed snap. The flow is similar to
548
+ * {@link SnapController.installSnaps}. The user will be asked if they want
549
+ * to update, then approve any permission changes, and finally the snap will
550
+ * be restarted.
551
+ *
552
+ * The update will fail if the user rejects any prompt or if the new version
553
+ * of the snap is blocked.
554
+ *
555
+ * If the original version of the snap was blocked and the update succeeded,
556
+ * the snap will be unblocked and enabled before it is restarted.
557
+ *
558
+ * @param origin - The origin requesting the snap update.
559
+ * @param snapId - The id of the Snap to be updated.
560
+ * @param newVersionRange - A semver version range in which the maximum version will be chosen.
561
+ * @returns The snap metadata if updated, `null` otherwise.
562
+ */
563
+ updateSnap(origin: string, snapId: ValidatedSnapId, newVersionRange?: string): Promise<TruncatedSnap | null>;
564
+ /**
565
+ * Fetches the manifest and source code of a snap.
566
+ *
567
+ * This function is not hash private yet because of tests.
568
+ *
569
+ * @param snapId - The id of the Snap.
570
+ * @param versionRange - The SemVer version of the Snap to fetch.
571
+ * @returns A tuple of the Snap manifest object and the Snap source code.
572
+ */
573
+ fetchSnap(snapId: ValidatedSnapId, versionRange?: string): Promise<FetchSnapResult>;
574
+ /**
575
+ * Initiates a request for the given snap's initial permissions.
576
+ * Must be called in order. See processRequestedSnap.
577
+ *
578
+ * This function is not hash private yet because of tests.
579
+ *
580
+ * @param origin - The origin of the install request.
581
+ * @param snapId - The id of the Snap.
582
+ * @returns The snap's approvedPermissions.
583
+ */
584
+ private authorize;
585
+ destroy(): void;
586
+ /**
587
+ * Passes a JSON-RPC request object to the RPC handler function of a snap.
588
+ *
589
+ * @param options - A bag of options.
590
+ * @param options.snapId - The ID of the recipient snap.
591
+ * @param options.origin - The origin of the RPC request.
592
+ * @param options.handler - The handler to trigger on the snap for the request.
593
+ * @param options.request - The JSON-RPC request object.
594
+ * @returns The result of the JSON-RPC request.
595
+ */
596
+ handleRequest({ snapId, origin, handler: handlerType, request, }: SnapRpcHookArgs & {
597
+ snapId: SnapId;
598
+ }): Promise<unknown>;
599
+ }
600
+ export {};