@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,11 +1,12 @@
|
|
|
1
|
+
import type { Messenger } from "@metamask/messenger";
|
|
1
2
|
import type { PermissionSpecificationBuilder, RestrictedMethodOptions } from "@metamask/permission-controller";
|
|
2
3
|
import { PermissionType } from "@metamask/permission-controller";
|
|
3
4
|
import { DialogType } from "@metamask/snaps-sdk";
|
|
4
|
-
import type { DialogParams,
|
|
5
|
+
import type { DialogParams, DialogResult } from "@metamask/snaps-sdk";
|
|
5
6
|
import type { InferMatching } from "@metamask/snaps-utils";
|
|
6
7
|
import type { Infer } from "@metamask/superstruct";
|
|
7
|
-
import type {
|
|
8
|
-
import {
|
|
8
|
+
import type { NonEmptyArray } from "@metamask/utils";
|
|
9
|
+
import type { ApprovalControllerAddRequestAction, SnapInterfaceControllerCreateInterfaceAction, SnapInterfaceControllerGetInterfaceAction, SnapInterfaceControllerSetInterfaceDisplayedAction } from "../types.cjs";
|
|
9
10
|
declare const methodName = "snap_dialog";
|
|
10
11
|
export type DialogApprovalTypes = Record<DialogType, string> & {
|
|
11
12
|
default: string;
|
|
@@ -18,54 +19,10 @@ export declare const DIALOG_APPROVAL_TYPES: {
|
|
|
18
19
|
};
|
|
19
20
|
declare const PlaceholderStruct: import("@metamask/superstruct").Struct<string | undefined, null>;
|
|
20
21
|
export type Placeholder = Infer<typeof PlaceholderStruct>;
|
|
21
|
-
type
|
|
22
|
-
id?: string;
|
|
23
|
-
origin: string;
|
|
24
|
-
type: string;
|
|
25
|
-
requestData: {
|
|
26
|
-
id: string;
|
|
27
|
-
placeholder?: string;
|
|
28
|
-
};
|
|
29
|
-
};
|
|
30
|
-
type RequestUserApproval = (opts: RequestUserApprovalOptions) => Promise<boolean | null | string | Json>;
|
|
31
|
-
type CreateInterface = (snapId: string, content: ComponentOrElement, context?: InterfaceContext, contentType?: ContentType) => Promise<string>;
|
|
32
|
-
type GetInterface = (snapId: string, id: string) => {
|
|
33
|
-
content: ComponentOrElement;
|
|
34
|
-
snapId: SnapId;
|
|
35
|
-
state: InterfaceState;
|
|
36
|
-
};
|
|
37
|
-
export type DialogMethodHooks = {
|
|
38
|
-
/**
|
|
39
|
-
* @param opts - The `requestUserApproval` options.
|
|
40
|
-
* @param opts.id - The approval ID. If not provided, a new approval ID will be generated.
|
|
41
|
-
* @param opts.origin - The origin of the request. In this case, the Snap ID.
|
|
42
|
-
* @param opts.type - The type of the approval request.
|
|
43
|
-
* @param opts.requestData - The data of the approval request.
|
|
44
|
-
* @param opts.requestData.id - The ID of the interface.
|
|
45
|
-
* @param opts.requestData.placeholder - The placeholder of the `Prompt` dialog.
|
|
46
|
-
*/
|
|
47
|
-
requestUserApproval: RequestUserApproval;
|
|
48
|
-
/**
|
|
49
|
-
* @param snapId - The Snap ID creating the interface.
|
|
50
|
-
* @param content - The content of the interface.
|
|
51
|
-
*/
|
|
52
|
-
createInterface: CreateInterface;
|
|
53
|
-
/**
|
|
54
|
-
* @param snapId - The SnapId requesting the interface.
|
|
55
|
-
* @param id - The interface ID.
|
|
56
|
-
*/
|
|
57
|
-
getInterface: GetInterface;
|
|
58
|
-
/**
|
|
59
|
-
* Set the interface as displayed.
|
|
60
|
-
*
|
|
61
|
-
* @param snapId - The Snap ID requesting the interface.
|
|
62
|
-
* @param id - The interface ID.
|
|
63
|
-
*/
|
|
64
|
-
setInterfaceDisplayed: (snapId: string, id: string) => void;
|
|
65
|
-
};
|
|
22
|
+
export type DialogMessengerActions = ApprovalControllerAddRequestAction | SnapInterfaceControllerCreateInterfaceAction | SnapInterfaceControllerGetInterfaceAction | SnapInterfaceControllerSetInterfaceDisplayedAction;
|
|
66
23
|
type DialogSpecificationBuilderOptions = {
|
|
67
24
|
allowedCaveats?: Readonly<NonEmptyArray<string>> | null;
|
|
68
|
-
|
|
25
|
+
messenger: Messenger<string, DialogMessengerActions>;
|
|
69
26
|
};
|
|
70
27
|
/**
|
|
71
28
|
* Display a [dialog](https://docs.metamask.io/snaps/features/custom-ui/dialogs/)
|
|
@@ -116,7 +73,7 @@ export declare const dialogBuilder: Readonly<{
|
|
|
116
73
|
methodImplementation: ReturnType<typeof getDialogImplementation>;
|
|
117
74
|
allowedCaveats: Readonly<NonEmptyArray<string>> | null;
|
|
118
75
|
}>;
|
|
119
|
-
readonly
|
|
76
|
+
readonly actionNames: readonly ["ApprovalController:addRequest", "SnapInterfaceController:createInterface", "SnapInterfaceController:getInterface", "SnapInterfaceController:setInterfaceDisplayed"];
|
|
120
77
|
}>;
|
|
121
78
|
declare const DialogParametersStruct: import("@metamask/superstruct").Struct<{
|
|
122
79
|
id: string;
|
|
@@ -491,16 +448,11 @@ export type DialogParameters = InferMatching<typeof DialogParametersStruct, Dial
|
|
|
491
448
|
/**
|
|
492
449
|
* Builds the method implementation for `snap_dialog`.
|
|
493
450
|
*
|
|
494
|
-
* @param
|
|
495
|
-
* @param
|
|
496
|
-
* This function should return a Promise that resolves with the appropriate value when the user has approved or rejected the request.
|
|
497
|
-
* @param hooks.createInterface - A function that creates the interface in SnapInterfaceController.
|
|
498
|
-
* @param hooks.getInterface - A function that gets an interface from SnapInterfaceController.
|
|
499
|
-
* @param hooks.setInterfaceDisplayed - A function that sets the interface as
|
|
500
|
-
* displayed in SnapInterfaceController.
|
|
451
|
+
* @param options - The options.
|
|
452
|
+
* @param options.messenger - The messenger.
|
|
501
453
|
* @returns The method implementation which return value depends on the dialog
|
|
502
454
|
* type, valid return types are: string, boolean, null.
|
|
503
455
|
*/
|
|
504
|
-
export declare function getDialogImplementation({
|
|
456
|
+
export declare function getDialogImplementation({ messenger, }: DialogSpecificationBuilderOptions): (args: RestrictedMethodOptions<DialogParameters>) => Promise<DialogResult>;
|
|
505
457
|
export {};
|
|
506
458
|
//# sourceMappingURL=dialog.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dialog.d.cts","sourceRoot":"","sources":["../../src/restricted/dialog.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,8BAA8B,EAC9B,uBAAuB,EAExB,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAe,wCAAwC;AAE9E,OAAO,EACL,UAAU,EAIX,4BAA4B;AAC7B,OAAO,KAAK,EACV,YAAY,
|
|
1
|
+
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e,gBAAgB;oBACnC,iBAAiB;8BACP,WAAW,8BAA8B,CAAC;wBAChD,SAAS,cAAc,MAAM,CAAC,CAAC,GAAG,IAAI;;;EAoF7C,CAAC;AAuEZ,QAAA,MAAM,sBAAsB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;QAmB1B,CAAC;AAEH,MAAM,MAAM,gBAAgB,GAAG,aAAa,CAC1C,OAAO,sBAAsB,EAC7B,YAAY,CACb,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CAAC,EACtC,SAAS,GACV,EAAE,iCAAiC,UAE1B,wBAAwB,gBAAgB,CAAC,KAC9C,QAAQ,YAAY,CAAC,CAyEzB"}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
import type { Messenger } from "@metamask/messenger";
|
|
1
2
|
import type { PermissionSpecificationBuilder, RestrictedMethodOptions } from "@metamask/permission-controller";
|
|
2
3
|
import { PermissionType } from "@metamask/permission-controller";
|
|
3
4
|
import { DialogType } from "@metamask/snaps-sdk";
|
|
4
|
-
import type { DialogParams,
|
|
5
|
+
import type { DialogParams, DialogResult } from "@metamask/snaps-sdk";
|
|
5
6
|
import type { InferMatching } from "@metamask/snaps-utils";
|
|
6
7
|
import type { Infer } from "@metamask/superstruct";
|
|
7
|
-
import type {
|
|
8
|
-
import {
|
|
8
|
+
import type { NonEmptyArray } from "@metamask/utils";
|
|
9
|
+
import type { ApprovalControllerAddRequestAction, SnapInterfaceControllerCreateInterfaceAction, SnapInterfaceControllerGetInterfaceAction, SnapInterfaceControllerSetInterfaceDisplayedAction } from "../types.mjs";
|
|
9
10
|
declare const methodName = "snap_dialog";
|
|
10
11
|
export type DialogApprovalTypes = Record<DialogType, string> & {
|
|
11
12
|
default: string;
|
|
@@ -18,54 +19,10 @@ export declare const DIALOG_APPROVAL_TYPES: {
|
|
|
18
19
|
};
|
|
19
20
|
declare const PlaceholderStruct: import("@metamask/superstruct").Struct<string | undefined, null>;
|
|
20
21
|
export type Placeholder = Infer<typeof PlaceholderStruct>;
|
|
21
|
-
type
|
|
22
|
-
id?: string;
|
|
23
|
-
origin: string;
|
|
24
|
-
type: string;
|
|
25
|
-
requestData: {
|
|
26
|
-
id: string;
|
|
27
|
-
placeholder?: string;
|
|
28
|
-
};
|
|
29
|
-
};
|
|
30
|
-
type RequestUserApproval = (opts: RequestUserApprovalOptions) => Promise<boolean | null | string | Json>;
|
|
31
|
-
type CreateInterface = (snapId: string, content: ComponentOrElement, context?: InterfaceContext, contentType?: ContentType) => Promise<string>;
|
|
32
|
-
type GetInterface = (snapId: string, id: string) => {
|
|
33
|
-
content: ComponentOrElement;
|
|
34
|
-
snapId: SnapId;
|
|
35
|
-
state: InterfaceState;
|
|
36
|
-
};
|
|
37
|
-
export type DialogMethodHooks = {
|
|
38
|
-
/**
|
|
39
|
-
* @param opts - The `requestUserApproval` options.
|
|
40
|
-
* @param opts.id - The approval ID. If not provided, a new approval ID will be generated.
|
|
41
|
-
* @param opts.origin - The origin of the request. In this case, the Snap ID.
|
|
42
|
-
* @param opts.type - The type of the approval request.
|
|
43
|
-
* @param opts.requestData - The data of the approval request.
|
|
44
|
-
* @param opts.requestData.id - The ID of the interface.
|
|
45
|
-
* @param opts.requestData.placeholder - The placeholder of the `Prompt` dialog.
|
|
46
|
-
*/
|
|
47
|
-
requestUserApproval: RequestUserApproval;
|
|
48
|
-
/**
|
|
49
|
-
* @param snapId - The Snap ID creating the interface.
|
|
50
|
-
* @param content - The content of the interface.
|
|
51
|
-
*/
|
|
52
|
-
createInterface: CreateInterface;
|
|
53
|
-
/**
|
|
54
|
-
* @param snapId - The SnapId requesting the interface.
|
|
55
|
-
* @param id - The interface ID.
|
|
56
|
-
*/
|
|
57
|
-
getInterface: GetInterface;
|
|
58
|
-
/**
|
|
59
|
-
* Set the interface as displayed.
|
|
60
|
-
*
|
|
61
|
-
* @param snapId - The Snap ID requesting the interface.
|
|
62
|
-
* @param id - The interface ID.
|
|
63
|
-
*/
|
|
64
|
-
setInterfaceDisplayed: (snapId: string, id: string) => void;
|
|
65
|
-
};
|
|
22
|
+
export type DialogMessengerActions = ApprovalControllerAddRequestAction | SnapInterfaceControllerCreateInterfaceAction | SnapInterfaceControllerGetInterfaceAction | SnapInterfaceControllerSetInterfaceDisplayedAction;
|
|
66
23
|
type DialogSpecificationBuilderOptions = {
|
|
67
24
|
allowedCaveats?: Readonly<NonEmptyArray<string>> | null;
|
|
68
|
-
|
|
25
|
+
messenger: Messenger<string, DialogMessengerActions>;
|
|
69
26
|
};
|
|
70
27
|
/**
|
|
71
28
|
* Display a [dialog](https://docs.metamask.io/snaps/features/custom-ui/dialogs/)
|
|
@@ -116,7 +73,7 @@ export declare const dialogBuilder: Readonly<{
|
|
|
116
73
|
methodImplementation: ReturnType<typeof getDialogImplementation>;
|
|
117
74
|
allowedCaveats: Readonly<NonEmptyArray<string>> | null;
|
|
118
75
|
}>;
|
|
119
|
-
readonly
|
|
76
|
+
readonly actionNames: readonly ["ApprovalController:addRequest", "SnapInterfaceController:createInterface", "SnapInterfaceController:getInterface", "SnapInterfaceController:setInterfaceDisplayed"];
|
|
120
77
|
}>;
|
|
121
78
|
declare const DialogParametersStruct: import("@metamask/superstruct").Struct<{
|
|
122
79
|
id: string;
|
|
@@ -491,16 +448,11 @@ export type DialogParameters = InferMatching<typeof DialogParametersStruct, Dial
|
|
|
491
448
|
/**
|
|
492
449
|
* Builds the method implementation for `snap_dialog`.
|
|
493
450
|
*
|
|
494
|
-
* @param
|
|
495
|
-
* @param
|
|
496
|
-
* This function should return a Promise that resolves with the appropriate value when the user has approved or rejected the request.
|
|
497
|
-
* @param hooks.createInterface - A function that creates the interface in SnapInterfaceController.
|
|
498
|
-
* @param hooks.getInterface - A function that gets an interface from SnapInterfaceController.
|
|
499
|
-
* @param hooks.setInterfaceDisplayed - A function that sets the interface as
|
|
500
|
-
* displayed in SnapInterfaceController.
|
|
451
|
+
* @param options - The options.
|
|
452
|
+
* @param options.messenger - The messenger.
|
|
501
453
|
* @returns The method implementation which return value depends on the dialog
|
|
502
454
|
* type, valid return types are: string, boolean, null.
|
|
503
455
|
*/
|
|
504
|
-
export declare function getDialogImplementation({
|
|
456
|
+
export declare function getDialogImplementation({ messenger, }: DialogSpecificationBuilderOptions): (args: RestrictedMethodOptions<DialogParameters>) => Promise<DialogResult>;
|
|
505
457
|
export {};
|
|
506
458
|
//# sourceMappingURL=dialog.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dialog.d.mts","sourceRoot":"","sources":["../../src/restricted/dialog.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,8BAA8B,EAC9B,uBAAuB,EAExB,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAe,wCAAwC;AAE9E,OAAO,EACL,UAAU,EAIX,4BAA4B;AAC7B,OAAO,KAAK,EACV,YAAY,
|
|
1
|
+
{"version":3,"file":"dialog.d.mts","sourceRoot":"","sources":["../../src/restricted/dialog.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EACV,8BAA8B,EAC9B,uBAAuB,EAExB,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAe,wCAAwC;AAE9E,OAAO,EACL,UAAU,EAIX,4BAA4B;AAC7B,OAAO,KAAK,EACV,YAAY,EAGZ,YA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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;QAmB1B,CAAC;AAEH,MAAM,MAAM,gBAAgB,GAAG,aAAa,CAC1C,OAAO,sBAAsB,EAC7B,YAAY,CACb,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CAAC,EACtC,SAAS,GACV,EAAE,iCAAiC,UAE1B,wBAAwB,gBAAgB,CAAC,KAC9C,QAAQ,YAAY,CAAC,CAyEzB"}
|
|
@@ -21,25 +21,18 @@ const PlaceholderStruct = optional(size(string(), 1, 40));
|
|
|
21
21
|
* @param options - The specification builder options.
|
|
22
22
|
* @param options.allowedCaveats - The optional allowed caveats for the
|
|
23
23
|
* permission.
|
|
24
|
-
* @param options.
|
|
25
|
-
* implementation.
|
|
24
|
+
* @param options.messenger - The messenger.
|
|
26
25
|
* @returns The specification for the `snap_dialog` permission.
|
|
27
26
|
*/
|
|
28
|
-
const specificationBuilder = ({ allowedCaveats = null,
|
|
27
|
+
const specificationBuilder = ({ allowedCaveats = null, messenger, }) => {
|
|
29
28
|
return {
|
|
30
29
|
permissionType: PermissionType.RestrictedMethod,
|
|
31
30
|
targetName: methodName,
|
|
32
31
|
allowedCaveats,
|
|
33
|
-
methodImplementation: getDialogImplementation(
|
|
32
|
+
methodImplementation: getDialogImplementation({ messenger }),
|
|
34
33
|
subjectTypes: [SubjectType.Snap],
|
|
35
34
|
};
|
|
36
35
|
};
|
|
37
|
-
const methodHooks = {
|
|
38
|
-
requestUserApproval: true,
|
|
39
|
-
createInterface: true,
|
|
40
|
-
getInterface: true,
|
|
41
|
-
setInterfaceDisplayed: true,
|
|
42
|
-
};
|
|
43
36
|
/* eslint-disable jsdoc/check-indentation */
|
|
44
37
|
/**
|
|
45
38
|
* Display a [dialog](https://docs.metamask.io/snaps/features/custom-ui/dialogs/)
|
|
@@ -85,7 +78,12 @@ const methodHooks = {
|
|
|
85
78
|
export const dialogBuilder = Object.freeze({
|
|
86
79
|
targetName: methodName,
|
|
87
80
|
specificationBuilder,
|
|
88
|
-
|
|
81
|
+
actionNames: [
|
|
82
|
+
'ApprovalController:addRequest',
|
|
83
|
+
'SnapInterfaceController:createInterface',
|
|
84
|
+
'SnapInterfaceController:getInterface',
|
|
85
|
+
'SnapInterfaceController:setInterfaceDisplayed',
|
|
86
|
+
],
|
|
89
87
|
});
|
|
90
88
|
/* eslint-enable jsdoc/check-indentation */
|
|
91
89
|
const AlertParametersWithContentStruct = object({
|
|
@@ -163,17 +161,12 @@ const DialogParametersStruct = selectiveUnion((value) => {
|
|
|
163
161
|
/**
|
|
164
162
|
* Builds the method implementation for `snap_dialog`.
|
|
165
163
|
*
|
|
166
|
-
* @param
|
|
167
|
-
* @param
|
|
168
|
-
* This function should return a Promise that resolves with the appropriate value when the user has approved or rejected the request.
|
|
169
|
-
* @param hooks.createInterface - A function that creates the interface in SnapInterfaceController.
|
|
170
|
-
* @param hooks.getInterface - A function that gets an interface from SnapInterfaceController.
|
|
171
|
-
* @param hooks.setInterfaceDisplayed - A function that sets the interface as
|
|
172
|
-
* displayed in SnapInterfaceController.
|
|
164
|
+
* @param options - The options.
|
|
165
|
+
* @param options.messenger - The messenger.
|
|
173
166
|
* @returns The method implementation which return value depends on the dialog
|
|
174
167
|
* type, valid return types are: string, boolean, null.
|
|
175
168
|
*/
|
|
176
|
-
export function getDialogImplementation({
|
|
169
|
+
export function getDialogImplementation({ messenger, }) {
|
|
177
170
|
return async function dialogImplementation(args) {
|
|
178
171
|
const { params, context: { origin }, } = args;
|
|
179
172
|
if (!isObject(params)) {
|
|
@@ -190,25 +183,25 @@ export function getDialogImplementation({ requestUserApproval, createInterface,
|
|
|
190
183
|
: 'default';
|
|
191
184
|
const approvalType = DIALOG_APPROVAL_TYPES[validatedType];
|
|
192
185
|
if (hasProperty(validatedParams, 'content')) {
|
|
193
|
-
const id =
|
|
194
|
-
setInterfaceDisplayed
|
|
195
|
-
return
|
|
186
|
+
const id = messenger.call('SnapInterfaceController:createInterface', origin, validatedParams.content);
|
|
187
|
+
messenger.call('SnapInterfaceController:setInterfaceDisplayed', origin, id);
|
|
188
|
+
return messenger.call('ApprovalController:addRequest', {
|
|
196
189
|
id: approvalType === DIALOG_APPROVAL_TYPES.default ? id : undefined,
|
|
197
190
|
origin,
|
|
198
191
|
type: approvalType,
|
|
199
192
|
requestData: { id, placeholder },
|
|
200
|
-
});
|
|
193
|
+
}, true);
|
|
201
194
|
}
|
|
202
|
-
validateInterface(origin, validatedParams.id,
|
|
203
|
-
setInterfaceDisplayed
|
|
204
|
-
return
|
|
195
|
+
validateInterface(origin, validatedParams.id, messenger);
|
|
196
|
+
messenger.call('SnapInterfaceController:setInterfaceDisplayed', origin, validatedParams.id);
|
|
197
|
+
return messenger.call('ApprovalController:addRequest', {
|
|
205
198
|
id: approvalType === DIALOG_APPROVAL_TYPES.default
|
|
206
199
|
? validatedParams.id
|
|
207
200
|
: undefined,
|
|
208
201
|
origin,
|
|
209
202
|
type: approvalType,
|
|
210
203
|
requestData: { id: validatedParams.id, placeholder },
|
|
211
|
-
});
|
|
204
|
+
}, true);
|
|
212
205
|
};
|
|
213
206
|
}
|
|
214
207
|
/**
|
|
@@ -216,11 +209,11 @@ export function getDialogImplementation({ requestUserApproval, createInterface,
|
|
|
216
209
|
*
|
|
217
210
|
* @param origin - The origin of the request.
|
|
218
211
|
* @param id - The interface ID.
|
|
219
|
-
* @param
|
|
212
|
+
* @param messenger - The messenger.
|
|
220
213
|
*/
|
|
221
|
-
function validateInterface(origin, id,
|
|
214
|
+
function validateInterface(origin, id, messenger) {
|
|
222
215
|
try {
|
|
223
|
-
getInterface
|
|
216
|
+
messenger.call('SnapInterfaceController:getInterface', origin, id);
|
|
224
217
|
}
|
|
225
218
|
catch (error) {
|
|
226
219
|
throw rpcErrors.invalidParams({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dialog.mjs","sourceRoot":"","sources":["../../src/restricted/dialog.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAC9E,OAAO,EAAE,SAAS,EAAE,6BAA6B;AACjD,OAAO,EACL,UAAU,EACV,SAAS,EACT,wBAAwB,EACxB,cAAc,EACf,4BAA4B;AAc7B,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,8BAA8B;AAE/E,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,aAAa,EAAE,wBAAwB;AAIvE,MAAM,UAAU,GAAG,aAAa,CAAC;AAMjC,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,GAAG,UAAU,QAAQ;IACzC,CAAC,UAAU,CAAC,YAAY,CAAC,EAAE,GAAG,UAAU,eAAe;IACvD,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,GAAG,UAAU,SAAS;IAC3C,OAAO,EAAE,UAAU;CACpB,CAAC;AAEF,MAAM,iBAAiB,GAAG,QAAQ,CAAC,IAAI,CAAC,MAAM,EAAE,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,cAAc,CAAC,gBAAgB;QAC/C,UAAU,EAAE,UAAU;QACtB,cAAc;QACd,oBAAoB,EAAE,uBAAuB,CAAC,WAAW,CAAC;QAC1D,YAAY,EAAE,CAAC,WAAW,CAAC,IAAI,CA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AED;;;;;GAKG;AACH,SAAS,aAAa,CAAC,MAAwB;IAC7C,OAAO,WAAW,CAAC,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,UAAU,CAAC,MAAM,CAAC;AACrD,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,MAAe;IACzC,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,MAAM,EAAE,sBAAsB,CAAC,CAAC;IAChD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,SAAS,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 }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"dialog.mjs","sourceRoot":"","sources":["../../src/restricted/dialog.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAC9E,OAAO,EAAE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AAa,CAAC,MAAM,CAAC,KAAK,UAAU,CAAC,MAAM,CAAC;AACrD,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,MAAe;IACzC,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,MAAM,EAAE,sBAAsB,CAAC,CAAC;IAChD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,SAAS,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"]}
|
|
@@ -13,15 +13,19 @@ const targetName = 'snap_getBip32Entropy';
|
|
|
13
13
|
* BIP-32 node.
|
|
14
14
|
*
|
|
15
15
|
* @param options - The specification builder options.
|
|
16
|
+
* @param options.messenger - The messenger.
|
|
16
17
|
* @param options.methodHooks - The RPC method hooks needed by the method implementation.
|
|
17
18
|
* @returns The specification for the `snap_getBip32Entropy` permission.
|
|
18
19
|
*/
|
|
19
|
-
const specificationBuilder = ({ methodHooks }) => {
|
|
20
|
+
const specificationBuilder = ({ methodHooks, messenger, }) => {
|
|
20
21
|
return {
|
|
21
22
|
permissionType: permission_controller_1.PermissionType.RestrictedMethod,
|
|
22
23
|
targetName,
|
|
23
24
|
allowedCaveats: [snaps_utils_1.SnapCaveatType.PermittedDerivationPaths],
|
|
24
|
-
methodImplementation: getBip32EntropyImplementation(
|
|
25
|
+
methodImplementation: getBip32EntropyImplementation({
|
|
26
|
+
methodHooks,
|
|
27
|
+
messenger,
|
|
28
|
+
}),
|
|
25
29
|
validator: ({ caveats }) => {
|
|
26
30
|
if (caveats?.length !== 1 ||
|
|
27
31
|
caveats[0].type !== snaps_utils_1.SnapCaveatType.PermittedDerivationPaths) {
|
|
@@ -34,8 +38,6 @@ const specificationBuilder = ({ methodHooks }) => {
|
|
|
34
38
|
};
|
|
35
39
|
};
|
|
36
40
|
const methodHooks = {
|
|
37
|
-
getMnemonic: true,
|
|
38
|
-
getMnemonicSeed: true,
|
|
39
41
|
getUnlockPromise: true,
|
|
40
42
|
getClientCryptography: true,
|
|
41
43
|
};
|
|
@@ -98,28 +100,29 @@ exports.getBip32EntropyBuilder = Object.freeze({
|
|
|
98
100
|
targetName,
|
|
99
101
|
specificationBuilder,
|
|
100
102
|
methodHooks,
|
|
103
|
+
actionNames: ['KeyringController:withKeyring'],
|
|
101
104
|
});
|
|
102
105
|
/**
|
|
103
106
|
* Builds the method implementation for `snap_getBip32Entropy`.
|
|
104
107
|
*
|
|
105
|
-
* @param
|
|
106
|
-
* @param
|
|
107
|
-
* @param
|
|
108
|
-
* @param
|
|
108
|
+
* @param options - The options.
|
|
109
|
+
* @param options.messenger - The messenger.
|
|
110
|
+
* @param options.methodHooks - The RPC method hooks.
|
|
111
|
+
* @param options.methodHooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked
|
|
109
112
|
* and prompts the user to unlock their MetaMask if it is locked.
|
|
110
|
-
* @param
|
|
113
|
+
* @param options.methodHooks.getClientCryptography - A function to retrieve the cryptographic
|
|
111
114
|
* functions to use for the client.
|
|
112
115
|
* @returns The method implementation which returns a `JsonSLIP10Node`.
|
|
113
116
|
* @throws If the params are invalid.
|
|
114
117
|
*/
|
|
115
|
-
function getBip32EntropyImplementation({
|
|
118
|
+
function getBip32EntropyImplementation({ methodHooks: { getUnlockPromise, getClientCryptography }, messenger, }) {
|
|
116
119
|
return async function getBip32Entropy(args) {
|
|
117
120
|
await getUnlockPromise(true);
|
|
118
121
|
const { params } = args;
|
|
119
122
|
(0, utils_1.assert)(params);
|
|
120
123
|
// Using the seed is much faster, but we can only do it for these specific curves.
|
|
121
124
|
if (params.curve === 'secp256k1' || params.curve === 'ed25519') {
|
|
122
|
-
const seed = await (0, utils_2.getValueFromEntropySource)(getMnemonicSeed, params.source);
|
|
125
|
+
const seed = await (0, utils_2.getValueFromEntropySource)(utils_2.getMnemonicSeed.bind(null, messenger), params.source);
|
|
123
126
|
const node = await (0, utils_2.getNodeFromSeed)({
|
|
124
127
|
curve: params.curve,
|
|
125
128
|
path: params.path,
|
|
@@ -128,7 +131,7 @@ function getBip32EntropyImplementation({ getMnemonic, getMnemonicSeed, getUnlock
|
|
|
128
131
|
});
|
|
129
132
|
return node.toJSON();
|
|
130
133
|
}
|
|
131
|
-
const secretRecoveryPhrase = await (0, utils_2.getValueFromEntropySource)(getMnemonic, params.source);
|
|
134
|
+
const secretRecoveryPhrase = await (0, utils_2.getValueFromEntropySource)(utils_2.getMnemonic.bind(null, messenger), params.source);
|
|
132
135
|
const node = await (0, utils_2.getNodeFromMnemonic)({
|
|
133
136
|
curve: params.curve,
|
|
134
137
|
path: params.path,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getBip32Entropy.cjs","sourceRoot":"","sources":["../../src/restricted/getBip32Entropy.ts"],"names":[],"mappings":";;;AAOA,2EAA8E;AAC9E,qDAAiD;AAKjD,uDAAuD;AAEvD,2CAAyC;AAGzC,wCAIkB;AAElB,MAAM,UAAU,GAAG,sBAAsB,CAAC;AAoD1C;;;;;;;;GAQG;AACH,MAAM,oBAAoB,GAItB,CAAC,EAAE,WAAW,EAA8C,EAAE,EAAE;IAClE,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,gBAAgB;QAC/C,UAAU;QACV,cAAc,EAAE,CAAC,4BAAc,CAAC,wBAAwB,CAAC;QACzD,oBAAoB,EAAE,6BAA6B,CAAC,WAAW,CAAC;QAChE,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE;YACzB,IACE,OAAO,EAAE,MAAM,KAAK,CAAC;gBACrB,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,4BAAc,CAAC,wBAAwB,EAC3D,CAAC;gBACD,MAAM,sBAAS,CAAC,aAAa,CAAC;oBAC5B,OAAO,EAAE,sBAAsB,4BAAc,CAAC,wBAAwB,WAAW;iBAClF,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QACD,YAAY,EAAE,CAAC,mCAAW,CAAC,IAAI,CAAC;KACjC,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,WAAW,GAAkD;IACjE,WAAW,EAAE,IAAI;IACjB,eAAe,EAAE,IAAI;IACrB,gBAAgB,EAAE,IAAI;IACtB,qBAAqB,EAAE,IAAI;CAC5B,CAAC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E,CAAC;IACvB,CAAC,CAAC;AACJ,CAAC;AA7CD,sEA6CC","sourcesContent":["import type { CryptographicFunctions } from '@metamask/key-tree';\nimport type {\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n GetBip32EntropyParams,\n GetBip32EntropyResult,\n} from '@metamask/snaps-sdk';\nimport { SnapCaveatType } from '@metamask/snaps-utils';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport {\n getNodeFromMnemonic,\n getNodeFromSeed,\n getValueFromEntropySource,\n} from '../utils';\n\nconst targetName = 'snap_getBip32Entropy';\n\nexport type GetBip32EntropyMethodHooks = {\n /**\n * Get the mnemonic of the provided source. If no source is provided, the\n * mnemonic of the primary keyring will be returned.\n *\n * @param source - The optional ID of the source to get the mnemonic of.\n * @returns The mnemonic of the provided source, or the default source if no\n * source is provided.\n */\n getMnemonic: (source?: string | undefined) => Promise<Uint8Array>;\n\n /**\n * Get the mnemonic seed of the provided source. If no source is provided, the\n * mnemonic seed of the primary keyring will be returned.\n *\n * @param source - The optional ID of the source to get the mnemonic of.\n * @returns The mnemonic seed of the provided source, or the default source if no\n * source is provided.\n */\n getMnemonicSeed: (source?: string | undefined) => Promise<Uint8Array>;\n\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * Get the cryptographic functions to use for the client. This may return an\n * empty object or `undefined` to fall back to the default cryptographic\n * functions.\n *\n * @returns The cryptographic functions to use for the client.\n */\n getClientCryptography: () => CryptographicFunctions | undefined;\n};\n\ntype GetBip32EntropySpecificationBuilderOptions = {\n methodHooks: GetBip32EntropyMethodHooks;\n};\n\ntype GetBip32EntropySpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof targetName;\n methodImplementation: ReturnType<typeof getBip32EntropyImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\n/**\n * The specification builder for the `snap_getBip32Entropy` permission.\n * `snap_getBip32Entropy` lets the Snap control private keys for a particular\n * BIP-32 node.\n *\n * @param options - The specification builder options.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_getBip32Entropy` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n GetBip32EntropySpecificationBuilderOptions,\n GetBip32EntropySpecification\n> = ({ methodHooks }: GetBip32EntropySpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName,\n allowedCaveats: [SnapCaveatType.PermittedDerivationPaths],\n methodImplementation: getBip32EntropyImplementation(methodHooks),\n validator: ({ caveats }) => {\n if (\n caveats?.length !== 1 ||\n caveats[0].type !== SnapCaveatType.PermittedDerivationPaths\n ) {\n throw rpcErrors.invalidParams({\n message: `Expected a single \"${SnapCaveatType.PermittedDerivationPaths}\" caveat.`,\n });\n }\n },\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<GetBip32EntropyMethodHooks> = {\n getMnemonic: true,\n getMnemonicSeed: true,\n getUnlockPromise: true,\n getClientCryptography: true,\n};\n\n/**\n * Enables you to [manage users' non-EVM accounts](https://docs.metamask.io/snaps/features/non-evm-networks/)\n * by deriving the [SLIP-10](https://github.com/satoshilabs/slips/blob/master/slip-0010.md)\n * keys specified by the `path` and `curve` parameters. The keys are derived\n * using the entropy from the user's Secret Recovery Phrase.\n *\n * If the keys you want to derive conform to the [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)\n * structure, use [snap_getBip44Entropy](https://docs.metamask.io/snaps/reference/snaps-api/snap_getbip44entropy)\n * instead. Otherwise, use this method.\n *\n * This method is designed to be used with the [`@metamask/key-tree`](https://npmjs.com/package/@metamask/key-tree)\n * module. `@metamask/key-tree` can help you get the [extended private keys](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#extended-keys)\n * for user addresses, but it's your responsibility to know how to use those\n * keys to, for example, derive an address for the relevant protocol or sign a\n * transaction for the user.\n *\n * @example\n * ```json name=\"Manifest\"\n * {\n * \"initialPermissions\": {\n * \"snap_getBip32Entropy\": [\n * {\n * \"path\": [\"m\", \"44'\", \"3'\"],\n * \"curve\": \"secp256k1\" // Or \"ed25519\", \"ed25519Bip32\"\n * }\n * ]\n * }\n * }\n * ```\n * ```ts name=\"Usage\"\n * import { SLIP10Node } from '@metamask/key-tree'\n *\n * // This example uses Dogecoin, which has a derivation path starting with\n * // m/44'/3'.\n * const dogecoinNode = await snap.request({\n * method: 'snap_getBip32Entropy',\n * params: {\n * // The path and curve must be specified in the initial permissions.\n * path: ['m', \"44'\", \"3'\"],\n * curve: 'secp256k1',\n * },\n * })\n *\n * // Next, create an instance of a SLIP-10 node for the Dogecoin node.\n * const dogecoinSlip10Node = await SLIP10Node.fromJSON(dogecoinNode)\n *\n * // m/44'/3'/0'\n * const accountKey0 = await dogecoinSlip10Node.derive([\"bip32:0'\"])\n *\n * // m/44'/3'/1'\n * const accountKey1 = await dogecoinSlip10Node.derive([\"bip32:1'\"])\n *\n * // Now, you can ask the user to sign transactions, etc.\n * ```\n */\nexport const getBip32EntropyBuilder = Object.freeze({\n targetName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_getBip32Entropy`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.\n * @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed of the user.\n * @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked\n * and prompts the user to unlock their MetaMask if it is locked.\n * @param hooks.getClientCryptography - A function to retrieve the cryptographic\n * functions to use for the client.\n * @returns The method implementation which returns a `JsonSLIP10Node`.\n * @throws If the params are invalid.\n */\nexport function getBip32EntropyImplementation({\n getMnemonic,\n getMnemonicSeed,\n getUnlockPromise,\n getClientCryptography,\n}: GetBip32EntropyMethodHooks) {\n return async function getBip32Entropy(\n args: RestrictedMethodOptions<GetBip32EntropyParams>,\n ): Promise<GetBip32EntropyResult> {\n await getUnlockPromise(true);\n\n const { params } = args;\n assert(params);\n\n // Using the seed is much faster, but we can only do it for these specific curves.\n if (params.curve === 'secp256k1' || params.curve === 'ed25519') {\n const seed = await getValueFromEntropySource(\n getMnemonicSeed,\n params.source,\n );\n\n const node = await getNodeFromSeed({\n curve: params.curve,\n path: params.path,\n seed,\n cryptographicFunctions: getClientCryptography(),\n });\n\n return node.toJSON();\n }\n\n const secretRecoveryPhrase = await getValueFromEntropySource(\n getMnemonic,\n params.source,\n );\n\n const node = await getNodeFromMnemonic({\n curve: params.curve,\n path: params.path,\n secretRecoveryPhrase,\n cryptographicFunctions: getClientCryptography(),\n });\n\n return node.toJSON();\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"getBip32Entropy.cjs","sourceRoot":"","sources":["../../src/restricted/getBip32Entropy.ts"],"names":[],"mappings":";;;AAQA,2EAA8E;AAC9E,qDAAiD;AAKjD,uDAAuD;AAEvD,2CAAyC;AAIzC,wCAMkB;AAElB,MAAM,UAAU,GAAG,sBAAsB,CAAC;AAoC1C;;;;;;;;;GASG;AACH,MAAM,oBAAoB,GAItB,CAAC,EACH,WAAW,EACX,SAAS,GACkC,EAAE,EAAE;IAC/C,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,gBAAgB;QAC/C,UAAU;QACV,cAAc,EAAE,CAAC,4BAAc,CAAC,wBAAwB,CAAC;QACzD,oBAAoB,EAAE,6BAA6B,CAAC;YAClD,WAAW;YACX,SAAS;SACV,CAAC;QACF,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE;YACzB,IAC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EAAC;gBACjC,KAAK,EAAE,MAAM,CAAC,KAAK;gBACnB,IAAI,EAAE,MAAM,CAAC,IAAI;gBACjB,IAAI;gBACJ,sBAAsB,EAAE,qBAAqB,EAAE;aAChD,CAAC,CAAC;YAEH,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;QACvB,CAAC;QAED,MAAM,oBAAoB,GAAG,MAAM,IAAA,iCAAyB,EAC1D,mBAAW,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,CAAC,EACjC,MAAM,CAAC,MAAM,CACd,CAAC;QAEF,MAAM,IAAI,GAAG,MAAM,IAAA,2BAAmB,EAAC;YACrC,KAAK,EAAE,MAAM,CAAC,KAAK;YACnB,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,oBAAoB;YACpB,sBAAsB,EAAE,qBAAqB,EAAE;SAChD,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC,CAAC;AACJ,CAAC;AA3CD,sEA2CC","sourcesContent":["import type { CryptographicFunctions } from '@metamask/key-tree';\nimport type { Messenger } from '@metamask/messenger';\nimport type {\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n GetBip32EntropyParams,\n GetBip32EntropyResult,\n} from '@metamask/snaps-sdk';\nimport { SnapCaveatType } from '@metamask/snaps-utils';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\n\nimport type { KeyringControllerWithKeyringAction } from '../types';\nimport type { MethodHooksObject } from '../utils';\nimport {\n getMnemonic,\n getMnemonicSeed,\n getNodeFromMnemonic,\n getNodeFromSeed,\n getValueFromEntropySource,\n} from '../utils';\n\nconst targetName = 'snap_getBip32Entropy';\n\nexport type GetBip32EntropyMethodHooks = {\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * Get the cryptographic functions to use for the client. This may return an\n * empty object or `undefined` to fall back to the default cryptographic\n * functions.\n *\n * @returns The cryptographic functions to use for the client.\n */\n getClientCryptography: () => CryptographicFunctions | undefined;\n};\n\nexport type GetBip32EntropyMessengerActions =\n KeyringControllerWithKeyringAction;\n\ntype GetBip32EntropySpecificationBuilderOptions = {\n methodHooks: GetBip32EntropyMethodHooks;\n messenger: Messenger<string, GetBip32EntropyMessengerActions>;\n};\n\ntype GetBip32EntropySpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof targetName;\n methodImplementation: ReturnType<typeof getBip32EntropyImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\n/**\n * The specification builder for the `snap_getBip32Entropy` permission.\n * `snap_getBip32Entropy` lets the Snap control private keys for a particular\n * BIP-32 node.\n *\n * @param options - The specification builder options.\n * @param options.messenger - The messenger.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_getBip32Entropy` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n GetBip32EntropySpecificationBuilderOptions,\n GetBip32EntropySpecification\n> = ({\n methodHooks,\n messenger,\n}: GetBip32EntropySpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName,\n allowedCaveats: [SnapCaveatType.PermittedDerivationPaths],\n methodImplementation: getBip32EntropyImplementation({\n methodHooks,\n messenger,\n }),\n validator: ({ caveats }) => {\n if (\n caveats?.length !== 1 ||\n caveats[0].type !== SnapCaveatType.PermittedDerivationPaths\n ) {\n throw rpcErrors.invalidParams({\n message: `Expected a single \"${SnapCaveatType.PermittedDerivationPaths}\" caveat.`,\n });\n }\n },\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<GetBip32EntropyMethodHooks> = {\n getUnlockPromise: true,\n getClientCryptography: true,\n};\n\n/**\n * Enables you to [manage users' non-EVM accounts](https://docs.metamask.io/snaps/features/non-evm-networks/)\n * by deriving the [SLIP-10](https://github.com/satoshilabs/slips/blob/master/slip-0010.md)\n * keys specified by the `path` and `curve` parameters. The keys are derived\n * using the entropy from the user's Secret Recovery Phrase.\n *\n * If the keys you want to derive conform to the [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)\n * structure, use [snap_getBip44Entropy](https://docs.metamask.io/snaps/reference/snaps-api/snap_getbip44entropy)\n * instead. Otherwise, use this method.\n *\n * This method is designed to be used with the [`@metamask/key-tree`](https://npmjs.com/package/@metamask/key-tree)\n * module. `@metamask/key-tree` can help you get the [extended private keys](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#extended-keys)\n * for user addresses, but it's your responsibility to know how to use those\n * keys to, for example, derive an address for the relevant protocol or sign a\n * transaction for the user.\n *\n * @example\n * ```json name=\"Manifest\"\n * {\n * \"initialPermissions\": {\n * \"snap_getBip32Entropy\": [\n * {\n * \"path\": [\"m\", \"44'\", \"3'\"],\n * \"curve\": \"secp256k1\" // Or \"ed25519\", \"ed25519Bip32\"\n * }\n * ]\n * }\n * }\n * ```\n * ```ts name=\"Usage\"\n * import { SLIP10Node } from '@metamask/key-tree'\n *\n * // This example uses Dogecoin, which has a derivation path starting with\n * // m/44'/3'.\n * const dogecoinNode = await snap.request({\n * method: 'snap_getBip32Entropy',\n * params: {\n * // The path and curve must be specified in the initial permissions.\n * path: ['m', \"44'\", \"3'\"],\n * curve: 'secp256k1',\n * },\n * })\n *\n * // Next, create an instance of a SLIP-10 node for the Dogecoin node.\n * const dogecoinSlip10Node = await SLIP10Node.fromJSON(dogecoinNode)\n *\n * // m/44'/3'/0'\n * const accountKey0 = await dogecoinSlip10Node.derive([\"bip32:0'\"])\n *\n * // m/44'/3'/1'\n * const accountKey1 = await dogecoinSlip10Node.derive([\"bip32:1'\"])\n *\n * // Now, you can ask the user to sign transactions, etc.\n * ```\n */\nexport const getBip32EntropyBuilder = Object.freeze({\n targetName,\n specificationBuilder,\n methodHooks,\n actionNames: ['KeyringController:withKeyring'],\n} as const);\n\n/**\n * Builds the method implementation for `snap_getBip32Entropy`.\n *\n * @param options - The options.\n * @param options.messenger - The messenger.\n * @param options.methodHooks - The RPC method hooks.\n * @param options.methodHooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked\n * and prompts the user to unlock their MetaMask if it is locked.\n * @param options.methodHooks.getClientCryptography - A function to retrieve the cryptographic\n * functions to use for the client.\n * @returns The method implementation which returns a `JsonSLIP10Node`.\n * @throws If the params are invalid.\n */\nexport function getBip32EntropyImplementation({\n methodHooks: { getUnlockPromise, getClientCryptography },\n messenger,\n}: GetBip32EntropySpecificationBuilderOptions) {\n return async function getBip32Entropy(\n args: RestrictedMethodOptions<GetBip32EntropyParams>,\n ): Promise<GetBip32EntropyResult> {\n await getUnlockPromise(true);\n\n const { params } = args;\n assert(params);\n\n // Using the seed is much faster, but we can only do it for these specific curves.\n if (params.curve === 'secp256k1' || params.curve === 'ed25519') {\n const seed = await getValueFromEntropySource(\n getMnemonicSeed.bind(null, messenger),\n params.source,\n );\n\n const node = await getNodeFromSeed({\n curve: params.curve,\n path: params.path,\n seed,\n cryptographicFunctions: getClientCryptography(),\n });\n\n return node.toJSON();\n }\n\n const secretRecoveryPhrase = await getValueFromEntropySource(\n getMnemonic.bind(null, messenger),\n params.source,\n );\n\n const node = await getNodeFromMnemonic({\n curve: params.curve,\n path: params.path,\n secretRecoveryPhrase,\n cryptographicFunctions: getClientCryptography(),\n });\n\n return node.toJSON();\n };\n}\n"]}
|