@metamask/snaps-controllers 9.9.0 → 9.11.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 +24 -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 +71 -91
- package/dist/interface/SnapInterfaceController.cjs.map +1 -1
- package/dist/interface/SnapInterfaceController.d.cts +2 -1
- package/dist/interface/SnapInterfaceController.d.cts.map +1 -1
- package/dist/interface/SnapInterfaceController.d.mts +2 -1
- package/dist/interface/SnapInterfaceController.d.mts.map +1 -1
- package/dist/interface/SnapInterfaceController.mjs +71 -91
- package/dist/interface/SnapInterfaceController.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
|
/**
|
|
@@ -79,16 +74,21 @@ class SnapInterfaceController extends base_controller_1.BaseController {
|
|
|
79
74
|
* @param snapId - The snap id requesting the update.
|
|
80
75
|
* @param id - The interface id.
|
|
81
76
|
* @param content - The new content.
|
|
77
|
+
* @param context - An optional interface context object.
|
|
82
78
|
*/
|
|
83
|
-
async updateInterface(snapId, id, content) {
|
|
84
|
-
this
|
|
79
|
+
async updateInterface(snapId, id, content, context) {
|
|
80
|
+
__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateArgs).call(this, snapId, id);
|
|
85
81
|
const element = (0, utils_2.getJsxInterface)(content);
|
|
86
|
-
await this
|
|
82
|
+
await __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateContent).call(this, element);
|
|
83
|
+
(0, utils_2.validateInterfaceContext)(context);
|
|
87
84
|
const oldState = this.state.interfaces[id].state;
|
|
88
85
|
const newState = (0, utils_2.constructState)(oldState, element);
|
|
89
86
|
this.update((draftState) => {
|
|
90
87
|
draftState.interfaces[id].state = newState;
|
|
91
88
|
draftState.interfaces[id].content = (0, immer_1.castDraft)(element);
|
|
89
|
+
if (context) {
|
|
90
|
+
draftState.interfaces[id].context = context;
|
|
91
|
+
}
|
|
92
92
|
});
|
|
93
93
|
}
|
|
94
94
|
/**
|
|
@@ -121,81 +121,61 @@ class SnapInterfaceController extends base_controller_1.BaseController {
|
|
|
121
121
|
* @param value - The value to resolve the promise with.
|
|
122
122
|
*/
|
|
123
123
|
async resolveInterface(snapId, id, value) {
|
|
124
|
-
this
|
|
125
|
-
this
|
|
126
|
-
await this
|
|
124
|
+
__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateArgs).call(this, snapId, id);
|
|
125
|
+
__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateApproval).call(this, id);
|
|
126
|
+
await __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_acceptApprovalRequest).call(this, id, value);
|
|
127
127
|
this.deleteInterface(id);
|
|
128
128
|
}
|
|
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
129
|
}
|
|
200
130
|
exports.SnapInterfaceController = SnapInterfaceController;
|
|
131
|
+
_SnapInterfaceController_instances = new WeakSet(), _SnapInterfaceController_registerMessageHandlers = function _SnapInterfaceController_registerMessageHandlers() {
|
|
132
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:createInterface`, this.createInterface.bind(this));
|
|
133
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:getInterface`, this.getInterface.bind(this));
|
|
134
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:updateInterface`, this.updateInterface.bind(this));
|
|
135
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:deleteInterface`, this.deleteInterface.bind(this));
|
|
136
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:updateInterfaceState`, this.updateInterfaceState.bind(this));
|
|
137
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:resolveInterface`, this.resolveInterface.bind(this));
|
|
138
|
+
}, _SnapInterfaceController_validateArgs = function _SnapInterfaceController_validateArgs(snapId, id) {
|
|
139
|
+
const existingInterface = this.state.interfaces[id];
|
|
140
|
+
(0, utils_1.assert)(existingInterface !== undefined, `Interface with id '${id}' not found.`);
|
|
141
|
+
(0, utils_1.assert)(existingInterface.snapId === snapId, `Interface not created by ${snapId}.`);
|
|
142
|
+
}, _SnapInterfaceController_validateApproval = function _SnapInterfaceController_validateApproval(id) {
|
|
143
|
+
(0, utils_1.assert)(__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_hasApprovalRequest).call(this, id), `Approval request with id '${id}' not found.`);
|
|
144
|
+
}, _SnapInterfaceController_triggerPhishingListUpdate =
|
|
145
|
+
/**
|
|
146
|
+
* Trigger a Phishing list update if needed.
|
|
147
|
+
*/
|
|
148
|
+
async function _SnapInterfaceController_triggerPhishingListUpdate() {
|
|
149
|
+
await this.messagingSystem.call('PhishingController:maybeUpdateState');
|
|
150
|
+
}, _SnapInterfaceController_checkPhishingList = function _SnapInterfaceController_checkPhishingList(origin) {
|
|
151
|
+
return this.messagingSystem.call('PhishingController:testOrigin', origin)
|
|
152
|
+
.result;
|
|
153
|
+
}, _SnapInterfaceController_hasApprovalRequest = function _SnapInterfaceController_hasApprovalRequest(id) {
|
|
154
|
+
return this.messagingSystem.call('ApprovalController:hasRequest', {
|
|
155
|
+
id,
|
|
156
|
+
});
|
|
157
|
+
}, _SnapInterfaceController_acceptApprovalRequest =
|
|
158
|
+
/**
|
|
159
|
+
* Accept an approval request for a given interface.
|
|
160
|
+
*
|
|
161
|
+
* @param id - The interface id.
|
|
162
|
+
* @param value - The value to resolve the promise with.
|
|
163
|
+
*/
|
|
164
|
+
async function _SnapInterfaceController_acceptApprovalRequest(id, value) {
|
|
165
|
+
await this.messagingSystem.call('ApprovalController:acceptRequest', id, value);
|
|
166
|
+
}, _SnapInterfaceController_validateContent =
|
|
167
|
+
/**
|
|
168
|
+
* Utility function to validate the components of an interface.
|
|
169
|
+
* Throws if something is invalid.
|
|
170
|
+
*
|
|
171
|
+
* @param element - The JSX element to verify.
|
|
172
|
+
*/
|
|
173
|
+
async function _SnapInterfaceController_validateContent(element) {
|
|
174
|
+
// We assume the validity of this JSON to be validated by the caller.
|
|
175
|
+
// E.g., in the RPC method implementation.
|
|
176
|
+
const size = (0, snaps_utils_1.getJsonSizeUnsafe)(element);
|
|
177
|
+
(0, utils_1.assert)(size <= MAX_UI_CONTENT_SIZE, `A Snap UI may not be larger than ${MAX_UI_CONTENT_SIZE / 1000000} MB.`);
|
|
178
|
+
await __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_triggerPhishingListUpdate).call(this);
|
|
179
|
+
(0, snaps_utils_1.validateJsxLinks)(element, __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_checkPhishingList).bind(this), (id) => this.messagingSystem.call('SnapController:get', id));
|
|
180
|
+
};
|
|
201
181
|
//# 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;QAC3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AE,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,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eAAe,CAAC,IAAI,CAAC,qCAAqC,CAAC,CAAC;AACzE,CAAC,mGAQkB,MAAc;IAC/B,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,+BAA+B,EAAE,MAAM,CAAC;SACtE,MAAM,CAAC;AACZ,CAAC,qGASmB,EAAU;IAC5B,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,+BAA+B,EAAE;QAChE,EAAE;KACH,CAAC,CAAC;AACL,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,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 * @param context - An optional interface context object.\n */\n async updateInterface(\n snapId: SnapId,\n id: string,\n content: ComponentOrElement,\n context?: InterfaceContext,\n ) {\n this.#validateArgs(snapId, id);\n const element = getJsxInterface(content);\n await this.#validateContent(element);\n validateInterfaceContext(context);\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 if (context) {\n draftState.interfaces[id].context = context;\n }\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"]}
|
|
@@ -79,8 +79,9 @@ export declare class SnapInterfaceController extends BaseController<typeof contr
|
|
|
79
79
|
* @param snapId - The snap id requesting the update.
|
|
80
80
|
* @param id - The interface id.
|
|
81
81
|
* @param content - The new content.
|
|
82
|
+
* @param context - An optional interface context object.
|
|
82
83
|
*/
|
|
83
|
-
updateInterface(snapId: SnapId, id: string, content: ComponentOrElement): Promise<void>;
|
|
84
|
+
updateInterface(snapId: SnapId, id: string, content: ComponentOrElement, context?: InterfaceContext): Promise<void>;
|
|
84
85
|
/**
|
|
85
86
|
* Delete an interface from state.
|
|
86
87
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapInterfaceController.d.cts","sourceRoot":"","sources":["../../src/interface/SnapInterfaceController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,aAAa,EACb,kBAAkB,EACnB,sCAAsC;AACvC,OAAO,KAAK,EACV,6BAA6B,EAC7B,wBAAwB,EACxB,0BAA0B,EAC3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EACV,gBAAgB,EAChB,UAAU,EACX,sCAAsC;AACvC,OAAO,KAAK,EACV,cAAc,EACd,MAAM,EACN,kBAAkB,EAClB,gBAAgB,EACjB,4BAA4B;AAC7B,OAAO,KAAK,EAAE,UAAU,EAAE,gCAAgC;AAE1D,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAK5C,OAAO,KAAK,EAAE,OAAO,EAAE,2BAAiB;AASxC,QAAA,MAAM,cAAc,4BAA4B,CAAC;AAEjD,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,GAAG,OAAO,cAAc,kBAAkB,CAAC;IACjD,OAAO,EAAE,uBAAuB,CAAC,iBAAiB,CAAC,CAAC;CACrD,CAAC;AAEF,MAAM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8BAA8B,GAAG,qCAAqC,EACtE,6BAA6B,EAC7B,qCAAqC,CAAC,MAAM,CAAC,EAC7C,6BAA6B,CAAC,MAAM,CAAC,CACtC,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,UAAU,CAAC;IACpB,KAAK,EAAE,cAAc,CAAC;IACtB,OAAO,EAAE,gBAAgB,GAAG,IAAI,CAAC;CAClC,CAAC;AAEF,MAAM,MAAM,4BAA4B,GAAG;IACzC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;CAC7C,CAAC;AAEF,MAAM,MAAM,2BAA2B,GAAG;IACxC,SAAS,EAAE,gCAAgC,CAAC;IAC5C,KAAK,CAAC,EAAE,4BAA4B,CAAC;CACtC,CAAC;AAEF;;GAEG;AACH,qBAAa,uBAAwB,SAAQ,cAAc,CACzD,OAAO,cAAc,EACrB,4BAA4B,EAC5B,gCAAgC,CACjC;;gBACa,EAAE,SAAS,EAAE,KAAK,EAAE,EAAE,2BAA2B;IAiD7D;;;;;;;OAOG;IACG,eAAe,CACnB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,kBAAkB,EAC3B,OAAO,CAAC,EAAE,gBAAgB;IAuB5B;;;;;;OAMG;IACH,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM;IAMvC
|
|
1
|
+
{"version":3,"file":"SnapInterfaceController.d.cts","sourceRoot":"","sources":["../../src/interface/SnapInterfaceController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,aAAa,EACb,kBAAkB,EACnB,sCAAsC;AACvC,OAAO,KAAK,EACV,6BAA6B,EAC7B,wBAAwB,EACxB,0BAA0B,EAC3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EACV,gBAAgB,EAChB,UAAU,EACX,sCAAsC;AACvC,OAAO,KAAK,EACV,cAAc,EACd,M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,EAAE,MAAM;IAMvC;;;;;;;OAOG;IACG,eAAe,CACnB,MAAM,EAAE,MAAM,EACd,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,kBAAkB,EAC3B,OAAO,CAAC,EAAE,gBAAgB;IAmB5B;;;;OAIG;IACH,eAAe,CAAC,EAAE,EAAE,MAAM;IAM1B;;;;;OAKG;IACH,oBAAoB,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,cAAc;IAMtD;;;;;;;OAOG;IACG,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI;CA2G/D"}
|
|
@@ -79,8 +79,9 @@ export declare class SnapInterfaceController extends BaseController<typeof contr
|
|
|
79
79
|
* @param snapId - The snap id requesting the update.
|
|
80
80
|
* @param id - The interface id.
|
|
81
81
|
* @param content - The new content.
|
|
82
|
+
* @param context - An optional interface context object.
|
|
82
83
|
*/
|
|
83
|
-
updateInterface(snapId: SnapId, id: string, content: ComponentOrElement): Promise<void>;
|
|
84
|
+
updateInterface(snapId: SnapId, id: string, content: ComponentOrElement, context?: InterfaceContext): Promise<void>;
|
|
84
85
|
/**
|
|
85
86
|
* Delete an interface from state.
|
|
86
87
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapInterfaceController.d.mts","sourceRoot":"","sources":["../../src/interface/SnapInterfaceController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,aAAa,EACb,kBAAkB,EACnB,sCAAsC;AACvC,OAAO,KAAK,EACV,6BAA6B,EAC7B,wBAAwB,EACxB,0BAA0B,EAC3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EACV,gBAAgB,EAChB,UAAU,EACX,sCAAsC;AACvC,OAAO,KAAK,EACV,cAAc,EACd,MAAM,EACN,kBAAkB,EAClB,gBAAgB,EACjB,4BAA4B;AAC7B,OAAO,KAAK,EAAE,UAAU,EA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SAAQ,cAAc,CACzD,OAAO,cAAc,EACrB,4BAA4B,EAC5B,gCAAgC,CACjC;;gBACa,EAAE,SAAS,EAAE,KAAK,EAAE,EAAE,2BAA2B;IAiD7D;;;;;;;OAOG;IACG,eAAe,CACnB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,kBAAkB,EAC3B,OAAO,CAAC,EAAE,gBAAgB;IAuB5B;;;;;;OAMG;IACH,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM;IAMvC;;;;;;;OAOG;IACG,eAAe,CACnB,MAAM,EAAE,MAAM,EACd,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,kBAAkB,EAC3B,OAAO,CAAC,EAAE,gBAAgB;IAmB5B;;;;OAIG;IACH,eAAe,CAAC,EAAE,EAAE,MAAM;IAM1B;;;;;OAKG;IACH,oBAAoB,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,cAAc;IAMtD;;;;;;;OAOG;IACG,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI;CA2G/D"}
|
|
@@ -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
|
/**
|
|
@@ -76,16 +71,21 @@ export class SnapInterfaceController extends BaseController {
|
|
|
76
71
|
* @param snapId - The snap id requesting the update.
|
|
77
72
|
* @param id - The interface id.
|
|
78
73
|
* @param content - The new content.
|
|
74
|
+
* @param context - An optional interface context object.
|
|
79
75
|
*/
|
|
80
|
-
async updateInterface(snapId, id, content) {
|
|
81
|
-
this
|
|
76
|
+
async updateInterface(snapId, id, content, context) {
|
|
77
|
+
__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateArgs).call(this, snapId, id);
|
|
82
78
|
const element = getJsxInterface(content);
|
|
83
|
-
await this
|
|
79
|
+
await __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateContent).call(this, element);
|
|
80
|
+
validateInterfaceContext(context);
|
|
84
81
|
const oldState = this.state.interfaces[id].state;
|
|
85
82
|
const newState = constructState(oldState, element);
|
|
86
83
|
this.update((draftState) => {
|
|
87
84
|
draftState.interfaces[id].state = newState;
|
|
88
85
|
draftState.interfaces[id].content = castDraft(element);
|
|
86
|
+
if (context) {
|
|
87
|
+
draftState.interfaces[id].context = context;
|
|
88
|
+
}
|
|
89
89
|
});
|
|
90
90
|
}
|
|
91
91
|
/**
|
|
@@ -118,80 +118,60 @@ export class SnapInterfaceController extends BaseController {
|
|
|
118
118
|
* @param value - The value to resolve the promise with.
|
|
119
119
|
*/
|
|
120
120
|
async resolveInterface(snapId, id, value) {
|
|
121
|
-
this
|
|
122
|
-
this
|
|
123
|
-
await this
|
|
121
|
+
__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateArgs).call(this, snapId, id);
|
|
122
|
+
__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_validateApproval).call(this, id);
|
|
123
|
+
await __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_acceptApprovalRequest).call(this, id, value);
|
|
124
124
|
this.deleteInterface(id);
|
|
125
125
|
}
|
|
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
126
|
}
|
|
127
|
+
_SnapInterfaceController_instances = new WeakSet(), _SnapInterfaceController_registerMessageHandlers = function _SnapInterfaceController_registerMessageHandlers() {
|
|
128
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:createInterface`, this.createInterface.bind(this));
|
|
129
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:getInterface`, this.getInterface.bind(this));
|
|
130
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:updateInterface`, this.updateInterface.bind(this));
|
|
131
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:deleteInterface`, this.deleteInterface.bind(this));
|
|
132
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:updateInterfaceState`, this.updateInterfaceState.bind(this));
|
|
133
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:resolveInterface`, this.resolveInterface.bind(this));
|
|
134
|
+
}, _SnapInterfaceController_validateArgs = function _SnapInterfaceController_validateArgs(snapId, id) {
|
|
135
|
+
const existingInterface = this.state.interfaces[id];
|
|
136
|
+
assert(existingInterface !== undefined, `Interface with id '${id}' not found.`);
|
|
137
|
+
assert(existingInterface.snapId === snapId, `Interface not created by ${snapId}.`);
|
|
138
|
+
}, _SnapInterfaceController_validateApproval = function _SnapInterfaceController_validateApproval(id) {
|
|
139
|
+
assert(__classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_hasApprovalRequest).call(this, id), `Approval request with id '${id}' not found.`);
|
|
140
|
+
}, _SnapInterfaceController_triggerPhishingListUpdate =
|
|
141
|
+
/**
|
|
142
|
+
* Trigger a Phishing list update if needed.
|
|
143
|
+
*/
|
|
144
|
+
async function _SnapInterfaceController_triggerPhishingListUpdate() {
|
|
145
|
+
await this.messagingSystem.call('PhishingController:maybeUpdateState');
|
|
146
|
+
}, _SnapInterfaceController_checkPhishingList = function _SnapInterfaceController_checkPhishingList(origin) {
|
|
147
|
+
return this.messagingSystem.call('PhishingController:testOrigin', origin)
|
|
148
|
+
.result;
|
|
149
|
+
}, _SnapInterfaceController_hasApprovalRequest = function _SnapInterfaceController_hasApprovalRequest(id) {
|
|
150
|
+
return this.messagingSystem.call('ApprovalController:hasRequest', {
|
|
151
|
+
id,
|
|
152
|
+
});
|
|
153
|
+
}, _SnapInterfaceController_acceptApprovalRequest =
|
|
154
|
+
/**
|
|
155
|
+
* Accept an approval request for a given interface.
|
|
156
|
+
*
|
|
157
|
+
* @param id - The interface id.
|
|
158
|
+
* @param value - The value to resolve the promise with.
|
|
159
|
+
*/
|
|
160
|
+
async function _SnapInterfaceController_acceptApprovalRequest(id, value) {
|
|
161
|
+
await this.messagingSystem.call('ApprovalController:acceptRequest', id, value);
|
|
162
|
+
}, _SnapInterfaceController_validateContent =
|
|
163
|
+
/**
|
|
164
|
+
* Utility function to validate the components of an interface.
|
|
165
|
+
* Throws if something is invalid.
|
|
166
|
+
*
|
|
167
|
+
* @param element - The JSX element to verify.
|
|
168
|
+
*/
|
|
169
|
+
async function _SnapInterfaceController_validateContent(element) {
|
|
170
|
+
// We assume the validity of this JSON to be validated by the caller.
|
|
171
|
+
// E.g., in the RPC method implementation.
|
|
172
|
+
const size = getJsonSizeUnsafe(element);
|
|
173
|
+
assert(size <= MAX_UI_CONTENT_SIZE, `A Snap UI may not be larger than ${MAX_UI_CONTENT_SIZE / 1000000} MB.`);
|
|
174
|
+
await __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_triggerPhishingListUpdate).call(this);
|
|
175
|
+
validateJsxLinks(element, __classPrivateFieldGet(this, _SnapInterfaceController_instances, "m", _SnapInterfaceController_checkPhishingList).bind(this), (id) => this.messagingSystem.call('SnapController:get', id));
|
|
176
|
+
};
|
|
197
177
|
//# 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,oBAAg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AAC,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;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AU;IAC1B,MAAM,CACJ,uBAAA,IAAI,uFAAoB,MAAxB,IAAI,EAAqB,EAAE,CAAC,EAC5B,6BAA6B,EAAE,cAAc,CAC9C,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,KAAK;IACH,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,qCAAqC,CAAC,CAAC;AACzE,CAAC,mGAQkB,MAAc;IAC/B,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,+BAA+B,EAAE,MAAM,CAAC;SACtE,MAAM,CAAC;AACZ,CAAC,qGASmB,EAAU;IAC5B,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,+BAA+B,EAAE;QAChE,EAAE;KACH,CAAC,CAAC;AACL,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 * @param context - An optional interface context object.\n */\n async updateInterface(\n snapId: SnapId,\n id: string,\n content: ComponentOrElement,\n context?: InterfaceContext,\n ) {\n this.#validateArgs(snapId, id);\n const element = getJsxInterface(content);\n await this.#validateContent(element);\n validateInterfaceContext(context);\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 if (context) {\n draftState.interfaces[id].context = context;\n }\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"]}
|