@metamask/snaps-rpc-methods 15.1.1 → 16.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -4
- package/dist/index.cjs +1 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -2
- package/dist/index.mjs.map +1 -1
- package/dist/permissions.cjs +18 -8
- package/dist/permissions.cjs.map +1 -1
- package/dist/permissions.d.cts +3 -2
- package/dist/permissions.d.cts.map +1 -1
- package/dist/permissions.d.mts +3 -2
- package/dist/permissions.d.mts.map +1 -1
- package/dist/permissions.mjs +17 -7
- package/dist/permissions.mjs.map +1 -1
- package/dist/permitted/cancelBackgroundEvent.cjs +10 -14
- package/dist/permitted/cancelBackgroundEvent.cjs.map +1 -1
- package/dist/permitted/cancelBackgroundEvent.d.cts +9 -12
- package/dist/permitted/cancelBackgroundEvent.d.cts.map +1 -1
- package/dist/permitted/cancelBackgroundEvent.d.mts +9 -12
- package/dist/permitted/cancelBackgroundEvent.d.mts.map +1 -1
- package/dist/permitted/cancelBackgroundEvent.mjs +10 -14
- package/dist/permitted/cancelBackgroundEvent.mjs.map +1 -1
- package/dist/permitted/clearState.cjs +10 -16
- package/dist/permitted/clearState.cjs.map +1 -1
- package/dist/permitted/clearState.d.cts +9 -23
- package/dist/permitted/clearState.d.cts.map +1 -1
- package/dist/permitted/clearState.d.mts +9 -23
- package/dist/permitted/clearState.d.mts.map +1 -1
- package/dist/permitted/clearState.mjs +10 -16
- package/dist/permitted/clearState.mjs.map +1 -1
- package/dist/permitted/closeWebSocket.cjs +7 -14
- package/dist/permitted/closeWebSocket.cjs.map +1 -1
- package/dist/permitted/closeWebSocket.d.cts +9 -12
- package/dist/permitted/closeWebSocket.d.cts.map +1 -1
- package/dist/permitted/closeWebSocket.d.mts +9 -12
- package/dist/permitted/closeWebSocket.d.mts.map +1 -1
- package/dist/permitted/closeWebSocket.mjs +7 -14
- package/dist/permitted/closeWebSocket.mjs.map +1 -1
- package/dist/permitted/createInterface.cjs +11 -15
- package/dist/permitted/createInterface.cjs.map +1 -1
- package/dist/permitted/createInterface.d.cts +23 -37
- package/dist/permitted/createInterface.d.cts.map +1 -1
- package/dist/permitted/createInterface.d.mts +23 -37
- package/dist/permitted/createInterface.d.mts.map +1 -1
- package/dist/permitted/createInterface.mjs +11 -15
- package/dist/permitted/createInterface.mjs.map +1 -1
- package/dist/permitted/endTrace.cjs +4 -6
- package/dist/permitted/endTrace.cjs.map +1 -1
- package/dist/permitted/endTrace.d.cts +8 -11
- package/dist/permitted/endTrace.d.cts.map +1 -1
- package/dist/permitted/endTrace.d.mts +8 -11
- package/dist/permitted/endTrace.d.mts.map +1 -1
- package/dist/permitted/endTrace.mjs +4 -6
- package/dist/permitted/endTrace.mjs.map +1 -1
- package/dist/permitted/getAllSnaps.cjs +5 -11
- package/dist/permitted/getAllSnaps.cjs.map +1 -1
- package/dist/permitted/getAllSnaps.d.cts +10 -14
- package/dist/permitted/getAllSnaps.d.cts.map +1 -1
- package/dist/permitted/getAllSnaps.d.mts +10 -14
- package/dist/permitted/getAllSnaps.d.mts.map +1 -1
- package/dist/permitted/getAllSnaps.mjs +5 -11
- package/dist/permitted/getAllSnaps.mjs.map +1 -1
- package/dist/permitted/getBackgroundEvents.cjs +8 -15
- package/dist/permitted/getBackgroundEvents.cjs.map +1 -1
- package/dist/permitted/getBackgroundEvents.d.cts +10 -13
- package/dist/permitted/getBackgroundEvents.d.cts.map +1 -1
- package/dist/permitted/getBackgroundEvents.d.mts +10 -13
- package/dist/permitted/getBackgroundEvents.d.mts.map +1 -1
- package/dist/permitted/getBackgroundEvents.mjs +8 -15
- package/dist/permitted/getBackgroundEvents.mjs.map +1 -1
- package/dist/permitted/getClientStatus.cjs +5 -6
- package/dist/permitted/getClientStatus.cjs.map +1 -1
- package/dist/permitted/getClientStatus.d.cts +17 -18
- package/dist/permitted/getClientStatus.d.cts.map +1 -1
- package/dist/permitted/getClientStatus.d.mts +17 -18
- package/dist/permitted/getClientStatus.d.mts.map +1 -1
- package/dist/permitted/getClientStatus.mjs +5 -6
- package/dist/permitted/getClientStatus.mjs.map +1 -1
- package/dist/permitted/getFile.cjs +7 -11
- package/dist/permitted/getFile.cjs.map +1 -1
- package/dist/permitted/getFile.d.cts +8 -10
- package/dist/permitted/getFile.d.cts.map +1 -1
- package/dist/permitted/getFile.d.mts +8 -10
- package/dist/permitted/getFile.d.mts.map +1 -1
- package/dist/permitted/getFile.mjs +7 -11
- package/dist/permitted/getFile.mjs.map +1 -1
- package/dist/permitted/getInterfaceContext.cjs +12 -15
- package/dist/permitted/getInterfaceContext.cjs.map +1 -1
- package/dist/permitted/getInterfaceContext.d.cts +9 -21
- package/dist/permitted/getInterfaceContext.d.cts.map +1 -1
- package/dist/permitted/getInterfaceContext.d.mts +9 -21
- package/dist/permitted/getInterfaceContext.d.mts.map +1 -1
- package/dist/permitted/getInterfaceContext.mjs +12 -15
- package/dist/permitted/getInterfaceContext.mjs.map +1 -1
- package/dist/permitted/getInterfaceState.cjs +11 -15
- package/dist/permitted/getInterfaceState.cjs.map +1 -1
- package/dist/permitted/getInterfaceState.d.cts +9 -21
- package/dist/permitted/getInterfaceState.d.cts.map +1 -1
- package/dist/permitted/getInterfaceState.d.mts +9 -21
- package/dist/permitted/getInterfaceState.d.mts.map +1 -1
- package/dist/permitted/getInterfaceState.mjs +11 -15
- package/dist/permitted/getInterfaceState.mjs.map +1 -1
- package/dist/permitted/getSnaps.cjs +6 -12
- package/dist/permitted/getSnaps.cjs.map +1 -1
- package/dist/permitted/getSnaps.d.cts +11 -14
- package/dist/permitted/getSnaps.d.cts.map +1 -1
- package/dist/permitted/getSnaps.d.mts +11 -14
- package/dist/permitted/getSnaps.d.mts.map +1 -1
- package/dist/permitted/getSnaps.mjs +6 -12
- package/dist/permitted/getSnaps.mjs.map +1 -1
- package/dist/permitted/getState.cjs +9 -11
- package/dist/permitted/getState.cjs.map +1 -1
- package/dist/permitted/getState.d.cts +17 -28
- package/dist/permitted/getState.d.cts.map +1 -1
- package/dist/permitted/getState.d.mts +17 -28
- package/dist/permitted/getState.d.mts.map +1 -1
- package/dist/permitted/getState.mjs +9 -11
- package/dist/permitted/getState.mjs.map +1 -1
- package/dist/permitted/getWebSockets.cjs +11 -14
- package/dist/permitted/getWebSockets.cjs.map +1 -1
- package/dist/permitted/getWebSockets.d.cts +10 -13
- package/dist/permitted/getWebSockets.d.cts.map +1 -1
- package/dist/permitted/getWebSockets.d.mts +10 -13
- package/dist/permitted/getWebSockets.d.mts.map +1 -1
- package/dist/permitted/getWebSockets.mjs +11 -14
- package/dist/permitted/getWebSockets.mjs.map +1 -1
- package/dist/permitted/index.cjs +0 -1
- package/dist/permitted/index.cjs.map +1 -1
- package/dist/permitted/index.d.cts +29 -25
- package/dist/permitted/index.d.cts.map +1 -1
- package/dist/permitted/index.d.mts +29 -25
- package/dist/permitted/index.d.mts.map +1 -1
- package/dist/permitted/index.mjs +0 -1
- package/dist/permitted/index.mjs.map +1 -1
- package/dist/permitted/invokeKeyring.cjs +12 -13
- package/dist/permitted/invokeKeyring.cjs.map +1 -1
- package/dist/permitted/invokeKeyring.d.cts +17 -16
- package/dist/permitted/invokeKeyring.d.cts.map +1 -1
- package/dist/permitted/invokeKeyring.d.mts +17 -16
- package/dist/permitted/invokeKeyring.d.mts.map +1 -1
- package/dist/permitted/invokeKeyring.mjs +12 -13
- package/dist/permitted/invokeKeyring.mjs.map +1 -1
- package/dist/permitted/invokeSnapSugar.cjs +6 -10
- package/dist/permitted/invokeSnapSugar.cjs.map +1 -1
- package/dist/permitted/invokeSnapSugar.d.cts +9 -12
- package/dist/permitted/invokeSnapSugar.d.cts.map +1 -1
- package/dist/permitted/invokeSnapSugar.d.mts +9 -12
- package/dist/permitted/invokeSnapSugar.d.mts.map +1 -1
- package/dist/permitted/invokeSnapSugar.mjs +6 -10
- package/dist/permitted/invokeSnapSugar.mjs.map +1 -1
- package/dist/permitted/listEntropySources.cjs +28 -12
- package/dist/permitted/listEntropySources.cjs.map +1 -1
- package/dist/permitted/listEntropySources.d.cts +12 -23
- package/dist/permitted/listEntropySources.d.cts.map +1 -1
- package/dist/permitted/listEntropySources.d.mts +12 -23
- package/dist/permitted/listEntropySources.d.mts.map +1 -1
- package/dist/permitted/listEntropySources.mjs +28 -12
- package/dist/permitted/listEntropySources.mjs.map +1 -1
- package/dist/permitted/middleware.cjs +70 -20
- package/dist/permitted/middleware.cjs.map +1 -1
- package/dist/permitted/middleware.d.cts +5 -2
- package/dist/permitted/middleware.d.cts.map +1 -1
- package/dist/permitted/middleware.d.mts +5 -2
- package/dist/permitted/middleware.d.mts.map +1 -1
- package/dist/permitted/middleware.mjs +70 -20
- package/dist/permitted/middleware.mjs.map +1 -1
- package/dist/permitted/openWebSocket.cjs +7 -14
- package/dist/permitted/openWebSocket.cjs.map +1 -1
- package/dist/permitted/openWebSocket.d.cts +9 -12
- package/dist/permitted/openWebSocket.d.cts.map +1 -1
- package/dist/permitted/openWebSocket.d.mts +9 -12
- package/dist/permitted/openWebSocket.d.mts.map +1 -1
- package/dist/permitted/openWebSocket.mjs +7 -14
- package/dist/permitted/openWebSocket.mjs.map +1 -1
- package/dist/permitted/requestSnaps.cjs +14 -19
- package/dist/permitted/requestSnaps.cjs.map +1 -1
- package/dist/permitted/requestSnaps.d.cts +9 -36
- package/dist/permitted/requestSnaps.d.cts.map +1 -1
- package/dist/permitted/requestSnaps.d.mts +9 -36
- package/dist/permitted/requestSnaps.d.mts.map +1 -1
- package/dist/permitted/requestSnaps.mjs +15 -20
- package/dist/permitted/requestSnaps.mjs.map +1 -1
- package/dist/permitted/resolveInterface.cjs +11 -15
- package/dist/permitted/resolveInterface.cjs.map +1 -1
- package/dist/permitted/resolveInterface.d.cts +12 -24
- package/dist/permitted/resolveInterface.d.cts.map +1 -1
- package/dist/permitted/resolveInterface.d.mts +12 -24
- package/dist/permitted/resolveInterface.d.mts.map +1 -1
- package/dist/permitted/resolveInterface.mjs +11 -15
- package/dist/permitted/resolveInterface.mjs.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.cjs +11 -14
- package/dist/permitted/scheduleBackgroundEvent.cjs.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.d.cts +10 -17
- package/dist/permitted/scheduleBackgroundEvent.d.cts.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.d.mts +10 -17
- package/dist/permitted/scheduleBackgroundEvent.d.mts.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.mjs +11 -14
- package/dist/permitted/scheduleBackgroundEvent.mjs.map +1 -1
- package/dist/permitted/sendWebSocketMessage.cjs +10 -14
- package/dist/permitted/sendWebSocketMessage.cjs.map +1 -1
- package/dist/permitted/sendWebSocketMessage.d.cts +9 -12
- package/dist/permitted/sendWebSocketMessage.d.cts.map +1 -1
- package/dist/permitted/sendWebSocketMessage.d.mts +9 -12
- package/dist/permitted/sendWebSocketMessage.d.mts.map +1 -1
- package/dist/permitted/sendWebSocketMessage.mjs +10 -14
- package/dist/permitted/sendWebSocketMessage.mjs.map +1 -1
- package/dist/permitted/setState.cjs +18 -21
- package/dist/permitted/setState.cjs.map +1 -1
- package/dist/permitted/setState.d.cts +18 -45
- package/dist/permitted/setState.d.cts.map +1 -1
- package/dist/permitted/setState.d.mts +18 -45
- package/dist/permitted/setState.d.mts.map +1 -1
- package/dist/permitted/setState.mjs +18 -21
- package/dist/permitted/setState.mjs.map +1 -1
- package/dist/permitted/startTrace.cjs +4 -6
- package/dist/permitted/startTrace.cjs.map +1 -1
- package/dist/permitted/startTrace.d.cts +8 -11
- package/dist/permitted/startTrace.d.cts.map +1 -1
- package/dist/permitted/startTrace.d.mts +8 -11
- package/dist/permitted/startTrace.d.mts.map +1 -1
- package/dist/permitted/startTrace.mjs +4 -6
- package/dist/permitted/startTrace.mjs.map +1 -1
- package/dist/permitted/trackError.cjs +4 -6
- package/dist/permitted/trackError.cjs.map +1 -1
- package/dist/permitted/trackError.d.cts +8 -11
- package/dist/permitted/trackError.d.cts.map +1 -1
- package/dist/permitted/trackError.d.mts +8 -11
- package/dist/permitted/trackError.d.mts.map +1 -1
- package/dist/permitted/trackError.mjs +4 -6
- package/dist/permitted/trackError.mjs.map +1 -1
- package/dist/permitted/trackEvent.cjs +4 -6
- package/dist/permitted/trackEvent.cjs.map +1 -1
- package/dist/permitted/trackEvent.d.cts +8 -11
- package/dist/permitted/trackEvent.d.cts.map +1 -1
- package/dist/permitted/trackEvent.d.mts +8 -11
- package/dist/permitted/trackEvent.d.mts.map +1 -1
- package/dist/permitted/trackEvent.mjs +4 -6
- package/dist/permitted/trackEvent.mjs.map +1 -1
- package/dist/permitted/updateInterface.cjs +11 -15
- package/dist/permitted/updateInterface.cjs.map +1 -1
- package/dist/permitted/updateInterface.d.cts +9 -22
- package/dist/permitted/updateInterface.d.cts.map +1 -1
- package/dist/permitted/updateInterface.d.mts +9 -22
- package/dist/permitted/updateInterface.d.mts.map +1 -1
- package/dist/permitted/updateInterface.mjs +11 -15
- package/dist/permitted/updateInterface.mjs.map +1 -1
- package/dist/restricted/dialog.cjs +23 -30
- package/dist/restricted/dialog.cjs.map +1 -1
- package/dist/restricted/dialog.d.cts +10 -58
- package/dist/restricted/dialog.d.cts.map +1 -1
- package/dist/restricted/dialog.d.mts +10 -58
- package/dist/restricted/dialog.d.mts.map +1 -1
- package/dist/restricted/dialog.mjs +23 -30
- package/dist/restricted/dialog.mjs.map +1 -1
- package/dist/restricted/getBip32Entropy.cjs +15 -12
- package/dist/restricted/getBip32Entropy.cjs.map +1 -1
- package/dist/restricted/getBip32Entropy.d.cts +11 -24
- package/dist/restricted/getBip32Entropy.d.cts.map +1 -1
- package/dist/restricted/getBip32Entropy.d.mts +11 -24
- package/dist/restricted/getBip32Entropy.d.mts.map +1 -1
- package/dist/restricted/getBip32Entropy.mjs +16 -13
- package/dist/restricted/getBip32Entropy.mjs.map +1 -1
- package/dist/restricted/getBip32PublicKey.cjs +15 -12
- package/dist/restricted/getBip32PublicKey.cjs.map +1 -1
- package/dist/restricted/getBip32PublicKey.d.cts +11 -24
- package/dist/restricted/getBip32PublicKey.d.cts.map +1 -1
- package/dist/restricted/getBip32PublicKey.d.mts +11 -24
- package/dist/restricted/getBip32PublicKey.d.mts.map +1 -1
- package/dist/restricted/getBip32PublicKey.mjs +16 -13
- package/dist/restricted/getBip32PublicKey.mjs.map +1 -1
- package/dist/restricted/getBip44Entropy.cjs +14 -10
- package/dist/restricted/getBip44Entropy.cjs.map +1 -1
- package/dist/restricted/getBip44Entropy.d.cts +11 -15
- package/dist/restricted/getBip44Entropy.d.cts.map +1 -1
- package/dist/restricted/getBip44Entropy.d.mts +11 -15
- package/dist/restricted/getBip44Entropy.d.mts.map +1 -1
- package/dist/restricted/getBip44Entropy.mjs +15 -11
- package/dist/restricted/getBip44Entropy.mjs.map +1 -1
- package/dist/restricted/getEntropy.cjs +10 -10
- package/dist/restricted/getEntropy.cjs.map +1 -1
- package/dist/restricted/getEntropy.d.cts +11 -15
- package/dist/restricted/getEntropy.d.cts.map +1 -1
- package/dist/restricted/getEntropy.d.mts +11 -15
- package/dist/restricted/getEntropy.d.mts.map +1 -1
- package/dist/restricted/getEntropy.mjs +11 -11
- package/dist/restricted/getEntropy.mjs.map +1 -1
- package/dist/restricted/index.cjs.map +1 -1
- package/dist/restricted/index.d.cts +20 -160
- package/dist/restricted/index.d.cts.map +1 -1
- package/dist/restricted/index.d.mts +20 -160
- package/dist/restricted/index.d.mts.map +1 -1
- package/dist/restricted/index.mjs.map +1 -1
- package/dist/restricted/invokeSnap.cjs +11 -12
- package/dist/restricted/invokeSnap.cjs.map +1 -1
- package/dist/restricted/invokeSnap.d.cts +9 -13
- package/dist/restricted/invokeSnap.d.cts.map +1 -1
- package/dist/restricted/invokeSnap.d.mts +9 -13
- package/dist/restricted/invokeSnap.d.mts.map +1 -1
- package/dist/restricted/invokeSnap.mjs +11 -12
- package/dist/restricted/invokeSnap.mjs.map +1 -1
- package/dist/restricted/manageState.cjs +21 -20
- package/dist/restricted/manageState.cjs.map +1 -1
- package/dist/restricted/manageState.d.cts +12 -34
- package/dist/restricted/manageState.d.cts.map +1 -1
- package/dist/restricted/manageState.d.mts +12 -34
- package/dist/restricted/manageState.d.mts.map +1 -1
- package/dist/restricted/manageState.mjs +21 -20
- package/dist/restricted/manageState.mjs.map +1 -1
- package/dist/restricted/notify.cjs +31 -24
- package/dist/restricted/notify.cjs.map +1 -1
- package/dist/restricted/notify.d.cts +16 -25
- package/dist/restricted/notify.d.cts.map +1 -1
- package/dist/restricted/notify.d.mts +16 -25
- package/dist/restricted/notify.d.mts.map +1 -1
- package/dist/restricted/notify.mjs +32 -25
- package/dist/restricted/notify.mjs.map +1 -1
- package/dist/types.cjs +3 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +155 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +155 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +2 -0
- package/dist/types.mjs.map +1 -0
- package/dist/utils.cjs +78 -24
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +21 -13
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +21 -13
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +75 -22
- package/dist/utils.mjs.map +1 -1
- package/package.json +6 -6
- package/dist/permitted/handlers.cjs +0 -63
- package/dist/permitted/handlers.cjs.map +0 -1
- package/dist/permitted/handlers.d.cts +0 -299
- package/dist/permitted/handlers.d.cts.map +0 -1
- package/dist/permitted/handlers.d.mts +0 -299
- package/dist/permitted/handlers.d.mts.map +0 -1
- package/dist/permitted/handlers.mjs +0 -60
- package/dist/permitted/handlers.mjs.map +0 -1
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { rpcErrors } from "@metamask/rpc-errors";
|
|
2
2
|
import { create, object, optional, record, refine, string, StructError } from "@metamask/superstruct";
|
|
3
3
|
import { JsonStruct } from "@metamask/utils";
|
|
4
|
-
const methodName = 'snap_trackEvent';
|
|
5
4
|
const PropertiesStruct = optional(record(string(), JsonStruct));
|
|
6
5
|
const snakeCaseRegex = /^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/u;
|
|
7
6
|
const SnakeCasePropertiesStruct = refine(PropertiesStruct, 'snake_case_keys', (value) => {
|
|
@@ -12,7 +11,6 @@ const SnakeCasePropertiesStruct = refine(PropertiesStruct, 'snake_case_keys', (v
|
|
|
12
11
|
});
|
|
13
12
|
const hookNames = {
|
|
14
13
|
trackEvent: true,
|
|
15
|
-
getSnap: true,
|
|
16
14
|
};
|
|
17
15
|
const TrackEventParametersStruct = object({
|
|
18
16
|
event: object({
|
|
@@ -27,9 +25,9 @@ const TrackEventParametersStruct = object({
|
|
|
27
25
|
* @internal
|
|
28
26
|
*/
|
|
29
27
|
export const trackEventHandler = {
|
|
30
|
-
methodNames: [methodName],
|
|
31
28
|
implementation: getTrackEventImplementation,
|
|
32
29
|
hookNames,
|
|
30
|
+
actionNames: ['SnapController:getSnap'],
|
|
33
31
|
};
|
|
34
32
|
/**
|
|
35
33
|
* The `snap_trackEvent` method implementation.
|
|
@@ -41,11 +39,11 @@ export const trackEventHandler = {
|
|
|
41
39
|
* @param end - The `json-rpc-engine` "end" callback.
|
|
42
40
|
* @param hooks - The RPC method hooks.
|
|
43
41
|
* @param hooks.trackEvent - The function to track the event.
|
|
44
|
-
* @param
|
|
42
|
+
* @param messenger - The messenger used to call controller actions.
|
|
45
43
|
* @returns Nothing.
|
|
46
44
|
*/
|
|
47
|
-
function getTrackEventImplementation(req, res, _next, end, { trackEvent,
|
|
48
|
-
const snap = getSnap
|
|
45
|
+
function getTrackEventImplementation(req, res, _next, end, { trackEvent }, messenger) {
|
|
46
|
+
const snap = messenger.call('SnapController:getSnap', req.origin);
|
|
49
47
|
if (!snap?.preinstalled) {
|
|
50
48
|
return end(rpcErrors.methodNotFound());
|
|
51
49
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trackEvent.mjs","sourceRoot":"","sources":["../../src/permitted/trackEvent.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"trackEvent.mjs","sourceRoot":"","sources":["../../src/permitted/trackEvent.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAGjD,OAAO,EACL,MAAM,EACN,MAAM,EACN,QAAQ,EACR,MAAM,EACN,MAAM,EACN,MAAM,EACN,WAAW,EACZ,8BAA8B;AAE/B,OAAO,EAAE,UAAU,EAAE,wBAAwB;AAQ7C,MAAM,gBAAgB,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,UAAU,CAAC,CAAC,CAAC;AAEhE,MAAM,cAAc,GAAG,kCAAkC,CAAC;AAE1D,MAAM,yBAAyB,GAAG,MAAM,CACtC,gBAAgB,EAChB,iBAAiB,EACjB,CAAC,KAAK,EAAE,EAAE;IACR,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,cAAc,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AACrE,CAAC,CACF,CAAC;AAEF,MAAM,SAAS,GAA6C;IAC1D,UAAU,EAAE,IAAI;CACjB,CAAC;AAmBF,MAAM,0BAA0B,GAAG,MAAM,CAAC;IACxC,KAAK,EAAE,MAAM,CAAC;QACZ,KAAK,EAAE,MAAM,EAAE;QACf,UAAU,EAAE,yBAAyB;QACrC,mBAAmB,EAAE,yBAAyB;KAC/C,CAAC;CACH,CAAC,CAAC;AAOH;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAC/B,cAAc,EAAE,2BAA2B;IAC3C,SAAS;IACT,WAAW,EAAE,CAAC,wBAAwB,CA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B,MAAM,SAAS,CAAC,QAAQ,EAAE,CAAC;IAC7B,CAAC;AACH,CAAC","sourcesContent":["import type {\n JsonRpcEngineEndCallback,\n MethodHandler,\n} from '@metamask/json-rpc-engine';\nimport type { Messenger } from '@metamask/messenger';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { TrackEventParams, TrackEventResult } from '@metamask/snaps-sdk';\nimport type { InferMatching } from '@metamask/snaps-utils';\nimport {\n create,\n object,\n optional,\n record,\n refine,\n string,\n StructError,\n} from '@metamask/superstruct';\nimport type { Json, PendingJsonRpcResponse } from '@metamask/utils';\nimport { JsonStruct } from '@metamask/utils';\n\nimport type {\n JsonRpcRequestWithOrigin,\n SnapControllerGetSnapAction,\n} from '../types';\nimport type { MethodHooksObject } from '../utils';\n\nconst PropertiesStruct = optional(record(string(), JsonStruct));\n\nconst snakeCaseRegex = /^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/u;\n\nconst SnakeCasePropertiesStruct = refine(\n PropertiesStruct,\n 'snake_case_keys',\n (value) => {\n if (!value) {\n return true;\n }\n\n return Object.keys(value).every((key) => snakeCaseRegex.test(key));\n },\n);\n\nconst hookNames: MethodHooksObject<TrackEventMethodHooks> = {\n trackEvent: true,\n};\n\nexport type TrackEventMethodHooks = {\n /**\n * Track an event.\n *\n * @param event - The event object containing event details and properties.\n */\n trackEvent: (event: TrackEventObject) => void;\n};\n\nexport type TrackEventMethodActions = SnapControllerGetSnapAction;\n\nexport type TrackEventObject = {\n event: string;\n properties?: Record<string, Json>;\n sensitiveProperties?: Record<string, Json>;\n};\n\nconst TrackEventParametersStruct = object({\n event: object({\n event: string(),\n properties: SnakeCasePropertiesStruct,\n sensitiveProperties: SnakeCasePropertiesStruct,\n }),\n});\n\nexport type TrackEventParameters = InferMatching<\n typeof TrackEventParametersStruct,\n TrackEventParams\n>;\n\n/**\n * Handler for the `snap_trackEvent` method.\n *\n * @internal\n */\nexport const trackEventHandler = {\n implementation: getTrackEventImplementation,\n hookNames,\n actionNames: ['SnapController:getSnap'],\n} satisfies MethodHandler<\n TrackEventMethodHooks,\n TrackEventMethodActions,\n TrackEventParameters,\n TrackEventResult,\n { origin: string }\n>;\n\n/**\n * The `snap_trackEvent` method implementation.\n * This method allows pre-installed Snaps to submit tracking events to the client.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.trackEvent - The function to track the event.\n * @param messenger - The messenger used to call controller actions.\n * @returns Nothing.\n */\nfunction getTrackEventImplementation(\n req: JsonRpcRequestWithOrigin<TrackEventParameters>,\n res: PendingJsonRpcResponse<TrackEventResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { trackEvent }: TrackEventMethodHooks,\n messenger: Messenger<string, TrackEventMethodActions>,\n): void {\n const snap = messenger.call('SnapController:getSnap', req.origin);\n\n if (!snap?.preinstalled) {\n return end(rpcErrors.methodNotFound());\n }\n\n const { params } = req;\n\n try {\n const validatedParams = getValidatedParams(params);\n trackEvent(validatedParams.event);\n res.result = null;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validates the parameters for the snap_trackEvent method.\n *\n * @param params - Parameters to validate.\n * @returns Validated parameters.\n * @throws Throws RPC error if validation fails.\n */\nfunction getValidatedParams(params: unknown): TrackEventParameters {\n try {\n return create(params, TrackEventParametersStruct);\n } catch (error) {\n if (error instanceof StructError) {\n if (error.refinement === 'snake_case_keys') {\n throw rpcErrors.invalidParams({\n message: `Invalid params: All property keys must be in snake_case format. The following key contains invalid properties: \"${error.key}\".`,\n });\n }\n\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"]}
|
|
@@ -5,11 +5,6 @@ const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
|
5
5
|
const snaps_sdk_1 = require("@metamask/snaps-sdk");
|
|
6
6
|
const superstruct_1 = require("@metamask/superstruct");
|
|
7
7
|
const utils_1 = require("../utils.cjs");
|
|
8
|
-
const methodName = 'snap_updateInterface';
|
|
9
|
-
const hookNames = {
|
|
10
|
-
hasPermission: true,
|
|
11
|
-
updateInterface: true,
|
|
12
|
-
};
|
|
13
8
|
/**
|
|
14
9
|
* Update an interactive interface. For use in
|
|
15
10
|
* [interactive UI](https://docs.metamask.io/snaps/features/custom-ui/interactive-ui/).
|
|
@@ -46,9 +41,11 @@ const hookNames = {
|
|
|
46
41
|
* ```
|
|
47
42
|
*/
|
|
48
43
|
exports.updateInterfaceHandler = {
|
|
49
|
-
methodNames: [methodName],
|
|
50
44
|
implementation: getUpdateInterfaceImplementation,
|
|
51
|
-
|
|
45
|
+
actionNames: [
|
|
46
|
+
'PermissionController:hasPermission',
|
|
47
|
+
'SnapInterfaceController:updateInterface',
|
|
48
|
+
],
|
|
52
49
|
};
|
|
53
50
|
const UpdateInterfaceParametersStruct = (0, superstruct_1.object)({
|
|
54
51
|
id: (0, superstruct_1.string)(),
|
|
@@ -63,23 +60,22 @@ const UpdateInterfaceParametersStruct = (0, superstruct_1.object)({
|
|
|
63
60
|
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
64
61
|
* function.
|
|
65
62
|
* @param end - The `json-rpc-engine` "end" callback.
|
|
66
|
-
* @param
|
|
67
|
-
* @param
|
|
68
|
-
* permission.
|
|
69
|
-
* @param hooks.updateInterface - The function to update the interface.
|
|
63
|
+
* @param _hooks - The RPC method hooks. Not used by this function.
|
|
64
|
+
* @param messenger - The messenger used to call controller actions.
|
|
70
65
|
* @returns Nothing.
|
|
71
66
|
*/
|
|
72
|
-
function getUpdateInterfaceImplementation(req, res, _next, end,
|
|
73
|
-
|
|
67
|
+
function getUpdateInterfaceImplementation(req, res, _next, end, _hooks, messenger) {
|
|
68
|
+
const { params, origin } = req;
|
|
69
|
+
const isPermitted = utils_1.UI_PERMISSIONS.some((permission) => messenger.call('PermissionController:hasPermission', origin, permission));
|
|
70
|
+
if (!isPermitted) {
|
|
74
71
|
return end(rpc_errors_1.providerErrors.unauthorized({
|
|
75
72
|
message: `This method can only be used if the Snap has one of the following permissions: ${utils_1.UI_PERMISSIONS.join(', ')}.`,
|
|
76
73
|
}));
|
|
77
74
|
}
|
|
78
|
-
const { params } = req;
|
|
79
75
|
try {
|
|
80
76
|
const validatedParams = getValidatedParams(params);
|
|
81
77
|
const { id, ui, context } = validatedParams;
|
|
82
|
-
updateInterface
|
|
78
|
+
messenger.call('SnapInterfaceController:updateInterface', origin, id, ui, context);
|
|
83
79
|
res.result = null;
|
|
84
80
|
}
|
|
85
81
|
catch (error) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"updateInterface.cjs","sourceRoot":"","sources":["../../src/permitted/updateInterface.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"updateInterface.cjs","sourceRoot":"","sources":["../../src/permitted/updateInterface.ts"],"names":[],"mappings":";;;AAMA,qDAAiE;AAKjE,mDAG6B;AAE7B,uDAM+B;AAO/B,wCAA0C;AAM1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACU,QAAA,sBAAsB,GAAG;IACpC,cAAc,EAAE,gCAAgC;IAChD,WAAW,EAAE;QACX,oCAAoC;QACpC,yCAAyC;KAC1C;CAOF,CAAC;AAEF,MAAM,+BAA+B,GAAG,IAAA,oBAAM,EAAC;IAC7C,EAAE,EAAE,IAAA,oBAAM,GAAE;IACZ,EAAE,EAAE,oCAAwB;IAC5B,OAAO,EAAE,IAAA,sBAAQ,EAAC,kCAAsB,CAAC;CAC1C,CAAC,CAAC;AAOH;;;;;;;;;;;GAWG;AACH,SAAS,gCAAgC,CACvC,GAAwD,EACxD,GAAkD,EAClD,KAAc,EACd,GAA6B,EAC7B,MAAa,EACb,SAA0D;IAE1D,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC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amask/json-rpc-engine';\nimport type { Messenger } from '@metamask/messenger';\nimport type { PermissionControllerHasPermissionAction } from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type {\n UpdateInterfaceParams,\n UpdateInterfaceResult,\n} from '@metamask/snaps-sdk';\nimport {\n ComponentOrElementStruct,\n InterfaceContextStruct,\n} from '@metamask/snaps-sdk';\nimport { type InferMatching } from '@metamask/snaps-utils';\nimport {\n StructError,\n create,\n object,\n optional,\n string,\n} from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport type {\n JsonRpcRequestWithOrigin,\n SnapInterfaceControllerUpdateInterfaceAction,\n} from '../types';\nimport { UI_PERMISSIONS } from '../utils';\n\nexport type UpdateInterfaceMethodActions =\n | PermissionControllerHasPermissionAction\n | SnapInterfaceControllerUpdateInterfaceAction;\n\n/**\n * Update an interactive interface. For use in\n * [interactive UI](https://docs.metamask.io/snaps/features/custom-ui/interactive-ui/).\n *\n * @example\n * ```tsx\n * import { Box, Heading, Text } from '@metamask/snaps-sdk/jsx';\n *\n * // First, create an interface and get its ID.\n * const id = await snap.request({\n * method: 'snap_createInterface',\n * params: {\n * ui: (\n * <Box>\n * ...\n * </Box>\n * ),\n * },\n * });\n *\n * // Later, update the interface with new content using the ID.\n * snap.request({\n * method: 'snap_updateInterface',\n * params: {\n * id: 'interface-id',\n * ui: (\n * <Box>\n * <Heading>Updated Interface</Heading>\n * <Text>This interface has been updated.</Text>\n * </Box>\n * ),\n * },\n * });\n * ```\n */\nexport const updateInterfaceHandler = {\n implementation: getUpdateInterfaceImplementation,\n actionNames: [\n 'PermissionController:hasPermission',\n 'SnapInterfaceController:updateInterface',\n ],\n} satisfies MethodHandler<\n never,\n UpdateInterfaceMethodActions,\n UpdateInterfaceParameters,\n UpdateInterfaceResult,\n { origin: string }\n>;\n\nconst UpdateInterfaceParametersStruct = object({\n id: string(),\n ui: ComponentOrElementStruct,\n context: optional(InterfaceContextStruct),\n});\n\nexport type UpdateInterfaceParameters = InferMatching<\n typeof UpdateInterfaceParametersStruct,\n UpdateInterfaceParams\n>;\n\n/**\n * The `snap_updateInterface` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param _hooks - The RPC method hooks. Not used by this function.\n * @param messenger - The messenger used to call controller actions.\n * @returns Nothing.\n */\nfunction getUpdateInterfaceImplementation(\n req: JsonRpcRequestWithOrigin<UpdateInterfaceParameters>,\n res: PendingJsonRpcResponse<UpdateInterfaceResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n _hooks: never,\n messenger: Messenger<string, UpdateInterfaceMethodActions>,\n): void {\n const { params, origin } = req;\n\n const isPermitted = UI_PERMISSIONS.some((permission) =>\n messenger.call('PermissionController:hasPermission', origin, permission),\n );\n\n if (!isPermitted) {\n return end(\n providerErrors.unauthorized({\n message: `This method can only be used if the Snap has one of the following permissions: ${UI_PERMISSIONS.join(', ')}.`,\n }),\n );\n }\n\n try {\n const validatedParams = getValidatedParams(params);\n\n const { id, ui, context } = validatedParams;\n\n messenger.call(\n 'SnapInterfaceController:updateInterface',\n origin,\n id,\n ui,\n context,\n );\n res.result = null;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the updateInterface method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated updateInterface method parameter object.\n */\nfunction getValidatedParams(params: unknown): UpdateInterfaceParameters {\n try {\n return create(params, UpdateInterfaceParametersStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"]}
|
|
@@ -1,21 +1,11 @@
|
|
|
1
1
|
import type { JsonRpcEngineEndCallback } from "@metamask/json-rpc-engine";
|
|
2
|
-
import type {
|
|
2
|
+
import type { Messenger } from "@metamask/messenger";
|
|
3
|
+
import type { PermissionControllerHasPermissionAction } from "@metamask/permission-controller";
|
|
4
|
+
import type { UpdateInterfaceParams, UpdateInterfaceResult } from "@metamask/snaps-sdk";
|
|
3
5
|
import { type InferMatching } from "@metamask/snaps-utils";
|
|
4
6
|
import type { PendingJsonRpcResponse } from "@metamask/utils";
|
|
5
|
-
import type {
|
|
6
|
-
export type
|
|
7
|
-
/**
|
|
8
|
-
* @param permissionName - The name of the permission to check.
|
|
9
|
-
* @returns Whether the Snap has the permission.
|
|
10
|
-
*/
|
|
11
|
-
hasPermission: (permissionName: string) => boolean;
|
|
12
|
-
/**
|
|
13
|
-
* @param id - The interface ID.
|
|
14
|
-
* @param ui - The UI components.
|
|
15
|
-
* @param context - The optional interface context object.
|
|
16
|
-
*/
|
|
17
|
-
updateInterface: (id: string, ui: ComponentOrElement, context?: InterfaceContext) => void;
|
|
18
|
-
};
|
|
7
|
+
import type { JsonRpcRequestWithOrigin, SnapInterfaceControllerUpdateInterfaceAction } from "../types.cjs";
|
|
8
|
+
export type UpdateInterfaceMethodActions = PermissionControllerHasPermissionAction | SnapInterfaceControllerUpdateInterfaceAction;
|
|
19
9
|
/**
|
|
20
10
|
* Update an interactive interface. For use in
|
|
21
11
|
* [interactive UI](https://docs.metamask.io/snaps/features/custom-ui/interactive-ui/).
|
|
@@ -52,9 +42,8 @@ export type UpdateInterfaceMethodHooks = {
|
|
|
52
42
|
* ```
|
|
53
43
|
*/
|
|
54
44
|
export declare const updateInterfaceHandler: {
|
|
55
|
-
methodNames: ["snap_updateInterface"];
|
|
56
45
|
implementation: typeof getUpdateInterfaceImplementation;
|
|
57
|
-
|
|
46
|
+
actionNames: ("SnapInterfaceController:updateInterface" | "PermissionController:hasPermission")[];
|
|
58
47
|
};
|
|
59
48
|
declare const UpdateInterfaceParametersStruct: import("@metamask/superstruct").Struct<{
|
|
60
49
|
id: string;
|
|
@@ -246,12 +235,10 @@ export type UpdateInterfaceParameters = InferMatching<typeof UpdateInterfacePara
|
|
|
246
235
|
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
247
236
|
* function.
|
|
248
237
|
* @param end - The `json-rpc-engine` "end" callback.
|
|
249
|
-
* @param
|
|
250
|
-
* @param
|
|
251
|
-
* permission.
|
|
252
|
-
* @param hooks.updateInterface - The function to update the interface.
|
|
238
|
+
* @param _hooks - The RPC method hooks. Not used by this function.
|
|
239
|
+
* @param messenger - The messenger used to call controller actions.
|
|
253
240
|
* @returns Nothing.
|
|
254
241
|
*/
|
|
255
|
-
declare function getUpdateInterfaceImplementation(req:
|
|
242
|
+
declare function getUpdateInterfaceImplementation(req: JsonRpcRequestWithOrigin<UpdateInterfaceParameters>, res: PendingJsonRpcResponse<UpdateInterfaceResult>, _next: unknown, end: JsonRpcEngineEndCallback, _hooks: never, messenger: Messenger<string, UpdateInterfaceMethodActions>): void;
|
|
256
243
|
export {};
|
|
257
244
|
//# sourceMappingURL=updateInterface.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"updateInterface.d.cts","sourceRoot":"","sources":["../../src/permitted/updateInterface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"updateInterface.d.cts","sourceRoot":"","sources":["../../src/permitted/updateInterface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EAEzB,kCAAkC;AACnC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,uCAAuC,EAAE,wCAAwC;AAE/F,OAAO,KAAK,EACV,qBAAqB,EACrB,qBAAqB,EACtB,4BAA4B;AAK7B,OAAO,EAAE,KAAK,aAAa,EAAE,8BAA8B;AAQ3D,OAAO,KAAK,EAAE,sBAAsB,EAAE,wBAAwB;AAE9D,OAAO,KAAK,EACV,wBAAwB,EACxB,4CAA4C,EAC7C,qBAAiB;AAGlB,MAAM,MAAM,4BAA4B,GACpC,uCAAuC,GACvC,4CAA4C,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,eAAO,MAAM,sBAAsB;;;CAYlC,CAAC;AAEF,QAAA,MAAM,+BAA+B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAInC,CAAC;AAEH,MAAM,MAAM,yBAAyB,GAAG,aAAa,CACnD,OAAO,+BAA+B,EACtC,qBAAqB,CACtB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,iBAAS,gCAAgC,CACvC,GAAG,EAAE,wBAAwB,CAAC,yBAAyB,CAAC,EACxD,GAAG,EAAE,sBAAsB,CAAC,qBAAqB,CAAC,EAClD,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,wBAAwB,EAC7B,MAAM,EAAE,KAAK,EACb,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,4BAA4B,CAAC,GACzD,IAAI,CAiCN"}
|
|
@@ -1,21 +1,11 @@
|
|
|
1
1
|
import type { JsonRpcEngineEndCallback } from "@metamask/json-rpc-engine";
|
|
2
|
-
import type {
|
|
2
|
+
import type { Messenger } from "@metamask/messenger";
|
|
3
|
+
import type { PermissionControllerHasPermissionAction } from "@metamask/permission-controller";
|
|
4
|
+
import type { UpdateInterfaceParams, UpdateInterfaceResult } from "@metamask/snaps-sdk";
|
|
3
5
|
import { type InferMatching } from "@metamask/snaps-utils";
|
|
4
6
|
import type { PendingJsonRpcResponse } from "@metamask/utils";
|
|
5
|
-
import type {
|
|
6
|
-
export type
|
|
7
|
-
/**
|
|
8
|
-
* @param permissionName - The name of the permission to check.
|
|
9
|
-
* @returns Whether the Snap has the permission.
|
|
10
|
-
*/
|
|
11
|
-
hasPermission: (permissionName: string) => boolean;
|
|
12
|
-
/**
|
|
13
|
-
* @param id - The interface ID.
|
|
14
|
-
* @param ui - The UI components.
|
|
15
|
-
* @param context - The optional interface context object.
|
|
16
|
-
*/
|
|
17
|
-
updateInterface: (id: string, ui: ComponentOrElement, context?: InterfaceContext) => void;
|
|
18
|
-
};
|
|
7
|
+
import type { JsonRpcRequestWithOrigin, SnapInterfaceControllerUpdateInterfaceAction } from "../types.mjs";
|
|
8
|
+
export type UpdateInterfaceMethodActions = PermissionControllerHasPermissionAction | SnapInterfaceControllerUpdateInterfaceAction;
|
|
19
9
|
/**
|
|
20
10
|
* Update an interactive interface. For use in
|
|
21
11
|
* [interactive UI](https://docs.metamask.io/snaps/features/custom-ui/interactive-ui/).
|
|
@@ -52,9 +42,8 @@ export type UpdateInterfaceMethodHooks = {
|
|
|
52
42
|
* ```
|
|
53
43
|
*/
|
|
54
44
|
export declare const updateInterfaceHandler: {
|
|
55
|
-
methodNames: ["snap_updateInterface"];
|
|
56
45
|
implementation: typeof getUpdateInterfaceImplementation;
|
|
57
|
-
|
|
46
|
+
actionNames: ("SnapInterfaceController:updateInterface" | "PermissionController:hasPermission")[];
|
|
58
47
|
};
|
|
59
48
|
declare const UpdateInterfaceParametersStruct: import("@metamask/superstruct").Struct<{
|
|
60
49
|
id: string;
|
|
@@ -246,12 +235,10 @@ export type UpdateInterfaceParameters = InferMatching<typeof UpdateInterfacePara
|
|
|
246
235
|
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
247
236
|
* function.
|
|
248
237
|
* @param end - The `json-rpc-engine` "end" callback.
|
|
249
|
-
* @param
|
|
250
|
-
* @param
|
|
251
|
-
* permission.
|
|
252
|
-
* @param hooks.updateInterface - The function to update the interface.
|
|
238
|
+
* @param _hooks - The RPC method hooks. Not used by this function.
|
|
239
|
+
* @param messenger - The messenger used to call controller actions.
|
|
253
240
|
* @returns Nothing.
|
|
254
241
|
*/
|
|
255
|
-
declare function getUpdateInterfaceImplementation(req:
|
|
242
|
+
declare function getUpdateInterfaceImplementation(req: JsonRpcRequestWithOrigin<UpdateInterfaceParameters>, res: PendingJsonRpcResponse<UpdateInterfaceResult>, _next: unknown, end: JsonRpcEngineEndCallback, _hooks: never, messenger: Messenger<string, UpdateInterfaceMethodActions>): void;
|
|
256
243
|
export {};
|
|
257
244
|
//# sourceMappingURL=updateInterface.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"updateInterface.d.mts","sourceRoot":"","sources":["../../src/permitted/updateInterface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"updateInterface.d.mts","sourceRoot":"","sources":["../../src/permitted/updateInterface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EAEzB,kCAAkC;AACnC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,uCAAuC,EAAE,wCAAwC;AAE/F,OAAO,KAAK,EACV,qBAAqB,EACrB,qBAAqB,EACtB,4BAA4B;AAK7B,OAAO,EAAE,KAAK,aAAa,EAAE,8BAA8B;AAQ3D,OAAO,KAAK,EAAE,sBAAsB,EAAE,wBAAwB;AAE9D,OAAO,KAAK,EACV,wBAAwB,EACxB,4CAA4C,EAC7C,qBAAiB;AAGlB,MAAM,MAAM,4BAA4B,GACpC,uCAAuC,GACvC,4CAA4C,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,eAAO,MAAM,sBAAsB;;;CAYlC,CAAC;AAEF,QAAA,MAAM,+BAA+B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAInC,CAAC;AAEH,MAAM,MAAM,yBAAyB,GAAG,aAAa,CACnD,OAAO,+BAA+B,EACtC,qBAAqB,CACtB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,iBAAS,gCAAgC,CACvC,GAAG,EAAE,wBAAwB,CAAC,yBAAyB,CAAC,EACxD,GAAG,EAAE,sBAAsB,CAAC,qBAAqB,CAAC,EAClD,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,wBAAwB,EAC7B,MAAM,EAAE,KAAK,EACb,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,4BAA4B,CAAC,GACzD,IAAI,CAiCN"}
|
|
@@ -2,11 +2,6 @@ import { providerErrors, rpcErrors } from "@metamask/rpc-errors";
|
|
|
2
2
|
import { ComponentOrElementStruct, InterfaceContextStruct } from "@metamask/snaps-sdk";
|
|
3
3
|
import { StructError, create, object, optional, string } from "@metamask/superstruct";
|
|
4
4
|
import { UI_PERMISSIONS } from "../utils.mjs";
|
|
5
|
-
const methodName = 'snap_updateInterface';
|
|
6
|
-
const hookNames = {
|
|
7
|
-
hasPermission: true,
|
|
8
|
-
updateInterface: true,
|
|
9
|
-
};
|
|
10
5
|
/**
|
|
11
6
|
* Update an interactive interface. For use in
|
|
12
7
|
* [interactive UI](https://docs.metamask.io/snaps/features/custom-ui/interactive-ui/).
|
|
@@ -43,9 +38,11 @@ const hookNames = {
|
|
|
43
38
|
* ```
|
|
44
39
|
*/
|
|
45
40
|
export const updateInterfaceHandler = {
|
|
46
|
-
methodNames: [methodName],
|
|
47
41
|
implementation: getUpdateInterfaceImplementation,
|
|
48
|
-
|
|
42
|
+
actionNames: [
|
|
43
|
+
'PermissionController:hasPermission',
|
|
44
|
+
'SnapInterfaceController:updateInterface',
|
|
45
|
+
],
|
|
49
46
|
};
|
|
50
47
|
const UpdateInterfaceParametersStruct = object({
|
|
51
48
|
id: string(),
|
|
@@ -60,23 +57,22 @@ const UpdateInterfaceParametersStruct = object({
|
|
|
60
57
|
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
61
58
|
* function.
|
|
62
59
|
* @param end - The `json-rpc-engine` "end" callback.
|
|
63
|
-
* @param
|
|
64
|
-
* @param
|
|
65
|
-
* permission.
|
|
66
|
-
* @param hooks.updateInterface - The function to update the interface.
|
|
60
|
+
* @param _hooks - The RPC method hooks. Not used by this function.
|
|
61
|
+
* @param messenger - The messenger used to call controller actions.
|
|
67
62
|
* @returns Nothing.
|
|
68
63
|
*/
|
|
69
|
-
function getUpdateInterfaceImplementation(req, res, _next, end,
|
|
70
|
-
|
|
64
|
+
function getUpdateInterfaceImplementation(req, res, _next, end, _hooks, messenger) {
|
|
65
|
+
const { params, origin } = req;
|
|
66
|
+
const isPermitted = UI_PERMISSIONS.some((permission) => messenger.call('PermissionController:hasPermission', origin, permission));
|
|
67
|
+
if (!isPermitted) {
|
|
71
68
|
return end(providerErrors.unauthorized({
|
|
72
69
|
message: `This method can only be used if the Snap has one of the following permissions: ${UI_PERMISSIONS.join(', ')}.`,
|
|
73
70
|
}));
|
|
74
71
|
}
|
|
75
|
-
const { params } = req;
|
|
76
72
|
try {
|
|
77
73
|
const validatedParams = getValidatedParams(params);
|
|
78
74
|
const { id, ui, context } = validatedParams;
|
|
79
|
-
updateInterface
|
|
75
|
+
messenger.call('SnapInterfaceController:updateInterface', origin, id, ui, context);
|
|
80
76
|
res.result = null;
|
|
81
77
|
}
|
|
82
78
|
catch (error) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"updateInterface.mjs","sourceRoot":"","sources":["../../src/permitted/updateInterface.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"updateInterface.mjs","sourceRoot":"","sources":["../../src/permitted/updateInterface.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,6BAA6B;AAKjE,OAAO,EACL,wBAAwB,EACxB,sBAAsB,EACvB,4BAA4B;AAE7B,OAAO,EACL,WAAW,EACX,MAAM,EACN,MAAM,EACN,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IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,MAAe;IACzC,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,MAAM,EAAE,+BAA+B,CAAC,CAAC;IACzD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,WAAW,EAAE,CAAC;YACjC,MAAM,SAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,mBAAmB,KAAK,CAAC,OAAO,GAAG;aAC7C,CAAC,CAAC;QACL,CAAC;QACD,0BAA0B;QAC1B,MAAM,SAAS,CAAC,QAAQ,EAAE,CAAC;IAC7B,CAAC;AACH,CAAC","sourcesContent":["import type {\n JsonRpcEngineEndCallback,\n MethodHandler,\n} from '@metamask/json-rpc-engine';\nimport type { Messenger } from '@metamask/messenger';\nimport type { PermissionControllerHasPermissionAction } from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type {\n UpdateInterfaceParams,\n UpdateInterfaceResult,\n} from '@metamask/snaps-sdk';\nimport {\n ComponentOrElementStruct,\n InterfaceContextStruct,\n} from '@metamask/snaps-sdk';\nimport { type InferMatching } from '@metamask/snaps-utils';\nimport {\n StructError,\n create,\n object,\n optional,\n string,\n} from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport type {\n JsonRpcRequestWithOrigin,\n SnapInterfaceControllerUpdateInterfaceAction,\n} from '../types';\nimport { UI_PERMISSIONS } from '../utils';\n\nexport type UpdateInterfaceMethodActions =\n | PermissionControllerHasPermissionAction\n | SnapInterfaceControllerUpdateInterfaceAction;\n\n/**\n * Update an interactive interface. For use in\n * [interactive UI](https://docs.metamask.io/snaps/features/custom-ui/interactive-ui/).\n *\n * @example\n * ```tsx\n * import { Box, Heading, Text } from '@metamask/snaps-sdk/jsx';\n *\n * // First, create an interface and get its ID.\n * const id = await snap.request({\n * method: 'snap_createInterface',\n * params: {\n * ui: (\n * <Box>\n * ...\n * </Box>\n * ),\n * },\n * });\n *\n * // Later, update the interface with new content using the ID.\n * snap.request({\n * method: 'snap_updateInterface',\n * params: {\n * id: 'interface-id',\n * ui: (\n * <Box>\n * <Heading>Updated Interface</Heading>\n * <Text>This interface has been updated.</Text>\n * </Box>\n * ),\n * },\n * });\n * ```\n */\nexport const updateInterfaceHandler = {\n implementation: getUpdateInterfaceImplementation,\n actionNames: [\n 'PermissionController:hasPermission',\n 'SnapInterfaceController:updateInterface',\n ],\n} satisfies MethodHandler<\n never,\n UpdateInterfaceMethodActions,\n UpdateInterfaceParameters,\n UpdateInterfaceResult,\n { origin: string }\n>;\n\nconst UpdateInterfaceParametersStruct = object({\n id: string(),\n ui: ComponentOrElementStruct,\n context: optional(InterfaceContextStruct),\n});\n\nexport type UpdateInterfaceParameters = InferMatching<\n typeof UpdateInterfaceParametersStruct,\n UpdateInterfaceParams\n>;\n\n/**\n * The `snap_updateInterface` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param _hooks - The RPC method hooks. Not used by this function.\n * @param messenger - The messenger used to call controller actions.\n * @returns Nothing.\n */\nfunction getUpdateInterfaceImplementation(\n req: JsonRpcRequestWithOrigin<UpdateInterfaceParameters>,\n res: PendingJsonRpcResponse<UpdateInterfaceResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n _hooks: never,\n messenger: Messenger<string, UpdateInterfaceMethodActions>,\n): void {\n const { params, origin } = req;\n\n const isPermitted = UI_PERMISSIONS.some((permission) =>\n messenger.call('PermissionController:hasPermission', origin, permission),\n );\n\n if (!isPermitted) {\n return end(\n providerErrors.unauthorized({\n message: `This method can only be used if the Snap has one of the following permissions: ${UI_PERMISSIONS.join(', ')}.`,\n }),\n );\n }\n\n try {\n const validatedParams = getValidatedParams(params);\n\n const { id, ui, context } = validatedParams;\n\n messenger.call(\n 'SnapInterfaceController:updateInterface',\n origin,\n id,\n ui,\n context,\n );\n res.result = null;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the updateInterface method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated updateInterface method parameter object.\n */\nfunction getValidatedParams(params: unknown): UpdateInterfaceParameters {\n try {\n return create(params, UpdateInterfaceParametersStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"]}
|
|
@@ -24,25 +24,18 @@ const PlaceholderStruct = (0, superstruct_1.optional)((0, superstruct_1.size)((0
|
|
|
24
24
|
* @param options - The specification builder options.
|
|
25
25
|
* @param options.allowedCaveats - The optional allowed caveats for the
|
|
26
26
|
* permission.
|
|
27
|
-
* @param options.
|
|
28
|
-
* implementation.
|
|
27
|
+
* @param options.messenger - The messenger.
|
|
29
28
|
* @returns The specification for the `snap_dialog` permission.
|
|
30
29
|
*/
|
|
31
|
-
const specificationBuilder = ({ allowedCaveats = null,
|
|
30
|
+
const specificationBuilder = ({ allowedCaveats = null, messenger, }) => {
|
|
32
31
|
return {
|
|
33
32
|
permissionType: permission_controller_1.PermissionType.RestrictedMethod,
|
|
34
33
|
targetName: methodName,
|
|
35
34
|
allowedCaveats,
|
|
36
|
-
methodImplementation: getDialogImplementation(
|
|
35
|
+
methodImplementation: getDialogImplementation({ messenger }),
|
|
37
36
|
subjectTypes: [permission_controller_1.SubjectType.Snap],
|
|
38
37
|
};
|
|
39
38
|
};
|
|
40
|
-
const methodHooks = {
|
|
41
|
-
requestUserApproval: true,
|
|
42
|
-
createInterface: true,
|
|
43
|
-
getInterface: true,
|
|
44
|
-
setInterfaceDisplayed: true,
|
|
45
|
-
};
|
|
46
39
|
/* eslint-disable jsdoc/check-indentation */
|
|
47
40
|
/**
|
|
48
41
|
* Display a [dialog](https://docs.metamask.io/snaps/features/custom-ui/dialogs/)
|
|
@@ -88,7 +81,12 @@ const methodHooks = {
|
|
|
88
81
|
exports.dialogBuilder = Object.freeze({
|
|
89
82
|
targetName: methodName,
|
|
90
83
|
specificationBuilder,
|
|
91
|
-
|
|
84
|
+
actionNames: [
|
|
85
|
+
'ApprovalController:addRequest',
|
|
86
|
+
'SnapInterfaceController:createInterface',
|
|
87
|
+
'SnapInterfaceController:getInterface',
|
|
88
|
+
'SnapInterfaceController:setInterfaceDisplayed',
|
|
89
|
+
],
|
|
92
90
|
});
|
|
93
91
|
/* eslint-enable jsdoc/check-indentation */
|
|
94
92
|
const AlertParametersWithContentStruct = (0, superstruct_1.object)({
|
|
@@ -166,17 +164,12 @@ const DialogParametersStruct = (0, snaps_sdk_1.selectiveUnion)((value) => {
|
|
|
166
164
|
/**
|
|
167
165
|
* Builds the method implementation for `snap_dialog`.
|
|
168
166
|
*
|
|
169
|
-
* @param
|
|
170
|
-
* @param
|
|
171
|
-
* This function should return a Promise that resolves with the appropriate value when the user has approved or rejected the request.
|
|
172
|
-
* @param hooks.createInterface - A function that creates the interface in SnapInterfaceController.
|
|
173
|
-
* @param hooks.getInterface - A function that gets an interface from SnapInterfaceController.
|
|
174
|
-
* @param hooks.setInterfaceDisplayed - A function that sets the interface as
|
|
175
|
-
* displayed in SnapInterfaceController.
|
|
167
|
+
* @param options - The options.
|
|
168
|
+
* @param options.messenger - The messenger.
|
|
176
169
|
* @returns The method implementation which return value depends on the dialog
|
|
177
170
|
* type, valid return types are: string, boolean, null.
|
|
178
171
|
*/
|
|
179
|
-
function getDialogImplementation({
|
|
172
|
+
function getDialogImplementation({ messenger, }) {
|
|
180
173
|
return async function dialogImplementation(args) {
|
|
181
174
|
const { params, context: { origin }, } = args;
|
|
182
175
|
if (!(0, utils_1.isObject)(params)) {
|
|
@@ -193,25 +186,25 @@ function getDialogImplementation({ requestUserApproval, createInterface, getInte
|
|
|
193
186
|
: 'default';
|
|
194
187
|
const approvalType = exports.DIALOG_APPROVAL_TYPES[validatedType];
|
|
195
188
|
if ((0, utils_1.hasProperty)(validatedParams, 'content')) {
|
|
196
|
-
const id =
|
|
197
|
-
setInterfaceDisplayed
|
|
198
|
-
return
|
|
189
|
+
const id = messenger.call('SnapInterfaceController:createInterface', origin, validatedParams.content);
|
|
190
|
+
messenger.call('SnapInterfaceController:setInterfaceDisplayed', origin, id);
|
|
191
|
+
return messenger.call('ApprovalController:addRequest', {
|
|
199
192
|
id: approvalType === exports.DIALOG_APPROVAL_TYPES.default ? id : undefined,
|
|
200
193
|
origin,
|
|
201
194
|
type: approvalType,
|
|
202
195
|
requestData: { id, placeholder },
|
|
203
|
-
});
|
|
196
|
+
}, true);
|
|
204
197
|
}
|
|
205
|
-
validateInterface(origin, validatedParams.id,
|
|
206
|
-
setInterfaceDisplayed
|
|
207
|
-
return
|
|
198
|
+
validateInterface(origin, validatedParams.id, messenger);
|
|
199
|
+
messenger.call('SnapInterfaceController:setInterfaceDisplayed', origin, validatedParams.id);
|
|
200
|
+
return messenger.call('ApprovalController:addRequest', {
|
|
208
201
|
id: approvalType === exports.DIALOG_APPROVAL_TYPES.default
|
|
209
202
|
? validatedParams.id
|
|
210
203
|
: undefined,
|
|
211
204
|
origin,
|
|
212
205
|
type: approvalType,
|
|
213
206
|
requestData: { id: validatedParams.id, placeholder },
|
|
214
|
-
});
|
|
207
|
+
}, true);
|
|
215
208
|
};
|
|
216
209
|
}
|
|
217
210
|
exports.getDialogImplementation = getDialogImplementation;
|
|
@@ -220,11 +213,11 @@ exports.getDialogImplementation = getDialogImplementation;
|
|
|
220
213
|
*
|
|
221
214
|
* @param origin - The origin of the request.
|
|
222
215
|
* @param id - The interface ID.
|
|
223
|
-
* @param
|
|
216
|
+
* @param messenger - The messenger.
|
|
224
217
|
*/
|
|
225
|
-
function validateInterface(origin, id,
|
|
218
|
+
function validateInterface(origin, id, messenger) {
|
|
226
219
|
try {
|
|
227
|
-
getInterface
|
|
220
|
+
messenger.call('SnapInterfaceController:getInterface', origin, id);
|
|
228
221
|
}
|
|
229
222
|
catch (error) {
|
|
230
223
|
throw rpc_errors_1.rpcErrors.invalidParams({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dialog.cjs","sourceRoot":"","sources":["../../src/restricted/dialog.ts"],"names":[],"mappings":";;;AAKA,2EAA8E;AAC9E,qDAAiD;AACjD,mDAK6B;AAc7B,uDAA+E;AAE/E,2CAAuE;AAIvE,MAAM,UAAU,GAAG,aAAa,CAAC;AAMpB,QAAA,qBAAqB,GAAG;IACnC,CAAC,sBAAU,CAAC,KAAK,CAAC,EAAE,GAAG,UAAU,QAAQ;IACzC,CAAC,sBAAU,CAAC,YAAY,CAAC,EAAE,GAAG,UAAU,eAAe;IACvD,CAAC,sBAAU,CAAC,MAAM,CAAC,EAAE,GAAG,UAAU,SAAS;IAC3C,OAAO,EAAE,UAAU;CACpB,CAAC;AAEF,MAAM,iBAAiB,GAAG,IAAA,sBAAQ,EAAC,IAAA,kBAAI,EAAC,IAAA,oBAAM,GAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;AA0E1D;;;;;;;;;;;;;GAaG;AACH,MAAM,oBAAoB,GAItB,CAAC,EACH,cAAc,GAAG,IAAI,EACrB,WAAW,GACuB,EAAE,EAAE;IACtC,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,gBAAgB;QAC/C,UAAU,EAAE,UAAU;QACtB,cAAc;QACd,oBAAoB,EAAE,uBAAuB,CAAC,WAAW,CAAC;QAC1D,YAAY,EAAE,CAAC,mCAAW,CAAC,IAAI,CAAC;KACjC,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,WAAW,GAAyC;IACxD,mBAAmB,EAAE,IAAI;IACzB,eAAe,EAAE,IAAI;IACrB,YAAY,EAAE,IA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,EAAE,CAAC;QACf,MAAM,sBAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,mBAAmB,KAAK,CAAC,OAAO,EAAE;SAC5C,CAAC,CAAC;IACL,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,SAAS,aAAa,CAAC,MAAwB;IAC7C,OAAO,IAAA,mBAAW,EAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAE,MAAM,CAAC,IAAmB,CAAC,CAAC,CAAC,SAAS,CAAC;AAC/E,CAAC;AAED;;;;;GAKG;AACH,SAAS,cAAc,CAAC,MAAwB;IAC9C,OAAO,aAAa,CAAC,MAAM,CAAC,KAAK,sBAAU,CAAC,MAAM,CAAC;AACrD,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,MAAe;IACzC,IAAI,CAAC;QACH,OAAO,IAAA,oBAAM,EAAC,MAAM,EAAE,sBAAsB,CAAC,CAAC;IAChD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,sBAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,mBAAmB,KAAK,CAAC,OAAO,EAAE;SAC5C,CAAC,CAAC;IACL,CAAC;AACH,CAAC","sourcesContent":["import type {\n PermissionSpecificationBuilder,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport {\n DialogType,\n enumValue,\n ComponentOrElementStruct,\n selectiveUnion,\n} from '@metamask/snaps-sdk';\nimport type {\n DialogParams,\n Component,\n InterfaceState,\n SnapId,\n PromptDialog,\n ComponentOrElement,\n InterfaceContext,\n ContentType,\n DialogResult,\n} from '@metamask/snaps-sdk';\nimport type { InferMatching } from '@metamask/snaps-utils';\nimport type { Infer } from '@metamask/superstruct';\nimport { create, object, optional, size, string } from '@metamask/superstruct';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport { hasProperty, isObject, isPlainObject } from '@metamask/utils';\n\nimport { type MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_dialog';\n\nexport type DialogApprovalTypes = Record<DialogType, string> & {\n default: string;\n};\n\nexport const DIALOG_APPROVAL_TYPES = {\n [DialogType.Alert]: `${methodName}:alert`,\n [DialogType.Confirmation]: `${methodName}:confirmation`,\n [DialogType.Prompt]: `${methodName}:prompt`,\n default: methodName,\n};\n\nconst PlaceholderStruct = optional(size(string(), 1, 40));\n\nexport type Placeholder = Infer<typeof PlaceholderStruct>;\n\ntype RequestUserApprovalOptions = {\n id?: string;\n origin: string;\n type: string;\n requestData: {\n id: string;\n placeholder?: string;\n };\n};\n\ntype RequestUserApproval = (\n opts: RequestUserApprovalOptions,\n) => Promise<boolean | null | string | Json>;\n\ntype CreateInterface = (\n snapId: string,\n content: ComponentOrElement,\n context?: InterfaceContext,\n contentType?: ContentType,\n) => Promise<string>;\n\ntype GetInterface = (\n snapId: string,\n id: string,\n) => { content: ComponentOrElement; snapId: SnapId; state: InterfaceState };\n\nexport type DialogMethodHooks = {\n /**\n * @param opts - The `requestUserApproval` options.\n * @param opts.id - The approval ID. If not provided, a new approval ID will be generated.\n * @param opts.origin - The origin of the request. In this case, the Snap ID.\n * @param opts.type - The type of the approval request.\n * @param opts.requestData - The data of the approval request.\n * @param opts.requestData.id - The ID of the interface.\n * @param opts.requestData.placeholder - The placeholder of the `Prompt` dialog.\n */\n requestUserApproval: RequestUserApproval;\n\n /**\n * @param snapId - The Snap ID creating the interface.\n * @param content - The content of the interface.\n */\n createInterface: CreateInterface;\n /**\n * @param snapId - The SnapId requesting the interface.\n * @param id - The interface ID.\n */\n getInterface: GetInterface;\n\n /**\n * Set the interface as displayed.\n *\n * @param snapId - The Snap ID requesting the interface.\n * @param id - The interface ID.\n */\n setInterfaceDisplayed: (snapId: string, id: string) => void;\n};\n\ntype DialogSpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: DialogMethodHooks;\n};\n\ntype DialogSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof methodName;\n methodImplementation: ReturnType<typeof getDialogImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * The specification builder for the `snap_dialog` permission. `snap_dialog`\n * lets the Snap display one of the following dialogs to the user:\n * - An alert, for displaying information.\n * - A confirmation, for accepting or rejecting some action.\n * - A prompt, for inputting some information.\n *\n * @param options - The specification builder options.\n * @param options.allowedCaveats - The optional allowed caveats for the\n * permission.\n * @param options.methodHooks - The RPC method hooks needed by the method\n * implementation.\n * @returns The specification for the `snap_dialog` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n DialogSpecificationBuilderOptions,\n DialogSpecification\n> = ({\n allowedCaveats = null,\n methodHooks,\n}: DialogSpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: methodName,\n allowedCaveats,\n methodImplementation: getDialogImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<DialogMethodHooks> = {\n requestUserApproval: true,\n createInterface: true,\n getInterface: true,\n setInterfaceDisplayed: true,\n};\n\n/* eslint-disable jsdoc/check-indentation */\n/**\n * Display a [dialog](https://docs.metamask.io/snaps/features/custom-ui/dialogs/)\n * in the MetaMask UI.\n *\n * - `type` - The type of dialog. Not providing a type will create a fully\n * [custom dialog](https://docs.metamask.io/snaps/features/custom-ui/dialogs/#display-a-custom-dialog).\n * Possible values are:\n * - `alert` - An alert that can only be acknowledged.\n * - `confirmation` - A confirmation that can be accepted or rejected.\n * - `prompt` - A prompt where the user can enter a text response.\n *\n * - One of:\n * - `content` - The content of the alert, as a\n * [custom UI](https://docs.metamask.io/snaps/features/custom-ui/) component.\n * - `id` - The ID of an\n * [interactive interface](https://docs.metamask.io/snaps/reference/snaps-api/snap_createinterface).\n * - `placeholder` - An optional placeholder text to display in the dialog. Only\n * applicable for the `prompt` dialog.\n *\n * @example\n * ```ts\n * import { Box, Heading, Text } from '@metamask/snaps-sdk/jsx';\n *\n * const walletAddress = await snap.request({\n * method: 'snap_dialog',\n * params: {\n * type: 'prompt',\n * content: (\n * <Box>\n * <Heading>What is the wallet address?</Heading>\n * <Text>Please enter the wallet address to be monitored.</Text>\n * </Box>\n * ),\n * placeholder: '0x123...',\n * },\n * });\n *\n * // `walletAddress` will be a string containing the address entered by the\n * // user.\n * ```\n */\nexport const dialogBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n methodHooks,\n} as const);\n/* eslint-enable jsdoc/check-indentation */\n\nconst AlertParametersWithContentStruct = object({\n type: enumValue(DialogType.Alert),\n content: ComponentOrElementStruct,\n});\n\nconst AlertParametersWithIdStruct = object({\n type: enumValue(DialogType.Alert),\n id: string(),\n});\n\nconst AlertParametersStruct = selectiveUnion((value) => {\n if (isPlainObject(value) && hasProperty(value, 'id')) {\n return AlertParametersWithIdStruct;\n }\n return AlertParametersWithContentStruct;\n});\n\nconst ConfirmationParametersWithContentStruct = object({\n type: enumValue(DialogType.Confirmation),\n content: ComponentOrElementStruct,\n});\n\nconst ConfirmationParametersWithIdStruct = object({\n type: enumValue(DialogType.Confirmation),\n id: string(),\n});\n\nconst ConfirmationParametersStruct = selectiveUnion((value) => {\n if (isPlainObject(value) && hasProperty(value, 'id')) {\n return ConfirmationParametersWithIdStruct;\n }\n return ConfirmationParametersWithContentStruct;\n});\n\nconst PromptParametersWithContentStruct = object({\n type: enumValue(DialogType.Prompt),\n content: ComponentOrElementStruct,\n placeholder: PlaceholderStruct,\n});\n\nconst PromptParametersWithIdStruct = object({\n type: enumValue(DialogType.Prompt),\n id: string(),\n placeholder: PlaceholderStruct,\n});\n\nconst PromptParametersStruct = selectiveUnion((value) => {\n if (isPlainObject(value) && hasProperty(value, 'id')) {\n return PromptParametersWithIdStruct;\n }\n return PromptParametersWithContentStruct;\n});\n\nconst DefaultParametersWithContentStruct = object({\n content: ComponentOrElementStruct,\n});\n\nconst DefaultParametersWithIdStruct = object({\n id: string(),\n});\n\nconst DefaultParametersStruct = selectiveUnion((value) => {\n if (isPlainObject(value) && hasProperty(value, 'id')) {\n return DefaultParametersWithIdStruct;\n }\n return DefaultParametersWithContentStruct;\n});\n\nconst DialogParametersStruct = selectiveUnion((value) => {\n if (isPlainObject(value) && hasProperty(value, 'type')) {\n switch (value.type) {\n // We cannot use typedUnion here unfortunately.\n case DialogType.Alert:\n return AlertParametersStruct;\n case DialogType.Confirmation:\n return ConfirmationParametersStruct;\n case DialogType.Prompt:\n return PromptParametersStruct;\n default:\n throw new Error(\n `The \"type\" property must be one of: ${Object.values(DialogType).join(\n ', ',\n )}.`,\n );\n }\n }\n return DefaultParametersStruct;\n});\n\nexport type DialogParameters = InferMatching<\n typeof DialogParametersStruct,\n DialogParams\n>;\n\n/**\n * Builds the method implementation for `snap_dialog`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.requestUserApproval - A function that creates a new Approval in the ApprovalController.\n * This function should return a Promise that resolves with the appropriate value when the user has approved or rejected the request.\n * @param hooks.createInterface - A function that creates the interface in SnapInterfaceController.\n * @param hooks.getInterface - A function that gets an interface from SnapInterfaceController.\n * @param hooks.setInterfaceDisplayed - A function that sets the interface as\n * displayed in SnapInterfaceController.\n * @returns The method implementation which return value depends on the dialog\n * type, valid return types are: string, boolean, null.\n */\nexport function getDialogImplementation({\n requestUserApproval,\n createInterface,\n getInterface,\n setInterfaceDisplayed,\n}: DialogMethodHooks) {\n return async function dialogImplementation(\n args: RestrictedMethodOptions<DialogParameters>,\n ): Promise<DialogResult> {\n const {\n params,\n context: { origin },\n } = args;\n\n if (!isObject(params)) {\n throw rpcErrors.invalidParams({\n message: 'Invalid params: Expected params to be a single object.',\n });\n }\n\n const validatedParams = getValidatedParams(params);\n const placeholder = isPromptDialog(validatedParams)\n ? validatedParams.placeholder\n : undefined;\n\n const validatedType = hasProperty(validatedParams, 'type')\n ? validatedParams.type\n : 'default';\n\n const approvalType =\n DIALOG_APPROVAL_TYPES[\n validatedType as keyof typeof DIALOG_APPROVAL_TYPES\n ];\n\n if (hasProperty(validatedParams, 'content')) {\n const id = await createInterface(\n origin,\n validatedParams.content as Component,\n );\n\n setInterfaceDisplayed(origin, id);\n\n return requestUserApproval({\n id: approvalType === DIALOG_APPROVAL_TYPES.default ? id : undefined,\n origin,\n type: approvalType,\n requestData: { id, placeholder },\n });\n }\n\n validateInterface(origin, validatedParams.id, getInterface);\n setInterfaceDisplayed(origin, validatedParams.id);\n\n return requestUserApproval({\n id:\n approvalType === DIALOG_APPROVAL_TYPES.default\n ? validatedParams.id\n : undefined,\n origin,\n type: approvalType,\n requestData: { id: validatedParams.id, placeholder },\n });\n };\n}\n/**\n * Validate that the interface ID is valid.\n *\n * @param origin - The origin of the request.\n * @param id - The interface ID.\n * @param getInterface - The function to get the interface.\n */\nfunction validateInterface(\n origin: string,\n id: string,\n getInterface: GetInterface,\n) {\n try {\n getInterface(origin, id);\n } catch (error) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}`,\n });\n }\n}\n\n/**\n * Gets the dialog type from the dialog parameters.\n *\n * @param params - The dialog parameters.\n * @returns The dialog type.\n */\nfunction getDialogType(params: DialogParameters): DialogType | undefined {\n return hasProperty(params, 'type') ? (params.type as DialogType) : undefined;\n}\n\n/**\n * Checks if the dialog parameters are for a prompt dialog.\n *\n * @param params - The dialog parameters.\n * @returns `true` if the dialog parameters are for a prompt dialog, `false` otherwise.\n */\nfunction isPromptDialog(params: DialogParameters): params is PromptDialog {\n return getDialogType(params) === DialogType.Prompt;\n}\n\n/**\n * Validates the confirm method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated confirm method parameter object.\n */\nfunction getValidatedParams(params: unknown): DialogParameters {\n try {\n return create(params, DialogParametersStruct);\n } catch (error) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}`,\n });\n }\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,aAAa,CAAC,MAAwB;IAC7C,OAAO,IAAA,mBAAW,EAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAE,MAAM,CAAC,IAAmB,CAAC,CAAC,CAAC,SAAS,CAAC;AAC/E,CAAC;AAED;;;;;GAKG;AACH,SAAS,cAAc,CAAC,MAAwB;IAC9C,OAAO,aAAa,CAAC,MAAM,CAAC,KAAK,sBAAU,CAAC,MAAM,CAAC;AACrD,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,MAAe;IACzC,IAAI,CAAC;QACH,OAAO,IAAA,oBAAM,EAAC,MAAM,EAAE,sBAAsB,CAAC,CAAC;IAChD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,sBAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,mBAAmB,KAAK,CAAC,OAAO,EAAE;SAC5C,CAAC,CAAC;IACL,CAAC;AACH,CAAC","sourcesContent":["import type { Messenger } from '@metamask/messenger';\nimport type {\n PermissionSpecificationBuilder,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport {\n DialogType,\n enumValue,\n ComponentOrElementStruct,\n selectiveUnion,\n} from '@metamask/snaps-sdk';\nimport type {\n DialogParams,\n Component,\n PromptDialog,\n DialogResult,\n} from '@metamask/snaps-sdk';\nimport type { InferMatching } from '@metamask/snaps-utils';\nimport type { Infer } from '@metamask/superstruct';\nimport { create, object, optional, size, string } from '@metamask/superstruct';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { hasProperty, isObject, isPlainObject } from '@metamask/utils';\n\nimport type {\n ApprovalControllerAddRequestAction,\n SnapInterfaceControllerCreateInterfaceAction,\n SnapInterfaceControllerGetInterfaceAction,\n SnapInterfaceControllerSetInterfaceDisplayedAction,\n} from '../types';\n\nconst methodName = 'snap_dialog';\n\nexport type DialogApprovalTypes = Record<DialogType, string> & {\n default: string;\n};\n\nexport const DIALOG_APPROVAL_TYPES = {\n [DialogType.Alert]: `${methodName}:alert`,\n [DialogType.Confirmation]: `${methodName}:confirmation`,\n [DialogType.Prompt]: `${methodName}:prompt`,\n default: methodName,\n};\n\nconst PlaceholderStruct = optional(size(string(), 1, 40));\n\nexport type Placeholder = Infer<typeof PlaceholderStruct>;\n\nexport type DialogMessengerActions =\n | ApprovalControllerAddRequestAction\n | SnapInterfaceControllerCreateInterfaceAction\n | SnapInterfaceControllerGetInterfaceAction\n | SnapInterfaceControllerSetInterfaceDisplayedAction;\n\ntype DialogSpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n messenger: Messenger<string, DialogMessengerActions>;\n};\n\ntype DialogSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof methodName;\n methodImplementation: ReturnType<typeof getDialogImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * The specification builder for the `snap_dialog` permission. `snap_dialog`\n * lets the Snap display one of the following dialogs to the user:\n * - An alert, for displaying information.\n * - A confirmation, for accepting or rejecting some action.\n * - A prompt, for inputting some information.\n *\n * @param options - The specification builder options.\n * @param options.allowedCaveats - The optional allowed caveats for the\n * permission.\n * @param options.messenger - The messenger.\n * @returns The specification for the `snap_dialog` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n DialogSpecificationBuilderOptions,\n DialogSpecification\n> = ({\n allowedCaveats = null,\n messenger,\n}: DialogSpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: methodName,\n allowedCaveats,\n methodImplementation: getDialogImplementation({ messenger }),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\n/* eslint-disable jsdoc/check-indentation */\n/**\n * Display a [dialog](https://docs.metamask.io/snaps/features/custom-ui/dialogs/)\n * in the MetaMask UI.\n *\n * - `type` - The type of dialog. Not providing a type will create a fully\n * [custom dialog](https://docs.metamask.io/snaps/features/custom-ui/dialogs/#display-a-custom-dialog).\n * Possible values are:\n * - `alert` - An alert that can only be acknowledged.\n * - `confirmation` - A confirmation that can be accepted or rejected.\n * - `prompt` - A prompt where the user can enter a text response.\n *\n * - One of:\n * - `content` - The content of the alert, as a\n * [custom UI](https://docs.metamask.io/snaps/features/custom-ui/) component.\n * - `id` - The ID of an\n * [interactive interface](https://docs.metamask.io/snaps/reference/snaps-api/snap_createinterface).\n * - `placeholder` - An optional placeholder text to display in the dialog. Only\n * applicable for the `prompt` dialog.\n *\n * @example\n * ```ts\n * import { Box, Heading, Text } from '@metamask/snaps-sdk/jsx';\n *\n * const walletAddress = await snap.request({\n * method: 'snap_dialog',\n * params: {\n * type: 'prompt',\n * content: (\n * <Box>\n * <Heading>What is the wallet address?</Heading>\n * <Text>Please enter the wallet address to be monitored.</Text>\n * </Box>\n * ),\n * placeholder: '0x123...',\n * },\n * });\n *\n * // `walletAddress` will be a string containing the address entered by the\n * // user.\n * ```\n */\nexport const dialogBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n actionNames: [\n 'ApprovalController:addRequest',\n 'SnapInterfaceController:createInterface',\n 'SnapInterfaceController:getInterface',\n 'SnapInterfaceController:setInterfaceDisplayed',\n ],\n} as const);\n/* eslint-enable jsdoc/check-indentation */\n\nconst AlertParametersWithContentStruct = object({\n type: enumValue(DialogType.Alert),\n content: ComponentOrElementStruct,\n});\n\nconst AlertParametersWithIdStruct = object({\n type: enumValue(DialogType.Alert),\n id: string(),\n});\n\nconst AlertParametersStruct = selectiveUnion((value) => {\n if (isPlainObject(value) && hasProperty(value, 'id')) {\n return AlertParametersWithIdStruct;\n }\n return AlertParametersWithContentStruct;\n});\n\nconst ConfirmationParametersWithContentStruct = object({\n type: enumValue(DialogType.Confirmation),\n content: ComponentOrElementStruct,\n});\n\nconst ConfirmationParametersWithIdStruct = object({\n type: enumValue(DialogType.Confirmation),\n id: string(),\n});\n\nconst ConfirmationParametersStruct = selectiveUnion((value) => {\n if (isPlainObject(value) && hasProperty(value, 'id')) {\n return ConfirmationParametersWithIdStruct;\n }\n return ConfirmationParametersWithContentStruct;\n});\n\nconst PromptParametersWithContentStruct = object({\n type: enumValue(DialogType.Prompt),\n content: ComponentOrElementStruct,\n placeholder: PlaceholderStruct,\n});\n\nconst PromptParametersWithIdStruct = object({\n type: enumValue(DialogType.Prompt),\n id: string(),\n placeholder: PlaceholderStruct,\n});\n\nconst PromptParametersStruct = selectiveUnion((value) => {\n if (isPlainObject(value) && hasProperty(value, 'id')) {\n return PromptParametersWithIdStruct;\n }\n return PromptParametersWithContentStruct;\n});\n\nconst DefaultParametersWithContentStruct = object({\n content: ComponentOrElementStruct,\n});\n\nconst DefaultParametersWithIdStruct = object({\n id: string(),\n});\n\nconst DefaultParametersStruct = selectiveUnion((value) => {\n if (isPlainObject(value) && hasProperty(value, 'id')) {\n return DefaultParametersWithIdStruct;\n }\n return DefaultParametersWithContentStruct;\n});\n\nconst DialogParametersStruct = selectiveUnion((value) => {\n if (isPlainObject(value) && hasProperty(value, 'type')) {\n switch (value.type) {\n // We cannot use typedUnion here unfortunately.\n case DialogType.Alert:\n return AlertParametersStruct;\n case DialogType.Confirmation:\n return ConfirmationParametersStruct;\n case DialogType.Prompt:\n return PromptParametersStruct;\n default:\n throw new Error(\n `The \"type\" property must be one of: ${Object.values(DialogType).join(\n ', ',\n )}.`,\n );\n }\n }\n return DefaultParametersStruct;\n});\n\nexport type DialogParameters = InferMatching<\n typeof DialogParametersStruct,\n DialogParams\n>;\n\n/**\n * Builds the method implementation for `snap_dialog`.\n *\n * @param options - The options.\n * @param options.messenger - The messenger.\n * @returns The method implementation which return value depends on the dialog\n * type, valid return types are: string, boolean, null.\n */\nexport function getDialogImplementation({\n messenger,\n}: DialogSpecificationBuilderOptions) {\n return async function dialogImplementation(\n args: RestrictedMethodOptions<DialogParameters>,\n ): Promise<DialogResult> {\n const {\n params,\n context: { origin },\n } = args;\n\n if (!isObject(params)) {\n throw rpcErrors.invalidParams({\n message: 'Invalid params: Expected params to be a single object.',\n });\n }\n\n const validatedParams = getValidatedParams(params);\n const placeholder = isPromptDialog(validatedParams)\n ? validatedParams.placeholder\n : undefined;\n\n const validatedType = hasProperty(validatedParams, 'type')\n ? validatedParams.type\n : 'default';\n\n const approvalType =\n DIALOG_APPROVAL_TYPES[\n validatedType as keyof typeof DIALOG_APPROVAL_TYPES\n ];\n\n if (hasProperty(validatedParams, 'content')) {\n const id = messenger.call(\n 'SnapInterfaceController:createInterface',\n origin,\n validatedParams.content as Component,\n );\n\n messenger.call(\n 'SnapInterfaceController:setInterfaceDisplayed',\n origin,\n id,\n );\n\n return messenger.call(\n 'ApprovalController:addRequest',\n {\n id: approvalType === DIALOG_APPROVAL_TYPES.default ? id : undefined,\n origin,\n type: approvalType,\n requestData: { id, placeholder },\n },\n true,\n );\n }\n\n validateInterface(origin, validatedParams.id, messenger);\n\n messenger.call(\n 'SnapInterfaceController:setInterfaceDisplayed',\n origin,\n validatedParams.id,\n );\n\n return messenger.call(\n 'ApprovalController:addRequest',\n {\n id:\n approvalType === DIALOG_APPROVAL_TYPES.default\n ? validatedParams.id\n : undefined,\n origin,\n type: approvalType,\n requestData: { id: validatedParams.id, placeholder },\n },\n true,\n );\n };\n}\n/**\n * Validate that the interface ID is valid.\n *\n * @param origin - The origin of the request.\n * @param id - The interface ID.\n * @param messenger - The messenger.\n */\nfunction validateInterface(\n origin: string,\n id: string,\n messenger: Messenger<string, SnapInterfaceControllerGetInterfaceAction>,\n) {\n try {\n messenger.call('SnapInterfaceController:getInterface', origin, id);\n } catch (error) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}`,\n });\n }\n}\n\n/**\n * Gets the dialog type from the dialog parameters.\n *\n * @param params - The dialog parameters.\n * @returns The dialog type.\n */\nfunction getDialogType(params: DialogParameters): DialogType | undefined {\n return hasProperty(params, 'type') ? (params.type as DialogType) : undefined;\n}\n\n/**\n * Checks if the dialog parameters are for a prompt dialog.\n *\n * @param params - The dialog parameters.\n * @returns `true` if the dialog parameters are for a prompt dialog, `false` otherwise.\n */\nfunction isPromptDialog(params: DialogParameters): params is PromptDialog {\n return getDialogType(params) === DialogType.Prompt;\n}\n\n/**\n * Validates the confirm method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated confirm method parameter object.\n */\nfunction getValidatedParams(params: unknown): DialogParameters {\n try {\n return create(params, DialogParametersStruct);\n } catch (error) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}`,\n });\n }\n}\n"]}
|