@metamask/snaps-jest 7.0.2 → 8.1.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 +25 -1
- package/dist/{chunk-7L5S3PID.js → chunk-2OHD2VKS.js} +72 -20
- package/dist/chunk-2OHD2VKS.js.map +1 -0
- package/dist/{chunk-LMCG5RIX.js → chunk-3FDEYMQU.js} +12 -12
- package/dist/{chunk-DTDYOBCI.js → chunk-44ARQBXS.js} +3 -3
- package/dist/{chunk-BVGI3E45.mjs → chunk-6V5GEUDO.mjs} +84 -7
- package/dist/chunk-6V5GEUDO.mjs.map +1 -0
- package/dist/{chunk-QPC6UJH7.mjs → chunk-AFA4KKWW.mjs} +134 -41
- package/dist/chunk-AFA4KKWW.mjs.map +1 -0
- package/dist/{chunk-VSUNWJMQ.mjs → chunk-E2BYTLOT.mjs} +3 -3
- package/dist/{chunk-MCYTJUDG.js → chunk-EDFQDZNY.js} +6 -6
- package/dist/{chunk-WH5C5WIZ.mjs → chunk-FQWOVTBB.mjs} +4 -3
- package/dist/chunk-FQWOVTBB.mjs.map +1 -0
- package/dist/{chunk-LG3VKXGH.mjs → chunk-GG2BCPQH.mjs} +2 -2
- package/dist/{chunk-YEVKBYKO.mjs → chunk-HOI6FPLR.mjs} +4 -7
- package/dist/chunk-HOI6FPLR.mjs.map +1 -0
- package/dist/{chunk-MJHR5RTY.js → chunk-J3I5KZIF.js} +4 -3
- package/dist/chunk-J3I5KZIF.js.map +1 -0
- package/dist/{chunk-2JAGD4N6.js → chunk-JXAJWUVZ.js} +3 -3
- package/dist/{chunk-X5IPMTHO.mjs → chunk-K55LIE3W.mjs} +2 -2
- package/dist/{chunk-FIZAYEHV.js → chunk-KQBJQLZG.js} +87 -10
- package/dist/chunk-KQBJQLZG.js.map +1 -0
- package/dist/{chunk-ZWN4SO2V.js → chunk-MBQHVBLA.js} +4 -4
- package/dist/{chunk-KR7CYXCR.js → chunk-R55KAAM4.js} +130 -37
- package/dist/chunk-R55KAAM4.js.map +1 -0
- package/dist/{chunk-TAIJXTLU.mjs → chunk-TMAWGVZP.mjs} +2 -2
- package/dist/{chunk-3OEADJAL.mjs → chunk-U3DVRTS2.mjs} +3 -3
- package/dist/{chunk-ZJVA3AOC.mjs → chunk-UPR3PTSA.mjs} +70 -18
- package/dist/chunk-UPR3PTSA.mjs.map +1 -0
- package/dist/{chunk-IDGD7TZ7.js → chunk-YNUVT3HC.js} +3 -6
- package/dist/chunk-YNUVT3HC.js.map +1 -0
- package/dist/environment.js +9 -9
- package/dist/environment.mjs +8 -8
- package/dist/helpers.js +9 -9
- package/dist/helpers.mjs +8 -8
- package/dist/index.js +10 -10
- package/dist/index.mjs +9 -9
- package/dist/internals/index.js +10 -8
- package/dist/internals/index.mjs +9 -7
- package/dist/internals/request.js +8 -7
- package/dist/internals/request.mjs +7 -6
- package/dist/internals/simulation/controllers.js +4 -4
- package/dist/internals/simulation/controllers.mjs +3 -3
- package/dist/internals/simulation/index.js +8 -6
- package/dist/internals/simulation/index.mjs +7 -5
- package/dist/internals/simulation/interface.js +4 -2
- package/dist/internals/simulation/interface.mjs +3 -1
- package/dist/internals/simulation/methods/hooks/index.js +2 -2
- package/dist/internals/simulation/methods/hooks/index.mjs +1 -1
- package/dist/internals/simulation/methods/hooks/interface.js +2 -2
- package/dist/internals/simulation/methods/hooks/interface.mjs +1 -1
- package/dist/internals/simulation/methods/index.js +3 -3
- package/dist/internals/simulation/methods/index.mjs +2 -2
- package/dist/internals/simulation/methods/specifications.js +3 -3
- package/dist/internals/simulation/methods/specifications.mjs +2 -2
- package/dist/internals/simulation/simulation.js +5 -5
- package/dist/internals/simulation/simulation.mjs +4 -4
- package/dist/internals/structs.js +2 -2
- package/dist/internals/structs.mjs +1 -1
- package/dist/matchers.js +11 -9
- package/dist/matchers.mjs +10 -8
- package/dist/setup.js +8 -8
- package/dist/setup.mjs +8 -8
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/global.d.ts +2 -2
- package/dist/types/internals/request.d.ts +1 -1
- package/dist/types/internals/simulation/interface.d.ts +29 -8
- package/dist/types/internals/simulation/methods/hooks/interface.d.ts +2 -2
- package/dist/types/internals/structs.d.ts +16 -152
- package/dist/types/matchers.d.ts +12 -2
- package/dist/types/types.d.ts +13 -5
- package/package.json +10 -10
- package/dist/chunk-7L5S3PID.js.map +0 -1
- package/dist/chunk-BVGI3E45.mjs.map +0 -1
- package/dist/chunk-FIZAYEHV.js.map +0 -1
- package/dist/chunk-IDGD7TZ7.js.map +0 -1
- package/dist/chunk-KR7CYXCR.js.map +0 -1
- package/dist/chunk-MJHR5RTY.js.map +0 -1
- package/dist/chunk-QPC6UJH7.mjs.map +0 -1
- package/dist/chunk-WH5C5WIZ.mjs.map +0 -1
- package/dist/chunk-YEVKBYKO.mjs.map +0 -1
- package/dist/chunk-ZJVA3AOC.mjs.map +0 -1
- /package/dist/{chunk-LMCG5RIX.js.map → chunk-3FDEYMQU.js.map} +0 -0
- /package/dist/{chunk-DTDYOBCI.js.map → chunk-44ARQBXS.js.map} +0 -0
- /package/dist/{chunk-VSUNWJMQ.mjs.map → chunk-E2BYTLOT.mjs.map} +0 -0
- /package/dist/{chunk-MCYTJUDG.js.map → chunk-EDFQDZNY.js.map} +0 -0
- /package/dist/{chunk-LG3VKXGH.mjs.map → chunk-GG2BCPQH.mjs.map} +0 -0
- /package/dist/{chunk-2JAGD4N6.js.map → chunk-JXAJWUVZ.js.map} +0 -0
- /package/dist/{chunk-X5IPMTHO.mjs.map → chunk-K55LIE3W.mjs.map} +0 -0
- /package/dist/{chunk-ZWN4SO2V.js.map → chunk-MBQHVBLA.js.map} +0 -0
- /package/dist/{chunk-TAIJXTLU.mjs.map → chunk-TMAWGVZP.mjs.map} +0 -0
- /package/dist/{chunk-3OEADJAL.mjs.map → chunk-U3DVRTS2.mjs.map} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/matchers.ts"],"names":["message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K,OAAO,SAAS,QAAQ;AAK1C,QAAM,aAAa;AAAA,IACjB,aAAa,QAAQ;AAAA,IACrB,aAAa,OAAO;AAAA,EACtB;AAEA,QAAM,UAAU,OACZ,MACE,GAAG,KAAK,MAAM,YAAY,eAAe,CAAC;AAAA;AAAA;AAAA,EAC5B,eAAe,aAAa,QAAQ,CAAC,CAAC;AAAA;AAAA;AAAA,EACtC,eAAe,aAAa,OAAO,CAAC,CAAC;AAAA;AAAA;AAAA;AAAA,EAC7B,UAAU,KAClC,MACE,GAAG,KAAK,MAAM,YAAY,WAAW,CAAC;AAAA;AAAA;AAAA,EACxB,eAAe,aAAa,QAAQ,CAAC,CAAC;AAAA;AAAA;AAAA,EACtC,eAAe,aAAa,OAAO,CAAC,CAAC;AAAA;AAAA;AAAA;AAAA,EAC7B,UAAU;AAEtC,SAAO,EAAE,SAAS,KAAK;AACzB;AAEF,OAAO,OAAO;AAAA,EACZ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC","sourcesContent":["/* eslint-disable no-invalid-this */\n\n// Note: Because this file imports from `@jest/globals`, it can only be used in\n// a Jest environment. This is why it's not exported from the index file.\n\nimport type { MatcherFunction } from '@jest/expect';\nimport { expect } from '@jest/globals';\nimport type {\n NotificationType,\n EnumToUnion,\n ComponentOrElement,\n Component,\n} from '@metamask/snaps-sdk';\nimport type { JSXElement, SnapNode } from '@metamask/snaps-sdk/jsx';\nimport { isJSXElementUnsafe } from '@metamask/snaps-sdk/jsx';\nimport { getJsxElementFromComponent } from '@metamask/snaps-utils';\nimport type { Json } from '@metamask/utils';\nimport { hasProperty } from '@metamask/utils';\nimport type { MatcherHintOptions } from 'jest-matcher-utils';\nimport {\n EXPECTED_COLOR,\n diff,\n matcherErrorMessage,\n matcherHint,\n printReceived,\n printWithType,\n RECEIVED_COLOR,\n} from 'jest-matcher-utils';\nimport { is } from 'superstruct';\n\nimport { InterfaceStruct, SnapResponseStruct } from './internals';\nimport type { SnapResponse } from './types';\n\n/**\n * Ensure that the actual value is a response from the `request` function.\n *\n * @param actual - The actual value.\n * @param matcherName - The name of the matcher.\n * @param options - The matcher options.\n */\nfunction assertActualIsSnapResponse(\n actual: unknown,\n matcherName: string,\n options?: MatcherHintOptions,\n): asserts actual is SnapResponse {\n if (!is(actual, SnapResponseStruct)) {\n throw new Error(\n matcherErrorMessage(\n matcherHint(matcherName, undefined, undefined, options),\n `${RECEIVED_COLOR(\n 'received',\n )} value must be a response from the \\`request\\` function`,\n printWithType('Received', actual, printReceived),\n ),\n );\n }\n}\n\n/**\n * Ensure that the actual value is a response from the `request` function, and\n * that it has a `ui` property.\n *\n * @param actual - The actual value.\n * @param matcherName - The name of the matcher.\n * @param options - The matcher options.\n */\nfunction assertHasInterface(\n actual: unknown,\n matcherName: string,\n options?: MatcherHintOptions,\n): asserts actual is { content: JSXElement } {\n if (!is(actual, InterfaceStruct) || !actual.content) {\n throw new Error(\n matcherErrorMessage(\n matcherHint(matcherName, undefined, undefined, options),\n `${RECEIVED_COLOR('received')} value must have a \\`content\\` property`,\n printWithType('Received', actual, printReceived),\n ),\n );\n }\n}\n\n/**\n * Check if a JSON-RPC response matches the expected value. This matcher is\n * intended to be used with the `expect` global.\n *\n * @param actual - The actual response.\n * @param expected - The expected response.\n * @returns The status and message.\n */\nexport const toRespondWith: MatcherFunction<[expected: Json]> = function (\n actual,\n expected,\n) {\n assertActualIsSnapResponse(actual, 'toRespondWith');\n\n const { response } = actual;\n if (hasProperty(response, 'error')) {\n const message = () =>\n `${this.utils.matcherHint('.toRespondWith')}\\n\\n` +\n `Expected response: ${this.utils.printExpected(expected)}\\n` +\n `Received error: ${this.utils.printReceived(response.error)}`;\n\n return { message, pass: false };\n }\n\n const pass = this.equals(response.result, expected);\n const message = pass\n ? () =>\n `${this.utils.matcherHint('.not.toRespondWith')}\\n\\n` +\n `Expected: ${this.utils.printExpected(expected)}\\n` +\n `Received: ${this.utils.printReceived(response.result)}`\n : () =>\n `${this.utils.matcherHint('.toRespondWith')}\\n\\n` +\n `Expected: ${this.utils.printExpected(expected)}\\n` +\n `Received: ${this.utils.printReceived(response.result)}`;\n\n return { message, pass };\n};\n\nexport const toRespondWithError: MatcherFunction<[expected: Json]> = function (\n actual,\n expected,\n) {\n assertActualIsSnapResponse(actual, 'toRespondWithError');\n\n const { response } = actual;\n if (hasProperty(response, 'result')) {\n const message = () =>\n `${this.utils.matcherHint('.toRespondWithError')}\\n\\n` +\n `Expected error: ${this.utils.printExpected(expected)}\\n` +\n `Received result: ${this.utils.printReceived(response.result)}`;\n\n return { message, pass: false };\n }\n\n const pass = this.equals(response.error, expected);\n const message = pass\n ? () =>\n `${this.utils.matcherHint('.not.toRespondWithError')}\\n\\n` +\n `Expected: ${this.utils.printExpected(expected)}\\n` +\n `Received: ${this.utils.printReceived(response.error)}`\n : () =>\n `${this.utils.matcherHint('.toRespondWithError')}\\n\\n` +\n `Expected: ${this.utils.printExpected(expected)}\\n` +\n `Received: ${this.utils.printReceived(response.error)}`;\n\n return { message, pass };\n};\n\n/**\n * Check if the snap sent a notification with the expected message. This matcher\n * is intended to be used with the `expect` global.\n *\n * @param actual - The actual response.\n * @param expected - The expected notification message.\n * @param type - The expected notification type.\n * @returns The status and message.\n */\nexport const toSendNotification: MatcherFunction<\n [expected: string, type?: EnumToUnion<NotificationType> | undefined]\n> = function (actual, expected, type) {\n assertActualIsSnapResponse(actual, 'toSendNotification');\n\n const { notifications } = actual;\n const pass = notifications.some(\n (notification) =>\n this.equals(notification.message, expected) &&\n (type === undefined || notification.type === type),\n );\n\n const message = pass\n ? () =>\n `${this.utils.matcherHint('.not.toSendNotification')}\\n\\n` +\n `Expected: ${this.utils.printExpected(expected)}\\n` +\n `Expected type: ${this.utils.printExpected(type)}\\n` +\n `Received: ${this.utils.printReceived(notifications)}`\n : () =>\n `${this.utils.matcherHint('.toSendNotification')}\\n\\n` +\n `Expected: ${this.utils.printExpected(expected)}\\n` +\n `Expected type: ${this.utils.printExpected(type)}\\n` +\n `Received: ${this.utils.printReceived(notifications)}`;\n\n return { message, pass };\n};\n\n/**\n * Serialise a JSX prop to a string.\n *\n * @param prop - The JSX prop.\n * @returns The serialised JSX prop.\n */\nfunction serialiseProp(prop: unknown): string {\n if (typeof prop === 'string') {\n return `\"${prop}\"`;\n }\n\n return `{${JSON.stringify(prop)}}`;\n}\n\n/**\n * Serialise JSX props to a string.\n *\n * @param props - The JSX props.\n * @returns The serialised JSX props.\n */\nfunction serialiseProps(props: Record<string, unknown>): string {\n return Object.entries(props)\n .filter(([key]) => key !== 'children')\n .sort(([a], [b]) => a.localeCompare(b))\n .map(([key, value]) => ` ${key}=${serialiseProp(value)}`)\n .join('');\n}\n\n/**\n * Serialise a JSX node to a string.\n *\n * @param node - The JSX node.\n * @param indentation - The indentation level. Defaults to `0`. This should not\n * be set by the caller, as it is used for recursion.\n * @returns The serialised JSX node.\n */\nexport function serialiseJsx(node: SnapNode, indentation = 0): string {\n if (Array.isArray(node)) {\n return node.map((child) => serialiseJsx(child, indentation)).join('');\n }\n\n const indent = ' '.repeat(indentation);\n if (typeof node === 'string') {\n return `${indent}${node}\\n`;\n }\n\n if (!node) {\n return '';\n }\n\n const { type, props } = node;\n const trailingNewline = indentation > 0 ? '\\n' : '';\n\n if (hasProperty(props, 'children')) {\n const children = serialiseJsx(props.children as SnapNode, indentation + 1);\n return `${indent}<${type}${serialiseProps(\n props,\n )}>\\n${children}${indent}</${type}>${trailingNewline}`;\n }\n\n return `${indent}<${type}${serialiseProps(props)} />${trailingNewline}`;\n}\n\nconst toRenderLegacy: MatcherFunction<[expected: Component]> = function (\n actual,\n expected,\n) {\n assertHasInterface(actual, 'toRender');\n\n const { content } = actual;\n const expectedElement = getJsxElementFromComponent(expected);\n const pass = this.equals(content, expectedElement);\n\n // This is typed as `string | null`, but in practice it's always a string.\n // The function only returns `null` if both the expected and actual values\n // are numbers, bigints, or booleans, which is never the case here.\n const difference = diff(expectedElement, content) as string;\n\n const message = pass\n ? () =>\n `${this.utils.matcherHint('.not.toRender')}\\n\\n` +\n `Expected:\\n${this.utils.printExpected(expectedElement)}\\n\\n` +\n `Received:\\n${this.utils.printReceived(content)}` +\n `\\n\\nDifference:\\n\\n${difference}`\n : () =>\n `${this.utils.matcherHint('.toRender')}\\n\\n` +\n `Expected:\\n${this.utils.printExpected(expectedElement)}\\n\\n` +\n `Received:\\n${this.utils.printReceived(content)}` +\n `\\n\\nDifference:\\n\\n${difference}`;\n\n return { message, pass };\n};\n\nexport const toRender: MatcherFunction<[expected: ComponentOrElement]> =\n function (actual, expected) {\n assertHasInterface(actual, 'toRender');\n\n if (!isJSXElementUnsafe(expected)) {\n return toRenderLegacy.call(this, actual, expected);\n }\n\n const { content } = actual;\n const pass = this.equals(content, expected);\n\n // This is typed as `string | null`, but in practice it's always a string.\n // The function only returns `null` if both the expected and actual values\n // are numbers, bigints, or booleans, which is never the case here.\n const difference = diff(\n serialiseJsx(expected),\n serialiseJsx(content),\n ) as string;\n\n const message = pass\n ? () =>\n `${this.utils.matcherHint('.not.toRender')}\\n\\n` +\n `Expected:\\n${EXPECTED_COLOR(serialiseJsx(expected))}\\n\\n` +\n `Received:\\n${RECEIVED_COLOR(serialiseJsx(content))}` +\n `\\n\\nDifference:\\n\\n${difference}`\n : () =>\n `${this.utils.matcherHint('.toRender')}\\n\\n` +\n `Expected:\\n${EXPECTED_COLOR(serialiseJsx(expected))}\\n\\n` +\n `Received:\\n${RECEIVED_COLOR(serialiseJsx(content))}` +\n `\\n\\nDifference:\\n\\n${difference}`;\n\n return { message, pass };\n };\n\nexpect.extend({\n toRespondWith,\n toRespondWithError,\n toSendNotification,\n toRender,\n});\n"]}
|
|
@@ -4,7 +4,7 @@ var _chunk265BMFM5js = require('./chunk-265BMFM5.js');
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
var
|
|
7
|
+
var _chunkJ3I5KZIFjs = require('./chunk-J3I5KZIF.js');
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
|
|
@@ -58,8 +58,8 @@ function getPermissionSpecifications({
|
|
|
58
58
|
showInAppNotification: _chunkGMTKFAWOjs.getShowInAppNotificationImplementation.call(void 0, runSaga),
|
|
59
59
|
showNativeNotification: _chunkGMTKFAWOjs.getShowNativeNotificationImplementation.call(void 0, runSaga),
|
|
60
60
|
updateSnapState: _chunkSB5EPHE3js.getUpdateSnapStateMethodImplementation.call(void 0, runSaga),
|
|
61
|
-
createInterface:
|
|
62
|
-
getInterface:
|
|
61
|
+
createInterface: _chunkJ3I5KZIFjs.getCreateInterfaceImplementation.call(void 0, controllerMessenger),
|
|
62
|
+
getInterface: _chunkJ3I5KZIFjs.getGetInterfaceImplementation.call(void 0, controllerMessenger)
|
|
63
63
|
})
|
|
64
64
|
};
|
|
65
65
|
}
|
|
@@ -86,4 +86,4 @@ async function getEndowments(permissionController, snapId) {
|
|
|
86
86
|
|
|
87
87
|
|
|
88
88
|
exports.resolve = resolve; exports.asyncResolve = asyncResolve; exports.getPermissionSpecifications = getPermissionSpecifications; exports.getEndowments = getEndowments;
|
|
89
|
-
//# sourceMappingURL=chunk-
|
|
89
|
+
//# sourceMappingURL=chunk-MBQHVBLA.js.map
|
|
@@ -5,14 +5,14 @@
|
|
|
5
5
|
var _chunkPZDTZGSIjs = require('./chunk-PZDTZGSI.js');
|
|
6
6
|
|
|
7
7
|
// src/internals/simulation/interface.ts
|
|
8
|
+
var _snapssdk = require('@metamask/snaps-sdk');
|
|
8
9
|
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
var _snapssdk = require('@metamask/snaps-sdk');
|
|
15
14
|
var _snapsutils = require('@metamask/snaps-utils');
|
|
15
|
+
var _utils = require('@metamask/utils');
|
|
16
16
|
var _effects = require('redux-saga/effects');
|
|
17
17
|
function getInterfaceResponse(runSaga, type, content, interfaceActions) {
|
|
18
18
|
switch (type) {
|
|
@@ -77,23 +77,36 @@ function* getStoredInterface(controllerMessenger, snapId) {
|
|
|
77
77
|
);
|
|
78
78
|
return { ...payload, content };
|
|
79
79
|
}
|
|
80
|
+
function isJSXElementWithName(element, name) {
|
|
81
|
+
return _utils.hasProperty.call(void 0, element.props, "name") && element.props.name === name;
|
|
82
|
+
}
|
|
83
|
+
function getFormElement(form, name) {
|
|
84
|
+
const element = _snapsutils.walkJsx.call(void 0, form, (childElement) => {
|
|
85
|
+
if (isJSXElementWithName(childElement, name)) {
|
|
86
|
+
return childElement;
|
|
87
|
+
}
|
|
88
|
+
return void 0;
|
|
89
|
+
});
|
|
90
|
+
if (element === void 0) {
|
|
91
|
+
return void 0;
|
|
92
|
+
}
|
|
93
|
+
return { element, form: form.props.name };
|
|
94
|
+
}
|
|
80
95
|
function getElement(content, name) {
|
|
81
|
-
|
|
82
|
-
if ((type === _snapssdk.NodeType.Button || type === _snapssdk.NodeType.Input) && content.name === name) {
|
|
96
|
+
if (isJSXElementWithName(content, name)) {
|
|
83
97
|
return { element: content };
|
|
84
98
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const form = type === _snapssdk.NodeType.Form ? content.name : result?.form;
|
|
89
|
-
if (result) {
|
|
90
|
-
return { element: result.element, form };
|
|
91
|
-
}
|
|
99
|
+
return _snapsutils.walkJsx.call(void 0, content, (element) => {
|
|
100
|
+
if (element.type === "Form") {
|
|
101
|
+
return getFormElement(element, name);
|
|
92
102
|
}
|
|
93
|
-
|
|
94
|
-
|
|
103
|
+
if (isJSXElementWithName(element, name)) {
|
|
104
|
+
return { element };
|
|
105
|
+
}
|
|
106
|
+
return void 0;
|
|
107
|
+
});
|
|
95
108
|
}
|
|
96
|
-
async function handleEvent(controllerMessenger, snapId, id, event) {
|
|
109
|
+
async function handleEvent(controllerMessenger, snapId, id, event, context) {
|
|
97
110
|
try {
|
|
98
111
|
await controllerMessenger.call(
|
|
99
112
|
"ExecutionService:handleRpcRequest",
|
|
@@ -106,7 +119,8 @@ async function handleEvent(controllerMessenger, snapId, id, event) {
|
|
|
106
119
|
method: " ",
|
|
107
120
|
params: {
|
|
108
121
|
event,
|
|
109
|
-
id
|
|
122
|
+
id,
|
|
123
|
+
context
|
|
110
124
|
}
|
|
111
125
|
}
|
|
112
126
|
}
|
|
@@ -119,24 +133,40 @@ async function handleEvent(controllerMessenger, snapId, id, event) {
|
|
|
119
133
|
async function clickElement(controllerMessenger, id, content, snapId, name) {
|
|
120
134
|
const result = getElement(content, name);
|
|
121
135
|
_snapssdk.assert.call(void 0,
|
|
122
|
-
result !== void 0
|
|
123
|
-
|
|
136
|
+
result !== void 0,
|
|
137
|
+
`Could not find an element in the interface with the name "${name}".`
|
|
124
138
|
);
|
|
125
|
-
|
|
126
|
-
type
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
139
|
+
_snapssdk.assert.call(void 0,
|
|
140
|
+
result.element.type === "Button",
|
|
141
|
+
`Expected an element of type "Button", but found "${result.element.type}".`
|
|
142
|
+
);
|
|
143
|
+
const { state, context } = controllerMessenger.call(
|
|
144
|
+
"SnapInterfaceController:getInterface",
|
|
145
|
+
snapId,
|
|
146
|
+
id
|
|
147
|
+
);
|
|
148
|
+
await handleEvent(
|
|
149
|
+
controllerMessenger,
|
|
150
|
+
snapId,
|
|
151
|
+
id,
|
|
152
|
+
{
|
|
153
|
+
type: _snapssdk.UserInputEventType.ButtonClickEvent,
|
|
154
|
+
name: result.element.props.name
|
|
155
|
+
},
|
|
156
|
+
context
|
|
157
|
+
);
|
|
158
|
+
if (result.form && result.element.props.type === "submit") {
|
|
159
|
+
await handleEvent(
|
|
160
|
+
controllerMessenger,
|
|
132
161
|
snapId,
|
|
133
|
-
id
|
|
162
|
+
id,
|
|
163
|
+
{
|
|
164
|
+
type: _snapssdk.UserInputEventType.FormSubmitEvent,
|
|
165
|
+
name: result.form,
|
|
166
|
+
value: state[result.form]
|
|
167
|
+
},
|
|
168
|
+
context
|
|
134
169
|
);
|
|
135
|
-
await handleEvent(controllerMessenger, snapId, id, {
|
|
136
|
-
type: _snapssdk.UserInputEventType.FormSubmitEvent,
|
|
137
|
-
name: result.form,
|
|
138
|
-
value: state[result.form]
|
|
139
|
-
});
|
|
140
170
|
}
|
|
141
171
|
}
|
|
142
172
|
function mergeValue(state, name, value, form) {
|
|
@@ -154,10 +184,61 @@ function mergeValue(state, name, value, form) {
|
|
|
154
184
|
async function typeInField(controllerMessenger, id, content, snapId, name, value) {
|
|
155
185
|
const result = getElement(content, name);
|
|
156
186
|
_snapssdk.assert.call(void 0,
|
|
157
|
-
result !== void 0
|
|
158
|
-
|
|
187
|
+
result !== void 0,
|
|
188
|
+
`Could not find an element in the interface with the name "${name}".`
|
|
189
|
+
);
|
|
190
|
+
_snapssdk.assert.call(void 0,
|
|
191
|
+
result.element.type === "Input",
|
|
192
|
+
`Expected an element of type "Input", but found "${result.element.type}".`
|
|
193
|
+
);
|
|
194
|
+
const { state, context } = controllerMessenger.call(
|
|
195
|
+
"SnapInterfaceController:getInterface",
|
|
196
|
+
snapId,
|
|
197
|
+
id
|
|
198
|
+
);
|
|
199
|
+
const newState = mergeValue(state, name, value, result.form);
|
|
200
|
+
controllerMessenger.call(
|
|
201
|
+
"SnapInterfaceController:updateInterfaceState",
|
|
202
|
+
id,
|
|
203
|
+
newState
|
|
204
|
+
);
|
|
205
|
+
await controllerMessenger.call("ExecutionService:handleRpcRequest", snapId, {
|
|
206
|
+
origin: "",
|
|
207
|
+
handler: _snapsutils.HandlerType.OnUserInput,
|
|
208
|
+
request: {
|
|
209
|
+
jsonrpc: "2.0",
|
|
210
|
+
method: " ",
|
|
211
|
+
params: {
|
|
212
|
+
event: {
|
|
213
|
+
type: _snapssdk.UserInputEventType.InputChangeEvent,
|
|
214
|
+
name: result.element.props.name,
|
|
215
|
+
value
|
|
216
|
+
},
|
|
217
|
+
id,
|
|
218
|
+
context
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
async function selectInDropdown(controllerMessenger, id, content, snapId, name, value) {
|
|
224
|
+
const result = getElement(content, name);
|
|
225
|
+
_snapssdk.assert.call(void 0,
|
|
226
|
+
result !== void 0,
|
|
227
|
+
`Could not find an element in the interface with the name "${name}".`
|
|
159
228
|
);
|
|
160
|
-
|
|
229
|
+
_snapssdk.assert.call(void 0,
|
|
230
|
+
result.element.type === "Dropdown",
|
|
231
|
+
`Expected an element of type "Dropdown", but found "${result.element.type}".`
|
|
232
|
+
);
|
|
233
|
+
const options = _snapsutils.getJsxChildren.call(void 0, result.element);
|
|
234
|
+
const selectedOption = options.find(
|
|
235
|
+
(option) => _utils.hasProperty.call(void 0, option.props, "value") && option.props.value === value
|
|
236
|
+
);
|
|
237
|
+
_snapssdk.assert.call(void 0,
|
|
238
|
+
selectedOption !== void 0,
|
|
239
|
+
`The dropdown with the name "${name}" does not contain "${value}".`
|
|
240
|
+
);
|
|
241
|
+
const { state, context } = controllerMessenger.call(
|
|
161
242
|
"SnapInterfaceController:getInterface",
|
|
162
243
|
snapId,
|
|
163
244
|
id
|
|
@@ -177,10 +258,11 @@ async function typeInField(controllerMessenger, id, content, snapId, name, value
|
|
|
177
258
|
params: {
|
|
178
259
|
event: {
|
|
179
260
|
type: _snapssdk.UserInputEventType.InputChangeEvent,
|
|
180
|
-
name: result.element.name,
|
|
261
|
+
name: result.element.props.name,
|
|
181
262
|
value
|
|
182
263
|
},
|
|
183
|
-
id
|
|
264
|
+
id,
|
|
265
|
+
context
|
|
184
266
|
}
|
|
185
267
|
}
|
|
186
268
|
});
|
|
@@ -197,6 +279,16 @@ function* getInterface(runSaga, snapId, controllerMessenger) {
|
|
|
197
279
|
},
|
|
198
280
|
typeInField: async (name, value) => {
|
|
199
281
|
await typeInField(controllerMessenger, id, content, snapId, name, value);
|
|
282
|
+
},
|
|
283
|
+
selectInDropdown: async (name, value) => {
|
|
284
|
+
await selectInDropdown(
|
|
285
|
+
controllerMessenger,
|
|
286
|
+
id,
|
|
287
|
+
content,
|
|
288
|
+
snapId,
|
|
289
|
+
name,
|
|
290
|
+
value
|
|
291
|
+
);
|
|
200
292
|
}
|
|
201
293
|
};
|
|
202
294
|
return getInterfaceResponse(runSaga, type, content, interfaceActions);
|
|
@@ -209,5 +301,6 @@ function* getInterface(runSaga, snapId, controllerMessenger) {
|
|
|
209
301
|
|
|
210
302
|
|
|
211
303
|
|
|
212
|
-
|
|
213
|
-
|
|
304
|
+
|
|
305
|
+
exports.getInterfaceResponse = getInterfaceResponse; exports.getElement = getElement; exports.clickElement = clickElement; exports.mergeValue = mergeValue; exports.typeInField = typeInField; exports.selectInDropdown = selectInDropdown; exports.getInterface = getInterface;
|
|
306
|
+
//# sourceMappingURL=chunk-R55KAAM4.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/internals/simulation/interface.ts"],"names":["content"],"mappings":";;;;;;;AAOA,SAAS,YAAY,oBAAoB,cAAc;AAEvD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,mBAAmB;AAG5B,SAAS,MAAM,KAAK,QAAQ,YAAY;AAgBjC,SAAS,qBACd,SACA,MACA,SACA,kBACe;AACf,UAAQ,MAAM;AAAA,IACZ,KAAK,WAAW;AACd,aAAO;AAAA,QACL,GAAG;AAAA,QACH;AAAA,QACA;AAAA,QACA,IAAI,YAAY,SAAS,IAAI;AAAA,MAC/B;AAAA,IAEF,KAAK,WAAW;AACd,aAAO;AAAA,QACL,GAAG;AAAA,QACH;AAAA,QACA;AAAA,QAEA,IAAI,YAAY,SAAS,IAAI;AAAA,QAC7B,QAAQ,YAAY,SAAS,KAAK;AAAA,MACpC;AAAA,IAEF,KAAK,WAAW;AACd,aAAO;AAAA,QACL,GAAG;AAAA,QACH;AAAA,QACA;AAAA,QAEA,IAAI,iBAAiB,OAAO;AAAA,QAC5B,QAAQ,YAAY,SAAS,IAAI;AAAA,MACnC;AAAA,IAEF;AACE,YAAM,IAAI,MAAM,wCAAwC,OAAO,IAAI,CAAC,IAAI;AAAA,E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AO,MAAc,UAAkB;AAClD,YAAM,YAAY,qBAAqB,IAAI,SAAS,QAAQ,MAAM,KAAK;AAAA,IACzE;AAAA,IACA,kBAAkB,OAAO,MAAc,UAAkB;AACvD,YAAM;AAAA,QACJ;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO,qBAAqB,SAAS,MAAM,SAAS,gBAAgB;AACtE","sourcesContent":["import type {\n FormState,\n InterfaceContext,\n InterfaceState,\n SnapId,\n UserInputEvent,\n} from '@metamask/snaps-sdk';\nimport { DialogType, UserInputEventType, assert } from '@metamask/snaps-sdk';\nimport type { FormElement, JSXElement } from '@metamask/snaps-sdk/jsx';\nimport {\n HandlerType,\n getJsxChildren,\n unwrapError,\n walkJsx,\n} from '@metamask/snaps-utils';\nimport { hasProperty } from '@metamask/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: JSXElement,\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: JSXElement }> {\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 const { content } = controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n payload.id,\n );\n\n return { ...payload, content };\n}\n\n/**\n * A JSX element with a name.\n */\nexport type NamedJSXElement = JSXElement & { props: { name: string } };\n\n/**\n * Check if a JSX element is a JSX element with a given name.\n *\n * @param element - The JSX element.\n * @param name - The element name.\n * @returns True if the element is a JSX element with the given name, otherwise\n * false.\n */\nfunction isJSXElementWithName<Element extends JSXElement, Name extends string>(\n element: Element,\n name: Name,\n): element is Element & { props: { name: Name } } {\n return hasProperty(element.props, 'name') && element.props.name === name;\n}\n\n/**\n * Find an element inside a form element in a JSX tree.\n *\n * @param form - The form element.\n * @param name - The element name.\n * @returns An object containing the element and the form name if it's contained\n * in a form, otherwise undefined.\n */\nfunction getFormElement(form: FormElement, name: string) {\n const element = walkJsx<NamedJSXElement>(form, (childElement) => {\n if (isJSXElementWithName(childElement, name)) {\n return childElement;\n }\n\n return undefined;\n });\n\n if (element === undefined) {\n return undefined;\n }\n\n return { element, form: form.props.name };\n}\n\n/**\n * Get an element from a JSX tree with the given name.\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\n * in a form, otherwise undefined.\n */\nexport function getElement(\n content: JSXElement,\n name: string,\n):\n | {\n element: NamedJSXElement;\n form?: string;\n }\n | undefined {\n if (isJSXElementWithName(content, name)) {\n return { element: content };\n }\n\n return walkJsx(content, (element) => {\n if (element.type === 'Form') {\n return getFormElement(element, name);\n }\n\n if (isJSXElementWithName(element, name)) {\n return { element };\n }\n\n return undefined;\n });\n}\n/**\n * Handle submitting event requests to OnUserInput including unwrapping potential errors.\n *\n * @param controllerMessenger - The controller messenger used to call actions.\n * @param snapId - The Snap ID.\n * @param id - The interface ID.\n * @param event - The event to submit.\n * @param context - The interface context.\n */\nasync function handleEvent(\n controllerMessenger: RootControllerMessenger,\n snapId: SnapId,\n id: string,\n event: UserInputEvent,\n context: InterfaceContext | null,\n) {\n try {\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 id,\n context,\n },\n },\n },\n );\n } catch (error) {\n const [unwrapped] = unwrapError(error);\n throw unwrapped;\n }\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: JSXElement,\n snapId: SnapId,\n name: string,\n): Promise<void> {\n const result = getElement(content, name);\n assert(\n result !== undefined,\n `Could not find an element in the interface with the name \"${name}\".`,\n );\n\n assert(\n result.element.type === 'Button',\n `Expected an element of type \"Button\", but found \"${result.element.type}\".`,\n );\n\n const { state, context } = controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n id,\n );\n\n // Button click events are always triggered.\n await handleEvent(\n controllerMessenger,\n snapId,\n id,\n {\n type: UserInputEventType.ButtonClickEvent,\n name: result.element.props.name,\n },\n context,\n );\n\n if (result.form && result.element.props.type === 'submit') {\n await handleEvent(\n controllerMessenger,\n snapId,\n id,\n {\n type: UserInputEventType.FormSubmitEvent,\n name: result.form,\n value: state[result.form] as Record<string, string | null>,\n },\n context,\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: JSXElement,\n snapId: SnapId,\n name: string,\n value: string,\n) {\n const result = getElement(content, name);\n\n assert(\n result !== undefined,\n `Could not find an element in the interface with the name \"${name}\".`,\n );\n\n assert(\n result.element.type === 'Input',\n `Expected an element of type \"Input\", but found \"${result.element.type}\".`,\n );\n\n const { state, context } = 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.props.name,\n value,\n },\n id,\n context,\n },\n },\n });\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 selectInDropdown(\n controllerMessenger: RootControllerMessenger,\n id: string,\n content: JSXElement,\n snapId: SnapId,\n name: string,\n value: string,\n) {\n const result = getElement(content, name);\n\n assert(\n result !== undefined,\n `Could not find an element in the interface with the name \"${name}\".`,\n );\n\n assert(\n result.element.type === 'Dropdown',\n `Expected an element of type \"Dropdown\", but found \"${result.element.type}\".`,\n );\n\n const options = getJsxChildren(result.element) as JSXElement[];\n const selectedOption = options.find(\n (option) =>\n hasProperty(option.props, 'value') && option.props.value === value,\n );\n\n assert(\n selectedOption !== undefined,\n `The dropdown with the name \"${name}\" does not contain \"${value}\".`,\n );\n\n const { state, context } = 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.props.name,\n value,\n },\n id,\n context,\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 selectInDropdown: async (name: string, value: string) => {\n await selectInDropdown(\n controllerMessenger,\n id,\n content,\n snapId,\n name,\n value,\n );\n },\n };\n\n return getInterfaceResponse(runSaga, type, content, interfaceActions);\n}\n"]}
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
import {
|
|
5
5
|
getCreateInterfaceImplementation,
|
|
6
6
|
getGetInterfaceImplementation
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-FQWOVTBB.mjs";
|
|
8
8
|
import {
|
|
9
9
|
getShowInAppNotificationImplementation,
|
|
10
10
|
getShowNativeNotificationImplementation
|
|
@@ -86,4 +86,4 @@ export {
|
|
|
86
86
|
getPermissionSpecifications,
|
|
87
87
|
getEndowments
|
|
88
88
|
};
|
|
89
|
-
//# sourceMappingURL=chunk-
|
|
89
|
+
//# sourceMappingURL=chunk-TMAWGVZP.mjs.map
|
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
} from "./chunk-XK5HPZZC.mjs";
|
|
4
4
|
import {
|
|
5
5
|
handleRequest
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-UPR3PTSA.mjs";
|
|
7
7
|
import {
|
|
8
8
|
rootLogger
|
|
9
9
|
} from "./chunk-J4ZPUCLX.mjs";
|
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
SignatureOptionsStruct,
|
|
13
13
|
SnapResponseWithInterfaceStruct,
|
|
14
14
|
TransactionOptionsStruct
|
|
15
|
-
} from "./chunk-
|
|
15
|
+
} from "./chunk-HOI6FPLR.mjs";
|
|
16
16
|
import {
|
|
17
17
|
addJsonRpcMock,
|
|
18
18
|
removeJsonRpcMock
|
|
@@ -160,4 +160,4 @@ async function installSnap(snapId, options = {}) {
|
|
|
160
160
|
export {
|
|
161
161
|
installSnap
|
|
162
162
|
};
|
|
163
|
-
//# sourceMappingURL=chunk-
|
|
163
|
+
//# sourceMappingURL=chunk-U3DVRTS2.mjs.map
|
|
@@ -1,17 +1,30 @@
|
|
|
1
1
|
import {
|
|
2
2
|
clickElement,
|
|
3
3
|
getInterface,
|
|
4
|
+
selectInDropdown,
|
|
4
5
|
typeInField
|
|
5
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-AFA4KKWW.mjs";
|
|
7
|
+
import {
|
|
8
|
+
SnapResponseStruct
|
|
9
|
+
} from "./chunk-HOI6FPLR.mjs";
|
|
6
10
|
import {
|
|
7
11
|
clearNotifications,
|
|
8
12
|
getNotifications
|
|
9
13
|
} from "./chunk-LB4R3BUA.mjs";
|
|
10
14
|
|
|
11
15
|
// src/internals/request.ts
|
|
16
|
+
import {
|
|
17
|
+
ComponentOrElementStruct
|
|
18
|
+
} from "@metamask/snaps-sdk";
|
|
12
19
|
import { unwrapError } from "@metamask/snaps-utils";
|
|
13
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
assert,
|
|
22
|
+
getSafeJson,
|
|
23
|
+
hasProperty,
|
|
24
|
+
isPlainObject
|
|
25
|
+
} from "@metamask/utils";
|
|
14
26
|
import { nanoid } from "@reduxjs/toolkit";
|
|
27
|
+
import { is } from "superstruct";
|
|
15
28
|
function handleRequest({
|
|
16
29
|
snapId,
|
|
17
30
|
store,
|
|
@@ -21,6 +34,11 @@ function handleRequest({
|
|
|
21
34
|
runSaga,
|
|
22
35
|
request: { id = nanoid(), origin = "https://metamask.io", ...options }
|
|
23
36
|
}) {
|
|
37
|
+
const getInterfaceError = () => {
|
|
38
|
+
throw new Error(
|
|
39
|
+
"Unable to get the interface from the Snap: The request to the Snap failed."
|
|
40
|
+
);
|
|
41
|
+
};
|
|
24
42
|
const promise = executionService.handleRpcRequest(snapId, {
|
|
25
43
|
origin,
|
|
26
44
|
handler,
|
|
@@ -32,19 +50,31 @@ function handleRequest({
|
|
|
32
50
|
}).then(async (result) => {
|
|
33
51
|
const notifications = getNotifications(store.getState());
|
|
34
52
|
store.dispatch(clearNotifications());
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
53
|
+
try {
|
|
54
|
+
const getInterfaceFn = await getInterfaceApi(
|
|
55
|
+
result,
|
|
56
|
+
snapId,
|
|
57
|
+
controllerMessenger
|
|
58
|
+
);
|
|
59
|
+
return {
|
|
60
|
+
id: String(id),
|
|
61
|
+
response: {
|
|
62
|
+
result: getSafeJson(result)
|
|
63
|
+
},
|
|
64
|
+
notifications,
|
|
65
|
+
...getInterfaceFn ? { getInterface: getInterfaceFn } : {}
|
|
66
|
+
};
|
|
67
|
+
} catch (error) {
|
|
68
|
+
const [unwrappedError] = unwrapError(error);
|
|
69
|
+
return {
|
|
70
|
+
id: String(id),
|
|
71
|
+
response: {
|
|
72
|
+
error: unwrappedError.serialize()
|
|
73
|
+
},
|
|
74
|
+
notifications: [],
|
|
75
|
+
getInterface: getInterfaceError
|
|
76
|
+
};
|
|
77
|
+
}
|
|
48
78
|
}).catch((error) => {
|
|
49
79
|
const [unwrappedError] = unwrapError(error);
|
|
50
80
|
return {
|
|
@@ -52,16 +82,24 @@ function handleRequest({
|
|
|
52
82
|
response: {
|
|
53
83
|
error: unwrappedError.serialize()
|
|
54
84
|
},
|
|
55
|
-
notifications: []
|
|
85
|
+
notifications: [],
|
|
86
|
+
getInterface: getInterfaceError
|
|
56
87
|
};
|
|
57
88
|
});
|
|
58
89
|
promise.getInterface = async () => {
|
|
59
|
-
|
|
90
|
+
const sagaPromise = runSaga(
|
|
60
91
|
getInterface,
|
|
61
92
|
runSaga,
|
|
62
93
|
snapId,
|
|
63
94
|
controllerMessenger
|
|
64
95
|
).toPromise();
|
|
96
|
+
const result = await Promise.race([promise, sagaPromise]);
|
|
97
|
+
if (is(result, SnapResponseStruct) && hasProperty(result.response, "error")) {
|
|
98
|
+
throw new Error(
|
|
99
|
+
`Unable to get the interface from the Snap: The returned interface may be invalid. The error message received was: ${result.response.error.message}`
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
return await sagaPromise;
|
|
65
103
|
};
|
|
66
104
|
return promise;
|
|
67
105
|
}
|
|
@@ -70,6 +108,10 @@ async function getInterfaceFromResult(result, snapId, controllerMessenger) {
|
|
|
70
108
|
return result.id;
|
|
71
109
|
}
|
|
72
110
|
if (isPlainObject(result) && hasProperty(result, "content")) {
|
|
111
|
+
assert(
|
|
112
|
+
is(result.content, ComponentOrElementStruct),
|
|
113
|
+
"The Snap returned an invalid interface."
|
|
114
|
+
);
|
|
73
115
|
const id = await controllerMessenger.call(
|
|
74
116
|
"SnapInterfaceController:createInterface",
|
|
75
117
|
snapId,
|
|
@@ -112,6 +154,16 @@ async function getInterfaceApi(result, snapId, controllerMessenger) {
|
|
|
112
154
|
name,
|
|
113
155
|
value
|
|
114
156
|
);
|
|
157
|
+
},
|
|
158
|
+
selectInDropdown: async (name, value) => {
|
|
159
|
+
await selectInDropdown(
|
|
160
|
+
controllerMessenger,
|
|
161
|
+
interfaceId,
|
|
162
|
+
content,
|
|
163
|
+
snapId,
|
|
164
|
+
name,
|
|
165
|
+
value
|
|
166
|
+
);
|
|
115
167
|
}
|
|
116
168
|
};
|
|
117
169
|
};
|
|
@@ -124,4 +176,4 @@ export {
|
|
|
124
176
|
getInterfaceFromResult,
|
|
125
177
|
getInterfaceApi
|
|
126
178
|
};
|
|
127
|
-
//# sourceMappingURL=chunk-
|
|
179
|
+
//# sourceMappingURL=chunk-UPR3PTSA.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/internals/request.ts"],"sourcesContent":["import type { AbstractExecutionService } from '@metamask/snaps-controllers';\nimport {\n type SnapId,\n type JsonRpcError,\n type ComponentOrElement,\n ComponentOrElementStruct,\n} from '@metamask/snaps-sdk';\nimport type { HandlerType } from '@metamask/snaps-utils';\nimport { unwrapError } from '@metamask/snaps-utils';\nimport {\n assert,\n getSafeJson,\n hasProperty,\n isPlainObject,\n} from '@metamask/utils';\nimport { nanoid } from '@reduxjs/toolkit';\nimport { is } from 'superstruct';\n\nimport type {\n RequestOptions,\n SnapHandlerInterface,\n SnapRequest,\n} from '../types';\nimport {\n clearNotifications,\n clickElement,\n getInterface,\n getNotifications,\n typeInField,\n selectInDropdown,\n} from './simulation';\nimport type { RunSagaFunction, Store } from './simulation';\nimport type { RootControllerMessenger } from './simulation/controllers';\nimport { SnapResponseStruct } from './structs';\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 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 selectInDropdown: async (name, value) => {\n await selectInDropdown(\n controllerMessenger,\n interfaceId,\n content,\n snapId,\n name,\n value,\n );\n },\n };\n };\n }\n\n return undefined;\n}\n"],"mappings":";;;;;;;;;;;;;;;AACA;AAAA,EAIE;AAAA,OACK;AAEP,SAAS,mBAAmB;AAC5B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,cAAc;AACvB,SAA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ACA;AAAA,YACA;AAAA,UACF;AAAA,QACF;AAAA,QACA,kBAAkB,OAAO,MAAM,UAAU;AACvC,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":[]}
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/internals/structs.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
2
|
var _snapssdk = require('@metamask/snaps-sdk');
|
|
3
|
+
var _jsx = require('@metamask/snaps-sdk/jsx');
|
|
7
4
|
|
|
8
5
|
|
|
9
6
|
|
|
@@ -196,7 +193,7 @@ var JsonRpcMockOptionsStruct = _superstruct.object.call(void 0, {
|
|
|
196
193
|
result: _utils.JsonStruct
|
|
197
194
|
});
|
|
198
195
|
var InterfaceStruct = _superstruct.type.call(void 0, {
|
|
199
|
-
content: _superstruct.optional.call(void 0,
|
|
196
|
+
content: _superstruct.optional.call(void 0, _jsx.JSXElementStruct)
|
|
200
197
|
});
|
|
201
198
|
var SnapResponseWithoutInterfaceStruct = _superstruct.object.call(void 0, {
|
|
202
199
|
id: _superstruct.string.call(void 0, ),
|
|
@@ -240,4 +237,4 @@ var SnapResponseStruct = _superstruct.union.call(void 0, [
|
|
|
240
237
|
|
|
241
238
|
|
|
242
239
|
exports.TransactionOptionsStruct = TransactionOptionsStruct; exports.SignatureOptionsStruct = SignatureOptionsStruct; exports.SnapOptionsStruct = SnapOptionsStruct; exports.JsonRpcMockOptionsStruct = JsonRpcMockOptionsStruct; exports.InterfaceStruct = InterfaceStruct; exports.SnapResponseWithoutInterfaceStruct = SnapResponseWithoutInterfaceStruct; exports.SnapResponseWithInterfaceStruct = SnapResponseWithInterfaceStruct; exports.SnapResponseStruct = SnapResponseStruct;
|
|
243
|
-
//# sourceMappingURL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CAAC;AAEM,IAAM,kCAAkC;AAAA,EAC7C;AAAA,EACA,OAAO;AAAA,IACL,cAAc,KAAK;AAAA,EACrB,CAAC;AACH;AAEO,IAAM,qBAAqB,MAAM;AAAA,EACtC;AAAA,EACA;AACF,CAAC","sourcesContent":["import { NotificationType, enumValue } from '@metamask/snaps-sdk';\nimport { JSXElementStruct } from '@metamask/snaps-sdk/jsx';\nimport {\n bytesToHex,\n JsonStruct,\n StrictHexStruct,\n valueToBytes,\n} from '@metamask/utils';\nimport { randomBytes } from 'crypto';\nimport {\n array,\n assign,\n bigint,\n coerce,\n defaulted,\n instance,\n literal,\n number,\n object,\n optional,\n string,\n union,\n record,\n any,\n func,\n type,\n} from 'superstruct';\n\n// TODO: Export this from `@metamask/utils` instead.\nconst BytesLikeStruct = union([\n bigint(),\n number(),\n string(),\n instance(Uint8Array),\n]);\n\nexport const TransactionOptionsStruct = object({\n /**\n * The CAIP-2 chain ID to send the transaction on. Defaults to `eip155:1`.\n */\n chainId: defaulted(string(), 'eip155:1'),\n\n /**\n * The origin to send the transaction from. Defaults to `metamask.io`.\n */\n origin: defaulted(string(), 'metamask.io'),\n\n /**\n * The address to send the transaction from. Defaults to a randomly generated\n * address.\n */\n // TODO: Move this coercer to `@metamask/utils`.\n from: coerce(StrictHexStruct, optional(BytesLikeStruct), (value) => {\n if (value) {\n return bytesToHex(valueToBytes(value));\n }\n\n return bytesToHex(randomBytes(20));\n }),\n\n /**\n * The address to send the transaction to. Defaults to a randomly generated\n * address.\n */\n // TODO: Move this coercer to `@metamask/utils`.\n to: coerce(StrictHexStruct, optional(BytesLikeStruct), (value) => {\n if (value) {\n return bytesToHex(valueToBytes(value));\n }\n\n return bytesToHex(randomBytes(20));\n }),\n\n /**\n * The value to send with the transaction. The value may be specified as a\n * `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0`.\n */\n value: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n '0x0',\n ),\n\n /**\n * The gas limit to use for the transaction. The gas limit may be specified\n * as a `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `21_000`.\n */\n gasLimit: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n valueToBytes(21_000),\n ),\n\n /**\n * The max fee per gas (in Wei) to use for the transaction. The max fee per\n * gas may be specified as a `number`, `bigint`, `string`, or `Uint8Array`.\n * Defaults to `1`.\n */\n maxFeePerGas: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n valueToBytes(1),\n ),\n\n /**\n * The max priority fee per gas (in Wei) to use for the transaction. The max\n * priority fee per gas may be specified as a `number`, `bigint`, `string`,\n * or `Uint8Array`. Defaults to `1`.\n */\n maxPriorityFeePerGas: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n valueToBytes(1),\n ),\n\n /**\n * The nonce to use for the transaction. The nonce may be specified as a\n * `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0`.\n */\n nonce: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n valueToBytes(0),\n ),\n\n /**\n * The data to send with the transaction. The data may be specified as a\n * `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0x`.\n */\n data: defaulted(\n coerce(union([StrictHexStruct, literal('0x')]), BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n '0x',\n ),\n});\n\nexport const SignatureOptionsStruct = object({\n /**\n * The origin making the signature request.\n */\n origin: defaulted(string(), 'metamask.io'),\n\n /**\n * The address signing the signature request. Defaults to a randomly generated\n * address.\n */\n from: coerce(StrictHexStruct, optional(BytesLikeStruct), (value) => {\n if (value) {\n return bytesToHex(valueToBytes(value));\n }\n\n return bytesToHex(randomBytes(20));\n }),\n\n /**\n * The data to send with the transaction. The data may be specified as a\n * `string`, an object, or an array of objects. This covers the data types\n * for the supported signature methods. Defaults to `0x`.\n */\n data: defaulted(\n union([\n StrictHexStruct,\n literal('0x'),\n record(string(), any()),\n array(record(string(), any())),\n ]),\n '0x',\n ),\n\n /**\n * The signature method being used.\n */\n signatureMethod: defaulted(\n union([\n literal('eth_sign'),\n literal('personal_sign'),\n literal('eth_signTypedData'),\n literal('eth_signTypedData_v3'),\n literal('eth_signTypedData_v4'),\n ]),\n 'personal_sign',\n ),\n});\n\nexport const SnapOptionsStruct = object({\n /**\n * The timeout in milliseconds to use for requests to the snap. Defaults to\n * `1000`.\n */\n timeout: defaulted(optional(number()), 1000),\n});\n\nexport const JsonRpcMockOptionsStruct = object({\n method: string(),\n result: JsonStruct,\n});\n\nexport const InterfaceStruct = type({\n content: optional(JSXElementStruct),\n});\n\nexport const SnapResponseWithoutInterfaceStruct = object({\n id: string(),\n\n response: union([\n object({\n result: JsonStruct,\n }),\n object({\n error: JsonStruct,\n }),\n ]),\n\n notifications: array(\n object({\n id: string(),\n message: string(),\n type: union([\n enumValue(NotificationType.InApp),\n enumValue(NotificationType.Native),\n ]),\n }),\n ),\n});\n\nexport const SnapResponseWithInterfaceStruct = assign(\n SnapResponseWithoutInterfaceStruct,\n object({\n getInterface: func(),\n }),\n);\n\nexport const SnapResponseStruct = union([\n SnapResponseWithoutInterfaceStruct,\n SnapResponseWithInterfaceStruct,\n]);\n"]}
|
package/dist/environment.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
var
|
|
4
|
+
var _chunkJXAJWUVZjs = require('./chunk-JXAJWUVZ.js');
|
|
5
5
|
require('./chunk-T2PHS5FY.js');
|
|
6
6
|
require('./chunk-HROOL3VI.js');
|
|
7
7
|
require('./chunk-2JTGBHPR.js');
|
|
8
|
-
require('./chunk-
|
|
8
|
+
require('./chunk-2OHD2VKS.js');
|
|
9
9
|
require('./chunk-GECE47HW.js');
|
|
10
|
-
require('./chunk-
|
|
11
|
-
require('./chunk-
|
|
10
|
+
require('./chunk-R55KAAM4.js');
|
|
11
|
+
require('./chunk-EDFQDZNY.js');
|
|
12
12
|
require('./chunk-ZTX624YO.js');
|
|
13
13
|
require('./chunk-XWGECIXI.js');
|
|
14
14
|
require('./chunk-WHGYMK5B.js');
|
|
@@ -20,14 +20,14 @@ require('./chunk-TVD4SWH7.js');
|
|
|
20
20
|
require('./chunk-VXPCUDBI.js');
|
|
21
21
|
require('./chunk-N6MAO223.js');
|
|
22
22
|
require('./chunk-TZB4LBT7.js');
|
|
23
|
-
require('./chunk-
|
|
23
|
+
require('./chunk-YNUVT3HC.js');
|
|
24
24
|
require('./chunk-XKJHFUHE.js');
|
|
25
|
-
require('./chunk-
|
|
25
|
+
require('./chunk-44ARQBXS.js');
|
|
26
26
|
require('./chunk-DR6Y7IVX.js');
|
|
27
|
-
require('./chunk-
|
|
27
|
+
require('./chunk-MBQHVBLA.js');
|
|
28
28
|
require('./chunk-CKRORVDW.js');
|
|
29
29
|
require('./chunk-265BMFM5.js');
|
|
30
|
-
require('./chunk-
|
|
30
|
+
require('./chunk-J3I5KZIF.js');
|
|
31
31
|
require('./chunk-GMTKFAWO.js');
|
|
32
32
|
require('./chunk-RAMAN5RM.js');
|
|
33
33
|
require('./chunk-SB5EPHE3.js');
|
|
@@ -42,5 +42,5 @@ require('./chunk-PHUTP7NB.js');
|
|
|
42
42
|
|
|
43
43
|
|
|
44
44
|
|
|
45
|
-
exports.SnapsEnvironment =
|
|
45
|
+
exports.SnapsEnvironment = _chunkJXAJWUVZjs.SnapsEnvironment; exports.default = _chunkJXAJWUVZjs.environment_default;
|
|
46
46
|
//# sourceMappingURL=environment.js.map
|