@metamask/snaps-controllers 9.9.0 → 9.10.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 +9 -1
- package/dist/cronjob/CronjobController.cjs +34 -21
- package/dist/cronjob/CronjobController.cjs.map +1 -1
- package/dist/cronjob/CronjobController.mjs +34 -21
- package/dist/cronjob/CronjobController.mjs.map +1 -1
- package/dist/insights/SnapInsightsController.cjs +144 -194
- package/dist/insights/SnapInsightsController.cjs.map +1 -1
- package/dist/insights/SnapInsightsController.mjs +143 -193
- package/dist/insights/SnapInsightsController.mjs.map +1 -1
- package/dist/interface/SnapInterfaceController.cjs +65 -90
- package/dist/interface/SnapInterfaceController.cjs.map +1 -1
- package/dist/interface/SnapInterfaceController.mjs +65 -90
- package/dist/interface/SnapInterfaceController.mjs.map +1 -1
- package/dist/interface/utils.cjs +6 -2
- package/dist/interface/utils.cjs.map +1 -1
- package/dist/interface/utils.d.cts.map +1 -1
- package/dist/interface/utils.d.mts.map +1 -1
- package/dist/interface/utils.mjs +6 -2
- package/dist/interface/utils.mjs.map +1 -1
- package/dist/services/AbstractExecutionService.cjs +77 -71
- package/dist/services/AbstractExecutionService.cjs.map +1 -1
- package/dist/services/AbstractExecutionService.mjs +77 -71
- package/dist/services/AbstractExecutionService.mjs.map +1 -1
- package/dist/services/ProxyPostMessageStream.cjs +26 -19
- package/dist/services/ProxyPostMessageStream.cjs.map +1 -1
- package/dist/services/ProxyPostMessageStream.mjs +26 -19
- package/dist/services/ProxyPostMessageStream.mjs.map +1 -1
- package/dist/services/iframe/IframeExecutionService.cjs +4 -2
- package/dist/services/iframe/IframeExecutionService.cjs.map +1 -1
- package/dist/services/iframe/IframeExecutionService.d.cts.map +1 -1
- package/dist/services/iframe/IframeExecutionService.d.mts.map +1 -1
- package/dist/services/iframe/IframeExecutionService.mjs +4 -2
- package/dist/services/iframe/IframeExecutionService.mjs.map +1 -1
- package/dist/services/offscreen/OffscreenExecutionService.cjs +16 -3
- package/dist/services/offscreen/OffscreenExecutionService.cjs.map +1 -1
- package/dist/services/offscreen/OffscreenExecutionService.mjs +16 -3
- package/dist/services/offscreen/OffscreenExecutionService.mjs.map +1 -1
- package/dist/services/proxy/ProxyExecutionService.cjs +17 -4
- package/dist/services/proxy/ProxyExecutionService.cjs.map +1 -1
- package/dist/services/proxy/ProxyExecutionService.mjs +17 -4
- package/dist/services/proxy/ProxyExecutionService.mjs.map +1 -1
- package/dist/services/webview/WebViewExecutionService.cjs +23 -9
- package/dist/services/webview/WebViewExecutionService.cjs.map +1 -1
- package/dist/services/webview/WebViewExecutionService.mjs +23 -9
- package/dist/services/webview/WebViewExecutionService.mjs.map +1 -1
- package/dist/services/webview/WebViewMessageStream.cjs +25 -12
- package/dist/services/webview/WebViewMessageStream.cjs.map +1 -1
- package/dist/services/webview/WebViewMessageStream.mjs +25 -12
- package/dist/services/webview/WebViewMessageStream.mjs.map +1 -1
- package/dist/services/webworker/WebWorkerExecutionService.cjs +27 -10
- package/dist/services/webworker/WebWorkerExecutionService.cjs.map +1 -1
- package/dist/services/webworker/WebWorkerExecutionService.mjs +27 -10
- package/dist/services/webworker/WebWorkerExecutionService.mjs.map +1 -1
- package/dist/snaps/RequestQueue.cjs +0 -2
- package/dist/snaps/RequestQueue.cjs.map +1 -1
- package/dist/snaps/RequestQueue.mjs +0 -2
- package/dist/snaps/RequestQueue.mjs.map +1 -1
- package/dist/snaps/SnapController.cjs +1001 -1141
- package/dist/snaps/SnapController.cjs.map +1 -1
- package/dist/snaps/SnapController.mjs +1000 -1140
- package/dist/snaps/SnapController.mjs.map +1 -1
- package/dist/snaps/Timer.cjs +0 -1
- package/dist/snaps/Timer.cjs.map +1 -1
- package/dist/snaps/Timer.mjs +0 -1
- package/dist/snaps/Timer.mjs.map +1 -1
- package/dist/snaps/location/http.cjs +7 -11
- package/dist/snaps/location/http.cjs.map +1 -1
- package/dist/snaps/location/http.mjs +7 -11
- package/dist/snaps/location/http.mjs.map +1 -1
- package/dist/snaps/location/local.cjs +17 -4
- package/dist/snaps/location/local.cjs.map +1 -1
- package/dist/snaps/location/local.mjs +17 -4
- package/dist/snaps/location/local.mjs.map +1 -1
- package/dist/snaps/location/npm.cjs +37 -25
- package/dist/snaps/location/npm.cjs.map +1 -1
- package/dist/snaps/location/npm.mjs +37 -25
- package/dist/snaps/location/npm.mjs.map +1 -1
- package/dist/snaps/registry/json.cjs +173 -172
- package/dist/snaps/registry/json.cjs.map +1 -1
- package/dist/snaps/registry/json.mjs +172 -171
- package/dist/snaps/registry/json.mjs.map +1 -1
- package/package.json +5 -5
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
7
|
+
var _SnapInterfaceController_instances, _SnapInterfaceController_registerMessageHandlers, _SnapInterfaceController_validateArgs, _SnapInterfaceController_validateApproval, _SnapInterfaceController_triggerPhishingListUpdate, _SnapInterfaceController_checkPhishingList, _SnapInterfaceController_hasApprovalRequest, _SnapInterfaceController_acceptApprovalRequest, _SnapInterfaceController_validateContent;
|
|
2
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
9
|
exports.SnapInterfaceController = void 0;
|
|
4
10
|
const base_controller_1 = require("@metamask/base-controller");
|
|
@@ -22,19 +28,8 @@ class SnapInterfaceController extends base_controller_1.BaseController {
|
|
|
22
28
|
name: controllerName,
|
|
23
29
|
state: { interfaces: {}, ...state },
|
|
24
30
|
});
|
|
25
|
-
this
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Constructor helper for registering this controller's messaging system
|
|
29
|
-
* actions.
|
|
30
|
-
*/
|
|
31
|
-
#registerMessageHandlers() {
|
|
32
|
-
this.messagingSystem.registerActionHandler(`${controllerName}:createInterface`, this.createInterface.bind(this));
|
|
33
|
-
this.messagingSystem.registerActionHandler(`${controllerName}:getInterface`, this.getInterface.bind(this));
|
|
34
|
-
this.messagingSystem.registerActionHandler(`${controllerName}:updateInterface`, this.updateInterface.bind(this));
|
|
35
|
-
this.messagingSystem.registerActionHandler(`${controllerName}:deleteInterface`, this.deleteInterface.bind(this));
|
|
36
|
-
this.messagingSystem.registerActionHandler(`${controllerName}:updateInterfaceState`, this.updateInterfaceState.bind(this));
|
|
37
|
-
this.messagingSystem.registerActionHandler(`${controllerName}:resolveInterface`, this.resolveInterface.bind(this));
|
|
31
|
+
_SnapInterfaceController_instances.add(this);
|
|
32
|
+
__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_registerMessageHandlers).call(this);
|
|
38
33
|
}
|
|
39
34
|
/**
|
|
40
35
|
* Create an interface in the controller state with the associated data.
|
|
@@ -46,7 +41,7 @@ class SnapInterfaceController extends base_controller_1.BaseController {
|
|
|
46
41
|
*/
|
|
47
42
|
async createInterface(snapId, content, context) {
|
|
48
43
|
const element = (0, utils_2.getJsxInterface)(content);
|
|
49
|
-
await this
|
|
44
|
+
await __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateContent).call(this, element);
|
|
50
45
|
(0, utils_2.validateInterfaceContext)(context);
|
|
51
46
|
const id = (0, nanoid_1.nanoid)();
|
|
52
47
|
const componentState = (0, utils_2.constructState)({}, element);
|
|
@@ -70,7 +65,7 @@ class SnapInterfaceController extends base_controller_1.BaseController {
|
|
|
70
65
|
* @returns The interface state.
|
|
71
66
|
*/
|
|
72
67
|
getInterface(snapId, id) {
|
|
73
|
-
this
|
|
68
|
+
__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateArgs).call(this, snapId, id);
|
|
74
69
|
return this.state.interfaces[id];
|
|
75
70
|
}
|
|
76
71
|
/**
|
|
@@ -81,9 +76,9 @@ class SnapInterfaceController extends base_controller_1.BaseController {
|
|
|
81
76
|
* @param content - The new content.
|
|
82
77
|
*/
|
|
83
78
|
async updateInterface(snapId, id, content) {
|
|
84
|
-
this
|
|
79
|
+
__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateArgs).call(this, snapId, id);
|
|
85
80
|
const element = (0, utils_2.getJsxInterface)(content);
|
|
86
|
-
await this
|
|
81
|
+
await __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateContent).call(this, element);
|
|
87
82
|
const oldState = this.state.interfaces[id].state;
|
|
88
83
|
const newState = (0, utils_2.constructState)(oldState, element);
|
|
89
84
|
this.update((draftState) => {
|
|
@@ -121,81 +116,61 @@ class SnapInterfaceController extends base_controller_1.BaseController {
|
|
|
121
116
|
* @param value - The value to resolve the promise with.
|
|
122
117
|
*/
|
|
123
118
|
async resolveInterface(snapId, id, value) {
|
|
124
|
-
this
|
|
125
|
-
this
|
|
126
|
-
await this
|
|
119
|
+
__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateArgs).call(this, snapId, id);
|
|
120
|
+
__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateApproval).call(this, id);
|
|
121
|
+
await __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_acceptApprovalRequest).call(this, id, value);
|
|
127
122
|
this.deleteInterface(id);
|
|
128
123
|
}
|
|
129
|
-
/**
|
|
130
|
-
* Utility function to validate the args passed to the other methods.
|
|
131
|
-
*
|
|
132
|
-
* @param snapId - The snap id.
|
|
133
|
-
* @param id - The interface id.
|
|
134
|
-
*/
|
|
135
|
-
#validateArgs(snapId, id) {
|
|
136
|
-
const existingInterface = this.state.interfaces[id];
|
|
137
|
-
(0, utils_1.assert)(existingInterface !== undefined, `Interface with id '${id}' not found.`);
|
|
138
|
-
(0, utils_1.assert)(existingInterface.snapId === snapId, `Interface not created by ${snapId}.`);
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Utility function to validate that the approval request exists.
|
|
142
|
-
*
|
|
143
|
-
* @param id - The interface id.
|
|
144
|
-
*/
|
|
145
|
-
#validateApproval(id) {
|
|
146
|
-
(0, utils_1.assert)(this.#hasApprovalRequest(id), `Approval request with id '${id}' not found.`);
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Trigger a Phishing list update if needed.
|
|
150
|
-
*/
|
|
151
|
-
async #triggerPhishingListUpdate() {
|
|
152
|
-
await this.messagingSystem.call('PhishingController:maybeUpdateState');
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Check an origin against the phishing list.
|
|
156
|
-
*
|
|
157
|
-
* @param origin - The origin to check.
|
|
158
|
-
* @returns True if the origin is on the phishing list, otherwise false.
|
|
159
|
-
*/
|
|
160
|
-
#checkPhishingList(origin) {
|
|
161
|
-
return this.messagingSystem.call('PhishingController:testOrigin', origin)
|
|
162
|
-
.result;
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Check if an approval request exists for a given interface by looking up
|
|
166
|
-
* if the ApprovalController has a request with the given interface ID.
|
|
167
|
-
*
|
|
168
|
-
* @param id - The interface id.
|
|
169
|
-
* @returns True if an approval request exists, otherwise false.
|
|
170
|
-
*/
|
|
171
|
-
#hasApprovalRequest(id) {
|
|
172
|
-
return this.messagingSystem.call('ApprovalController:hasRequest', {
|
|
173
|
-
id,
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Accept an approval request for a given interface.
|
|
178
|
-
*
|
|
179
|
-
* @param id - The interface id.
|
|
180
|
-
* @param value - The value to resolve the promise with.
|
|
181
|
-
*/
|
|
182
|
-
async #acceptApprovalRequest(id, value) {
|
|
183
|
-
await this.messagingSystem.call('ApprovalController:acceptRequest', id, value);
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* Utility function to validate the components of an interface.
|
|
187
|
-
* Throws if something is invalid.
|
|
188
|
-
*
|
|
189
|
-
* @param element - The JSX element to verify.
|
|
190
|
-
*/
|
|
191
|
-
async #validateContent(element) {
|
|
192
|
-
// We assume the validity of this JSON to be validated by the caller.
|
|
193
|
-
// E.g., in the RPC method implementation.
|
|
194
|
-
const size = (0, snaps_utils_1.getJsonSizeUnsafe)(element);
|
|
195
|
-
(0, utils_1.assert)(size <= MAX_UI_CONTENT_SIZE, `A Snap UI may not be larger than ${MAX_UI_CONTENT_SIZE / 1000000} MB.`);
|
|
196
|
-
await this.#triggerPhishingListUpdate();
|
|
197
|
-
(0, snaps_utils_1.validateJsxLinks)(element, this.#checkPhishingList.bind(this), (id) => this.messagingSystem.call('SnapController:get', id));
|
|
198
|
-
}
|
|
199
124
|
}
|
|
200
125
|
exports.SnapInterfaceController = SnapInterfaceController;
|
|
126
|
+
_SnapInterfaceController_instances = new WeakSet(), _SnapInterfaceController_registerMessageHandlers = function _SnapInterfaceController_registerMessageHandlers() {
|
|
127
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:createInterface`, this.createInterface.bind(this));
|
|
128
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:getInterface`, this.getInterface.bind(this));
|
|
129
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:updateInterface`, this.updateInterface.bind(this));
|
|
130
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:deleteInterface`, this.deleteInterface.bind(this));
|
|
131
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:updateInterfaceState`, this.updateInterfaceState.bind(this));
|
|
132
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:resolveInterface`, this.resolveInterface.bind(this));
|
|
133
|
+
}, _SnapInterfaceController_validateArgs = function _SnapInterfaceController_validateArgs(snapId, id) {
|
|
134
|
+
const existingInterface = this.state.interfaces[id];
|
|
135
|
+
(0, utils_1.assert)(existingInterface !== undefined, `Interface with id '${id}' not found.`);
|
|
136
|
+
(0, utils_1.assert)(existingInterface.snapId === snapId, `Interface not created by ${snapId}.`);
|
|
137
|
+
}, _SnapInterfaceController_validateApproval = function _SnapInterfaceController_validateApproval(id) {
|
|
138
|
+
(0, utils_1.assert)(__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_hasApprovalRequest).call(this, id), `Approval request with id '${id}' not found.`);
|
|
139
|
+
}, _SnapInterfaceController_triggerPhishingListUpdate =
|
|
140
|
+
/**
|
|
141
|
+
* Trigger a Phishing list update if needed.
|
|
142
|
+
*/
|
|
143
|
+
async function _SnapInterfaceController_triggerPhishingListUpdate() {
|
|
144
|
+
await this.messagingSystem.call('PhishingController:maybeUpdateState');
|
|
145
|
+
}, _SnapInterfaceController_checkPhishingList = function _SnapInterfaceController_checkPhishingList(origin) {
|
|
146
|
+
return this.messagingSystem.call('PhishingController:testOrigin', origin)
|
|
147
|
+
.result;
|
|
148
|
+
}, _SnapInterfaceController_hasApprovalRequest = function _SnapInterfaceController_hasApprovalRequest(id) {
|
|
149
|
+
return this.messagingSystem.call('ApprovalController:hasRequest', {
|
|
150
|
+
id,
|
|
151
|
+
});
|
|
152
|
+
}, _SnapInterfaceController_acceptApprovalRequest =
|
|
153
|
+
/**
|
|
154
|
+
* Accept an approval request for a given interface.
|
|
155
|
+
*
|
|
156
|
+
* @param id - The interface id.
|
|
157
|
+
* @param value - The value to resolve the promise with.
|
|
158
|
+
*/
|
|
159
|
+
async function _SnapInterfaceController_acceptApprovalRequest(id, value) {
|
|
160
|
+
await this.messagingSystem.call('ApprovalController:acceptRequest', id, value);
|
|
161
|
+
}, _SnapInterfaceController_validateContent =
|
|
162
|
+
/**
|
|
163
|
+
* Utility function to validate the components of an interface.
|
|
164
|
+
* Throws if something is invalid.
|
|
165
|
+
*
|
|
166
|
+
* @param element - The JSX element to verify.
|
|
167
|
+
*/
|
|
168
|
+
async function _SnapInterfaceController_validateContent(element) {
|
|
169
|
+
// We assume the validity of this JSON to be validated by the caller.
|
|
170
|
+
// E.g., in the RPC method implementation.
|
|
171
|
+
const size = (0, snaps_utils_1.getJsonSizeUnsafe)(element);
|
|
172
|
+
(0, utils_1.assert)(size <= MAX_UI_CONTENT_SIZE, `A Snap UI may not be larger than ${MAX_UI_CONTENT_SIZE / 1000000} MB.`);
|
|
173
|
+
await __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_triggerPhishingListUpdate).call(this);
|
|
174
|
+
(0, snaps_utils_1.validateJsxLinks)(element, __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_checkPhishingList).bind(this), (id) => this.messagingSystem.call('SnapController:get', id));
|
|
175
|
+
};
|
|
201
176
|
//# sourceMappingURL=SnapInterfaceController.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapInterfaceController.cjs","sourceRoot":"","sources":["../../src/interface/SnapInterfaceController.ts"],"names":[],"mappings":";;;AASA,+DAA2D;AAY3D,uDAA4E;AAE5E,2CAAyC;AACzC,iCAAkC;AAClC,mCAAgC;AAGhC,uCAIiB;AAEjB,MAAM,mBAAmB,GAAG,QAAU,CAAC,CAAC,QAAQ;AAEhD,MAAM,cAAc,GAAG,yBAAyB,CAAC;AAsFjD;;GAEG;AACH,MAAa,uBAAwB,SAAQ,gCAI5C;IACC,YAAY,EAAE,SAAS,EAAE,KAAK,EAA+B;QAC3D,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE;gBACR,UAAU,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE;aACjD;YACD,IAAI,EAAE,cAAc;YACpB,KAAK,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,GAAG,KAAK,EAAE;SACpC,CAAC,CAAC;QAEH,IAAI,CAAC,wBAAwB,EAAE,CAAC;IAClC,CAAC;IAED;;;OAGG;IACH,wBAAwB;QACtB,IAAI,CAAC,eAAe,C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B,CAAC,MAAc;QAC/B,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,+BAA+B,EAAE,MAAM,CAAC;aACtE,MAAM,CAAC;IACZ,CAAC;IAED;;;;;;OAMG;IACH,mBAAmB,CAAC,EAAU;QAC5B,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,+BAA+B,EAAE;YAChE,EAAE;SACH,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,sBAAsB,CAAC,EAAU,EAAE,KAAW;QAClD,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAC7B,kCAAkC,EAClC,EAAE,EACF,KAAK,CACN,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,gBAAgB,CAAC,OAAmB;QACxC,qEAAqE;QACrE,0CAA0C;QAC1C,MAAM,IAAI,GAAG,IAAA,+BAAiB,EAAC,OAAO,CAAC,CAAC;QACxC,IAAA,cAAM,EACJ,IAAI,IAAI,mBAAmB,EAC3B,oCAAoC,mBAAmB,GAAG,OAAO,MAAM,CACxE,CAAC;QAEF,MAAM,IAAI,CAAC,0BAA0B,EAAE,CAAC;QACxC,IAAA,8BAAgB,EACd,OAAO,EACP,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,EAClC,CAAC,EAAU,EAAE,EAAE,CAAC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,oBAAoB,EAAE,EAAE,CAAC,CACpE,CAAC;IACJ,CAAC;CACF;AAxQD,0DAwQC","sourcesContent":["import type {\n AcceptRequest,\n HasApprovalRequest,\n} from '@metamask/approval-controller';\nimport type {\n RestrictedControllerMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n MaybeUpdateState,\n TestOrigin,\n} from '@metamask/phishing-controller';\nimport type {\n InterfaceState,\n SnapId,\n ComponentOrElement,\n InterfaceContext,\n} from '@metamask/snaps-sdk';\nimport type { JSXElement } from '@metamask/snaps-sdk/jsx';\nimport { getJsonSizeUnsafe, validateJsxLinks } from '@metamask/snaps-utils';\nimport type { Json } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\nimport { castDraft } from 'immer';\nimport { nanoid } from 'nanoid';\n\nimport type { GetSnap } from '../snaps';\nimport {\n constructState,\n getJsxInterface,\n validateInterfaceContext,\n} from './utils';\n\nconst MAX_UI_CONTENT_SIZE = 10_000_000; // 10 mb\n\nconst controllerName = 'SnapInterfaceController';\n\nexport type CreateInterface = {\n type: `${typeof controllerName}:createInterface`;\n handler: SnapInterfaceController['createInterface'];\n};\n\nexport type GetInterface = {\n type: `${typeof controllerName}:getInterface`;\n handler: SnapInterfaceController['getInterface'];\n};\n\nexport type UpdateInterface = {\n type: `${typeof controllerName}:updateInterface`;\n handler: SnapInterfaceController['updateInterface'];\n};\n\nexport type DeleteInterface = {\n type: `${typeof controllerName}:deleteInterface`;\n handler: SnapInterfaceController['deleteInterface'];\n};\n\nexport type UpdateInterfaceState = {\n type: `${typeof controllerName}:updateInterfaceState`;\n handler: SnapInterfaceController['updateInterfaceState'];\n};\n\nexport type ResolveInterface = {\n type: `${typeof controllerName}:resolveInterface`;\n handler: SnapInterfaceController['resolveInterface'];\n};\n\nexport type SnapInterfaceControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SnapInterfaceControllerState\n>;\n\nexport type SnapInterfaceControllerAllowedActions =\n | TestOrigin\n | MaybeUpdateState\n | HasApprovalRequest\n | AcceptRequest\n | GetSnap;\n\nexport type SnapInterfaceControllerActions =\n | CreateInterface\n | GetInterface\n | UpdateInterface\n | DeleteInterface\n | UpdateInterfaceState\n | ResolveInterface\n | SnapInterfaceControllerGetStateAction;\n\nexport type SnapInterfaceControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n SnapInterfaceControllerState\n >;\n\nexport type SnapInterfaceControllerEvents =\n SnapInterfaceControllerStateChangeEvent;\n\nexport type SnapInterfaceControllerMessenger = RestrictedControllerMessenger<\n typeof controllerName,\n SnapInterfaceControllerActions | SnapInterfaceControllerAllowedActions,\n SnapInterfaceControllerEvents,\n SnapInterfaceControllerAllowedActions['type'],\n SnapInterfaceControllerEvents['type']\n>;\n\nexport type StoredInterface = {\n snapId: SnapId;\n content: JSXElement;\n state: InterfaceState;\n context: InterfaceContext | null;\n};\n\nexport type SnapInterfaceControllerState = {\n interfaces: Record<string, StoredInterface>;\n};\n\nexport type SnapInterfaceControllerArgs = {\n messenger: SnapInterfaceControllerMessenger;\n state?: SnapInterfaceControllerState;\n};\n\n/**\n * Use this controller to manage snaps UI interfaces using RPC method hooks.\n */\nexport class SnapInterfaceController extends BaseController<\n typeof controllerName,\n SnapInterfaceControllerState,\n SnapInterfaceControllerMessenger\n> {\n constructor({ messenger, state }: SnapInterfaceControllerArgs) {\n super({\n messenger,\n metadata: {\n interfaces: { persist: false, anonymous: false },\n },\n name: controllerName,\n state: { interfaces: {}, ...state },\n });\n\n this.#registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering this controller's messaging system\n * actions.\n */\n #registerMessageHandlers() {\n this.messagingSystem.registerActionHandler(\n `${controllerName}:createInterface`,\n this.createInterface.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:getInterface`,\n this.getInterface.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:updateInterface`,\n this.updateInterface.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:deleteInterface`,\n this.deleteInterface.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:updateInterfaceState`,\n this.updateInterfaceState.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:resolveInterface`,\n this.resolveInterface.bind(this),\n );\n }\n\n /**\n * Create an interface in the controller state with the associated data.\n *\n * @param snapId - The snap id that created the interface.\n * @param content - The interface content.\n * @param context - An optional interface context object.\n * @returns The newly interface id.\n */\n async createInterface(\n snapId: SnapId,\n content: ComponentOrElement,\n context?: InterfaceContext,\n ) {\n const element = getJsxInterface(content);\n await this.#validateContent(element);\n validateInterfaceContext(context);\n\n const id = nanoid();\n const componentState = constructState({}, element);\n\n this.update((draftState) => {\n // @ts-expect-error - TS2589: Type instantiation is excessively deep and\n // possibly infinite.\n draftState.interfaces[id] = {\n snapId,\n content: castDraft(element),\n state: componentState,\n context: context ?? null,\n };\n });\n\n return id;\n }\n\n /**\n * Get the data of a given interface id.\n *\n * @param snapId - The snap id requesting the interface data.\n * @param id - The interface id.\n * @returns The interface state.\n */\n getInterface(snapId: SnapId, id: string) {\n this.#validateArgs(snapId, id);\n\n return this.state.interfaces[id];\n }\n\n /**\n * Update the interface with the given content.\n *\n * @param snapId - The snap id requesting the update.\n * @param id - The interface id.\n * @param content - The new content.\n */\n async updateInterface(\n snapId: SnapId,\n id: string,\n content: ComponentOrElement,\n ) {\n this.#validateArgs(snapId, id);\n const element = getJsxInterface(content);\n await this.#validateContent(element);\n\n const oldState = this.state.interfaces[id].state;\n const newState = constructState(oldState, element);\n\n this.update((draftState) => {\n draftState.interfaces[id].state = newState;\n draftState.interfaces[id].content = castDraft(element);\n });\n }\n\n /**\n * Delete an interface from state.\n *\n * @param id - The interface id.\n */\n deleteInterface(id: string) {\n this.update((draftState) => {\n delete draftState.interfaces[id];\n });\n }\n\n /**\n * Update the interface state.\n *\n * @param id - The interface id.\n * @param state - The new state.\n */\n updateInterfaceState(id: string, state: InterfaceState) {\n this.update((draftState) => {\n draftState.interfaces[id].state = state;\n });\n }\n\n /**\n * Resolve the promise of a given interface approval request.\n * The approval needs to have the same ID as the interface.\n *\n * @param snapId - The snap id.\n * @param id - The interface id.\n * @param value - The value to resolve the promise with.\n */\n async resolveInterface(snapId: SnapId, id: string, value: Json) {\n this.#validateArgs(snapId, id);\n this.#validateApproval(id);\n\n await this.#acceptApprovalRequest(id, value);\n\n this.deleteInterface(id);\n }\n\n /**\n * Utility function to validate the args passed to the other methods.\n *\n * @param snapId - The snap id.\n * @param id - The interface id.\n */\n #validateArgs(snapId: SnapId, id: string) {\n const existingInterface = this.state.interfaces[id];\n\n assert(\n existingInterface !== undefined,\n `Interface with id '${id}' not found.`,\n );\n assert(\n existingInterface.snapId === snapId,\n `Interface not created by ${snapId}.`,\n );\n }\n\n /**\n * Utility function to validate that the approval request exists.\n *\n * @param id - The interface id.\n */\n #validateApproval(id: string) {\n assert(\n this.#hasApprovalRequest(id),\n `Approval request with id '${id}' not found.`,\n );\n }\n\n /**\n * Trigger a Phishing list update if needed.\n */\n async #triggerPhishingListUpdate() {\n await this.messagingSystem.call('PhishingController:maybeUpdateState');\n }\n\n /**\n * Check an origin against the phishing list.\n *\n * @param origin - The origin to check.\n * @returns True if the origin is on the phishing list, otherwise false.\n */\n #checkPhishingList(origin: string) {\n return this.messagingSystem.call('PhishingController:testOrigin', origin)\n .result;\n }\n\n /**\n * Check if an approval request exists for a given interface by looking up\n * if the ApprovalController has a request with the given interface ID.\n *\n * @param id - The interface id.\n * @returns True if an approval request exists, otherwise false.\n */\n #hasApprovalRequest(id: string) {\n return this.messagingSystem.call('ApprovalController:hasRequest', {\n id,\n });\n }\n\n /**\n * Accept an approval request for a given interface.\n *\n * @param id - The interface id.\n * @param value - The value to resolve the promise with.\n */\n async #acceptApprovalRequest(id: string, value: Json) {\n await this.messagingSystem.call(\n 'ApprovalController:acceptRequest',\n id,\n value,\n );\n }\n\n /**\n * Utility function to validate the components of an interface.\n * Throws if something is invalid.\n *\n * @param element - The JSX element to verify.\n */\n async #validateContent(element: JSXElement) {\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(element);\n assert(\n size <= MAX_UI_CONTENT_SIZE,\n `A Snap UI may not be larger than ${MAX_UI_CONTENT_SIZE / 1000000} MB.`,\n );\n\n await this.#triggerPhishingListUpdate();\n validateJsxLinks(\n element,\n this.#checkPhishingList.bind(this),\n (id: string) => this.messagingSystem.call('SnapController:get', id),\n );\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"SnapInterfaceController.cjs","sourceRoot":"","sources":["../../src/interface/SnapInterfaceController.ts"],"names":[],"mappings":";;;;;;;;;AASA,+DAA2D;AAY3D,uDAA4E;AAE5E,2CAAyC;AACzC,iCAAkC;AAClC,mCAAgC;AAGhC,uCAIiB;AAEjB,MAAM,mBAAmB,GAAG,QAAU,CAAC,CAAC,QAAQ;AAEhD,MAAM,cAAc,GAAG,yBAAyB,CAAC;AAsFjD;;GAEG;AACH,MAAa,uBAAwB,SAAQ,gCAI5C;IACC,YAAY,EAAE,SAAS,EAAE,KAAK,EAA+B;QAC3D,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE;gBACR,UAAU,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE;aACjD;YACD,IAAI,EAAE,cAAc;YACpB,KAAK,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,GAAG,KAAK,EAAE;SACpC,CAAC,CAAC;;QAEH,uBAAA,IAAI,4FAAyB,MAA7B,IAAI,CAA2B,CAAC;IAClC,CAAC;IAsCD;;;;;;;OAOG;IACH,KAAK,CAAC,eAAe,CACnB,MAAc,EACd,OAA2B,EAC3B,OAA0B;QAE1B,MAAM,OAAO,GAAG,IAAA,uBAAe,EAAC,OAAO,CAAC,CAAC;QACzC,MAAM,uBAAA,IAAI,oFAAiB,MAArB,IAAI,EAAkB,OAAO,CAAC,CAAC;QACrC,IAAA,gCAAwB,EAAC,OAAO,CAAC,CAAC;QAElC,MAAM,EAAE,GAAG,IAAA,eAAM,GAAE,CAAC;QACpB,MAAM,cAAc,GAAG,IAAA,sBAAc,EAAC,EAAE,EAAE,OAAO,CAAC,CAAC;QAEnD,IAAI,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,EAAE;YACzB,wEAAwE;YACxE,qBAAqB;YACrB,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC,GAAG;gBAC1B,MAAM;gBACN,O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rE,0CAA0C;IAC1C,MAAM,IAAI,GAAG,IAAA,+BAAiB,EAAC,OAAO,CAAC,CAAC;IACxC,IAAA,cAAM,EACJ,IAAI,IAAI,mBAAmB,EAC3B,oCAAoC,mBAAmB,GAAG,OAAO,MAAM,CACxE,CAAC;IAEF,MAAM,uBAAA,IAAI,8FAA2B,MAA/B,IAAI,CAA6B,CAAC;IACxC,IAAA,8BAAgB,EACd,OAAO,EACP,uBAAA,IAAI,sFAAmB,CAAC,IAAI,CAAC,IAAI,CAAC,EAClC,CAAC,EAAU,EAAE,EAAE,CAAC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,oBAAoB,EAAE,EAAE,CAAC,CACpE,CAAC;AACJ,CAAC","sourcesContent":["import type {\n AcceptRequest,\n HasApprovalRequest,\n} from '@metamask/approval-controller';\nimport type {\n RestrictedControllerMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n MaybeUpdateState,\n TestOrigin,\n} from '@metamask/phishing-controller';\nimport type {\n InterfaceState,\n SnapId,\n ComponentOrElement,\n InterfaceContext,\n} from '@metamask/snaps-sdk';\nimport type { JSXElement } from '@metamask/snaps-sdk/jsx';\nimport { getJsonSizeUnsafe, validateJsxLinks } from '@metamask/snaps-utils';\nimport type { Json } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\nimport { castDraft } from 'immer';\nimport { nanoid } from 'nanoid';\n\nimport type { GetSnap } from '../snaps';\nimport {\n constructState,\n getJsxInterface,\n validateInterfaceContext,\n} from './utils';\n\nconst MAX_UI_CONTENT_SIZE = 10_000_000; // 10 mb\n\nconst controllerName = 'SnapInterfaceController';\n\nexport type CreateInterface = {\n type: `${typeof controllerName}:createInterface`;\n handler: SnapInterfaceController['createInterface'];\n};\n\nexport type GetInterface = {\n type: `${typeof controllerName}:getInterface`;\n handler: SnapInterfaceController['getInterface'];\n};\n\nexport type UpdateInterface = {\n type: `${typeof controllerName}:updateInterface`;\n handler: SnapInterfaceController['updateInterface'];\n};\n\nexport type DeleteInterface = {\n type: `${typeof controllerName}:deleteInterface`;\n handler: SnapInterfaceController['deleteInterface'];\n};\n\nexport type UpdateInterfaceState = {\n type: `${typeof controllerName}:updateInterfaceState`;\n handler: SnapInterfaceController['updateInterfaceState'];\n};\n\nexport type ResolveInterface = {\n type: `${typeof controllerName}:resolveInterface`;\n handler: SnapInterfaceController['resolveInterface'];\n};\n\nexport type SnapInterfaceControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SnapInterfaceControllerState\n>;\n\nexport type SnapInterfaceControllerAllowedActions =\n | TestOrigin\n | MaybeUpdateState\n | HasApprovalRequest\n | AcceptRequest\n | GetSnap;\n\nexport type SnapInterfaceControllerActions =\n | CreateInterface\n | GetInterface\n | UpdateInterface\n | DeleteInterface\n | UpdateInterfaceState\n | ResolveInterface\n | SnapInterfaceControllerGetStateAction;\n\nexport type SnapInterfaceControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n SnapInterfaceControllerState\n >;\n\nexport type SnapInterfaceControllerEvents =\n SnapInterfaceControllerStateChangeEvent;\n\nexport type SnapInterfaceControllerMessenger = RestrictedControllerMessenger<\n typeof controllerName,\n SnapInterfaceControllerActions | SnapInterfaceControllerAllowedActions,\n SnapInterfaceControllerEvents,\n SnapInterfaceControllerAllowedActions['type'],\n SnapInterfaceControllerEvents['type']\n>;\n\nexport type StoredInterface = {\n snapId: SnapId;\n content: JSXElement;\n state: InterfaceState;\n context: InterfaceContext | null;\n};\n\nexport type SnapInterfaceControllerState = {\n interfaces: Record<string, StoredInterface>;\n};\n\nexport type SnapInterfaceControllerArgs = {\n messenger: SnapInterfaceControllerMessenger;\n state?: SnapInterfaceControllerState;\n};\n\n/**\n * Use this controller to manage snaps UI interfaces using RPC method hooks.\n */\nexport class SnapInterfaceController extends BaseController<\n typeof controllerName,\n SnapInterfaceControllerState,\n SnapInterfaceControllerMessenger\n> {\n constructor({ messenger, state }: SnapInterfaceControllerArgs) {\n super({\n messenger,\n metadata: {\n interfaces: { persist: false, anonymous: false },\n },\n name: controllerName,\n state: { interfaces: {}, ...state },\n });\n\n this.#registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering this controller's messaging system\n * actions.\n */\n #registerMessageHandlers() {\n this.messagingSystem.registerActionHandler(\n `${controllerName}:createInterface`,\n this.createInterface.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:getInterface`,\n this.getInterface.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:updateInterface`,\n this.updateInterface.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:deleteInterface`,\n this.deleteInterface.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:updateInterfaceState`,\n this.updateInterfaceState.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:resolveInterface`,\n this.resolveInterface.bind(this),\n );\n }\n\n /**\n * Create an interface in the controller state with the associated data.\n *\n * @param snapId - The snap id that created the interface.\n * @param content - The interface content.\n * @param context - An optional interface context object.\n * @returns The newly interface id.\n */\n async createInterface(\n snapId: SnapId,\n content: ComponentOrElement,\n context?: InterfaceContext,\n ) {\n const element = getJsxInterface(content);\n await this.#validateContent(element);\n validateInterfaceContext(context);\n\n const id = nanoid();\n const componentState = constructState({}, element);\n\n this.update((draftState) => {\n // @ts-expect-error - TS2589: Type instantiation is excessively deep and\n // possibly infinite.\n draftState.interfaces[id] = {\n snapId,\n content: castDraft(element),\n state: componentState,\n context: context ?? null,\n };\n });\n\n return id;\n }\n\n /**\n * Get the data of a given interface id.\n *\n * @param snapId - The snap id requesting the interface data.\n * @param id - The interface id.\n * @returns The interface state.\n */\n getInterface(snapId: SnapId, id: string) {\n this.#validateArgs(snapId, id);\n\n return this.state.interfaces[id];\n }\n\n /**\n * Update the interface with the given content.\n *\n * @param snapId - The snap id requesting the update.\n * @param id - The interface id.\n * @param content - The new content.\n */\n async updateInterface(\n snapId: SnapId,\n id: string,\n content: ComponentOrElement,\n ) {\n this.#validateArgs(snapId, id);\n const element = getJsxInterface(content);\n await this.#validateContent(element);\n\n const oldState = this.state.interfaces[id].state;\n const newState = constructState(oldState, element);\n\n this.update((draftState) => {\n draftState.interfaces[id].state = newState;\n draftState.interfaces[id].content = castDraft(element);\n });\n }\n\n /**\n * Delete an interface from state.\n *\n * @param id - The interface id.\n */\n deleteInterface(id: string) {\n this.update((draftState) => {\n delete draftState.interfaces[id];\n });\n }\n\n /**\n * Update the interface state.\n *\n * @param id - The interface id.\n * @param state - The new state.\n */\n updateInterfaceState(id: string, state: InterfaceState) {\n this.update((draftState) => {\n draftState.interfaces[id].state = state;\n });\n }\n\n /**\n * Resolve the promise of a given interface approval request.\n * The approval needs to have the same ID as the interface.\n *\n * @param snapId - The snap id.\n * @param id - The interface id.\n * @param value - The value to resolve the promise with.\n */\n async resolveInterface(snapId: SnapId, id: string, value: Json) {\n this.#validateArgs(snapId, id);\n this.#validateApproval(id);\n\n await this.#acceptApprovalRequest(id, value);\n\n this.deleteInterface(id);\n }\n\n /**\n * Utility function to validate the args passed to the other methods.\n *\n * @param snapId - The snap id.\n * @param id - The interface id.\n */\n #validateArgs(snapId: SnapId, id: string) {\n const existingInterface = this.state.interfaces[id];\n\n assert(\n existingInterface !== undefined,\n `Interface with id '${id}' not found.`,\n );\n assert(\n existingInterface.snapId === snapId,\n `Interface not created by ${snapId}.`,\n );\n }\n\n /**\n * Utility function to validate that the approval request exists.\n *\n * @param id - The interface id.\n */\n #validateApproval(id: string) {\n assert(\n this.#hasApprovalRequest(id),\n `Approval request with id '${id}' not found.`,\n );\n }\n\n /**\n * Trigger a Phishing list update if needed.\n */\n async #triggerPhishingListUpdate() {\n await this.messagingSystem.call('PhishingController:maybeUpdateState');\n }\n\n /**\n * Check an origin against the phishing list.\n *\n * @param origin - The origin to check.\n * @returns True if the origin is on the phishing list, otherwise false.\n */\n #checkPhishingList(origin: string) {\n return this.messagingSystem.call('PhishingController:testOrigin', origin)\n .result;\n }\n\n /**\n * Check if an approval request exists for a given interface by looking up\n * if the ApprovalController has a request with the given interface ID.\n *\n * @param id - The interface id.\n * @returns True if an approval request exists, otherwise false.\n */\n #hasApprovalRequest(id: string) {\n return this.messagingSystem.call('ApprovalController:hasRequest', {\n id,\n });\n }\n\n /**\n * Accept an approval request for a given interface.\n *\n * @param id - The interface id.\n * @param value - The value to resolve the promise with.\n */\n async #acceptApprovalRequest(id: string, value: Json) {\n await this.messagingSystem.call(\n 'ApprovalController:acceptRequest',\n id,\n value,\n );\n }\n\n /**\n * Utility function to validate the components of an interface.\n * Throws if something is invalid.\n *\n * @param element - The JSX element to verify.\n */\n async #validateContent(element: JSXElement) {\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(element);\n assert(\n size <= MAX_UI_CONTENT_SIZE,\n `A Snap UI may not be larger than ${MAX_UI_CONTENT_SIZE / 1000000} MB.`,\n );\n\n await this.#triggerPhishingListUpdate();\n validateJsxLinks(\n element,\n this.#checkPhishingList.bind(this),\n (id: string) => this.messagingSystem.call('SnapController:get', id),\n );\n }\n}\n"]}
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var _SnapInterfaceController_instances, _SnapInterfaceController_registerMessageHandlers, _SnapInterfaceController_validateArgs, _SnapInterfaceController_validateApproval, _SnapInterfaceController_triggerPhishingListUpdate, _SnapInterfaceController_checkPhishingList, _SnapInterfaceController_hasApprovalRequest, _SnapInterfaceController_acceptApprovalRequest, _SnapInterfaceController_validateContent;
|
|
1
7
|
import { BaseController } from "@metamask/base-controller";
|
|
2
8
|
import { getJsonSizeUnsafe, validateJsxLinks } from "@metamask/snaps-utils";
|
|
3
9
|
import { assert } from "@metamask/utils";
|
|
@@ -19,19 +25,8 @@ export class SnapInterfaceController extends BaseController {
|
|
|
19
25
|
name: controllerName,
|
|
20
26
|
state: { interfaces: {}, ...state },
|
|
21
27
|
});
|
|
22
|
-
this
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Constructor helper for registering this controller's messaging system
|
|
26
|
-
* actions.
|
|
27
|
-
*/
|
|
28
|
-
#registerMessageHandlers() {
|
|
29
|
-
this.messagingSystem.registerActionHandler(`${controllerName}:createInterface`, this.createInterface.bind(this));
|
|
30
|
-
this.messagingSystem.registerActionHandler(`${controllerName}:getInterface`, this.getInterface.bind(this));
|
|
31
|
-
this.messagingSystem.registerActionHandler(`${controllerName}:updateInterface`, this.updateInterface.bind(this));
|
|
32
|
-
this.messagingSystem.registerActionHandler(`${controllerName}:deleteInterface`, this.deleteInterface.bind(this));
|
|
33
|
-
this.messagingSystem.registerActionHandler(`${controllerName}:updateInterfaceState`, this.updateInterfaceState.bind(this));
|
|
34
|
-
this.messagingSystem.registerActionHandler(`${controllerName}:resolveInterface`, this.resolveInterface.bind(this));
|
|
28
|
+
_SnapInterfaceController_instances.add(this);
|
|
29
|
+
__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_registerMessageHandlers).call(this);
|
|
35
30
|
}
|
|
36
31
|
/**
|
|
37
32
|
* Create an interface in the controller state with the associated data.
|
|
@@ -43,7 +38,7 @@ export class SnapInterfaceController extends BaseController {
|
|
|
43
38
|
*/
|
|
44
39
|
async createInterface(snapId, content, context) {
|
|
45
40
|
const element = getJsxInterface(content);
|
|
46
|
-
await this
|
|
41
|
+
await __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateContent).call(this, element);
|
|
47
42
|
validateInterfaceContext(context);
|
|
48
43
|
const id = nanoid();
|
|
49
44
|
const componentState = constructState({}, element);
|
|
@@ -67,7 +62,7 @@ export class SnapInterfaceController extends BaseController {
|
|
|
67
62
|
* @returns The interface state.
|
|
68
63
|
*/
|
|
69
64
|
getInterface(snapId, id) {
|
|
70
|
-
this
|
|
65
|
+
__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateArgs).call(this, snapId, id);
|
|
71
66
|
return this.state.interfaces[id];
|
|
72
67
|
}
|
|
73
68
|
/**
|
|
@@ -78,9 +73,9 @@ export class SnapInterfaceController extends BaseController {
|
|
|
78
73
|
* @param content - The new content.
|
|
79
74
|
*/
|
|
80
75
|
async updateInterface(snapId, id, content) {
|
|
81
|
-
this
|
|
76
|
+
__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateArgs).call(this, snapId, id);
|
|
82
77
|
const element = getJsxInterface(content);
|
|
83
|
-
await this
|
|
78
|
+
await __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateContent).call(this, element);
|
|
84
79
|
const oldState = this.state.interfaces[id].state;
|
|
85
80
|
const newState = constructState(oldState, element);
|
|
86
81
|
this.update((draftState) => {
|
|
@@ -118,80 +113,60 @@ export class SnapInterfaceController extends BaseController {
|
|
|
118
113
|
* @param value - The value to resolve the promise with.
|
|
119
114
|
*/
|
|
120
115
|
async resolveInterface(snapId, id, value) {
|
|
121
|
-
this
|
|
122
|
-
this
|
|
123
|
-
await this
|
|
116
|
+
__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateArgs).call(this, snapId, id);
|
|
117
|
+
__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateApproval).call(this, id);
|
|
118
|
+
await __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_acceptApprovalRequest).call(this, id, value);
|
|
124
119
|
this.deleteInterface(id);
|
|
125
120
|
}
|
|
126
|
-
/**
|
|
127
|
-
* Utility function to validate the args passed to the other methods.
|
|
128
|
-
*
|
|
129
|
-
* @param snapId - The snap id.
|
|
130
|
-
* @param id - The interface id.
|
|
131
|
-
*/
|
|
132
|
-
#validateArgs(snapId, id) {
|
|
133
|
-
const existingInterface = this.state.interfaces[id];
|
|
134
|
-
assert(existingInterface !== undefined, `Interface with id '${id}' not found.`);
|
|
135
|
-
assert(existingInterface.snapId === snapId, `Interface not created by ${snapId}.`);
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Utility function to validate that the approval request exists.
|
|
139
|
-
*
|
|
140
|
-
* @param id - The interface id.
|
|
141
|
-
*/
|
|
142
|
-
#validateApproval(id) {
|
|
143
|
-
assert(this.#hasApprovalRequest(id), `Approval request with id '${id}' not found.`);
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Trigger a Phishing list update if needed.
|
|
147
|
-
*/
|
|
148
|
-
async #triggerPhishingListUpdate() {
|
|
149
|
-
await this.messagingSystem.call('PhishingController:maybeUpdateState');
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Check an origin against the phishing list.
|
|
153
|
-
*
|
|
154
|
-
* @param origin - The origin to check.
|
|
155
|
-
* @returns True if the origin is on the phishing list, otherwise false.
|
|
156
|
-
*/
|
|
157
|
-
#checkPhishingList(origin) {
|
|
158
|
-
return this.messagingSystem.call('PhishingController:testOrigin', origin)
|
|
159
|
-
.result;
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Check if an approval request exists for a given interface by looking up
|
|
163
|
-
* if the ApprovalController has a request with the given interface ID.
|
|
164
|
-
*
|
|
165
|
-
* @param id - The interface id.
|
|
166
|
-
* @returns True if an approval request exists, otherwise false.
|
|
167
|
-
*/
|
|
168
|
-
#hasApprovalRequest(id) {
|
|
169
|
-
return this.messagingSystem.call('ApprovalController:hasRequest', {
|
|
170
|
-
id,
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Accept an approval request for a given interface.
|
|
175
|
-
*
|
|
176
|
-
* @param id - The interface id.
|
|
177
|
-
* @param value - The value to resolve the promise with.
|
|
178
|
-
*/
|
|
179
|
-
async #acceptApprovalRequest(id, value) {
|
|
180
|
-
await this.messagingSystem.call('ApprovalController:acceptRequest', id, value);
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Utility function to validate the components of an interface.
|
|
184
|
-
* Throws if something is invalid.
|
|
185
|
-
*
|
|
186
|
-
* @param element - The JSX element to verify.
|
|
187
|
-
*/
|
|
188
|
-
async #validateContent(element) {
|
|
189
|
-
// We assume the validity of this JSON to be validated by the caller.
|
|
190
|
-
// E.g., in the RPC method implementation.
|
|
191
|
-
const size = getJsonSizeUnsafe(element);
|
|
192
|
-
assert(size <= MAX_UI_CONTENT_SIZE, `A Snap UI may not be larger than ${MAX_UI_CONTENT_SIZE / 1000000} MB.`);
|
|
193
|
-
await this.#triggerPhishingListUpdate();
|
|
194
|
-
validateJsxLinks(element, this.#checkPhishingList.bind(this), (id) => this.messagingSystem.call('SnapController:get', id));
|
|
195
|
-
}
|
|
196
121
|
}
|
|
122
|
+
_SnapInterfaceController_instances = new WeakSet(), _SnapInterfaceController_registerMessageHandlers = function _SnapInterfaceController_registerMessageHandlers() {
|
|
123
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:createInterface`, this.createInterface.bind(this));
|
|
124
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:getInterface`, this.getInterface.bind(this));
|
|
125
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:updateInterface`, this.updateInterface.bind(this));
|
|
126
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:deleteInterface`, this.deleteInterface.bind(this));
|
|
127
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:updateInterfaceState`, this.updateInterfaceState.bind(this));
|
|
128
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:resolveInterface`, this.resolveInterface.bind(this));
|
|
129
|
+
}, _SnapInterfaceController_validateArgs = function _SnapInterfaceController_validateArgs(snapId, id) {
|
|
130
|
+
const existingInterface = this.state.interfaces[id];
|
|
131
|
+
assert(existingInterface !== undefined, `Interface with id '${id}' not found.`);
|
|
132
|
+
assert(existingInterface.snapId === snapId, `Interface not created by ${snapId}.`);
|
|
133
|
+
}, _SnapInterfaceController_validateApproval = function _SnapInterfaceController_validateApproval(id) {
|
|
134
|
+
assert(__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_hasApprovalRequest).call(this, id), `Approval request with id '${id}' not found.`);
|
|
135
|
+
}, _SnapInterfaceController_triggerPhishingListUpdate =
|
|
136
|
+
/**
|
|
137
|
+
* Trigger a Phishing list update if needed.
|
|
138
|
+
*/
|
|
139
|
+
async function _SnapInterfaceController_triggerPhishingListUpdate() {
|
|
140
|
+
await this.messagingSystem.call('PhishingController:maybeUpdateState');
|
|
141
|
+
}, _SnapInterfaceController_checkPhishingList = function _SnapInterfaceController_checkPhishingList(origin) {
|
|
142
|
+
return this.messagingSystem.call('PhishingController:testOrigin', origin)
|
|
143
|
+
.result;
|
|
144
|
+
}, _SnapInterfaceController_hasApprovalRequest = function _SnapInterfaceController_hasApprovalRequest(id) {
|
|
145
|
+
return this.messagingSystem.call('ApprovalController:hasRequest', {
|
|
146
|
+
id,
|
|
147
|
+
});
|
|
148
|
+
}, _SnapInterfaceController_acceptApprovalRequest =
|
|
149
|
+
/**
|
|
150
|
+
* Accept an approval request for a given interface.
|
|
151
|
+
*
|
|
152
|
+
* @param id - The interface id.
|
|
153
|
+
* @param value - The value to resolve the promise with.
|
|
154
|
+
*/
|
|
155
|
+
async function _SnapInterfaceController_acceptApprovalRequest(id, value) {
|
|
156
|
+
await this.messagingSystem.call('ApprovalController:acceptRequest', id, value);
|
|
157
|
+
}, _SnapInterfaceController_validateContent =
|
|
158
|
+
/**
|
|
159
|
+
* Utility function to validate the components of an interface.
|
|
160
|
+
* Throws if something is invalid.
|
|
161
|
+
*
|
|
162
|
+
* @param element - The JSX element to verify.
|
|
163
|
+
*/
|
|
164
|
+
async function _SnapInterfaceController_validateContent(element) {
|
|
165
|
+
// We assume the validity of this JSON to be validated by the caller.
|
|
166
|
+
// E.g., in the RPC method implementation.
|
|
167
|
+
const size = getJsonSizeUnsafe(element);
|
|
168
|
+
assert(size <= MAX_UI_CONTENT_SIZE, `A Snap UI may not be larger than ${MAX_UI_CONTENT_SIZE / 1000000} MB.`);
|
|
169
|
+
await __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_triggerPhishingListUpdate).call(this);
|
|
170
|
+
validateJsxLinks(element, __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_checkPhishingList).bind(this), (id) => this.messagingSystem.call('SnapController:get', id));
|
|
171
|
+
};
|
|
197
172
|
//# sourceMappingURL=SnapInterfaceController.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapInterfaceController.mjs","sourceRoot":"","sources":["../../src/interface/SnapInterfaceController.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAY3D,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,8BAA8B;AAE5E,OAAO,EAAE,MAAM,EAAE,wBAAwB;AACzC,OAAO,EAAE,SAAS,EAAE,cAAc;AAClC,OAAO,EAAE,MAAM,EAAE,eAAe;AAGhC,OAAO,EACL,cAAc,EACd,eAAe,EACf,wBAAwB,EACzB,oBAAgB;AAEjB,MAAM,mBAAmB,GAAG,QAAU,CAAC,CAAC,QAAQ;AAEhD,MAAM,cAAc,GAAG,yBAAyB,CAAC;AAsFjD;;GAEG;AACH,MAAM,OAAO,uBAAwB,SAAQ,cAI5C;IACC,YAAY,EAAE,SAAS,EAAE,KAAK,EAA+B;QAC3D,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE;gBACR,UAAU,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,S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,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,0BAA0B;QAC9B,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,qCAAqC,CAAC,CAAC;IACzE,CAAC;IAED;;;;;OAKG;IACH,kBAAkB,CAAC,MAAc;QAC/B,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,+BAA+B,EAAE,MAAM,CAAC;aACtE,MAAM,CAAC;IACZ,CAAC;IAED;;;;;;OAMG;IACH,mBAAmB,CAAC,EAAU;QAC5B,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,+BAA+B,EAAE;YAChE,EAAE;SACH,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,sBAAsB,CAAC,EAAU,EAAE,KAAW;QAClD,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAC7B,kCAAkC,EAClC,EAAE,EACF,KAAK,CACN,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,gBAAgB,CAAC,OAAmB;QACxC,qEAAqE;QACrE,0CAA0C;QAC1C,MAAM,IAAI,GAAG,iBAAiB,CAAC,OAAO,CAAC,CAAC;QACxC,MAAM,CACJ,IAAI,IAAI,mBAAmB,EAC3B,oCAAoC,mBAAmB,GAAG,OAAO,MAAM,CACxE,CAAC;QAEF,MAAM,IAAI,CAAC,0BAA0B,EAAE,CAAC;QACxC,gBAAgB,CACd,OAAO,EACP,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,EAClC,CAAC,EAAU,EAAE,EAAE,CAAC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,oBAAoB,EAAE,EAAE,CAAC,CACpE,CAAC;IACJ,CAAC;CACF","sourcesContent":["import type {\n AcceptRequest,\n HasApprovalRequest,\n} from '@metamask/approval-controller';\nimport type {\n RestrictedControllerMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n MaybeUpdateState,\n TestOrigin,\n} from '@metamask/phishing-controller';\nimport type {\n InterfaceState,\n SnapId,\n ComponentOrElement,\n InterfaceContext,\n} from '@metamask/snaps-sdk';\nimport type { JSXElement } from '@metamask/snaps-sdk/jsx';\nimport { getJsonSizeUnsafe, validateJsxLinks } from '@metamask/snaps-utils';\nimport type { Json } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\nimport { castDraft } from 'immer';\nimport { nanoid } from 'nanoid';\n\nimport type { GetSnap } from '../snaps';\nimport {\n constructState,\n getJsxInterface,\n validateInterfaceContext,\n} from './utils';\n\nconst MAX_UI_CONTENT_SIZE = 10_000_000; // 10 mb\n\nconst controllerName = 'SnapInterfaceController';\n\nexport type CreateInterface = {\n type: `${typeof controllerName}:createInterface`;\n handler: SnapInterfaceController['createInterface'];\n};\n\nexport type GetInterface = {\n type: `${typeof controllerName}:getInterface`;\n handler: SnapInterfaceController['getInterface'];\n};\n\nexport type UpdateInterface = {\n type: `${typeof controllerName}:updateInterface`;\n handler: SnapInterfaceController['updateInterface'];\n};\n\nexport type DeleteInterface = {\n type: `${typeof controllerName}:deleteInterface`;\n handler: SnapInterfaceController['deleteInterface'];\n};\n\nexport type UpdateInterfaceState = {\n type: `${typeof controllerName}:updateInterfaceState`;\n handler: SnapInterfaceController['updateInterfaceState'];\n};\n\nexport type ResolveInterface = {\n type: `${typeof controllerName}:resolveInterface`;\n handler: SnapInterfaceController['resolveInterface'];\n};\n\nexport type SnapInterfaceControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SnapInterfaceControllerState\n>;\n\nexport type SnapInterfaceControllerAllowedActions =\n | TestOrigin\n | MaybeUpdateState\n | HasApprovalRequest\n | AcceptRequest\n | GetSnap;\n\nexport type SnapInterfaceControllerActions =\n | CreateInterface\n | GetInterface\n | UpdateInterface\n | DeleteInterface\n | UpdateInterfaceState\n | ResolveInterface\n | SnapInterfaceControllerGetStateAction;\n\nexport type SnapInterfaceControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n SnapInterfaceControllerState\n >;\n\nexport type SnapInterfaceControllerEvents =\n SnapInterfaceControllerStateChangeEvent;\n\nexport type SnapInterfaceControllerMessenger = RestrictedControllerMessenger<\n typeof controllerName,\n SnapInterfaceControllerActions | SnapInterfaceControllerAllowedActions,\n SnapInterfaceControllerEvents,\n SnapInterfaceControllerAllowedActions['type'],\n SnapInterfaceControllerEvents['type']\n>;\n\nexport type StoredInterface = {\n snapId: SnapId;\n content: JSXElement;\n state: InterfaceState;\n context: InterfaceContext | null;\n};\n\nexport type SnapInterfaceControllerState = {\n interfaces: Record<string, StoredInterface>;\n};\n\nexport type SnapInterfaceControllerArgs = {\n messenger: SnapInterfaceControllerMessenger;\n state?: SnapInterfaceControllerState;\n};\n\n/**\n * Use this controller to manage snaps UI interfaces using RPC method hooks.\n */\nexport class SnapInterfaceController extends BaseController<\n typeof controllerName,\n SnapInterfaceControllerState,\n SnapInterfaceControllerMessenger\n> {\n constructor({ messenger, state }: SnapInterfaceControllerArgs) {\n super({\n messenger,\n metadata: {\n interfaces: { persist: false, anonymous: false },\n },\n name: controllerName,\n state: { interfaces: {}, ...state },\n });\n\n this.#registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering this controller's messaging system\n * actions.\n */\n #registerMessageHandlers() {\n this.messagingSystem.registerActionHandler(\n `${controllerName}:createInterface`,\n this.createInterface.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:getInterface`,\n this.getInterface.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:updateInterface`,\n this.updateInterface.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:deleteInterface`,\n this.deleteInterface.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:updateInterfaceState`,\n this.updateInterfaceState.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:resolveInterface`,\n this.resolveInterface.bind(this),\n );\n }\n\n /**\n * Create an interface in the controller state with the associated data.\n *\n * @param snapId - The snap id that created the interface.\n * @param content - The interface content.\n * @param context - An optional interface context object.\n * @returns The newly interface id.\n */\n async createInterface(\n snapId: SnapId,\n content: ComponentOrElement,\n context?: InterfaceContext,\n ) {\n const element = getJsxInterface(content);\n await this.#validateContent(element);\n validateInterfaceContext(context);\n\n const id = nanoid();\n const componentState = constructState({}, element);\n\n this.update((draftState) => {\n // @ts-expect-error - TS2589: Type instantiation is excessively deep and\n // possibly infinite.\n draftState.interfaces[id] = {\n snapId,\n content: castDraft(element),\n state: componentState,\n context: context ?? null,\n };\n });\n\n return id;\n }\n\n /**\n * Get the data of a given interface id.\n *\n * @param snapId - The snap id requesting the interface data.\n * @param id - The interface id.\n * @returns The interface state.\n */\n getInterface(snapId: SnapId, id: string) {\n this.#validateArgs(snapId, id);\n\n return this.state.interfaces[id];\n }\n\n /**\n * Update the interface with the given content.\n *\n * @param snapId - The snap id requesting the update.\n * @param id - The interface id.\n * @param content - The new content.\n */\n async updateInterface(\n snapId: SnapId,\n id: string,\n content: ComponentOrElement,\n ) {\n this.#validateArgs(snapId, id);\n const element = getJsxInterface(content);\n await this.#validateContent(element);\n\n const oldState = this.state.interfaces[id].state;\n const newState = constructState(oldState, element);\n\n this.update((draftState) => {\n draftState.interfaces[id].state = newState;\n draftState.interfaces[id].content = castDraft(element);\n });\n }\n\n /**\n * Delete an interface from state.\n *\n * @param id - The interface id.\n */\n deleteInterface(id: string) {\n this.update((draftState) => {\n delete draftState.interfaces[id];\n });\n }\n\n /**\n * Update the interface state.\n *\n * @param id - The interface id.\n * @param state - The new state.\n */\n updateInterfaceState(id: string, state: InterfaceState) {\n this.update((draftState) => {\n draftState.interfaces[id].state = state;\n });\n }\n\n /**\n * Resolve the promise of a given interface approval request.\n * The approval needs to have the same ID as the interface.\n *\n * @param snapId - The snap id.\n * @param id - The interface id.\n * @param value - The value to resolve the promise with.\n */\n async resolveInterface(snapId: SnapId, id: string, value: Json) {\n this.#validateArgs(snapId, id);\n this.#validateApproval(id);\n\n await this.#acceptApprovalRequest(id, value);\n\n this.deleteInterface(id);\n }\n\n /**\n * Utility function to validate the args passed to the other methods.\n *\n * @param snapId - The snap id.\n * @param id - The interface id.\n */\n #validateArgs(snapId: SnapId, id: string) {\n const existingInterface = this.state.interfaces[id];\n\n assert(\n existingInterface !== undefined,\n `Interface with id '${id}' not found.`,\n );\n assert(\n existingInterface.snapId === snapId,\n `Interface not created by ${snapId}.`,\n );\n }\n\n /**\n * Utility function to validate that the approval request exists.\n *\n * @param id - The interface id.\n */\n #validateApproval(id: string) {\n assert(\n this.#hasApprovalRequest(id),\n `Approval request with id '${id}' not found.`,\n );\n }\n\n /**\n * Trigger a Phishing list update if needed.\n */\n async #triggerPhishingListUpdate() {\n await this.messagingSystem.call('PhishingController:maybeUpdateState');\n }\n\n /**\n * Check an origin against the phishing list.\n *\n * @param origin - The origin to check.\n * @returns True if the origin is on the phishing list, otherwise false.\n */\n #checkPhishingList(origin: string) {\n return this.messagingSystem.call('PhishingController:testOrigin', origin)\n .result;\n }\n\n /**\n * Check if an approval request exists for a given interface by looking up\n * if the ApprovalController has a request with the given interface ID.\n *\n * @param id - The interface id.\n * @returns True if an approval request exists, otherwise false.\n */\n #hasApprovalRequest(id: string) {\n return this.messagingSystem.call('ApprovalController:hasRequest', {\n id,\n });\n }\n\n /**\n * Accept an approval request for a given interface.\n *\n * @param id - The interface id.\n * @param value - The value to resolve the promise with.\n */\n async #acceptApprovalRequest(id: string, value: Json) {\n await this.messagingSystem.call(\n 'ApprovalController:acceptRequest',\n id,\n value,\n );\n }\n\n /**\n * Utility function to validate the components of an interface.\n * Throws if something is invalid.\n *\n * @param element - The JSX element to verify.\n */\n async #validateContent(element: JSXElement) {\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(element);\n assert(\n size <= MAX_UI_CONTENT_SIZE,\n `A Snap UI may not be larger than ${MAX_UI_CONTENT_SIZE / 1000000} MB.`,\n );\n\n await this.#triggerPhishingListUpdate();\n validateJsxLinks(\n element,\n this.#checkPhishingList.bind(this),\n (id: string) => this.messagingSystem.call('SnapController:get', id),\n );\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"SnapInterfaceController.mjs","sourceRoot":"","sources":["../../src/interface/SnapInterfaceController.ts"],"names":[],"mappings":";;;;;;AASA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAY3D,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,8BAA8B;AAE5E,OAAO,EAAE,MAAM,EAAE,wBAAwB;AACzC,OAAO,EAAE,SAAS,EAAE,cAAc;AAClC,OAAO,EAAE,MAAM,EAAE,eAAe;AAGhC,OAAO,EACL,cAAc,EACd,eAAe,EACf,wBAAwB,EACzB,oBAAgB;AAEjB,MAAM,mBAAmB,GAAG,QAAU,CAAC,CAAC,QAAQ;AAEhD,MAAM,cAAc,GAAG,yBAAyB,CAAC;AAsFjD;;GAEG;AACH,MAAM,OAAO,uBAAwB,SAAQ,cAI5C;IACC,YAAY,EAAE,SAAS,EAAE,KAAK,EAA+B;QAC3D,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE;gBACR,UAAU,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE;aACjD;YACD,IAAI,EAAE,cAAc;YACpB,KAAK,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,GAAG,KAAK,EAAE;SACpC,CAAC,CAAC;;QAEH,uBAAA,IAAI,4FAAyB,MAA7B,IAAI,CAA2B,CAAC;IAClC,CAAC;IAsCD;;;;;;;OAOG;IACH,KAAK,CAAC,eAAe,CACnB,MAAc,EACd,OAA2B,EAC3B,OAA0B;QAE1B,MAAM,OAAO,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC;QACzC,MAAM,uBAAA,IAAI,oFAAiB,MAArB,IAAI,EAAkB,OAAO,CAAC,CAAC;QACrC,wBAAwB,C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,CAAC;AAED;;;;;GAKG;AACH,KAAK,yDAAwB,EAAU,EAAE,KAAW;IAClD,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAC7B,kCAAkC,EAClC,EAAE,EACF,KAAK,CACN,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,KAAK,mDAAkB,OAAmB;IACxC,qEAAqE;IACrE,0CAA0C;IAC1C,MAAM,IAAI,GAAG,iBAAiB,CAAC,OAAO,CAAC,CAAC;IACxC,MAAM,CACJ,IAAI,IAAI,mBAAmB,EAC3B,oCAAoC,mBAAmB,GAAG,OAAO,MAAM,CACxE,CAAC;IAEF,MAAM,uBAAA,IAAI,8FAA2B,MAA/B,IAAI,CAA6B,CAAC;IACxC,gBAAgB,CACd,OAAO,EACP,uBAAA,IAAI,sFAAmB,CAAC,IAAI,CAAC,IAAI,CAAC,EAClC,CAAC,EAAU,EAAE,EAAE,CAAC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,oBAAoB,EAAE,EAAE,CAAC,CACpE,CAAC;AACJ,CAAC","sourcesContent":["import type {\n AcceptRequest,\n HasApprovalRequest,\n} from '@metamask/approval-controller';\nimport type {\n RestrictedControllerMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n MaybeUpdateState,\n TestOrigin,\n} from '@metamask/phishing-controller';\nimport type {\n InterfaceState,\n SnapId,\n ComponentOrElement,\n InterfaceContext,\n} from '@metamask/snaps-sdk';\nimport type { JSXElement } from '@metamask/snaps-sdk/jsx';\nimport { getJsonSizeUnsafe, validateJsxLinks } from '@metamask/snaps-utils';\nimport type { Json } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\nimport { castDraft } from 'immer';\nimport { nanoid } from 'nanoid';\n\nimport type { GetSnap } from '../snaps';\nimport {\n constructState,\n getJsxInterface,\n validateInterfaceContext,\n} from './utils';\n\nconst MAX_UI_CONTENT_SIZE = 10_000_000; // 10 mb\n\nconst controllerName = 'SnapInterfaceController';\n\nexport type CreateInterface = {\n type: `${typeof controllerName}:createInterface`;\n handler: SnapInterfaceController['createInterface'];\n};\n\nexport type GetInterface = {\n type: `${typeof controllerName}:getInterface`;\n handler: SnapInterfaceController['getInterface'];\n};\n\nexport type UpdateInterface = {\n type: `${typeof controllerName}:updateInterface`;\n handler: SnapInterfaceController['updateInterface'];\n};\n\nexport type DeleteInterface = {\n type: `${typeof controllerName}:deleteInterface`;\n handler: SnapInterfaceController['deleteInterface'];\n};\n\nexport type UpdateInterfaceState = {\n type: `${typeof controllerName}:updateInterfaceState`;\n handler: SnapInterfaceController['updateInterfaceState'];\n};\n\nexport type ResolveInterface = {\n type: `${typeof controllerName}:resolveInterface`;\n handler: SnapInterfaceController['resolveInterface'];\n};\n\nexport type SnapInterfaceControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SnapInterfaceControllerState\n>;\n\nexport type SnapInterfaceControllerAllowedActions =\n | TestOrigin\n | MaybeUpdateState\n | HasApprovalRequest\n | AcceptRequest\n | GetSnap;\n\nexport type SnapInterfaceControllerActions =\n | CreateInterface\n | GetInterface\n | UpdateInterface\n | DeleteInterface\n | UpdateInterfaceState\n | ResolveInterface\n | SnapInterfaceControllerGetStateAction;\n\nexport type SnapInterfaceControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n SnapInterfaceControllerState\n >;\n\nexport type SnapInterfaceControllerEvents =\n SnapInterfaceControllerStateChangeEvent;\n\nexport type SnapInterfaceControllerMessenger = RestrictedControllerMessenger<\n typeof controllerName,\n SnapInterfaceControllerActions | SnapInterfaceControllerAllowedActions,\n SnapInterfaceControllerEvents,\n SnapInterfaceControllerAllowedActions['type'],\n SnapInterfaceControllerEvents['type']\n>;\n\nexport type StoredInterface = {\n snapId: SnapId;\n content: JSXElement;\n state: InterfaceState;\n context: InterfaceContext | null;\n};\n\nexport type SnapInterfaceControllerState = {\n interfaces: Record<string, StoredInterface>;\n};\n\nexport type SnapInterfaceControllerArgs = {\n messenger: SnapInterfaceControllerMessenger;\n state?: SnapInterfaceControllerState;\n};\n\n/**\n * Use this controller to manage snaps UI interfaces using RPC method hooks.\n */\nexport class SnapInterfaceController extends BaseController<\n typeof controllerName,\n SnapInterfaceControllerState,\n SnapInterfaceControllerMessenger\n> {\n constructor({ messenger, state }: SnapInterfaceControllerArgs) {\n super({\n messenger,\n metadata: {\n interfaces: { persist: false, anonymous: false },\n },\n name: controllerName,\n state: { interfaces: {}, ...state },\n });\n\n this.#registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering this controller's messaging system\n * actions.\n */\n #registerMessageHandlers() {\n this.messagingSystem.registerActionHandler(\n `${controllerName}:createInterface`,\n this.createInterface.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:getInterface`,\n this.getInterface.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:updateInterface`,\n this.updateInterface.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:deleteInterface`,\n this.deleteInterface.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:updateInterfaceState`,\n this.updateInterfaceState.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:resolveInterface`,\n this.resolveInterface.bind(this),\n );\n }\n\n /**\n * Create an interface in the controller state with the associated data.\n *\n * @param snapId - The snap id that created the interface.\n * @param content - The interface content.\n * @param context - An optional interface context object.\n * @returns The newly interface id.\n */\n async createInterface(\n snapId: SnapId,\n content: ComponentOrElement,\n context?: InterfaceContext,\n ) {\n const element = getJsxInterface(content);\n await this.#validateContent(element);\n validateInterfaceContext(context);\n\n const id = nanoid();\n const componentState = constructState({}, element);\n\n this.update((draftState) => {\n // @ts-expect-error - TS2589: Type instantiation is excessively deep and\n // possibly infinite.\n draftState.interfaces[id] = {\n snapId,\n content: castDraft(element),\n state: componentState,\n context: context ?? null,\n };\n });\n\n return id;\n }\n\n /**\n * Get the data of a given interface id.\n *\n * @param snapId - The snap id requesting the interface data.\n * @param id - The interface id.\n * @returns The interface state.\n */\n getInterface(snapId: SnapId, id: string) {\n this.#validateArgs(snapId, id);\n\n return this.state.interfaces[id];\n }\n\n /**\n * Update the interface with the given content.\n *\n * @param snapId - The snap id requesting the update.\n * @param id - The interface id.\n * @param content - The new content.\n */\n async updateInterface(\n snapId: SnapId,\n id: string,\n content: ComponentOrElement,\n ) {\n this.#validateArgs(snapId, id);\n const element = getJsxInterface(content);\n await this.#validateContent(element);\n\n const oldState = this.state.interfaces[id].state;\n const newState = constructState(oldState, element);\n\n this.update((draftState) => {\n draftState.interfaces[id].state = newState;\n draftState.interfaces[id].content = castDraft(element);\n });\n }\n\n /**\n * Delete an interface from state.\n *\n * @param id - The interface id.\n */\n deleteInterface(id: string) {\n this.update((draftState) => {\n delete draftState.interfaces[id];\n });\n }\n\n /**\n * Update the interface state.\n *\n * @param id - The interface id.\n * @param state - The new state.\n */\n updateInterfaceState(id: string, state: InterfaceState) {\n this.update((draftState) => {\n draftState.interfaces[id].state = state;\n });\n }\n\n /**\n * Resolve the promise of a given interface approval request.\n * The approval needs to have the same ID as the interface.\n *\n * @param snapId - The snap id.\n * @param id - The interface id.\n * @param value - The value to resolve the promise with.\n */\n async resolveInterface(snapId: SnapId, id: string, value: Json) {\n this.#validateArgs(snapId, id);\n this.#validateApproval(id);\n\n await this.#acceptApprovalRequest(id, value);\n\n this.deleteInterface(id);\n }\n\n /**\n * Utility function to validate the args passed to the other methods.\n *\n * @param snapId - The snap id.\n * @param id - The interface id.\n */\n #validateArgs(snapId: SnapId, id: string) {\n const existingInterface = this.state.interfaces[id];\n\n assert(\n existingInterface !== undefined,\n `Interface with id '${id}' not found.`,\n );\n assert(\n existingInterface.snapId === snapId,\n `Interface not created by ${snapId}.`,\n );\n }\n\n /**\n * Utility function to validate that the approval request exists.\n *\n * @param id - The interface id.\n */\n #validateApproval(id: string) {\n assert(\n this.#hasApprovalRequest(id),\n `Approval request with id '${id}' not found.`,\n );\n }\n\n /**\n * Trigger a Phishing list update if needed.\n */\n async #triggerPhishingListUpdate() {\n await this.messagingSystem.call('PhishingController:maybeUpdateState');\n }\n\n /**\n * Check an origin against the phishing list.\n *\n * @param origin - The origin to check.\n * @returns True if the origin is on the phishing list, otherwise false.\n */\n #checkPhishingList(origin: string) {\n return this.messagingSystem.call('PhishingController:testOrigin', origin)\n .result;\n }\n\n /**\n * Check if an approval request exists for a given interface by looking up\n * if the ApprovalController has a request with the given interface ID.\n *\n * @param id - The interface id.\n * @returns True if an approval request exists, otherwise false.\n */\n #hasApprovalRequest(id: string) {\n return this.messagingSystem.call('ApprovalController:hasRequest', {\n id,\n });\n }\n\n /**\n * Accept an approval request for a given interface.\n *\n * @param id - The interface id.\n * @param value - The value to resolve the promise with.\n */\n async #acceptApprovalRequest(id: string, value: Json) {\n await this.messagingSystem.call(\n 'ApprovalController:acceptRequest',\n id,\n value,\n );\n }\n\n /**\n * Utility function to validate the components of an interface.\n * Throws if something is invalid.\n *\n * @param element - The JSX element to verify.\n */\n async #validateContent(element: JSXElement) {\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(element);\n assert(\n size <= MAX_UI_CONTENT_SIZE,\n `A Snap UI may not be larger than ${MAX_UI_CONTENT_SIZE / 1000000} MB.`,\n );\n\n await this.#triggerPhishingListUpdate();\n validateJsxLinks(\n element,\n this.#checkPhishingList.bind(this),\n (id: string) => this.messagingSystem.call('SnapController:get', id),\n );\n }\n}\n"]}
|
package/dist/interface/utils.cjs
CHANGED
|
@@ -71,6 +71,8 @@ function getComponentStateValue(element) {
|
|
|
71
71
|
switch (element.type) {
|
|
72
72
|
case 'Checkbox':
|
|
73
73
|
return element.props.checked;
|
|
74
|
+
case 'AccountSelector':
|
|
75
|
+
return element.props.selectedAddress;
|
|
74
76
|
default:
|
|
75
77
|
return element.props.value;
|
|
76
78
|
}
|
|
@@ -125,7 +127,8 @@ function constructState(oldState, rootComponent) {
|
|
|
125
127
|
component.type === 'RadioGroup' ||
|
|
126
128
|
component.type === 'FileInput' ||
|
|
127
129
|
component.type === 'Checkbox' ||
|
|
128
|
-
component.type === 'Selector'
|
|
130
|
+
component.type === 'Selector' ||
|
|
131
|
+
component.type === 'AccountSelector')) {
|
|
129
132
|
const formState = newState[currentForm.name];
|
|
130
133
|
assertNameIsUnique(formState, component.props.name);
|
|
131
134
|
formState[component.props.name] = constructInputState(oldState, component, currentForm.name);
|
|
@@ -137,7 +140,8 @@ function constructState(oldState, rootComponent) {
|
|
|
137
140
|
component.type === 'RadioGroup' ||
|
|
138
141
|
component.type === 'FileInput' ||
|
|
139
142
|
component.type === 'Checkbox' ||
|
|
140
|
-
component.type === 'Selector'
|
|
143
|
+
component.type === 'Selector' ||
|
|
144
|
+
component.type === 'AccountSelector') {
|
|
141
145
|
assertNameIsUnique(newState, component.props.name);
|
|
142
146
|
newState[component.props.name] = constructInputState(oldState, component);
|
|
143
147
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/interface/utils.ts"],"names":[],"mappings":";;;AAAA,mDAA6C;AAoB7C,iDAA6D;AAC7D,uDAK+B;AAE/B;;;;;;;GAOG;AACH,SAAgB,eAAe,CAAC,SAA6B;IAC3D,IAAI,IAAA,wBAAkB,EAAC,SAAS,CAAC,EAAE,CAAC;QAClC,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,OAAO,IAAA,wCAA0B,EAAC,SAAS,CAAC,CAAC;AAC/C,CAAC;AAND,0CAMC;AAED;;;;;GAKG;AACH,SAAgB,kBAAkB,CAAC,KAAqB,EAAE,IAAY;IACpE,IAAA,kBAAM,EACJ,KAAK,CAAC,IAAI,CAAC,KAAK,SAAS,EACzB,4EAA4E,IAAI,IAAI,CACrF,CAAC;AACJ,CAAC;AALD,gDAKC;AAED;;;;;;;;GAQG;AACH,SAAS,sCAAsC,CAC7C,OAKmB;IAEnB,QAAQ,OAAO,CAA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DAcC","sourcesContent":["import { assert } from '@metamask/snaps-sdk';\nimport type {\n FormState,\n InterfaceState,\n ComponentOrElement,\n InterfaceContext,\n State,\n} from '@metamask/snaps-sdk';\nimport type {\n DropdownElement,\n InputElement,\n JSXElement,\n OptionElement,\n FileInputElement,\n CheckboxElement,\n RadioGroupElement,\n RadioElement,\n SelectorElement,\n SelectorOptionElement,\n} from '@metamask/snaps-sdk/jsx';\nimport { isJSXElementUnsafe } from '@metamask/snaps-sdk/jsx';\nimport {\n getJsonSizeUnsafe,\n getJsxChildren,\n getJsxElementFromComponent,\n walkJsx,\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:\n | InputElement\n | DropdownElement\n | RadioGroupElement\n | CheckboxElement\n | SelectorElement,\n) {\n switch (element.type) {\n case 'Dropdown': {\n const children = getJsxChildren(element) as OptionElement[];\n return children[0]?.props.value;\n }\n\n case 'RadioGroup': {\n const children = getJsxChildren(element) as RadioElement[];\n return children[0]?.props.value;\n }\n\n case 'Selector': {\n const children = getJsxChildren(element) as SelectorOptionElement[];\n return children[0]?.props.value;\n }\n\n case 'Checkbox':\n return false;\n\n default:\n return null;\n }\n}\n\n/**\n * Get the state value for a stateful component.\n *\n * Most components store the state value as a `value` prop.\n * This function exists to account for components where that isn't the case.\n *\n * @param element - The input element.\n * @returns The state value for a given component.\n */\nfunction getComponentStateValue(\n element:\n | InputElement\n | DropdownElement\n | RadioGroupElement\n | CheckboxElement\n | SelectorElement,\n) {\n switch (element.type) {\n case 'Checkbox':\n return element.props.checked;\n\n default:\n return element.props.value;\n }\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 * @param form - An optional form that the input is enclosed in.\n * @returns The input state.\n */\nfunction constructInputState(\n oldState: InterfaceState,\n element:\n | InputElement\n | DropdownElement\n | RadioGroupElement\n | FileInputElement\n | CheckboxElement\n | SelectorElement,\n form?: string,\n) {\n const oldStateUnwrapped = form ? (oldState[form] as FormState) : oldState;\n const oldInputState = oldStateUnwrapped?.[element.props.name] as State;\n\n if (element.type === 'FileInput') {\n return oldInputState ?? null;\n }\n\n return (\n getComponentStateValue(element) ??\n oldInputState ??\n constructComponentSpecificDefaultState(element) ??\n null\n );\n}\n\n/**\n * Construct the interface state for a given component tree.\n *\n * @param oldState - The previous state.\n * @param rootComponent - The UI component to construct state from.\n * @returns The interface state of the passed component.\n */\nexport function constructState(\n oldState: InterfaceState,\n rootComponent: JSXElement,\n): InterfaceState {\n const newState: InterfaceState = {};\n\n // Stack containing the forms we have visited and at which depth\n const formStack: { name: string; depth: number }[] = [];\n\n walkJsx(rootComponent, (component, depth) => {\n let currentForm = formStack[formStack.length - 1];\n\n // Pop the current form of the stack once we leave its depth.\n if (currentForm && depth <= currentForm.depth) {\n formStack.pop();\n currentForm = formStack[formStack.length - 1];\n }\n\n if (component.type === 'Form') {\n assertNameIsUnique(newState, component.props.name);\n formStack.push({ name: component.props.name, depth });\n newState[component.props.name] = {};\n return;\n }\n\n // Stateful components inside a form\n if (\n currentForm &&\n (component.type === 'Input' ||\n component.type === 'Dropdown' ||\n component.type === 'RadioGroup' ||\n component.type === 'FileInput' ||\n component.type === 'Checkbox' ||\n component.type === 'Selector')\n ) {\n const formState = newState[currentForm.name] as FormState;\n assertNameIsUnique(formState, component.props.name);\n formState[component.props.name] = constructInputState(\n oldState,\n component,\n currentForm.name,\n );\n return;\n }\n\n // Stateful components outside a form\n if (\n component.type === 'Input' ||\n component.type === 'Dropdown' ||\n component.type === 'RadioGroup' ||\n component.type === 'FileInput' ||\n component.type === 'Checkbox' ||\n component.type === 'Selector'\n ) {\n assertNameIsUnique(newState, component.props.name);\n newState[component.props.name] = constructInputState(oldState, component);\n }\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"]}
|
|
1
|
+
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/interface/utils.ts"],"names":[],"mappings":";;;AAAA,mDAA6C;AAqB7C,iDAA6D;AAC7D,uDAK+B;AAE/B;;;;;;;GAOG;AACH,SAAgB,eAAe,CAAC,SAA6B;IAC3D,IAAI,IAAA,wBAAkB,EAAC,SAAS,CAAC,EAAE,CAAC;QAClC,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,OAAO,IAAA,wCAA0B,EAAC,SAAS,CAAC,CAAC;AAC/C,CAAC;AAND,0CAMC;AAED;;;;;GAKG;AACH,SAAgB,kBAAkB,CAAC,KAAqB,EAAE,IAAY;IACpE,IAAA,kBAAM,EACJ,KAAK,CAAC,IAAI,CAAC,KAAK,SAAS,EACzB,4EAA4E,IAAI,IAAI,CACrF,CAAC;AACJ,CAAC;AALD,gDAKC;AAED;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OAAO;IACT,CAAC;IAED,qEAAqE;IACrE,0CAA0C;IAC1C,MAAM,IAAI,GAAG,IAAA,+BAAiB,EAAC,OAAO,CAAC,CAAC;IACxC,IAAA,kBAAM,EACJ,IAAI,IAAI,gBAAgB,EACxB,mDACE,gBAAgB,GAAG,OACrB,MAAM,CACP,CAAC;AACJ,CAAC;AAdD,4DAcC","sourcesContent":["import { assert } from '@metamask/snaps-sdk';\nimport type {\n FormState,\n InterfaceState,\n ComponentOrElement,\n InterfaceContext,\n State,\n} from '@metamask/snaps-sdk';\nimport type {\n DropdownElement,\n InputElement,\n JSXElement,\n OptionElement,\n FileInputElement,\n CheckboxElement,\n RadioGroupElement,\n RadioElement,\n SelectorElement,\n SelectorOptionElement,\n AccountSelectorElement,\n} from '@metamask/snaps-sdk/jsx';\nimport { isJSXElementUnsafe } from '@metamask/snaps-sdk/jsx';\nimport {\n getJsonSizeUnsafe,\n getJsxChildren,\n getJsxElementFromComponent,\n walkJsx,\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:\n | InputElement\n | DropdownElement\n | RadioGroupElement\n | CheckboxElement\n | SelectorElement\n | AccountSelectorElement,\n) {\n switch (element.type) {\n case 'Dropdown': {\n const children = getJsxChildren(element) as OptionElement[];\n return children[0]?.props.value;\n }\n\n case 'RadioGroup': {\n const children = getJsxChildren(element) as RadioElement[];\n return children[0]?.props.value;\n }\n\n case 'Selector': {\n const children = getJsxChildren(element) as SelectorOptionElement[];\n return children[0]?.props.value;\n }\n\n case 'Checkbox':\n return false;\n\n default:\n return null;\n }\n}\n\n/**\n * Get the state value for a stateful component.\n *\n * Most components store the state value as a `value` prop.\n * This function exists to account for components where that isn't the case.\n *\n * @param element - The input element.\n * @returns The state value for a given component.\n */\nfunction getComponentStateValue(\n element:\n | InputElement\n | DropdownElement\n | RadioGroupElement\n | CheckboxElement\n | SelectorElement\n | AccountSelectorElement,\n) {\n switch (element.type) {\n case 'Checkbox':\n return element.props.checked;\n\n case 'AccountSelector':\n return element.props.selectedAddress;\n\n default:\n return element.props.value;\n }\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 * @param form - An optional form that the input is enclosed in.\n * @returns The input state.\n */\nfunction constructInputState(\n oldState: InterfaceState,\n element:\n | InputElement\n | DropdownElement\n | RadioGroupElement\n | FileInputElement\n | CheckboxElement\n | SelectorElement\n | AccountSelectorElement,\n form?: string,\n) {\n const oldStateUnwrapped = form ? (oldState[form] as FormState) : oldState;\n const oldInputState = oldStateUnwrapped?.[element.props.name] as State;\n\n if (element.type === 'FileInput') {\n return oldInputState ?? null;\n }\n\n return (\n getComponentStateValue(element) ??\n oldInputState ??\n constructComponentSpecificDefaultState(element) ??\n null\n );\n}\n\n/**\n * Construct the interface state for a given component tree.\n *\n * @param oldState - The previous state.\n * @param rootComponent - The UI component to construct state from.\n * @returns The interface state of the passed component.\n */\nexport function constructState(\n oldState: InterfaceState,\n rootComponent: JSXElement,\n): InterfaceState {\n const newState: InterfaceState = {};\n\n // Stack containing the forms we have visited and at which depth\n const formStack: { name: string; depth: number }[] = [];\n\n walkJsx(rootComponent, (component, depth) => {\n let currentForm = formStack[formStack.length - 1];\n\n // Pop the current form of the stack once we leave its depth.\n if (currentForm && depth <= currentForm.depth) {\n formStack.pop();\n currentForm = formStack[formStack.length - 1];\n }\n\n if (component.type === 'Form') {\n assertNameIsUnique(newState, component.props.name);\n formStack.push({ name: component.props.name, depth });\n newState[component.props.name] = {};\n return;\n }\n\n // Stateful components inside a form\n if (\n currentForm &&\n (component.type === 'Input' ||\n component.type === 'Dropdown' ||\n component.type === 'RadioGroup' ||\n component.type === 'FileInput' ||\n component.type === 'Checkbox' ||\n component.type === 'Selector' ||\n component.type === 'AccountSelector')\n ) {\n const formState = newState[currentForm.name] as FormState;\n assertNameIsUnique(formState, component.props.name);\n formState[component.props.name] = constructInputState(\n oldState,\n component,\n currentForm.name,\n );\n return;\n }\n\n // Stateful components outside a form\n if (\n component.type === 'Input' ||\n component.type === 'Dropdown' ||\n component.type === 'RadioGroup' ||\n component.type === 'FileInput' ||\n component.type === 'Checkbox' ||\n component.type === 'Selector' ||\n component.type === 'AccountSelector'\n ) {\n assertNameIsUnique(newState, component.props.name);\n newState[component.props.name] = constructInputState(oldState, component);\n }\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"]}
|