@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,23 @@
|
|
|
1
|
+
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
|
|
2
|
+
import type { Json, JsonRpcParams } from "@metamask/utils";
|
|
3
|
+
export type InternalMethodsMiddlewareHooks = {
|
|
4
|
+
/**
|
|
5
|
+
* A hook that returns the user's secret recovery phrase.
|
|
6
|
+
*
|
|
7
|
+
* @returns The user's secret recovery phrase.
|
|
8
|
+
*/
|
|
9
|
+
getMnemonic: () => Promise<Uint8Array>;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Create a middleware for handling JSON-RPC methods normally handled internally
|
|
13
|
+
* by the MetaMask client.
|
|
14
|
+
*
|
|
15
|
+
* NOTE: This middleware provides all `hooks` to all handlers and should
|
|
16
|
+
* therefore NOT be used outside of the simulation environment. It is intended
|
|
17
|
+
* for testing purposes only.
|
|
18
|
+
*
|
|
19
|
+
* @param hooks - Any hooks used by the middleware handlers.
|
|
20
|
+
* @returns A middleware function.
|
|
21
|
+
*/
|
|
22
|
+
export declare function createInternalMethodsMiddleware(hooks: InternalMethodsMiddlewareHooks): JsonRpcMiddleware<JsonRpcParams, Json>;
|
|
23
|
+
//# sourceMappingURL=middleware.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.d.mts","sourceRoot":"","sources":["../../../src/middleware/internal-methods/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AAEnE,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,wBAAwB;AAK3D,MAAM,MAAM,8BAA8B,GAAG;IAC3C;;;;OAIG;IACH,WAAW,EAAE,MAAM,OAAO,CAAC,UAAU,CAAC,CAAC;CACxC,CAAC;AAUF;;;;;;;;;;GAUG;AACH,wBAAgB,+BAA+B,CAC7C,KAAK,EAAE,8BAA8B,GACpC,iBAAiB,CAAC,aAAa,EAAE,IAAI,CAAC,CAkBxC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { logError } from "@metamask/snaps-utils";
|
|
2
|
+
import { getAccountsHandler } from "./accounts.mjs";
|
|
3
|
+
import { getProviderStateHandler } from "./provider-state.mjs";
|
|
4
|
+
const methodHandlers = {
|
|
5
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
6
|
+
metamask_getProviderState: getProviderStateHandler,
|
|
7
|
+
eth_requestAccounts: getAccountsHandler,
|
|
8
|
+
eth_accounts: getAccountsHandler,
|
|
9
|
+
/* eslint-enable @typescript-eslint/naming-convention */
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Create a middleware for handling JSON-RPC methods normally handled internally
|
|
13
|
+
* by the MetaMask client.
|
|
14
|
+
*
|
|
15
|
+
* NOTE: This middleware provides all `hooks` to all handlers and should
|
|
16
|
+
* therefore NOT be used outside of the simulation environment. It is intended
|
|
17
|
+
* for testing purposes only.
|
|
18
|
+
*
|
|
19
|
+
* @param hooks - Any hooks used by the middleware handlers.
|
|
20
|
+
* @returns A middleware function.
|
|
21
|
+
*/
|
|
22
|
+
export function createInternalMethodsMiddleware(hooks) {
|
|
23
|
+
// This should probably use createAsyncMiddleware.
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
25
|
+
return async function methodMiddleware(request, response, next, end) {
|
|
26
|
+
const handler = methodHandlers[request.method];
|
|
27
|
+
if (handler) {
|
|
28
|
+
try {
|
|
29
|
+
// Implementations may or may not be async, so we must await them.
|
|
30
|
+
return await handler(request, response, next, end, hooks);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
logError(error);
|
|
34
|
+
return end(error);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return next();
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=middleware.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.mjs","sourceRoot":"","sources":["../../../src/middleware/internal-methods/middleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,8BAA8B;AAGjD,OAAO,EAAE,kBAAkB,EAAE,uBAAmB;AAChD,OAAO,EAAE,uBAAuB,EAAE,6BAAyB;AAW3D,MAAM,cAAc,GAAG;IACrB,yDAAyD;IACzD,yBAAyB,EAAE,uBAAuB;IAClD,mBAAmB,EAAE,kBAAkB;IACvC,YAAY,EAAE,kBAAkB;IAChC,wDAAwD;CACzD,CAAC;AAEF;;;;;;;;;;GAUG;AACH,MAAM,UAAU,+BAA+B,CAC7C,KAAqC;IAErC,kDAAkD;IAClD,kEAAkE;IAClE,OAAO,KAAK,UAAU,gBAAgB,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG;QACjE,MAAM,OAAO,GACX,cAAc,CAAC,OAAO,CAAC,MAAqC,CAAC,CAAC;QAChE,IAAI,OAAO,EAAE,CAAC;YACZ,IAAI,CAAC;gBACH,kEAAkE;gBAClE,OAAO,MAAM,OAAO,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;YAC5D,CAAC;YAAC,OAAO,KAAU,EAAE,CAAC;gBACpB,QAAQ,CAAC,KAAK,CAAC,CAAC;gBAChB,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;YACpB,CAAC;QACH,CAAC;QAED,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { logError } from '@metamask/snaps-utils';\nimport type { Json, JsonRpcParams } from '@metamask/utils';\n\nimport { getAccountsHandler } from './accounts';\nimport { getProviderStateHandler } from './provider-state';\n\nexport type InternalMethodsMiddlewareHooks = {\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\nconst methodHandlers = {\n /* eslint-disable @typescript-eslint/naming-convention */\n metamask_getProviderState: getProviderStateHandler,\n eth_requestAccounts: getAccountsHandler,\n eth_accounts: getAccountsHandler,\n /* eslint-enable @typescript-eslint/naming-convention */\n};\n\n/**\n * Create a middleware for handling JSON-RPC methods normally handled internally\n * by the MetaMask client.\n *\n * NOTE: This middleware provides all `hooks` to all handlers and should\n * therefore NOT be used outside of the simulation environment. It is intended\n * for testing purposes only.\n *\n * @param hooks - Any hooks used by the middleware handlers.\n * @returns A middleware function.\n */\nexport function createInternalMethodsMiddleware(\n hooks: InternalMethodsMiddlewareHooks,\n): JsonRpcMiddleware<JsonRpcParams, Json> {\n // This should probably use createAsyncMiddleware.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n return async function methodMiddleware(request, response, next, end) {\n const handler =\n methodHandlers[request.method as keyof typeof methodHandlers];\n if (handler) {\n try {\n // Implementations may or may not be async, so we must await them.\n return await handler(request, response, next, end, hooks);\n } catch (error: any) {\n logError(error);\n return end(error);\n }\n }\n\n return next();\n };\n}\n"]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getProviderStateHandler = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* A mock handler for metamask_getProviderState that always returns a specific
|
|
6
|
+
* hardcoded result.
|
|
7
|
+
*
|
|
8
|
+
* @param _request - Incoming JSON-RPC request. Ignored for this specific
|
|
9
|
+
* handler.
|
|
10
|
+
* @param response - The outgoing JSON-RPC response, modified to return the
|
|
11
|
+
* result.
|
|
12
|
+
* @param _next - The `json-rpc-engine` middleware next handler.
|
|
13
|
+
* @param end - The `json-rpc-engine` middleware end handler.
|
|
14
|
+
* @returns The JSON-RPC response.
|
|
15
|
+
*/
|
|
16
|
+
async function getProviderStateHandler(_request, response, _next, end) {
|
|
17
|
+
// For now this will return a mocked result, this should probably match
|
|
18
|
+
// whatever network the simulation is using.
|
|
19
|
+
response.result = {
|
|
20
|
+
isUnlocked: true,
|
|
21
|
+
chainId: '0x01',
|
|
22
|
+
networkVersion: '0x01',
|
|
23
|
+
accounts: [],
|
|
24
|
+
};
|
|
25
|
+
return end();
|
|
26
|
+
}
|
|
27
|
+
exports.getProviderStateHandler = getProviderStateHandler;
|
|
28
|
+
//# sourceMappingURL=provider-state.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-state.cjs","sourceRoot":"","sources":["../../../src/middleware/internal-methods/provider-state.ts"],"names":[],"mappings":";;;AAUA;;;;;;;;;;;GAWG;AACI,KAAK,UAAU,uBAAuB,CAC3C,QAAwB,EACxB,QAAsC,EACtC,KAAgC,EAChC,GAA6B;IAE7B,uEAAuE;IACvE,4CAA4C;IAC5C,QAAQ,CAAC,MAAM,GAAG;QAChB,UAAU,EAAE,IAAI;QAChB,OAAO,EAAE,MAAM;QACf,cAAc,EAAE,MAAM;QACtB,QAAQ,EAAE,EAAE;KACb,CAAC;IAEF,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAhBD,0DAgBC","sourcesContent":["import type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n} from '@metamask/json-rpc-engine';\nimport type {\n Json,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\n/**\n * A mock handler for metamask_getProviderState that always returns a specific\n * hardcoded result.\n *\n * @param _request - Incoming JSON-RPC request. Ignored for this specific\n * handler.\n * @param response - The outgoing JSON-RPC response, modified to return the\n * result.\n * @param _next - The `json-rpc-engine` middleware next handler.\n * @param end - The `json-rpc-engine` middleware end handler.\n * @returns The JSON-RPC response.\n */\nexport async function getProviderStateHandler(\n _request: JsonRpcRequest,\n response: PendingJsonRpcResponse<Json>,\n _next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n) {\n // For now this will return a mocked result, this should probably match\n // whatever network the simulation is using.\n response.result = {\n isUnlocked: true,\n chainId: '0x01',\n networkVersion: '0x01',\n accounts: [],\n };\n\n return end();\n}\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from "@metamask/json-rpc-engine";
|
|
2
|
+
import type { Json, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
|
|
3
|
+
/**
|
|
4
|
+
* A mock handler for metamask_getProviderState that always returns a specific
|
|
5
|
+
* hardcoded result.
|
|
6
|
+
*
|
|
7
|
+
* @param _request - Incoming JSON-RPC request. Ignored for this specific
|
|
8
|
+
* handler.
|
|
9
|
+
* @param response - The outgoing JSON-RPC response, modified to return the
|
|
10
|
+
* result.
|
|
11
|
+
* @param _next - The `json-rpc-engine` middleware next handler.
|
|
12
|
+
* @param end - The `json-rpc-engine` middleware end handler.
|
|
13
|
+
* @returns The JSON-RPC response.
|
|
14
|
+
*/
|
|
15
|
+
export declare function getProviderStateHandler(_request: JsonRpcRequest, response: PendingJsonRpcResponse<Json>, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback): Promise<void>;
|
|
16
|
+
//# sourceMappingURL=provider-state.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-state.d.cts","sourceRoot":"","sources":["../../../src/middleware/internal-methods/provider-state.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,yBAAyB,EAC1B,kCAAkC;AACnC,OAAO,KAAK,EACV,IAAI,EACJ,cAAc,EACd,sBAAsB,EACvB,wBAAwB;AAEzB;;;;;;;;;;;GAWG;AACH,wBAAsB,uBAAuB,CAC3C,QAAQ,EAAE,cAAc,EACxB,QAAQ,EAAE,sBAAsB,CAAC,IAAI,CAAC,EACtC,KAAK,EAAE,yBAAyB,EAChC,GAAG,EAAE,wBAAwB,iBAY9B"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from "@metamask/json-rpc-engine";
|
|
2
|
+
import type { Json, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
|
|
3
|
+
/**
|
|
4
|
+
* A mock handler for metamask_getProviderState that always returns a specific
|
|
5
|
+
* hardcoded result.
|
|
6
|
+
*
|
|
7
|
+
* @param _request - Incoming JSON-RPC request. Ignored for this specific
|
|
8
|
+
* handler.
|
|
9
|
+
* @param response - The outgoing JSON-RPC response, modified to return the
|
|
10
|
+
* result.
|
|
11
|
+
* @param _next - The `json-rpc-engine` middleware next handler.
|
|
12
|
+
* @param end - The `json-rpc-engine` middleware end handler.
|
|
13
|
+
* @returns The JSON-RPC response.
|
|
14
|
+
*/
|
|
15
|
+
export declare function getProviderStateHandler(_request: JsonRpcRequest, response: PendingJsonRpcResponse<Json>, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback): Promise<void>;
|
|
16
|
+
//# sourceMappingURL=provider-state.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-state.d.mts","sourceRoot":"","sources":["../../../src/middleware/internal-methods/provider-state.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,yBAAyB,EAC1B,kCAAkC;AACnC,OAAO,KAAK,EACV,IAAI,EACJ,cAAc,EACd,sBAAsB,EACvB,wBAAwB;AAEzB;;;;;;;;;;;GAWG;AACH,wBAAsB,uBAAuB,CAC3C,QAAQ,EAAE,cAAc,EACxB,QAAQ,EAAE,sBAAsB,CAAC,IAAI,CAAC,EACtC,KAAK,EAAE,yBAAyB,EAChC,GAAG,EAAE,wBAAwB,iBAY9B"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A mock handler for metamask_getProviderState that always returns a specific
|
|
3
|
+
* hardcoded result.
|
|
4
|
+
*
|
|
5
|
+
* @param _request - Incoming JSON-RPC request. Ignored for this specific
|
|
6
|
+
* handler.
|
|
7
|
+
* @param response - The outgoing JSON-RPC response, modified to return the
|
|
8
|
+
* result.
|
|
9
|
+
* @param _next - The `json-rpc-engine` middleware next handler.
|
|
10
|
+
* @param end - The `json-rpc-engine` middleware end handler.
|
|
11
|
+
* @returns The JSON-RPC response.
|
|
12
|
+
*/
|
|
13
|
+
export async function getProviderStateHandler(_request, response, _next, end) {
|
|
14
|
+
// For now this will return a mocked result, this should probably match
|
|
15
|
+
// whatever network the simulation is using.
|
|
16
|
+
response.result = {
|
|
17
|
+
isUnlocked: true,
|
|
18
|
+
chainId: '0x01',
|
|
19
|
+
networkVersion: '0x01',
|
|
20
|
+
accounts: [],
|
|
21
|
+
};
|
|
22
|
+
return end();
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=provider-state.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-state.mjs","sourceRoot":"","sources":["../../../src/middleware/internal-methods/provider-state.ts"],"names":[],"mappings":"AAUA;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,QAAwB,EACxB,QAAsC,EACtC,KAAgC,EAChC,GAA6B;IAE7B,uEAAuE;IACvE,4CAA4C;IAC5C,QAAQ,CAAC,MAAM,GAAG;QAChB,UAAU,EAAE,IAAI;QAChB,OAAO,EAAE,MAAM;QACf,cAAc,EAAE,MAAM;QACtB,QAAQ,EAAE,EAAE;KACb,CAAC;IAEF,OAAO,GAAG,EAAE,CAAC;AACf,CAAC","sourcesContent":["import type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n} from '@metamask/json-rpc-engine';\nimport type {\n Json,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\n/**\n * A mock handler for metamask_getProviderState that always returns a specific\n * hardcoded result.\n *\n * @param _request - Incoming JSON-RPC request. Ignored for this specific\n * handler.\n * @param response - The outgoing JSON-RPC response, modified to return the\n * result.\n * @param _next - The `json-rpc-engine` middleware next handler.\n * @param end - The `json-rpc-engine` middleware end handler.\n * @returns The JSON-RPC response.\n */\nexport async function getProviderStateHandler(\n _request: JsonRpcRequest,\n response: PendingJsonRpcResponse<Json>,\n _next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n) {\n // For now this will return a mocked result, this should probably match\n // whatever network the simulation is using.\n response.result = {\n isUnlocked: true,\n chainId: '0x01',\n networkVersion: '0x01',\n accounts: [],\n };\n\n return end();\n}\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createMockMiddleware = void 0;
|
|
4
|
+
const mocks_1 = require("../store/mocks.cjs");
|
|
5
|
+
/**
|
|
6
|
+
* Create a middleware for handling JSON-RPC methods that have been mocked.
|
|
7
|
+
*
|
|
8
|
+
* @param store - The Redux store to use.
|
|
9
|
+
* @returns A middleware function.
|
|
10
|
+
*/
|
|
11
|
+
function createMockMiddleware(store) {
|
|
12
|
+
return function mockMiddleware(request, response, next, end) {
|
|
13
|
+
const result = (0, mocks_1.getJsonRpcMock)(store.getState(), request.method);
|
|
14
|
+
if (result) {
|
|
15
|
+
response.result = result;
|
|
16
|
+
return end();
|
|
17
|
+
}
|
|
18
|
+
return next();
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
exports.createMockMiddleware = createMockMiddleware;
|
|
22
|
+
//# sourceMappingURL=mock.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mock.cjs","sourceRoot":"","sources":["../../src/middleware/mock.ts"],"names":[],"mappings":";;;AAIA,8CAAgD;AAEhD;;;;;GAKG;AACH,SAAgB,oBAAoB,CAClC,KAAY;IAEZ,OAAO,SAAS,cAAc,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG;QACzD,MAAM,MAAM,GAAG,IAAA,sBAAc,EAAC,KAAK,CAAC,QAAQ,EAAE,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;QAChE,IAAI,MAAM,EAAE,CAAC;YACX,QAAQ,CAAC,MAAM,GAAG,MAAM,CAAC;YACzB,OAAO,GAAG,EAAE,CAAC;QACf,CAAC;QAED,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AAZD,oDAYC","sourcesContent":["import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport type { Json, JsonRpcParams } from '@metamask/utils';\n\nimport type { Store } from '../store';\nimport { getJsonRpcMock } from '../store/mocks';\n\n/**\n * Create a middleware for handling JSON-RPC methods that have been mocked.\n *\n * @param store - The Redux store to use.\n * @returns A middleware function.\n */\nexport function createMockMiddleware(\n store: Store,\n): JsonRpcMiddleware<JsonRpcParams, Json> {\n return function mockMiddleware(request, response, next, end) {\n const result = getJsonRpcMock(store.getState(), request.method);\n if (result) {\n response.result = result;\n return end();\n }\n\n return next();\n };\n}\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
|
|
2
|
+
import type { Json, JsonRpcParams } from "@metamask/utils";
|
|
3
|
+
import type { Store } from "../store/index.cjs";
|
|
4
|
+
/**
|
|
5
|
+
* Create a middleware for handling JSON-RPC methods that have been mocked.
|
|
6
|
+
*
|
|
7
|
+
* @param store - The Redux store to use.
|
|
8
|
+
* @returns A middleware function.
|
|
9
|
+
*/
|
|
10
|
+
export declare function createMockMiddleware(store: Store): JsonRpcMiddleware<JsonRpcParams, Json>;
|
|
11
|
+
//# sourceMappingURL=mock.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mock.d.cts","sourceRoot":"","sources":["../../src/middleware/mock.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AACnE,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,wBAAwB;AAE3D,OAAO,KAAK,EAAE,KAAK,EAAE,2BAAiB;AAGtC;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,KAAK,GACX,iBAAiB,CAAC,aAAa,EAAE,IAAI,CAAC,CAUxC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
|
|
2
|
+
import type { Json, JsonRpcParams } from "@metamask/utils";
|
|
3
|
+
import type { Store } from "../store/index.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* Create a middleware for handling JSON-RPC methods that have been mocked.
|
|
6
|
+
*
|
|
7
|
+
* @param store - The Redux store to use.
|
|
8
|
+
* @returns A middleware function.
|
|
9
|
+
*/
|
|
10
|
+
export declare function createMockMiddleware(store: Store): JsonRpcMiddleware<JsonRpcParams, Json>;
|
|
11
|
+
//# sourceMappingURL=mock.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mock.d.mts","sourceRoot":"","sources":["../../src/middleware/mock.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AACnE,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,wBAAwB;AAE3D,OAAO,KAAK,EAAE,KAAK,EAAE,2BAAiB;AAGtC;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,KAAK,GACX,iBAAiB,CAAC,aAAa,EAAE,IAAI,CAAC,CAUxC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { getJsonRpcMock } from "../store/mocks.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Create a middleware for handling JSON-RPC methods that have been mocked.
|
|
4
|
+
*
|
|
5
|
+
* @param store - The Redux store to use.
|
|
6
|
+
* @returns A middleware function.
|
|
7
|
+
*/
|
|
8
|
+
export function createMockMiddleware(store) {
|
|
9
|
+
return function mockMiddleware(request, response, next, end) {
|
|
10
|
+
const result = getJsonRpcMock(store.getState(), request.method);
|
|
11
|
+
if (result) {
|
|
12
|
+
response.result = result;
|
|
13
|
+
return end();
|
|
14
|
+
}
|
|
15
|
+
return next();
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=mock.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mock.mjs","sourceRoot":"","sources":["../../src/middleware/mock.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,2BAAuB;AAEhD;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAClC,KAAY;IAEZ,OAAO,SAAS,cAAc,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG;QACzD,MAAM,MAAM,GAAG,cAAc,CAAC,KAAK,CAAC,QAAQ,EAAE,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;QAChE,IAAI,MAAM,EAAE,CAAC;YACX,QAAQ,CAAC,MAAM,GAAG,MAAM,CAAC;YACzB,OAAO,GAAG,EAAE,CAAC;QACf,CAAC;QAED,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport type { Json, JsonRpcParams } from '@metamask/utils';\n\nimport type { Store } from '../store';\nimport { getJsonRpcMock } from '../store/mocks';\n\n/**\n * Create a middleware for handling JSON-RPC methods that have been mocked.\n *\n * @param store - The Redux store to use.\n * @returns A middleware function.\n */\nexport function createMockMiddleware(\n store: Store,\n): JsonRpcMiddleware<JsonRpcParams, Json> {\n return function mockMiddleware(request, response, next, end) {\n const result = getJsonRpcMock(store.getState(), request.method);\n if (result) {\n response.result = result;\n return end();\n }\n\n return next();\n };\n}\n"]}
|
package/dist/options.cjs
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getOptions = void 0;
|
|
4
|
+
const superstruct_1 = require("@metamask/superstruct");
|
|
5
|
+
const utils_1 = require("@metamask/utils");
|
|
6
|
+
const constants_1 = require("./constants.cjs");
|
|
7
|
+
const SimulationOptionsStruct = (0, superstruct_1.object)({
|
|
8
|
+
currency: (0, superstruct_1.defaulted)((0, superstruct_1.optional)((0, superstruct_1.string)()), constants_1.DEFAULT_CURRENCY),
|
|
9
|
+
secretRecoveryPhrase: (0, superstruct_1.defaulted)((0, superstruct_1.optional)((0, superstruct_1.string)()), constants_1.DEFAULT_SRP),
|
|
10
|
+
locale: (0, superstruct_1.defaulted)((0, superstruct_1.optional)((0, superstruct_1.string)()), constants_1.DEFAULT_LOCALE),
|
|
11
|
+
state: (0, superstruct_1.defaulted)((0, superstruct_1.optional)((0, superstruct_1.nullable)((0, superstruct_1.record)((0, superstruct_1.string)(), utils_1.JsonStruct))), null),
|
|
12
|
+
unencryptedState: (0, superstruct_1.defaulted)((0, superstruct_1.optional)((0, superstruct_1.nullable)((0, superstruct_1.record)((0, superstruct_1.string)(), utils_1.JsonStruct))), null),
|
|
13
|
+
});
|
|
14
|
+
/**
|
|
15
|
+
* Get the options for the simulation.
|
|
16
|
+
*
|
|
17
|
+
* @param options - The user options. Any options not specified will be filled
|
|
18
|
+
* in with default values.
|
|
19
|
+
* @returns The simulation options.
|
|
20
|
+
*/
|
|
21
|
+
function getOptions(options) {
|
|
22
|
+
return (0, superstruct_1.create)(options, SimulationOptionsStruct);
|
|
23
|
+
}
|
|
24
|
+
exports.getOptions = getOptions;
|
|
25
|
+
//# sourceMappingURL=options.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.cjs","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":";;;AACA,uDAQ+B;AAC/B,2CAA6C;AAE7C,+CAA4E;AAE5E,MAAM,uBAAuB,GAAG,IAAA,oBAAM,EAAC;IACrC,QAAQ,EAAE,IAAA,uBAAS,EAAC,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC,EAAE,4BAAgB,CAAC;IACzD,oBAAoB,EAAE,IAAA,uBAAS,EAAC,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC,EAAE,uBAAW,CAAC;IAChE,MAAM,EAAE,IAAA,uBAAS,EAAC,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC,EAAE,0BAAc,CAAC;IACrD,KAAK,EAAE,IAAA,uBAAS,EAAC,IAAA,sBAAQ,EAAC,IAAA,sBAAQ,EAAC,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,kBAAU,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC;IACxE,gBAAgB,EAAE,IAAA,uBAAS,EACzB,IAAA,sBAAQ,EAAC,IAAA,sBAAQ,EAAC,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,kBAAU,CAAC,CAAC,CAAC,EAChD,IAAI,CACL;CACF,CAAC,CAAC;AAoBH;;;;;;GAMG;AACH,SAAgB,UAAU,CAAC,OAA8B;IACvD,OAAO,IAAA,oBAAM,EACX,OAAO,EACP,uBAAuB,CACW,CAAC;AACvC,CAAC;AALD,gCAKC","sourcesContent":["import type { Infer } from '@metamask/superstruct';\nimport {\n create,\n defaulted,\n nullable,\n object,\n optional,\n record,\n string,\n} from '@metamask/superstruct';\nimport { JsonStruct } from '@metamask/utils';\n\nimport { DEFAULT_CURRENCY, DEFAULT_LOCALE, DEFAULT_SRP } from './constants';\n\nconst SimulationOptionsStruct = object({\n currency: defaulted(optional(string()), DEFAULT_CURRENCY),\n secretRecoveryPhrase: defaulted(optional(string()), DEFAULT_SRP),\n locale: defaulted(optional(string()), DEFAULT_LOCALE),\n state: defaulted(optional(nullable(record(string(), JsonStruct))), null),\n unencryptedState: defaulted(\n optional(nullable(record(string(), JsonStruct))),\n null,\n ),\n});\n\n/**\n * Options for the simulation.\n *\n * @property secretRecoveryPhrase - The secret recovery phrase to use. This is\n * used to derive addresses and private keys. Defaults to a test recovery\n * phrase.\n * @property locale - The locale to use. Defaults to `en`.\n * @property state - The initial state of the Snap, if any. Defaults to `null`.\n */\nexport type SimulationUserOptions = Infer<typeof SimulationOptionsStruct>;\n\n/**\n * Options for the simulation, with defaults filled in.\n *\n * See {@link SimulationUserOptions} for documentation.\n */\nexport type SimulationOptions = Required<SimulationUserOptions>;\n\n/**\n * Get the options for the simulation.\n *\n * @param options - The user options. Any options not specified will be filled\n * in with default values.\n * @returns The simulation options.\n */\nexport function getOptions(options: SimulationUserOptions): SimulationOptions {\n return create(\n options,\n SimulationOptionsStruct,\n ) as Required<SimulationUserOptions>;\n}\n"]}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Infer } from "@metamask/superstruct";
|
|
2
|
+
declare const SimulationOptionsStruct: import("@metamask/superstruct").Struct<{
|
|
3
|
+
state?: Record<string, import("@metamask/utils").Json> | null | undefined;
|
|
4
|
+
currency?: string | undefined;
|
|
5
|
+
locale?: string | undefined;
|
|
6
|
+
secretRecoveryPhrase?: string | undefined;
|
|
7
|
+
unencryptedState?: Record<string, import("@metamask/utils").Json> | null | undefined;
|
|
8
|
+
}, {
|
|
9
|
+
currency: import("@metamask/superstruct").Struct<string | undefined, null>;
|
|
10
|
+
secretRecoveryPhrase: import("@metamask/superstruct").Struct<string | undefined, null>;
|
|
11
|
+
locale: import("@metamask/superstruct").Struct<string | undefined, null>;
|
|
12
|
+
state: import("@metamask/superstruct").Struct<Record<string, import("@metamask/utils").Json> | null | undefined, null>;
|
|
13
|
+
unencryptedState: import("@metamask/superstruct").Struct<Record<string, import("@metamask/utils").Json> | null | undefined, null>;
|
|
14
|
+
}>;
|
|
15
|
+
/**
|
|
16
|
+
* Options for the simulation.
|
|
17
|
+
*
|
|
18
|
+
* @property secretRecoveryPhrase - The secret recovery phrase to use. This is
|
|
19
|
+
* used to derive addresses and private keys. Defaults to a test recovery
|
|
20
|
+
* phrase.
|
|
21
|
+
* @property locale - The locale to use. Defaults to `en`.
|
|
22
|
+
* @property state - The initial state of the Snap, if any. Defaults to `null`.
|
|
23
|
+
*/
|
|
24
|
+
export type SimulationUserOptions = Infer<typeof SimulationOptionsStruct>;
|
|
25
|
+
/**
|
|
26
|
+
* Options for the simulation, with defaults filled in.
|
|
27
|
+
*
|
|
28
|
+
* See {@link SimulationUserOptions} for documentation.
|
|
29
|
+
*/
|
|
30
|
+
export type SimulationOptions = Required<SimulationUserOptions>;
|
|
31
|
+
/**
|
|
32
|
+
* Get the options for the simulation.
|
|
33
|
+
*
|
|
34
|
+
* @param options - The user options. Any options not specified will be filled
|
|
35
|
+
* in with default values.
|
|
36
|
+
* @returns The simulation options.
|
|
37
|
+
*/
|
|
38
|
+
export declare function getOptions(options: SimulationUserOptions): SimulationOptions;
|
|
39
|
+
export {};
|
|
40
|
+
//# sourceMappingURL=options.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.d.cts","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,8BAA8B;AAcnD,QAAA,MAAM,uBAAuB;;;;;;;;;;;;EAS3B,CAAC;AAEH;;;;;;;;GAQG;AACH,MAAM,MAAM,qBAAqB,GAAG,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAE1E;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG,QAAQ,CAAC,qBAAqB,CAAC,CAAC;AAEhE;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,qBAAqB,GAAG,iBAAiB,CAK5E"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Infer } from "@metamask/superstruct";
|
|
2
|
+
declare const SimulationOptionsStruct: import("@metamask/superstruct").Struct<{
|
|
3
|
+
state?: Record<string, import("@metamask/utils").Json> | null | undefined;
|
|
4
|
+
currency?: string | undefined;
|
|
5
|
+
locale?: string | undefined;
|
|
6
|
+
secretRecoveryPhrase?: string | undefined;
|
|
7
|
+
unencryptedState?: Record<string, import("@metamask/utils").Json> | null | undefined;
|
|
8
|
+
}, {
|
|
9
|
+
currency: import("@metamask/superstruct").Struct<string | undefined, null>;
|
|
10
|
+
secretRecoveryPhrase: import("@metamask/superstruct").Struct<string | undefined, null>;
|
|
11
|
+
locale: import("@metamask/superstruct").Struct<string | undefined, null>;
|
|
12
|
+
state: import("@metamask/superstruct").Struct<Record<string, import("@metamask/utils").Json> | null | undefined, null>;
|
|
13
|
+
unencryptedState: import("@metamask/superstruct").Struct<Record<string, import("@metamask/utils").Json> | null | undefined, null>;
|
|
14
|
+
}>;
|
|
15
|
+
/**
|
|
16
|
+
* Options for the simulation.
|
|
17
|
+
*
|
|
18
|
+
* @property secretRecoveryPhrase - The secret recovery phrase to use. This is
|
|
19
|
+
* used to derive addresses and private keys. Defaults to a test recovery
|
|
20
|
+
* phrase.
|
|
21
|
+
* @property locale - The locale to use. Defaults to `en`.
|
|
22
|
+
* @property state - The initial state of the Snap, if any. Defaults to `null`.
|
|
23
|
+
*/
|
|
24
|
+
export type SimulationUserOptions = Infer<typeof SimulationOptionsStruct>;
|
|
25
|
+
/**
|
|
26
|
+
* Options for the simulation, with defaults filled in.
|
|
27
|
+
*
|
|
28
|
+
* See {@link SimulationUserOptions} for documentation.
|
|
29
|
+
*/
|
|
30
|
+
export type SimulationOptions = Required<SimulationUserOptions>;
|
|
31
|
+
/**
|
|
32
|
+
* Get the options for the simulation.
|
|
33
|
+
*
|
|
34
|
+
* @param options - The user options. Any options not specified will be filled
|
|
35
|
+
* in with default values.
|
|
36
|
+
* @returns The simulation options.
|
|
37
|
+
*/
|
|
38
|
+
export declare function getOptions(options: SimulationUserOptions): SimulationOptions;
|
|
39
|
+
export {};
|
|
40
|
+
//# sourceMappingURL=options.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.d.mts","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,8BAA8B;AAcnD,QAAA,MAAM,uBAAuB;;;;;;;;;;;;EAS3B,CAAC;AAEH;;;;;;;;GAQG;AACH,MAAM,MAAM,qBAAqB,GAAG,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAE1E;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG,QAAQ,CAAC,qBAAqB,CAAC,CAAC;AAEhE;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,qBAAqB,GAAG,iBAAiB,CAK5E"}
|
package/dist/options.mjs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { create, defaulted, nullable, object, optional, record, string } from "@metamask/superstruct";
|
|
2
|
+
import { JsonStruct } from "@metamask/utils";
|
|
3
|
+
import { DEFAULT_CURRENCY, DEFAULT_LOCALE, DEFAULT_SRP } from "./constants.mjs";
|
|
4
|
+
const SimulationOptionsStruct = object({
|
|
5
|
+
currency: defaulted(optional(string()), DEFAULT_CURRENCY),
|
|
6
|
+
secretRecoveryPhrase: defaulted(optional(string()), DEFAULT_SRP),
|
|
7
|
+
locale: defaulted(optional(string()), DEFAULT_LOCALE),
|
|
8
|
+
state: defaulted(optional(nullable(record(string(), JsonStruct))), null),
|
|
9
|
+
unencryptedState: defaulted(optional(nullable(record(string(), JsonStruct))), null),
|
|
10
|
+
});
|
|
11
|
+
/**
|
|
12
|
+
* Get the options for the simulation.
|
|
13
|
+
*
|
|
14
|
+
* @param options - The user options. Any options not specified will be filled
|
|
15
|
+
* in with default values.
|
|
16
|
+
* @returns The simulation options.
|
|
17
|
+
*/
|
|
18
|
+
export function getOptions(options) {
|
|
19
|
+
return create(options, SimulationOptionsStruct);
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=options.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.mjs","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AACA,OAAO,EACL,MAAM,EACN,SAAS,EACT,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,MAAM,EACN,MAAM,EACP,8BAA8B;AAC/B,OAAO,EAAE,UAAU,EAAE,wBAAwB;AAE7C,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,WAAW,EAAE,wBAAoB;AAE5E,MAAM,uBAAuB,GAAG,MAAM,CAAC;IACrC,QAAQ,EAAE,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,gBAAgB,CAAC;IACzD,oBAAoB,EAAE,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,WAAW,CAAC;IAChE,MAAM,EAAE,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,cAAc,CAAC;IACrD,KAAK,EAAE,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC;IACxE,gBAAgB,EAAE,SAAS,CACzB,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,UAAU,CAAC,CAAC,CAAC,EAChD,IAAI,CACL;CACF,CAAC,CAAC;AAoBH;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CAAC,OAA8B;IACvD,OAAO,MAAM,CACX,OAAO,EACP,uBAAuB,CACW,CAAC;AACvC,CAAC","sourcesContent":["import type { Infer } from '@metamask/superstruct';\nimport {\n create,\n defaulted,\n nullable,\n object,\n optional,\n record,\n string,\n} from '@metamask/superstruct';\nimport { JsonStruct } from '@metamask/utils';\n\nimport { DEFAULT_CURRENCY, DEFAULT_LOCALE, DEFAULT_SRP } from './constants';\n\nconst SimulationOptionsStruct = object({\n currency: defaulted(optional(string()), DEFAULT_CURRENCY),\n secretRecoveryPhrase: defaulted(optional(string()), DEFAULT_SRP),\n locale: defaulted(optional(string()), DEFAULT_LOCALE),\n state: defaulted(optional(nullable(record(string(), JsonStruct))), null),\n unencryptedState: defaulted(\n optional(nullable(record(string(), JsonStruct))),\n null,\n ),\n});\n\n/**\n * Options for the simulation.\n *\n * @property secretRecoveryPhrase - The secret recovery phrase to use. This is\n * used to derive addresses and private keys. Defaults to a test recovery\n * phrase.\n * @property locale - The locale to use. Defaults to `en`.\n * @property state - The initial state of the Snap, if any. Defaults to `null`.\n */\nexport type SimulationUserOptions = Infer<typeof SimulationOptionsStruct>;\n\n/**\n * Options for the simulation, with defaults filled in.\n *\n * See {@link SimulationUserOptions} for documentation.\n */\nexport type SimulationOptions = Required<SimulationUserOptions>;\n\n/**\n * Get the options for the simulation.\n *\n * @param options - The user options. Any options not specified will be filled\n * in with default values.\n * @returns The simulation options.\n */\nexport function getOptions(options: SimulationUserOptions): SimulationOptions {\n return create(\n options,\n SimulationOptionsStruct,\n ) as Required<SimulationUserOptions>;\n}\n"]}
|
package/dist/request.cjs
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getInterfaceApi = exports.getInterfaceFromResult = exports.handleRequest = void 0;
|
|
4
|
+
const snaps_sdk_1 = require("@metamask/snaps-sdk");
|
|
5
|
+
const snaps_utils_1 = require("@metamask/snaps-utils");
|
|
6
|
+
const superstruct_1 = require("@metamask/superstruct");
|
|
7
|
+
const utils_1 = require("@metamask/utils");
|
|
8
|
+
const toolkit_1 = require("@reduxjs/toolkit");
|
|
9
|
+
const interface_1 = require("./interface.cjs");
|
|
10
|
+
const store_1 = require("./store/index.cjs");
|
|
11
|
+
const structs_1 = require("./structs.cjs");
|
|
12
|
+
/**
|
|
13
|
+
* Send a JSON-RPC request to the Snap, and wrap the response in a
|
|
14
|
+
* {@link SnapResponse} object.
|
|
15
|
+
*
|
|
16
|
+
* @param options - The request options.
|
|
17
|
+
* @param options.snapId - The ID of the Snap to send the request to.
|
|
18
|
+
* @param options.store - The Redux store.
|
|
19
|
+
* @param options.executionService - The execution service to use to send the
|
|
20
|
+
* request.
|
|
21
|
+
* @param options.handler - The handler to use to send the request.
|
|
22
|
+
* @param options.controllerMessenger - The controller messenger used to call actions.
|
|
23
|
+
* @param options.runSaga - A function to run a saga outside the usual Redux
|
|
24
|
+
* flow.
|
|
25
|
+
* @param options.request - The request to send.
|
|
26
|
+
* @param options.request.id - The ID of the request. If not provided, a random
|
|
27
|
+
* ID will be generated.
|
|
28
|
+
* @param options.request.origin - The origin of the request. Defaults to
|
|
29
|
+
* `https://metamask.io`.
|
|
30
|
+
* @returns The response, wrapped in a {@link SnapResponse} object.
|
|
31
|
+
*/
|
|
32
|
+
function handleRequest({ snapId, store, executionService, handler, controllerMessenger, runSaga, request: { id = (0, toolkit_1.nanoid)(), origin = 'https://metamask.io', ...options }, }) {
|
|
33
|
+
const getInterfaceError = () => {
|
|
34
|
+
throw new Error('Unable to get the interface from the Snap: The request to the Snap failed.');
|
|
35
|
+
};
|
|
36
|
+
const promise = executionService
|
|
37
|
+
.handleRpcRequest(snapId, {
|
|
38
|
+
origin,
|
|
39
|
+
handler,
|
|
40
|
+
request: {
|
|
41
|
+
jsonrpc: '2.0',
|
|
42
|
+
id: 1,
|
|
43
|
+
...options,
|
|
44
|
+
},
|
|
45
|
+
})
|
|
46
|
+
.then(async (result) => {
|
|
47
|
+
const notifications = (0, store_1.getNotifications)(store.getState());
|
|
48
|
+
store.dispatch((0, store_1.clearNotifications)());
|
|
49
|
+
try {
|
|
50
|
+
const getInterfaceFn = await getInterfaceApi(result, snapId, controllerMessenger);
|
|
51
|
+
return {
|
|
52
|
+
id: String(id),
|
|
53
|
+
response: {
|
|
54
|
+
result: (0, utils_1.getSafeJson)(result),
|
|
55
|
+
},
|
|
56
|
+
notifications,
|
|
57
|
+
...(getInterfaceFn ? { getInterface: getInterfaceFn } : {}),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
const [unwrappedError] = (0, snaps_utils_1.unwrapError)(error);
|
|
62
|
+
return {
|
|
63
|
+
id: String(id),
|
|
64
|
+
response: {
|
|
65
|
+
error: unwrappedError.serialize(),
|
|
66
|
+
},
|
|
67
|
+
notifications: [],
|
|
68
|
+
getInterface: getInterfaceError,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
.catch((error) => {
|
|
73
|
+
const [unwrappedError] = (0, snaps_utils_1.unwrapError)(error);
|
|
74
|
+
return {
|
|
75
|
+
id: String(id),
|
|
76
|
+
response: {
|
|
77
|
+
error: unwrappedError.serialize(),
|
|
78
|
+
},
|
|
79
|
+
notifications: [],
|
|
80
|
+
getInterface: getInterfaceError,
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
promise.getInterface = async () => {
|
|
84
|
+
const sagaPromise = runSaga(interface_1.getInterface, runSaga, snapId, controllerMessenger).toPromise();
|
|
85
|
+
const result = await Promise.race([promise, sagaPromise]);
|
|
86
|
+
// If the request promise has resolved to an error, we should throw
|
|
87
|
+
// instead of waiting for an interface that likely will never be displayed
|
|
88
|
+
if ((0, superstruct_1.is)(result, structs_1.SnapResponseStruct) &&
|
|
89
|
+
(0, utils_1.hasProperty)(result.response, 'error')) {
|
|
90
|
+
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}`);
|
|
91
|
+
}
|
|
92
|
+
return await sagaPromise;
|
|
93
|
+
};
|
|
94
|
+
return promise;
|
|
95
|
+
}
|
|
96
|
+
exports.handleRequest = handleRequest;
|
|
97
|
+
/**
|
|
98
|
+
* Get the interface ID from the result if it's available or create a new interface if the result contains static components.
|
|
99
|
+
*
|
|
100
|
+
* @param result - The handler result object.
|
|
101
|
+
* @param snapId - The Snap ID.
|
|
102
|
+
* @param controllerMessenger - The controller messenger.
|
|
103
|
+
* @returns The interface ID or undefined if the result doesn't include content.
|
|
104
|
+
*/
|
|
105
|
+
async function getInterfaceFromResult(result, snapId, controllerMessenger) {
|
|
106
|
+
if ((0, utils_1.isPlainObject)(result) && (0, utils_1.hasProperty)(result, 'id')) {
|
|
107
|
+
return result.id;
|
|
108
|
+
}
|
|
109
|
+
if ((0, utils_1.isPlainObject)(result) && (0, utils_1.hasProperty)(result, 'content')) {
|
|
110
|
+
(0, utils_1.assert)((0, superstruct_1.is)(result.content, snaps_sdk_1.ComponentOrElementStruct), 'The Snap returned an invalid interface.');
|
|
111
|
+
const id = await controllerMessenger.call('SnapInterfaceController:createInterface', snapId, result.content);
|
|
112
|
+
return id;
|
|
113
|
+
}
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
exports.getInterfaceFromResult = getInterfaceFromResult;
|
|
117
|
+
/**
|
|
118
|
+
* Get the response content from the `SnapInterfaceController` and include the
|
|
119
|
+
* interaction methods.
|
|
120
|
+
*
|
|
121
|
+
* @param result - The handler result object.
|
|
122
|
+
* @param snapId - The Snap ID.
|
|
123
|
+
* @param controllerMessenger - The controller messenger.
|
|
124
|
+
* @returns The content components if any.
|
|
125
|
+
*/
|
|
126
|
+
async function getInterfaceApi(result, snapId, controllerMessenger) {
|
|
127
|
+
const interfaceId = await getInterfaceFromResult(result, snapId, controllerMessenger);
|
|
128
|
+
if (interfaceId) {
|
|
129
|
+
return () => {
|
|
130
|
+
const { content } = controllerMessenger.call('SnapInterfaceController:getInterface', snapId, interfaceId);
|
|
131
|
+
const actions = (0, interface_1.getInterfaceActions)(snapId, controllerMessenger, {
|
|
132
|
+
id: interfaceId,
|
|
133
|
+
content,
|
|
134
|
+
});
|
|
135
|
+
return {
|
|
136
|
+
content,
|
|
137
|
+
...actions,
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
exports.getInterfaceApi = getInterfaceApi;
|
|
144
|
+
//# sourceMappingURL=request.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.cjs","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"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","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"]}
|