@metamask/snaps-controllers 9.0.0 → 9.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 +8 -1
- package/dist/chunk-3T6W5VI2.mjs +100 -0
- package/dist/chunk-3T6W5VI2.mjs.map +1 -0
- package/dist/{chunk-6VGMRXGY.js → chunk-BUHS4M6F.js} +7 -7
- package/dist/chunk-H7TB7I6Z.js +100 -0
- package/dist/chunk-H7TB7I6Z.js.map +1 -0
- package/dist/{chunk-YAZ55WUL.mjs → chunk-WP3DAIFM.mjs} +2 -2
- package/dist/cronjob/CronjobController.js +2 -2
- package/dist/cronjob/CronjobController.mjs +2 -2
- package/dist/cronjob/index.js +2 -2
- package/dist/cronjob/index.mjs +2 -2
- package/dist/index.js +3 -3
- package/dist/index.mjs +2 -2
- package/dist/interface/SnapInterfaceController.js +3 -3
- package/dist/interface/SnapInterfaceController.mjs +2 -2
- package/dist/interface/index.js +3 -3
- package/dist/interface/index.mjs +2 -2
- package/dist/interface/utils.js +2 -2
- package/dist/interface/utils.mjs +1 -1
- package/dist/node.js +3 -3
- package/dist/node.mjs +2 -2
- package/dist/react-native.js +3 -3
- package/dist/react-native.mjs +2 -2
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/interface/utils.d.ts +2 -3
- package/package.json +5 -5
- package/dist/chunk-7CZDBXIY.mjs +0 -100
- package/dist/chunk-7CZDBXIY.mjs.map +0 -1
- package/dist/chunk-WCDDYBFW.js +0 -100
- package/dist/chunk-WCDDYBFW.js.map +0 -1
- /package/dist/{chunk-6VGMRXGY.js.map → chunk-BUHS4M6F.js.map} +0 -0
- /package/dist/{chunk-YAZ55WUL.mjs.map → chunk-WP3DAIFM.mjs.map} +0 -0
|
@@ -20,11 +20,10 @@ export declare function assertNameIsUnique(state: InterfaceState, name: string):
|
|
|
20
20
|
* Construct the interface state for a given component tree.
|
|
21
21
|
*
|
|
22
22
|
* @param oldState - The previous state.
|
|
23
|
-
* @param
|
|
24
|
-
* @param newState - The state that is being constructed.
|
|
23
|
+
* @param rootComponent - The UI component to construct state from.
|
|
25
24
|
* @returns The interface state of the passed component.
|
|
26
25
|
*/
|
|
27
|
-
export declare function constructState(oldState: InterfaceState,
|
|
26
|
+
export declare function constructState(oldState: InterfaceState, rootComponent: JSXElement): InterfaceState;
|
|
28
27
|
/**
|
|
29
28
|
* Validate a JSON blob to be used as the interface context.
|
|
30
29
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/snaps-controllers",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.1.0",
|
|
4
4
|
"description": "Controllers for MetaMask Snaps.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -63,9 +63,9 @@
|
|
|
63
63
|
"@metamask/post-message-stream": "^8.1.0",
|
|
64
64
|
"@metamask/rpc-errors": "^6.2.1",
|
|
65
65
|
"@metamask/snaps-registry": "^3.1.0",
|
|
66
|
-
"@metamask/snaps-rpc-methods": "^9.1.
|
|
67
|
-
"@metamask/snaps-sdk": "^
|
|
68
|
-
"@metamask/snaps-utils": "^7.
|
|
66
|
+
"@metamask/snaps-rpc-methods": "^9.1.4",
|
|
67
|
+
"@metamask/snaps-sdk": "^6.0.0",
|
|
68
|
+
"@metamask/snaps-utils": "^7.7.0",
|
|
69
69
|
"@metamask/utils": "^8.3.0",
|
|
70
70
|
"@xstate/fsm": "^2.0.0",
|
|
71
71
|
"browserify-zlib": "^0.2.0",
|
|
@@ -138,7 +138,7 @@
|
|
|
138
138
|
"webdriverio": "^8.19.0"
|
|
139
139
|
},
|
|
140
140
|
"peerDependencies": {
|
|
141
|
-
"@metamask/snaps-execution-environments": "^6.
|
|
141
|
+
"@metamask/snaps-execution-environments": "^6.5.0"
|
|
142
142
|
},
|
|
143
143
|
"peerDependenciesMeta": {
|
|
144
144
|
"@metamask/snaps-execution-environments": {
|
package/dist/chunk-7CZDBXIY.mjs
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
// src/interface/utils.ts
|
|
2
|
-
import { assert } from "@metamask/snaps-sdk";
|
|
3
|
-
import { isJSXElementUnsafe } from "@metamask/snaps-sdk/jsx";
|
|
4
|
-
import {
|
|
5
|
-
getJsonSizeUnsafe,
|
|
6
|
-
getJsxChildren,
|
|
7
|
-
getJsxElementFromComponent
|
|
8
|
-
} from "@metamask/snaps-utils";
|
|
9
|
-
function getJsxInterface(component) {
|
|
10
|
-
if (isJSXElementUnsafe(component)) {
|
|
11
|
-
return component;
|
|
12
|
-
}
|
|
13
|
-
return getJsxElementFromComponent(component);
|
|
14
|
-
}
|
|
15
|
-
function assertNameIsUnique(state, name) {
|
|
16
|
-
assert(
|
|
17
|
-
state[name] === void 0,
|
|
18
|
-
`Duplicate component names are not allowed, found multiple instances of: "${name}".`
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
function constructComponentSpecificDefaultState(element) {
|
|
22
|
-
if (element.type === "Dropdown") {
|
|
23
|
-
const children = getJsxChildren(element);
|
|
24
|
-
return children[0]?.props.value;
|
|
25
|
-
}
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
function constructInputState(oldState, element) {
|
|
29
|
-
return element.props.value ?? oldState[element.props.name] ?? constructComponentSpecificDefaultState(element) ?? null;
|
|
30
|
-
}
|
|
31
|
-
function constructFormInputState(oldState, component, form) {
|
|
32
|
-
const oldFormState = oldState[form];
|
|
33
|
-
const oldInputState = oldFormState?.[component.props.name];
|
|
34
|
-
return component.props.value ?? oldInputState ?? constructComponentSpecificDefaultState(component) ?? null;
|
|
35
|
-
}
|
|
36
|
-
function getFieldInput(element) {
|
|
37
|
-
if (Array.isArray(element.props.children)) {
|
|
38
|
-
return element.props.children[0];
|
|
39
|
-
}
|
|
40
|
-
return element.props.children;
|
|
41
|
-
}
|
|
42
|
-
function constructFormState(oldState, component, form, newState) {
|
|
43
|
-
if (component.type === "Button") {
|
|
44
|
-
return newState;
|
|
45
|
-
}
|
|
46
|
-
const input = getFieldInput(component);
|
|
47
|
-
assertNameIsUnique(newState, input.props.name);
|
|
48
|
-
newState[input.props.name] = constructFormInputState(oldState, input, form);
|
|
49
|
-
return newState;
|
|
50
|
-
}
|
|
51
|
-
function constructState(oldState, component, newState = {}) {
|
|
52
|
-
if (component.type === "Box") {
|
|
53
|
-
const children = getJsxChildren(component);
|
|
54
|
-
return children.reduce(
|
|
55
|
-
(accumulator, node) => constructState(oldState, node, accumulator),
|
|
56
|
-
newState
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
if (component.type === "Form") {
|
|
60
|
-
assertNameIsUnique(newState, component.props.name);
|
|
61
|
-
const children = getJsxChildren(component);
|
|
62
|
-
newState[component.props.name] = children.reduce(
|
|
63
|
-
(accumulator, node) => {
|
|
64
|
-
return constructFormState(
|
|
65
|
-
oldState,
|
|
66
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
67
|
-
node,
|
|
68
|
-
component.props.name,
|
|
69
|
-
accumulator
|
|
70
|
-
);
|
|
71
|
-
},
|
|
72
|
-
{}
|
|
73
|
-
);
|
|
74
|
-
return newState;
|
|
75
|
-
}
|
|
76
|
-
if (component.type === "Input" || component.type === "Dropdown") {
|
|
77
|
-
assertNameIsUnique(newState, component.props.name);
|
|
78
|
-
newState[component.props.name] = constructInputState(oldState, component);
|
|
79
|
-
}
|
|
80
|
-
return newState;
|
|
81
|
-
}
|
|
82
|
-
var MAX_CONTEXT_SIZE = 1e6;
|
|
83
|
-
function validateInterfaceContext(context) {
|
|
84
|
-
if (!context) {
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
const size = getJsonSizeUnsafe(context);
|
|
88
|
-
assert(
|
|
89
|
-
size <= MAX_CONTEXT_SIZE,
|
|
90
|
-
`A Snap interface context may not be larger than ${MAX_CONTEXT_SIZE / 1e6} MB.`
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export {
|
|
95
|
-
getJsxInterface,
|
|
96
|
-
assertNameIsUnique,
|
|
97
|
-
constructState,
|
|
98
|
-
validateInterfaceContext
|
|
99
|
-
};
|
|
100
|
-
//# sourceMappingURL=chunk-7CZDBXIY.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/interface/utils.ts"],"sourcesContent":["import { assert } from '@metamask/snaps-sdk';\nimport type {\n FormState,\n InterfaceState,\n ComponentOrElement,\n InterfaceContext,\n} from '@metamask/snaps-sdk';\nimport type {\n ButtonElement,\n DropdownElement,\n FieldElement,\n InputElement,\n JSXElement,\n OptionElement,\n} from '@metamask/snaps-sdk/jsx';\nimport { isJSXElementUnsafe } from '@metamask/snaps-sdk/jsx';\nimport {\n getJsonSizeUnsafe,\n getJsxChildren,\n getJsxElementFromComponent,\n} from '@metamask/snaps-utils';\n\n/**\n * Get a JSX element from a component or JSX element. If the component is a\n * JSX element, it is returned as is. Otherwise, the component is converted to\n * a JSX element.\n *\n * @param component - The component to convert.\n * @returns The JSX element.\n */\nexport function getJsxInterface(component: ComponentOrElement): JSXElement {\n if (isJSXElementUnsafe(component)) {\n return component;\n }\n\n return getJsxElementFromComponent(component);\n}\n\n/**\n * Assert that the component name is unique in state.\n *\n * @param state - The interface state to verify against.\n * @param name - The component name to verify.\n */\nexport function assertNameIsUnique(state: InterfaceState, name: string) {\n assert(\n state[name] === undefined,\n `Duplicate component names are not allowed, found multiple instances of: \"${name}\".`,\n );\n}\n\n/**\n * Construct default state for a component.\n *\n * This function is meant to be used inside constructInputState to account\n * for component specific defaults and will not override the component value or existing form state.\n *\n * @param element - The input element.\n * @returns The default state for the specific component, if any.\n */\nfunction constructComponentSpecificDefaultState(\n element: InputElement | DropdownElement,\n) {\n if (element.type === 'Dropdown') {\n const children = getJsxChildren(element) as OptionElement[];\n return children[0]?.props.value;\n }\n\n return null;\n}\n\n/**\n * Construct the state for an input field.\n *\n * @param oldState - The previous state.\n * @param element - The input element.\n * @returns The input state.\n */\nfunction constructInputState(\n oldState: InterfaceState,\n element: InputElement | DropdownElement,\n) {\n return (\n element.props.value ??\n oldState[element.props.name] ??\n constructComponentSpecificDefaultState(element) ??\n null\n );\n}\n\n/**\n * Construct the state for a form input.\n *\n * @param oldState - The previous state.\n * @param component - The input element.\n * @param form - The parent form name of the input.\n * @returns The input state.\n */\nfunction constructFormInputState(\n oldState: InterfaceState,\n component: InputElement | DropdownElement,\n form: string,\n) {\n const oldFormState = oldState[form] as FormState;\n const oldInputState = oldFormState?.[component.props.name];\n return (\n component.props.value ??\n oldInputState ??\n constructComponentSpecificDefaultState(component) ??\n null\n );\n}\n\n/**\n * Get the input field from a field element.\n *\n * @param element - The field element.\n * @returns The input element.\n */\nfunction getFieldInput(element: FieldElement) {\n if (Array.isArray(element.props.children)) {\n return element.props.children[0];\n }\n\n return element.props.children;\n}\n\n/**\n * Construct the state for a form input.\n *\n * @param oldState - The previous state.\n * @param component - The field element.\n * @param form - The parent form name of the input.\n * @param newState - The new state.\n * @returns The input state.\n */\nfunction constructFormState(\n oldState: InterfaceState,\n component: FieldElement | ButtonElement,\n form: string,\n newState: FormState,\n): FormState {\n if (component.type === 'Button') {\n return newState;\n }\n\n const input = getFieldInput(component);\n assertNameIsUnique(newState, input.props.name);\n\n newState[input.props.name] = constructFormInputState(oldState, input, form);\n\n return newState;\n}\n\n/**\n * Construct the interface state for a given component tree.\n *\n * @param oldState - The previous state.\n * @param component - The UI component to construct state from.\n * @param newState - The state that is being constructed.\n * @returns The interface state of the passed component.\n */\nexport function constructState(\n oldState: InterfaceState,\n component: JSXElement,\n newState: InterfaceState = {},\n): InterfaceState {\n if (component.type === 'Box') {\n const children = getJsxChildren(component);\n return children.reduce(\n (accumulator, node) =>\n constructState(oldState, node as JSXElement, accumulator),\n newState,\n );\n }\n\n if (component.type === 'Form') {\n assertNameIsUnique(newState, component.props.name);\n\n const children = getJsxChildren(component);\n newState[component.props.name] = children.reduce<FormState>(\n (accumulator, node) => {\n return constructFormState(\n oldState,\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion\n node as FieldElement | ButtonElement,\n component.props.name,\n accumulator,\n );\n },\n {},\n );\n\n return newState;\n }\n\n if (component.type === 'Input' || component.type === 'Dropdown') {\n assertNameIsUnique(newState, component.props.name);\n newState[component.props.name] = constructInputState(oldState, component);\n }\n\n return newState;\n}\n\nconst MAX_CONTEXT_SIZE = 1_000_000; // 1 mb\n\n/**\n * Validate a JSON blob to be used as the interface context.\n *\n * @param context - The JSON blob.\n * @throws If the JSON blob is too large.\n */\nexport function validateInterfaceContext(context?: InterfaceContext) {\n if (!context) {\n return;\n }\n\n // We assume the validity of this JSON to be validated by the caller.\n // E.g., in the RPC method implementation.\n const size = getJsonSizeUnsafe(context);\n assert(\n size <= MAX_CONTEXT_SIZE,\n `A Snap interface context may not be larger than ${\n MAX_CONTEXT_SIZE / 1000000\n } MB.`,\n );\n}\n"],"mappings":";AAAA,SAAS,cAAc;AAevB,SAAS,0BAA0B;AACnC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAUA,SAAS,gBAAgB,WAA2C;AACzE,MAAI,mBAAmB,SAAS,GAAG;AACjC,WAAO;AAAA,EACT;AAEA,SAAO,2BAA2B,SAAS;AAC7C;AAQO,SAAS,mBAAmB,OAAuB,MAAc;AACtE;AAAA,IACE,MAAM,IAAI,MAAM;AAAA,IAChB,4EAA4E,IAAI;AAAA,EAClF;AACF;AAWA,SAAS,uCACP,SACA;AACA,MAAI,QAAQ,SAAS,YAAY;AAC/B,UAAM,WAAW,eAAe,OAAO;AACvC,WAAO,SAAS,CAAC,GAAG,MAAM;AAAA,EAC5B;AAEA,SAAO;AACT;AASA,SAAS,oBACP,UACA,SACA;AACA,SACE,QAAQ,MAAM,SACd,SAAS,QAAQ,MAAM,IAAI,KAC3B,uCAAuC,OAAO,KAC9C;AAEJ;AAUA,SAAS,wBACP,UACA,WACA,MACA;AACA,QAAM,eA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T;AAEA,IAAM,mBAAmB;AAQlB,SAAS,yBAAyB,SAA4B;AACnE,MAAI,CAAC,SAAS;AACZ;AAAA,EACF;AAIA,QAAM,OAAO,kBAAkB,OAAO;AACtC;AAAA,IACE,QAAQ;AAAA,IACR,mDACE,mBAAmB,GACrB;AAAA,EACF;AACF;","names":[]}
|
package/dist/chunk-WCDDYBFW.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/interface/utils.ts
|
|
2
|
-
var _snapssdk = require('@metamask/snaps-sdk');
|
|
3
|
-
var _jsx = require('@metamask/snaps-sdk/jsx');
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var _snapsutils = require('@metamask/snaps-utils');
|
|
9
|
-
function getJsxInterface(component) {
|
|
10
|
-
if (_jsx.isJSXElementUnsafe.call(void 0, component)) {
|
|
11
|
-
return component;
|
|
12
|
-
}
|
|
13
|
-
return _snapsutils.getJsxElementFromComponent.call(void 0, component);
|
|
14
|
-
}
|
|
15
|
-
function assertNameIsUnique(state, name) {
|
|
16
|
-
_snapssdk.assert.call(void 0,
|
|
17
|
-
state[name] === void 0,
|
|
18
|
-
`Duplicate component names are not allowed, found multiple instances of: "${name}".`
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
function constructComponentSpecificDefaultState(element) {
|
|
22
|
-
if (element.type === "Dropdown") {
|
|
23
|
-
const children = _snapsutils.getJsxChildren.call(void 0, element);
|
|
24
|
-
return children[0]?.props.value;
|
|
25
|
-
}
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
function constructInputState(oldState, element) {
|
|
29
|
-
return element.props.value ?? oldState[element.props.name] ?? constructComponentSpecificDefaultState(element) ?? null;
|
|
30
|
-
}
|
|
31
|
-
function constructFormInputState(oldState, component, form) {
|
|
32
|
-
const oldFormState = oldState[form];
|
|
33
|
-
const oldInputState = oldFormState?.[component.props.name];
|
|
34
|
-
return component.props.value ?? oldInputState ?? constructComponentSpecificDefaultState(component) ?? null;
|
|
35
|
-
}
|
|
36
|
-
function getFieldInput(element) {
|
|
37
|
-
if (Array.isArray(element.props.children)) {
|
|
38
|
-
return element.props.children[0];
|
|
39
|
-
}
|
|
40
|
-
return element.props.children;
|
|
41
|
-
}
|
|
42
|
-
function constructFormState(oldState, component, form, newState) {
|
|
43
|
-
if (component.type === "Button") {
|
|
44
|
-
return newState;
|
|
45
|
-
}
|
|
46
|
-
const input = getFieldInput(component);
|
|
47
|
-
assertNameIsUnique(newState, input.props.name);
|
|
48
|
-
newState[input.props.name] = constructFormInputState(oldState, input, form);
|
|
49
|
-
return newState;
|
|
50
|
-
}
|
|
51
|
-
function constructState(oldState, component, newState = {}) {
|
|
52
|
-
if (component.type === "Box") {
|
|
53
|
-
const children = _snapsutils.getJsxChildren.call(void 0, component);
|
|
54
|
-
return children.reduce(
|
|
55
|
-
(accumulator, node) => constructState(oldState, node, accumulator),
|
|
56
|
-
newState
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
if (component.type === "Form") {
|
|
60
|
-
assertNameIsUnique(newState, component.props.name);
|
|
61
|
-
const children = _snapsutils.getJsxChildren.call(void 0, component);
|
|
62
|
-
newState[component.props.name] = children.reduce(
|
|
63
|
-
(accumulator, node) => {
|
|
64
|
-
return constructFormState(
|
|
65
|
-
oldState,
|
|
66
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
67
|
-
node,
|
|
68
|
-
component.props.name,
|
|
69
|
-
accumulator
|
|
70
|
-
);
|
|
71
|
-
},
|
|
72
|
-
{}
|
|
73
|
-
);
|
|
74
|
-
return newState;
|
|
75
|
-
}
|
|
76
|
-
if (component.type === "Input" || component.type === "Dropdown") {
|
|
77
|
-
assertNameIsUnique(newState, component.props.name);
|
|
78
|
-
newState[component.props.name] = constructInputState(oldState, component);
|
|
79
|
-
}
|
|
80
|
-
return newState;
|
|
81
|
-
}
|
|
82
|
-
var MAX_CONTEXT_SIZE = 1e6;
|
|
83
|
-
function validateInterfaceContext(context) {
|
|
84
|
-
if (!context) {
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
const size = _snapsutils.getJsonSizeUnsafe.call(void 0, context);
|
|
88
|
-
_snapssdk.assert.call(void 0,
|
|
89
|
-
size <= MAX_CONTEXT_SIZE,
|
|
90
|
-
`A Snap interface context may not be larger than ${MAX_CONTEXT_SIZE / 1e6} MB.`
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
exports.getJsxInterface = getJsxInterface; exports.assertNameIsUnique = assertNameIsUnique; exports.constructState = constructState; exports.validateInterfaceContext = validateInterfaceContext;
|
|
100
|
-
//# sourceMappingURL=chunk-WCDDYBFW.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/interface/utils.ts"],"names":[],"mappings":";AAAA,SAAS,cAAc;AAevB,SAAS,0BAA0B;AACnC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAUA,SAAS,gBAAgB,WAA2C;AACzE,MAAI,mBAAmB,SAAS,GAAG;AACjC,WAAO;AAAA,EACT;AAEA,SAAO,2BAA2B,SAAS;AAC7C;AAQO,SAAS,mBAAmB,OAAuB,MAAc;AACtE;AAAA,IACE,MAAM,IAAI,MAAM;AAAA,IAChB,4EAA4E,IAAI;AAAA,EAClF;AACF;AAWA,SAAS,uCACP,SACA;AACA,MAAI,QAAQ,SAAS,YAAY;AAC/B,UAAM,WAAW,eAAe,OAAO;AACvC,WAAO,SAAS,CAAC,GAAG,MAAM;AAAA,EAC5B;AAEA,SAAO;AACT;AASA,SAAS,oBACP,UACA,SACA;AACA,SACE,QAAQ,MAAM,SACd,SAAS,QAAQ,MAAM,IAAI,KAC3B,uCAAuC,OAAO,KAC9C;AAEJ;AAUA,SAAS,wBACP,UACA,WACA,MACA;A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EA,SAAO;AACT;AAEA,IAAM,mBAAmB;AAQlB,SAAS,yBAAyB,SAA4B;AACnE,MAAI,CAAC,SAAS;AACZ;AAAA,EACF;AAIA,QAAM,OAAO,kBAAkB,OAAO;AACtC;AAAA,IACE,QAAQ;AAAA,IACR,mDACE,mBAAmB,GACrB;AAAA,EACF;AACF","sourcesContent":["import { assert } from '@metamask/snaps-sdk';\nimport type {\n FormState,\n InterfaceState,\n ComponentOrElement,\n InterfaceContext,\n} from '@metamask/snaps-sdk';\nimport type {\n ButtonElement,\n DropdownElement,\n FieldElement,\n InputElement,\n JSXElement,\n OptionElement,\n} from '@metamask/snaps-sdk/jsx';\nimport { isJSXElementUnsafe } from '@metamask/snaps-sdk/jsx';\nimport {\n getJsonSizeUnsafe,\n getJsxChildren,\n getJsxElementFromComponent,\n} from '@metamask/snaps-utils';\n\n/**\n * Get a JSX element from a component or JSX element. If the component is a\n * JSX element, it is returned as is. Otherwise, the component is converted to\n * a JSX element.\n *\n * @param component - The component to convert.\n * @returns The JSX element.\n */\nexport function getJsxInterface(component: ComponentOrElement): JSXElement {\n if (isJSXElementUnsafe(component)) {\n return component;\n }\n\n return getJsxElementFromComponent(component);\n}\n\n/**\n * Assert that the component name is unique in state.\n *\n * @param state - The interface state to verify against.\n * @param name - The component name to verify.\n */\nexport function assertNameIsUnique(state: InterfaceState, name: string) {\n assert(\n state[name] === undefined,\n `Duplicate component names are not allowed, found multiple instances of: \"${name}\".`,\n );\n}\n\n/**\n * Construct default state for a component.\n *\n * This function is meant to be used inside constructInputState to account\n * for component specific defaults and will not override the component value or existing form state.\n *\n * @param element - The input element.\n * @returns The default state for the specific component, if any.\n */\nfunction constructComponentSpecificDefaultState(\n element: InputElement | DropdownElement,\n) {\n if (element.type === 'Dropdown') {\n const children = getJsxChildren(element) as OptionElement[];\n return children[0]?.props.value;\n }\n\n return null;\n}\n\n/**\n * Construct the state for an input field.\n *\n * @param oldState - The previous state.\n * @param element - The input element.\n * @returns The input state.\n */\nfunction constructInputState(\n oldState: InterfaceState,\n element: InputElement | DropdownElement,\n) {\n return (\n element.props.value ??\n oldState[element.props.name] ??\n constructComponentSpecificDefaultState(element) ??\n null\n );\n}\n\n/**\n * Construct the state for a form input.\n *\n * @param oldState - The previous state.\n * @param component - The input element.\n * @param form - The parent form name of the input.\n * @returns The input state.\n */\nfunction constructFormInputState(\n oldState: InterfaceState,\n component: InputElement | DropdownElement,\n form: string,\n) {\n const oldFormState = oldState[form] as FormState;\n const oldInputState = oldFormState?.[component.props.name];\n return (\n component.props.value ??\n oldInputState ??\n constructComponentSpecificDefaultState(component) ??\n null\n );\n}\n\n/**\n * Get the input field from a field element.\n *\n * @param element - The field element.\n * @returns The input element.\n */\nfunction getFieldInput(element: FieldElement) {\n if (Array.isArray(element.props.children)) {\n return element.props.children[0];\n }\n\n return element.props.children;\n}\n\n/**\n * Construct the state for a form input.\n *\n * @param oldState - The previous state.\n * @param component - The field element.\n * @param form - The parent form name of the input.\n * @param newState - The new state.\n * @returns The input state.\n */\nfunction constructFormState(\n oldState: InterfaceState,\n component: FieldElement | ButtonElement,\n form: string,\n newState: FormState,\n): FormState {\n if (component.type === 'Button') {\n return newState;\n }\n\n const input = getFieldInput(component);\n assertNameIsUnique(newState, input.props.name);\n\n newState[input.props.name] = constructFormInputState(oldState, input, form);\n\n return newState;\n}\n\n/**\n * Construct the interface state for a given component tree.\n *\n * @param oldState - The previous state.\n * @param component - The UI component to construct state from.\n * @param newState - The state that is being constructed.\n * @returns The interface state of the passed component.\n */\nexport function constructState(\n oldState: InterfaceState,\n component: JSXElement,\n newState: InterfaceState = {},\n): InterfaceState {\n if (component.type === 'Box') {\n const children = getJsxChildren(component);\n return children.reduce(\n (accumulator, node) =>\n constructState(oldState, node as JSXElement, accumulator),\n newState,\n );\n }\n\n if (component.type === 'Form') {\n assertNameIsUnique(newState, component.props.name);\n\n const children = getJsxChildren(component);\n newState[component.props.name] = children.reduce<FormState>(\n (accumulator, node) => {\n return constructFormState(\n oldState,\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion\n node as FieldElement | ButtonElement,\n component.props.name,\n accumulator,\n );\n },\n {},\n );\n\n return newState;\n }\n\n if (component.type === 'Input' || component.type === 'Dropdown') {\n assertNameIsUnique(newState, component.props.name);\n newState[component.props.name] = constructInputState(oldState, component);\n }\n\n return newState;\n}\n\nconst MAX_CONTEXT_SIZE = 1_000_000; // 1 mb\n\n/**\n * Validate a JSON blob to be used as the interface context.\n *\n * @param context - The JSON blob.\n * @throws If the JSON blob is too large.\n */\nexport function validateInterfaceContext(context?: InterfaceContext) {\n if (!context) {\n return;\n }\n\n // We assume the validity of this JSON to be validated by the caller.\n // E.g., in the RPC method implementation.\n const size = getJsonSizeUnsafe(context);\n assert(\n size <= MAX_CONTEXT_SIZE,\n `A Snap interface context may not be larger than ${\n MAX_CONTEXT_SIZE / 1000000\n } MB.`,\n );\n}\n"]}
|
|
File without changes
|
|
File without changes
|