@metamask/snaps-simulation 1.0.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/CHANGELOG.md +14 -0
- package/LICENSE +15 -0
- package/README.md +14 -0
- package/dist/constants.cjs +21 -0
- package/dist/constants.cjs.map +1 -0
- package/dist/constants.d.cts +18 -0
- package/dist/constants.d.cts.map +1 -0
- package/dist/constants.d.mts +18 -0
- package/dist/constants.d.mts.map +1 -0
- package/dist/constants.mjs +18 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/controllers.cjs +101 -0
- package/dist/controllers.cjs.map +1 -0
- package/dist/controllers.d.cts +41 -0
- package/dist/controllers.d.cts.map +1 -0
- package/dist/controllers.d.mts +41 -0
- package/dist/controllers.d.mts.map +1 -0
- package/dist/controllers.mjs +96 -0
- package/dist/controllers.mjs.map +1 -0
- package/dist/files.cjs +94 -0
- package/dist/files.cjs.map +1 -0
- package/dist/files.d.cts +49 -0
- package/dist/files.d.cts.map +1 -0
- package/dist/files.d.mts +49 -0
- package/dist/files.d.mts.map +1 -0
- package/dist/files.mjs +91 -0
- package/dist/files.mjs.map +1 -0
- package/dist/index.cjs +27 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +11 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +11 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +11 -0
- package/dist/index.mjs.map +1 -0
- package/dist/interface.cjs +569 -0
- package/dist/interface.cjs.map +1 -0
- package/dist/interface.d.cts +163 -0
- package/dist/interface.d.cts.map +1 -0
- package/dist/interface.d.mts +163 -0
- package/dist/interface.d.mts.map +1 -0
- package/dist/interface.mjs +554 -0
- package/dist/interface.mjs.map +1 -0
- package/dist/methods/constants.cjs +72 -0
- package/dist/methods/constants.cjs.map +1 -0
- package/dist/methods/constants.d.cts +11 -0
- package/dist/methods/constants.d.cts.map +1 -0
- package/dist/methods/constants.d.mts +11 -0
- package/dist/methods/constants.d.mts.map +1 -0
- package/dist/methods/constants.mjs +69 -0
- package/dist/methods/constants.mjs.map +1 -0
- package/dist/methods/hooks/get-preferences.cjs +18 -0
- package/dist/methods/hooks/get-preferences.cjs.map +1 -0
- package/dist/methods/hooks/get-preferences.d.cts +14 -0
- package/dist/methods/hooks/get-preferences.d.cts.map +1 -0
- package/dist/methods/hooks/get-preferences.d.mts +14 -0
- package/dist/methods/hooks/get-preferences.d.mts.map +1 -0
- package/dist/methods/hooks/get-preferences.mjs +14 -0
- package/dist/methods/hooks/get-preferences.mjs.map +1 -0
- package/dist/methods/hooks/index.cjs +22 -0
- package/dist/methods/hooks/index.cjs.map +1 -0
- package/dist/methods/hooks/index.d.cts +6 -0
- package/dist/methods/hooks/index.d.cts.map +1 -0
- package/dist/methods/hooks/index.d.mts +6 -0
- package/dist/methods/hooks/index.d.mts.map +1 -0
- package/dist/methods/hooks/index.mjs +6 -0
- package/dist/methods/hooks/index.mjs.map +1 -0
- package/dist/methods/hooks/interface.cjs +24 -0
- package/dist/methods/hooks/interface.cjs.map +1 -0
- package/dist/methods/hooks/interface.d.cts +17 -0
- package/dist/methods/hooks/interface.d.cts.map +1 -0
- package/dist/methods/hooks/interface.d.mts +17 -0
- package/dist/methods/hooks/interface.d.mts.map +1 -0
- package/dist/methods/hooks/interface.mjs +19 -0
- package/dist/methods/hooks/interface.mjs.map +1 -0
- package/dist/methods/hooks/notifications.cjs +58 -0
- package/dist/methods/hooks/notifications.cjs.map +1 -0
- package/dist/methods/hooks/notifications.d.cts +17 -0
- package/dist/methods/hooks/notifications.d.cts.map +1 -0
- package/dist/methods/hooks/notifications.d.mts +17 -0
- package/dist/methods/hooks/notifications.d.mts.map +1 -0
- package/dist/methods/hooks/notifications.mjs +55 -0
- package/dist/methods/hooks/notifications.mjs.map +1 -0
- package/dist/methods/hooks/request-user-approval.cjs +38 -0
- package/dist/methods/hooks/request-user-approval.cjs.map +1 -0
- package/dist/methods/hooks/request-user-approval.d.cts +16 -0
- package/dist/methods/hooks/request-user-approval.d.cts.map +1 -0
- package/dist/methods/hooks/request-user-approval.d.mts +16 -0
- package/dist/methods/hooks/request-user-approval.d.mts.map +1 -0
- package/dist/methods/hooks/request-user-approval.mjs +35 -0
- package/dist/methods/hooks/request-user-approval.mjs.map +1 -0
- package/dist/methods/hooks/state.cjs +84 -0
- package/dist/methods/hooks/state.cjs.map +1 -0
- package/dist/methods/hooks/state.d.cts +24 -0
- package/dist/methods/hooks/state.d.cts.map +1 -0
- package/dist/methods/hooks/state.d.mts +24 -0
- package/dist/methods/hooks/state.d.mts.map +1 -0
- package/dist/methods/hooks/state.mjs +79 -0
- package/dist/methods/hooks/state.mjs.map +1 -0
- package/dist/methods/index.cjs +18 -0
- package/dist/methods/index.cjs.map +1 -0
- package/dist/methods/index.d.cts +2 -0
- package/dist/methods/index.d.cts.map +1 -0
- package/dist/methods/index.d.mts +2 -0
- package/dist/methods/index.d.mts.map +1 -0
- package/dist/methods/index.mjs +2 -0
- package/dist/methods/index.mjs.map +1 -0
- package/dist/methods/specifications.cjs +85 -0
- package/dist/methods/specifications.cjs.map +1 -0
- package/dist/methods/specifications.d.cts +57 -0
- package/dist/methods/specifications.d.cts.map +1 -0
- package/dist/methods/specifications.d.mts +57 -0
- package/dist/methods/specifications.d.mts.map +1 -0
- package/dist/methods/specifications.mjs +78 -0
- package/dist/methods/specifications.mjs.map +1 -0
- package/dist/middleware/engine.cjs +37 -0
- package/dist/middleware/engine.cjs.map +1 -0
- package/dist/middleware/engine.d.cts +27 -0
- package/dist/middleware/engine.d.cts.map +1 -0
- package/dist/middleware/engine.d.mts +27 -0
- package/dist/middleware/engine.d.mts.map +1 -0
- package/dist/middleware/engine.mjs +34 -0
- package/dist/middleware/engine.mjs.map +1 -0
- package/dist/middleware/index.cjs +18 -0
- package/dist/middleware/index.cjs.map +1 -0
- package/dist/middleware/index.d.cts +2 -0
- package/dist/middleware/index.d.cts.map +1 -0
- package/dist/middleware/index.d.mts +2 -0
- package/dist/middleware/index.d.mts.map +1 -0
- package/dist/middleware/index.mjs +2 -0
- package/dist/middleware/index.mjs.map +1 -0
- package/dist/middleware/internal-methods/accounts.cjs +34 -0
- package/dist/middleware/internal-methods/accounts.cjs.map +1 -0
- package/dist/middleware/internal-methods/accounts.d.cts +20 -0
- package/dist/middleware/internal-methods/accounts.d.cts.map +1 -0
- package/dist/middleware/internal-methods/accounts.d.mts +20 -0
- package/dist/middleware/internal-methods/accounts.d.mts.map +1 -0
- package/dist/middleware/internal-methods/accounts.mjs +30 -0
- package/dist/middleware/internal-methods/accounts.mjs.map +1 -0
- package/dist/middleware/internal-methods/index.cjs +18 -0
- package/dist/middleware/internal-methods/index.cjs.map +1 -0
- package/dist/middleware/internal-methods/index.d.cts +2 -0
- package/dist/middleware/internal-methods/index.d.cts.map +1 -0
- package/dist/middleware/internal-methods/index.d.mts +2 -0
- package/dist/middleware/internal-methods/index.d.mts.map +1 -0
- package/dist/middleware/internal-methods/index.mjs +2 -0
- package/dist/middleware/internal-methods/index.mjs.map +1 -0
- package/dist/middleware/internal-methods/middleware.cjs +44 -0
- package/dist/middleware/internal-methods/middleware.cjs.map +1 -0
- package/dist/middleware/internal-methods/middleware.d.cts +23 -0
- package/dist/middleware/internal-methods/middleware.d.cts.map +1 -0
- package/dist/middleware/internal-methods/middleware.d.mts +23 -0
- package/dist/middleware/internal-methods/middleware.d.mts.map +1 -0
- package/dist/middleware/internal-methods/middleware.mjs +40 -0
- package/dist/middleware/internal-methods/middleware.mjs.map +1 -0
- package/dist/middleware/internal-methods/provider-state.cjs +28 -0
- package/dist/middleware/internal-methods/provider-state.cjs.map +1 -0
- package/dist/middleware/internal-methods/provider-state.d.cts +16 -0
- package/dist/middleware/internal-methods/provider-state.d.cts.map +1 -0
- package/dist/middleware/internal-methods/provider-state.d.mts +16 -0
- package/dist/middleware/internal-methods/provider-state.d.mts.map +1 -0
- package/dist/middleware/internal-methods/provider-state.mjs +24 -0
- package/dist/middleware/internal-methods/provider-state.mjs.map +1 -0
- package/dist/middleware/mock.cjs +22 -0
- package/dist/middleware/mock.cjs.map +1 -0
- package/dist/middleware/mock.d.cts +11 -0
- package/dist/middleware/mock.d.cts.map +1 -0
- package/dist/middleware/mock.d.mts +11 -0
- package/dist/middleware/mock.d.mts.map +1 -0
- package/dist/middleware/mock.mjs +18 -0
- package/dist/middleware/mock.mjs.map +1 -0
- package/dist/options.cjs +25 -0
- package/dist/options.cjs.map +1 -0
- package/dist/options.d.cts +40 -0
- package/dist/options.d.cts.map +1 -0
- package/dist/options.d.mts +40 -0
- package/dist/options.d.mts.map +1 -0
- package/dist/options.mjs +21 -0
- package/dist/options.mjs.map +1 -0
- package/dist/request.cjs +144 -0
- package/dist/request.cjs.map +1 -0
- package/dist/request.d.cts +56 -0
- package/dist/request.d.cts.map +1 -0
- package/dist/request.d.mts +56 -0
- package/dist/request.d.mts.map +1 -0
- package/dist/request.mjs +139 -0
- package/dist/request.mjs.map +1 -0
- package/dist/simulation.cjs +155 -0
- package/dist/simulation.cjs.map +1 -0
- package/dist/simulation.d.cts +103 -0
- package/dist/simulation.d.cts.map +1 -0
- package/dist/simulation.d.mts +103 -0
- package/dist/simulation.d.mts.map +1 -0
- package/dist/simulation.mjs +150 -0
- package/dist/simulation.mjs.map +1 -0
- package/dist/store/index.cjs +22 -0
- package/dist/store/index.cjs.map +1 -0
- package/dist/store/index.d.cts +6 -0
- package/dist/store/index.d.cts.map +1 -0
- package/dist/store/index.d.mts +6 -0
- package/dist/store/index.d.mts.map +1 -0
- package/dist/store/index.mjs +6 -0
- package/dist/store/index.mjs.map +1 -0
- package/dist/store/mocks.cjs +39 -0
- package/dist/store/mocks.cjs.map +1 -0
- package/dist/store/mocks.d.cts +36 -0
- package/dist/store/mocks.d.cts.map +1 -0
- package/dist/store/mocks.d.mts +36 -0
- package/dist/store/mocks.d.mts.map +1 -0
- package/dist/store/mocks.mjs +35 -0
- package/dist/store/mocks.mjs.map +1 -0
- package/dist/store/notifications.cjs +35 -0
- package/dist/store/notifications.cjs.map +1 -0
- package/dist/store/notifications.d.cts +45 -0
- package/dist/store/notifications.d.cts.map +1 -0
- package/dist/store/notifications.d.mts +45 -0
- package/dist/store/notifications.d.mts.map +1 -0
- package/dist/store/notifications.mjs +32 -0
- package/dist/store/notifications.mjs.map +1 -0
- package/dist/store/state.cjs +54 -0
- package/dist/store/state.cjs.map +1 -0
- package/dist/store/state.d.cts +56 -0
- package/dist/store/state.d.cts.map +1 -0
- package/dist/store/state.d.mts +56 -0
- package/dist/store/state.d.mts.map +1 -0
- package/dist/store/state.mjs +50 -0
- package/dist/store/state.mjs.map +1 -0
- package/dist/store/store.cjs +51 -0
- package/dist/store/store.cjs.map +1 -0
- package/dist/store/store.d.cts +22 -0
- package/dist/store/store.d.cts.map +1 -0
- package/dist/store/store.d.mts +22 -0
- package/dist/store/store.d.mts.map +1 -0
- package/dist/store/store.mjs +45 -0
- package/dist/store/store.mjs.map +1 -0
- package/dist/store/ui.cjs +24 -0
- package/dist/store/ui.cjs.map +1 -0
- package/dist/store/ui.d.cts +27 -0
- package/dist/store/ui.d.cts.map +1 -0
- package/dist/store/ui.d.mts +27 -0
- package/dist/store/ui.d.mts.map +1 -0
- package/dist/store/ui.mjs +21 -0
- package/dist/store/ui.mjs.map +1 -0
- package/dist/structs.cjs +157 -0
- package/dist/structs.cjs.map +1 -0
- package/dist/structs.d.cts +205 -0
- package/dist/structs.d.cts.map +1 -0
- package/dist/structs.d.mts +205 -0
- package/dist/structs.d.mts.map +1 -0
- package/dist/structs.mjs +154 -0
- package/dist/structs.mjs.map +1 -0
- package/dist/types.cjs +3 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +393 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +393 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +2 -0
- package/dist/types.mjs.map +1 -0
- package/dist/validation.cjs +74 -0
- package/dist/validation.cjs.map +1 -0
- package/dist/validation.d.cts +44 -0
- package/dist/validation.d.cts.map +1 -0
- package/dist/validation.d.mts +44 -0
- package/dist/validation.d.mts.map +1 -0
- package/dist/validation.mjs +64 -0
- package/dist/validation.mjs.map +1 -0
- package/package.json +101 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { AbstractExecutionService } from "@metamask/snaps-controllers";
|
|
2
|
+
import { type SnapId } from "@metamask/snaps-sdk";
|
|
3
|
+
import type { HandlerType } from "@metamask/snaps-utils";
|
|
4
|
+
import type { RootControllerMessenger } from "./controllers.cjs";
|
|
5
|
+
import type { RunSagaFunction, Store } from "./store/index.cjs";
|
|
6
|
+
import type { RequestOptions, SnapHandlerInterface, SnapRequest } from "./types.cjs";
|
|
7
|
+
export type HandleRequestOptions = {
|
|
8
|
+
snapId: SnapId;
|
|
9
|
+
store: Store;
|
|
10
|
+
executionService: AbstractExecutionService<unknown>;
|
|
11
|
+
handler: HandlerType;
|
|
12
|
+
controllerMessenger: RootControllerMessenger;
|
|
13
|
+
runSaga: RunSagaFunction;
|
|
14
|
+
request: RequestOptions;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Send a JSON-RPC request to the Snap, and wrap the response in a
|
|
18
|
+
* {@link SnapResponse} object.
|
|
19
|
+
*
|
|
20
|
+
* @param options - The request options.
|
|
21
|
+
* @param options.snapId - The ID of the Snap to send the request to.
|
|
22
|
+
* @param options.store - The Redux store.
|
|
23
|
+
* @param options.executionService - The execution service to use to send the
|
|
24
|
+
* request.
|
|
25
|
+
* @param options.handler - The handler to use to send the request.
|
|
26
|
+
* @param options.controllerMessenger - The controller messenger used to call actions.
|
|
27
|
+
* @param options.runSaga - A function to run a saga outside the usual Redux
|
|
28
|
+
* flow.
|
|
29
|
+
* @param options.request - The request to send.
|
|
30
|
+
* @param options.request.id - The ID of the request. If not provided, a random
|
|
31
|
+
* ID will be generated.
|
|
32
|
+
* @param options.request.origin - The origin of the request. Defaults to
|
|
33
|
+
* `https://metamask.io`.
|
|
34
|
+
* @returns The response, wrapped in a {@link SnapResponse} object.
|
|
35
|
+
*/
|
|
36
|
+
export declare function handleRequest({ snapId, store, executionService, handler, controllerMessenger, runSaga, request: { id, origin, ...options }, }: HandleRequestOptions): SnapRequest;
|
|
37
|
+
/**
|
|
38
|
+
* Get the interface ID from the result if it's available or create a new interface if the result contains static components.
|
|
39
|
+
*
|
|
40
|
+
* @param result - The handler result object.
|
|
41
|
+
* @param snapId - The Snap ID.
|
|
42
|
+
* @param controllerMessenger - The controller messenger.
|
|
43
|
+
* @returns The interface ID or undefined if the result doesn't include content.
|
|
44
|
+
*/
|
|
45
|
+
export declare function getInterfaceFromResult(result: unknown, snapId: SnapId, controllerMessenger: RootControllerMessenger): Promise<string | undefined>;
|
|
46
|
+
/**
|
|
47
|
+
* Get the response content from the `SnapInterfaceController` and include the
|
|
48
|
+
* interaction methods.
|
|
49
|
+
*
|
|
50
|
+
* @param result - The handler result object.
|
|
51
|
+
* @param snapId - The Snap ID.
|
|
52
|
+
* @param controllerMessenger - The controller messenger.
|
|
53
|
+
* @returns The content components if any.
|
|
54
|
+
*/
|
|
55
|
+
export declare function getInterfaceApi(result: unknown, snapId: SnapId, controllerMessenger: RootControllerMessenger): Promise<(() => SnapHandlerInterface) | undefined>;
|
|
56
|
+
//# sourceMappingURL=request.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.d.cts","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,oCAAoC;AAC5E,OAAO,EAIL,KAAK,MAAM,EACZ,4BAA4B;AAC7B,OAAO,KAAK,EAAE,WAAW,EAAE,8BAA8B;AAWzD,OAAO,KAAK,EAAE,uBAAuB,EAAE,0BAAsB;AAG7D,OAAO,KAAK,EAAE,eAAe,EAAE,KAAK,EAAE,0BAAgB;AAEtD,OAAO,KAAK,EACV,cAAc,EACd,oBAAoB,EACpB,WAAW,EACZ,oBAAgB;AAEjB,MAAM,MAAM,oBAAoB,GAAG;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,KAAK,CAAC;IACb,gBAAgB,EAAE,wBAAwB,CAAC,OAAO,CAAC,CAAC;IACpD,OAAO,EAAE,WAAW,CAAC;IACrB,mBAAmB,EAAE,uBAAuB,CAAC;IAC7C,OAAO,EAAE,eAAe,CAAC;IACzB,OAAO,EAAE,cAAc,CAAC;CACzB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,aAAa,CAAC,EAC5B,MAAM,EACN,KAAK,EACL,gBAAgB,EAChB,OAAO,EACP,mBAAmB,EACnB,OAAO,EACP,OAAO,EAAE,EAAE,EAAa,EAAE,MAA8B,EAAE,GAAG,OAAO,EAAE,GACvE,EAAE,oBAAoB,GAAG,WAAW,CAuFpC;AAED;;;;;;;GAOG;AACH,wBAAsB,sBAAsB,CAC1C,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,MAAM,EACd,mBAAmB,EAAE,uBAAuB,+BAqB7C;AAED;;;;;;;;GAQG;AACH,wBAAsB,eAAe,CACnC,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,MAAM,EACd,mBAAmB,EAAE,uBAAuB,GAC3C,OAAO,CAAC,CAAC,MAAM,oBAAoB,CAAC,GAAG,SAAS,CAAC,CA4BnD"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { AbstractExecutionService } from "@metamask/snaps-controllers";
|
|
2
|
+
import { type SnapId } from "@metamask/snaps-sdk";
|
|
3
|
+
import type { HandlerType } from "@metamask/snaps-utils";
|
|
4
|
+
import type { RootControllerMessenger } from "./controllers.mjs";
|
|
5
|
+
import type { RunSagaFunction, Store } from "./store/index.mjs";
|
|
6
|
+
import type { RequestOptions, SnapHandlerInterface, SnapRequest } from "./types.mjs";
|
|
7
|
+
export type HandleRequestOptions = {
|
|
8
|
+
snapId: SnapId;
|
|
9
|
+
store: Store;
|
|
10
|
+
executionService: AbstractExecutionService<unknown>;
|
|
11
|
+
handler: HandlerType;
|
|
12
|
+
controllerMessenger: RootControllerMessenger;
|
|
13
|
+
runSaga: RunSagaFunction;
|
|
14
|
+
request: RequestOptions;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Send a JSON-RPC request to the Snap, and wrap the response in a
|
|
18
|
+
* {@link SnapResponse} object.
|
|
19
|
+
*
|
|
20
|
+
* @param options - The request options.
|
|
21
|
+
* @param options.snapId - The ID of the Snap to send the request to.
|
|
22
|
+
* @param options.store - The Redux store.
|
|
23
|
+
* @param options.executionService - The execution service to use to send the
|
|
24
|
+
* request.
|
|
25
|
+
* @param options.handler - The handler to use to send the request.
|
|
26
|
+
* @param options.controllerMessenger - The controller messenger used to call actions.
|
|
27
|
+
* @param options.runSaga - A function to run a saga outside the usual Redux
|
|
28
|
+
* flow.
|
|
29
|
+
* @param options.request - The request to send.
|
|
30
|
+
* @param options.request.id - The ID of the request. If not provided, a random
|
|
31
|
+
* ID will be generated.
|
|
32
|
+
* @param options.request.origin - The origin of the request. Defaults to
|
|
33
|
+
* `https://metamask.io`.
|
|
34
|
+
* @returns The response, wrapped in a {@link SnapResponse} object.
|
|
35
|
+
*/
|
|
36
|
+
export declare function handleRequest({ snapId, store, executionService, handler, controllerMessenger, runSaga, request: { id, origin, ...options }, }: HandleRequestOptions): SnapRequest;
|
|
37
|
+
/**
|
|
38
|
+
* Get the interface ID from the result if it's available or create a new interface if the result contains static components.
|
|
39
|
+
*
|
|
40
|
+
* @param result - The handler result object.
|
|
41
|
+
* @param snapId - The Snap ID.
|
|
42
|
+
* @param controllerMessenger - The controller messenger.
|
|
43
|
+
* @returns The interface ID or undefined if the result doesn't include content.
|
|
44
|
+
*/
|
|
45
|
+
export declare function getInterfaceFromResult(result: unknown, snapId: SnapId, controllerMessenger: RootControllerMessenger): Promise<string | undefined>;
|
|
46
|
+
/**
|
|
47
|
+
* Get the response content from the `SnapInterfaceController` and include the
|
|
48
|
+
* interaction methods.
|
|
49
|
+
*
|
|
50
|
+
* @param result - The handler result object.
|
|
51
|
+
* @param snapId - The Snap ID.
|
|
52
|
+
* @param controllerMessenger - The controller messenger.
|
|
53
|
+
* @returns The content components if any.
|
|
54
|
+
*/
|
|
55
|
+
export declare function getInterfaceApi(result: unknown, snapId: SnapId, controllerMessenger: RootControllerMessenger): Promise<(() => SnapHandlerInterface) | undefined>;
|
|
56
|
+
//# sourceMappingURL=request.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.d.mts","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,oCAAoC;AAC5E,OAAO,EAIL,KAAK,MAAM,EACZ,4BAA4B;AAC7B,OAAO,KAAK,EAAE,WAAW,EAAE,8BAA8B;AAWzD,OAAO,KAAK,EAAE,uBAAuB,EAAE,0BAAsB;AAG7D,OAAO,KAAK,EAAE,eAAe,EAAE,KAAK,EAAE,0BAAgB;AAEtD,OAAO,KAAK,EACV,cAAc,EACd,oBAAoB,EACpB,WAAW,EACZ,oBAAgB;AAEjB,MAAM,MAAM,oBAAoB,GAAG;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,KAAK,CAAC;IACb,gBAAgB,EAAE,wBAAwB,CAAC,OAAO,CAAC,CAAC;IACpD,OAAO,EAAE,WAAW,CAAC;IACrB,mBAAmB,EAAE,uBAAuB,CAAC;IAC7C,OAAO,EAAE,eAAe,CAAC;IACzB,OAAO,EAAE,cAAc,CAAC;CACzB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,aAAa,CAAC,EAC5B,MAAM,EACN,KAAK,EACL,gBAAgB,EAChB,OAAO,EACP,mBAAmB,EACnB,OAAO,EACP,OAAO,EAAE,EAAE,EAAa,EAAE,MAA8B,EAAE,GAAG,OAAO,EAAE,GACvE,EAAE,oBAAoB,GAAG,WAAW,CAuFpC;AAED;;;;;;;GAOG;AACH,wBAAsB,sBAAsB,CAC1C,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,MAAM,EACd,mBAAmB,EAAE,uBAAuB,+BAqB7C;AAED;;;;;;;;GAQG;AACH,wBAAsB,eAAe,CACnC,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,MAAM,EACd,mBAAmB,EAAE,uBAAuB,GAC3C,OAAO,CAAC,CAAC,MAAM,oBAAoB,CAAC,GAAG,SAAS,CAAC,CA4BnD"}
|
package/dist/request.mjs
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { ComponentOrElementStruct } from "@metamask/snaps-sdk";
|
|
2
|
+
import { unwrapError } from "@metamask/snaps-utils";
|
|
3
|
+
import { is } from "@metamask/superstruct";
|
|
4
|
+
import { assert, getSafeJson, hasProperty, isPlainObject } from "@metamask/utils";
|
|
5
|
+
import $reduxjstoolkit from "@reduxjs/toolkit";
|
|
6
|
+
const { nanoid } = $reduxjstoolkit;
|
|
7
|
+
import { getInterface, getInterfaceActions } from "./interface.mjs";
|
|
8
|
+
import { clearNotifications, getNotifications } from "./store/index.mjs";
|
|
9
|
+
import { SnapResponseStruct } from "./structs.mjs";
|
|
10
|
+
/**
|
|
11
|
+
* Send a JSON-RPC request to the Snap, and wrap the response in a
|
|
12
|
+
* {@link SnapResponse} object.
|
|
13
|
+
*
|
|
14
|
+
* @param options - The request options.
|
|
15
|
+
* @param options.snapId - The ID of the Snap to send the request to.
|
|
16
|
+
* @param options.store - The Redux store.
|
|
17
|
+
* @param options.executionService - The execution service to use to send the
|
|
18
|
+
* request.
|
|
19
|
+
* @param options.handler - The handler to use to send the request.
|
|
20
|
+
* @param options.controllerMessenger - The controller messenger used to call actions.
|
|
21
|
+
* @param options.runSaga - A function to run a saga outside the usual Redux
|
|
22
|
+
* flow.
|
|
23
|
+
* @param options.request - The request to send.
|
|
24
|
+
* @param options.request.id - The ID of the request. If not provided, a random
|
|
25
|
+
* ID will be generated.
|
|
26
|
+
* @param options.request.origin - The origin of the request. Defaults to
|
|
27
|
+
* `https://metamask.io`.
|
|
28
|
+
* @returns The response, wrapped in a {@link SnapResponse} object.
|
|
29
|
+
*/
|
|
30
|
+
export function handleRequest({ snapId, store, executionService, handler, controllerMessenger, runSaga, request: { id = nanoid(), origin = 'https://metamask.io', ...options }, }) {
|
|
31
|
+
const getInterfaceError = () => {
|
|
32
|
+
throw new Error('Unable to get the interface from the Snap: The request to the Snap failed.');
|
|
33
|
+
};
|
|
34
|
+
const promise = executionService
|
|
35
|
+
.handleRpcRequest(snapId, {
|
|
36
|
+
origin,
|
|
37
|
+
handler,
|
|
38
|
+
request: {
|
|
39
|
+
jsonrpc: '2.0',
|
|
40
|
+
id: 1,
|
|
41
|
+
...options,
|
|
42
|
+
},
|
|
43
|
+
})
|
|
44
|
+
.then(async (result) => {
|
|
45
|
+
const notifications = getNotifications(store.getState());
|
|
46
|
+
store.dispatch(clearNotifications());
|
|
47
|
+
try {
|
|
48
|
+
const getInterfaceFn = await getInterfaceApi(result, snapId, controllerMessenger);
|
|
49
|
+
return {
|
|
50
|
+
id: String(id),
|
|
51
|
+
response: {
|
|
52
|
+
result: getSafeJson(result),
|
|
53
|
+
},
|
|
54
|
+
notifications,
|
|
55
|
+
...(getInterfaceFn ? { getInterface: getInterfaceFn } : {}),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
const [unwrappedError] = unwrapError(error);
|
|
60
|
+
return {
|
|
61
|
+
id: String(id),
|
|
62
|
+
response: {
|
|
63
|
+
error: unwrappedError.serialize(),
|
|
64
|
+
},
|
|
65
|
+
notifications: [],
|
|
66
|
+
getInterface: getInterfaceError,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
})
|
|
70
|
+
.catch((error) => {
|
|
71
|
+
const [unwrappedError] = unwrapError(error);
|
|
72
|
+
return {
|
|
73
|
+
id: String(id),
|
|
74
|
+
response: {
|
|
75
|
+
error: unwrappedError.serialize(),
|
|
76
|
+
},
|
|
77
|
+
notifications: [],
|
|
78
|
+
getInterface: getInterfaceError,
|
|
79
|
+
};
|
|
80
|
+
});
|
|
81
|
+
promise.getInterface = async () => {
|
|
82
|
+
const sagaPromise = runSaga(getInterface, runSaga, snapId, controllerMessenger).toPromise();
|
|
83
|
+
const result = await Promise.race([promise, sagaPromise]);
|
|
84
|
+
// If the request promise has resolved to an error, we should throw
|
|
85
|
+
// instead of waiting for an interface that likely will never be displayed
|
|
86
|
+
if (is(result, SnapResponseStruct) &&
|
|
87
|
+
hasProperty(result.response, 'error')) {
|
|
88
|
+
throw new Error(`Unable to get the interface from the Snap: The returned interface may be invalid. The error message received was: ${result.response.error.message}`);
|
|
89
|
+
}
|
|
90
|
+
return await sagaPromise;
|
|
91
|
+
};
|
|
92
|
+
return promise;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Get the interface ID from the result if it's available or create a new interface if the result contains static components.
|
|
96
|
+
*
|
|
97
|
+
* @param result - The handler result object.
|
|
98
|
+
* @param snapId - The Snap ID.
|
|
99
|
+
* @param controllerMessenger - The controller messenger.
|
|
100
|
+
* @returns The interface ID or undefined if the result doesn't include content.
|
|
101
|
+
*/
|
|
102
|
+
export async function getInterfaceFromResult(result, snapId, controllerMessenger) {
|
|
103
|
+
if (isPlainObject(result) && hasProperty(result, 'id')) {
|
|
104
|
+
return result.id;
|
|
105
|
+
}
|
|
106
|
+
if (isPlainObject(result) && hasProperty(result, 'content')) {
|
|
107
|
+
assert(is(result.content, ComponentOrElementStruct), 'The Snap returned an invalid interface.');
|
|
108
|
+
const id = await controllerMessenger.call('SnapInterfaceController:createInterface', snapId, result.content);
|
|
109
|
+
return id;
|
|
110
|
+
}
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Get the response content from the `SnapInterfaceController` and include the
|
|
115
|
+
* interaction methods.
|
|
116
|
+
*
|
|
117
|
+
* @param result - The handler result object.
|
|
118
|
+
* @param snapId - The Snap ID.
|
|
119
|
+
* @param controllerMessenger - The controller messenger.
|
|
120
|
+
* @returns The content components if any.
|
|
121
|
+
*/
|
|
122
|
+
export async function getInterfaceApi(result, snapId, controllerMessenger) {
|
|
123
|
+
const interfaceId = await getInterfaceFromResult(result, snapId, controllerMessenger);
|
|
124
|
+
if (interfaceId) {
|
|
125
|
+
return () => {
|
|
126
|
+
const { content } = controllerMessenger.call('SnapInterfaceController:getInterface', snapId, interfaceId);
|
|
127
|
+
const actions = getInterfaceActions(snapId, controllerMessenger, {
|
|
128
|
+
id: interfaceId,
|
|
129
|
+
content,
|
|
130
|
+
});
|
|
131
|
+
return {
|
|
132
|
+
content,
|
|
133
|
+
...actions,
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=request.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.mjs","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,wBAAwB,EAGzB,4BAA4B;AAE7B,OAAO,EAAE,WAAW,EAAE,8BAA8B;AACpD,OAAO,EAAE,EAAE,EAAE,8BAA8B;AAC3C,OAAO,EACL,MAAM,EACN,WAAW,EACX,WAAW,EACX,aAAa,EACd,wBAAwB;;;AAIzB,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,wBAAoB;AAChE,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,0BAAgB;AAE/D,OAAO,EAAE,kBAAkB,EAAE,sBAAkB;AAiB/C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,aAAa,CAAC,EAC5B,MAAM,EACN,KAAK,EACL,gBAAgB,EAChB,OAAO,EACP,mBAAmB,EACnB,OAAO,EACP,OAAO,EAAE,EAAE,EAAE,GAAG,MAAM,EAAE,EAAE,MAAM,GAAG,qBAAqB,EAAE,GAAG,OAAO,EAAE,GACjD;IACrB,MAAM,iBAAiB,GAAG,GAAG,EAAE;QAC7B,MAAM,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CAAC,IAAI,CACvC,yCAAyC,EACzC,MAAM,EACN,MAAM,CAAC,OAA6B,CACrC,CAAC;QAEF,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,MAAe,EACf,MAAc,EACd,mBAA4C;IAE5C,MAAM,WAAW,GAAG,MAAM,sBAAsB,CAC9C,MAAM,EACN,MAAM,EACN,mBAAmB,CACpB,CAAC;IAEF,IAAI,WAAW,EAAE,CAAC;QAChB,OAAO,GAAG,EAAE;YACV,MAAM,EAAE,OAAO,EAAE,GAAG,mBAAmB,CAAC,IAAI,CAC1C,sCAAsC,EACtC,MAAM,EACN,WAAW,CACZ,CAAC;YAEF,MAAM,OAAO,GAAG,mBAAmB,CAAC,MAAM,EAAE,mBAAmB,EAAE;gBAC/D,EAAE,EAAE,WAAW;gBACf,OAAO;aACR,CAAC,CAAC;YAEH,OAAO;gBACL,OAAO;gBACP,GAAG,OAAO;aACX,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["import type { AbstractExecutionService } from '@metamask/snaps-controllers';\nimport {\n type ComponentOrElement,\n ComponentOrElementStruct,\n type JsonRpcError,\n type SnapId,\n} from '@metamask/snaps-sdk';\nimport type { HandlerType } from '@metamask/snaps-utils';\nimport { unwrapError } from '@metamask/snaps-utils';\nimport { is } from '@metamask/superstruct';\nimport {\n assert,\n getSafeJson,\n hasProperty,\n isPlainObject,\n} from '@metamask/utils';\nimport { nanoid } from '@reduxjs/toolkit';\n\nimport type { RootControllerMessenger } from './controllers';\nimport { getInterface, getInterfaceActions } from './interface';\nimport { clearNotifications, getNotifications } from './store';\nimport type { RunSagaFunction, Store } from './store';\nimport { SnapResponseStruct } from './structs';\nimport type {\n RequestOptions,\n SnapHandlerInterface,\n SnapRequest,\n} from './types';\n\nexport type HandleRequestOptions = {\n snapId: SnapId;\n store: Store;\n executionService: AbstractExecutionService<unknown>;\n handler: HandlerType;\n controllerMessenger: RootControllerMessenger;\n runSaga: RunSagaFunction;\n request: RequestOptions;\n};\n\n/**\n * Send a JSON-RPC request to the Snap, and wrap the response in a\n * {@link SnapResponse} object.\n *\n * @param options - The request options.\n * @param options.snapId - The ID of the Snap to send the request to.\n * @param options.store - The Redux store.\n * @param options.executionService - The execution service to use to send the\n * request.\n * @param options.handler - The handler to use to send the request.\n * @param options.controllerMessenger - The controller messenger used to call actions.\n * @param options.runSaga - A function to run a saga outside the usual Redux\n * flow.\n * @param options.request - The request to send.\n * @param options.request.id - The ID of the request. If not provided, a random\n * ID will be generated.\n * @param options.request.origin - The origin of the request. Defaults to\n * `https://metamask.io`.\n * @returns The response, wrapped in a {@link SnapResponse} object.\n */\nexport function handleRequest({\n snapId,\n store,\n executionService,\n handler,\n controllerMessenger,\n runSaga,\n request: { id = nanoid(), origin = 'https://metamask.io', ...options },\n}: HandleRequestOptions): SnapRequest {\n const getInterfaceError = () => {\n throw new Error(\n 'Unable to get the interface from the Snap: The request to the Snap failed.',\n );\n };\n\n const promise = executionService\n .handleRpcRequest(snapId, {\n origin,\n handler,\n request: {\n jsonrpc: '2.0',\n id: 1,\n ...options,\n },\n })\n .then(async (result) => {\n const notifications = getNotifications(store.getState());\n store.dispatch(clearNotifications());\n\n try {\n const getInterfaceFn = await getInterfaceApi(\n result,\n snapId,\n controllerMessenger,\n );\n\n return {\n id: String(id),\n response: {\n result: getSafeJson(result),\n },\n notifications,\n ...(getInterfaceFn ? { getInterface: getInterfaceFn } : {}),\n };\n } catch (error) {\n const [unwrappedError] = unwrapError(error);\n return {\n id: String(id),\n response: {\n error: unwrappedError.serialize(),\n },\n notifications: [],\n getInterface: getInterfaceError,\n };\n }\n })\n .catch((error) => {\n const [unwrappedError] = unwrapError(error);\n\n return {\n id: String(id),\n response: {\n error: unwrappedError.serialize(),\n },\n notifications: [],\n getInterface: getInterfaceError,\n };\n }) as unknown as SnapRequest;\n\n promise.getInterface = async () => {\n const sagaPromise = runSaga(\n getInterface,\n runSaga,\n snapId,\n controllerMessenger,\n ).toPromise();\n const result = await Promise.race([promise, sagaPromise]);\n\n // If the request promise has resolved to an error, we should throw\n // instead of waiting for an interface that likely will never be displayed\n if (\n is(result, SnapResponseStruct) &&\n hasProperty(result.response, 'error')\n ) {\n throw new Error(\n `Unable to get the interface from the Snap: The returned interface may be invalid. The error message received was: ${\n (result.response.error as JsonRpcError).message\n }`,\n );\n }\n\n return await sagaPromise;\n };\n\n return promise;\n}\n\n/**\n * Get the interface ID from the result if it's available or create a new interface if the result contains static components.\n *\n * @param result - The handler result object.\n * @param snapId - The Snap ID.\n * @param controllerMessenger - The controller messenger.\n * @returns The interface ID or undefined if the result doesn't include content.\n */\nexport async function getInterfaceFromResult(\n result: unknown,\n snapId: SnapId,\n controllerMessenger: RootControllerMessenger,\n) {\n if (isPlainObject(result) && hasProperty(result, 'id')) {\n return result.id as string;\n }\n\n if (isPlainObject(result) && hasProperty(result, 'content')) {\n assert(\n is(result.content, ComponentOrElementStruct),\n 'The Snap returned an invalid interface.',\n );\n const id = await controllerMessenger.call(\n 'SnapInterfaceController:createInterface',\n snapId,\n result.content as ComponentOrElement,\n );\n\n return id;\n }\n\n return undefined;\n}\n\n/**\n * Get the response content from the `SnapInterfaceController` and include the\n * interaction methods.\n *\n * @param result - The handler result object.\n * @param snapId - The Snap ID.\n * @param controllerMessenger - The controller messenger.\n * @returns The content components if any.\n */\nexport async function getInterfaceApi(\n result: unknown,\n snapId: SnapId,\n controllerMessenger: RootControllerMessenger,\n): Promise<(() => SnapHandlerInterface) | undefined> {\n const interfaceId = await getInterfaceFromResult(\n result,\n snapId,\n controllerMessenger,\n );\n\n if (interfaceId) {\n return () => {\n const { content } = controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n interfaceId,\n );\n\n const actions = getInterfaceActions(snapId, controllerMessenger, {\n id: interfaceId,\n content,\n });\n\n return {\n content,\n ...actions,\n };\n };\n }\n\n return undefined;\n}\n"]}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerActions = exports.getHooks = exports.installSnap = void 0;
|
|
4
|
+
const base_controller_1 = require("@metamask/base-controller");
|
|
5
|
+
const json_rpc_middleware_stream_1 = require("@metamask/json-rpc-middleware-stream");
|
|
6
|
+
const key_tree_1 = require("@metamask/key-tree");
|
|
7
|
+
const phishing_controller_1 = require("@metamask/phishing-controller");
|
|
8
|
+
const node_1 = require("@metamask/snaps-controllers/node");
|
|
9
|
+
const snaps_rpc_methods_1 = require("@metamask/snaps-rpc-methods");
|
|
10
|
+
const snaps_utils_1 = require("@metamask/snaps-utils");
|
|
11
|
+
const readable_stream_1 = require("readable-stream");
|
|
12
|
+
const effects_1 = require("redux-saga/effects");
|
|
13
|
+
const controllers_1 = require("./controllers.cjs");
|
|
14
|
+
const files_1 = require("./files.cjs");
|
|
15
|
+
const interface_1 = require("./interface.cjs");
|
|
16
|
+
const methods_1 = require("./methods/index.cjs");
|
|
17
|
+
const middleware_1 = require("./middleware/index.cjs");
|
|
18
|
+
const options_1 = require("./options.cjs");
|
|
19
|
+
const store_1 = require("./store/index.cjs");
|
|
20
|
+
/**
|
|
21
|
+
* Install a Snap in a simulated environment. This will fetch the Snap files,
|
|
22
|
+
* create a Redux store, set up the controllers and JSON-RPC stack, register the
|
|
23
|
+
* Snap, and run the Snap code in the execution service.
|
|
24
|
+
*
|
|
25
|
+
* @param snapId - The ID of the Snap to install.
|
|
26
|
+
* @param options - The options to use when installing the Snap.
|
|
27
|
+
* @param options.executionService - The execution service to use.
|
|
28
|
+
* @param options.executionServiceOptions - The options to use when creating the
|
|
29
|
+
* execution service, if any. This should only include options specific to the
|
|
30
|
+
* provided execution service.
|
|
31
|
+
* @param options.options - The simulation options.
|
|
32
|
+
* @returns The installed Snap object.
|
|
33
|
+
* @template Service - The type of the execution service.
|
|
34
|
+
*/
|
|
35
|
+
async function installSnap(snapId, { executionService, executionServiceOptions, options: rawOptions = {}, } = {}) {
|
|
36
|
+
const options = (0, options_1.getOptions)(rawOptions);
|
|
37
|
+
// Fetch Snap files.
|
|
38
|
+
const location = (0, node_1.detectSnapLocation)(snapId, {
|
|
39
|
+
allowLocal: true,
|
|
40
|
+
});
|
|
41
|
+
const snapFiles = await (0, node_1.fetchSnap)(snapId, location);
|
|
42
|
+
// Create Redux store.
|
|
43
|
+
const { store, runSaga } = (0, store_1.createStore)(options);
|
|
44
|
+
const controllerMessenger = new base_controller_1.ControllerMessenger();
|
|
45
|
+
registerActions(controllerMessenger, runSaga);
|
|
46
|
+
// Set up controllers and JSON-RPC stack.
|
|
47
|
+
const hooks = getHooks(options, snapFiles, snapId, controllerMessenger);
|
|
48
|
+
const { subjectMetadataController, permissionController } = (0, controllers_1.getControllers)({
|
|
49
|
+
controllerMessenger,
|
|
50
|
+
hooks,
|
|
51
|
+
runSaga,
|
|
52
|
+
options,
|
|
53
|
+
});
|
|
54
|
+
const engine = (0, middleware_1.createJsonRpcEngine)({
|
|
55
|
+
store,
|
|
56
|
+
hooks,
|
|
57
|
+
permissionMiddleware: permissionController.createPermissionMiddleware({
|
|
58
|
+
origin: snapId,
|
|
59
|
+
}),
|
|
60
|
+
});
|
|
61
|
+
// Create execution service.
|
|
62
|
+
const ExecutionService = executionService ?? node_1.NodeThreadExecutionService;
|
|
63
|
+
const service = new ExecutionService({
|
|
64
|
+
...executionServiceOptions,
|
|
65
|
+
messenger: controllerMessenger.getRestricted({
|
|
66
|
+
name: 'ExecutionService',
|
|
67
|
+
allowedActions: [],
|
|
68
|
+
allowedEvents: [],
|
|
69
|
+
}),
|
|
70
|
+
setupSnapProvider: (_snapId, rpcStream) => {
|
|
71
|
+
const mux = (0, node_1.setupMultiplex)(rpcStream, 'snapStream');
|
|
72
|
+
const stream = mux.createStream('metamask-provider');
|
|
73
|
+
const providerStream = (0, json_rpc_middleware_stream_1.createEngineStream)({ engine });
|
|
74
|
+
// Error function is difficult to test, so we ignore it.
|
|
75
|
+
/* istanbul ignore next 2 */
|
|
76
|
+
(0, readable_stream_1.pipeline)(stream, providerStream, stream, (error) => {
|
|
77
|
+
if (error) {
|
|
78
|
+
(0, snaps_utils_1.logError)(`Provider stream failure.`, error);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
// Register the Snap. This sets up the Snap's permissions and subject
|
|
84
|
+
// metadata.
|
|
85
|
+
await (0, controllers_1.registerSnap)(snapId, snapFiles.manifest.result, {
|
|
86
|
+
permissionController,
|
|
87
|
+
subjectMetadataController,
|
|
88
|
+
});
|
|
89
|
+
// Run the Snap code in the execution service.
|
|
90
|
+
await service.executeSnap({
|
|
91
|
+
snapId,
|
|
92
|
+
sourceCode: snapFiles.sourceCode.toString('utf8'),
|
|
93
|
+
endowments: await (0, methods_1.getEndowments)(permissionController, snapId),
|
|
94
|
+
});
|
|
95
|
+
return {
|
|
96
|
+
snapId,
|
|
97
|
+
store,
|
|
98
|
+
executionService: service,
|
|
99
|
+
controllerMessenger,
|
|
100
|
+
runSaga,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
exports.installSnap = installSnap;
|
|
104
|
+
/**
|
|
105
|
+
* Get the hooks for the simulation.
|
|
106
|
+
*
|
|
107
|
+
* @param options - The simulation options.
|
|
108
|
+
* @param snapFiles - The Snap files.
|
|
109
|
+
* @param snapId - The Snap ID.
|
|
110
|
+
* @param controllerMessenger - The controller messenger.
|
|
111
|
+
* @returns The hooks for the simulation.
|
|
112
|
+
*/
|
|
113
|
+
function getHooks(options, snapFiles, snapId, controllerMessenger) {
|
|
114
|
+
return {
|
|
115
|
+
getMnemonic: async () => Promise.resolve((0, key_tree_1.mnemonicPhraseToBytes)(options.secretRecoveryPhrase)),
|
|
116
|
+
getSnapFile: async (path, encoding) => await (0, files_1.getSnapFile)(snapFiles.auxiliaryFiles, path, encoding),
|
|
117
|
+
getIsLocked: () => false,
|
|
118
|
+
createInterface: async (...args) => controllerMessenger.call('SnapInterfaceController:createInterface', snapId, ...args),
|
|
119
|
+
updateInterface: async (...args) => controllerMessenger.call('SnapInterfaceController:updateInterface', snapId, ...args),
|
|
120
|
+
getInterfaceState: (...args) => controllerMessenger.call('SnapInterfaceController:getInterface', snapId, ...args).state,
|
|
121
|
+
resolveInterface: async (...args) => controllerMessenger.call('SnapInterfaceController:resolveInterface', snapId, ...args),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
exports.getHooks = getHooks;
|
|
125
|
+
/**
|
|
126
|
+
* Register mocked action handlers.
|
|
127
|
+
*
|
|
128
|
+
* @param controllerMessenger - The controller messenger.
|
|
129
|
+
* @param runSaga - The run saga function.
|
|
130
|
+
*/
|
|
131
|
+
function registerActions(controllerMessenger, runSaga) {
|
|
132
|
+
controllerMessenger.registerActionHandler('PhishingController:maybeUpdateState', async () => Promise.resolve());
|
|
133
|
+
controllerMessenger.registerActionHandler('PhishingController:testOrigin', () => ({ result: false, type: phishing_controller_1.PhishingDetectorResultType.All }));
|
|
134
|
+
controllerMessenger.registerActionHandler('ApprovalController:hasRequest', (opts) => {
|
|
135
|
+
/**
|
|
136
|
+
* Get the current interface from the store.
|
|
137
|
+
*
|
|
138
|
+
* @yields Selects the current interface from the store.
|
|
139
|
+
* @returns The current interface.
|
|
140
|
+
*/
|
|
141
|
+
function* getCurrentInterfaceSaga() {
|
|
142
|
+
const currentInterface = yield (0, effects_1.select)(store_1.getCurrentInterface);
|
|
143
|
+
return currentInterface;
|
|
144
|
+
}
|
|
145
|
+
const currentInterface = runSaga(getCurrentInterfaceSaga).result();
|
|
146
|
+
return (currentInterface?.type === snaps_rpc_methods_1.DIALOG_APPROVAL_TYPES.default &&
|
|
147
|
+
currentInterface?.id === opts?.id);
|
|
148
|
+
});
|
|
149
|
+
controllerMessenger.registerActionHandler('ApprovalController:acceptRequest', async (_id, value) => {
|
|
150
|
+
await runSaga(interface_1.resolveWithSaga, value).toPromise();
|
|
151
|
+
return { value };
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
exports.registerActions = registerActions;
|
|
155
|
+
//# sourceMappingURL=simulation.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simulation.cjs","sourceRoot":"","sources":["../src/simulation.ts"],"names":[],"mappings":";;;AAIA,+DAAgE;AAChE,qFAA0E;AAC1E,iDAA2D;AAC3D,uEAA2E;AAE3E,2DAK0C;AAC1C,mEAAoE;AAQpE,uDAAiD;AAGjD,qDAA2C;AAE3C,gDAA4C;AAG5C,mDAA6D;AAC7D,uCAAsC;AACtC,+CAA8C;AAC9C,iDAA0C;AAC1C,uDAAmD;AAEnD,2CAAuC;AAEvC,6CAA2D;AAkF3D;;;;;;;;;;;;;;GAcG;AACI,KAAK,UAAU,WAAW,CAK/B,MAAc,EACd,EACE,gBAAgB,EAChB,uBAAuB,EACvB,OAAO,EAAE,UAAU,GAAG,EAAE,MACgB,EAAE;IAE5C,MAAM,OAAO,GAAG,IAAA,oBAAU,EAAC,UAAU,CAAC,CAAC;IAEvC,oBAAoB;IACpB,MAAM,QAAQ,GAAG,IAAA,yBAAkB,EAAC,MAAM,EAAE;QAC1C,UAAU,EAAE,IAAI;KACjB,CAAC,CAAC;IAEH,MAAM,SAAS,GAAG,MAAM,IAAA,gBAAS,EAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IAEpD,sBAAsB;IACtB,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,IAAA,mBAAW,EAAC,OAAO,CAAC,CAAC;IAEhD,MAAM,mBAAmB,GAAG,IAAI,qCAAmB,EAAY,CAAC;IAEhE,eAAe,CAAC,mBAAmB,EAAE,OAAO,CAAC,CAAC;IAE9C,yCAAyC;IACzC,MAAM,KAAK,GAAG,QAAQ,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,mBAAmB,CAAC,CAAC;IAExE,MAAM,EA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AClC,mBAAmB,CAAC,IAAI,CACtB,0CAA0C,EAC1C,MAAM,EACN,GAAG,IAAI,CACR;KACJ,CAAC;AACJ,CAAC;AArCD,4BAqCC;AAED;;;;;GAKG;AACH,SAAgB,eAAe,CAC7B,mBAA4C,EAC5C,OAAwB;IAExB,mBAAmB,CAAC,qBAAqB,CACvC,qCAAqC,EACrC,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,CAC9B,CAAC;IAEF,mBAAmB,CAAC,qBAAqB,CACvC,+BAA+B,EAC/B,GAAG,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,gDAA0B,CAAC,GAAG,EAAE,CAAC,CAChE,CAAC;IAEF,mBAAmB,CAAC,qBAAqB,CACvC,+BAA+B,EAC/B,CAAC,IAAI,EAAE,EAAE;QACP;;;;;WAKG;QACH,QAAQ,CAAC,CAAC,uBAAuB;YAC/B,MAAM,gBAAgB,GAAc,MAAM,IAAA,gBAAM,EAAC,2BAAmB,CAAC,CAAC;YACtE,OAAO,gBAAgB,CAAC;QAC1B,CAAC;QAED,MAAM,gBAAgB,GAA0B,OAAO,CACrD,uBAAuB,CACxB,CAAC,MAAM,EAAE,CAAC;QACX,OAAO,CACL,gBAAgB,EAAE,IAAI,KAAK,yCAAqB,CAAC,OAAO;YACxD,gBAAgB,EAAE,EAAE,KAAK,IAAI,EAAE,EAAE,CAClC,CAAC;IACJ,CAAC,CACF,CAAC;IAEF,mBAAmB,CAAC,qBAAqB,CACvC,kCAAkC,EAClC,KAAK,EAAE,GAAW,EAAE,KAAc,EAAE,EAAE;QACpC,MAAM,OAAO,CAAC,2BAAe,EAAE,KAAK,CAAC,CAAC,SAAS,EAAE,CAAC;QAElD,OAAO,EAAE,KAAK,EAAE,CAAC;IACnB,CAAC,CACF,CAAC;AACJ,CAAC;AA9CD,0CA8CC","sourcesContent":["import type {\n ActionConstraint,\n EventConstraint,\n} from '@metamask/base-controller';\nimport { ControllerMessenger } from '@metamask/base-controller';\nimport { createEngineStream } from '@metamask/json-rpc-middleware-stream';\nimport { mnemonicPhraseToBytes } from '@metamask/key-tree';\nimport { PhishingDetectorResultType } from '@metamask/phishing-controller';\nimport type { AbstractExecutionService } from '@metamask/snaps-controllers';\nimport {\n detectSnapLocation,\n fetchSnap,\n NodeThreadExecutionService,\n setupMultiplex,\n} from '@metamask/snaps-controllers/node';\nimport { DIALOG_APPROVAL_TYPES } from '@metamask/snaps-rpc-methods';\nimport type {\n AuxiliaryFileEncoding,\n Component,\n InterfaceState,\n SnapId,\n} from '@metamask/snaps-sdk';\nimport type { FetchedSnapFiles } from '@metamask/snaps-utils';\nimport { logError } from '@metamask/snaps-utils';\nimport type { Json } from '@metamask/utils';\nimport type { Duplex } from 'readable-stream';\nimport { pipeline } from 'readable-stream';\nimport type { SagaIterator } from 'redux-saga';\nimport { select } from 'redux-saga/effects';\n\nimport type { RootControllerMessenger } from './controllers';\nimport { getControllers, registerSnap } from './controllers';\nimport { getSnapFile } from './files';\nimport { resolveWithSaga } from './interface';\nimport { getEndowments } from './methods';\nimport { createJsonRpcEngine } from './middleware';\nimport type { SimulationOptions, SimulationUserOptions } from './options';\nimport { getOptions } from './options';\nimport type { Interface, RunSagaFunction, Store } from './store';\nimport { createStore, getCurrentInterface } from './store';\n\n/**\n * Options for the execution service, without the options that are shared\n * between all execution services.\n *\n * @template Service - The type of the execution service, i.e., the class that\n * creates the execution service.\n */\nexport type ExecutionServiceOptions<\n Service extends new (...args: any[]) => any,\n> = Omit<\n ConstructorParameters<Service>[0],\n keyof ConstructorParameters<typeof AbstractExecutionService<unknown>>[0]\n>;\n\n/**\n * The options for running a Snap in a simulated environment.\n *\n * @property executionService - The execution service to use.\n * @property executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @property options - The simulation options.\n * @template Service - The type of the execution service.\n */\nexport type InstallSnapOptions<\n Service extends new (...args: any[]) => InstanceType<\n typeof AbstractExecutionService<unknown>\n >,\n> = ExecutionServiceOptions<Service> extends Record<string, never>\n ? {\n executionService: Service;\n executionServiceOptions?: ExecutionServiceOptions<Service>;\n options?: SimulationUserOptions;\n }\n : {\n executionService: Service;\n executionServiceOptions: ExecutionServiceOptions<Service>;\n options?: SimulationUserOptions;\n };\n\nexport type InstalledSnap = {\n snapId: SnapId;\n store: Store;\n executionService: InstanceType<typeof AbstractExecutionService>;\n controllerMessenger: ControllerMessenger<ActionConstraint, EventConstraint>;\n runSaga: RunSagaFunction;\n};\n\nexport type MiddlewareHooks = {\n /**\n * A hook that returns the user's secret recovery phrase.\n *\n * @returns The user's secret recovery phrase.\n */\n getMnemonic: () => Promise<Uint8Array>;\n\n /**\n * A hook that returns the Snap's auxiliary file for the given path.\n *\n * @param path - The path of the auxiliary file to get.\n * @param encoding - The encoding to use when returning the file.\n * @returns The Snap's auxiliary file for the given path.\n */\n getSnapFile: (\n path: string,\n encoding: AuxiliaryFileEncoding,\n ) => Promise<string | null>;\n\n /**\n * A hook that returns whether the client is locked or not.\n *\n * @returns A boolean flag signaling whether the client is locked.\n */\n getIsLocked: () => boolean;\n createInterface: (content: Component) => Promise<string>;\n updateInterface: (id: string, content: Component) => Promise<void>;\n getInterfaceState: (id: string) => InterfaceState;\n resolveInterface: (id: string, value: Json) => Promise<void>;\n};\n\n/**\n * Install a Snap in a simulated environment. This will fetch the Snap files,\n * create a Redux store, set up the controllers and JSON-RPC stack, register the\n * Snap, and run the Snap code in the execution service.\n *\n * @param snapId - The ID of the Snap to install.\n * @param options - The options to use when installing the Snap.\n * @param options.executionService - The execution service to use.\n * @param options.executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @param options.options - The simulation options.\n * @returns The installed Snap object.\n * @template Service - The type of the execution service.\n */\nexport async function installSnap<\n Service extends new (...args: any[]) => InstanceType<\n typeof AbstractExecutionService\n >,\n>(\n snapId: SnapId,\n {\n executionService,\n executionServiceOptions,\n options: rawOptions = {},\n }: Partial<InstallSnapOptions<Service>> = {},\n): Promise<InstalledSnap> {\n const options = getOptions(rawOptions);\n\n // Fetch Snap files.\n const location = detectSnapLocation(snapId, {\n allowLocal: true,\n });\n\n const snapFiles = await fetchSnap(snapId, location);\n\n // Create Redux store.\n const { store, runSaga } = createStore(options);\n\n const controllerMessenger = new ControllerMessenger<any, any>();\n\n registerActions(controllerMessenger, runSaga);\n\n // Set up controllers and JSON-RPC stack.\n const hooks = getHooks(options, snapFiles, snapId, controllerMessenger);\n\n const { subjectMetadataController, permissionController } = getControllers({\n controllerMessenger,\n hooks,\n runSaga,\n options,\n });\n\n const engine = createJsonRpcEngine({\n store,\n hooks,\n permissionMiddleware: permissionController.createPermissionMiddleware({\n origin: snapId,\n }),\n });\n\n // Create execution service.\n const ExecutionService = executionService ?? NodeThreadExecutionService;\n const service = new ExecutionService({\n ...executionServiceOptions,\n messenger: controllerMessenger.getRestricted({\n name: 'ExecutionService',\n allowedActions: [],\n allowedEvents: [],\n }),\n setupSnapProvider: (_snapId: string, rpcStream: Duplex) => {\n const mux = setupMultiplex(rpcStream, 'snapStream');\n const stream = mux.createStream('metamask-provider');\n const providerStream = createEngineStream({ engine });\n\n // Error function is difficult to test, so we ignore it.\n /* istanbul ignore next 2 */\n pipeline(stream, providerStream, stream, (error: unknown) => {\n if (error) {\n logError(`Provider stream failure.`, error);\n }\n });\n },\n });\n\n // Register the Snap. This sets up the Snap's permissions and subject\n // metadata.\n await registerSnap(snapId, snapFiles.manifest.result, {\n permissionController,\n subjectMetadataController,\n });\n\n // Run the Snap code in the execution service.\n await service.executeSnap({\n snapId,\n sourceCode: snapFiles.sourceCode.toString('utf8'),\n endowments: await getEndowments(permissionController, snapId),\n });\n\n return {\n snapId,\n store,\n executionService: service,\n controllerMessenger,\n runSaga,\n };\n}\n\n/**\n * Get the hooks for the simulation.\n *\n * @param options - The simulation options.\n * @param snapFiles - The Snap files.\n * @param snapId - The Snap ID.\n * @param controllerMessenger - The controller messenger.\n * @returns The hooks for the simulation.\n */\nexport function getHooks(\n options: SimulationOptions,\n snapFiles: FetchedSnapFiles,\n snapId: SnapId,\n controllerMessenger: RootControllerMessenger,\n): MiddlewareHooks {\n return {\n getMnemonic: async () =>\n Promise.resolve(mnemonicPhraseToBytes(options.secretRecoveryPhrase)),\n getSnapFile: async (path: string, encoding: AuxiliaryFileEncoding) =>\n await getSnapFile(snapFiles.auxiliaryFiles, path, encoding),\n getIsLocked: () => false,\n createInterface: async (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:createInterface',\n snapId,\n ...args,\n ),\n updateInterface: async (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:updateInterface',\n snapId,\n ...args,\n ),\n getInterfaceState: (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n ...args,\n ).state,\n resolveInterface: async (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:resolveInterface',\n snapId,\n ...args,\n ),\n };\n}\n\n/**\n * Register mocked action handlers.\n *\n * @param controllerMessenger - The controller messenger.\n * @param runSaga - The run saga function.\n */\nexport function registerActions(\n controllerMessenger: RootControllerMessenger,\n runSaga: RunSagaFunction,\n) {\n controllerMessenger.registerActionHandler(\n 'PhishingController:maybeUpdateState',\n async () => Promise.resolve(),\n );\n\n controllerMessenger.registerActionHandler(\n 'PhishingController:testOrigin',\n () => ({ result: false, type: PhishingDetectorResultType.All }),\n );\n\n controllerMessenger.registerActionHandler(\n 'ApprovalController:hasRequest',\n (opts) => {\n /**\n * Get the current interface from the store.\n *\n * @yields Selects the current interface from the store.\n * @returns The current interface.\n */\n function* getCurrentInterfaceSaga(): SagaIterator {\n const currentInterface: Interface = yield select(getCurrentInterface);\n return currentInterface;\n }\n\n const currentInterface: Interface | undefined = runSaga(\n getCurrentInterfaceSaga,\n ).result();\n return (\n currentInterface?.type === DIALOG_APPROVAL_TYPES.default &&\n currentInterface?.id === opts?.id\n );\n },\n );\n\n controllerMessenger.registerActionHandler(\n 'ApprovalController:acceptRequest',\n async (_id: string, value: unknown) => {\n await runSaga(resolveWithSaga, value).toPromise();\n\n return { value };\n },\n );\n}\n"]}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { ActionConstraint, EventConstraint } from "@metamask/base-controller";
|
|
2
|
+
import { ControllerMessenger } from "@metamask/base-controller";
|
|
3
|
+
import type { AbstractExecutionService } from "@metamask/snaps-controllers";
|
|
4
|
+
import type { AuxiliaryFileEncoding, Component, InterfaceState, SnapId } from "@metamask/snaps-sdk";
|
|
5
|
+
import type { FetchedSnapFiles } from "@metamask/snaps-utils";
|
|
6
|
+
import type { Json } from "@metamask/utils";
|
|
7
|
+
import type { RootControllerMessenger } from "./controllers.cjs";
|
|
8
|
+
import type { SimulationOptions, SimulationUserOptions } from "./options.cjs";
|
|
9
|
+
import type { RunSagaFunction, Store } from "./store/index.cjs";
|
|
10
|
+
/**
|
|
11
|
+
* Options for the execution service, without the options that are shared
|
|
12
|
+
* between all execution services.
|
|
13
|
+
*
|
|
14
|
+
* @template Service - The type of the execution service, i.e., the class that
|
|
15
|
+
* creates the execution service.
|
|
16
|
+
*/
|
|
17
|
+
export type ExecutionServiceOptions<Service extends new (...args: any[]) => any> = Omit<ConstructorParameters<Service>[0], keyof ConstructorParameters<typeof AbstractExecutionService<unknown>>[0]>;
|
|
18
|
+
/**
|
|
19
|
+
* The options for running a Snap in a simulated environment.
|
|
20
|
+
*
|
|
21
|
+
* @property executionService - The execution service to use.
|
|
22
|
+
* @property executionServiceOptions - The options to use when creating the
|
|
23
|
+
* execution service, if any. This should only include options specific to the
|
|
24
|
+
* provided execution service.
|
|
25
|
+
* @property options - The simulation options.
|
|
26
|
+
* @template Service - The type of the execution service.
|
|
27
|
+
*/
|
|
28
|
+
export type InstallSnapOptions<Service extends new (...args: any[]) => InstanceType<typeof AbstractExecutionService<unknown>>> = ExecutionServiceOptions<Service> extends Record<string, never> ? {
|
|
29
|
+
executionService: Service;
|
|
30
|
+
executionServiceOptions?: ExecutionServiceOptions<Service>;
|
|
31
|
+
options?: SimulationUserOptions;
|
|
32
|
+
} : {
|
|
33
|
+
executionService: Service;
|
|
34
|
+
executionServiceOptions: ExecutionServiceOptions<Service>;
|
|
35
|
+
options?: SimulationUserOptions;
|
|
36
|
+
};
|
|
37
|
+
export type InstalledSnap = {
|
|
38
|
+
snapId: SnapId;
|
|
39
|
+
store: Store;
|
|
40
|
+
executionService: InstanceType<typeof AbstractExecutionService>;
|
|
41
|
+
controllerMessenger: ControllerMessenger<ActionConstraint, EventConstraint>;
|
|
42
|
+
runSaga: RunSagaFunction;
|
|
43
|
+
};
|
|
44
|
+
export type MiddlewareHooks = {
|
|
45
|
+
/**
|
|
46
|
+
* A hook that returns the user's secret recovery phrase.
|
|
47
|
+
*
|
|
48
|
+
* @returns The user's secret recovery phrase.
|
|
49
|
+
*/
|
|
50
|
+
getMnemonic: () => Promise<Uint8Array>;
|
|
51
|
+
/**
|
|
52
|
+
* A hook that returns the Snap's auxiliary file for the given path.
|
|
53
|
+
*
|
|
54
|
+
* @param path - The path of the auxiliary file to get.
|
|
55
|
+
* @param encoding - The encoding to use when returning the file.
|
|
56
|
+
* @returns The Snap's auxiliary file for the given path.
|
|
57
|
+
*/
|
|
58
|
+
getSnapFile: (path: string, encoding: AuxiliaryFileEncoding) => Promise<string | null>;
|
|
59
|
+
/**
|
|
60
|
+
* A hook that returns whether the client is locked or not.
|
|
61
|
+
*
|
|
62
|
+
* @returns A boolean flag signaling whether the client is locked.
|
|
63
|
+
*/
|
|
64
|
+
getIsLocked: () => boolean;
|
|
65
|
+
createInterface: (content: Component) => Promise<string>;
|
|
66
|
+
updateInterface: (id: string, content: Component) => Promise<void>;
|
|
67
|
+
getInterfaceState: (id: string) => InterfaceState;
|
|
68
|
+
resolveInterface: (id: string, value: Json) => Promise<void>;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Install a Snap in a simulated environment. This will fetch the Snap files,
|
|
72
|
+
* create a Redux store, set up the controllers and JSON-RPC stack, register the
|
|
73
|
+
* Snap, and run the Snap code in the execution service.
|
|
74
|
+
*
|
|
75
|
+
* @param snapId - The ID of the Snap to install.
|
|
76
|
+
* @param options - The options to use when installing the Snap.
|
|
77
|
+
* @param options.executionService - The execution service to use.
|
|
78
|
+
* @param options.executionServiceOptions - The options to use when creating the
|
|
79
|
+
* execution service, if any. This should only include options specific to the
|
|
80
|
+
* provided execution service.
|
|
81
|
+
* @param options.options - The simulation options.
|
|
82
|
+
* @returns The installed Snap object.
|
|
83
|
+
* @template Service - The type of the execution service.
|
|
84
|
+
*/
|
|
85
|
+
export declare function installSnap<Service extends new (...args: any[]) => InstanceType<typeof AbstractExecutionService>>(snapId: SnapId, { executionService, executionServiceOptions, options: rawOptions, }?: Partial<InstallSnapOptions<Service>>): Promise<InstalledSnap>;
|
|
86
|
+
/**
|
|
87
|
+
* Get the hooks for the simulation.
|
|
88
|
+
*
|
|
89
|
+
* @param options - The simulation options.
|
|
90
|
+
* @param snapFiles - The Snap files.
|
|
91
|
+
* @param snapId - The Snap ID.
|
|
92
|
+
* @param controllerMessenger - The controller messenger.
|
|
93
|
+
* @returns The hooks for the simulation.
|
|
94
|
+
*/
|
|
95
|
+
export declare function getHooks(options: SimulationOptions, snapFiles: FetchedSnapFiles, snapId: SnapId, controllerMessenger: RootControllerMessenger): MiddlewareHooks;
|
|
96
|
+
/**
|
|
97
|
+
* Register mocked action handlers.
|
|
98
|
+
*
|
|
99
|
+
* @param controllerMessenger - The controller messenger.
|
|
100
|
+
* @param runSaga - The run saga function.
|
|
101
|
+
*/
|
|
102
|
+
export declare function registerActions(controllerMessenger: RootControllerMessenger, runSaga: RunSagaFunction): void;
|
|
103
|
+
//# sourceMappingURL=simulation.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simulation.d.cts","sourceRoot":"","sources":["../src/simulation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,eAAe,EAChB,kCAAkC;AACnC,OAAO,EAA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