@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,10 +1,39 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { JsonRpcEngineEndCallback } from "@metamask/json-rpc-engine";
|
|
2
|
+
import type { PermissionConstraint, RequestedPermissions } from "@metamask/permission-controller";
|
|
2
3
|
import type { RequestSnapsParams, RequestSnapsResult } from "@metamask/snaps-sdk";
|
|
3
4
|
import type { SnapsPermissionRequest } from "@metamask/snaps-utils";
|
|
5
|
+
import type { JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
|
|
6
|
+
import type { MethodHooksObject } from "../utils.cjs";
|
|
4
7
|
/**
|
|
5
|
-
*
|
|
8
|
+
* Request permission for a dapp to communicate with the specified Snaps and
|
|
9
|
+
* attempt to install them if they're not already installed.
|
|
10
|
+
*
|
|
11
|
+
* If the Snap version range is specified, MetaMask attempts to install a
|
|
12
|
+
* version of the Snap that satisfies the range. If a compatible version of the
|
|
13
|
+
* Snap is already installed, the request succeeds. If an incompatible version
|
|
14
|
+
* is installed, MetaMask attempts to update the Snap to the latest version that
|
|
15
|
+
* satisfies the range. The request succeeds if the Snap is successfully
|
|
16
|
+
* installed.
|
|
17
|
+
*
|
|
18
|
+
* If the installation of any Snap fails, or the user rejects the installation
|
|
19
|
+
* or permission request, this method returns the error that caused the failure.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const result = await snap.request({
|
|
24
|
+
* method: 'wallet_requestSnaps',
|
|
25
|
+
* params: {
|
|
26
|
+
* 'npm:@metamask/example-snap': {},
|
|
27
|
+
* 'npm:@metamask/another-snap': { version: '1.2.3' },
|
|
28
|
+
* },
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
6
31
|
*/
|
|
7
|
-
export declare const requestSnapsHandler:
|
|
32
|
+
export declare const requestSnapsHandler: {
|
|
33
|
+
methodNames: ["wallet_requestSnaps"];
|
|
34
|
+
implementation: typeof requestSnapsImplementation;
|
|
35
|
+
hookNames: MethodHooksObject<RequestSnapsHooks>;
|
|
36
|
+
};
|
|
8
37
|
export type RequestSnapsHooks = {
|
|
9
38
|
/**
|
|
10
39
|
* Installs the requested snaps if they are permitted.
|
|
@@ -48,4 +77,23 @@ export declare function hasRequestedSnaps(existingPermissions: Record<string, Pe
|
|
|
48
77
|
* @returns `requestedPermissions`.
|
|
49
78
|
*/
|
|
50
79
|
export declare function getSnapPermissionsRequest(existingPermissions: Record<string, PermissionConstraint>, requestedPermissions: unknown): SnapsPermissionRequest;
|
|
80
|
+
/**
|
|
81
|
+
* The `wallet_requestSnaps` method implementation.
|
|
82
|
+
* Tries to install the requested snaps and adds them to the JSON-RPC response.
|
|
83
|
+
*
|
|
84
|
+
* @param req - The JSON-RPC request object.
|
|
85
|
+
* @param res - The JSON-RPC response object.
|
|
86
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
87
|
+
* function.
|
|
88
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
89
|
+
* @param hooks - The RPC method hooks.
|
|
90
|
+
* @param hooks.installSnaps - A function that tries to install a given snap, prompting the user if necessary.
|
|
91
|
+
* @param hooks.requestPermissions - A function that requests permissions on
|
|
92
|
+
* behalf of a subject.
|
|
93
|
+
* @param hooks.getPermissions - A function that gets the current permissions.
|
|
94
|
+
* @returns A promise that resolves once the JSON-RPC response has been modified.
|
|
95
|
+
* @throws If the params are invalid.
|
|
96
|
+
*/
|
|
97
|
+
declare function requestSnapsImplementation(req: JsonRpcRequest<RequestSnapsParams>, res: PendingJsonRpcResponse<RequestSnapsResult>, _next: unknown, end: JsonRpcEngineEndCallback, { installSnaps, requestPermissions, getPermissions }: RequestSnapsHooks): Promise<void>;
|
|
98
|
+
export {};
|
|
51
99
|
//# sourceMappingURL=requestSnaps.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"requestSnaps.d.cts","sourceRoot":"","sources":["../../src/permitted/requestSnaps.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"requestSnaps.d.cts","sourceRoot":"","sources":["../../src/permitted/requestSnaps.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,kCAAkC;AAC1E,OAAO,KAAK,EACV,oBAAoB,EACpB,oBAAoB,EAGrB,wCAAwC;AAEzC,OAAO,KAAK,EACV,kBAAkB,EAClB,kBAAkB,EACnB,4BAA4B;AAC7B,OAAO,KAAK,EAAE,sBAAsB,EAAE,8BAA8B;AAKpE,OAAO,KAAK,EACV,cAAc,EACd,sBAAsB,EAEvB,wBAAwB;AAKzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;AAUlD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,mBAAmB;;;;CAQ/B,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B;;OAEG;IACH,YAAY,EAAE,CACZ,cAAc,EAAE,kBAAkB,KAC/B,OAAO,CAAC,kBAAkB,CAAC,CAAC;IAEjC;;;;OAIG;IACH,kBAAkB,EAAE,CAClB,WAAW,EAAE,oBAAoB,KAC9B,OAAO,CACV;QACE,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC;QACpC;YAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;YAAC,EAAE,EAAE,MAAM,CAAC;YAAC,MAAM,EAAE,MAAM,CAAA;SAAE;KAC9D,CACF,CAAC;IAEF;;;;OAIG;IACH,cAAc,EAAE,MAAM,OAAO,CAC3B,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,GAAG,SAAS,CACjD,CAAC;CACH,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAC/B,mBAAmB,EAAE,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,EACzD,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACtC,OAAO,CAcT;AAED;;;;;;;GAOG;AACH,wBAAgB,yBAAyB,CACvC,mBAAmB,EAAE,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,EACzD,oBAAoB,EAAE,OAAO,GAC5B,sBAAsB,CAmCxB;AAiBD;;;;;;;;;;;;;;;;GAgBG;AACH,iBAAe,0BAA0B,CACvC,GAAG,EAAE,cAAc,CAAC,kBAAkB,CAAC,EACvC,GAAG,EAAE,sBAAsB,CAAC,kBAAkB,CAAC,EAC/C,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,wBAAwB,EAC7B,EAAE,YAAY,EAAE,kBAAkB,EAAE,cAAc,EAAE,EAAE,iBAAiB,GACtE,OAAO,CAAC,IAAI,CAAC,CA0Df"}
|
|
@@ -1,10 +1,39 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { JsonRpcEngineEndCallback } from "@metamask/json-rpc-engine";
|
|
2
|
+
import type { PermissionConstraint, RequestedPermissions } from "@metamask/permission-controller";
|
|
2
3
|
import type { RequestSnapsParams, RequestSnapsResult } from "@metamask/snaps-sdk";
|
|
3
4
|
import type { SnapsPermissionRequest } from "@metamask/snaps-utils";
|
|
5
|
+
import type { JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
|
|
6
|
+
import type { MethodHooksObject } from "../utils.mjs";
|
|
4
7
|
/**
|
|
5
|
-
*
|
|
8
|
+
* Request permission for a dapp to communicate with the specified Snaps and
|
|
9
|
+
* attempt to install them if they're not already installed.
|
|
10
|
+
*
|
|
11
|
+
* If the Snap version range is specified, MetaMask attempts to install a
|
|
12
|
+
* version of the Snap that satisfies the range. If a compatible version of the
|
|
13
|
+
* Snap is already installed, the request succeeds. If an incompatible version
|
|
14
|
+
* is installed, MetaMask attempts to update the Snap to the latest version that
|
|
15
|
+
* satisfies the range. The request succeeds if the Snap is successfully
|
|
16
|
+
* installed.
|
|
17
|
+
*
|
|
18
|
+
* If the installation of any Snap fails, or the user rejects the installation
|
|
19
|
+
* or permission request, this method returns the error that caused the failure.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const result = await snap.request({
|
|
24
|
+
* method: 'wallet_requestSnaps',
|
|
25
|
+
* params: {
|
|
26
|
+
* 'npm:@metamask/example-snap': {},
|
|
27
|
+
* 'npm:@metamask/another-snap': { version: '1.2.3' },
|
|
28
|
+
* },
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
6
31
|
*/
|
|
7
|
-
export declare const requestSnapsHandler:
|
|
32
|
+
export declare const requestSnapsHandler: {
|
|
33
|
+
methodNames: ["wallet_requestSnaps"];
|
|
34
|
+
implementation: typeof requestSnapsImplementation;
|
|
35
|
+
hookNames: MethodHooksObject<RequestSnapsHooks>;
|
|
36
|
+
};
|
|
8
37
|
export type RequestSnapsHooks = {
|
|
9
38
|
/**
|
|
10
39
|
* Installs the requested snaps if they are permitted.
|
|
@@ -48,4 +77,23 @@ export declare function hasRequestedSnaps(existingPermissions: Record<string, Pe
|
|
|
48
77
|
* @returns `requestedPermissions`.
|
|
49
78
|
*/
|
|
50
79
|
export declare function getSnapPermissionsRequest(existingPermissions: Record<string, PermissionConstraint>, requestedPermissions: unknown): SnapsPermissionRequest;
|
|
80
|
+
/**
|
|
81
|
+
* The `wallet_requestSnaps` method implementation.
|
|
82
|
+
* Tries to install the requested snaps and adds them to the JSON-RPC response.
|
|
83
|
+
*
|
|
84
|
+
* @param req - The JSON-RPC request object.
|
|
85
|
+
* @param res - The JSON-RPC response object.
|
|
86
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
87
|
+
* function.
|
|
88
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
89
|
+
* @param hooks - The RPC method hooks.
|
|
90
|
+
* @param hooks.installSnaps - A function that tries to install a given snap, prompting the user if necessary.
|
|
91
|
+
* @param hooks.requestPermissions - A function that requests permissions on
|
|
92
|
+
* behalf of a subject.
|
|
93
|
+
* @param hooks.getPermissions - A function that gets the current permissions.
|
|
94
|
+
* @returns A promise that resolves once the JSON-RPC response has been modified.
|
|
95
|
+
* @throws If the params are invalid.
|
|
96
|
+
*/
|
|
97
|
+
declare function requestSnapsImplementation(req: JsonRpcRequest<RequestSnapsParams>, res: PendingJsonRpcResponse<RequestSnapsResult>, _next: unknown, end: JsonRpcEngineEndCallback, { installSnaps, requestPermissions, getPermissions }: RequestSnapsHooks): Promise<void>;
|
|
98
|
+
export {};
|
|
51
99
|
//# sourceMappingURL=requestSnaps.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"requestSnaps.d.mts","sourceRoot":"","sources":["../../src/permitted/requestSnaps.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"requestSnaps.d.mts","sourceRoot":"","sources":["../../src/permitted/requestSnaps.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,kCAAkC;AAC1E,OAAO,KAAK,EACV,oBAAoB,EACpB,oBAAoB,EAGrB,wCAAwC;AAEzC,OAAO,KAAK,EACV,kBAAkB,EAClB,kBAAkB,EACnB,4BAA4B;AAC7B,OAAO,KAAK,EAAE,sBAAsB,EAAE,8BAA8B;AAKpE,OAAO,KAAK,EACV,cAAc,EACd,sBAAsB,EAEvB,wBAAwB;AAKzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;AAUlD;;;;;;;;;;;;;;;;;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@@ import { SnapCaveatType, verifyRequestedSnapPermissions } from "@metamask/snaps-
|
|
|
3
3
|
import { hasProperty, isObject } from "@metamask/utils";
|
|
4
4
|
import { Mutex } from "async-mutex";
|
|
5
5
|
import { WALLET_SNAP_PERMISSION_KEY } from "../restricted/invokeSnap.mjs";
|
|
6
|
+
const methodName = 'wallet_requestSnaps';
|
|
6
7
|
const hookNames = {
|
|
7
8
|
installSnaps: true,
|
|
8
9
|
requestPermissions: true,
|
|
9
10
|
getPermissions: true,
|
|
10
11
|
};
|
|
11
12
|
/**
|
|
12
|
-
*
|
|
13
|
+
* Request permission for a dapp to communicate with the specified Snaps and
|
|
14
|
+
* attempt to install them if they're not already installed.
|
|
15
|
+
*
|
|
16
|
+
* If the Snap version range is specified, MetaMask attempts to install a
|
|
17
|
+
* version of the Snap that satisfies the range. If a compatible version of the
|
|
18
|
+
* Snap is already installed, the request succeeds. If an incompatible version
|
|
19
|
+
* is installed, MetaMask attempts to update the Snap to the latest version that
|
|
20
|
+
* satisfies the range. The request succeeds if the Snap is successfully
|
|
21
|
+
* installed.
|
|
22
|
+
*
|
|
23
|
+
* If the installation of any Snap fails, or the user rejects the installation
|
|
24
|
+
* or permission request, this method returns the error that caused the failure.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const result = await snap.request({
|
|
29
|
+
* method: 'wallet_requestSnaps',
|
|
30
|
+
* params: {
|
|
31
|
+
* 'npm:@metamask/example-snap': {},
|
|
32
|
+
* 'npm:@metamask/another-snap': { version: '1.2.3' },
|
|
33
|
+
* },
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
13
36
|
*/
|
|
14
37
|
export const requestSnapsHandler = {
|
|
15
|
-
methodNames: [
|
|
38
|
+
methodNames: [methodName],
|
|
16
39
|
implementation: requestSnapsImplementation,
|
|
17
40
|
hookNames,
|
|
18
41
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"requestSnaps.mjs","sourceR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YAC1B,CAAC;iBAAM,IAAI,iBAAiB,CAAC,mBAAmB,EAAE,cAAc,CAAC,EAAE,CAAC;gBAClE,GAAG,CAAC,MAAM,GAAG,MAAM,YAAY,CAAC,cAAc,CAAC,CAAC;YAClD,CAAC;iBAAM,CAAC;gBACN,MAAM,wBAAwB,GAAG,yBAAyB,CACxD,mBAAmB,EACnB,oBAAoB,CACrB,CAAC;gBAEF,MAAM,CAAC,EAAE,QAAQ,CAAC,GAAG,MAAM,kBAAkB,CAAC,wBAAwB,CAAC,CAAC;gBACxE,GAAG,CAAC,MAAM,GAAG,QAAQ,CAAC,IAAI,CACxB,0BAA0B,CACL,CAAC;YAC1B,CAAC;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC;QACpB,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,GAAG,EAAE,CAAC;AACf,CAAC","sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type {\n PermissionConstraint,\n RequestedPermissions,\n Caveat,\n PermittedHandlerExport,\n} from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n RequestSnapsParams,\n RequestSnapsResult,\n} from '@metamask/snaps-sdk';\nimport type { SnapsPermissionRequest } from '@metamask/snaps-utils';\nimport {\n SnapCaveatType,\n verifyRequestedSnapPermissions,\n} from '@metamask/snaps-utils';\nimport type {\n JsonRpcRequest,\n PendingJsonRpcResponse,\n Json,\n} from '@metamask/utils';\nimport { hasProperty, isObject } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport { WALLET_SNAP_PERMISSION_KEY } from '../restricted/invokeSnap';\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<RequestSnapsHooks> = {\n installSnaps: true,\n requestPermissions: true,\n getPermissions: true,\n};\n\n/**\n * `wallet_requestSnaps` installs the requested Snaps and requests permission to use them if necessary.\n */\nexport const requestSnapsHandler: PermittedHandlerExport<\n RequestSnapsHooks,\n RequestSnapsParams,\n RequestSnapsResult\n> = {\n methodNames: ['wallet_requestSnaps'],\n implementation: requestSnapsImplementation,\n hookNames,\n};\n\nexport type RequestSnapsHooks = {\n /**\n * Installs the requested snaps if they are permitted.\n */\n installSnaps: (\n requestedSnaps: RequestSnapsParams,\n ) => Promise<RequestSnapsResult>;\n\n /**\n * Initiates a permission request for the requesting origin.\n *\n * @returns The result of the permissions request.\n */\n requestPermissions: (\n permissions: RequestedPermissions,\n ) => Promise<\n [\n Record<string, PermissionConstraint>,\n { data: Record<string, unknown>; id: string; origin: string },\n ]\n >;\n\n /**\n * Gets the current permissions for the requesting origin.\n *\n * @returns The current permissions of the requesting origin.\n */\n getPermissions: () => Promise<\n Record<string, PermissionConstraint> | undefined\n >;\n};\n\n/**\n * Checks whether an origin has existing `wallet_snap` permission and\n * whether or not it has the requested snapIds caveat.\n *\n * @param existingPermissions - The existing permissions for the origin.\n * @param requestedSnaps - The requested snaps.\n * @returns True if the existing permissions satisfy the requested snaps, otherwise false.\n */\nexport function hasRequestedSnaps(\n existingPermissions: Record<string, PermissionConstraint>,\n requestedSnaps: Record<string, unknown>,\n): boolean {\n const snapIdCaveat = existingPermissions[\n WALLET_SNAP_PERMISSION_KEY\n ]?.caveats?.find(\n (caveat: Caveat<string, Json>) => caveat.type === SnapCaveatType.SnapIds,\n );\n\n const permittedSnaps = snapIdCaveat?.value;\n if (isObject(permittedSnaps)) {\n return Object.keys(requestedSnaps).every((requestedSnap) =>\n hasProperty(permittedSnaps, requestedSnap),\n );\n }\n return false;\n}\n\n/**\n * Constructs a valid permission request with merged caveats based on existing permissions\n * and the requested snaps.\n *\n * @param existingPermissions - The existing permissions for the origin.\n * @param requestedPermissions - The permission request passed into `requestPermissions`.\n * @returns `requestedPermissions`.\n */\nexport function getSnapPermissionsRequest(\n existingPermissions: Record<string, PermissionConstraint>,\n requestedPermissions: unknown,\n): SnapsPermissionRequest {\n verifyRequestedSnapPermissions(requestedPermissions);\n\n if (!existingPermissions[WALLET_SNAP_PERMISSION_KEY]) {\n return requestedPermissions;\n }\n\n const snapIdCaveat = existingPermissions[\n WALLET_SNAP_PERMISSION_KEY\n ].caveats?.find(\n (caveat: Caveat<string, Json>) => caveat.type === SnapCaveatType.SnapIds,\n );\n\n const permittedSnaps = (snapIdCaveat?.value as Record<string, Json>) ?? {};\n\n const requestedSnaps =\n requestedPermissions[WALLET_SNAP_PERMISSION_KEY].caveats[0].value;\n\n const snapIdSet = new Set([\n ...Object.keys(permittedSnaps),\n ...Object.keys(requestedSnaps),\n ]);\n\n const mergedCaveatValue = [...snapIdSet].reduce<Record<string, Json>>(\n (request, snapId) => {\n request[snapId] = requestedSnaps[snapId] ?? permittedSnaps[snapId];\n return request;\n },\n {},\n );\n\n requestedPermissions[WALLET_SNAP_PERMISSION_KEY].caveats[0].value =\n mergedCaveatValue;\n\n return requestedPermissions;\n}\n\nconst mutexes = new Map();\n\n/**\n * Get the corresponding Snap installation mutex for a given origin.\n *\n * @param origin - The origin of the request.\n * @returns A mutex for that specific origin.\n */\nfunction getMutex(origin: string) {\n if (!mutexes.has(origin)) {\n mutexes.set(origin, new Mutex());\n }\n return mutexes.get(origin);\n}\n\n/**\n * The `wallet_requestSnaps` method implementation.\n * Tries to install the requested snaps and adds them to the JSON-RPC response.\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.installSnaps - A function that tries to install a given snap, prompting the user if necessary.\n * @param hooks.requestPermissions - A function that requests permissions on\n * behalf of a subject.\n * @param hooks.getPermissions - A function that gets the current permissions.\n * @returns A promise that resolves once the JSON-RPC response has been modified.\n * @throws If the params are invalid.\n */\nasync function requestSnapsImplementation(\n req: JsonRpcRequest<RequestSnapsParams>,\n res: PendingJsonRpcResponse<RequestSnapsResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { installSnaps, requestPermissions, getPermissions }: RequestSnapsHooks,\n): Promise<void> {\n const requestedSnaps = req.params;\n if (!isObject(requestedSnaps)) {\n return end(\n rpcErrors.invalidParams({\n message: '\"params\" must be an object.',\n }),\n );\n }\n\n if (Object.keys(requestedSnaps).length === 0) {\n return end(\n rpcErrors.invalidParams({\n message: 'Request must have at least one requested snap.',\n }),\n );\n }\n\n // We expect the MM middleware stack to always add the origin to requests\n const { origin } = req as JsonRpcRequest & { origin: string };\n\n const mutex = getMutex(origin);\n\n // Process requests sequentially for each origin as permissions need to be merged\n // for every request.\n await mutex.runExclusive(async () => {\n try {\n const requestedPermissions = {\n [WALLET_SNAP_PERMISSION_KEY]: {\n caveats: [{ type: SnapCaveatType.SnapIds, value: requestedSnaps }],\n },\n } as RequestedPermissions;\n const existingPermissions = await getPermissions();\n\n if (!existingPermissions) {\n const [, metadata] = await requestPermissions(requestedPermissions);\n res.result = metadata.data[\n WALLET_SNAP_PERMISSION_KEY\n ] as RequestSnapsResult;\n } else if (hasRequestedSnaps(existingPermissions, requestedSnaps)) {\n res.result = await installSnaps(requestedSnaps);\n } else {\n const mergedPermissionsRequest = getSnapPermissionsRequest(\n existingPermissions,\n requestedPermissions,\n );\n\n const [, metadata] = await requestPermissions(mergedPermissionsRequest);\n res.result = metadata.data[\n WALLET_SNAP_PERMISSION_KEY\n ] as RequestSnapsResult;\n }\n } catch (error) {\n res.error = error;\n }\n });\n\n return end();\n}\n"]}
|
|
1
|
+
{"version":3,"file":"requestSnaps.mjs","sourceRoot":"","sources":["../../src/permitted/requestSnaps.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAMjD,OAAO,EACL,cAAc,EACd,8BAA8B,EAC/B,8BAA8B;AAM/B,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,wBAAwB;AACxD,OAAO,EAAE,KAAK,EAAE,oBAAoB;AAEpC,OAAO,EAAE,0BA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AAwB,GAAG,yBAAyB,CACxD,mBAAmB,EACnB,oBAAoB,CACrB,CAAC;gBAEF,MAAM,CAAC,EAAE,QAAQ,CAAC,GAAG,MAAM,kBAAkB,CAAC,wBAAwB,CAAC,CAAC;gBACxE,GAAG,CAAC,MAAM,GAAG,QAAQ,CAAC,IAAI,CACxB,0BAA0B,CACL,CAAC;YAC1B,CAAC;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC;QACpB,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,GAAG,EAAE,CAAC;AACf,CAAC","sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type {\n PermissionConstraint,\n RequestedPermissions,\n Caveat,\n PermittedHandlerExport,\n} from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n RequestSnapsParams,\n RequestSnapsResult,\n} from '@metamask/snaps-sdk';\nimport type { SnapsPermissionRequest } from '@metamask/snaps-utils';\nimport {\n SnapCaveatType,\n verifyRequestedSnapPermissions,\n} from '@metamask/snaps-utils';\nimport type {\n JsonRpcRequest,\n PendingJsonRpcResponse,\n Json,\n} from '@metamask/utils';\nimport { hasProperty, isObject } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport { WALLET_SNAP_PERMISSION_KEY } from '../restricted/invokeSnap';\nimport type { MethodHooksObject } from '../utils';\n\nconst methodName = 'wallet_requestSnaps';\n\nconst hookNames: MethodHooksObject<RequestSnapsHooks> = {\n installSnaps: true,\n requestPermissions: true,\n getPermissions: true,\n};\n\n/**\n * Request permission for a dapp to communicate with the specified Snaps and\n * attempt to install them if they're not already installed.\n *\n * If the Snap version range is specified, MetaMask attempts to install a\n * version of the Snap that satisfies the range. If a compatible version of the\n * Snap is already installed, the request succeeds. If an incompatible version\n * is installed, MetaMask attempts to update the Snap to the latest version that\n * satisfies the range. The request succeeds if the Snap is successfully\n * installed.\n *\n * If the installation of any Snap fails, or the user rejects the installation\n * or permission request, this method returns the error that caused the failure.\n *\n * @example\n * ```ts\n * const result = await snap.request({\n * method: 'wallet_requestSnaps',\n * params: {\n * 'npm:@metamask/example-snap': {},\n * 'npm:@metamask/another-snap': { version: '1.2.3' },\n * },\n * });\n * ```\n */\nexport const requestSnapsHandler = {\n methodNames: [methodName] as const,\n implementation: requestSnapsImplementation,\n hookNames,\n} satisfies PermittedHandlerExport<\n RequestSnapsHooks,\n RequestSnapsParams,\n RequestSnapsResult\n>;\n\nexport type RequestSnapsHooks = {\n /**\n * Installs the requested snaps if they are permitted.\n */\n installSnaps: (\n requestedSnaps: RequestSnapsParams,\n ) => Promise<RequestSnapsResult>;\n\n /**\n * Initiates a permission request for the requesting origin.\n *\n * @returns The result of the permissions request.\n */\n requestPermissions: (\n permissions: RequestedPermissions,\n ) => Promise<\n [\n Record<string, PermissionConstraint>,\n { data: Record<string, unknown>; id: string; origin: string },\n ]\n >;\n\n /**\n * Gets the current permissions for the requesting origin.\n *\n * @returns The current permissions of the requesting origin.\n */\n getPermissions: () => Promise<\n Record<string, PermissionConstraint> | undefined\n >;\n};\n\n/**\n * Checks whether an origin has existing `wallet_snap` permission and\n * whether or not it has the requested snapIds caveat.\n *\n * @param existingPermissions - The existing permissions for the origin.\n * @param requestedSnaps - The requested snaps.\n * @returns True if the existing permissions satisfy the requested snaps, otherwise false.\n */\nexport function hasRequestedSnaps(\n existingPermissions: Record<string, PermissionConstraint>,\n requestedSnaps: Record<string, unknown>,\n): boolean {\n const snapIdCaveat = existingPermissions[\n WALLET_SNAP_PERMISSION_KEY\n ]?.caveats?.find(\n (caveat: Caveat<string, Json>) => caveat.type === SnapCaveatType.SnapIds,\n );\n\n const permittedSnaps = snapIdCaveat?.value;\n if (isObject(permittedSnaps)) {\n return Object.keys(requestedSnaps).every((requestedSnap) =>\n hasProperty(permittedSnaps, requestedSnap),\n );\n }\n return false;\n}\n\n/**\n * Constructs a valid permission request with merged caveats based on existing permissions\n * and the requested snaps.\n *\n * @param existingPermissions - The existing permissions for the origin.\n * @param requestedPermissions - The permission request passed into `requestPermissions`.\n * @returns `requestedPermissions`.\n */\nexport function getSnapPermissionsRequest(\n existingPermissions: Record<string, PermissionConstraint>,\n requestedPermissions: unknown,\n): SnapsPermissionRequest {\n verifyRequestedSnapPermissions(requestedPermissions);\n\n if (!existingPermissions[WALLET_SNAP_PERMISSION_KEY]) {\n return requestedPermissions;\n }\n\n const snapIdCaveat = existingPermissions[\n WALLET_SNAP_PERMISSION_KEY\n ].caveats?.find(\n (caveat: Caveat<string, Json>) => caveat.type === SnapCaveatType.SnapIds,\n );\n\n const permittedSnaps = (snapIdCaveat?.value as Record<string, Json>) ?? {};\n\n const requestedSnaps =\n requestedPermissions[WALLET_SNAP_PERMISSION_KEY].caveats[0].value;\n\n const snapIdSet = new Set([\n ...Object.keys(permittedSnaps),\n ...Object.keys(requestedSnaps),\n ]);\n\n const mergedCaveatValue = [...snapIdSet].reduce<Record<string, Json>>(\n (request, snapId) => {\n request[snapId] = requestedSnaps[snapId] ?? permittedSnaps[snapId];\n return request;\n },\n {},\n );\n\n requestedPermissions[WALLET_SNAP_PERMISSION_KEY].caveats[0].value =\n mergedCaveatValue;\n\n return requestedPermissions;\n}\n\nconst mutexes = new Map();\n\n/**\n * Get the corresponding Snap installation mutex for a given origin.\n *\n * @param origin - The origin of the request.\n * @returns A mutex for that specific origin.\n */\nfunction getMutex(origin: string) {\n if (!mutexes.has(origin)) {\n mutexes.set(origin, new Mutex());\n }\n return mutexes.get(origin);\n}\n\n/**\n * The `wallet_requestSnaps` method implementation.\n * Tries to install the requested snaps and adds them to the JSON-RPC response.\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.installSnaps - A function that tries to install a given snap, prompting the user if necessary.\n * @param hooks.requestPermissions - A function that requests permissions on\n * behalf of a subject.\n * @param hooks.getPermissions - A function that gets the current permissions.\n * @returns A promise that resolves once the JSON-RPC response has been modified.\n * @throws If the params are invalid.\n */\nasync function requestSnapsImplementation(\n req: JsonRpcRequest<RequestSnapsParams>,\n res: PendingJsonRpcResponse<RequestSnapsResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { installSnaps, requestPermissions, getPermissions }: RequestSnapsHooks,\n): Promise<void> {\n const requestedSnaps = req.params;\n if (!isObject(requestedSnaps)) {\n return end(\n rpcErrors.invalidParams({\n message: '\"params\" must be an object.',\n }),\n );\n }\n\n if (Object.keys(requestedSnaps).length === 0) {\n return end(\n rpcErrors.invalidParams({\n message: 'Request must have at least one requested snap.',\n }),\n );\n }\n\n // We expect the MM middleware stack to always add the origin to requests\n const { origin } = req as JsonRpcRequest & { origin: string };\n\n const mutex = getMutex(origin);\n\n // Process requests sequentially for each origin as permissions need to be merged\n // for every request.\n await mutex.runExclusive(async () => {\n try {\n const requestedPermissions = {\n [WALLET_SNAP_PERMISSION_KEY]: {\n caveats: [{ type: SnapCaveatType.SnapIds, value: requestedSnaps }],\n },\n } as RequestedPermissions;\n const existingPermissions = await getPermissions();\n\n if (!existingPermissions) {\n const [, metadata] = await requestPermissions(requestedPermissions);\n res.result = metadata.data[\n WALLET_SNAP_PERMISSION_KEY\n ] as RequestSnapsResult;\n } else if (hasRequestedSnaps(existingPermissions, requestedSnaps)) {\n res.result = await installSnaps(requestedSnaps);\n } else {\n const mergedPermissionsRequest = getSnapPermissionsRequest(\n existingPermissions,\n requestedPermissions,\n );\n\n const [, metadata] = await requestPermissions(mergedPermissionsRequest);\n res.result = metadata.data[\n WALLET_SNAP_PERMISSION_KEY\n ] as RequestSnapsResult;\n }\n } catch (error) {\n res.error = error;\n }\n });\n\n return end();\n}\n"]}
|
|
@@ -4,11 +4,40 @@ exports.resolveInterfaceHandler = void 0;
|
|
|
4
4
|
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
5
5
|
const superstruct_1 = require("@metamask/superstruct");
|
|
6
6
|
const utils_1 = require("@metamask/utils");
|
|
7
|
+
const utils_2 = require("../utils.cjs");
|
|
8
|
+
const methodName = 'snap_resolveInterface';
|
|
7
9
|
const hookNames = {
|
|
10
|
+
hasPermission: true,
|
|
8
11
|
resolveInterface: true,
|
|
9
12
|
};
|
|
13
|
+
/**
|
|
14
|
+
* Resolve an interactive interface. For use in
|
|
15
|
+
* [custom dialogs](https://docs.metamask.io/snaps/features/custom-ui/dialogs/#display-a-custom-dialog).
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const id = await snap.request({
|
|
20
|
+
* method: 'snap_createInterface',
|
|
21
|
+
* params: {
|
|
22
|
+
* // The parameters to create the interface with.
|
|
23
|
+
* },
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* // Later, when the user has interacted with the interface, and you want to
|
|
27
|
+
* // resolve it:
|
|
28
|
+
* await snap.request({
|
|
29
|
+
* method: 'snap_resolveInterface',
|
|
30
|
+
* params: {
|
|
31
|
+
* id: interfaceId,
|
|
32
|
+
* value: {
|
|
33
|
+
* // The value to resolve the interface with.
|
|
34
|
+
* },
|
|
35
|
+
* },
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
10
39
|
exports.resolveInterfaceHandler = {
|
|
11
|
-
methodNames: [
|
|
40
|
+
methodNames: [methodName],
|
|
12
41
|
implementation: getResolveInterfaceImplementation,
|
|
13
42
|
hookNames,
|
|
14
43
|
};
|
|
@@ -25,10 +54,17 @@ const ResolveInterfaceParametersStruct = (0, superstruct_1.object)({
|
|
|
25
54
|
* function.
|
|
26
55
|
* @param end - The `json-rpc-engine` "end" callback.
|
|
27
56
|
* @param hooks - The RPC method hooks.
|
|
57
|
+
* @param hooks.hasPermission - The function to check if the Snap has a given
|
|
58
|
+
* permission.
|
|
28
59
|
* @param hooks.resolveInterface - The function to resolve the interface.
|
|
29
60
|
* @returns Nothing.
|
|
30
61
|
*/
|
|
31
|
-
async function getResolveInterfaceImplementation(req, res, _next, end, { resolveInterface }) {
|
|
62
|
+
async function getResolveInterfaceImplementation(req, res, _next, end, { hasPermission, resolveInterface }) {
|
|
63
|
+
if (!utils_2.UI_PERMISSIONS.some(hasPermission)) {
|
|
64
|
+
return end(rpc_errors_1.providerErrors.unauthorized({
|
|
65
|
+
message: `This method can only be used if the Snap has one of the following permissions: ${utils_2.UI_PERMISSIONS.join(', ')}.`,
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
32
68
|
const { params } = req;
|
|
33
69
|
try {
|
|
34
70
|
const validatedParams = getValidatedParams(params);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolveInterface.cjs","sourceRoot":"","sources":["../../src/permitted/resolveInterface.ts"],"names":[],"mappings":";;;AAEA,
|
|
1
|
+
{"version":3,"file":"resolveInterface.cjs","sourceRoot":"","sources":["../../src/permitted/resolveInterface.ts"],"names":[],"mappings":";;;AAEA,qDAAiE;AAOjE,uDAA4E;AAE5E,2CAA6C;AAG7C,wCAA0C;AAE1C,MAAM,UAAU,GAAG,uBAAuB,CAAC;AAE3C,MAAM,SAAS,GAAmD;IAChE,aAAa,EAAE,IAAI;IACnB,gBAAgB,EAAE,IAAI;CACvB,CAAC;AAgBF;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACU,QAAA,uBAAuB,GAAG;IACrC,WAAW,EAAE,CAAC,UAAU,CAAU;IAClC,cAAc,EAAE,iCAAiC;IACjD,SAAS;CAKV,CAAC;AAEF,MAAM,gCAAgC,GAAG,IAAA,oBAAM,EAAC;IAC9C,EAAE,EAAE,IAAA,oBAAM,GAAE;IACZ,KAAK,EAAE,kBAAU;CAClB,CAAC,CAAC;AAOH;;;;;;;;;;;;;GAaG;AACH,KAAK,UAAU,iCAAiC,CAC9C,GAA+C,EAC/C,GAAmD,EACnD,KAAc,EACd,GAA6B,EAC7B,EAAE,aAAa,EAAE,gBAAgB,EAA+B;IAEhE,IAAI,CAAC,sBAAc,CAAC,IAAI,CAAC,aAAa,CAAC,EAAE,CAAC;QACxC,OAAO,GAAG,CACR,2BAAc,CAAC,YAAY,CAAC;YAC1B,OAAO,EAAE,kFAAkF,sBAAc,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,KAAK,EAAE,GAAG,eAAe,CAAC;QAEtC,MAAM,gBAAgB,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;QAClC,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,IAAA,oBAAM,EAAC,MAAM,EAAE,gCAAgC,CAAC,CAAC;IAC1D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,yBAAW,EAAE,CAAC;YACjC,MAAM,sBAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,mBAAmB,KAAK,CAAC,OAAO,GAAG;aAC7C,CAAC,CAAC;QACL,CAAC;QACD,0BAA0B;QAC1B,MAAM,sBAAS,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 JsonRpcRequest,\n ResolveInterfaceParams,\n ResolveInterfaceResult,\n} from '@metamask/snaps-sdk';\nimport type { InferMatching } from '@metamask/snaps-utils';\nimport { StructError, create, object, string } from '@metamask/superstruct';\nimport type { Json, PendingJsonRpcResponse } from '@metamask/utils';\nimport { JsonStruct } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport { UI_PERMISSIONS } from '../utils';\n\nconst methodName = 'snap_resolveInterface';\n\nconst hookNames: MethodHooksObject<ResolveInterfaceMethodHooks> = {\n hasPermission: true,\n resolveInterface: true,\n};\n\nexport type ResolveInterfaceMethodHooks = {\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 value - The value to resolve the interface with.\n */\n resolveInterface: (id: string, value: Json) => Promise<void>;\n};\n\n/**\n * Resolve an interactive interface. For use in\n * [custom dialogs](https://docs.metamask.io/snaps/features/custom-ui/dialogs/#display-a-custom-dialog).\n *\n * @example\n * ```ts\n * const id = await snap.request({\n * method: 'snap_createInterface',\n * params: {\n * // The parameters to create the interface with.\n * },\n * });\n *\n * // Later, when the user has interacted with the interface, and you want to\n * // resolve it:\n * await snap.request({\n * method: 'snap_resolveInterface',\n * params: {\n * id: interfaceId,\n * value: {\n * // The value to resolve the interface with.\n * },\n * },\n * });\n * ```\n */\nexport const resolveInterfaceHandler = {\n methodNames: [methodName] as const,\n implementation: getResolveInterfaceImplementation,\n hookNames,\n} satisfies PermittedHandlerExport<\n ResolveInterfaceMethodHooks,\n ResolveInterfaceParameters,\n ResolveInterfaceResult\n>;\n\nconst ResolveInterfaceParametersStruct = object({\n id: string(),\n value: JsonStruct,\n});\n\nexport type ResolveInterfaceParameters = InferMatching<\n typeof ResolveInterfaceParametersStruct,\n ResolveInterfaceParams\n>;\n\n/**\n * The `snap_resolveInterface` 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.resolveInterface - The function to resolve the interface.\n * @returns Nothing.\n */\nasync function getResolveInterfaceImplementation(\n req: JsonRpcRequest<ResolveInterfaceParameters>,\n res: PendingJsonRpcResponse<ResolveInterfaceResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { hasPermission, resolveInterface }: ResolveInterfaceMethodHooks,\n): Promise<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, value } = validatedParams;\n\n await resolveInterface(id, value);\n res.result = null;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the resolveInterface 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 resolveInterface method parameter object.\n */\nfunction getValidatedParams(params: unknown): ResolveInterfaceParameters {\n try {\n return create(params, ResolveInterfaceParametersStruct);\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,15 +1,51 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { ResolveInterfaceParams, ResolveInterfaceResult } from "@metamask/snaps-sdk";
|
|
1
|
+
import type { JsonRpcEngineEndCallback } from "@metamask/json-rpc-engine";
|
|
2
|
+
import type { JsonRpcRequest, ResolveInterfaceParams, ResolveInterfaceResult } from "@metamask/snaps-sdk";
|
|
3
3
|
import type { InferMatching } from "@metamask/snaps-utils";
|
|
4
|
-
import type { Json } from "@metamask/utils";
|
|
4
|
+
import type { Json, PendingJsonRpcResponse } from "@metamask/utils";
|
|
5
|
+
import type { MethodHooksObject } from "../utils.cjs";
|
|
5
6
|
export type ResolveInterfaceMethodHooks = {
|
|
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;
|
|
6
12
|
/**
|
|
7
13
|
* @param id - The interface id.
|
|
8
14
|
* @param value - The value to resolve the interface with.
|
|
9
15
|
*/
|
|
10
16
|
resolveInterface: (id: string, value: Json) => Promise<void>;
|
|
11
17
|
};
|
|
12
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Resolve an interactive interface. For use in
|
|
20
|
+
* [custom dialogs](https://docs.metamask.io/snaps/features/custom-ui/dialogs/#display-a-custom-dialog).
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* const id = await snap.request({
|
|
25
|
+
* method: 'snap_createInterface',
|
|
26
|
+
* params: {
|
|
27
|
+
* // The parameters to create the interface with.
|
|
28
|
+
* },
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* // Later, when the user has interacted with the interface, and you want to
|
|
32
|
+
* // resolve it:
|
|
33
|
+
* await snap.request({
|
|
34
|
+
* method: 'snap_resolveInterface',
|
|
35
|
+
* params: {
|
|
36
|
+
* id: interfaceId,
|
|
37
|
+
* value: {
|
|
38
|
+
* // The value to resolve the interface with.
|
|
39
|
+
* },
|
|
40
|
+
* },
|
|
41
|
+
* });
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare const resolveInterfaceHandler: {
|
|
45
|
+
methodNames: ["snap_resolveInterface"];
|
|
46
|
+
implementation: typeof getResolveInterfaceImplementation;
|
|
47
|
+
hookNames: MethodHooksObject<ResolveInterfaceMethodHooks>;
|
|
48
|
+
};
|
|
13
49
|
declare const ResolveInterfaceParametersStruct: import("@metamask/superstruct").Struct<{
|
|
14
50
|
id: string;
|
|
15
51
|
value: Json;
|
|
@@ -18,5 +54,20 @@ declare const ResolveInterfaceParametersStruct: import("@metamask/superstruct").
|
|
|
18
54
|
value: import("@metamask/superstruct").Struct<Json, unknown>;
|
|
19
55
|
}>;
|
|
20
56
|
export type ResolveInterfaceParameters = InferMatching<typeof ResolveInterfaceParametersStruct, ResolveInterfaceParams>;
|
|
57
|
+
/**
|
|
58
|
+
* The `snap_resolveInterface` method implementation.
|
|
59
|
+
*
|
|
60
|
+
* @param req - The JSON-RPC request object.
|
|
61
|
+
* @param res - The JSON-RPC response object.
|
|
62
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
63
|
+
* function.
|
|
64
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
65
|
+
* @param hooks - The RPC method hooks.
|
|
66
|
+
* @param hooks.hasPermission - The function to check if the Snap has a given
|
|
67
|
+
* permission.
|
|
68
|
+
* @param hooks.resolveInterface - The function to resolve the interface.
|
|
69
|
+
* @returns Nothing.
|
|
70
|
+
*/
|
|
71
|
+
declare function getResolveInterfaceImplementation(req: JsonRpcRequest<ResolveInterfaceParameters>, res: PendingJsonRpcResponse<ResolveInterfaceResult>, _next: unknown, end: JsonRpcEngineEndCallback, { hasPermission, resolveInterface }: ResolveInterfaceMethodHooks): Promise<void>;
|
|
21
72
|
export {};
|
|
22
73
|
//# sourceMappingURL=resolveInterface.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolveInterface.d.cts","sourceRoot":"","sources":["../../src/permitted/resolveInterface.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"resolveInterface.d.cts","sourceRoot":"","sources":["../../src/permitted/resolveInterface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,kCAAkC;AAG1E,OAAO,KAAK,EACV,cAAc,EACd,sBAAsB,EACtB,sBAAsB,EACvB,4BAA4B;AAC7B,OAAO,KAAK,EAAE,aAAa,EAAE,8BAA8B;AAE3D,OAAO,KAAK,EAAE,IAAI,EAAE,sBAAsB,EAAE,wBAAwB;AAGpE,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;AAUlD,MAAM,MAAM,2BAA2B,GAAG;IACxC;;;OAGG;IACH,aAAa,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC;IAEnD;;;OAGG;IACH,gBAAgB,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9D,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,uBAAuB;;;;CAQnC,CAAC;AAEF,QAAA,MAAM,gCAAgC;;;;;;EAGpC,CAAC;AAEH,MAAM,MAAM,0BAA0B,GAAG,aAAa,CACpD,OAAO,gCAAgC,EACvC,sBAAsB,CACvB,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,iBAAe,iCAAiC,CAC9C,GAAG,EAAE,cAAc,CAAC,0BAA0B,CAAC,EAC/C,GAAG,EAAE,sBAAsB,CAAC,sBAAsB,CAAC,EACnD,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,wBAAwB,EAC7B,EAAE,aAAa,EAAE,gBAAgB,EAAE,EAAE,2BAA2B,GAC/D,OAAO,CAAC,IAAI,CAAC,CAuBf"}
|
|
@@ -1,15 +1,51 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { ResolveInterfaceParams, ResolveInterfaceResult } from "@metamask/snaps-sdk";
|
|
1
|
+
import type { JsonRpcEngineEndCallback } from "@metamask/json-rpc-engine";
|
|
2
|
+
import type { JsonRpcRequest, ResolveInterfaceParams, ResolveInterfaceResult } from "@metamask/snaps-sdk";
|
|
3
3
|
import type { InferMatching } from "@metamask/snaps-utils";
|
|
4
|
-
import type { Json } from "@metamask/utils";
|
|
4
|
+
import type { Json, PendingJsonRpcResponse } from "@metamask/utils";
|
|
5
|
+
import type { MethodHooksObject } from "../utils.mjs";
|
|
5
6
|
export type ResolveInterfaceMethodHooks = {
|
|
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;
|
|
6
12
|
/**
|
|
7
13
|
* @param id - The interface id.
|
|
8
14
|
* @param value - The value to resolve the interface with.
|
|
9
15
|
*/
|
|
10
16
|
resolveInterface: (id: string, value: Json) => Promise<void>;
|
|
11
17
|
};
|
|
12
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Resolve an interactive interface. For use in
|
|
20
|
+
* [custom dialogs](https://docs.metamask.io/snaps/features/custom-ui/dialogs/#display-a-custom-dialog).
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* const id = await snap.request({
|
|
25
|
+
* method: 'snap_createInterface',
|
|
26
|
+
* params: {
|
|
27
|
+
* // The parameters to create the interface with.
|
|
28
|
+
* },
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* // Later, when the user has interacted with the interface, and you want to
|
|
32
|
+
* // resolve it:
|
|
33
|
+
* await snap.request({
|
|
34
|
+
* method: 'snap_resolveInterface',
|
|
35
|
+
* params: {
|
|
36
|
+
* id: interfaceId,
|
|
37
|
+
* value: {
|
|
38
|
+
* // The value to resolve the interface with.
|
|
39
|
+
* },
|
|
40
|
+
* },
|
|
41
|
+
* });
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare const resolveInterfaceHandler: {
|
|
45
|
+
methodNames: ["snap_resolveInterface"];
|
|
46
|
+
implementation: typeof getResolveInterfaceImplementation;
|
|
47
|
+
hookNames: MethodHooksObject<ResolveInterfaceMethodHooks>;
|
|
48
|
+
};
|
|
13
49
|
declare const ResolveInterfaceParametersStruct: import("@metamask/superstruct").Struct<{
|
|
14
50
|
id: string;
|
|
15
51
|
value: Json;
|
|
@@ -18,5 +54,20 @@ declare const ResolveInterfaceParametersStruct: import("@metamask/superstruct").
|
|
|
18
54
|
value: import("@metamask/superstruct").Struct<Json, unknown>;
|
|
19
55
|
}>;
|
|
20
56
|
export type ResolveInterfaceParameters = InferMatching<typeof ResolveInterfaceParametersStruct, ResolveInterfaceParams>;
|
|
57
|
+
/**
|
|
58
|
+
* The `snap_resolveInterface` method implementation.
|
|
59
|
+
*
|
|
60
|
+
* @param req - The JSON-RPC request object.
|
|
61
|
+
* @param res - The JSON-RPC response object.
|
|
62
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
63
|
+
* function.
|
|
64
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
65
|
+
* @param hooks - The RPC method hooks.
|
|
66
|
+
* @param hooks.hasPermission - The function to check if the Snap has a given
|
|
67
|
+
* permission.
|
|
68
|
+
* @param hooks.resolveInterface - The function to resolve the interface.
|
|
69
|
+
* @returns Nothing.
|
|
70
|
+
*/
|
|
71
|
+
declare function getResolveInterfaceImplementation(req: JsonRpcRequest<ResolveInterfaceParameters>, res: PendingJsonRpcResponse<ResolveInterfaceResult>, _next: unknown, end: JsonRpcEngineEndCallback, { hasPermission, resolveInterface }: ResolveInterfaceMethodHooks): Promise<void>;
|
|
21
72
|
export {};
|
|
22
73
|
//# sourceMappingURL=resolveInterface.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolveInterface.d.mts","sourceRoot":"","sources":["../../src/permitted/resolveInterface.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"resolveInterface.d.mts","sourceRoot":"","sources":["../../src/permitted/resolveInterface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,kCAAkC;AAG1E,OAAO,KAAK,EACV,cAAc,EACd,sBAAsB,EACtB,sBAAsB,EACvB,4BAA4B;AAC7B,OAAO,KAAK,EAAE,aAAa,EAAE,8BAA8B;AAE3D,OAAO,KAAK,EAAE,IAAI,EAAE,sBAAsB,EAAE,wBAAwB;AAGpE,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;AAUlD,MAAM,MAAM,2BAA2B,GAAG;IACxC;;;OAGG;IACH,aAAa,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC;IAEnD;;;OAGG;IACH,gBAAgB,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9D,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,uBAAuB;;;;CAQnC,CAAC;AAEF,QAAA,MAAM,gCAAgC;;;;;;EAGpC,CAAC;AAEH,MAAM,MAAM,0BAA0B,GAAG,aAAa,CACpD,OAAO,gCAAgC,EACvC,sBAAsB,CACvB,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,iBAAe,iCAAiC,CAC9C,GAAG,EAAE,cAAc,CAAC,0BAA0B,CAAC,EAC/C,GAAG,EAAE,sBAAsB,CAAC,sBAAsB,CAAC,EACnD,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,wBAAwB,EAC7B,EAAE,aAAa,EAAE,gBAAgB,EAAE,EAAE,2BAA2B,GAC/D,OAAO,CAAC,IAAI,CAAC,CAuBf"}
|
|
@@ -1,11 +1,40 @@
|
|
|
1
|
-
import { rpcErrors } from "@metamask/rpc-errors";
|
|
1
|
+
import { providerErrors, rpcErrors } from "@metamask/rpc-errors";
|
|
2
2
|
import { StructError, create, object, string } from "@metamask/superstruct";
|
|
3
3
|
import { JsonStruct } from "@metamask/utils";
|
|
4
|
+
import { UI_PERMISSIONS } from "../utils.mjs";
|
|
5
|
+
const methodName = 'snap_resolveInterface';
|
|
4
6
|
const hookNames = {
|
|
7
|
+
hasPermission: true,
|
|
5
8
|
resolveInterface: true,
|
|
6
9
|
};
|
|
10
|
+
/**
|
|
11
|
+
* Resolve an interactive interface. For use in
|
|
12
|
+
* [custom dialogs](https://docs.metamask.io/snaps/features/custom-ui/dialogs/#display-a-custom-dialog).
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const id = await snap.request({
|
|
17
|
+
* method: 'snap_createInterface',
|
|
18
|
+
* params: {
|
|
19
|
+
* // The parameters to create the interface with.
|
|
20
|
+
* },
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Later, when the user has interacted with the interface, and you want to
|
|
24
|
+
* // resolve it:
|
|
25
|
+
* await snap.request({
|
|
26
|
+
* method: 'snap_resolveInterface',
|
|
27
|
+
* params: {
|
|
28
|
+
* id: interfaceId,
|
|
29
|
+
* value: {
|
|
30
|
+
* // The value to resolve the interface with.
|
|
31
|
+
* },
|
|
32
|
+
* },
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
7
36
|
export const resolveInterfaceHandler = {
|
|
8
|
-
methodNames: [
|
|
37
|
+
methodNames: [methodName],
|
|
9
38
|
implementation: getResolveInterfaceImplementation,
|
|
10
39
|
hookNames,
|
|
11
40
|
};
|
|
@@ -22,10 +51,17 @@ const ResolveInterfaceParametersStruct = object({
|
|
|
22
51
|
* function.
|
|
23
52
|
* @param end - The `json-rpc-engine` "end" callback.
|
|
24
53
|
* @param hooks - The RPC method hooks.
|
|
54
|
+
* @param hooks.hasPermission - The function to check if the Snap has a given
|
|
55
|
+
* permission.
|
|
25
56
|
* @param hooks.resolveInterface - The function to resolve the interface.
|
|
26
57
|
* @returns Nothing.
|
|
27
58
|
*/
|
|
28
|
-
async function getResolveInterfaceImplementation(req, res, _next, end, { resolveInterface }) {
|
|
59
|
+
async function getResolveInterfaceImplementation(req, res, _next, end, { hasPermission, resolveInterface }) {
|
|
60
|
+
if (!UI_PERMISSIONS.some(hasPermission)) {
|
|
61
|
+
return end(providerErrors.unauthorized({
|
|
62
|
+
message: `This method can only be used if the Snap has one of the following permissions: ${UI_PERMISSIONS.join(', ')}.`,
|
|
63
|
+
}));
|
|
64
|
+
}
|
|
29
65
|
const { params } = req;
|
|
30
66
|
try {
|
|
31
67
|
const validatedParams = getValidatedParams(params);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolveInterface.mjs","sourceRoot":"","sources":["../../src/permitted/resolveInterface.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,6BAA6B;
|
|
1
|
+
{"version":3,"file":"resolveInterface.mjs","sourceRoot":"","sources":["../../src/permitted/resolveInterface.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,6BAA6B;AAOjE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,8BAA8B;AAE5E,OAAO,EAAE,UAAU,EAAE,wBAAwB;AAG7C,OAAO,EAAE,cAAc,EAAE,qBAAiB;AAE1C,MAAM,UAAU,GAAG,uBAAuB,CAAC;AAE3C,MAAM,SAAS,GAAmD;IAChE,aAAa,EAAE,IAAI;IACnB,gBAAgB,EAAE,IAAI;CACvB,CAAC;AAgBF;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG;IACrC,WAAW,EAAE,CAAC,UAAU,CAAU;IAClC,cAAc,EAAE,iCAAiC;IACjD,SAAS;CAKV,CAAC;AAEF,MAAM,gCAAgC,GAAG,MAAM,CAAC;IAC9C,EAAE,EAAE,MAAM,EAAE;IACZ,KAAK,EAAE,UAAU;CAClB,CAAC,CAAC;AAOH;;;;;;;;;;;;;GAaG;AACH,KAAK,UAAU,iCAAiC,CAC9C,GAA+C,EAC/C,GAAmD,EACnD,KAAc,EACd,GAA6B,EAC7B,EAAE,aAAa,EAAE,gBAAgB,EAA+B;IAEhE,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,KAAK,EAAE,GAAG,eAAe,CAAC;QAEtC,MAAM,gBAAgB,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;QAClC,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,gCAAgC,CAAC,CAAC;IAC1D,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 JsonRpcRequest,\n ResolveInterfaceParams,\n ResolveInterfaceResult,\n} from '@metamask/snaps-sdk';\nimport type { InferMatching } from '@metamask/snaps-utils';\nimport { StructError, create, object, string } from '@metamask/superstruct';\nimport type { Json, PendingJsonRpcResponse } from '@metamask/utils';\nimport { JsonStruct } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport { UI_PERMISSIONS } from '../utils';\n\nconst methodName = 'snap_resolveInterface';\n\nconst hookNames: MethodHooksObject<ResolveInterfaceMethodHooks> = {\n hasPermission: true,\n resolveInterface: true,\n};\n\nexport type ResolveInterfaceMethodHooks = {\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 value - The value to resolve the interface with.\n */\n resolveInterface: (id: string, value: Json) => Promise<void>;\n};\n\n/**\n * Resolve an interactive interface. For use in\n * [custom dialogs](https://docs.metamask.io/snaps/features/custom-ui/dialogs/#display-a-custom-dialog).\n *\n * @example\n * ```ts\n * const id = await snap.request({\n * method: 'snap_createInterface',\n * params: {\n * // The parameters to create the interface with.\n * },\n * });\n *\n * // Later, when the user has interacted with the interface, and you want to\n * // resolve it:\n * await snap.request({\n * method: 'snap_resolveInterface',\n * params: {\n * id: interfaceId,\n * value: {\n * // The value to resolve the interface with.\n * },\n * },\n * });\n * ```\n */\nexport const resolveInterfaceHandler = {\n methodNames: [methodName] as const,\n implementation: getResolveInterfaceImplementation,\n hookNames,\n} satisfies PermittedHandlerExport<\n ResolveInterfaceMethodHooks,\n ResolveInterfaceParameters,\n ResolveInterfaceResult\n>;\n\nconst ResolveInterfaceParametersStruct = object({\n id: string(),\n value: JsonStruct,\n});\n\nexport type ResolveInterfaceParameters = InferMatching<\n typeof ResolveInterfaceParametersStruct,\n ResolveInterfaceParams\n>;\n\n/**\n * The `snap_resolveInterface` 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.resolveInterface - The function to resolve the interface.\n * @returns Nothing.\n */\nasync function getResolveInterfaceImplementation(\n req: JsonRpcRequest<ResolveInterfaceParameters>,\n res: PendingJsonRpcResponse<ResolveInterfaceResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { hasPermission, resolveInterface }: ResolveInterfaceMethodHooks,\n): Promise<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, value } = validatedParams;\n\n await resolveInterface(id, value);\n res.result = null;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the resolveInterface 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 resolveInterface method parameter object.\n */\nfunction getValidatedParams(params: unknown): ResolveInterfaceParameters {\n try {\n return create(params, ResolveInterfaceParametersStruct);\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"]}
|