@metamask/snaps-rpc-methods 15.1.0 → 16.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -4
- package/dist/endowments/keyring.cjs +2 -5
- package/dist/endowments/keyring.cjs.map +1 -1
- package/dist/endowments/keyring.d.cts +0 -2
- package/dist/endowments/keyring.d.cts.map +1 -1
- package/dist/endowments/keyring.d.mts +0 -2
- package/dist/endowments/keyring.d.mts.map +1 -1
- package/dist/endowments/keyring.mjs +3 -6
- package/dist/endowments/keyring.mjs.map +1 -1
- package/dist/index.cjs +1 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -2
- package/dist/index.mjs.map +1 -1
- package/dist/permissions.cjs +18 -8
- package/dist/permissions.cjs.map +1 -1
- package/dist/permissions.d.cts +3 -2
- package/dist/permissions.d.cts.map +1 -1
- package/dist/permissions.d.mts +3 -2
- package/dist/permissions.d.mts.map +1 -1
- package/dist/permissions.mjs +17 -7
- package/dist/permissions.mjs.map +1 -1
- package/dist/permitted/cancelBackgroundEvent.cjs +10 -14
- package/dist/permitted/cancelBackgroundEvent.cjs.map +1 -1
- package/dist/permitted/cancelBackgroundEvent.d.cts +9 -12
- package/dist/permitted/cancelBackgroundEvent.d.cts.map +1 -1
- package/dist/permitted/cancelBackgroundEvent.d.mts +9 -12
- package/dist/permitted/cancelBackgroundEvent.d.mts.map +1 -1
- package/dist/permitted/cancelBackgroundEvent.mjs +10 -14
- package/dist/permitted/cancelBackgroundEvent.mjs.map +1 -1
- package/dist/permitted/clearState.cjs +10 -16
- package/dist/permitted/clearState.cjs.map +1 -1
- package/dist/permitted/clearState.d.cts +9 -23
- package/dist/permitted/clearState.d.cts.map +1 -1
- package/dist/permitted/clearState.d.mts +9 -23
- package/dist/permitted/clearState.d.mts.map +1 -1
- package/dist/permitted/clearState.mjs +10 -16
- package/dist/permitted/clearState.mjs.map +1 -1
- package/dist/permitted/closeWebSocket.cjs +7 -14
- package/dist/permitted/closeWebSocket.cjs.map +1 -1
- package/dist/permitted/closeWebSocket.d.cts +9 -12
- package/dist/permitted/closeWebSocket.d.cts.map +1 -1
- package/dist/permitted/closeWebSocket.d.mts +9 -12
- package/dist/permitted/closeWebSocket.d.mts.map +1 -1
- package/dist/permitted/closeWebSocket.mjs +7 -14
- package/dist/permitted/closeWebSocket.mjs.map +1 -1
- package/dist/permitted/createInterface.cjs +11 -15
- package/dist/permitted/createInterface.cjs.map +1 -1
- package/dist/permitted/createInterface.d.cts +23 -37
- package/dist/permitted/createInterface.d.cts.map +1 -1
- package/dist/permitted/createInterface.d.mts +23 -37
- package/dist/permitted/createInterface.d.mts.map +1 -1
- package/dist/permitted/createInterface.mjs +11 -15
- package/dist/permitted/createInterface.mjs.map +1 -1
- package/dist/permitted/endTrace.cjs +4 -6
- package/dist/permitted/endTrace.cjs.map +1 -1
- package/dist/permitted/endTrace.d.cts +8 -11
- package/dist/permitted/endTrace.d.cts.map +1 -1
- package/dist/permitted/endTrace.d.mts +8 -11
- package/dist/permitted/endTrace.d.mts.map +1 -1
- package/dist/permitted/endTrace.mjs +4 -6
- package/dist/permitted/endTrace.mjs.map +1 -1
- package/dist/permitted/getAllSnaps.cjs +5 -11
- package/dist/permitted/getAllSnaps.cjs.map +1 -1
- package/dist/permitted/getAllSnaps.d.cts +10 -14
- package/dist/permitted/getAllSnaps.d.cts.map +1 -1
- package/dist/permitted/getAllSnaps.d.mts +10 -14
- package/dist/permitted/getAllSnaps.d.mts.map +1 -1
- package/dist/permitted/getAllSnaps.mjs +5 -11
- package/dist/permitted/getAllSnaps.mjs.map +1 -1
- package/dist/permitted/getBackgroundEvents.cjs +8 -15
- package/dist/permitted/getBackgroundEvents.cjs.map +1 -1
- package/dist/permitted/getBackgroundEvents.d.cts +10 -13
- package/dist/permitted/getBackgroundEvents.d.cts.map +1 -1
- package/dist/permitted/getBackgroundEvents.d.mts +10 -13
- package/dist/permitted/getBackgroundEvents.d.mts.map +1 -1
- package/dist/permitted/getBackgroundEvents.mjs +8 -15
- package/dist/permitted/getBackgroundEvents.mjs.map +1 -1
- package/dist/permitted/getClientStatus.cjs +5 -6
- package/dist/permitted/getClientStatus.cjs.map +1 -1
- package/dist/permitted/getClientStatus.d.cts +17 -18
- package/dist/permitted/getClientStatus.d.cts.map +1 -1
- package/dist/permitted/getClientStatus.d.mts +17 -18
- package/dist/permitted/getClientStatus.d.mts.map +1 -1
- package/dist/permitted/getClientStatus.mjs +5 -6
- package/dist/permitted/getClientStatus.mjs.map +1 -1
- package/dist/permitted/getFile.cjs +7 -11
- package/dist/permitted/getFile.cjs.map +1 -1
- package/dist/permitted/getFile.d.cts +8 -10
- package/dist/permitted/getFile.d.cts.map +1 -1
- package/dist/permitted/getFile.d.mts +8 -10
- package/dist/permitted/getFile.d.mts.map +1 -1
- package/dist/permitted/getFile.mjs +7 -11
- package/dist/permitted/getFile.mjs.map +1 -1
- package/dist/permitted/getInterfaceContext.cjs +12 -15
- package/dist/permitted/getInterfaceContext.cjs.map +1 -1
- package/dist/permitted/getInterfaceContext.d.cts +9 -21
- package/dist/permitted/getInterfaceContext.d.cts.map +1 -1
- package/dist/permitted/getInterfaceContext.d.mts +9 -21
- package/dist/permitted/getInterfaceContext.d.mts.map +1 -1
- package/dist/permitted/getInterfaceContext.mjs +12 -15
- package/dist/permitted/getInterfaceContext.mjs.map +1 -1
- package/dist/permitted/getInterfaceState.cjs +11 -15
- package/dist/permitted/getInterfaceState.cjs.map +1 -1
- package/dist/permitted/getInterfaceState.d.cts +9 -21
- package/dist/permitted/getInterfaceState.d.cts.map +1 -1
- package/dist/permitted/getInterfaceState.d.mts +9 -21
- package/dist/permitted/getInterfaceState.d.mts.map +1 -1
- package/dist/permitted/getInterfaceState.mjs +11 -15
- package/dist/permitted/getInterfaceState.mjs.map +1 -1
- package/dist/permitted/getSnaps.cjs +6 -12
- package/dist/permitted/getSnaps.cjs.map +1 -1
- package/dist/permitted/getSnaps.d.cts +11 -14
- package/dist/permitted/getSnaps.d.cts.map +1 -1
- package/dist/permitted/getSnaps.d.mts +11 -14
- package/dist/permitted/getSnaps.d.mts.map +1 -1
- package/dist/permitted/getSnaps.mjs +6 -12
- package/dist/permitted/getSnaps.mjs.map +1 -1
- package/dist/permitted/getState.cjs +9 -11
- package/dist/permitted/getState.cjs.map +1 -1
- package/dist/permitted/getState.d.cts +17 -28
- package/dist/permitted/getState.d.cts.map +1 -1
- package/dist/permitted/getState.d.mts +17 -28
- package/dist/permitted/getState.d.mts.map +1 -1
- package/dist/permitted/getState.mjs +9 -11
- package/dist/permitted/getState.mjs.map +1 -1
- package/dist/permitted/getWebSockets.cjs +11 -14
- package/dist/permitted/getWebSockets.cjs.map +1 -1
- package/dist/permitted/getWebSockets.d.cts +10 -13
- package/dist/permitted/getWebSockets.d.cts.map +1 -1
- package/dist/permitted/getWebSockets.d.mts +10 -13
- package/dist/permitted/getWebSockets.d.mts.map +1 -1
- package/dist/permitted/getWebSockets.mjs +11 -14
- package/dist/permitted/getWebSockets.mjs.map +1 -1
- package/dist/permitted/index.cjs +0 -1
- package/dist/permitted/index.cjs.map +1 -1
- package/dist/permitted/index.d.cts +29 -25
- package/dist/permitted/index.d.cts.map +1 -1
- package/dist/permitted/index.d.mts +29 -25
- package/dist/permitted/index.d.mts.map +1 -1
- package/dist/permitted/index.mjs +0 -1
- package/dist/permitted/index.mjs.map +1 -1
- package/dist/permitted/invokeKeyring.cjs +12 -13
- package/dist/permitted/invokeKeyring.cjs.map +1 -1
- package/dist/permitted/invokeKeyring.d.cts +17 -16
- package/dist/permitted/invokeKeyring.d.cts.map +1 -1
- package/dist/permitted/invokeKeyring.d.mts +17 -16
- package/dist/permitted/invokeKeyring.d.mts.map +1 -1
- package/dist/permitted/invokeKeyring.mjs +12 -13
- package/dist/permitted/invokeKeyring.mjs.map +1 -1
- package/dist/permitted/invokeSnapSugar.cjs +6 -10
- package/dist/permitted/invokeSnapSugar.cjs.map +1 -1
- package/dist/permitted/invokeSnapSugar.d.cts +9 -12
- package/dist/permitted/invokeSnapSugar.d.cts.map +1 -1
- package/dist/permitted/invokeSnapSugar.d.mts +9 -12
- package/dist/permitted/invokeSnapSugar.d.mts.map +1 -1
- package/dist/permitted/invokeSnapSugar.mjs +6 -10
- package/dist/permitted/invokeSnapSugar.mjs.map +1 -1
- package/dist/permitted/listEntropySources.cjs +28 -12
- package/dist/permitted/listEntropySources.cjs.map +1 -1
- package/dist/permitted/listEntropySources.d.cts +12 -23
- package/dist/permitted/listEntropySources.d.cts.map +1 -1
- package/dist/permitted/listEntropySources.d.mts +12 -23
- package/dist/permitted/listEntropySources.d.mts.map +1 -1
- package/dist/permitted/listEntropySources.mjs +28 -12
- package/dist/permitted/listEntropySources.mjs.map +1 -1
- package/dist/permitted/middleware.cjs +70 -20
- package/dist/permitted/middleware.cjs.map +1 -1
- package/dist/permitted/middleware.d.cts +5 -2
- package/dist/permitted/middleware.d.cts.map +1 -1
- package/dist/permitted/middleware.d.mts +5 -2
- package/dist/permitted/middleware.d.mts.map +1 -1
- package/dist/permitted/middleware.mjs +70 -20
- package/dist/permitted/middleware.mjs.map +1 -1
- package/dist/permitted/openWebSocket.cjs +7 -14
- package/dist/permitted/openWebSocket.cjs.map +1 -1
- package/dist/permitted/openWebSocket.d.cts +9 -12
- package/dist/permitted/openWebSocket.d.cts.map +1 -1
- package/dist/permitted/openWebSocket.d.mts +9 -12
- package/dist/permitted/openWebSocket.d.mts.map +1 -1
- package/dist/permitted/openWebSocket.mjs +7 -14
- package/dist/permitted/openWebSocket.mjs.map +1 -1
- package/dist/permitted/requestSnaps.cjs +14 -19
- package/dist/permitted/requestSnaps.cjs.map +1 -1
- package/dist/permitted/requestSnaps.d.cts +9 -36
- package/dist/permitted/requestSnaps.d.cts.map +1 -1
- package/dist/permitted/requestSnaps.d.mts +9 -36
- package/dist/permitted/requestSnaps.d.mts.map +1 -1
- package/dist/permitted/requestSnaps.mjs +15 -20
- package/dist/permitted/requestSnaps.mjs.map +1 -1
- package/dist/permitted/resolveInterface.cjs +11 -15
- package/dist/permitted/resolveInterface.cjs.map +1 -1
- package/dist/permitted/resolveInterface.d.cts +12 -24
- package/dist/permitted/resolveInterface.d.cts.map +1 -1
- package/dist/permitted/resolveInterface.d.mts +12 -24
- package/dist/permitted/resolveInterface.d.mts.map +1 -1
- package/dist/permitted/resolveInterface.mjs +11 -15
- package/dist/permitted/resolveInterface.mjs.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.cjs +11 -14
- package/dist/permitted/scheduleBackgroundEvent.cjs.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.d.cts +10 -17
- package/dist/permitted/scheduleBackgroundEvent.d.cts.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.d.mts +10 -17
- package/dist/permitted/scheduleBackgroundEvent.d.mts.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.mjs +11 -14
- package/dist/permitted/scheduleBackgroundEvent.mjs.map +1 -1
- package/dist/permitted/sendWebSocketMessage.cjs +10 -14
- package/dist/permitted/sendWebSocketMessage.cjs.map +1 -1
- package/dist/permitted/sendWebSocketMessage.d.cts +9 -12
- package/dist/permitted/sendWebSocketMessage.d.cts.map +1 -1
- package/dist/permitted/sendWebSocketMessage.d.mts +9 -12
- package/dist/permitted/sendWebSocketMessage.d.mts.map +1 -1
- package/dist/permitted/sendWebSocketMessage.mjs +10 -14
- package/dist/permitted/sendWebSocketMessage.mjs.map +1 -1
- package/dist/permitted/setState.cjs +18 -21
- package/dist/permitted/setState.cjs.map +1 -1
- package/dist/permitted/setState.d.cts +18 -45
- package/dist/permitted/setState.d.cts.map +1 -1
- package/dist/permitted/setState.d.mts +18 -45
- package/dist/permitted/setState.d.mts.map +1 -1
- package/dist/permitted/setState.mjs +18 -21
- package/dist/permitted/setState.mjs.map +1 -1
- package/dist/permitted/startTrace.cjs +4 -6
- package/dist/permitted/startTrace.cjs.map +1 -1
- package/dist/permitted/startTrace.d.cts +8 -11
- package/dist/permitted/startTrace.d.cts.map +1 -1
- package/dist/permitted/startTrace.d.mts +8 -11
- package/dist/permitted/startTrace.d.mts.map +1 -1
- package/dist/permitted/startTrace.mjs +4 -6
- package/dist/permitted/startTrace.mjs.map +1 -1
- package/dist/permitted/trackError.cjs +4 -6
- package/dist/permitted/trackError.cjs.map +1 -1
- package/dist/permitted/trackError.d.cts +8 -11
- package/dist/permitted/trackError.d.cts.map +1 -1
- package/dist/permitted/trackError.d.mts +8 -11
- package/dist/permitted/trackError.d.mts.map +1 -1
- package/dist/permitted/trackError.mjs +4 -6
- package/dist/permitted/trackError.mjs.map +1 -1
- package/dist/permitted/trackEvent.cjs +4 -6
- package/dist/permitted/trackEvent.cjs.map +1 -1
- package/dist/permitted/trackEvent.d.cts +8 -11
- package/dist/permitted/trackEvent.d.cts.map +1 -1
- package/dist/permitted/trackEvent.d.mts +8 -11
- package/dist/permitted/trackEvent.d.mts.map +1 -1
- package/dist/permitted/trackEvent.mjs +4 -6
- package/dist/permitted/trackEvent.mjs.map +1 -1
- package/dist/permitted/updateInterface.cjs +11 -15
- package/dist/permitted/updateInterface.cjs.map +1 -1
- package/dist/permitted/updateInterface.d.cts +9 -22
- package/dist/permitted/updateInterface.d.cts.map +1 -1
- package/dist/permitted/updateInterface.d.mts +9 -22
- package/dist/permitted/updateInterface.d.mts.map +1 -1
- package/dist/permitted/updateInterface.mjs +11 -15
- package/dist/permitted/updateInterface.mjs.map +1 -1
- package/dist/restricted/dialog.cjs +23 -30
- package/dist/restricted/dialog.cjs.map +1 -1
- package/dist/restricted/dialog.d.cts +10 -58
- package/dist/restricted/dialog.d.cts.map +1 -1
- package/dist/restricted/dialog.d.mts +10 -58
- package/dist/restricted/dialog.d.mts.map +1 -1
- package/dist/restricted/dialog.mjs +23 -30
- package/dist/restricted/dialog.mjs.map +1 -1
- package/dist/restricted/getBip32Entropy.cjs +15 -12
- package/dist/restricted/getBip32Entropy.cjs.map +1 -1
- package/dist/restricted/getBip32Entropy.d.cts +11 -24
- package/dist/restricted/getBip32Entropy.d.cts.map +1 -1
- package/dist/restricted/getBip32Entropy.d.mts +11 -24
- package/dist/restricted/getBip32Entropy.d.mts.map +1 -1
- package/dist/restricted/getBip32Entropy.mjs +16 -13
- package/dist/restricted/getBip32Entropy.mjs.map +1 -1
- package/dist/restricted/getBip32PublicKey.cjs +15 -12
- package/dist/restricted/getBip32PublicKey.cjs.map +1 -1
- package/dist/restricted/getBip32PublicKey.d.cts +11 -24
- package/dist/restricted/getBip32PublicKey.d.cts.map +1 -1
- package/dist/restricted/getBip32PublicKey.d.mts +11 -24
- package/dist/restricted/getBip32PublicKey.d.mts.map +1 -1
- package/dist/restricted/getBip32PublicKey.mjs +16 -13
- package/dist/restricted/getBip32PublicKey.mjs.map +1 -1
- package/dist/restricted/getBip44Entropy.cjs +14 -10
- package/dist/restricted/getBip44Entropy.cjs.map +1 -1
- package/dist/restricted/getBip44Entropy.d.cts +11 -15
- package/dist/restricted/getBip44Entropy.d.cts.map +1 -1
- package/dist/restricted/getBip44Entropy.d.mts +11 -15
- package/dist/restricted/getBip44Entropy.d.mts.map +1 -1
- package/dist/restricted/getBip44Entropy.mjs +15 -11
- package/dist/restricted/getBip44Entropy.mjs.map +1 -1
- package/dist/restricted/getEntropy.cjs +10 -10
- package/dist/restricted/getEntropy.cjs.map +1 -1
- package/dist/restricted/getEntropy.d.cts +11 -15
- package/dist/restricted/getEntropy.d.cts.map +1 -1
- package/dist/restricted/getEntropy.d.mts +11 -15
- package/dist/restricted/getEntropy.d.mts.map +1 -1
- package/dist/restricted/getEntropy.mjs +11 -11
- package/dist/restricted/getEntropy.mjs.map +1 -1
- package/dist/restricted/index.cjs.map +1 -1
- package/dist/restricted/index.d.cts +20 -160
- package/dist/restricted/index.d.cts.map +1 -1
- package/dist/restricted/index.d.mts +20 -160
- package/dist/restricted/index.d.mts.map +1 -1
- package/dist/restricted/index.mjs.map +1 -1
- package/dist/restricted/invokeSnap.cjs +11 -12
- package/dist/restricted/invokeSnap.cjs.map +1 -1
- package/dist/restricted/invokeSnap.d.cts +9 -13
- package/dist/restricted/invokeSnap.d.cts.map +1 -1
- package/dist/restricted/invokeSnap.d.mts +9 -13
- package/dist/restricted/invokeSnap.d.mts.map +1 -1
- package/dist/restricted/invokeSnap.mjs +11 -12
- package/dist/restricted/invokeSnap.mjs.map +1 -1
- package/dist/restricted/manageState.cjs +21 -20
- package/dist/restricted/manageState.cjs.map +1 -1
- package/dist/restricted/manageState.d.cts +12 -34
- package/dist/restricted/manageState.d.cts.map +1 -1
- package/dist/restricted/manageState.d.mts +12 -34
- package/dist/restricted/manageState.d.mts.map +1 -1
- package/dist/restricted/manageState.mjs +21 -20
- package/dist/restricted/manageState.mjs.map +1 -1
- package/dist/restricted/notify.cjs +31 -24
- package/dist/restricted/notify.cjs.map +1 -1
- package/dist/restricted/notify.d.cts +16 -25
- package/dist/restricted/notify.d.cts.map +1 -1
- package/dist/restricted/notify.d.mts +16 -25
- package/dist/restricted/notify.d.mts.map +1 -1
- package/dist/restricted/notify.mjs +32 -25
- package/dist/restricted/notify.mjs.map +1 -1
- package/dist/types.cjs +3 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +155 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +155 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +2 -0
- package/dist/types.mjs.map +1 -0
- package/dist/utils.cjs +78 -24
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +21 -13
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +21 -13
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +75 -22
- package/dist/utils.mjs.map +1 -1
- package/package.json +6 -6
- package/dist/permitted/handlers.cjs +0 -63
- package/dist/permitted/handlers.cjs.map +0 -1
- package/dist/permitted/handlers.d.cts +0 -299
- package/dist/permitted/handlers.d.cts.map +0 -1
- package/dist/permitted/handlers.d.mts +0 -299
- package/dist/permitted/handlers.d.mts.map +0 -1
- package/dist/permitted/handlers.mjs +0 -60
- package/dist/permitted/handlers.mjs.map +0 -1
package/dist/utils.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.UI_PERMISSIONS = exports.getValueFromEntropySource = exports.StateKeyStruct = exports.isValidStateKey = exports.getNodeFromSeed = exports.getNodeFromMnemonic = exports.getPathPrefix = exports.deriveEntropyFromSeed = exports.
|
|
3
|
+
exports.getMnemonicSeed = exports.getMnemonic = exports.HD_KEYRING = exports.UI_PERMISSIONS = exports.getValueFromEntropySource = exports.StateKeyStruct = exports.isValidStateKey = exports.getNodeFromSeed = exports.getNodeFromMnemonic = exports.getPathPrefix = exports.deriveEntropyFromSeed = exports.FORBIDDEN_KEYS = void 0;
|
|
4
4
|
const key_tree_1 = require("@metamask/key-tree");
|
|
5
5
|
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
6
6
|
const superstruct_1 = require("@metamask/superstruct");
|
|
@@ -9,29 +9,6 @@ const sha3_1 = require("@noble/hashes/sha3");
|
|
|
9
9
|
const endowments_1 = require("./endowments/index.cjs");
|
|
10
10
|
const HARDENED_VALUE = 0x80000000;
|
|
11
11
|
exports.FORBIDDEN_KEYS = ['constructor', '__proto__', 'prototype'];
|
|
12
|
-
/**
|
|
13
|
-
* Returns the subset of the specified `hooks` that are included in the
|
|
14
|
-
* `hookNames` object. This is a Principle of Least Authority (POLA) measure
|
|
15
|
-
* to ensure that each RPC method implementation only has access to the
|
|
16
|
-
* API "hooks" it needs to do its job.
|
|
17
|
-
*
|
|
18
|
-
* @param hooks - The hooks to select from.
|
|
19
|
-
* @param hookNames - The names of the hooks to select.
|
|
20
|
-
* @returns The selected hooks.
|
|
21
|
-
* @template Hooks - The hooks to select from.
|
|
22
|
-
* @template HookName - The names of the hooks to select.
|
|
23
|
-
*/
|
|
24
|
-
function selectHooks(hooks, hookNames) {
|
|
25
|
-
if (hookNames) {
|
|
26
|
-
return Object.keys(hookNames).reduce((hookSubset, _hookName) => {
|
|
27
|
-
const hookName = _hookName;
|
|
28
|
-
hookSubset[hookName] = hooks[hookName];
|
|
29
|
-
return hookSubset;
|
|
30
|
-
}, {});
|
|
31
|
-
}
|
|
32
|
-
return undefined;
|
|
33
|
-
}
|
|
34
|
-
exports.selectHooks = selectHooks;
|
|
35
12
|
/**
|
|
36
13
|
* Get a BIP-32 derivation path array from a hash, which is compatible with
|
|
37
14
|
* `@metamask/key-tree`. The hash is assumed to be 32 bytes long.
|
|
@@ -248,4 +225,81 @@ exports.UI_PERMISSIONS = [
|
|
|
248
225
|
endowments_1.SnapEndowments.TransactionInsight,
|
|
249
226
|
endowments_1.SnapEndowments.SignatureInsight,
|
|
250
227
|
];
|
|
228
|
+
exports.HD_KEYRING = 'HD Key Tree';
|
|
229
|
+
/**
|
|
230
|
+
* Get the mnemonic for a given entropy source. If no source is
|
|
231
|
+
* provided, the primary HD keyring's mnemonic will be returned.
|
|
232
|
+
*
|
|
233
|
+
* @param messenger - The messenger.
|
|
234
|
+
* @param source - The ID of the entropy source keyring.
|
|
235
|
+
* @returns The mnemonic.
|
|
236
|
+
*/
|
|
237
|
+
async function getMnemonic(messenger, source) {
|
|
238
|
+
if (!source) {
|
|
239
|
+
const mnemonic = (await messenger.call('KeyringController:withKeyring', {
|
|
240
|
+
type: exports.HD_KEYRING,
|
|
241
|
+
index: 0,
|
|
242
|
+
}, async ({ keyring }) => keyring.mnemonic));
|
|
243
|
+
if (!mnemonic) {
|
|
244
|
+
throw new Error('Primary keyring mnemonic unavailable.');
|
|
245
|
+
}
|
|
246
|
+
return mnemonic;
|
|
247
|
+
}
|
|
248
|
+
try {
|
|
249
|
+
const keyringData = await messenger.call('KeyringController:withKeyring', {
|
|
250
|
+
id: source,
|
|
251
|
+
}, async ({ keyring }) => ({
|
|
252
|
+
type: keyring.type,
|
|
253
|
+
mnemonic: keyring.mnemonic,
|
|
254
|
+
}));
|
|
255
|
+
const { type, mnemonic } = keyringData;
|
|
256
|
+
// The keyring isn't guaranteed to have a mnemonic (e.g.,
|
|
257
|
+
// hardware wallets, which can't be used as entropy sources),
|
|
258
|
+
// so we throw an error if it doesn't.
|
|
259
|
+
(0, utils_1.assert)(type === exports.HD_KEYRING && mnemonic);
|
|
260
|
+
return mnemonic;
|
|
261
|
+
}
|
|
262
|
+
catch {
|
|
263
|
+
throw new Error(`Entropy source with ID "${source}" not found.`);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
exports.getMnemonic = getMnemonic;
|
|
267
|
+
/**
|
|
268
|
+
* Get the mnemonic seed for a given entropy source. If no source is
|
|
269
|
+
* provided, the primary HD keyring's mnemonic seed will be returned.
|
|
270
|
+
*
|
|
271
|
+
* @param messenger - The messenger.
|
|
272
|
+
* @param source - The ID of the entropy source keyring.
|
|
273
|
+
* @returns The mnemonic seed.
|
|
274
|
+
*/
|
|
275
|
+
async function getMnemonicSeed(messenger, source) {
|
|
276
|
+
if (!source) {
|
|
277
|
+
const seed = (await messenger.call('KeyringController:withKeyring', {
|
|
278
|
+
type: exports.HD_KEYRING,
|
|
279
|
+
index: 0,
|
|
280
|
+
}, async ({ keyring }) => keyring.seed));
|
|
281
|
+
if (!seed) {
|
|
282
|
+
throw new Error('Primary keyring mnemonic unavailable.');
|
|
283
|
+
}
|
|
284
|
+
return seed;
|
|
285
|
+
}
|
|
286
|
+
try {
|
|
287
|
+
const keyringData = await messenger.call('KeyringController:withKeyring', {
|
|
288
|
+
id: source,
|
|
289
|
+
}, async ({ keyring }) => ({
|
|
290
|
+
type: keyring.type,
|
|
291
|
+
seed: keyring.seed,
|
|
292
|
+
}));
|
|
293
|
+
const { type, seed } = keyringData;
|
|
294
|
+
// The keyring isn't guaranteed to have a mnemonic (e.g.,
|
|
295
|
+
// hardware wallets, which can't be used as entropy sources),
|
|
296
|
+
// so we throw an error if it doesn't.
|
|
297
|
+
(0, utils_1.assert)(type === exports.HD_KEYRING && seed);
|
|
298
|
+
return seed;
|
|
299
|
+
}
|
|
300
|
+
catch {
|
|
301
|
+
throw new Error(`Entropy source with ID "${source}" not found.`);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
exports.getMnemonicSeed = getMnemonicSeed;
|
|
251
305
|
//# sourceMappingURL=utils.cjs.map
|
package/dist/utils.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAOA,iDAAgD;AAChD,qDAAiD;AAEjD,uDAAuD;AACvD,2CAOyB;AACzB,6CAA6D;AAE7D,uDAA8C;AAE9C,MAAM,cAAc,GAAG,UAAU,CAAC;AAErB,QAAA,cAAc,GAAG,CAAC,aAAa,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;AAWxE;;;;;;;;;;;GAWG;AACH,SAAgB,WAAW,CAIzB,KAAY,EACZ,SAAqC;IAErC,IAAI,SAAS,EAAE,CAAC;QACd,OAAO,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,MAAM,CAClC,CAAC,UAAU,EAAE,SAAS,EAAE,EAAE;YACxB,MAAM,QAAQ,GAAG,SAAqB,CAAC;YACvC,UAAU,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC;YACvC,OAAO,UAAU,CAAC;QACpB,CAAC,EACD,EAAE,CACsB,CAAC;IAC7B,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAlBD,kCAkBC;AAED;;;;;;GAMG;AACH,SAAS,sBAAsB,CAAC,IAAgB;IAC9C,MAAM,KAAK,GAAwB,EAAE,CAAC;IACtC,MAAM,IAAI,GAAG,IAAA,sBAAc,EAAC,IAAI,CAAC,CAAC;IAElC,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,CAAC,EAA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BC;AAED;;;GAGG;AACU,QAAA,cAAc,GAAG;IAC5B,aAAa;IACb,aAAa;IACb,2BAAc,CAAC,QAAQ;IACvB,2BAAc,CAAC,YAAY;IAC3B,2BAAc,CAAC,kBAAkB;IACjC,2BAAc,CAAC,gBAAgB;CACvB,CAAC","sourcesContent":["import type {\n HardenedBIP32Node,\n BIP32Node,\n SLIP10PathNode,\n SupportedCurve,\n CryptographicFunctions,\n} from '@metamask/key-tree';\nimport { SLIP10Node } from '@metamask/key-tree';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { MagicValue } from '@metamask/snaps-utils';\nimport { refine, string } from '@metamask/superstruct';\nimport {\n assertExhaustive,\n add0x,\n assert,\n concatBytes,\n createDataView,\n stringToBytes,\n} from '@metamask/utils';\nimport { keccak_256 as keccak256 } from '@noble/hashes/sha3';\n\nimport { SnapEndowments } from './endowments';\n\nconst HARDENED_VALUE = 0x80000000;\n\nexport const FORBIDDEN_KEYS = ['constructor', '__proto__', 'prototype'];\n\n/**\n * Maps an interface with method hooks to an object, using the keys of the\n * interface, and `true` as value. This ensures that the `methodHooks` object\n * has the same values as the interface.\n */\nexport type MethodHooksObject<HooksType extends Record<string, unknown>> = {\n [Key in keyof HooksType]: true;\n};\n\n/**\n * Returns the subset of the specified `hooks` that are included in the\n * `hookNames` object. This is a Principle of Least Authority (POLA) measure\n * to ensure that each RPC method implementation only has access to the\n * API \"hooks\" it needs to do its job.\n *\n * @param hooks - The hooks to select from.\n * @param hookNames - The names of the hooks to select.\n * @returns The selected hooks.\n * @template Hooks - The hooks to select from.\n * @template HookName - The names of the hooks to select.\n */\nexport function selectHooks<\n Hooks extends Record<string, unknown>,\n HookName extends keyof Hooks,\n>(\n hooks: Hooks,\n hookNames?: Record<HookName, boolean>,\n): Pick<Hooks, HookName> | undefined {\n if (hookNames) {\n return Object.keys(hookNames).reduce<Partial<Pick<Hooks, HookName>>>(\n (hookSubset, _hookName) => {\n const hookName = _hookName as HookName;\n hookSubset[hookName] = hooks[hookName];\n return hookSubset;\n },\n {},\n ) as Pick<Hooks, HookName>;\n }\n return undefined;\n}\n\n/**\n * Get a BIP-32 derivation path array from a hash, which is compatible with\n * `@metamask/key-tree`. The hash is assumed to be 32 bytes long.\n *\n * @param hash - The hash to derive indices from.\n * @returns The derived indices as a {@link HardenedBIP32Node} array.\n */\nfunction getDerivationPathArray(hash: Uint8Array): HardenedBIP32Node[] {\n const array: HardenedBIP32Node[] = [];\n const view = createDataView(hash);\n\n for (let index = 0; index < 8; index++) {\n const uint32 = view.getUint32(index * 4);\n\n // This is essentially `index | 0x80000000`. Because JavaScript numbers are\n // signed, we use the bitwise unsigned right shift operator to ensure that\n // the result is a positive number.\n // eslint-disable-next-line no-bitwise\n const pathIndex = (uint32 | HARDENED_VALUE) >>> 0;\n array.push(`bip32:${pathIndex - HARDENED_VALUE}'` as const);\n }\n\n return array;\n}\n\ntype BaseDeriveEntropyOptions = {\n /**\n * The input value to derive entropy from.\n */\n input: string;\n\n /**\n * An optional salt to use when deriving entropy.\n */\n salt?: string;\n\n /**\n * A hardened BIP-32 index, which is used to derive the root key from the\n * mnemonic phrase.\n */\n magic: MagicValue;\n\n /**\n * The cryptographic functions to use for the derivation.\n */\n cryptographicFunctions: CryptographicFunctions | undefined;\n};\n\ntype SeedDeriveEntropyOptions = BaseDeriveEntropyOptions & {\n /**\n * The mnemonic seed to use for entropy derivation.\n */\n seed: Uint8Array;\n};\n\n/**\n * Get the derivation path to use for entropy derivation.\n *\n * This is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param options - The options for entropy derivation.\n * @param options.input - The input value to derive entropy from.\n * @param options.salt - An optional salt to use when deriving entropy.\n * @param options.magic - A hardened BIP-32 index, which is used to derive the\n * root key from the mnemonic phrase.\n * @returns The derivation path to be used for entropy key derivation.\n */\nfunction getEntropyDerivationPath({\n input,\n salt,\n magic,\n}: Required<Omit<BaseDeriveEntropyOptions, 'cryptographicFunctions'>>):\n | BIP32Node[]\n | SLIP10PathNode[] {\n const inputBytes = stringToBytes(input);\n const saltBytes = stringToBytes(salt);\n\n // Get the derivation path from the snap ID.\n const hash = keccak256(concatBytes([inputBytes, keccak256(saltBytes)]));\n const computedDerivationPath = getDerivationPathArray(hash);\n\n return [`bip32:${magic}`, ...computedDerivationPath];\n}\n\n/**\n * Derive entropy from the given mnemonic seed and salt.\n *\n * This is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param options - The options for entropy derivation.\n * @param options.input - The input value to derive entropy from.\n * @param options.salt - An optional salt to use when deriving entropy.\n * @param options.seed - The mnemonic seed to use for entropy\n * derivation.\n * @param options.magic - A hardened BIP-32 index, which is used to derive the\n * root key from the mnemonic phrase.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the derivation.\n * @returns The derived entropy.\n */\nexport async function deriveEntropyFromSeed({\n input,\n salt = '',\n seed,\n magic,\n cryptographicFunctions,\n}: SeedDeriveEntropyOptions) {\n const computedDerivationPath = getEntropyDerivationPath({\n input,\n salt,\n magic,\n });\n\n // Derive the private key using BIP-32.\n const { privateKey } = await SLIP10Node.fromSeed(\n {\n derivationPath: [seed, ...computedDerivationPath],\n curve: 'secp256k1',\n },\n cryptographicFunctions,\n );\n\n // This should never happen, but this keeps TypeScript happy.\n assert(privateKey, 'Failed to derive the entropy.');\n\n return add0x(privateKey);\n}\n\n/**\n * Get the path prefix to use for key derivation in `key-tree`. This assumes the\n * following:\n *\n * - The Secp256k1 curve always uses the BIP-32 specification.\n * - The Ed25519 curve always uses the SLIP-10 specification.\n * - The BIP-32-Ed25519 curve always uses the CIP-3 specification.\n *\n * While this does not matter in most situations (no known case at the time of\n * writing), `key-tree` requires a specific specification to be used.\n *\n * @param curve - The curve to get the path prefix for. The curve is NOT\n * validated by this function.\n * @returns The path prefix, i.e., `bip32` or `slip10`.\n */\nexport function getPathPrefix(\n curve: SupportedCurve,\n): 'bip32' | 'slip10' | 'cip3' {\n switch (curve) {\n case 'secp256k1':\n return 'bip32';\n case 'ed25519':\n return 'slip10';\n case 'ed25519Bip32':\n return 'cip3';\n default:\n return assertExhaustive(curve);\n }\n}\n\ntype BaseGetNodeArgs = {\n curve: SupportedCurve;\n path: string[];\n cryptographicFunctions: CryptographicFunctions | undefined;\n};\n\ntype GetNodeArgsMnemonic = BaseGetNodeArgs & {\n secretRecoveryPhrase: Uint8Array;\n};\n\ntype GetNodeArgsSeed = BaseGetNodeArgs & {\n seed: Uint8Array;\n};\n\n/**\n * Get a `key-tree`-compatible node.\n *\n * Note: This function assumes that all the parameters have been validated\n * beforehand.\n *\n * @param options - The derivation options.\n * @param options.curve - The curve to use for derivation.\n * @param options.secretRecoveryPhrase - The secret recovery phrase to use for\n * derivation.\n * @param options.path - The derivation path to use as array, starting with an\n * \"m\" as the first item.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the node.\n * @returns The `key-tree` SLIP-10 node.\n */\nexport async function getNodeFromMnemonic({\n curve,\n secretRecoveryPhrase,\n path,\n cryptographicFunctions,\n}: GetNodeArgsMnemonic) {\n const prefix = getPathPrefix(curve);\n\n return await SLIP10Node.fromDerivationPath(\n {\n curve,\n derivationPath: [\n secretRecoveryPhrase,\n ...(path.slice(1).map((index) => `${prefix}:${index}`) as\n | BIP32Node[]\n | SLIP10PathNode[]),\n ],\n },\n cryptographicFunctions,\n );\n}\n\n/**\n * Get a `key-tree`-compatible node.\n *\n * Note: This function assumes that all the parameters have been validated\n * beforehand.\n *\n * @param options - The derivation options.\n * @param options.curve - The curve to use for derivation.\n * @param options.seed - The BIP-39 to use for\n * derivation.\n * @param options.path - The derivation path to use as array, starting with an\n * \"m\" as the first item.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the node.\n * @returns The `key-tree` SLIP-10 node.\n */\nexport async function getNodeFromSeed({\n curve,\n seed,\n path,\n cryptographicFunctions,\n}: GetNodeArgsSeed) {\n const prefix = getPathPrefix(curve);\n\n return await SLIP10Node.fromSeed(\n {\n curve,\n derivationPath: [\n seed,\n ...(path.slice(1).map((index) => `${prefix}:${index}`) as\n | BIP32Node[]\n | SLIP10PathNode[]),\n ],\n },\n cryptographicFunctions,\n );\n}\n\n/**\n * Validate the key of a state object.\n *\n * @param key - The key to validate.\n * @returns `true` if the key is valid, `false` otherwise.\n */\nexport function isValidStateKey(key: string | undefined) {\n if (key === undefined) {\n return true;\n }\n\n return key.split('.').every((part) => part.length > 0);\n}\n\nexport const StateKeyStruct = refine(string(), 'state key', (value) => {\n if (!isValidStateKey(value)) {\n return 'Invalid state key. Each part of the key must be non-empty.';\n }\n\n return true;\n});\n\n/**\n * Get a value using the entropy source hooks: getMnemonic or getMnemonicSeed.\n * This function calls the passed hook and handles any errors that occur,\n * throwing formatted JSON-RPC errors.\n *\n * @param hook - The hook.\n * @param source - The entropy source to use.\n * @returns The secret recovery phrase.\n */\nexport async function getValueFromEntropySource(\n hook: (source?: string | undefined) => Promise<Uint8Array>,\n source?: string | undefined,\n): Promise<Uint8Array> {\n try {\n return await hook(source);\n } catch (error) {\n if (error instanceof Error) {\n throw rpcErrors.invalidParams({\n message: error.message,\n });\n }\n\n throw rpcErrors.internal({\n message: 'An unknown error occurred.',\n data: {\n error: error.toString(),\n },\n });\n }\n}\n\n/**\n * The permissions that allow a Snap to show UI. Snaps must have at least one\n * of these permissions to use the interface management RPC methods.\n */\nexport const UI_PERMISSIONS = [\n 'snap_dialog',\n 'snap_notify',\n SnapEndowments.HomePage,\n SnapEndowments.SettingsPage,\n SnapEndowments.TransactionInsight,\n SnapEndowments.SignatureInsight,\n] as const;\n"]}
|
|
1
|
+
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAOA,iDAAgD;AAEhD,qDAAiD;AAEjD,uDAAuD;AACvD,2CAOyB;AACzB,6CAA6D;AAE7D,uDAA8C;AAG9C,MAAM,cAAc,GAAG,UAAU,CAAC;AAErB,QAAA,cAAc,GAAG,CAAC,aAAa,EAAE,WA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AC,MAAM,CAAC;QACP,MAAM,IAAI,KAAK,CAAC,2BAA2B,MAAM,cAAc,CAAC,CAAC;IACnE,CAAC;AACH,CAAC;AA5CD,0CA4CC","sourcesContent":["import type {\n HardenedBIP32Node,\n BIP32Node,\n SLIP10PathNode,\n SupportedCurve,\n CryptographicFunctions,\n} from '@metamask/key-tree';\nimport { SLIP10Node } from '@metamask/key-tree';\nimport type { Messenger } from '@metamask/messenger';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { MagicValue } from '@metamask/snaps-utils';\nimport { refine, string } from '@metamask/superstruct';\nimport {\n assertExhaustive,\n add0x,\n assert,\n concatBytes,\n createDataView,\n stringToBytes,\n} from '@metamask/utils';\nimport { keccak_256 as keccak256 } from '@noble/hashes/sha3';\n\nimport { SnapEndowments } from './endowments';\nimport type { KeyringControllerWithKeyringAction } from './types';\n\nconst HARDENED_VALUE = 0x80000000;\n\nexport const FORBIDDEN_KEYS = ['constructor', '__proto__', 'prototype'];\n\n/**\n * Maps an interface with method hooks to an object, using the keys of the\n * interface, and `true` as value. This ensures that the `methodHooks` object\n * has the same values as the interface.\n */\nexport type MethodHooksObject<HooksType extends Record<string, unknown>> = {\n [Key in keyof HooksType]: true;\n};\n\n/**\n * Get a BIP-32 derivation path array from a hash, which is compatible with\n * `@metamask/key-tree`. The hash is assumed to be 32 bytes long.\n *\n * @param hash - The hash to derive indices from.\n * @returns The derived indices as a {@link HardenedBIP32Node} array.\n */\nfunction getDerivationPathArray(hash: Uint8Array): HardenedBIP32Node[] {\n const array: HardenedBIP32Node[] = [];\n const view = createDataView(hash);\n\n for (let index = 0; index < 8; index++) {\n const uint32 = view.getUint32(index * 4);\n\n // This is essentially `index | 0x80000000`. Because JavaScript numbers are\n // signed, we use the bitwise unsigned right shift operator to ensure that\n // the result is a positive number.\n // eslint-disable-next-line no-bitwise\n const pathIndex = (uint32 | HARDENED_VALUE) >>> 0;\n array.push(`bip32:${pathIndex - HARDENED_VALUE}'` as const);\n }\n\n return array;\n}\n\ntype BaseDeriveEntropyOptions = {\n /**\n * The input value to derive entropy from.\n */\n input: string;\n\n /**\n * An optional salt to use when deriving entropy.\n */\n salt?: string;\n\n /**\n * A hardened BIP-32 index, which is used to derive the root key from the\n * mnemonic phrase.\n */\n magic: MagicValue;\n\n /**\n * The cryptographic functions to use for the derivation.\n */\n cryptographicFunctions: CryptographicFunctions | undefined;\n};\n\ntype SeedDeriveEntropyOptions = BaseDeriveEntropyOptions & {\n /**\n * The mnemonic seed to use for entropy derivation.\n */\n seed: Uint8Array;\n};\n\n/**\n * Get the derivation path to use for entropy derivation.\n *\n * This is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param options - The options for entropy derivation.\n * @param options.input - The input value to derive entropy from.\n * @param options.salt - An optional salt to use when deriving entropy.\n * @param options.magic - A hardened BIP-32 index, which is used to derive the\n * root key from the mnemonic phrase.\n * @returns The derivation path to be used for entropy key derivation.\n */\nfunction getEntropyDerivationPath({\n input,\n salt,\n magic,\n}: Required<Omit<BaseDeriveEntropyOptions, 'cryptographicFunctions'>>):\n | BIP32Node[]\n | SLIP10PathNode[] {\n const inputBytes = stringToBytes(input);\n const saltBytes = stringToBytes(salt);\n\n // Get the derivation path from the snap ID.\n const hash = keccak256(concatBytes([inputBytes, keccak256(saltBytes)]));\n const computedDerivationPath = getDerivationPathArray(hash);\n\n return [`bip32:${magic}`, ...computedDerivationPath];\n}\n\n/**\n * Derive entropy from the given mnemonic seed and salt.\n *\n * This is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param options - The options for entropy derivation.\n * @param options.input - The input value to derive entropy from.\n * @param options.salt - An optional salt to use when deriving entropy.\n * @param options.seed - The mnemonic seed to use for entropy\n * derivation.\n * @param options.magic - A hardened BIP-32 index, which is used to derive the\n * root key from the mnemonic phrase.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the derivation.\n * @returns The derived entropy.\n */\nexport async function deriveEntropyFromSeed({\n input,\n salt = '',\n seed,\n magic,\n cryptographicFunctions,\n}: SeedDeriveEntropyOptions) {\n const computedDerivationPath = getEntropyDerivationPath({\n input,\n salt,\n magic,\n });\n\n // Derive the private key using BIP-32.\n const { privateKey } = await SLIP10Node.fromSeed(\n {\n derivationPath: [seed, ...computedDerivationPath],\n curve: 'secp256k1',\n },\n cryptographicFunctions,\n );\n\n // This should never happen, but this keeps TypeScript happy.\n assert(privateKey, 'Failed to derive the entropy.');\n\n return add0x(privateKey);\n}\n\n/**\n * Get the path prefix to use for key derivation in `key-tree`. This assumes the\n * following:\n *\n * - The Secp256k1 curve always uses the BIP-32 specification.\n * - The Ed25519 curve always uses the SLIP-10 specification.\n * - The BIP-32-Ed25519 curve always uses the CIP-3 specification.\n *\n * While this does not matter in most situations (no known case at the time of\n * writing), `key-tree` requires a specific specification to be used.\n *\n * @param curve - The curve to get the path prefix for. The curve is NOT\n * validated by this function.\n * @returns The path prefix, i.e., `bip32` or `slip10`.\n */\nexport function getPathPrefix(\n curve: SupportedCurve,\n): 'bip32' | 'slip10' | 'cip3' {\n switch (curve) {\n case 'secp256k1':\n return 'bip32';\n case 'ed25519':\n return 'slip10';\n case 'ed25519Bip32':\n return 'cip3';\n default:\n return assertExhaustive(curve);\n }\n}\n\ntype BaseGetNodeArgs = {\n curve: SupportedCurve;\n path: string[];\n cryptographicFunctions: CryptographicFunctions | undefined;\n};\n\ntype GetNodeArgsMnemonic = BaseGetNodeArgs & {\n secretRecoveryPhrase: Uint8Array;\n};\n\ntype GetNodeArgsSeed = BaseGetNodeArgs & {\n seed: Uint8Array;\n};\n\n/**\n * Get a `key-tree`-compatible node.\n *\n * Note: This function assumes that all the parameters have been validated\n * beforehand.\n *\n * @param options - The derivation options.\n * @param options.curve - The curve to use for derivation.\n * @param options.secretRecoveryPhrase - The secret recovery phrase to use for\n * derivation.\n * @param options.path - The derivation path to use as array, starting with an\n * \"m\" as the first item.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the node.\n * @returns The `key-tree` SLIP-10 node.\n */\nexport async function getNodeFromMnemonic({\n curve,\n secretRecoveryPhrase,\n path,\n cryptographicFunctions,\n}: GetNodeArgsMnemonic) {\n const prefix = getPathPrefix(curve);\n\n return await SLIP10Node.fromDerivationPath(\n {\n curve,\n derivationPath: [\n secretRecoveryPhrase,\n ...(path.slice(1).map((index) => `${prefix}:${index}`) as\n | BIP32Node[]\n | SLIP10PathNode[]),\n ],\n },\n cryptographicFunctions,\n );\n}\n\n/**\n * Get a `key-tree`-compatible node.\n *\n * Note: This function assumes that all the parameters have been validated\n * beforehand.\n *\n * @param options - The derivation options.\n * @param options.curve - The curve to use for derivation.\n * @param options.seed - The BIP-39 to use for\n * derivation.\n * @param options.path - The derivation path to use as array, starting with an\n * \"m\" as the first item.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the node.\n * @returns The `key-tree` SLIP-10 node.\n */\nexport async function getNodeFromSeed({\n curve,\n seed,\n path,\n cryptographicFunctions,\n}: GetNodeArgsSeed) {\n const prefix = getPathPrefix(curve);\n\n return await SLIP10Node.fromSeed(\n {\n curve,\n derivationPath: [\n seed,\n ...(path.slice(1).map((index) => `${prefix}:${index}`) as\n | BIP32Node[]\n | SLIP10PathNode[]),\n ],\n },\n cryptographicFunctions,\n );\n}\n\n/**\n * Validate the key of a state object.\n *\n * @param key - The key to validate.\n * @returns `true` if the key is valid, `false` otherwise.\n */\nexport function isValidStateKey(key: string | undefined) {\n if (key === undefined) {\n return true;\n }\n\n return key.split('.').every((part) => part.length > 0);\n}\n\nexport const StateKeyStruct = refine(string(), 'state key', (value) => {\n if (!isValidStateKey(value)) {\n return 'Invalid state key. Each part of the key must be non-empty.';\n }\n\n return true;\n});\n\n/**\n * Get a value using the entropy source hooks: getMnemonic or getMnemonicSeed.\n * This function calls the passed hook and handles any errors that occur,\n * throwing formatted JSON-RPC errors.\n *\n * @param hook - The hook.\n * @param source - The entropy source to use.\n * @returns The secret recovery phrase.\n */\nexport async function getValueFromEntropySource(\n hook: (source?: string | undefined) => Promise<Uint8Array>,\n source?: string | undefined,\n): Promise<Uint8Array> {\n try {\n return await hook(source);\n } catch (error) {\n if (error instanceof Error) {\n throw rpcErrors.invalidParams({\n message: error.message,\n });\n }\n\n throw rpcErrors.internal({\n message: 'An unknown error occurred.',\n data: {\n error: error.toString(),\n },\n });\n }\n}\n\n/**\n * The permissions that allow a Snap to show UI. Snaps must have at least one\n * of these permissions to use the interface management RPC methods.\n */\nexport const UI_PERMISSIONS = [\n 'snap_dialog',\n 'snap_notify',\n SnapEndowments.HomePage,\n SnapEndowments.SettingsPage,\n SnapEndowments.TransactionInsight,\n SnapEndowments.SignatureInsight,\n] as const;\n\nexport const HD_KEYRING = 'HD Key Tree';\n\n/**\n * Get the mnemonic for a given entropy source. If no source is\n * provided, the primary HD keyring's mnemonic will be returned.\n *\n * @param messenger - The messenger.\n * @param source - The ID of the entropy source keyring.\n * @returns The mnemonic.\n */\nexport async function getMnemonic(\n messenger: Messenger<string, KeyringControllerWithKeyringAction>,\n source?: string | undefined,\n): Promise<Uint8Array> {\n if (!source) {\n const mnemonic = (await messenger.call(\n 'KeyringController:withKeyring',\n {\n type: HD_KEYRING,\n index: 0,\n },\n async ({ keyring }) => keyring.mnemonic,\n )) as Uint8Array | null;\n\n if (!mnemonic) {\n throw new Error('Primary keyring mnemonic unavailable.');\n }\n\n return mnemonic;\n }\n\n try {\n const keyringData = await messenger.call(\n 'KeyringController:withKeyring',\n {\n id: source,\n },\n async ({ keyring }) => ({\n type: keyring.type,\n mnemonic: keyring.mnemonic,\n }),\n );\n\n const { type, mnemonic } = keyringData as {\n type: string;\n mnemonic?: Uint8Array;\n };\n\n // The keyring isn't guaranteed to have a mnemonic (e.g.,\n // hardware wallets, which can't be used as entropy sources),\n // so we throw an error if it doesn't.\n assert(type === HD_KEYRING && mnemonic);\n\n return mnemonic;\n } catch {\n throw new Error(`Entropy source with ID \"${source}\" not found.`);\n }\n}\n\n/**\n * Get the mnemonic seed for a given entropy source. If no source is\n * provided, the primary HD keyring's mnemonic seed will be returned.\n *\n * @param messenger - The messenger.\n * @param source - The ID of the entropy source keyring.\n * @returns The mnemonic seed.\n */\nexport async function getMnemonicSeed(\n messenger: Messenger<string, KeyringControllerWithKeyringAction>,\n source?: string | undefined,\n): Promise<Uint8Array> {\n if (!source) {\n const seed = (await messenger.call(\n 'KeyringController:withKeyring',\n {\n type: HD_KEYRING,\n index: 0,\n },\n async ({ keyring }) => keyring.seed,\n )) as Uint8Array | null;\n\n if (!seed) {\n throw new Error('Primary keyring mnemonic unavailable.');\n }\n\n return seed;\n }\n\n try {\n const keyringData = await messenger.call(\n 'KeyringController:withKeyring',\n {\n id: source,\n },\n async ({ keyring }) => ({\n type: keyring.type,\n seed: keyring.seed,\n }),\n );\n\n const { type, seed } = keyringData as { type: string; seed?: Uint8Array };\n\n // The keyring isn't guaranteed to have a mnemonic (e.g.,\n // hardware wallets, which can't be used as entropy sources),\n // so we throw an error if it doesn't.\n assert(type === HD_KEYRING && seed);\n\n return seed;\n } catch {\n throw new Error(`Entropy source with ID \"${source}\" not found.`);\n }\n}\n"]}
|
package/dist/utils.d.cts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { SupportedCurve, CryptographicFunctions } from "@metamask/key-tree";
|
|
2
2
|
import { SLIP10Node } from "@metamask/key-tree";
|
|
3
|
+
import type { Messenger } from "@metamask/messenger";
|
|
3
4
|
import type { MagicValue } from "@metamask/snaps-utils";
|
|
4
5
|
import { SnapEndowments } from "./endowments/index.cjs";
|
|
6
|
+
import type { KeyringControllerWithKeyringAction } from "./types.cjs";
|
|
5
7
|
export declare const FORBIDDEN_KEYS: string[];
|
|
6
8
|
/**
|
|
7
9
|
* Maps an interface with method hooks to an object, using the keys of the
|
|
@@ -11,19 +13,6 @@ export declare const FORBIDDEN_KEYS: string[];
|
|
|
11
13
|
export type MethodHooksObject<HooksType extends Record<string, unknown>> = {
|
|
12
14
|
[Key in keyof HooksType]: true;
|
|
13
15
|
};
|
|
14
|
-
/**
|
|
15
|
-
* Returns the subset of the specified `hooks` that are included in the
|
|
16
|
-
* `hookNames` object. This is a Principle of Least Authority (POLA) measure
|
|
17
|
-
* to ensure that each RPC method implementation only has access to the
|
|
18
|
-
* API "hooks" it needs to do its job.
|
|
19
|
-
*
|
|
20
|
-
* @param hooks - The hooks to select from.
|
|
21
|
-
* @param hookNames - The names of the hooks to select.
|
|
22
|
-
* @returns The selected hooks.
|
|
23
|
-
* @template Hooks - The hooks to select from.
|
|
24
|
-
* @template HookName - The names of the hooks to select.
|
|
25
|
-
*/
|
|
26
|
-
export declare function selectHooks<Hooks extends Record<string, unknown>, HookName extends keyof Hooks>(hooks: Hooks, hookNames?: Record<HookName, boolean>): Pick<Hooks, HookName> | undefined;
|
|
27
16
|
type BaseDeriveEntropyOptions = {
|
|
28
17
|
/**
|
|
29
18
|
* The input value to derive entropy from.
|
|
@@ -151,5 +140,24 @@ export declare function getValueFromEntropySource(hook: (source?: string | undef
|
|
|
151
140
|
* of these permissions to use the interface management RPC methods.
|
|
152
141
|
*/
|
|
153
142
|
export declare const UI_PERMISSIONS: readonly ["snap_dialog", "snap_notify", SnapEndowments.HomePage, SnapEndowments.SettingsPage, SnapEndowments.TransactionInsight, SnapEndowments.SignatureInsight];
|
|
143
|
+
export declare const HD_KEYRING = "HD Key Tree";
|
|
144
|
+
/**
|
|
145
|
+
* Get the mnemonic for a given entropy source. If no source is
|
|
146
|
+
* provided, the primary HD keyring's mnemonic will be returned.
|
|
147
|
+
*
|
|
148
|
+
* @param messenger - The messenger.
|
|
149
|
+
* @param source - The ID of the entropy source keyring.
|
|
150
|
+
* @returns The mnemonic.
|
|
151
|
+
*/
|
|
152
|
+
export declare function getMnemonic(messenger: Messenger<string, KeyringControllerWithKeyringAction>, source?: string | undefined): Promise<Uint8Array>;
|
|
153
|
+
/**
|
|
154
|
+
* Get the mnemonic seed for a given entropy source. If no source is
|
|
155
|
+
* provided, the primary HD keyring's mnemonic seed will be returned.
|
|
156
|
+
*
|
|
157
|
+
* @param messenger - The messenger.
|
|
158
|
+
* @param source - The ID of the entropy source keyring.
|
|
159
|
+
* @returns The mnemonic seed.
|
|
160
|
+
*/
|
|
161
|
+
export declare function getMnemonicSeed(messenger: Messenger<string, KeyringControllerWithKeyringAction>, source?: string | undefined): Promise<Uint8Array>;
|
|
154
162
|
export {};
|
|
155
163
|
//# sourceMappingURL=utils.d.cts.map
|
package/dist/utils.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,cAAc,EACd,sBAAsB,EACvB,2BAA2B;AAC5B,OAAO,EAAE,UAAU,EAAE,2BAA2B;
|
|
1
|
+
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,cAAc,EACd,sBAAsB,EACvB,2BAA2B;AAC5B,OAAO,EAAE,UAAU,EAAE,2BAA2B;AAChD,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,UAAU,EAAE,8BAA8B;AAYxD,OAAO,EAAE,cAAc,EAAE,+BAAqB;AAC9C,OAAO,KAAK,EAAE,kCAAkC,EAAE,oBAAgB;AAIlE,eAAO,MAAM,cAAc,UAA4C,CAAC;AAExE;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,CAAC,SAAS,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI;KACxE,GAAG,IAAI,MAAM,SAAS,GAAG,IAAI;CAC/B,CAAC;AA2BF,KAAK,wBAAwB,GAAG;IAC9B;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;OAGG;IACH,KAAK,EAAE,UAAU,CAAC;IAElB;;OAEG;IACH,sBAAsB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AC1D,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,GAC1B,OAAO,CAAC,UAAU,CAAC,CAiBrB;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,mKAOjB,CAAC;AAEX,eAAO,MAAM,UAAU,gBAAgB,CAAC;AAExC;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAC/B,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,kCAAkC,CAAC,EAChE,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,GAC1B,OAAO,CAAC,UAAU,CAAC,CA4CrB;AAED;;;;;;;GAOG;AACH,wBAAsB,eAAe,CACnC,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,kCAAkC,CAAC,EAChE,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,GAC1B,OAAO,CAAC,UAAU,CAAC,CAyCrB"}
|
package/dist/utils.d.mts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { SupportedCurve, CryptographicFunctions } from "@metamask/key-tree";
|
|
2
2
|
import { SLIP10Node } from "@metamask/key-tree";
|
|
3
|
+
import type { Messenger } from "@metamask/messenger";
|
|
3
4
|
import type { MagicValue } from "@metamask/snaps-utils";
|
|
4
5
|
import { SnapEndowments } from "./endowments/index.mjs";
|
|
6
|
+
import type { KeyringControllerWithKeyringAction } from "./types.mjs";
|
|
5
7
|
export declare const FORBIDDEN_KEYS: string[];
|
|
6
8
|
/**
|
|
7
9
|
* Maps an interface with method hooks to an object, using the keys of the
|
|
@@ -11,19 +13,6 @@ export declare const FORBIDDEN_KEYS: string[];
|
|
|
11
13
|
export type MethodHooksObject<HooksType extends Record<string, unknown>> = {
|
|
12
14
|
[Key in keyof HooksType]: true;
|
|
13
15
|
};
|
|
14
|
-
/**
|
|
15
|
-
* Returns the subset of the specified `hooks` that are included in the
|
|
16
|
-
* `hookNames` object. This is a Principle of Least Authority (POLA) measure
|
|
17
|
-
* to ensure that each RPC method implementation only has access to the
|
|
18
|
-
* API "hooks" it needs to do its job.
|
|
19
|
-
*
|
|
20
|
-
* @param hooks - The hooks to select from.
|
|
21
|
-
* @param hookNames - The names of the hooks to select.
|
|
22
|
-
* @returns The selected hooks.
|
|
23
|
-
* @template Hooks - The hooks to select from.
|
|
24
|
-
* @template HookName - The names of the hooks to select.
|
|
25
|
-
*/
|
|
26
|
-
export declare function selectHooks<Hooks extends Record<string, unknown>, HookName extends keyof Hooks>(hooks: Hooks, hookNames?: Record<HookName, boolean>): Pick<Hooks, HookName> | undefined;
|
|
27
16
|
type BaseDeriveEntropyOptions = {
|
|
28
17
|
/**
|
|
29
18
|
* The input value to derive entropy from.
|
|
@@ -151,5 +140,24 @@ export declare function getValueFromEntropySource(hook: (source?: string | undef
|
|
|
151
140
|
* of these permissions to use the interface management RPC methods.
|
|
152
141
|
*/
|
|
153
142
|
export declare const UI_PERMISSIONS: readonly ["snap_dialog", "snap_notify", SnapEndowments.HomePage, SnapEndowments.SettingsPage, SnapEndowments.TransactionInsight, SnapEndowments.SignatureInsight];
|
|
143
|
+
export declare const HD_KEYRING = "HD Key Tree";
|
|
144
|
+
/**
|
|
145
|
+
* Get the mnemonic for a given entropy source. If no source is
|
|
146
|
+
* provided, the primary HD keyring's mnemonic will be returned.
|
|
147
|
+
*
|
|
148
|
+
* @param messenger - The messenger.
|
|
149
|
+
* @param source - The ID of the entropy source keyring.
|
|
150
|
+
* @returns The mnemonic.
|
|
151
|
+
*/
|
|
152
|
+
export declare function getMnemonic(messenger: Messenger<string, KeyringControllerWithKeyringAction>, source?: string | undefined): Promise<Uint8Array>;
|
|
153
|
+
/**
|
|
154
|
+
* Get the mnemonic seed for a given entropy source. If no source is
|
|
155
|
+
* provided, the primary HD keyring's mnemonic seed will be returned.
|
|
156
|
+
*
|
|
157
|
+
* @param messenger - The messenger.
|
|
158
|
+
* @param source - The ID of the entropy source keyring.
|
|
159
|
+
* @returns The mnemonic seed.
|
|
160
|
+
*/
|
|
161
|
+
export declare function getMnemonicSeed(messenger: Messenger<string, KeyringControllerWithKeyringAction>, source?: string | undefined): Promise<Uint8Array>;
|
|
154
162
|
export {};
|
|
155
163
|
//# sourceMappingURL=utils.d.mts.map
|
package/dist/utils.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,cAAc,EACd,sBAAsB,EACvB,2BAA2B;AAC5B,OAAO,EAAE,UAAU,EAAE,2BAA2B;
|
|
1
|
+
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,cAAc,EACd,sBAAsB,EACvB,2BAA2B;AAC5B,OAAO,EAAE,UAAU,EAAE,2BAA2B;AAChD,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,UAAU,EAAE,8BAA8B;AAYxD,OAAO,EAAE,cAAc,EAAE,+BAAqB;AAC9C,OAAO,KAAK,EAAE,kCAAkC,EAAE,oBAAgB;AAIlE,eAAO,MAAM,cAAc,UAA4C,CAAC;AAExE;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,CAAC,SAAS,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI;KACxE,GAAG,IAAI,MAAM,SAAS,GAAG,IAAI;CAC/B,CAAC;AA2BF,KAAK,wBAAwB,GAAG;IAC9B;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;OAGG;IACH,KAAK,EAAE,UAAU,CAAC;IAElB;;OAEG;IACH,sBAAsB,EAAE,sBAAsB,GAAG,SAAS,CAAC;CAC5D,CAAC;AAEF,KAAK,wBAAwB,GAAG,wBAAwB,GAAG;IACzD;;OAEG;IACH,IAAI,EAAE,UAAU,CAAC;CAClB,CAAC;AAgCF;;;;;;;;;;;;;;;;GAgBG;AACH,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;;;;GAOG;AACH,wBAAsB,WAAW,CAC/B,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,kCAAkC,CAAC,EAChE,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,GAC1B,OAAO,CAAC,UAAU,CAAC,CA4CrB;AAED;;;;;;;GAOG;AACH,wBAAsB,eAAe,CACnC,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,kCAAkC,CAAC,EAChE,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,GAC1B,OAAO,CAAC,UAAU,CAAC,CAyCrB"}
|
package/dist/utils.mjs
CHANGED
|
@@ -6,28 +6,6 @@ import { keccak_256 as keccak256 } from "@noble/hashes/sha3";
|
|
|
6
6
|
import { SnapEndowments } from "./endowments/index.mjs";
|
|
7
7
|
const HARDENED_VALUE = 0x80000000;
|
|
8
8
|
export const FORBIDDEN_KEYS = ['constructor', '__proto__', 'prototype'];
|
|
9
|
-
/**
|
|
10
|
-
* Returns the subset of the specified `hooks` that are included in the
|
|
11
|
-
* `hookNames` object. This is a Principle of Least Authority (POLA) measure
|
|
12
|
-
* to ensure that each RPC method implementation only has access to the
|
|
13
|
-
* API "hooks" it needs to do its job.
|
|
14
|
-
*
|
|
15
|
-
* @param hooks - The hooks to select from.
|
|
16
|
-
* @param hookNames - The names of the hooks to select.
|
|
17
|
-
* @returns The selected hooks.
|
|
18
|
-
* @template Hooks - The hooks to select from.
|
|
19
|
-
* @template HookName - The names of the hooks to select.
|
|
20
|
-
*/
|
|
21
|
-
export function selectHooks(hooks, hookNames) {
|
|
22
|
-
if (hookNames) {
|
|
23
|
-
return Object.keys(hookNames).reduce((hookSubset, _hookName) => {
|
|
24
|
-
const hookName = _hookName;
|
|
25
|
-
hookSubset[hookName] = hooks[hookName];
|
|
26
|
-
return hookSubset;
|
|
27
|
-
}, {});
|
|
28
|
-
}
|
|
29
|
-
return undefined;
|
|
30
|
-
}
|
|
31
9
|
/**
|
|
32
10
|
* Get a BIP-32 derivation path array from a hash, which is compatible with
|
|
33
11
|
* `@metamask/key-tree`. The hash is assumed to be 32 bytes long.
|
|
@@ -238,4 +216,79 @@ export const UI_PERMISSIONS = [
|
|
|
238
216
|
SnapEndowments.TransactionInsight,
|
|
239
217
|
SnapEndowments.SignatureInsight,
|
|
240
218
|
];
|
|
219
|
+
export const HD_KEYRING = 'HD Key Tree';
|
|
220
|
+
/**
|
|
221
|
+
* Get the mnemonic for a given entropy source. If no source is
|
|
222
|
+
* provided, the primary HD keyring's mnemonic will be returned.
|
|
223
|
+
*
|
|
224
|
+
* @param messenger - The messenger.
|
|
225
|
+
* @param source - The ID of the entropy source keyring.
|
|
226
|
+
* @returns The mnemonic.
|
|
227
|
+
*/
|
|
228
|
+
export async function getMnemonic(messenger, source) {
|
|
229
|
+
if (!source) {
|
|
230
|
+
const mnemonic = (await messenger.call('KeyringController:withKeyring', {
|
|
231
|
+
type: HD_KEYRING,
|
|
232
|
+
index: 0,
|
|
233
|
+
}, async ({ keyring }) => keyring.mnemonic));
|
|
234
|
+
if (!mnemonic) {
|
|
235
|
+
throw new Error('Primary keyring mnemonic unavailable.');
|
|
236
|
+
}
|
|
237
|
+
return mnemonic;
|
|
238
|
+
}
|
|
239
|
+
try {
|
|
240
|
+
const keyringData = await messenger.call('KeyringController:withKeyring', {
|
|
241
|
+
id: source,
|
|
242
|
+
}, async ({ keyring }) => ({
|
|
243
|
+
type: keyring.type,
|
|
244
|
+
mnemonic: keyring.mnemonic,
|
|
245
|
+
}));
|
|
246
|
+
const { type, mnemonic } = keyringData;
|
|
247
|
+
// The keyring isn't guaranteed to have a mnemonic (e.g.,
|
|
248
|
+
// hardware wallets, which can't be used as entropy sources),
|
|
249
|
+
// so we throw an error if it doesn't.
|
|
250
|
+
assert(type === HD_KEYRING && mnemonic);
|
|
251
|
+
return mnemonic;
|
|
252
|
+
}
|
|
253
|
+
catch {
|
|
254
|
+
throw new Error(`Entropy source with ID "${source}" not found.`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Get the mnemonic seed for a given entropy source. If no source is
|
|
259
|
+
* provided, the primary HD keyring's mnemonic seed will be returned.
|
|
260
|
+
*
|
|
261
|
+
* @param messenger - The messenger.
|
|
262
|
+
* @param source - The ID of the entropy source keyring.
|
|
263
|
+
* @returns The mnemonic seed.
|
|
264
|
+
*/
|
|
265
|
+
export async function getMnemonicSeed(messenger, source) {
|
|
266
|
+
if (!source) {
|
|
267
|
+
const seed = (await messenger.call('KeyringController:withKeyring', {
|
|
268
|
+
type: HD_KEYRING,
|
|
269
|
+
index: 0,
|
|
270
|
+
}, async ({ keyring }) => keyring.seed));
|
|
271
|
+
if (!seed) {
|
|
272
|
+
throw new Error('Primary keyring mnemonic unavailable.');
|
|
273
|
+
}
|
|
274
|
+
return seed;
|
|
275
|
+
}
|
|
276
|
+
try {
|
|
277
|
+
const keyringData = await messenger.call('KeyringController:withKeyring', {
|
|
278
|
+
id: source,
|
|
279
|
+
}, async ({ keyring }) => ({
|
|
280
|
+
type: keyring.type,
|
|
281
|
+
seed: keyring.seed,
|
|
282
|
+
}));
|
|
283
|
+
const { type, seed } = keyringData;
|
|
284
|
+
// The keyring isn't guaranteed to have a mnemonic (e.g.,
|
|
285
|
+
// hardware wallets, which can't be used as entropy sources),
|
|
286
|
+
// so we throw an error if it doesn't.
|
|
287
|
+
assert(type === HD_KEYRING && seed);
|
|
288
|
+
return seed;
|
|
289
|
+
}
|
|
290
|
+
catch {
|
|
291
|
+
throw new Error(`Entropy source with ID "${source}" not found.`);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
241
294
|
//# sourceMappingURL=utils.mjs.map
|
package/dist/utils.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,UAAU,EAAE,2BAA2B;AAChD,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAEjD,OAAO,EAAE,MAAM,E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AC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AACzD,CAAC;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,EAAE,WAAW,EAAE,CAAC,KAAK,EAAE,EAAE;IACpE,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,EAAE,CAAC;QAC5B,OAAO,4DAA4D,CAAC;IACtE,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC,CAAC,CAAC;AAEH;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,yBAAyB,CAC7C,IAA0D,EAC1D,MAA2B;IAE3B,IAAI,CAAC;QACH,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,CAAC;IAC5B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;YAC3B,MAAM,SAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,KAAK,CAAC,OAAO;aACvB,CAAC,CAAC;QACL,CAAC;QAED,MAAM,SAAS,CAAC,QAAQ,CAAC;YACvB,OAAO,EAAE,4BAA4B;YACrC,IAAI,EAAE;gBACJ,KAAK,EAAE,KAAK,CAAC,QAAQ,EAAE;aACxB;SACF,CAAC,CAAC;IACL,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,aAAa;IACb,aAAa;IACb,cAAc,CAAC,QAAQ;IACvB,cAAc,CAAC,YAAY;IAC3B,cAAc,CAAC,kBAAkB;IACjC,cAAc,CAAC,gBAAgB;CACvB,CAAC","sourcesContent":["import type {\n HardenedBIP32Node,\n BIP32Node,\n SLIP10PathNode,\n SupportedCurve,\n CryptographicFunctions,\n} from '@metamask/key-tree';\nimport { SLIP10Node } from '@metamask/key-tree';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { MagicValue } from '@metamask/snaps-utils';\nimport { refine, string } from '@metamask/superstruct';\nimport {\n assertExhaustive,\n add0x,\n assert,\n concatBytes,\n createDataView,\n stringToBytes,\n} from '@metamask/utils';\nimport { keccak_256 as keccak256 } from '@noble/hashes/sha3';\n\nimport { SnapEndowments } from './endowments';\n\nconst HARDENED_VALUE = 0x80000000;\n\nexport const FORBIDDEN_KEYS = ['constructor', '__proto__', 'prototype'];\n\n/**\n * Maps an interface with method hooks to an object, using the keys of the\n * interface, and `true` as value. This ensures that the `methodHooks` object\n * has the same values as the interface.\n */\nexport type MethodHooksObject<HooksType extends Record<string, unknown>> = {\n [Key in keyof HooksType]: true;\n};\n\n/**\n * Returns the subset of the specified `hooks` that are included in the\n * `hookNames` object. This is a Principle of Least Authority (POLA) measure\n * to ensure that each RPC method implementation only has access to the\n * API \"hooks\" it needs to do its job.\n *\n * @param hooks - The hooks to select from.\n * @param hookNames - The names of the hooks to select.\n * @returns The selected hooks.\n * @template Hooks - The hooks to select from.\n * @template HookName - The names of the hooks to select.\n */\nexport function selectHooks<\n Hooks extends Record<string, unknown>,\n HookName extends keyof Hooks,\n>(\n hooks: Hooks,\n hookNames?: Record<HookName, boolean>,\n): Pick<Hooks, HookName> | undefined {\n if (hookNames) {\n return Object.keys(hookNames).reduce<Partial<Pick<Hooks, HookName>>>(\n (hookSubset, _hookName) => {\n const hookName = _hookName as HookName;\n hookSubset[hookName] = hooks[hookName];\n return hookSubset;\n },\n {},\n ) as Pick<Hooks, HookName>;\n }\n return undefined;\n}\n\n/**\n * Get a BIP-32 derivation path array from a hash, which is compatible with\n * `@metamask/key-tree`. The hash is assumed to be 32 bytes long.\n *\n * @param hash - The hash to derive indices from.\n * @returns The derived indices as a {@link HardenedBIP32Node} array.\n */\nfunction getDerivationPathArray(hash: Uint8Array): HardenedBIP32Node[] {\n const array: HardenedBIP32Node[] = [];\n const view = createDataView(hash);\n\n for (let index = 0; index < 8; index++) {\n const uint32 = view.getUint32(index * 4);\n\n // This is essentially `index | 0x80000000`. Because JavaScript numbers are\n // signed, we use the bitwise unsigned right shift operator to ensure that\n // the result is a positive number.\n // eslint-disable-next-line no-bitwise\n const pathIndex = (uint32 | HARDENED_VALUE) >>> 0;\n array.push(`bip32:${pathIndex - HARDENED_VALUE}'` as const);\n }\n\n return array;\n}\n\ntype BaseDeriveEntropyOptions = {\n /**\n * The input value to derive entropy from.\n */\n input: string;\n\n /**\n * An optional salt to use when deriving entropy.\n */\n salt?: string;\n\n /**\n * A hardened BIP-32 index, which is used to derive the root key from the\n * mnemonic phrase.\n */\n magic: MagicValue;\n\n /**\n * The cryptographic functions to use for the derivation.\n */\n cryptographicFunctions: CryptographicFunctions | undefined;\n};\n\ntype SeedDeriveEntropyOptions = BaseDeriveEntropyOptions & {\n /**\n * The mnemonic seed to use for entropy derivation.\n */\n seed: Uint8Array;\n};\n\n/**\n * Get the derivation path to use for entropy derivation.\n *\n * This is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param options - The options for entropy derivation.\n * @param options.input - The input value to derive entropy from.\n * @param options.salt - An optional salt to use when deriving entropy.\n * @param options.magic - A hardened BIP-32 index, which is used to derive the\n * root key from the mnemonic phrase.\n * @returns The derivation path to be used for entropy key derivation.\n */\nfunction getEntropyDerivationPath({\n input,\n salt,\n magic,\n}: Required<Omit<BaseDeriveEntropyOptions, 'cryptographicFunctions'>>):\n | BIP32Node[]\n | SLIP10PathNode[] {\n const inputBytes = stringToBytes(input);\n const saltBytes = stringToBytes(salt);\n\n // Get the derivation path from the snap ID.\n const hash = keccak256(concatBytes([inputBytes, keccak256(saltBytes)]));\n const computedDerivationPath = getDerivationPathArray(hash);\n\n return [`bip32:${magic}`, ...computedDerivationPath];\n}\n\n/**\n * Derive entropy from the given mnemonic seed and salt.\n *\n * This is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param options - The options for entropy derivation.\n * @param options.input - The input value to derive entropy from.\n * @param options.salt - An optional salt to use when deriving entropy.\n * @param options.seed - The mnemonic seed to use for entropy\n * derivation.\n * @param options.magic - A hardened BIP-32 index, which is used to derive the\n * root key from the mnemonic phrase.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the derivation.\n * @returns The derived entropy.\n */\nexport async function deriveEntropyFromSeed({\n input,\n salt = '',\n seed,\n magic,\n cryptographicFunctions,\n}: SeedDeriveEntropyOptions) {\n const computedDerivationPath = getEntropyDerivationPath({\n input,\n salt,\n magic,\n });\n\n // Derive the private key using BIP-32.\n const { privateKey } = await SLIP10Node.fromSeed(\n {\n derivationPath: [seed, ...computedDerivationPath],\n curve: 'secp256k1',\n },\n cryptographicFunctions,\n );\n\n // This should never happen, but this keeps TypeScript happy.\n assert(privateKey, 'Failed to derive the entropy.');\n\n return add0x(privateKey);\n}\n\n/**\n * Get the path prefix to use for key derivation in `key-tree`. This assumes the\n * following:\n *\n * - The Secp256k1 curve always uses the BIP-32 specification.\n * - The Ed25519 curve always uses the SLIP-10 specification.\n * - The BIP-32-Ed25519 curve always uses the CIP-3 specification.\n *\n * While this does not matter in most situations (no known case at the time of\n * writing), `key-tree` requires a specific specification to be used.\n *\n * @param curve - The curve to get the path prefix for. The curve is NOT\n * validated by this function.\n * @returns The path prefix, i.e., `bip32` or `slip10`.\n */\nexport function getPathPrefix(\n curve: SupportedCurve,\n): 'bip32' | 'slip10' | 'cip3' {\n switch (curve) {\n case 'secp256k1':\n return 'bip32';\n case 'ed25519':\n return 'slip10';\n case 'ed25519Bip32':\n return 'cip3';\n default:\n return assertExhaustive(curve);\n }\n}\n\ntype BaseGetNodeArgs = {\n curve: SupportedCurve;\n path: string[];\n cryptographicFunctions: CryptographicFunctions | undefined;\n};\n\ntype GetNodeArgsMnemonic = BaseGetNodeArgs & {\n secretRecoveryPhrase: Uint8Array;\n};\n\ntype GetNodeArgsSeed = BaseGetNodeArgs & {\n seed: Uint8Array;\n};\n\n/**\n * Get a `key-tree`-compatible node.\n *\n * Note: This function assumes that all the parameters have been validated\n * beforehand.\n *\n * @param options - The derivation options.\n * @param options.curve - The curve to use for derivation.\n * @param options.secretRecoveryPhrase - The secret recovery phrase to use for\n * derivation.\n * @param options.path - The derivation path to use as array, starting with an\n * \"m\" as the first item.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the node.\n * @returns The `key-tree` SLIP-10 node.\n */\nexport async function getNodeFromMnemonic({\n curve,\n secretRecoveryPhrase,\n path,\n cryptographicFunctions,\n}: GetNodeArgsMnemonic) {\n const prefix = getPathPrefix(curve);\n\n return await SLIP10Node.fromDerivationPath(\n {\n curve,\n derivationPath: [\n secretRecoveryPhrase,\n ...(path.slice(1).map((index) => `${prefix}:${index}`) as\n | BIP32Node[]\n | SLIP10PathNode[]),\n ],\n },\n cryptographicFunctions,\n );\n}\n\n/**\n * Get a `key-tree`-compatible node.\n *\n * Note: This function assumes that all the parameters have been validated\n * beforehand.\n *\n * @param options - The derivation options.\n * @param options.curve - The curve to use for derivation.\n * @param options.seed - The BIP-39 to use for\n * derivation.\n * @param options.path - The derivation path to use as array, starting with an\n * \"m\" as the first item.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the node.\n * @returns The `key-tree` SLIP-10 node.\n */\nexport async function getNodeFromSeed({\n curve,\n seed,\n path,\n cryptographicFunctions,\n}: GetNodeArgsSeed) {\n const prefix = getPathPrefix(curve);\n\n return await SLIP10Node.fromSeed(\n {\n curve,\n derivationPath: [\n seed,\n ...(path.slice(1).map((index) => `${prefix}:${index}`) as\n | BIP32Node[]\n | SLIP10PathNode[]),\n ],\n },\n cryptographicFunctions,\n );\n}\n\n/**\n * Validate the key of a state object.\n *\n * @param key - The key to validate.\n * @returns `true` if the key is valid, `false` otherwise.\n */\nexport function isValidStateKey(key: string | undefined) {\n if (key === undefined) {\n return true;\n }\n\n return key.split('.').every((part) => part.length > 0);\n}\n\nexport const StateKeyStruct = refine(string(), 'state key', (value) => {\n if (!isValidStateKey(value)) {\n return 'Invalid state key. Each part of the key must be non-empty.';\n }\n\n return true;\n});\n\n/**\n * Get a value using the entropy source hooks: getMnemonic or getMnemonicSeed.\n * This function calls the passed hook and handles any errors that occur,\n * throwing formatted JSON-RPC errors.\n *\n * @param hook - The hook.\n * @param source - The entropy source to use.\n * @returns The secret recovery phrase.\n */\nexport async function getValueFromEntropySource(\n hook: (source?: string | undefined) => Promise<Uint8Array>,\n source?: string | undefined,\n): Promise<Uint8Array> {\n try {\n return await hook(source);\n } catch (error) {\n if (error instanceof Error) {\n throw rpcErrors.invalidParams({\n message: error.message,\n });\n }\n\n throw rpcErrors.internal({\n message: 'An unknown error occurred.',\n data: {\n error: error.toString(),\n },\n });\n }\n}\n\n/**\n * The permissions that allow a Snap to show UI. Snaps must have at least one\n * of these permissions to use the interface management RPC methods.\n */\nexport const UI_PERMISSIONS = [\n 'snap_dialog',\n 'snap_notify',\n SnapEndowments.HomePage,\n SnapEndowments.SettingsPage,\n SnapEndowments.TransactionInsight,\n SnapEndowments.SignatureInsight,\n] as const;\n"]}
|
|
1
|
+
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,UAAU,EAAE,2BAA2B;AAEhD,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAEjD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,8BAA8B;AACvD,OAAO,EACL,gBAAgB,EAChB,KAAK,EACL,MAAM,EACN,WAAW,EACX,cAAc,EACd,aAAa,EACd,wBAAwB;AACzB,OAAO,EAAE,UAAU,IAAI,SAAS,EAAE,2BAA2B;AAE7D,OAAO,EAAE,cAAc,EAAE,+BAAqB;AAG9C,MAAM,cAAc,GAAG,UAAU,CAAC;AAElC,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,aAAa,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;AAWxE;;;;;;GAMG;AACH,SAAS,sBAAsB,CAAC,IAAgB;IAC9C,MAAM,KAAK,GAAwB,EAAE,CAAC;IACtC,M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,MAAM,IAAI,KAAK,CAAC,2BAA2B,MAAM,cAAc,CAAC,CAAC;IACnE,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,SAAgE,EAChE,MAA2B;IAE3B,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,IAAI,GAAG,CAAC,MAAM,SAAS,CAAC,IAAI,CAChC,+BAA+B,EAC/B;YACE,IAAI,EAAE,UAAU;YAChB,KAAK,EAAE,CAAC;SACT,EACD,KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CACpC,CAAsB,CAAC;QAExB,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;QAC3D,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,CAAC;QACH,MAAM,WAAW,GAAG,MAAM,SAAS,CAAC,IAAI,CACtC,+BAA+B,EAC/B;YACE,EAAE,EAAE,MAAM;SACX,EACD,KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC;YACtB,IAAI,EAAE,OAAO,CAAC,IAAI;YAClB,IAAI,EAAE,OAAO,CAAC,IAAI;SACnB,CAAC,CACH,CAAC;QAEF,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,WAAkD,CAAC;QAE1E,yDAAyD;QACzD,6DAA6D;QAC7D,sCAAsC;QACtC,MAAM,CAAC,IAAI,KAAK,UAAU,IAAI,IAAI,CAAC,CAAC;QAEpC,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,KAAK,CAAC,2BAA2B,MAAM,cAAc,CAAC,CAAC;IACnE,CAAC;AACH,CAAC","sourcesContent":["import type {\n HardenedBIP32Node,\n BIP32Node,\n SLIP10PathNode,\n SupportedCurve,\n CryptographicFunctions,\n} from '@metamask/key-tree';\nimport { SLIP10Node } from '@metamask/key-tree';\nimport type { Messenger } from '@metamask/messenger';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { MagicValue } from '@metamask/snaps-utils';\nimport { refine, string } from '@metamask/superstruct';\nimport {\n assertExhaustive,\n add0x,\n assert,\n concatBytes,\n createDataView,\n stringToBytes,\n} from '@metamask/utils';\nimport { keccak_256 as keccak256 } from '@noble/hashes/sha3';\n\nimport { SnapEndowments } from './endowments';\nimport type { KeyringControllerWithKeyringAction } from './types';\n\nconst HARDENED_VALUE = 0x80000000;\n\nexport const FORBIDDEN_KEYS = ['constructor', '__proto__', 'prototype'];\n\n/**\n * Maps an interface with method hooks to an object, using the keys of the\n * interface, and `true` as value. This ensures that the `methodHooks` object\n * has the same values as the interface.\n */\nexport type MethodHooksObject<HooksType extends Record<string, unknown>> = {\n [Key in keyof HooksType]: true;\n};\n\n/**\n * Get a BIP-32 derivation path array from a hash, which is compatible with\n * `@metamask/key-tree`. The hash is assumed to be 32 bytes long.\n *\n * @param hash - The hash to derive indices from.\n * @returns The derived indices as a {@link HardenedBIP32Node} array.\n */\nfunction getDerivationPathArray(hash: Uint8Array): HardenedBIP32Node[] {\n const array: HardenedBIP32Node[] = [];\n const view = createDataView(hash);\n\n for (let index = 0; index < 8; index++) {\n const uint32 = view.getUint32(index * 4);\n\n // This is essentially `index | 0x80000000`. Because JavaScript numbers are\n // signed, we use the bitwise unsigned right shift operator to ensure that\n // the result is a positive number.\n // eslint-disable-next-line no-bitwise\n const pathIndex = (uint32 | HARDENED_VALUE) >>> 0;\n array.push(`bip32:${pathIndex - HARDENED_VALUE}'` as const);\n }\n\n return array;\n}\n\ntype BaseDeriveEntropyOptions = {\n /**\n * The input value to derive entropy from.\n */\n input: string;\n\n /**\n * An optional salt to use when deriving entropy.\n */\n salt?: string;\n\n /**\n * A hardened BIP-32 index, which is used to derive the root key from the\n * mnemonic phrase.\n */\n magic: MagicValue;\n\n /**\n * The cryptographic functions to use for the derivation.\n */\n cryptographicFunctions: CryptographicFunctions | undefined;\n};\n\ntype SeedDeriveEntropyOptions = BaseDeriveEntropyOptions & {\n /**\n * The mnemonic seed to use for entropy derivation.\n */\n seed: Uint8Array;\n};\n\n/**\n * Get the derivation path to use for entropy derivation.\n *\n * This is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param options - The options for entropy derivation.\n * @param options.input - The input value to derive entropy from.\n * @param options.salt - An optional salt to use when deriving entropy.\n * @param options.magic - A hardened BIP-32 index, which is used to derive the\n * root key from the mnemonic phrase.\n * @returns The derivation path to be used for entropy key derivation.\n */\nfunction getEntropyDerivationPath({\n input,\n salt,\n magic,\n}: Required<Omit<BaseDeriveEntropyOptions, 'cryptographicFunctions'>>):\n | BIP32Node[]\n | SLIP10PathNode[] {\n const inputBytes = stringToBytes(input);\n const saltBytes = stringToBytes(salt);\n\n // Get the derivation path from the snap ID.\n const hash = keccak256(concatBytes([inputBytes, keccak256(saltBytes)]));\n const computedDerivationPath = getDerivationPathArray(hash);\n\n return [`bip32:${magic}`, ...computedDerivationPath];\n}\n\n/**\n * Derive entropy from the given mnemonic seed and salt.\n *\n * This is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param options - The options for entropy derivation.\n * @param options.input - The input value to derive entropy from.\n * @param options.salt - An optional salt to use when deriving entropy.\n * @param options.seed - The mnemonic seed to use for entropy\n * derivation.\n * @param options.magic - A hardened BIP-32 index, which is used to derive the\n * root key from the mnemonic phrase.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the derivation.\n * @returns The derived entropy.\n */\nexport async function deriveEntropyFromSeed({\n input,\n salt = '',\n seed,\n magic,\n cryptographicFunctions,\n}: SeedDeriveEntropyOptions) {\n const computedDerivationPath = getEntropyDerivationPath({\n input,\n salt,\n magic,\n });\n\n // Derive the private key using BIP-32.\n const { privateKey } = await SLIP10Node.fromSeed(\n {\n derivationPath: [seed, ...computedDerivationPath],\n curve: 'secp256k1',\n },\n cryptographicFunctions,\n );\n\n // This should never happen, but this keeps TypeScript happy.\n assert(privateKey, 'Failed to derive the entropy.');\n\n return add0x(privateKey);\n}\n\n/**\n * Get the path prefix to use for key derivation in `key-tree`. This assumes the\n * following:\n *\n * - The Secp256k1 curve always uses the BIP-32 specification.\n * - The Ed25519 curve always uses the SLIP-10 specification.\n * - The BIP-32-Ed25519 curve always uses the CIP-3 specification.\n *\n * While this does not matter in most situations (no known case at the time of\n * writing), `key-tree` requires a specific specification to be used.\n *\n * @param curve - The curve to get the path prefix for. The curve is NOT\n * validated by this function.\n * @returns The path prefix, i.e., `bip32` or `slip10`.\n */\nexport function getPathPrefix(\n curve: SupportedCurve,\n): 'bip32' | 'slip10' | 'cip3' {\n switch (curve) {\n case 'secp256k1':\n return 'bip32';\n case 'ed25519':\n return 'slip10';\n case 'ed25519Bip32':\n return 'cip3';\n default:\n return assertExhaustive(curve);\n }\n}\n\ntype BaseGetNodeArgs = {\n curve: SupportedCurve;\n path: string[];\n cryptographicFunctions: CryptographicFunctions | undefined;\n};\n\ntype GetNodeArgsMnemonic = BaseGetNodeArgs & {\n secretRecoveryPhrase: Uint8Array;\n};\n\ntype GetNodeArgsSeed = BaseGetNodeArgs & {\n seed: Uint8Array;\n};\n\n/**\n * Get a `key-tree`-compatible node.\n *\n * Note: This function assumes that all the parameters have been validated\n * beforehand.\n *\n * @param options - The derivation options.\n * @param options.curve - The curve to use for derivation.\n * @param options.secretRecoveryPhrase - The secret recovery phrase to use for\n * derivation.\n * @param options.path - The derivation path to use as array, starting with an\n * \"m\" as the first item.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the node.\n * @returns The `key-tree` SLIP-10 node.\n */\nexport async function getNodeFromMnemonic({\n curve,\n secretRecoveryPhrase,\n path,\n cryptographicFunctions,\n}: GetNodeArgsMnemonic) {\n const prefix = getPathPrefix(curve);\n\n return await SLIP10Node.fromDerivationPath(\n {\n curve,\n derivationPath: [\n secretRecoveryPhrase,\n ...(path.slice(1).map((index) => `${prefix}:${index}`) as\n | BIP32Node[]\n | SLIP10PathNode[]),\n ],\n },\n cryptographicFunctions,\n );\n}\n\n/**\n * Get a `key-tree`-compatible node.\n *\n * Note: This function assumes that all the parameters have been validated\n * beforehand.\n *\n * @param options - The derivation options.\n * @param options.curve - The curve to use for derivation.\n * @param options.seed - The BIP-39 to use for\n * derivation.\n * @param options.path - The derivation path to use as array, starting with an\n * \"m\" as the first item.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the node.\n * @returns The `key-tree` SLIP-10 node.\n */\nexport async function getNodeFromSeed({\n curve,\n seed,\n path,\n cryptographicFunctions,\n}: GetNodeArgsSeed) {\n const prefix = getPathPrefix(curve);\n\n return await SLIP10Node.fromSeed(\n {\n curve,\n derivationPath: [\n seed,\n ...(path.slice(1).map((index) => `${prefix}:${index}`) as\n | BIP32Node[]\n | SLIP10PathNode[]),\n ],\n },\n cryptographicFunctions,\n );\n}\n\n/**\n * Validate the key of a state object.\n *\n * @param key - The key to validate.\n * @returns `true` if the key is valid, `false` otherwise.\n */\nexport function isValidStateKey(key: string | undefined) {\n if (key === undefined) {\n return true;\n }\n\n return key.split('.').every((part) => part.length > 0);\n}\n\nexport const StateKeyStruct = refine(string(), 'state key', (value) => {\n if (!isValidStateKey(value)) {\n return 'Invalid state key. Each part of the key must be non-empty.';\n }\n\n return true;\n});\n\n/**\n * Get a value using the entropy source hooks: getMnemonic or getMnemonicSeed.\n * This function calls the passed hook and handles any errors that occur,\n * throwing formatted JSON-RPC errors.\n *\n * @param hook - The hook.\n * @param source - The entropy source to use.\n * @returns The secret recovery phrase.\n */\nexport async function getValueFromEntropySource(\n hook: (source?: string | undefined) => Promise<Uint8Array>,\n source?: string | undefined,\n): Promise<Uint8Array> {\n try {\n return await hook(source);\n } catch (error) {\n if (error instanceof Error) {\n throw rpcErrors.invalidParams({\n message: error.message,\n });\n }\n\n throw rpcErrors.internal({\n message: 'An unknown error occurred.',\n data: {\n error: error.toString(),\n },\n });\n }\n}\n\n/**\n * The permissions that allow a Snap to show UI. Snaps must have at least one\n * of these permissions to use the interface management RPC methods.\n */\nexport const UI_PERMISSIONS = [\n 'snap_dialog',\n 'snap_notify',\n SnapEndowments.HomePage,\n SnapEndowments.SettingsPage,\n SnapEndowments.TransactionInsight,\n SnapEndowments.SignatureInsight,\n] as const;\n\nexport const HD_KEYRING = 'HD Key Tree';\n\n/**\n * Get the mnemonic for a given entropy source. If no source is\n * provided, the primary HD keyring's mnemonic will be returned.\n *\n * @param messenger - The messenger.\n * @param source - The ID of the entropy source keyring.\n * @returns The mnemonic.\n */\nexport async function getMnemonic(\n messenger: Messenger<string, KeyringControllerWithKeyringAction>,\n source?: string | undefined,\n): Promise<Uint8Array> {\n if (!source) {\n const mnemonic = (await messenger.call(\n 'KeyringController:withKeyring',\n {\n type: HD_KEYRING,\n index: 0,\n },\n async ({ keyring }) => keyring.mnemonic,\n )) as Uint8Array | null;\n\n if (!mnemonic) {\n throw new Error('Primary keyring mnemonic unavailable.');\n }\n\n return mnemonic;\n }\n\n try {\n const keyringData = await messenger.call(\n 'KeyringController:withKeyring',\n {\n id: source,\n },\n async ({ keyring }) => ({\n type: keyring.type,\n mnemonic: keyring.mnemonic,\n }),\n );\n\n const { type, mnemonic } = keyringData as {\n type: string;\n mnemonic?: Uint8Array;\n };\n\n // The keyring isn't guaranteed to have a mnemonic (e.g.,\n // hardware wallets, which can't be used as entropy sources),\n // so we throw an error if it doesn't.\n assert(type === HD_KEYRING && mnemonic);\n\n return mnemonic;\n } catch {\n throw new Error(`Entropy source with ID \"${source}\" not found.`);\n }\n}\n\n/**\n * Get the mnemonic seed for a given entropy source. If no source is\n * provided, the primary HD keyring's mnemonic seed will be returned.\n *\n * @param messenger - The messenger.\n * @param source - The ID of the entropy source keyring.\n * @returns The mnemonic seed.\n */\nexport async function getMnemonicSeed(\n messenger: Messenger<string, KeyringControllerWithKeyringAction>,\n source?: string | undefined,\n): Promise<Uint8Array> {\n if (!source) {\n const seed = (await messenger.call(\n 'KeyringController:withKeyring',\n {\n type: HD_KEYRING,\n index: 0,\n },\n async ({ keyring }) => keyring.seed,\n )) as Uint8Array | null;\n\n if (!seed) {\n throw new Error('Primary keyring mnemonic unavailable.');\n }\n\n return seed;\n }\n\n try {\n const keyringData = await messenger.call(\n 'KeyringController:withKeyring',\n {\n id: source,\n },\n async ({ keyring }) => ({\n type: keyring.type,\n seed: keyring.seed,\n }),\n );\n\n const { type, seed } = keyringData as { type: string; seed?: Uint8Array };\n\n // The keyring isn't guaranteed to have a mnemonic (e.g.,\n // hardware wallets, which can't be used as entropy sources),\n // so we throw an error if it doesn't.\n assert(type === HD_KEYRING && seed);\n\n return seed;\n } catch {\n throw new Error(`Entropy source with ID \"${source}\" not found.`);\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/snaps-rpc-methods",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "16.0.0",
|
|
4
4
|
"description": "MetaMask Snaps JSON-RPC method implementations",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -58,9 +58,9 @@
|
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
60
|
"@metamask/key-tree": "^10.1.1",
|
|
61
|
-
"@metamask/permission-controller": "^
|
|
61
|
+
"@metamask/permission-controller": "^13.1.0",
|
|
62
62
|
"@metamask/rpc-errors": "^7.0.3",
|
|
63
|
-
"@metamask/snaps-sdk": "^11.1.
|
|
63
|
+
"@metamask/snaps-sdk": "^11.1.1",
|
|
64
64
|
"@metamask/snaps-utils": "^12.2.0",
|
|
65
65
|
"@metamask/superstruct": "^3.2.1",
|
|
66
66
|
"@metamask/utils": "^11.11.0",
|
|
@@ -69,9 +69,9 @@
|
|
|
69
69
|
},
|
|
70
70
|
"devDependencies": {
|
|
71
71
|
"@lavamoat/allow-scripts": "^4.0.0",
|
|
72
|
-
"@metamask/auto-changelog": "^
|
|
73
|
-
"@metamask/json-rpc-engine": "^10.
|
|
74
|
-
"@metamask/messenger": "^1.
|
|
72
|
+
"@metamask/auto-changelog": "^6.1.0",
|
|
73
|
+
"@metamask/json-rpc-engine": "^10.5.0",
|
|
74
|
+
"@metamask/messenger": "^1.2.0",
|
|
75
75
|
"@swc/core": "1.11.31",
|
|
76
76
|
"@swc/jest": "^0.2.38",
|
|
77
77
|
"@ts-bridge/cli": "^0.6.1",
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.handlers = exports.methodHandlers = void 0;
|
|
4
|
-
const cancelBackgroundEvent_1 = require("./cancelBackgroundEvent.cjs");
|
|
5
|
-
const clearState_1 = require("./clearState.cjs");
|
|
6
|
-
const closeWebSocket_1 = require("./closeWebSocket.cjs");
|
|
7
|
-
const createInterface_1 = require("./createInterface.cjs");
|
|
8
|
-
const endTrace_1 = require("./endTrace.cjs");
|
|
9
|
-
const getAllSnaps_1 = require("./getAllSnaps.cjs");
|
|
10
|
-
const getBackgroundEvents_1 = require("./getBackgroundEvents.cjs");
|
|
11
|
-
const getClientStatus_1 = require("./getClientStatus.cjs");
|
|
12
|
-
const getFile_1 = require("./getFile.cjs");
|
|
13
|
-
const getInterfaceContext_1 = require("./getInterfaceContext.cjs");
|
|
14
|
-
const getInterfaceState_1 = require("./getInterfaceState.cjs");
|
|
15
|
-
const getSnaps_1 = require("./getSnaps.cjs");
|
|
16
|
-
const getState_1 = require("./getState.cjs");
|
|
17
|
-
const getWebSockets_1 = require("./getWebSockets.cjs");
|
|
18
|
-
const invokeKeyring_1 = require("./invokeKeyring.cjs");
|
|
19
|
-
const invokeSnapSugar_1 = require("./invokeSnapSugar.cjs");
|
|
20
|
-
const listEntropySources_1 = require("./listEntropySources.cjs");
|
|
21
|
-
const openWebSocket_1 = require("./openWebSocket.cjs");
|
|
22
|
-
const requestSnaps_1 = require("./requestSnaps.cjs");
|
|
23
|
-
const resolveInterface_1 = require("./resolveInterface.cjs");
|
|
24
|
-
const scheduleBackgroundEvent_1 = require("./scheduleBackgroundEvent.cjs");
|
|
25
|
-
const sendWebSocketMessage_1 = require("./sendWebSocketMessage.cjs");
|
|
26
|
-
const setState_1 = require("./setState.cjs");
|
|
27
|
-
const startTrace_1 = require("./startTrace.cjs");
|
|
28
|
-
const trackError_1 = require("./trackError.cjs");
|
|
29
|
-
const trackEvent_1 = require("./trackEvent.cjs");
|
|
30
|
-
const updateInterface_1 = require("./updateInterface.cjs");
|
|
31
|
-
/* eslint-disable @typescript-eslint/naming-convention */
|
|
32
|
-
exports.methodHandlers = {
|
|
33
|
-
wallet_getAllSnaps: getAllSnaps_1.getAllSnapsHandler,
|
|
34
|
-
wallet_getSnaps: getSnaps_1.getSnapsHandler,
|
|
35
|
-
wallet_requestSnaps: requestSnaps_1.requestSnapsHandler,
|
|
36
|
-
wallet_invokeSnap: invokeSnapSugar_1.invokeSnapSugarHandler,
|
|
37
|
-
wallet_invokeKeyring: invokeKeyring_1.invokeKeyringHandler,
|
|
38
|
-
snap_clearState: clearState_1.clearStateHandler,
|
|
39
|
-
snap_getClientStatus: getClientStatus_1.getClientStatusHandler,
|
|
40
|
-
snap_getFile: getFile_1.getFileHandler,
|
|
41
|
-
snap_getState: getState_1.getStateHandler,
|
|
42
|
-
snap_createInterface: createInterface_1.createInterfaceHandler,
|
|
43
|
-
snap_updateInterface: updateInterface_1.updateInterfaceHandler,
|
|
44
|
-
snap_getInterfaceState: getInterfaceState_1.getInterfaceStateHandler,
|
|
45
|
-
snap_getInterfaceContext: getInterfaceContext_1.getInterfaceContextHandler,
|
|
46
|
-
snap_listEntropySources: listEntropySources_1.listEntropySourcesHandler,
|
|
47
|
-
snap_resolveInterface: resolveInterface_1.resolveInterfaceHandler,
|
|
48
|
-
snap_scheduleBackgroundEvent: scheduleBackgroundEvent_1.scheduleBackgroundEventHandler,
|
|
49
|
-
snap_cancelBackgroundEvent: cancelBackgroundEvent_1.cancelBackgroundEventHandler,
|
|
50
|
-
snap_getBackgroundEvents: getBackgroundEvents_1.getBackgroundEventsHandler,
|
|
51
|
-
snap_setState: setState_1.setStateHandler,
|
|
52
|
-
snap_trackError: trackError_1.trackErrorHandler,
|
|
53
|
-
snap_trackEvent: trackEvent_1.trackEventHandler,
|
|
54
|
-
snap_openWebSocket: openWebSocket_1.openWebSocketHandler,
|
|
55
|
-
snap_closeWebSocket: closeWebSocket_1.closeWebSocketHandler,
|
|
56
|
-
snap_sendWebSocketMessage: sendWebSocketMessage_1.sendWebSocketMessageHandler,
|
|
57
|
-
snap_getWebSockets: getWebSockets_1.getWebSocketsHandler,
|
|
58
|
-
snap_startTrace: startTrace_1.startTraceHandler,
|
|
59
|
-
snap_endTrace: endTrace_1.endTraceHandler,
|
|
60
|
-
};
|
|
61
|
-
/* eslint-enable @typescript-eslint/naming-convention */
|
|
62
|
-
exports.handlers = Object.values(exports.methodHandlers);
|
|
63
|
-
//# sourceMappingURL=handlers.cjs.map
|