@metamask/snaps-jest 6.0.1 → 7.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 +18 -1
- package/README.md +100 -7
- package/dist/{chunk-JUNUGLG4.js → chunk-2JAGD4N6.js} +3 -3
- package/dist/{chunk-7YD4IW43.mjs → chunk-3M4GRUMH.mjs} +3 -6
- package/dist/chunk-3M4GRUMH.mjs.map +1 -0
- package/dist/{chunk-7BFTEFLS.mjs → chunk-5U5WB3SM.mjs} +5 -3
- package/dist/chunk-5U5WB3SM.mjs.map +1 -0
- package/dist/chunk-6POKT4OO.js +127 -0
- package/dist/chunk-6POKT4OO.js.map +1 -0
- package/dist/{chunk-7VILST6M.mjs → chunk-BVGI3E45.mjs} +2 -2
- package/dist/chunk-CKRORVDW.js +1 -0
- package/dist/{chunk-WFBQPYSV.js → chunk-DTDYOBCI.js} +10 -6
- package/dist/chunk-DTDYOBCI.js.map +1 -0
- package/dist/{chunk-WIFOID2P.mjs → chunk-EDEJD326.mjs} +20 -10
- package/dist/chunk-EDEJD326.mjs.map +1 -0
- package/dist/{chunk-B4QZXXQY.js → chunk-FIZAYEHV.js} +4 -4
- package/dist/chunk-I5P52IMF.mjs +223 -0
- package/dist/chunk-I5P52IMF.mjs.map +1 -0
- package/dist/{chunk-T2BE4O5H.js → chunk-IDGD7TZ7.js} +33 -23
- package/dist/chunk-IDGD7TZ7.js.map +1 -0
- package/dist/{chunk-CCYBLBVL.mjs → chunk-LG3VKXGH.mjs} +9 -5
- package/dist/chunk-LG3VKXGH.mjs.map +1 -0
- package/dist/{chunk-SQJOQ7AF.js → chunk-MCYTJUDG.js} +13 -16
- package/dist/chunk-MCYTJUDG.js.map +1 -0
- package/dist/chunk-PVMIH6OB.mjs +127 -0
- package/dist/chunk-PVMIH6OB.mjs.map +1 -0
- package/dist/chunk-S2HLITUN.js +1 -0
- package/dist/{chunk-ISWZ7XZ5.js → chunk-SB5EPHE3.js} +5 -3
- package/dist/chunk-SB5EPHE3.js.map +1 -0
- package/dist/{chunk-HBLJOACO.mjs → chunk-TAIJXTLU.mjs} +2 -10
- package/dist/chunk-TAIJXTLU.mjs.map +1 -0
- package/dist/chunk-TGZ7WOTJ.mjs +1 -0
- package/dist/chunk-UDOXICJK.mjs +1 -0
- package/dist/{chunk-LHU3EHLI.mjs → chunk-VSUNWJMQ.mjs} +11 -14
- package/dist/chunk-VSUNWJMQ.mjs.map +1 -0
- package/dist/{chunk-TO46ICOZ.js → chunk-WMLSD6B5.js} +3 -6
- package/dist/chunk-WMLSD6B5.js.map +1 -0
- package/dist/{chunk-4SC3HVI5.mjs → chunk-X5IPMTHO.mjs} +2 -2
- package/dist/chunk-Y4DKEF7Y.js +223 -0
- package/dist/chunk-Y4DKEF7Y.js.map +1 -0
- package/dist/{chunk-7J34R6YW.mjs → chunk-YEVKBYKO.mjs} +35 -25
- package/dist/chunk-YEVKBYKO.mjs.map +1 -0
- package/dist/{chunk-5PLDD7ZC.js → chunk-Z35PCKFD.js} +24 -14
- package/dist/chunk-Z35PCKFD.js.map +1 -0
- package/dist/{chunk-LXSNRZV4.js → chunk-ZWN4SO2V.js} +5 -13
- package/dist/chunk-ZWN4SO2V.js.map +1 -0
- package/dist/environment.js +13 -14
- package/dist/environment.mjs +12 -13
- package/dist/global.js +2 -0
- package/dist/global.js.map +1 -0
- package/dist/global.mjs +2 -0
- package/dist/global.mjs.map +1 -0
- package/dist/helpers.js +13 -14
- package/dist/helpers.mjs +12 -13
- package/dist/index.js +16 -16
- package/dist/index.mjs +15 -15
- package/dist/internals/index.js +27 -14
- package/dist/internals/index.mjs +30 -17
- package/dist/internals/request.js +12 -11
- package/dist/internals/request.mjs +13 -12
- package/dist/internals/simulation/controllers.js +6 -7
- package/dist/internals/simulation/controllers.mjs +5 -6
- package/dist/internals/simulation/index.js +17 -10
- package/dist/internals/simulation/index.mjs +17 -10
- package/dist/internals/simulation/interface.js +11 -3
- package/dist/internals/simulation/interface.mjs +12 -4
- package/dist/internals/simulation/methods/hooks/index.js +4 -10
- package/dist/internals/simulation/methods/hooks/index.mjs +3 -9
- package/dist/internals/simulation/methods/hooks/notifications.js +1 -1
- package/dist/internals/simulation/methods/hooks/notifications.mjs +1 -1
- package/dist/internals/simulation/methods/hooks/show-dialog.js +1 -1
- package/dist/internals/simulation/methods/hooks/show-dialog.mjs +1 -1
- package/dist/internals/simulation/methods/hooks/state.js +3 -3
- package/dist/internals/simulation/methods/hooks/state.mjs +2 -2
- package/dist/internals/simulation/methods/index.js +5 -6
- package/dist/internals/simulation/methods/index.mjs +4 -5
- package/dist/internals/simulation/methods/specifications.js +5 -6
- package/dist/internals/simulation/methods/specifications.mjs +4 -5
- package/dist/internals/simulation/simulation.js +8 -9
- package/dist/internals/simulation/simulation.mjs +7 -8
- package/dist/internals/simulation/store/index.js +2 -2
- package/dist/internals/simulation/store/index.mjs +1 -1
- package/dist/internals/simulation/store/store.js +2 -2
- package/dist/internals/simulation/store/store.mjs +1 -1
- package/dist/internals/structs.js +6 -2
- package/dist/internals/structs.mjs +5 -1
- package/dist/matchers.js +13 -14
- package/dist/matchers.mjs +12 -13
- package/dist/setup.js +12 -13
- package/dist/setup.mjs +12 -13
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/global.d.ts +69 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/internals/request.d.ts +13 -4
- package/dist/types/internals/simulation/interface.d.ts +47 -4
- package/dist/types/internals/simulation/methods/hooks/index.d.ts +0 -1
- package/dist/types/internals/simulation/methods/hooks/state.d.ts +2 -1
- package/dist/types/internals/simulation/store/store.d.ts +1 -2
- package/dist/types/internals/structs.d.ts +87 -156
- package/dist/types/types.d.ts +42 -21
- package/package.json +11 -11
- package/dist/chunk-2X23DUDP.mjs +0 -22
- package/dist/chunk-2X23DUDP.mjs.map +0 -1
- package/dist/chunk-5PLDD7ZC.js.map +0 -1
- package/dist/chunk-6MVV44M5.mjs +0 -76
- package/dist/chunk-6MVV44M5.mjs.map +0 -1
- package/dist/chunk-7BFTEFLS.mjs.map +0 -1
- package/dist/chunk-7J34R6YW.mjs.map +0 -1
- package/dist/chunk-7YD4IW43.mjs.map +0 -1
- package/dist/chunk-CCYBLBVL.mjs.map +0 -1
- package/dist/chunk-HBLJOACO.mjs.map +0 -1
- package/dist/chunk-HNH5UAKZ.js +0 -80
- package/dist/chunk-HNH5UAKZ.js.map +0 -1
- package/dist/chunk-ISWZ7XZ5.js.map +0 -1
- package/dist/chunk-LHU3EHLI.mjs.map +0 -1
- package/dist/chunk-LXSNRZV4.js.map +0 -1
- package/dist/chunk-LYK6EGZU.mjs +0 -80
- package/dist/chunk-LYK6EGZU.mjs.map +0 -1
- package/dist/chunk-RD7GQCPL.js +0 -22
- package/dist/chunk-RD7GQCPL.js.map +0 -1
- package/dist/chunk-SQJOQ7AF.js.map +0 -1
- package/dist/chunk-T2BE4O5H.js.map +0 -1
- package/dist/chunk-TO46ICOZ.js.map +0 -1
- package/dist/chunk-U5BLLHIU.js +0 -1
- package/dist/chunk-V6LFAN3U.mjs +0 -1
- package/dist/chunk-WFBQPYSV.js.map +0 -1
- package/dist/chunk-WIFOID2P.mjs.map +0 -1
- package/dist/chunk-ZAQZRYPW.js +0 -76
- package/dist/chunk-ZAQZRYPW.js.map +0 -1
- package/dist/internals/simulation/methods/hooks/encryption.js +0 -10
- package/dist/internals/simulation/methods/hooks/encryption.mjs +0 -10
- package/dist/types/internals/simulation/methods/hooks/encryption.d.ts +0 -29
- /package/dist/{chunk-JUNUGLG4.js.map → chunk-2JAGD4N6.js.map} +0 -0
- /package/dist/{chunk-7VILST6M.mjs.map → chunk-BVGI3E45.mjs.map} +0 -0
- /package/dist/{chunk-U5BLLHIU.js.map → chunk-CKRORVDW.js.map} +0 -0
- /package/dist/{chunk-B4QZXXQY.js.map → chunk-FIZAYEHV.js.map} +0 -0
- /package/dist/{internals/simulation/methods/hooks/encryption.js.map → chunk-S2HLITUN.js.map} +0 -0
- /package/dist/{chunk-V6LFAN3U.mjs.map → chunk-TGZ7WOTJ.mjs.map} +0 -0
- /package/dist/{internals/simulation/methods/hooks/encryption.mjs.map → chunk-UDOXICJK.mjs.map} +0 -0
- /package/dist/{chunk-4SC3HVI5.mjs.map → chunk-X5IPMTHO.mjs.map} +0 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import {
|
|
2
|
+
clickElement,
|
|
3
|
+
getInterface,
|
|
4
|
+
typeInField
|
|
5
|
+
} from "./chunk-I5P52IMF.mjs";
|
|
6
|
+
import {
|
|
7
|
+
clearNotifications,
|
|
8
|
+
getNotifications
|
|
9
|
+
} from "./chunk-LB4R3BUA.mjs";
|
|
10
|
+
|
|
11
|
+
// src/internals/request.ts
|
|
12
|
+
import { unwrapError } from "@metamask/snaps-utils";
|
|
13
|
+
import { getSafeJson, hasProperty, isPlainObject } from "@metamask/utils";
|
|
14
|
+
import { nanoid } from "@reduxjs/toolkit";
|
|
15
|
+
function handleRequest({
|
|
16
|
+
snapId,
|
|
17
|
+
store,
|
|
18
|
+
executionService,
|
|
19
|
+
handler,
|
|
20
|
+
controllerMessenger,
|
|
21
|
+
runSaga,
|
|
22
|
+
request: { id = nanoid(), origin = "https://metamask.io", ...options }
|
|
23
|
+
}) {
|
|
24
|
+
const promise = executionService.handleRpcRequest(snapId, {
|
|
25
|
+
origin,
|
|
26
|
+
handler,
|
|
27
|
+
request: {
|
|
28
|
+
jsonrpc: "2.0",
|
|
29
|
+
id: 1,
|
|
30
|
+
...options
|
|
31
|
+
}
|
|
32
|
+
}).then(async (result) => {
|
|
33
|
+
const notifications = getNotifications(store.getState());
|
|
34
|
+
store.dispatch(clearNotifications());
|
|
35
|
+
const getInterfaceFn = await getInterfaceApi(
|
|
36
|
+
result,
|
|
37
|
+
snapId,
|
|
38
|
+
controllerMessenger
|
|
39
|
+
);
|
|
40
|
+
return {
|
|
41
|
+
id: String(id),
|
|
42
|
+
response: {
|
|
43
|
+
result: getSafeJson(result)
|
|
44
|
+
},
|
|
45
|
+
notifications,
|
|
46
|
+
...getInterfaceFn ? { getInterface: getInterfaceFn } : {}
|
|
47
|
+
};
|
|
48
|
+
}).catch((error) => {
|
|
49
|
+
const [unwrappedError] = unwrapError(error);
|
|
50
|
+
return {
|
|
51
|
+
id: String(id),
|
|
52
|
+
response: {
|
|
53
|
+
error: unwrappedError.serialize()
|
|
54
|
+
},
|
|
55
|
+
notifications: []
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
promise.getInterface = async () => {
|
|
59
|
+
return await runSaga(
|
|
60
|
+
getInterface,
|
|
61
|
+
runSaga,
|
|
62
|
+
snapId,
|
|
63
|
+
controllerMessenger
|
|
64
|
+
).toPromise();
|
|
65
|
+
};
|
|
66
|
+
return promise;
|
|
67
|
+
}
|
|
68
|
+
async function getInterfaceFromResult(result, snapId, controllerMessenger) {
|
|
69
|
+
if (isPlainObject(result) && hasProperty(result, "id")) {
|
|
70
|
+
return result.id;
|
|
71
|
+
}
|
|
72
|
+
if (isPlainObject(result) && hasProperty(result, "content")) {
|
|
73
|
+
const id = await controllerMessenger.call(
|
|
74
|
+
"SnapInterfaceController:createInterface",
|
|
75
|
+
snapId,
|
|
76
|
+
result.content
|
|
77
|
+
);
|
|
78
|
+
return id;
|
|
79
|
+
}
|
|
80
|
+
return void 0;
|
|
81
|
+
}
|
|
82
|
+
async function getInterfaceApi(result, snapId, controllerMessenger) {
|
|
83
|
+
const interfaceId = await getInterfaceFromResult(
|
|
84
|
+
result,
|
|
85
|
+
snapId,
|
|
86
|
+
controllerMessenger
|
|
87
|
+
);
|
|
88
|
+
if (interfaceId) {
|
|
89
|
+
return () => {
|
|
90
|
+
const { content } = controllerMessenger.call(
|
|
91
|
+
"SnapInterfaceController:getInterface",
|
|
92
|
+
snapId,
|
|
93
|
+
interfaceId
|
|
94
|
+
);
|
|
95
|
+
return {
|
|
96
|
+
content,
|
|
97
|
+
clickElement: async (name) => {
|
|
98
|
+
await clickElement(
|
|
99
|
+
controllerMessenger,
|
|
100
|
+
interfaceId,
|
|
101
|
+
content,
|
|
102
|
+
snapId,
|
|
103
|
+
name
|
|
104
|
+
);
|
|
105
|
+
},
|
|
106
|
+
typeInField: async (name, value) => {
|
|
107
|
+
await typeInField(
|
|
108
|
+
controllerMessenger,
|
|
109
|
+
interfaceId,
|
|
110
|
+
content,
|
|
111
|
+
snapId,
|
|
112
|
+
name,
|
|
113
|
+
value
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
return void 0;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export {
|
|
123
|
+
handleRequest,
|
|
124
|
+
getInterfaceFromResult,
|
|
125
|
+
getInterfaceApi
|
|
126
|
+
};
|
|
127
|
+
//# sourceMappingURL=chunk-PVMIH6OB.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/internals/request.ts"],"sourcesContent":["import type { AbstractExecutionService } from '@metamask/snaps-controllers';\nimport type { SnapId, Component } from '@metamask/snaps-sdk';\nimport type { HandlerType } from '@metamask/snaps-utils';\nimport { unwrapError } from '@metamask/snaps-utils';\nimport { getSafeJson, hasProperty, isPlainObject } from '@metamask/utils';\nimport { nanoid } from '@reduxjs/toolkit';\n\nimport type {\n RequestOptions,\n SnapHandlerInterface,\n SnapRequest,\n} from '../types';\nimport {\n clearNotifications,\n clickElement,\n getInterface,\n getNotifications,\n typeInField,\n} from './simulation';\nimport type { RunSagaFunction, Store } from './simulation';\nimport type { RootControllerMessenger } from './simulation/controllers';\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 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 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 })\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 };\n }) as unknown as SnapRequest;\n\n promise.getInterface = async () => {\n return await runSaga(\n getInterface,\n runSaga,\n snapId,\n controllerMessenger,\n ).toPromise();\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 const id = await controllerMessenger.call(\n 'SnapInterfaceController:createInterface',\n snapId,\n result.content as Component,\n );\n\n return id;\n }\n\n return undefined;\n}\n\n/**\n * Get the response content from the SnapInterfaceController and include the 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 return {\n content,\n clickElement: async (name) => {\n await clickElement(\n controllerMessenger,\n interfaceId,\n content,\n snapId,\n name,\n );\n },\n typeInField: async (name, value) => {\n await typeInField(\n controllerMessenger,\n interfaceId,\n content,\n snapId,\n name,\n value,\n );\n },\n };\n };\n }\n\n return undefined;\n}\n"],"mappings":";;;;;;;;;;;AAGA,SAAS,mBAAmB;AAC5B,SAAS,aAAa,aAAa,qBAAqB;AACxD,SAAS,cAAc;AA+ChB,SAAS,cAAc;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,SAAS,EAAE,KAAK,OAAO,GAAG,SAAS,uBAAuB,GAAG,QAAQ;AACvE,GAAsC;AACpC,QAAM,UAAU,iBACb,iBAAiB,QAAQ;AAAA,IACxB;AAAA,IACA;AAAA,IACA,SAAS;AAAA,MACP,SAAS;AAAA,MACT,IAAI;AAAA,MACJ,GAAG;AAAA,IACL;AAAA,EACF,CAAC,EACA,KAAK,OAAO,WAAW;AACtB,UAAM,gBAAgB,iBAAiB,MAAM,SAAS,CAAC;AACvD,UAAM,SAAS,mBAAmB,CAAC;AAEnC,UAAM,iBAAiB,MAAM;AAAA,MAC3B;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAEA,WAAO;AAAA,MACL,IA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,gBAAM;AAAA,YACJ;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";//# sourceMappingURL=chunk-S2HLITUN.js.map
|
|
@@ -5,9 +5,11 @@
|
|
|
5
5
|
var _chunkLBC2OGSNjs = require('./chunk-LBC2OGSN.js');
|
|
6
6
|
|
|
7
7
|
// src/internals/simulation/methods/hooks/state.ts
|
|
8
|
+
var _snapsutils = require('@metamask/snaps-utils');
|
|
8
9
|
var _effects = require('redux-saga/effects');
|
|
9
10
|
function* getSnapStateImplementation(_snapId, encrypted = true) {
|
|
10
|
-
|
|
11
|
+
const state = yield _effects.select.call(void 0, _chunkLBC2OGSNjs.getState.call(void 0, encrypted));
|
|
12
|
+
return _snapsutils.parseJson.call(void 0, state);
|
|
11
13
|
}
|
|
12
14
|
function getGetSnapStateMethodImplementation(runSaga) {
|
|
13
15
|
return (...args) => {
|
|
@@ -15,7 +17,7 @@ function getGetSnapStateMethodImplementation(runSaga) {
|
|
|
15
17
|
};
|
|
16
18
|
}
|
|
17
19
|
function* updateSnapStateImplementation(_snapId, newState, encrypted = true) {
|
|
18
|
-
yield _effects.put.call(void 0, _chunkLBC2OGSNjs.setState.call(void 0, { state: newState, encrypted }));
|
|
20
|
+
yield _effects.put.call(void 0, _chunkLBC2OGSNjs.setState.call(void 0, { state: JSON.stringify(newState), encrypted }));
|
|
19
21
|
}
|
|
20
22
|
function getUpdateSnapStateMethodImplementation(runSaga) {
|
|
21
23
|
return (...args) => {
|
|
@@ -36,4 +38,4 @@ function getClearSnapStateMethodImplementation(runSaga) {
|
|
|
36
38
|
|
|
37
39
|
|
|
38
40
|
exports.getGetSnapStateMethodImplementation = getGetSnapStateMethodImplementation; exports.getUpdateSnapStateMethodImplementation = getUpdateSnapStateMethodImplementation; exports.getClearSnapStateMethodImplementation = getClearSnapStateMethodImplementation;
|
|
39
|
-
//# sourceMappingURL=chunk-
|
|
41
|
+
//# sourceMappingURL=chunk-SB5EPHE3.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/internals/simulation/methods/hooks/state.ts"],"names":[],"mappings":";;;;;;;AAAA,SAAS,iBAAiB;AAG1B,SAAS,KAAK,cAAc;AAe5B,UAAU,2BACR,SACA,YAAY,MACU;AACtB,QAAM,QAAQ,MAAM,OAAO,SAAS,SAAS,CAAC;AAE9C,SAAO,UAAU,KAAK;AACxB;AAQO,SAAS,oCAAoC,SAA0B;AAC5E,SAAO,IAAI,SAAwD;AACjE,WAAO,QAAQ,4BAA4B,GAAG,IAAI,EAAE,OAAO;AAAA,EAC7D;AACF;AAYA,UAAU,8BACR,SACA,UACA,YAAY,MACQ;AAEpB,QAAM,IAAI,SAAS,EAAE,OAAO,KAAK,UAAU,QAAQ,GAAG,UAAU,CAAC,CAAC;AACpE;AAQO,SAAS,uCACd,SACA;AACA,SAAO,IAAI,SAA2D;AACpE,YAAQ,+BAA+B,GAAG,IAAI,EAAE,OAAO;AAAA,EACzD;AACF;AAWA,UAAU,6BACR,SACA,YAAY,MACQ;AACpB,QAAM,IAAI,WAAW,EAAE,UAAU,CAAC,CAAC;AACrC;AAQO,SAAS,sCACd,SACA;AACA,SAAO,UAAU,SAA0D;AACzE,YAAQ,8BAA8B,GAAG,IAAI,EAAE,OAAO;AAAA,EACxD;AACF","sourcesContent":["import { parseJson } from '@metamask/snaps-utils';\nimport type { Json } from '@metamask/utils';\nimport type { SagaIterator } from 'redux-saga';\nimport { put, select } from 'redux-saga/effects';\n\nimport type { RunSagaFunction } from '../../store';\nimport { clearState, getState, setState } from '../../store';\n\n/**\n * Get the Snap state from the store.\n *\n * @param _snapId - The ID of the Snap to get the state for. This is ignored\n * because the simulator only supports one Snap.\n * @param encrypted - Whether to get the encrypted or unencrypted state.\n * Defaults to encrypted state.\n * @returns The state of the Snap.\n * @yields Selects the state from the store.\n */\nfunction* getSnapStateImplementation(\n _snapId: string,\n encrypted = true,\n): SagaIterator<string> {\n const state = yield select(getState(encrypted));\n // TODO: Use actual decryption implementation\n return parseJson(state);\n}\n\n/**\n * Get the implementation of the `getSnapState` hook.\n *\n * @param runSaga - The function to run a saga outside the usual Redux flow.\n * @returns The implementation of the `getSnapState` hook.\n */\nexport function getGetSnapStateMethodImplementation(runSaga: RunSagaFunction) {\n return (...args: Parameters<typeof getSnapStateImplementation>) => {\n return runSaga(getSnapStateImplementation, ...args).result();\n };\n}\n\n/**\n * Update the Snap state in the store.\n *\n * @param _snapId - The ID of the Snap to update the state for. This is ignored\n * because the simulator only supports one Snap.\n * @param newState - The new state.\n * @param encrypted - Whether to update the encrypted or unencrypted state.\n * Defaults to encrypted state.\n * @yields Puts the new state in the store.\n */\nfunction* updateSnapStateImplementation(\n _snapId: string,\n newState: Record<string, Json>,\n encrypted = true,\n): SagaIterator<void> {\n // TODO: Use actual encryption implementation\n yield put(setState({ state: JSON.stringify(newState), encrypted }));\n}\n\n/**\n * Get the implementation of the `updateSnapState` hook.\n *\n * @param runSaga - The function to run a saga outside the usual Redux flow.\n * @returns The implementation of the `updateSnapState` hook.\n */\nexport function getUpdateSnapStateMethodImplementation(\n runSaga: RunSagaFunction,\n) {\n return (...args: Parameters<typeof updateSnapStateImplementation>) => {\n runSaga(updateSnapStateImplementation, ...args).result();\n };\n}\n\n/**\n * Clear the Snap state in the store.\n *\n * @param _snapId - The ID of the Snap to clear the state for. This is ignored\n * because the simulator only supports one Snap.\n * @param encrypted - Whether to clear the encrypted or unencrypted state.\n * Defaults to encrypted state.\n * @yields Puts the new state in the store.\n */\nfunction* clearSnapStateImplementation(\n _snapId: string,\n encrypted = true,\n): SagaIterator<void> {\n yield put(clearState({ encrypted }));\n}\n\n/**\n * Get the implementation of the `clearSnapState` hook.\n *\n * @param runSaga - The function to run a saga outside the usual Redux flow.\n * @returns The implementation of the `clearSnapState` hook.\n */\nexport function getClearSnapStateMethodImplementation(\n runSaga: RunSagaFunction,\n) {\n return async (...args: Parameters<typeof clearSnapStateImplementation>) => {\n runSaga(clearSnapStateImplementation, ...args).result();\n };\n}\n"]}
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
decryptImplementation,
|
|
3
|
-
encryptImplementation
|
|
4
|
-
} from "./chunk-2X23DUDP.mjs";
|
|
5
1
|
import {
|
|
6
2
|
getGetLocaleMethodImplementation
|
|
7
3
|
} from "./chunk-KSIBNOB2.mjs";
|
|
@@ -20,7 +16,7 @@ import {
|
|
|
20
16
|
getClearSnapStateMethodImplementation,
|
|
21
17
|
getGetSnapStateMethodImplementation,
|
|
22
18
|
getUpdateSnapStateMethodImplementation
|
|
23
|
-
} from "./chunk-
|
|
19
|
+
} from "./chunk-5U5WB3SM.mjs";
|
|
24
20
|
import {
|
|
25
21
|
EXCLUDED_SNAP_ENDOWMENTS,
|
|
26
22
|
EXCLUDED_SNAP_PERMISSIONS
|
|
@@ -50,10 +46,6 @@ function getPermissionSpecifications({
|
|
|
50
46
|
...buildSnapRestrictedMethodSpecifications(EXCLUDED_SNAP_PERMISSIONS, {
|
|
51
47
|
// Shared hooks.
|
|
52
48
|
...hooks,
|
|
53
|
-
// Encryption and decryption hooks.
|
|
54
|
-
// TODO: Swap these out for the real implementations.
|
|
55
|
-
encrypt: encryptImplementation,
|
|
56
|
-
decrypt: decryptImplementation,
|
|
57
49
|
// Snaps-specific hooks.
|
|
58
50
|
clearSnapState: getClearSnapStateMethodImplementation(runSaga),
|
|
59
51
|
getLocale: getGetLocaleMethodImplementation(options),
|
|
@@ -94,4 +86,4 @@ export {
|
|
|
94
86
|
getPermissionSpecifications,
|
|
95
87
|
getEndowments
|
|
96
88
|
};
|
|
97
|
-
//# sourceMappingURL=chunk-
|
|
89
|
+
//# sourceMappingURL=chunk-TAIJXTLU.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/internals/simulation/methods/specifications.ts"],"sourcesContent":["import type { GenericPermissionController } from '@metamask/permission-controller';\nimport {\n endowmentPermissionBuilders,\n buildSnapEndowmentSpecifications,\n buildSnapRestrictedMethodSpecifications,\n} from '@metamask/snaps-rpc-methods';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport { DEFAULT_ENDOWMENTS } from '@metamask/snaps-utils';\n\nimport type { RootControllerMessenger } from '../controllers';\nimport type { SimulationOptions } from '../options';\nimport type { RunSagaFunction } from '../store';\nimport {\n EXCLUDED_SNAP_ENDOWMENTS,\n EXCLUDED_SNAP_PERMISSIONS,\n} from './constants';\nimport {\n getGetLocaleMethodImplementation,\n getClearSnapStateMethodImplementation,\n getGetSnapStateMethodImplementation,\n getUpdateSnapStateMethodImplementation,\n getShowDialogImplementation,\n getShowInAppNotificationImplementation,\n getShowNativeNotificationImplementation,\n getCreateInterfaceImplementation,\n getGetInterfaceImplementation,\n} from './hooks';\n\nexport type PermissionSpecificationsHooks = {\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\nexport type GetPermissionSpecificationsOptions = {\n controllerMessenger: RootControllerMessenger;\n hooks: PermissionSpecificationsHooks;\n runSaga: RunSagaFunction;\n options: SimulationOptions;\n};\n\n/**\n * Get a function which resolves with the specified result.\n *\n * @param result - The result to return.\n * @returns The function implementation.\n */\nexport function resolve(result: unknown) {\n return () => result;\n}\n\n/**\n * Get a function which resolves with the specified result.\n *\n * @param result - The result to return. If not specified, the function will\n * resolve with `undefined`.\n * @returns The function implementation.\n */\nexport function asyncResolve(result?: unknown) {\n return async () => result;\n}\n\n/**\n * Get the permission specifications for the Snap.\n *\n * @param options - The options.\n * @param options.controllerMessenger - The controller messenger.\n * @param options.hooks - The hooks.\n * @param options.runSaga - The function to run a saga outside the usual Redux\n * flow.\n * @param options.options - The simulation options.\n * @returns The permission specifications for the Snap.\n */\nexport function getPermissionSpecifications({\n controllerMessenger,\n hooks,\n runSaga,\n options,\n}: GetPermissionSpecificationsOptions) {\n return {\n ...buildSnapEndowmentSpecifications(EXCLUDED_SNAP_ENDOWMENTS),\n ...buildSnapRestrictedMethodSpecifications(EXCLUDED_SNAP_PERMISSIONS, {\n // Shared hooks.\n ...hooks,\n\n // Snaps-specific hooks.\n clearSnapState: getClearSnapStateMethodImplementation(runSaga),\n getLocale: getGetLocaleMethodImplementation(options),\n getSnapState: getGetSnapStateMethodImplementation(runSaga),\n getUnlockPromise: asyncResolve(true),\n\n // TODO: Allow the user to specify the result of this function.\n isOnPhishingList: resolve(false),\n\n maybeUpdatePhishingList: asyncResolve(),\n showDialog: getShowDialogImplementation(runSaga),\n showInAppNotification: getShowInAppNotificationImplementation(runSaga),\n showNativeNotification: getShowNativeNotificationImplementation(runSaga),\n updateSnapState: getUpdateSnapStateMethodImplementation(runSaga),\n createInterface: getCreateInterfaceImplementation(controllerMessenger),\n getInterface: getGetInterfaceImplementation(controllerMessenger),\n }),\n };\n}\n\n/**\n * Get the endowments for the Snap.\n *\n * @param permissionController - The permission controller.\n * @param snapId - The ID of the Snap.\n * @returns The endowments for the Snap.\n */\nexport async function getEndowments(\n permissionController: GenericPermissionController,\n snapId: SnapId,\n) {\n const allEndowments = await Object.keys(endowmentPermissionBuilders).reduce<\n Promise<string[]>\n >(async (promise, permissionName) => {\n const accumulator = await promise;\n if (permissionController.hasPermission(snapId, permissionName)) {\n const endowments = await permissionController.getEndowments(\n snapId,\n permissionName,\n );\n\n if (endowments) {\n return accumulator.concat(endowments as string[]);\n }\n }\n\n return accumulator;\n }, Promise.resolve([]));\n\n return [...new Set([...DEFAULT_ENDOWMENTS, ...allEndowments])];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AACA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,0BAA0B;AA2C5B,SAAS,QAAQ,QAAiB;AACvC,SAAO,MAAM;AACf;AASO,SAAS,aAAa,Q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-0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=chunk-UDOXICJK.mjs.map
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createJsonRpcEngine
|
|
3
3
|
} from "./chunk-KNJNL723.mjs";
|
|
4
|
+
import {
|
|
5
|
+
getSnapFile
|
|
6
|
+
} from "./chunk-TBDYQSRT.mjs";
|
|
4
7
|
import {
|
|
5
8
|
getOptions
|
|
6
9
|
} from "./chunk-IH7UNS5A.mjs";
|
|
7
10
|
import {
|
|
8
11
|
getControllers,
|
|
9
12
|
registerSnap
|
|
10
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-LG3VKXGH.mjs";
|
|
11
14
|
import {
|
|
12
15
|
getEndowments
|
|
13
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-TAIJXTLU.mjs";
|
|
14
17
|
import {
|
|
15
18
|
createStore
|
|
16
|
-
} from "./chunk-
|
|
17
|
-
import {
|
|
18
|
-
getSnapFile
|
|
19
|
-
} from "./chunk-TBDYQSRT.mjs";
|
|
19
|
+
} from "./chunk-3M4GRUMH.mjs";
|
|
20
20
|
|
|
21
21
|
// src/internals/simulation/simulation.ts
|
|
22
22
|
import { ControllerMessenger } from "@metamask/base-controller";
|
|
@@ -28,7 +28,6 @@ import {
|
|
|
28
28
|
NodeThreadExecutionService,
|
|
29
29
|
setupMultiplex
|
|
30
30
|
} from "@metamask/snaps-controllers/node";
|
|
31
|
-
import { getEncryptionKey } from "@metamask/snaps-rpc-methods";
|
|
32
31
|
import { logError } from "@metamask/snaps-utils";
|
|
33
32
|
import { pipeline } from "readable-stream";
|
|
34
33
|
async function handleInstallSnap(snapId, {
|
|
@@ -41,11 +40,7 @@ async function handleInstallSnap(snapId, {
|
|
|
41
40
|
allowLocal: true
|
|
42
41
|
});
|
|
43
42
|
const snapFiles = await fetchSnap(snapId, location);
|
|
44
|
-
const
|
|
45
|
-
mnemonicPhrase: mnemonicPhraseToBytes(options.secretRecoveryPhrase),
|
|
46
|
-
snapId
|
|
47
|
-
});
|
|
48
|
-
const { store, runSaga } = createStore(password, options);
|
|
43
|
+
const { store, runSaga } = createStore(options);
|
|
49
44
|
const controllerMessenger = new ControllerMessenger();
|
|
50
45
|
registerActions(controllerMessenger);
|
|
51
46
|
const hooks = getHooks(options, snapFiles, snapId, controllerMessenger);
|
|
@@ -66,7 +61,9 @@ async function handleInstallSnap(snapId, {
|
|
|
66
61
|
const service = new ExecutionService({
|
|
67
62
|
...executionServiceOptions,
|
|
68
63
|
messenger: controllerMessenger.getRestricted({
|
|
69
|
-
name: "ExecutionService"
|
|
64
|
+
name: "ExecutionService",
|
|
65
|
+
allowedActions: [],
|
|
66
|
+
allowedEvents: []
|
|
70
67
|
}),
|
|
71
68
|
setupSnapProvider: (_snapId, rpcStream) => {
|
|
72
69
|
const mux = setupMultiplex(rpcStream, "snapStream");
|
|
@@ -134,4 +131,4 @@ export {
|
|
|
134
131
|
getHooks,
|
|
135
132
|
registerActions
|
|
136
133
|
};
|
|
137
|
-
//# sourceMappingURL=chunk-
|
|
134
|
+
//# sourceMappingURL=chunk-VSUNWJMQ.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/internals/simulation/simulation.ts"],"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 type { AbstractExecutionService } from '@metamask/snaps-controllers';\nimport {\n fetchSnap,\n detectSnapLocation,\n NodeThreadExecutionService,\n setupMultiplex,\n} from '@metamask/snaps-controllers/node';\nimport type {\n SnapId,\n AuxiliaryFileEncoding,\n Component,\n InterfaceState,\n} from '@metamask/snaps-sdk';\nimport type { FetchedSnapFiles } from '@metamask/snaps-utils';\nimport { logError } from '@metamask/snaps-utils';\nimport type { Duplex } from 'readable-stream';\nimport { pipeline } from 'readable-stream';\n\nimport type { RootControllerMessenger } from './controllers';\nimport { getControllers, registerSnap } from './controllers';\nimport { getSnapFile } from './files';\nimport { getEndowments } from './methods';\nimport { createJsonRpcEngine } from './middleware';\nimport type { SimulationOptions, SimulationUserOptions } from './options';\nimport { getOptions } from './options';\nimport type { RunSagaFunction, Store } from './store';\nimport { createStore } 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};\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 * @template Service - The type of the execution service.\n */\nexport async function handleInstallSnap<\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);\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 };\n}\n\n/**\n * Register mocked action handlers.\n *\n * @param controllerMessenger - The controller messenger.\n */\nexport function registerActions(controllerMessenger: RootControllerMessenger) {\n controllerMessenger.registerActionHandler(\n 'PhishingController:maybeUpdateState',\n async () => Promise.resolve(),\n );\n\n controllerMessenger.registerActionHandler(\n 'PhishingController:testOrigin',\n () => ({ result: false, type: 'all' }),\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAIA,SAAS,2BAA2B;AACpC,SAAS,0BAA0B;AACnC,SAAS,6BAA6B;AAEtC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAQP,SAAS,gBAAgB;AAEzB,SAAS,gBAAgB;AAyGzB,eAAsB,kBAKpB,QACA;AAAA,EACE;AAAA,EACA;AAAA,EACA,SAAS,aAAa,CAAC;AACzB,IAA0C,CAAC,GACnB;AACxB,QAAM,UAAU,WAAW,UAAU;AAGrC,QAAM,WAAW,mBAAmB,QAAQ;AAAA,IAC1C,YAAY;AAAA,EACd,CAAC;AAED,QAAM,YAAY,MAAM,UAAU,QAAQ,QAAQ;AAGlD,QAAM,EAAE,OAAO,QAAQ,IAAI,YAAY,OAAO;AAE9C,QAAM,sBAAsB,IAAI,oBAA8B;AAE9D,kBAAgB,mBAAmB;AAGnC,QAAM,QAAQ,SAAS,SAAS,WAAW,QAAQ,mBAAmB;AAEtE,QAAM,EAAE,2BAA2B,qBAAqB,IAAI,eAAe;AAAA,IACzE;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AAED,QAAM,SAAS,oBAAoB;AAAA,IACjC;AAAA,IACA;AAAA,IACA,sBAAsB,qBAAqB,2BAA2B;AAAA,MACpE,QAAQ;AAAA,IACV,CAAC;AAAA,EACH,CAAC;AAGD,QAAM,mBAAmB,oBAAoB;AAC7C,QAAM,UAAU,IAAI,iBAAiB;AAAA,IACnC,GAAG;AAAA,IACH,WAAW,oBAAoB,cAAc;AAAA,MAC3C,MAAM;AAAA,MACN,gBAAgB,CAAC;AAAA,MACjB,eAAe,CAAC;AAAA,IAClB,CAAC;AAAA,IACD,mBAAmB,CAAC,SAAiB,cAAsB;AACzD,YAAM,MAAM,eAAe,WAAW,Y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,MAAM,MAAM;AAAA,EACtC;AACF;","names":[]}
|
|
@@ -15,7 +15,7 @@ var _chunkLBC2OGSNjs = require('./chunk-LBC2OGSN.js');
|
|
|
15
15
|
// src/internals/simulation/store/store.ts
|
|
16
16
|
var _toolkit = require('@reduxjs/toolkit');
|
|
17
17
|
var _reduxsaga = require('redux-saga'); var _reduxsaga2 = _interopRequireDefault(_reduxsaga);
|
|
18
|
-
function createStore(
|
|
18
|
+
function createStore({ state, unencryptedState }) {
|
|
19
19
|
const sagaMiddleware = _reduxsaga2.default.call(void 0, );
|
|
20
20
|
const store = _toolkit.configureStore.call(void 0, {
|
|
21
21
|
reducer: {
|
|
@@ -29,10 +29,7 @@ function createStore(password, { state, unencryptedState }) {
|
|
|
29
29
|
if (state) {
|
|
30
30
|
store.dispatch(
|
|
31
31
|
_chunkLBC2OGSNjs.setState.call(void 0, {
|
|
32
|
-
state: JSON.stringify(
|
|
33
|
-
password,
|
|
34
|
-
value: state
|
|
35
|
-
}),
|
|
32
|
+
state: JSON.stringify(state),
|
|
36
33
|
encrypted: true
|
|
37
34
|
})
|
|
38
35
|
);
|
|
@@ -54,4 +51,4 @@ function createStore(password, { state, unencryptedState }) {
|
|
|
54
51
|
|
|
55
52
|
|
|
56
53
|
exports.createStore = createStore;
|
|
57
|
-
//# sourceMappingURL=chunk-
|
|
54
|
+
//# sourceMappingURL=chunk-WMLSD6B5.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/internals/simulation/store/store.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,SAAS,sBAAsB;AAC/B,OAAO,0BAA0B;AAgB1B,SAAS,YAAY,EAAE,OAAO,iBAAiB,GAAsB;AAC1E,QAAM,iBAAiB,qBAAqB;AAC5C,QAAM,QAAQ,eAAe;AAAA,IAC3B,SAAS;AAAA,MACP,OAAO,WAAW;AAAA,MAClB,eAAe,mBAAmB;AAAA,MAClC,OAAO,WAAW;AAAA,MAClB,IAAI,QAAQ;AAAA,IACd;AAAA,IACA,YAAY,CAAC,yBACX,qBAAqB,EAAE,OAAO,MAAM,CAAC,EAAE,OAAO,cAAc;AAAA,EAChE,CAAC;AAGD,MAAI,OAAO;AACT,UAAM;AAAA,MACJ,SAAS;AAAA,QACP,OAAO,KAAK,UAAU,KAAK;AAAA,QAC3B,WAAW;AAAA,MACb,CAAC;AAAA,IACH;AAAA,EACF;AAEA,MAAI,kBAAkB;AACpB,UAAM;AAAA,MACJ,SAAS;AAAA,QACP,OAAO,KAAK,UAAU,gBAAgB;AAAA,QACtC,WAAW;AAAA,MACb,CAAC;AAAA,IACH;AAAA,EACF;AAEA,SAAO;AAAA,IACL;AAAA,IACA,SAAS,eAAe,IAAI,KAAK,cAAc;AAAA,EACjD;AACF","sourcesContent":["import { configureStore } from '@reduxjs/toolkit';\nimport createSagaMiddleware from 'redux-saga';\n\nimport type { SimulationOptions } from '../options';\nimport { mocksSlice } from './mocks';\nimport { notificationsSlice } from './notifications';\nimport { setState, stateSlice } from './state';\nimport { uiSlice } from './ui';\n\n/**\n * Create a Redux store.\n *\n * @param options - The simulation options.\n * @param options.state - The initial state for the Snap.\n * @param options.unencryptedState - The initial unencrypted state for the Snap.\n * @returns A Redux store with the default state.\n */\nexport function createStore({ state, unencryptedState }: SimulationOptions) {\n const sagaMiddleware = createSagaMiddleware();\n const store = configureStore({\n reducer: {\n mocks: mocksSlice.reducer,\n notifications: notificationsSlice.reducer,\n state: stateSlice.reducer,\n ui: uiSlice.reducer,\n },\n middleware: (getDefaultMiddleware) =>\n getDefaultMiddleware({ thunk: false }).concat(sagaMiddleware),\n });\n\n // Set initial state for the Snap.\n if (state) {\n store.dispatch(\n setState({\n state: JSON.stringify(state),\n encrypted: true,\n }),\n );\n }\n\n if (unencryptedState) {\n store.dispatch(\n setState({\n state: JSON.stringify(unencryptedState),\n encrypted: false,\n }),\n );\n }\n\n return {\n store,\n runSaga: sagaMiddleware.run.bind(sagaMiddleware),\n };\n}\n\nexport type Store = ReturnType<typeof createStore>['store'];\nexport type ApplicationState = ReturnType<Store['getState']>;\nexport type RunSagaFunction = ReturnType<typeof createStore>['runSaga'];\n"]}
|
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
} from "./chunk-B73P64TE.mjs";
|
|
4
4
|
import {
|
|
5
5
|
handleInstallSnap
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-VSUNWJMQ.mjs";
|
|
7
7
|
import {
|
|
8
8
|
startServer
|
|
9
9
|
} from "./chunk-SLU4FNKX.mjs";
|
|
@@ -101,4 +101,4 @@ export {
|
|
|
101
101
|
SnapsEnvironment,
|
|
102
102
|
environment_default
|
|
103
103
|
};
|
|
104
|
-
//# sourceMappingURL=chunk-
|
|
104
|
+
//# sourceMappingURL=chunk-X5IPMTHO.mjs.map
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
var _chunkPZDTZGSIjs = require('./chunk-PZDTZGSI.js');
|
|
6
|
+
|
|
7
|
+
// src/internals/simulation/interface.ts
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
var _snapssdk = require('@metamask/snaps-sdk');
|
|
15
|
+
var _snapsutils = require('@metamask/snaps-utils');
|
|
16
|
+
var _effects = require('redux-saga/effects');
|
|
17
|
+
function getInterfaceResponse(runSaga, type, content, interfaceActions) {
|
|
18
|
+
switch (type) {
|
|
19
|
+
case _snapssdk.DialogType.Alert:
|
|
20
|
+
return {
|
|
21
|
+
...interfaceActions,
|
|
22
|
+
type,
|
|
23
|
+
content,
|
|
24
|
+
ok: resolveWith(runSaga, null)
|
|
25
|
+
};
|
|
26
|
+
case _snapssdk.DialogType.Confirmation:
|
|
27
|
+
return {
|
|
28
|
+
...interfaceActions,
|
|
29
|
+
type,
|
|
30
|
+
content,
|
|
31
|
+
ok: resolveWith(runSaga, true),
|
|
32
|
+
cancel: resolveWith(runSaga, false)
|
|
33
|
+
};
|
|
34
|
+
case _snapssdk.DialogType.Prompt:
|
|
35
|
+
return {
|
|
36
|
+
...interfaceActions,
|
|
37
|
+
type,
|
|
38
|
+
content,
|
|
39
|
+
ok: resolveWithInput(runSaga),
|
|
40
|
+
cancel: resolveWith(runSaga, null)
|
|
41
|
+
};
|
|
42
|
+
default:
|
|
43
|
+
throw new Error(`Unknown or unsupported dialog type: "${String(type)}".`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function resolveWith(runSaga, value) {
|
|
47
|
+
function* resolveWithSaga() {
|
|
48
|
+
yield _effects.put.call(void 0, _chunkPZDTZGSIjs.resolveInterface.call(void 0, value));
|
|
49
|
+
}
|
|
50
|
+
return async () => {
|
|
51
|
+
await runSaga(resolveWithSaga).toPromise();
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function resolveWithInput(runSaga) {
|
|
55
|
+
function* resolveWithSaga(value) {
|
|
56
|
+
yield _effects.put.call(void 0, _chunkPZDTZGSIjs.resolveInterface.call(void 0, value));
|
|
57
|
+
}
|
|
58
|
+
return async (value = "") => {
|
|
59
|
+
await runSaga(resolveWithSaga, value).toPromise();
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function* getStoredInterface(controllerMessenger, snapId) {
|
|
63
|
+
const currentInterface = yield _effects.select.call(void 0, _chunkPZDTZGSIjs.getCurrentInterface);
|
|
64
|
+
if (currentInterface) {
|
|
65
|
+
const { content: content2 } = controllerMessenger.call(
|
|
66
|
+
"SnapInterfaceController:getInterface",
|
|
67
|
+
snapId,
|
|
68
|
+
currentInterface.id
|
|
69
|
+
);
|
|
70
|
+
return { ...currentInterface, content: content2 };
|
|
71
|
+
}
|
|
72
|
+
const { payload } = yield _effects.take.call(void 0, _chunkPZDTZGSIjs.setInterface.type);
|
|
73
|
+
const { content } = controllerMessenger.call(
|
|
74
|
+
"SnapInterfaceController:getInterface",
|
|
75
|
+
snapId,
|
|
76
|
+
payload.id
|
|
77
|
+
);
|
|
78
|
+
return { ...payload, content };
|
|
79
|
+
}
|
|
80
|
+
function getElement(content, name) {
|
|
81
|
+
const { type } = content;
|
|
82
|
+
if ((type === _snapssdk.NodeType.Button || type === _snapssdk.NodeType.Input) && content.name === name) {
|
|
83
|
+
return { element: content };
|
|
84
|
+
}
|
|
85
|
+
if (_snapsutils.hasChildren.call(void 0, content)) {
|
|
86
|
+
for (const element of content.children) {
|
|
87
|
+
const result = getElement(element, name);
|
|
88
|
+
const form = type === _snapssdk.NodeType.Form ? content.name : result?.form;
|
|
89
|
+
if (result) {
|
|
90
|
+
return { element: result.element, form };
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return void 0;
|
|
95
|
+
}
|
|
96
|
+
async function clickElement(controllerMessenger, id, content, snapId, name) {
|
|
97
|
+
const result = getElement(content, name);
|
|
98
|
+
_snapssdk.assert.call(void 0,
|
|
99
|
+
result !== void 0 && result.element.type === _snapssdk.NodeType.Button,
|
|
100
|
+
"No button found in the interface."
|
|
101
|
+
);
|
|
102
|
+
if (result.form && result.element.buttonType === _snapssdk.ButtonType.Submit) {
|
|
103
|
+
const { state } = controllerMessenger.call(
|
|
104
|
+
"SnapInterfaceController:getInterface",
|
|
105
|
+
snapId,
|
|
106
|
+
id
|
|
107
|
+
);
|
|
108
|
+
await controllerMessenger.call(
|
|
109
|
+
"ExecutionService:handleRpcRequest",
|
|
110
|
+
snapId,
|
|
111
|
+
{
|
|
112
|
+
origin: "",
|
|
113
|
+
handler: _snapsutils.HandlerType.OnUserInput,
|
|
114
|
+
request: {
|
|
115
|
+
jsonrpc: "2.0",
|
|
116
|
+
method: " ",
|
|
117
|
+
params: {
|
|
118
|
+
event: {
|
|
119
|
+
type: _snapssdk.UserInputEventType.FormSubmitEvent,
|
|
120
|
+
name: result.form,
|
|
121
|
+
value: state[result.form]
|
|
122
|
+
},
|
|
123
|
+
id
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (result.element.buttonType !== _snapssdk.ButtonType.Submit) {
|
|
131
|
+
await controllerMessenger.call(
|
|
132
|
+
"ExecutionService:handleRpcRequest",
|
|
133
|
+
snapId,
|
|
134
|
+
{
|
|
135
|
+
origin: "",
|
|
136
|
+
handler: _snapsutils.HandlerType.OnUserInput,
|
|
137
|
+
request: {
|
|
138
|
+
jsonrpc: "2.0",
|
|
139
|
+
method: " ",
|
|
140
|
+
params: {
|
|
141
|
+
event: {
|
|
142
|
+
type: _snapssdk.UserInputEventType.ButtonClickEvent,
|
|
143
|
+
name: result.element.name
|
|
144
|
+
},
|
|
145
|
+
id
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function mergeValue(state, name, value, form) {
|
|
153
|
+
if (form) {
|
|
154
|
+
return {
|
|
155
|
+
...state,
|
|
156
|
+
[form]: {
|
|
157
|
+
...state[form],
|
|
158
|
+
[name]: value
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
return { ...state, [name]: value };
|
|
163
|
+
}
|
|
164
|
+
async function typeInField(controllerMessenger, id, content, snapId, name, value) {
|
|
165
|
+
const result = getElement(content, name);
|
|
166
|
+
_snapssdk.assert.call(void 0,
|
|
167
|
+
result !== void 0 && result.element.type === _snapssdk.NodeType.Input,
|
|
168
|
+
"No input found in the interface."
|
|
169
|
+
);
|
|
170
|
+
const { state } = controllerMessenger.call(
|
|
171
|
+
"SnapInterfaceController:getInterface",
|
|
172
|
+
snapId,
|
|
173
|
+
id
|
|
174
|
+
);
|
|
175
|
+
const newState = mergeValue(state, name, value, result.form);
|
|
176
|
+
controllerMessenger.call(
|
|
177
|
+
"SnapInterfaceController:updateInterfaceState",
|
|
178
|
+
id,
|
|
179
|
+
newState
|
|
180
|
+
);
|
|
181
|
+
await controllerMessenger.call("ExecutionService:handleRpcRequest", snapId, {
|
|
182
|
+
origin: "",
|
|
183
|
+
handler: _snapsutils.HandlerType.OnUserInput,
|
|
184
|
+
request: {
|
|
185
|
+
jsonrpc: "2.0",
|
|
186
|
+
method: " ",
|
|
187
|
+
params: {
|
|
188
|
+
event: {
|
|
189
|
+
type: _snapssdk.UserInputEventType.InputChangeEvent,
|
|
190
|
+
name: result.element.name,
|
|
191
|
+
value
|
|
192
|
+
},
|
|
193
|
+
id
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
function* getInterface(runSaga, snapId, controllerMessenger) {
|
|
199
|
+
const { type, id, content } = yield _effects.call.call(void 0,
|
|
200
|
+
getStoredInterface,
|
|
201
|
+
controllerMessenger,
|
|
202
|
+
snapId
|
|
203
|
+
);
|
|
204
|
+
const interfaceActions = {
|
|
205
|
+
clickElement: async (name) => {
|
|
206
|
+
await clickElement(controllerMessenger, id, content, snapId, name);
|
|
207
|
+
},
|
|
208
|
+
typeInField: async (name, value) => {
|
|
209
|
+
await typeInField(controllerMessenger, id, content, snapId, name, value);
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
return getInterfaceResponse(runSaga, type, content, interfaceActions);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
exports.getInterfaceResponse = getInterfaceResponse; exports.getElement = getElement; exports.clickElement = clickElement; exports.mergeValue = mergeValue; exports.typeInField = typeInField; exports.getInterface = getInterface;
|
|
223
|
+
//# sourceMappingURL=chunk-Y4DKEF7Y.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/internals/simulation/interface.ts"],"names":["content"],"mappings":";;;;;;;AAQA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,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AAAA,MACR,QAAQ;AAAA,QACN,OAAO;AAAA,UACL,MAAM,mBAAmB;AAAA,UACzB,MAAM,OAAO,QAAQ;AAAA,UACrB;AAAA,QACF;AAAA,QACA;AAAA,MACF;AAAA,IACF;AAAA,EACF,CAAC;AACH;AAWO,UAAU,aACf,SACA,QACA,qBAC6B;AAC7B,QAAM,EAAE,MAAM,IAAI,QAAQ,IAAI,MAAM;AAAA,IAClC;AAAA,IACA;AAAA,IACA;AAAA,EACF;AAEA,QAAM,mBAAmB;AAAA,IACvB,cAAc,OAAO,SAAiB;AACpC,YAAM,aAAa,qBAAqB,IAAI,SAAS,QAAQ,IAAI;AAAA,IACnE;AAAA,IACA,aAAa,OAAO,MAAc,UAAkB;AAClD,YAAM,YAAY,qBAAqB,IAAI,SAAS,QAAQ,MAAM,KAAK;AAAA,IACzE;AAAA,EACF;AAEA,SAAO,qBAAqB,SAAS,MAAM,SAAS,gBAAgB;AACtE","sourcesContent":["import type {\n Button,\n Component,\n FormState,\n Input,\n InterfaceState,\n SnapId,\n} from '@metamask/snaps-sdk';\nimport {\n ButtonType,\n DialogType,\n NodeType,\n UserInputEventType,\n assert,\n} from '@metamask/snaps-sdk';\nimport { HandlerType, hasChildren } from '@metamask/snaps-utils';\nimport type { PayloadAction } from '@reduxjs/toolkit';\nimport { type SagaIterator } from 'redux-saga';\nimport { call, put, select, take } from 'redux-saga/effects';\n\nimport type { SnapInterface, SnapInterfaceActions } from '../../types';\nimport type { RootControllerMessenger } from './controllers';\nimport type { Interface, RunSagaFunction } from './store';\nimport { getCurrentInterface, resolveInterface, setInterface } from './store';\n\n/**\n * Get a user interface object from a type and content object.\n *\n * @param runSaga - A function to run a saga outside the usual Redux flow.\n * @param type - The type of the interface.\n * @param content - The content to show in the interface.\n * @param interfaceActions - The actions to interact with the interface.\n * @returns The user interface object.\n */\nexport function getInterfaceResponse(\n runSaga: RunSagaFunction,\n type: DialogType,\n content: Component,\n interfaceActions: SnapInterfaceActions,\n): SnapInterface {\n switch (type) {\n case DialogType.Alert:\n return {\n ...interfaceActions,\n type,\n content,\n ok: resolveWith(runSaga, null),\n };\n\n case DialogType.Confirmation:\n return {\n ...interfaceActions,\n type,\n content,\n\n ok: resolveWith(runSaga, true),\n cancel: resolveWith(runSaga, false),\n };\n\n case DialogType.Prompt:\n return {\n ...interfaceActions,\n type,\n content,\n\n ok: resolveWithInput(runSaga),\n cancel: resolveWith(runSaga, null),\n };\n\n default:\n throw new Error(`Unknown or unsupported dialog type: \"${String(type)}\".`);\n }\n}\n\n/**\n * Resolve the current user interface with the given value. This returns a\n * function that can be used to resolve the user interface.\n *\n * @param runSaga - A function to run a saga outside the usual Redux flow.\n * @param value - The value to resolve the user interface with.\n * @returns A function that can be used to resolve the user interface.\n */\nfunction resolveWith(runSaga: RunSagaFunction, value: unknown) {\n /**\n * Resolve the current user interface with the given value.\n *\n * @yields Puts the resolve user interface action.\n */\n function* resolveWithSaga(): SagaIterator {\n yield put(resolveInterface(value));\n }\n\n return async () => {\n await runSaga(resolveWithSaga).toPromise();\n };\n}\n\n/**\n * Resolve the current user interface with the provided input. This returns a\n * function that can be used to resolve the user interface.\n *\n * @param runSaga - A function to run a saga outside the usual Redux flow.\n * @returns A function that can be used to resolve the user interface.\n */\nfunction resolveWithInput(runSaga: RunSagaFunction) {\n /**\n * Resolve the current user interface with the given value.\n *\n * @param value - The value to resolve the user interface with.\n * @yields Puts the resolve user interface action.\n */\n function* resolveWithSaga(value: string): SagaIterator {\n yield put(resolveInterface(value));\n }\n\n return async (value = '') => {\n await runSaga(resolveWithSaga, value).toPromise();\n };\n}\n\n/**\n * Get the stored user interface from the store.\n *\n * @param controllerMessenger - The controller messenger used to call actions.\n * @param snapId - The Snap ID.\n * @yields Takes the set interface action.\n * @returns The user interface object.\n */\nfunction* getStoredInterface(\n controllerMessenger: RootControllerMessenger,\n snapId: SnapId,\n): SagaIterator<Interface & { content: Component }> {\n const currentInterface: Interface | null = yield select(getCurrentInterface);\n\n if (currentInterface) {\n const { content } = controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n currentInterface.id,\n );\n\n return { ...currentInterface, content };\n }\n\n const { payload }: PayloadAction<Interface> = yield take(setInterface.type);\n\n const { content } = controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n payload.id,\n );\n\n return { ...payload, content };\n}\n\n/**\n * Get a Button or an Input from an interface.\n *\n * @param content - The interface content.\n * @param name - The element name.\n * @returns An object containing the element and the form name if it's contained in a form, otherwise undefined.\n */\nexport function getElement(\n content: Component,\n name: string,\n):\n | {\n element: Button | Input;\n form?: string;\n }\n | undefined {\n const { type } = content;\n\n if (\n (type === NodeType.Button || type === NodeType.Input) &&\n content.name === name\n ) {\n return { element: content };\n }\n\n if (hasChildren(content)) {\n for (const element of content.children) {\n const result = getElement(element, name);\n const form = type === NodeType.Form ? content.name : result?.form;\n\n if (result) {\n return { element: result.element, form };\n }\n }\n }\n\n return undefined;\n}\n\n/**\n * Click on an element of the Snap interface.\n *\n * @param controllerMessenger - The controller messenger used to call actions.\n * @param id - The interface ID.\n * @param content - The interface content.\n * @param snapId - The Snap ID.\n * @param name - The element name.\n */\nexport async function clickElement(\n controllerMessenger: RootControllerMessenger,\n id: string,\n content: Component,\n snapId: SnapId,\n name: string,\n): Promise<void> {\n const result = getElement(content, name);\n assert(\n result !== undefined && result.element.type === NodeType.Button,\n 'No button found in the interface.',\n );\n\n if (result.form && result.element.buttonType === ButtonType.Submit) {\n const { state } = controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n id,\n );\n\n await controllerMessenger.call(\n 'ExecutionService:handleRpcRequest',\n snapId,\n {\n origin: '',\n handler: HandlerType.OnUserInput,\n request: {\n jsonrpc: '2.0',\n method: ' ',\n params: {\n event: {\n type: UserInputEventType.FormSubmitEvent,\n name: result.form,\n value: state[result.form],\n },\n id,\n },\n },\n },\n );\n\n return;\n }\n\n if (result.element.buttonType !== ButtonType.Submit) {\n await controllerMessenger.call(\n 'ExecutionService:handleRpcRequest',\n snapId,\n {\n origin: '',\n handler: HandlerType.OnUserInput,\n request: {\n jsonrpc: '2.0',\n method: ' ',\n params: {\n event: {\n type: UserInputEventType.ButtonClickEvent,\n name: result.element.name,\n },\n id,\n },\n },\n },\n );\n }\n}\n\n/**\n * Merge a value in the interface state.\n *\n * @param state - The actual interface state.\n * @param name - The component name that changed value.\n * @param value - The new value.\n * @param form - The form name if the element is in one.\n * @returns The state with the merged value.\n */\nexport function mergeValue(\n state: InterfaceState,\n name: string,\n value: string | null,\n form?: string,\n): InterfaceState {\n if (form) {\n return {\n ...state,\n [form]: {\n ...(state[form] as FormState),\n [name]: value,\n },\n };\n }\n\n return { ...state, [name]: value };\n}\n\n/**\n * Type a value in an interface element.\n *\n * @param controllerMessenger - The controller messenger used to call actions.\n * @param id - The interface ID.\n * @param content - The interface Components.\n * @param snapId - The Snap ID.\n * @param name - The element name.\n * @param value - The value to type in the element.\n */\nexport async function typeInField(\n controllerMessenger: RootControllerMessenger,\n id: string,\n content: Component,\n snapId: SnapId,\n name: string,\n value: string,\n) {\n const result = getElement(content, name);\n\n assert(\n result !== undefined && result.element.type === NodeType.Input,\n 'No input found in the interface.',\n );\n\n const { state } = controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n id,\n );\n\n const newState = mergeValue(state, name, value, result.form);\n\n controllerMessenger.call(\n 'SnapInterfaceController:updateInterfaceState',\n id,\n newState,\n );\n\n await controllerMessenger.call('ExecutionService:handleRpcRequest', snapId, {\n origin: '',\n handler: HandlerType.OnUserInput,\n request: {\n jsonrpc: '2.0',\n method: ' ',\n params: {\n event: {\n type: UserInputEventType.InputChangeEvent,\n name: result.element.name,\n value,\n },\n id,\n },\n },\n });\n}\n\n/**\n * Get a user interface object from a Snap.\n *\n * @param runSaga - A function to run a saga outside the usual Redux flow.\n * @param snapId - The Snap ID.\n * @param controllerMessenger - The controller messenger used to call actions.\n * @yields Takes the set interface action.\n * @returns The user interface object.\n */\nexport function* getInterface(\n runSaga: RunSagaFunction,\n snapId: SnapId,\n controllerMessenger: RootControllerMessenger,\n): SagaIterator<SnapInterface> {\n const { type, id, content } = yield call(\n getStoredInterface,\n controllerMessenger,\n snapId,\n );\n\n const interfaceActions = {\n clickElement: async (name: string) => {\n await clickElement(controllerMessenger, id, content, snapId, name);\n },\n typeInField: async (name: string, value: string) => {\n await typeInField(controllerMessenger, id, content, snapId, name, value);\n },\n };\n\n return getInterfaceResponse(runSaga, type, content, interfaceActions);\n}\n"]}
|