@metamask/snaps-rpc-methods 14.3.0 → 15.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -1
- package/dist/permitted/cancelBackgroundEvent.cjs +20 -0
- package/dist/permitted/cancelBackgroundEvent.cjs.map +1 -1
- package/dist/permitted/cancelBackgroundEvent.d.cts +43 -3
- package/dist/permitted/cancelBackgroundEvent.d.cts.map +1 -1
- package/dist/permitted/cancelBackgroundEvent.d.mts +43 -3
- package/dist/permitted/cancelBackgroundEvent.d.mts.map +1 -1
- package/dist/permitted/cancelBackgroundEvent.mjs +20 -0
- package/dist/permitted/cancelBackgroundEvent.mjs.map +1 -1
- package/dist/permitted/clearState.cjs +13 -2
- package/dist/permitted/clearState.cjs.map +1 -1
- package/dist/permitted/clearState.d.cts +35 -3
- package/dist/permitted/clearState.d.cts.map +1 -1
- package/dist/permitted/clearState.d.mts +35 -3
- package/dist/permitted/clearState.d.mts.map +1 -1
- package/dist/permitted/clearState.mjs +13 -2
- package/dist/permitted/clearState.mjs.map +1 -1
- package/dist/permitted/closeWebSocket.cjs +19 -2
- package/dist/permitted/closeWebSocket.cjs.map +1 -1
- package/dist/permitted/closeWebSocket.d.cts +39 -4
- package/dist/permitted/closeWebSocket.d.cts.map +1 -1
- package/dist/permitted/closeWebSocket.d.mts +39 -4
- package/dist/permitted/closeWebSocket.d.mts.map +1 -1
- package/dist/permitted/closeWebSocket.mjs +19 -2
- package/dist/permitted/closeWebSocket.mjs.map +1 -1
- package/dist/permitted/createInterface.cjs +33 -2
- package/dist/permitted/createInterface.cjs.map +1 -1
- package/dist/permitted/createInterface.d.cts +68 -17
- package/dist/permitted/createInterface.d.cts.map +1 -1
- package/dist/permitted/createInterface.d.mts +68 -17
- package/dist/permitted/createInterface.d.mts.map +1 -1
- package/dist/permitted/createInterface.mjs +34 -3
- package/dist/permitted/createInterface.mjs.map +1 -1
- package/dist/permitted/endTrace.cjs +6 -2
- package/dist/permitted/endTrace.cjs.map +1 -1
- package/dist/permitted/endTrace.d.cts +28 -4
- package/dist/permitted/endTrace.d.cts.map +1 -1
- package/dist/permitted/endTrace.d.mts +28 -4
- package/dist/permitted/endTrace.d.mts.map +1 -1
- package/dist/permitted/endTrace.mjs +6 -2
- package/dist/permitted/endTrace.mjs.map +1 -1
- package/dist/permitted/getAllSnaps.cjs +4 -1
- package/dist/permitted/getAllSnaps.cjs.map +1 -1
- package/dist/permitted/getAllSnaps.d.cts +25 -3
- package/dist/permitted/getAllSnaps.d.cts.map +1 -1
- package/dist/permitted/getAllSnaps.d.mts +25 -3
- package/dist/permitted/getAllSnaps.d.mts.map +1 -1
- package/dist/permitted/getAllSnaps.mjs +4 -1
- package/dist/permitted/getAllSnaps.mjs.map +1 -1
- package/dist/permitted/getBackgroundEvents.cjs +24 -0
- package/dist/permitted/getBackgroundEvents.cjs.map +1 -1
- package/dist/permitted/getBackgroundEvents.d.cts +48 -3
- package/dist/permitted/getBackgroundEvents.d.cts.map +1 -1
- package/dist/permitted/getBackgroundEvents.d.mts +48 -3
- package/dist/permitted/getBackgroundEvents.d.mts.map +1 -1
- package/dist/permitted/getBackgroundEvents.mjs +24 -0
- package/dist/permitted/getBackgroundEvents.mjs.map +1 -1
- package/dist/permitted/getClientStatus.cjs +27 -2
- package/dist/permitted/getClientStatus.cjs.map +1 -1
- package/dist/permitted/getClientStatus.d.cts +50 -4
- package/dist/permitted/getClientStatus.d.cts.map +1 -1
- package/dist/permitted/getClientStatus.d.mts +50 -4
- package/dist/permitted/getClientStatus.d.mts.map +1 -1
- package/dist/permitted/getClientStatus.mjs +27 -2
- package/dist/permitted/getClientStatus.mjs.map +1 -1
- package/dist/permitted/getFile.cjs +28 -1
- package/dist/permitted/getFile.cjs.map +1 -1
- package/dist/permitted/getFile.d.cts +49 -3
- package/dist/permitted/getFile.d.cts.map +1 -1
- package/dist/permitted/getFile.d.mts +49 -3
- package/dist/permitted/getFile.d.mts.map +1 -1
- package/dist/permitted/getFile.mjs +28 -1
- package/dist/permitted/getFile.mjs.map +1 -1
- package/dist/permitted/getInterfaceContext.cjs +49 -3
- package/dist/permitted/getInterfaceContext.cjs.map +1 -1
- package/dist/permitted/getInterfaceContext.d.cts +65 -3
- package/dist/permitted/getInterfaceContext.d.cts.map +1 -1
- package/dist/permitted/getInterfaceContext.d.mts +65 -3
- package/dist/permitted/getInterfaceContext.d.mts.map +1 -1
- package/dist/permitted/getInterfaceContext.mjs +50 -4
- package/dist/permitted/getInterfaceContext.mjs.map +1 -1
- package/dist/permitted/getInterfaceState.cjs +27 -3
- package/dist/permitted/getInterfaceState.cjs.map +1 -1
- package/dist/permitted/getInterfaceState.d.cts +43 -3
- package/dist/permitted/getInterfaceState.d.cts.map +1 -1
- package/dist/permitted/getInterfaceState.d.mts +43 -3
- package/dist/permitted/getInterfaceState.d.mts.map +1 -1
- package/dist/permitted/getInterfaceState.mjs +28 -4
- package/dist/permitted/getInterfaceState.mjs.map +1 -1
- package/dist/permitted/getSnaps.cjs +21 -2
- package/dist/permitted/getSnaps.cjs.map +1 -1
- package/dist/permitted/getSnaps.d.cts +42 -4
- package/dist/permitted/getSnaps.d.cts.map +1 -1
- package/dist/permitted/getSnaps.d.mts +42 -4
- package/dist/permitted/getSnaps.d.mts.map +1 -1
- package/dist/permitted/getSnaps.mjs +21 -2
- package/dist/permitted/getSnaps.mjs.map +1 -1
- package/dist/permitted/getState.cjs +24 -2
- package/dist/permitted/getState.cjs.map +1 -1
- package/dist/permitted/getState.d.cts +46 -4
- package/dist/permitted/getState.d.cts.map +1 -1
- package/dist/permitted/getState.d.mts +46 -4
- package/dist/permitted/getState.d.mts.map +1 -1
- package/dist/permitted/getState.mjs +24 -2
- package/dist/permitted/getState.mjs.map +1 -1
- package/dist/permitted/getWebSockets.cjs +29 -2
- package/dist/permitted/getWebSockets.cjs.map +1 -1
- package/dist/permitted/getWebSockets.d.cts +50 -4
- package/dist/permitted/getWebSockets.d.cts.map +1 -1
- package/dist/permitted/getWebSockets.d.mts +50 -4
- package/dist/permitted/getWebSockets.d.mts.map +1 -1
- package/dist/permitted/getWebSockets.mjs +29 -2
- package/dist/permitted/getWebSockets.mjs.map +1 -1
- package/dist/permitted/handlers.cjs +0 -4
- package/dist/permitted/handlers.cjs.map +1 -1
- package/dist/permitted/handlers.d.cts +274 -56
- package/dist/permitted/handlers.d.cts.map +1 -1
- package/dist/permitted/handlers.d.mts +274 -56
- package/dist/permitted/handlers.d.mts.map +1 -1
- package/dist/permitted/handlers.mjs +0 -4
- package/dist/permitted/handlers.mjs.map +1 -1
- package/dist/permitted/index.cjs.map +1 -1
- package/dist/permitted/index.d.cts +1 -3
- package/dist/permitted/index.d.cts.map +1 -1
- package/dist/permitted/index.d.mts +1 -3
- package/dist/permitted/index.d.mts.map +1 -1
- package/dist/permitted/index.mjs.map +1 -1
- package/dist/permitted/invokeKeyring.cjs +8 -2
- package/dist/permitted/invokeKeyring.cjs.map +1 -1
- package/dist/permitted/invokeKeyring.d.cts +34 -4
- package/dist/permitted/invokeKeyring.d.cts.map +1 -1
- package/dist/permitted/invokeKeyring.d.mts +34 -4
- package/dist/permitted/invokeKeyring.d.mts.map +1 -1
- package/dist/permitted/invokeKeyring.mjs +8 -2
- package/dist/permitted/invokeKeyring.mjs.map +1 -1
- package/dist/permitted/invokeSnapSugar.cjs +25 -2
- package/dist/permitted/invokeSnapSugar.cjs.map +1 -1
- package/dist/permitted/invokeSnapSugar.d.cts +30 -3
- package/dist/permitted/invokeSnapSugar.d.cts.map +1 -1
- package/dist/permitted/invokeSnapSugar.d.mts +30 -3
- package/dist/permitted/invokeSnapSugar.d.mts.map +1 -1
- package/dist/permitted/invokeSnapSugar.mjs +25 -2
- package/dist/permitted/invokeSnapSugar.mjs.map +1 -1
- package/dist/permitted/listEntropySources.cjs +40 -1
- package/dist/permitted/listEntropySources.cjs.map +1 -1
- package/dist/permitted/listEntropySources.d.cts +64 -3
- package/dist/permitted/listEntropySources.d.cts.map +1 -1
- package/dist/permitted/listEntropySources.d.mts +64 -3
- package/dist/permitted/listEntropySources.d.mts.map +1 -1
- package/dist/permitted/listEntropySources.mjs +40 -1
- package/dist/permitted/listEntropySources.mjs.map +1 -1
- package/dist/permitted/openWebSocket.cjs +46 -2
- package/dist/permitted/openWebSocket.cjs.map +1 -1
- package/dist/permitted/openWebSocket.d.cts +66 -4
- package/dist/permitted/openWebSocket.d.cts.map +1 -1
- package/dist/permitted/openWebSocket.d.mts +66 -4
- package/dist/permitted/openWebSocket.d.mts.map +1 -1
- package/dist/permitted/openWebSocket.mjs +46 -2
- package/dist/permitted/openWebSocket.mjs.map +1 -1
- package/dist/permitted/requestSnaps.cjs +25 -2
- package/dist/permitted/requestSnaps.cjs.map +1 -1
- package/dist/permitted/requestSnaps.d.cts +51 -3
- package/dist/permitted/requestSnaps.d.cts.map +1 -1
- package/dist/permitted/requestSnaps.d.mts +51 -3
- package/dist/permitted/requestSnaps.d.mts.map +1 -1
- package/dist/permitted/requestSnaps.mjs +25 -2
- package/dist/permitted/requestSnaps.mjs.map +1 -1
- package/dist/permitted/resolveInterface.cjs +38 -2
- package/dist/permitted/resolveInterface.cjs.map +1 -1
- package/dist/permitted/resolveInterface.d.cts +55 -4
- package/dist/permitted/resolveInterface.d.cts.map +1 -1
- package/dist/permitted/resolveInterface.d.mts +55 -4
- package/dist/permitted/resolveInterface.d.mts.map +1 -1
- package/dist/permitted/resolveInterface.mjs +39 -3
- package/dist/permitted/resolveInterface.mjs.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.cjs +26 -0
- package/dist/permitted/scheduleBackgroundEvent.cjs.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.d.cts +49 -3
- package/dist/permitted/scheduleBackgroundEvent.d.cts.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.d.mts +49 -3
- package/dist/permitted/scheduleBackgroundEvent.d.mts.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.mjs +26 -0
- package/dist/permitted/scheduleBackgroundEvent.mjs.map +1 -1
- package/dist/permitted/sendWebSocketMessage.cjs +24 -2
- package/dist/permitted/sendWebSocketMessage.cjs.map +1 -1
- package/dist/permitted/sendWebSocketMessage.d.cts +44 -4
- package/dist/permitted/sendWebSocketMessage.d.cts.map +1 -1
- package/dist/permitted/sendWebSocketMessage.d.mts +44 -4
- package/dist/permitted/sendWebSocketMessage.d.mts.map +1 -1
- package/dist/permitted/sendWebSocketMessage.mjs +24 -2
- package/dist/permitted/sendWebSocketMessage.mjs.map +1 -1
- package/dist/permitted/setState.cjs +48 -2
- package/dist/permitted/setState.cjs.map +1 -1
- package/dist/permitted/setState.d.cts +72 -4
- package/dist/permitted/setState.d.cts.map +1 -1
- package/dist/permitted/setState.d.mts +72 -4
- package/dist/permitted/setState.d.mts.map +1 -1
- package/dist/permitted/setState.mjs +48 -2
- package/dist/permitted/setState.mjs.map +1 -1
- package/dist/permitted/startTrace.cjs +4 -1
- package/dist/permitted/startTrace.cjs.map +1 -1
- package/dist/permitted/startTrace.d.cts +26 -3
- package/dist/permitted/startTrace.d.cts.map +1 -1
- package/dist/permitted/startTrace.d.mts +26 -3
- package/dist/permitted/startTrace.d.mts.map +1 -1
- package/dist/permitted/startTrace.mjs +4 -1
- package/dist/permitted/startTrace.mjs.map +1 -1
- package/dist/permitted/trackError.cjs +4 -1
- package/dist/permitted/trackError.cjs.map +1 -1
- package/dist/permitted/trackError.d.cts +26 -3
- package/dist/permitted/trackError.d.cts.map +1 -1
- package/dist/permitted/trackError.d.mts +26 -3
- package/dist/permitted/trackError.d.mts.map +1 -1
- package/dist/permitted/trackError.mjs +4 -1
- package/dist/permitted/trackError.mjs.map +1 -1
- package/dist/permitted/trackEvent.cjs +4 -1
- package/dist/permitted/trackEvent.cjs.map +1 -1
- package/dist/permitted/trackEvent.d.cts +25 -4
- package/dist/permitted/trackEvent.d.cts.map +1 -1
- package/dist/permitted/trackEvent.d.mts +25 -4
- package/dist/permitted/trackEvent.d.mts.map +1 -1
- package/dist/permitted/trackEvent.mjs +4 -1
- package/dist/permitted/trackEvent.mjs.map +1 -1
- package/dist/permitted/updateInterface.cjs +47 -2
- package/dist/permitted/updateInterface.cjs.map +1 -1
- package/dist/permitted/updateInterface.d.cts +64 -3
- package/dist/permitted/updateInterface.d.cts.map +1 -1
- package/dist/permitted/updateInterface.d.mts +64 -3
- package/dist/permitted/updateInterface.d.mts.map +1 -1
- package/dist/permitted/updateInterface.mjs +48 -3
- package/dist/permitted/updateInterface.mjs.map +1 -1
- package/dist/restricted/caveats/snapIds.cjs.map +1 -1
- package/dist/restricted/caveats/snapIds.d.cts.map +1 -1
- package/dist/restricted/caveats/snapIds.d.mts.map +1 -1
- package/dist/restricted/caveats/snapIds.mjs.map +1 -1
- package/dist/restricted/dialog.cjs +49 -1
- package/dist/restricted/dialog.cjs.map +1 -1
- package/dist/restricted/dialog.d.cts +52 -2
- package/dist/restricted/dialog.d.cts.map +1 -1
- package/dist/restricted/dialog.d.mts +52 -2
- package/dist/restricted/dialog.d.mts.map +1 -1
- package/dist/restricted/dialog.mjs +49 -1
- package/dist/restricted/dialog.mjs.map +1 -1
- package/dist/restricted/getBip32Entropy.cjs +55 -0
- package/dist/restricted/getBip32Entropy.cjs.map +1 -1
- package/dist/restricted/getBip32Entropy.d.cts +55 -0
- package/dist/restricted/getBip32Entropy.d.cts.map +1 -1
- package/dist/restricted/getBip32Entropy.d.mts +55 -0
- package/dist/restricted/getBip32Entropy.d.mts.map +1 -1
- package/dist/restricted/getBip32Entropy.mjs +55 -0
- package/dist/restricted/getBip32Entropy.mjs.map +1 -1
- package/dist/restricted/getBip32PublicKey.cjs +36 -0
- package/dist/restricted/getBip32PublicKey.cjs.map +1 -1
- package/dist/restricted/getBip32PublicKey.d.cts +36 -0
- package/dist/restricted/getBip32PublicKey.d.cts.map +1 -1
- package/dist/restricted/getBip32PublicKey.d.mts +36 -0
- package/dist/restricted/getBip32PublicKey.d.mts.map +1 -1
- package/dist/restricted/getBip32PublicKey.mjs +36 -0
- package/dist/restricted/getBip32PublicKey.mjs.map +1 -1
- package/dist/restricted/getBip44Entropy.cjs +53 -0
- package/dist/restricted/getBip44Entropy.cjs.map +1 -1
- package/dist/restricted/getBip44Entropy.d.cts +53 -0
- package/dist/restricted/getBip44Entropy.d.cts.map +1 -1
- package/dist/restricted/getBip44Entropy.d.mts +53 -0
- package/dist/restricted/getBip44Entropy.d.mts.map +1 -1
- package/dist/restricted/getBip44Entropy.mjs +53 -0
- package/dist/restricted/getBip44Entropy.mjs.map +1 -1
- package/dist/restricted/getEntropy.cjs +35 -0
- package/dist/restricted/getEntropy.cjs.map +1 -1
- package/dist/restricted/getEntropy.d.cts +35 -0
- package/dist/restricted/getEntropy.d.cts.map +1 -1
- package/dist/restricted/getEntropy.d.mts +35 -0
- package/dist/restricted/getEntropy.d.mts.map +1 -1
- package/dist/restricted/getEntropy.mjs +35 -0
- package/dist/restricted/getEntropy.mjs.map +1 -1
- package/dist/restricted/getLocale.cjs +11 -0
- package/dist/restricted/getLocale.cjs.map +1 -1
- package/dist/restricted/getLocale.d.cts +11 -0
- package/dist/restricted/getLocale.d.cts.map +1 -1
- package/dist/restricted/getLocale.d.mts +11 -0
- package/dist/restricted/getLocale.d.mts.map +1 -1
- package/dist/restricted/getLocale.mjs +11 -0
- package/dist/restricted/getLocale.mjs.map +1 -1
- package/dist/restricted/getPreferences.cjs +24 -0
- package/dist/restricted/getPreferences.cjs.map +1 -1
- package/dist/restricted/getPreferences.d.cts +24 -0
- package/dist/restricted/getPreferences.d.cts.map +1 -1
- package/dist/restricted/getPreferences.d.mts +24 -0
- package/dist/restricted/getPreferences.d.mts.map +1 -1
- package/dist/restricted/getPreferences.mjs +24 -0
- package/dist/restricted/getPreferences.mjs.map +1 -1
- package/dist/restricted/index.d.cts +2 -2
- package/dist/restricted/index.d.mts +2 -2
- package/dist/restricted/invokeSnap.cjs +24 -2
- package/dist/restricted/invokeSnap.cjs.map +1 -1
- package/dist/restricted/invokeSnap.d.cts +29 -11
- package/dist/restricted/invokeSnap.d.cts.map +1 -1
- package/dist/restricted/invokeSnap.d.mts +29 -11
- package/dist/restricted/invokeSnap.d.mts.map +1 -1
- package/dist/restricted/invokeSnap.mjs +24 -2
- package/dist/restricted/invokeSnap.mjs.map +1 -1
- package/dist/restricted/manageAccounts.cjs +10 -0
- package/dist/restricted/manageAccounts.cjs.map +1 -1
- package/dist/restricted/manageAccounts.d.cts +10 -0
- package/dist/restricted/manageAccounts.d.cts.map +1 -1
- package/dist/restricted/manageAccounts.d.mts +10 -0
- package/dist/restricted/manageAccounts.d.mts.map +1 -1
- package/dist/restricted/manageAccounts.mjs +10 -0
- package/dist/restricted/manageAccounts.mjs.map +1 -1
- package/dist/restricted/manageState.cjs +42 -0
- package/dist/restricted/manageState.cjs.map +1 -1
- package/dist/restricted/manageState.d.cts +42 -0
- package/dist/restricted/manageState.d.cts.map +1 -1
- package/dist/restricted/manageState.d.mts +42 -0
- package/dist/restricted/manageState.d.mts.map +1 -1
- package/dist/restricted/manageState.mjs +42 -0
- package/dist/restricted/manageState.mjs.map +1 -1
- package/dist/restricted/notify.cjs +74 -0
- package/dist/restricted/notify.cjs.map +1 -1
- package/dist/restricted/notify.d.cts +75 -1
- package/dist/restricted/notify.d.cts.map +1 -1
- package/dist/restricted/notify.d.mts +75 -1
- package/dist/restricted/notify.d.mts.map +1 -1
- package/dist/restricted/notify.mjs +74 -0
- package/dist/restricted/notify.mjs.map +1 -1
- package/dist/utils.cjs +14 -1
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +6 -0
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +6 -0
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +13 -0
- package/dist/utils.mjs.map +1 -1
- package/package.json +12 -8
- package/dist/permitted/experimentalProviderRequest.cjs +0 -152
- package/dist/permitted/experimentalProviderRequest.cjs.map +0 -1
- package/dist/permitted/experimentalProviderRequest.d.cts +0 -39
- package/dist/permitted/experimentalProviderRequest.d.cts.map +0 -1
- package/dist/permitted/experimentalProviderRequest.d.mts +0 -39
- package/dist/permitted/experimentalProviderRequest.d.mts.map +0 -1
- package/dist/permitted/experimentalProviderRequest.mjs +0 -149
- package/dist/permitted/experimentalProviderRequest.mjs.map +0 -1
- package/dist/permitted/getCurrencyRate.cjs +0 -63
- package/dist/permitted/getCurrencyRate.cjs.map +0 -1
- package/dist/permitted/getCurrencyRate.d.cts +0 -20
- package/dist/permitted/getCurrencyRate.d.cts.map +0 -1
- package/dist/permitted/getCurrencyRate.d.mts +0 -20
- package/dist/permitted/getCurrencyRate.d.mts.map +0 -1
- package/dist/permitted/getCurrencyRate.mjs +0 -60
- package/dist/permitted/getCurrencyRate.mjs.map +0 -1
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { UpdateInterfaceParams, UpdateInterfaceResult, ComponentOrElement, InterfaceContext } from "@metamask/snaps-sdk";
|
|
1
|
+
import type { JsonRpcEngineEndCallback } from "@metamask/json-rpc-engine";
|
|
2
|
+
import type { UpdateInterfaceParams, UpdateInterfaceResult, JsonRpcRequest, ComponentOrElement, InterfaceContext } from "@metamask/snaps-sdk";
|
|
3
3
|
import { type InferMatching } from "@metamask/snaps-utils";
|
|
4
|
+
import type { PendingJsonRpcResponse } from "@metamask/utils";
|
|
5
|
+
import type { MethodHooksObject } from "../utils.cjs";
|
|
4
6
|
export type UpdateInterfaceMethodHooks = {
|
|
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;
|
|
5
12
|
/**
|
|
6
13
|
* @param id - The interface ID.
|
|
7
14
|
* @param ui - The UI components.
|
|
@@ -9,7 +16,46 @@ export type UpdateInterfaceMethodHooks = {
|
|
|
9
16
|
*/
|
|
10
17
|
updateInterface: (id: string, ui: ComponentOrElement, context?: InterfaceContext) => void;
|
|
11
18
|
};
|
|
12
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Update an interactive interface. For use in
|
|
21
|
+
* [interactive UI](https://docs.metamask.io/snaps/features/custom-ui/interactive-ui/).
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```tsx
|
|
25
|
+
* import { Box, Heading, Text } from '@metamask/snaps-sdk/jsx';
|
|
26
|
+
*
|
|
27
|
+
* // First, create an interface and get its ID.
|
|
28
|
+
* const id = await snap.request({
|
|
29
|
+
* method: 'snap_createInterface',
|
|
30
|
+
* params: {
|
|
31
|
+
* ui: (
|
|
32
|
+
* <Box>
|
|
33
|
+
* ...
|
|
34
|
+
* </Box>
|
|
35
|
+
* ),
|
|
36
|
+
* },
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* // Later, update the interface with new content using the ID.
|
|
40
|
+
* snap.request({
|
|
41
|
+
* method: 'snap_updateInterface',
|
|
42
|
+
* params: {
|
|
43
|
+
* id: 'interface-id',
|
|
44
|
+
* ui: (
|
|
45
|
+
* <Box>
|
|
46
|
+
* <Heading>Updated Interface</Heading>
|
|
47
|
+
* <Text>This interface has been updated.</Text>
|
|
48
|
+
* </Box>
|
|
49
|
+
* ),
|
|
50
|
+
* },
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare const updateInterfaceHandler: {
|
|
55
|
+
methodNames: ["snap_updateInterface"];
|
|
56
|
+
implementation: typeof getUpdateInterfaceImplementation;
|
|
57
|
+
hookNames: MethodHooksObject<UpdateInterfaceMethodHooks>;
|
|
58
|
+
};
|
|
13
59
|
declare const UpdateInterfaceParametersStruct: import("@metamask/superstruct").Struct<{
|
|
14
60
|
id: string;
|
|
15
61
|
ui: import("@metamask/snaps-sdk").Panel | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AccountSelectorProps, "AccountSelector"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AddressInputProps, "AddressInput"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AssetSelectorProps, "AssetSelector"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ButtonProps, "Button"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CheckboxProps, "Checkbox"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").DateTimePickerProps, "DateTimePicker"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FormProps, "Form"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FieldProps, "Field"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FileInputProps, "FileInput"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").InputProps, "Input"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").DropdownProps, "Dropdown"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SelectorProps, "Selector"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BoldProps, "Bold"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ItalicProps, "Italic"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AvatarProps, "Avatar"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AddressProps, "Address"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BoxProps, "Box"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CardProps, "Card"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ContainerProps, "Container"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CopyableProps, "Copyable"> | import("@metamask/snaps-sdk/jsx").SnapElement<Record<string, never>, "Divider"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").IconProps, "Icon"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").LinkProps, "Link"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").RowProps, "Row"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SectionProps, "Section"> | import("@metamask/snaps-sdk/jsx").SnapElement<Record<string, never>, "Spinner"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").TextProps, "Text"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").TooltipProps, "Tooltip"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BannerProps, "Banner"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SkeletonProps, "Skeleton"> | {
|
|
@@ -192,5 +238,20 @@ declare const UpdateInterfaceParametersStruct: import("@metamask/superstruct").S
|
|
|
192
238
|
context: import("@metamask/superstruct").Struct<Record<string, import("@metamask/snaps-sdk").Json> | undefined, null>;
|
|
193
239
|
}>;
|
|
194
240
|
export type UpdateInterfaceParameters = InferMatching<typeof UpdateInterfaceParametersStruct, UpdateInterfaceParams>;
|
|
241
|
+
/**
|
|
242
|
+
* The `snap_updateInterface` method implementation.
|
|
243
|
+
*
|
|
244
|
+
* @param req - The JSON-RPC request object.
|
|
245
|
+
* @param res - The JSON-RPC response object.
|
|
246
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
247
|
+
* function.
|
|
248
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
249
|
+
* @param hooks - The RPC method hooks.
|
|
250
|
+
* @param hooks.hasPermission - The function to check if the Snap has a given
|
|
251
|
+
* permission.
|
|
252
|
+
* @param hooks.updateInterface - The function to update the interface.
|
|
253
|
+
* @returns Nothing.
|
|
254
|
+
*/
|
|
255
|
+
declare function getUpdateInterfaceImplementation(req: JsonRpcRequest<UpdateInterfaceParameters>, res: PendingJsonRpcResponse<UpdateInterfaceResult>, _next: unknown, end: JsonRpcEngineEndCallback, { hasPermission, updateInterface }: UpdateInterfaceMethodHooks): void;
|
|
195
256
|
export {};
|
|
196
257
|
//# sourceMappingURL=updateInterface.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"updateInterface.d.cts","sourceRoot":"","sources":["../../src/permitted/updateInterface.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"updateInterface.d.cts","sourceRoot":"","sources":["../../src/permitted/updateInterface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,kCAAkC;AAG1E,OAAO,KAAK,EACV,qBAAqB,EACrB,qBAAqB,EACrB,cAAc,EACd,kBAAkB,EAClB,gBAAgB,EACjB,4BAA4B;AAK7B,OAAO,EAAE,KAAK,aAAa,EAAE,8BAA8B;AAQ3D,OAAO,KAAK,EAAE,sBAAsB,EAAE,wBAAwB;AAE9D,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;AAUlD,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;OAGG;IACH,aAAa,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC;IAEnD;;;;OAIG;IACH,eAAe,EAAE,CACf,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,kBAAkB,EACtB,OAAO,CAAC,EAAE,gBAAgB,KACvB,IAAI,CAAC;CACX,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,eAAO,MAAM,sBAAsB;;;;CAQlC,CAAC;AAEF,QAAA,MAAM,+BAA+B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAInC,CAAC;AAEH,MAAM,MAAM,yBAAyB,GAAG,aAAa,CACnD,OAAO,+BAA+B,EACtC,qBAAqB,CACtB,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,iBAAS,gCAAgC,CACvC,GAAG,EAAE,cAAc,CAAC,yBAAyB,CAAC,EAC9C,GAAG,EAAE,sBAAsB,CAAC,qBAAqB,CAAC,EAClD,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,wBAAwB,EAC7B,EAAE,aAAa,EAAE,eAAe,EAAE,EAAE,0BAA0B,GAC7D,IAAI,CAuBN"}
|
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { UpdateInterfaceParams, UpdateInterfaceResult, ComponentOrElement, InterfaceContext } from "@metamask/snaps-sdk";
|
|
1
|
+
import type { JsonRpcEngineEndCallback } from "@metamask/json-rpc-engine";
|
|
2
|
+
import type { UpdateInterfaceParams, UpdateInterfaceResult, JsonRpcRequest, ComponentOrElement, InterfaceContext } from "@metamask/snaps-sdk";
|
|
3
3
|
import { type InferMatching } from "@metamask/snaps-utils";
|
|
4
|
+
import type { PendingJsonRpcResponse } from "@metamask/utils";
|
|
5
|
+
import type { MethodHooksObject } from "../utils.mjs";
|
|
4
6
|
export type UpdateInterfaceMethodHooks = {
|
|
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;
|
|
5
12
|
/**
|
|
6
13
|
* @param id - The interface ID.
|
|
7
14
|
* @param ui - The UI components.
|
|
@@ -9,7 +16,46 @@ export type UpdateInterfaceMethodHooks = {
|
|
|
9
16
|
*/
|
|
10
17
|
updateInterface: (id: string, ui: ComponentOrElement, context?: InterfaceContext) => void;
|
|
11
18
|
};
|
|
12
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Update an interactive interface. For use in
|
|
21
|
+
* [interactive UI](https://docs.metamask.io/snaps/features/custom-ui/interactive-ui/).
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```tsx
|
|
25
|
+
* import { Box, Heading, Text } from '@metamask/snaps-sdk/jsx';
|
|
26
|
+
*
|
|
27
|
+
* // First, create an interface and get its ID.
|
|
28
|
+
* const id = await snap.request({
|
|
29
|
+
* method: 'snap_createInterface',
|
|
30
|
+
* params: {
|
|
31
|
+
* ui: (
|
|
32
|
+
* <Box>
|
|
33
|
+
* ...
|
|
34
|
+
* </Box>
|
|
35
|
+
* ),
|
|
36
|
+
* },
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* // Later, update the interface with new content using the ID.
|
|
40
|
+
* snap.request({
|
|
41
|
+
* method: 'snap_updateInterface',
|
|
42
|
+
* params: {
|
|
43
|
+
* id: 'interface-id',
|
|
44
|
+
* ui: (
|
|
45
|
+
* <Box>
|
|
46
|
+
* <Heading>Updated Interface</Heading>
|
|
47
|
+
* <Text>This interface has been updated.</Text>
|
|
48
|
+
* </Box>
|
|
49
|
+
* ),
|
|
50
|
+
* },
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare const updateInterfaceHandler: {
|
|
55
|
+
methodNames: ["snap_updateInterface"];
|
|
56
|
+
implementation: typeof getUpdateInterfaceImplementation;
|
|
57
|
+
hookNames: MethodHooksObject<UpdateInterfaceMethodHooks>;
|
|
58
|
+
};
|
|
13
59
|
declare const UpdateInterfaceParametersStruct: import("@metamask/superstruct").Struct<{
|
|
14
60
|
id: string;
|
|
15
61
|
ui: import("@metamask/snaps-sdk").Panel | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AccountSelectorProps, "AccountSelector"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AddressInputProps, "AddressInput"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AssetSelectorProps, "AssetSelector"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ButtonProps, "Button"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CheckboxProps, "Checkbox"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").DateTimePickerProps, "DateTimePicker"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FormProps, "Form"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FieldProps, "Field"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FileInputProps, "FileInput"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").InputProps, "Input"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").DropdownProps, "Dropdown"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SelectorProps, "Selector"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BoldProps, "Bold"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ItalicProps, "Italic"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AvatarProps, "Avatar"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AddressProps, "Address"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BoxProps, "Box"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CardProps, "Card"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ContainerProps, "Container"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CopyableProps, "Copyable"> | import("@metamask/snaps-sdk/jsx").SnapElement<Record<string, never>, "Divider"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").IconProps, "Icon"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").LinkProps, "Link"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").RowProps, "Row"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SectionProps, "Section"> | import("@metamask/snaps-sdk/jsx").SnapElement<Record<string, never>, "Spinner"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").TextProps, "Text"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").TooltipProps, "Tooltip"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BannerProps, "Banner"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SkeletonProps, "Skeleton"> | {
|
|
@@ -192,5 +238,20 @@ declare const UpdateInterfaceParametersStruct: import("@metamask/superstruct").S
|
|
|
192
238
|
context: import("@metamask/superstruct").Struct<Record<string, import("@metamask/snaps-sdk").Json> | undefined, null>;
|
|
193
239
|
}>;
|
|
194
240
|
export type UpdateInterfaceParameters = InferMatching<typeof UpdateInterfaceParametersStruct, UpdateInterfaceParams>;
|
|
241
|
+
/**
|
|
242
|
+
* The `snap_updateInterface` method implementation.
|
|
243
|
+
*
|
|
244
|
+
* @param req - The JSON-RPC request object.
|
|
245
|
+
* @param res - The JSON-RPC response object.
|
|
246
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
247
|
+
* function.
|
|
248
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
249
|
+
* @param hooks - The RPC method hooks.
|
|
250
|
+
* @param hooks.hasPermission - The function to check if the Snap has a given
|
|
251
|
+
* permission.
|
|
252
|
+
* @param hooks.updateInterface - The function to update the interface.
|
|
253
|
+
* @returns Nothing.
|
|
254
|
+
*/
|
|
255
|
+
declare function getUpdateInterfaceImplementation(req: JsonRpcRequest<UpdateInterfaceParameters>, res: PendingJsonRpcResponse<UpdateInterfaceResult>, _next: unknown, end: JsonRpcEngineEndCallback, { hasPermission, updateInterface }: UpdateInterfaceMethodHooks): void;
|
|
195
256
|
export {};
|
|
196
257
|
//# sourceMappingURL=updateInterface.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"updateInterface.d.mts","sourceRoot":"","sources":["../../src/permitted/updateInterface.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"updateInterface.d.mts","sourceRoot":"","sources":["../../src/permitted/updateInterface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,kCAAkC;AAG1E,OAAO,KAAK,EACV,qBAAqB,EACrB,qBAAqB,EACrB,cAAc,EACd,kBAAkB,EAClB,gBAAgB,EACjB,4BAA4B;AAK7B,OAAO,EAAE,KAAK,aAAa,EAAE,8BAA8B;AAQ3D,OAAO,KAAK,EAAE,sBAAsB,EAAE,wBAAwB;AAE9D,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;AAUlD,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;OAGG;IACH,aAAa,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC;IAEnD;;;;OAIG;IACH,eAAe,EAAE,CACf,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,kBAAkB,EACtB,OAAO,CAAC,EAAE,gBAAgB,KACvB,IAAI,CAAC;CACX,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,eAAO,MAAM,sBAAsB;;;;CAQlC,CAAC;AAEF,QAAA,MAAM,+BAA+B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAInC,CAAC;AAEH,MAAM,MAAM,yBAAyB,GAAG,aAAa,CACnD,OAAO,+BAA+B,EACtC,qBAAqB,CACtB,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,iBAAS,gCAAgC,CACvC,GAAG,EAAE,cAAc,CAAC,yBAAyB,CAAC,EAC9C,GAAG,EAAE,sBAAsB,CAAC,qBAAqB,CAAC,EAClD,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,wBAAwB,EAC7B,EAAE,aAAa,EAAE,eAAe,EAAE,EAAE,0BAA0B,GAC7D,IAAI,CAuBN"}
|
|
@@ -1,11 +1,49 @@
|
|
|
1
|
-
import { rpcErrors } from "@metamask/rpc-errors";
|
|
1
|
+
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
|
+
import { UI_PERMISSIONS } from "../utils.mjs";
|
|
5
|
+
const methodName = 'snap_updateInterface';
|
|
4
6
|
const hookNames = {
|
|
7
|
+
hasPermission: true,
|
|
5
8
|
updateInterface: true,
|
|
6
9
|
};
|
|
10
|
+
/**
|
|
11
|
+
* Update an interactive interface. For use in
|
|
12
|
+
* [interactive UI](https://docs.metamask.io/snaps/features/custom-ui/interactive-ui/).
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* import { Box, Heading, Text } from '@metamask/snaps-sdk/jsx';
|
|
17
|
+
*
|
|
18
|
+
* // First, create an interface and get its ID.
|
|
19
|
+
* const id = await snap.request({
|
|
20
|
+
* method: 'snap_createInterface',
|
|
21
|
+
* params: {
|
|
22
|
+
* ui: (
|
|
23
|
+
* <Box>
|
|
24
|
+
* ...
|
|
25
|
+
* </Box>
|
|
26
|
+
* ),
|
|
27
|
+
* },
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* // Later, update the interface with new content using the ID.
|
|
31
|
+
* snap.request({
|
|
32
|
+
* method: 'snap_updateInterface',
|
|
33
|
+
* params: {
|
|
34
|
+
* id: 'interface-id',
|
|
35
|
+
* ui: (
|
|
36
|
+
* <Box>
|
|
37
|
+
* <Heading>Updated Interface</Heading>
|
|
38
|
+
* <Text>This interface has been updated.</Text>
|
|
39
|
+
* </Box>
|
|
40
|
+
* ),
|
|
41
|
+
* },
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
7
45
|
export const updateInterfaceHandler = {
|
|
8
|
-
methodNames: [
|
|
46
|
+
methodNames: [methodName],
|
|
9
47
|
implementation: getUpdateInterfaceImplementation,
|
|
10
48
|
hookNames,
|
|
11
49
|
};
|
|
@@ -23,10 +61,17 @@ const UpdateInterfaceParametersStruct = object({
|
|
|
23
61
|
* function.
|
|
24
62
|
* @param end - The `json-rpc-engine` "end" callback.
|
|
25
63
|
* @param hooks - The RPC method hooks.
|
|
64
|
+
* @param hooks.hasPermission - The function to check if the Snap has a given
|
|
65
|
+
* permission.
|
|
26
66
|
* @param hooks.updateInterface - The function to update the interface.
|
|
27
67
|
* @returns Nothing.
|
|
28
68
|
*/
|
|
29
|
-
function getUpdateInterfaceImplementation(req, res, _next, end, { updateInterface }) {
|
|
69
|
+
function getUpdateInterfaceImplementation(req, res, _next, end, { hasPermission, updateInterface }) {
|
|
70
|
+
if (!UI_PERMISSIONS.some(hasPermission)) {
|
|
71
|
+
return end(providerErrors.unauthorized({
|
|
72
|
+
message: `This method can only be used if the Snap has one of the following permissions: ${UI_PERMISSIONS.join(', ')}.`,
|
|
73
|
+
}));
|
|
74
|
+
}
|
|
30
75
|
const { params } = req;
|
|
31
76
|
try {
|
|
32
77
|
const validatedParams = getValidatedParams(params);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"updateInterface.mjs","sourceRoot":"","sources":["../../src/permitted/updateInterface.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,6BAA6B;
|
|
1
|
+
{"version":3,"file":"updateInterface.mjs","sourceRoot":"","sources":["../../src/permitted/updateInterface.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,6BAA6B;AAQjE,OAAO,EACL,wBAAwB,EACxB,sBAAsB,EACvB,4BAA4B;AAE7B,OAAO,EACL,WAAW,EACX,MAAM,EACN,MAAM,EACN,QAAQ,EACR,MAAM,EACP,8BAA8B;AAI/B,OAAO,EAAE,cAAc,EAAE,qBAAiB;AAE1C,MAAM,UAAU,GAAG,sBAAsB,CAAC;AAE1C,MAAM,SAAS,GAAkD;IAC/D,aAAa,EAAE,IAAI;IACnB,eAAe,EAAE,IAAI;CACtB,CAAC;AAqBF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG;IACpC,WAAW,EAAE,CAAC,UAAU,CAAU;IAClC,cAAc,EAAE,gCAAgC;IAChD,SAAS;CAKV,CAAC;AAEF,MAAM,+BAA+B,GAAG,MAAM,CAAC;IAC7C,EAAE,EAAE,MAAM,EAAE;IACZ,EAAE,EAAE,wBAAwB;IAC5B,OAAO,EAAE,QAAQ,CAAC,sBAAsB,CAAC;CAC1C,CAAC,CAAC;AAOH;;;;;;;;;;;;;GAaG;AACH,SAAS,gCAAgC,CACvC,GAA8C,EAC9C,GAAkD,EAClD,KAAc,EACd,GAA6B,EAC7B,EAAE,aAAa,EAAE,eAAe,EAA8B;IAE9D,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,aAAa,CAAC,EAAE,CAAC;QACxC,OAAO,GAAG,CACR,cAAc,CAAC,YAAY,CAAC;YAC1B,OAAO,EAAE,kFAAkF,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG;SACxH,CAAC,CACH,CAAC;IACJ,CAAC;IAED,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IAEvB,IAAI,CAAC;QACH,MAAM,eAAe,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;QAEnD,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,eAAe,CAAC;QAE5C,eAAe,CAAC,EAAE,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;QACjC,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC;IACpB,CAAC;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 { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type {\n UpdateInterfaceParams,\n UpdateInterfaceResult,\n JsonRpcRequest,\n ComponentOrElement,\n InterfaceContext,\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 { MethodHooksObject } from '../utils';\nimport { UI_PERMISSIONS } from '../utils';\n\nconst methodName = 'snap_updateInterface';\n\nconst hookNames: MethodHooksObject<UpdateInterfaceMethodHooks> = {\n hasPermission: true,\n updateInterface: true,\n};\n\nexport type UpdateInterfaceMethodHooks = {\n /**\n * @param permissionName - The name of the permission to check.\n * @returns Whether the Snap has the permission.\n */\n hasPermission: (permissionName: string) => boolean;\n\n /**\n * @param id - The interface ID.\n * @param ui - The UI components.\n * @param context - The optional interface context object.\n */\n updateInterface: (\n id: string,\n ui: ComponentOrElement,\n context?: InterfaceContext,\n ) => void;\n};\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 methodNames: [methodName] as const,\n implementation: getUpdateInterfaceImplementation,\n hookNames,\n} satisfies PermittedHandlerExport<\n UpdateInterfaceMethodHooks,\n UpdateInterfaceParameters,\n UpdateInterfaceResult\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.\n * @param hooks.hasPermission - The function to check if the Snap has a given\n * permission.\n * @param hooks.updateInterface - The function to update the interface.\n * @returns Nothing.\n */\nfunction getUpdateInterfaceImplementation(\n req: JsonRpcRequest<UpdateInterfaceParameters>,\n res: PendingJsonRpcResponse<UpdateInterfaceResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { hasPermission, updateInterface }: UpdateInterfaceMethodHooks,\n): void {\n if (!UI_PERMISSIONS.some(hasPermission)) {\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 const { params } = req;\n\n try {\n const validatedParams = getValidatedParams(params);\n\n const { id, ui, context } = validatedParams;\n\n updateInterface(id, ui, context);\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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snapIds.cjs","sourceRoot":"","sources":["../../../src/restricted/caveats/snapIds.ts"],"names":[],"mappings":";;;AAOA,qDAAiD;
|
|
1
|
+
{"version":3,"file":"snapIds.cjs","sourceRoot":"","sources":["../../../src/restricted/caveats/snapIds.ts"],"names":[],"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cesContent":["import type {\n Caveat,\n RestrictedMethodOptions,\n RestrictedMethodParameters,\n RestrictedMethodCaveatSpecificationConstraint,\n PermissionConstraint,\n} from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { InvokeSnapParams } from '@metamask/snaps-sdk';\nimport type { SnapIds } from '@metamask/snaps-utils';\nimport { SnapCaveatType, SnapIdsStruct } from '@metamask/snaps-utils';\nimport { type } from '@metamask/superstruct';\nimport type { Json } from '@metamask/utils';\nimport { hasProperty, assertStruct } from '@metamask/utils';\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function snapIdsCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n return {\n caveats: [\n {\n type: SnapCaveatType.SnapIds,\n value,\n },\n ],\n };\n}\n\n/**\n * Validates that the caveat value exists and is a non-empty object.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat is invalid.\n */\nexport function validateSnapIdsCaveat(\n caveat: Caveat<string, any>,\n): asserts caveat is Caveat<string, SnapIds> {\n assertStruct(\n caveat,\n type({\n value: SnapIdsStruct,\n }),\n 'Expected caveat to have a value property of a non-empty object of snap IDs.',\n rpcErrors.invalidParams,\n );\n}\n\nexport const SnapIdsCaveatSpecification: Record<\n SnapCaveatType.SnapIds,\n RestrictedMethodCaveatSpecificationConstraint\n> = {\n [SnapCaveatType.SnapIds]: Object.freeze({\n type: SnapCaveatType.SnapIds,\n validator: (caveat) => validateSnapIdsCaveat(caveat),\n decorator: (method, caveat) => {\n return async (args) => {\n const {\n params,\n context: { origin },\n }: RestrictedMethodOptions<RestrictedMethodParameters> = args;\n const snapIds = caveat.value as SnapIds;\n const { snapId } = params as InvokeSnapParams;\n if (!hasProperty(snapIds, snapId)) {\n throw new Error(\n `${origin} does not have permission to invoke ${snapId} snap.`,\n );\n }\n return await method(args);\n };\n },\n }),\n};\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snapIds.d.cts","sourceRoot":"","sources":["../../../src/restricted/caveats/snapIds.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,MAAM,EAGN,6CAA6C,EAC7C,oBAAoB,EACrB,wCAAwC;
|
|
1
|
+
{"version":3,"file":"snapIds.d.cts","sourceRoot":"","sources":["../../../src/restricted/caveats/snapIds.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,MAAM,EAGN,6CAA6C,EAC7C,oBAAoB,EACrB,wCAAwC;AAGzC,OAAO,KAAK,EAAE,OAAO,EAAE,8BAA8B;AACrD,OAAO,EAAE,cAAc,EAAiB,8BAA8B;AAEtE,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAG5C;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,IAAI,GACV,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC,CASvC;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAC1B,OAAO,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAS3C;AAED,eAAO,MAAM,0BAA0B,EAAE,MAAM,CAC7C,cAAc,CAAC,OAAO,EACtB,6CAA6C,CAsB9C,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snapIds.d.mts","sourceRoot":"","sources":["../../../src/restricted/caveats/snapIds.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,MAAM,EAGN,6CAA6C,EAC7C,oBAAoB,EACrB,wCAAwC;
|
|
1
|
+
{"version":3,"file":"snapIds.d.mts","sourceRoot":"","sources":["../../../src/restricted/caveats/snapIds.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,MAAM,EAGN,6CAA6C,EAC7C,oBAAoB,EACrB,wCAAwC;AAGzC,OAAO,KAAK,EAAE,OAAO,EAAE,8BAA8B;AACrD,OAAO,EAAE,cAAc,EAAiB,8BAA8B;AAEtE,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAG5C;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,IAAI,GACV,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC,CASvC;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAC1B,OAAO,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAS3C;AAED,eAAO,MAAM,0BAA0B,EAAE,MAAM,CAC7C,cAAc,CAAC,OAAO,EACtB,6CAA6C,CAsB9C,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snapIds.mjs","sourceRoot":"","sources":["../../../src/restricted/caveats/snapIds.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,SAAS,EAAE,6BAA6B;
|
|
1
|
+
{"version":3,"file":"snapIds.mjs","sourceRoot":"","sources":["../../../src/restricted/caveats/snapIds.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAGjD,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,8BAA8B;AACtE,OAAO,EAAE,IAAI,EAAE,8BAA8B;AAE7C,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,wBAAwB;AAE5D;;;;;;;GAOG;AACH,MAAM,UAAU,mBAAmB,CACjC,KAAW;IAEX,OAAO;QACL,OAAO,EAAE;YACP;gBACE,IAAI,EAAE,cAAc,CAAC,OAAO;gBAC5B,KAAK;aACN;SACF;KACF,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB,CACnC,MAA2B;IAE3B,YAAY,CACV,MAAM,EACN,IAAI,CAAC;QACH,KAAK,EAAE,aAAa;KACrB,CAAC,EACF,6EAA6E,EAC7E,SAAS,CAAC,aAAa,CACxB,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,MAAM,0BAA0B,GAGnC;IACF,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QACtC,IAAI,EAAE,cAAc,CAAC,OAAO;QAC5B,SAAS,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,qBAAqB,CAAC,MAAM,CAAC;QACpD,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE;YAC5B,OAAO,KAAK,EAAE,IAAI,EAAE,EAAE;gBACpB,MAAM,EACJ,MAAM,EACN,OAAO,EAAE,EAAE,MAAM,EAAE,GACpB,GAAwD,IAAI,CAAC;gBAC9D,MAAM,OAAO,GAAG,MAAM,CAAC,KAAgB,CAAC;gBACxC,MAAM,EAAE,MAAM,EAAE,GAAG,MAA0B,CAAC;gBAC9C,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE,CAAC;oBAClC,MAAM,IAAI,KAAK,CACb,GAAG,MAAM,uCAAuC,MAAM,QAAQ,CAC/D,CAAC;gBACJ,CAAC;gBACD,OAAO,MAAM,MAAM,CAAC,IAAI,CAAC,CAAC;YAC5B,CAAC,CAAC;QACJ,CAAC;KACF,CAAC;CACH,CAAC","sourcesContent":["import type {\n Caveat,\n RestrictedMethodOptions,\n RestrictedMethodParameters,\n RestrictedMethodCaveatSpecificationConstraint,\n PermissionConstraint,\n} from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { InvokeSnapParams } from '@metamask/snaps-sdk';\nimport type { SnapIds } from '@metamask/snaps-utils';\nimport { SnapCaveatType, SnapIdsStruct } from '@metamask/snaps-utils';\nimport { type } from '@metamask/superstruct';\nimport type { Json } from '@metamask/utils';\nimport { hasProperty, assertStruct } from '@metamask/utils';\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function snapIdsCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n return {\n caveats: [\n {\n type: SnapCaveatType.SnapIds,\n value,\n },\n ],\n };\n}\n\n/**\n * Validates that the caveat value exists and is a non-empty object.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat is invalid.\n */\nexport function validateSnapIdsCaveat(\n caveat: Caveat<string, any>,\n): asserts caveat is Caveat<string, SnapIds> {\n assertStruct(\n caveat,\n type({\n value: SnapIdsStruct,\n }),\n 'Expected caveat to have a value property of a non-empty object of snap IDs.',\n rpcErrors.invalidParams,\n );\n}\n\nexport const SnapIdsCaveatSpecification: Record<\n SnapCaveatType.SnapIds,\n RestrictedMethodCaveatSpecificationConstraint\n> = {\n [SnapCaveatType.SnapIds]: Object.freeze({\n type: SnapCaveatType.SnapIds,\n validator: (caveat) => validateSnapIdsCaveat(caveat),\n decorator: (method, caveat) => {\n return async (args) => {\n const {\n params,\n context: { origin },\n }: RestrictedMethodOptions<RestrictedMethodParameters> = args;\n const snapIds = caveat.value as SnapIds;\n const { snapId } = params as InvokeSnapParams;\n if (!hasProperty(snapIds, snapId)) {\n throw new Error(\n `${origin} does not have permission to invoke ${snapId} snap.`,\n );\n }\n return await method(args);\n };\n },\n }),\n};\n"]}
|
|
@@ -41,12 +41,56 @@ const methodHooks = {
|
|
|
41
41
|
requestUserApproval: true,
|
|
42
42
|
createInterface: true,
|
|
43
43
|
getInterface: true,
|
|
44
|
+
setInterfaceDisplayed: true,
|
|
44
45
|
};
|
|
46
|
+
/* eslint-disable jsdoc/check-indentation */
|
|
47
|
+
/**
|
|
48
|
+
* Display a [dialog](https://docs.metamask.io/snaps/features/custom-ui/dialogs/)
|
|
49
|
+
* in the MetaMask UI.
|
|
50
|
+
*
|
|
51
|
+
* - `type` - The type of dialog. Not providing a type will create a fully
|
|
52
|
+
* [custom dialog](https://docs.metamask.io/snaps/features/custom-ui/dialogs/#display-a-custom-dialog).
|
|
53
|
+
* Possible values are:
|
|
54
|
+
* - `alert` - An alert that can only be acknowledged.
|
|
55
|
+
* - `confirmation` - A confirmation that can be accepted or rejected.
|
|
56
|
+
* - `prompt` - A prompt where the user can enter a text response.
|
|
57
|
+
*
|
|
58
|
+
* - One of:
|
|
59
|
+
* - `content` - The content of the alert, as a
|
|
60
|
+
* [custom UI](https://docs.metamask.io/snaps/features/custom-ui/) component.
|
|
61
|
+
* - `id` - The ID of an
|
|
62
|
+
* [interactive interface](https://docs.metamask.io/snaps/reference/snaps-api/snap_createinterface).
|
|
63
|
+
* - `placeholder` - An optional placeholder text to display in the dialog. Only
|
|
64
|
+
* applicable for the `prompt` dialog.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* import { Box, Heading, Text } from '@metamask/snaps-sdk/jsx';
|
|
69
|
+
*
|
|
70
|
+
* const walletAddress = await snap.request({
|
|
71
|
+
* method: 'snap_dialog',
|
|
72
|
+
* params: {
|
|
73
|
+
* type: 'prompt',
|
|
74
|
+
* content: (
|
|
75
|
+
* <Box>
|
|
76
|
+
* <Heading>What is the wallet address?</Heading>
|
|
77
|
+
* <Text>Please enter the wallet address to be monitored.</Text>
|
|
78
|
+
* </Box>
|
|
79
|
+
* ),
|
|
80
|
+
* placeholder: '0x123...',
|
|
81
|
+
* },
|
|
82
|
+
* });
|
|
83
|
+
*
|
|
84
|
+
* // `walletAddress` will be a string containing the address entered by the
|
|
85
|
+
* // user.
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
45
88
|
exports.dialogBuilder = Object.freeze({
|
|
46
89
|
targetName: methodName,
|
|
47
90
|
specificationBuilder,
|
|
48
91
|
methodHooks,
|
|
49
92
|
});
|
|
93
|
+
/* eslint-enable jsdoc/check-indentation */
|
|
50
94
|
const AlertParametersWithContentStruct = (0, superstruct_1.object)({
|
|
51
95
|
type: (0, snaps_sdk_1.enumValue)(snaps_sdk_1.DialogType.Alert),
|
|
52
96
|
content: snaps_sdk_1.ComponentOrElementStruct,
|
|
@@ -127,10 +171,12 @@ const DialogParametersStruct = (0, snaps_sdk_1.selectiveUnion)((value) => {
|
|
|
127
171
|
* This function should return a Promise that resolves with the appropriate value when the user has approved or rejected the request.
|
|
128
172
|
* @param hooks.createInterface - A function that creates the interface in SnapInterfaceController.
|
|
129
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.
|
|
130
176
|
* @returns The method implementation which return value depends on the dialog
|
|
131
177
|
* type, valid return types are: string, boolean, null.
|
|
132
178
|
*/
|
|
133
|
-
function getDialogImplementation({ requestUserApproval, createInterface, getInterface, }) {
|
|
179
|
+
function getDialogImplementation({ requestUserApproval, createInterface, getInterface, setInterfaceDisplayed, }) {
|
|
134
180
|
return async function dialogImplementation(args) {
|
|
135
181
|
const { params, context: { origin }, } = args;
|
|
136
182
|
if (!(0, utils_1.isObject)(params)) {
|
|
@@ -148,6 +194,7 @@ function getDialogImplementation({ requestUserApproval, createInterface, getInte
|
|
|
148
194
|
const approvalType = exports.DIALOG_APPROVAL_TYPES[validatedType];
|
|
149
195
|
if ((0, utils_1.hasProperty)(validatedParams, 'content')) {
|
|
150
196
|
const id = await createInterface(origin, validatedParams.content);
|
|
197
|
+
setInterfaceDisplayed(origin, id);
|
|
151
198
|
return requestUserApproval({
|
|
152
199
|
id: approvalType === exports.DIALOG_APPROVAL_TYPES.default ? id : undefined,
|
|
153
200
|
origin,
|
|
@@ -156,6 +203,7 @@ function getDialogImplementation({ requestUserApproval, createInterface, getInte
|
|
|
156
203
|
});
|
|
157
204
|
}
|
|
158
205
|
validateInterface(origin, validatedParams.id, getInterface);
|
|
206
|
+
setInterfaceDisplayed(origin, validatedParams.id);
|
|
159
207
|
return requestUserApproval({
|
|
160
208
|
id: approvalType === exports.DIALOG_APPROVAL_TYPES.default
|
|
161
209
|
? validatedParams.id
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dialog.cjs","sourceRoot":"","sources":["../../src/restricted/dialog.ts"],"names":[],"mappings":";;;AAKA,2EAA8E;AAC9E,qDAAiD;AACjD,mDAK6B;AAa7B,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;AAkE1D;;;;;;;;;;;;;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,IAAI;CACnB,CAAC;AAEW,QAAA,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC;IACzC,UAAU,EAAE,UAAU;IACtB,oBAAoB;IACpB,WAAW;CACH,CAAC,CAAC;AAEZ,MAAM,gCAAgC,GAAG,IAAA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AK,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} 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\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};\n\nexport const dialogBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n methodHooks,\n} as const);\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 * @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}: DialogMethodHooks) {\n return async function dialogImplementation(\n args: RestrictedMethodOptions<DialogParameters>,\n ): Promise<boolean | null | string | Json> {\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 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\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.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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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 }\n}\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { PermissionSpecificationBuilder, RestrictedMethodOptions } from "@metamask/permission-controller";
|
|
2
2
|
import { PermissionType } from "@metamask/permission-controller";
|
|
3
3
|
import { DialogType } from "@metamask/snaps-sdk";
|
|
4
|
-
import type { DialogParams, InterfaceState, SnapId, ComponentOrElement, InterfaceContext, ContentType } from "@metamask/snaps-sdk";
|
|
4
|
+
import type { DialogParams, InterfaceState, SnapId, ComponentOrElement, InterfaceContext, ContentType, DialogResult } from "@metamask/snaps-sdk";
|
|
5
5
|
import type { InferMatching } from "@metamask/snaps-utils";
|
|
6
6
|
import type { Infer } from "@metamask/superstruct";
|
|
7
7
|
import type { Json, NonEmptyArray } from "@metamask/utils";
|
|
@@ -55,11 +55,59 @@ export type DialogMethodHooks = {
|
|
|
55
55
|
* @param id - The interface ID.
|
|
56
56
|
*/
|
|
57
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;
|
|
58
65
|
};
|
|
59
66
|
type DialogSpecificationBuilderOptions = {
|
|
60
67
|
allowedCaveats?: Readonly<NonEmptyArray<string>> | null;
|
|
61
68
|
methodHooks: DialogMethodHooks;
|
|
62
69
|
};
|
|
70
|
+
/**
|
|
71
|
+
* Display a [dialog](https://docs.metamask.io/snaps/features/custom-ui/dialogs/)
|
|
72
|
+
* in the MetaMask UI.
|
|
73
|
+
*
|
|
74
|
+
* - `type` - The type of dialog. Not providing a type will create a fully
|
|
75
|
+
* [custom dialog](https://docs.metamask.io/snaps/features/custom-ui/dialogs/#display-a-custom-dialog).
|
|
76
|
+
* Possible values are:
|
|
77
|
+
* - `alert` - An alert that can only be acknowledged.
|
|
78
|
+
* - `confirmation` - A confirmation that can be accepted or rejected.
|
|
79
|
+
* - `prompt` - A prompt where the user can enter a text response.
|
|
80
|
+
*
|
|
81
|
+
* - One of:
|
|
82
|
+
* - `content` - The content of the alert, as a
|
|
83
|
+
* [custom UI](https://docs.metamask.io/snaps/features/custom-ui/) component.
|
|
84
|
+
* - `id` - The ID of an
|
|
85
|
+
* [interactive interface](https://docs.metamask.io/snaps/reference/snaps-api/snap_createinterface).
|
|
86
|
+
* - `placeholder` - An optional placeholder text to display in the dialog. Only
|
|
87
|
+
* applicable for the `prompt` dialog.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* import { Box, Heading, Text } from '@metamask/snaps-sdk/jsx';
|
|
92
|
+
*
|
|
93
|
+
* const walletAddress = await snap.request({
|
|
94
|
+
* method: 'snap_dialog',
|
|
95
|
+
* params: {
|
|
96
|
+
* type: 'prompt',
|
|
97
|
+
* content: (
|
|
98
|
+
* <Box>
|
|
99
|
+
* <Heading>What is the wallet address?</Heading>
|
|
100
|
+
* <Text>Please enter the wallet address to be monitored.</Text>
|
|
101
|
+
* </Box>
|
|
102
|
+
* ),
|
|
103
|
+
* placeholder: '0x123...',
|
|
104
|
+
* },
|
|
105
|
+
* });
|
|
106
|
+
*
|
|
107
|
+
* // `walletAddress` will be a string containing the address entered by the
|
|
108
|
+
* // user.
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
63
111
|
export declare const dialogBuilder: Readonly<{
|
|
64
112
|
readonly targetName: "snap_dialog";
|
|
65
113
|
readonly specificationBuilder: PermissionSpecificationBuilder<PermissionType.RestrictedMethod, DialogSpecificationBuilderOptions, {
|
|
@@ -448,9 +496,11 @@ export type DialogParameters = InferMatching<typeof DialogParametersStruct, Dial
|
|
|
448
496
|
* This function should return a Promise that resolves with the appropriate value when the user has approved or rejected the request.
|
|
449
497
|
* @param hooks.createInterface - A function that creates the interface in SnapInterfaceController.
|
|
450
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
501
|
* @returns The method implementation which return value depends on the dialog
|
|
452
502
|
* type, valid return types are: string, boolean, null.
|
|
453
503
|
*/
|
|
454
|
-
export declare function getDialogImplementation({ requestUserApproval, createInterface, getInterface, }: DialogMethodHooks): (args: RestrictedMethodOptions<DialogParameters>) => Promise<
|
|
504
|
+
export declare function getDialogImplementation({ requestUserApproval, createInterface, getInterface, setInterfaceDisplayed, }: DialogMethodHooks): (args: RestrictedMethodOptions<DialogParameters>) => Promise<DialogResult>;
|
|
455
505
|
export {};
|
|
456
506
|
//# sourceMappingURL=dialog.d.cts.map
|