@metamask/snaps-rpc-methods 16.0.0 → 17.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 +10 -2
- package/dist/restricted/getBip32Entropy.cjs +1 -1
- package/dist/restricted/getBip32Entropy.cjs.map +1 -1
- package/dist/restricted/getBip32Entropy.d.cts +3 -3
- package/dist/restricted/getBip32Entropy.d.cts.map +1 -1
- package/dist/restricted/getBip32Entropy.d.mts +3 -3
- package/dist/restricted/getBip32Entropy.d.mts.map +1 -1
- package/dist/restricted/getBip32Entropy.mjs +1 -1
- package/dist/restricted/getBip32Entropy.mjs.map +1 -1
- package/dist/restricted/getBip32PublicKey.cjs +1 -1
- package/dist/restricted/getBip32PublicKey.cjs.map +1 -1
- package/dist/restricted/getBip32PublicKey.d.cts +3 -3
- package/dist/restricted/getBip32PublicKey.d.cts.map +1 -1
- package/dist/restricted/getBip32PublicKey.d.mts +3 -3
- package/dist/restricted/getBip32PublicKey.d.mts.map +1 -1
- package/dist/restricted/getBip32PublicKey.mjs +1 -1
- package/dist/restricted/getBip32PublicKey.mjs.map +1 -1
- package/dist/restricted/getBip44Entropy.cjs +1 -1
- package/dist/restricted/getBip44Entropy.cjs.map +1 -1
- package/dist/restricted/getBip44Entropy.d.cts +3 -3
- package/dist/restricted/getBip44Entropy.d.cts.map +1 -1
- package/dist/restricted/getBip44Entropy.d.mts +3 -3
- package/dist/restricted/getBip44Entropy.d.mts.map +1 -1
- package/dist/restricted/getBip44Entropy.mjs +1 -1
- package/dist/restricted/getBip44Entropy.mjs.map +1 -1
- package/dist/restricted/getEntropy.cjs +1 -1
- package/dist/restricted/getEntropy.cjs.map +1 -1
- package/dist/restricted/getEntropy.d.cts +3 -3
- package/dist/restricted/getEntropy.d.cts.map +1 -1
- package/dist/restricted/getEntropy.d.mts +3 -3
- package/dist/restricted/getEntropy.d.mts.map +1 -1
- package/dist/restricted/getEntropy.mjs +1 -1
- package/dist/restricted/getEntropy.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +3 -3
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +3 -3
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils.cjs +5 -5
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +4 -4
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +4 -4
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +5 -5
- package/dist/utils.mjs.map +1 -1
- package/package.json +4 -4
package/dist/utils.d.mts
CHANGED
|
@@ -3,7 +3,7 @@ import { SLIP10Node } from "@metamask/key-tree";
|
|
|
3
3
|
import type { Messenger } from "@metamask/messenger";
|
|
4
4
|
import type { MagicValue } from "@metamask/snaps-utils";
|
|
5
5
|
import { SnapEndowments } from "./endowments/index.mjs";
|
|
6
|
-
import type {
|
|
6
|
+
import type { KeyringControllerWithKeyringV2UnsafeAction } from "./types.mjs";
|
|
7
7
|
export declare const FORBIDDEN_KEYS: string[];
|
|
8
8
|
/**
|
|
9
9
|
* Maps an interface with method hooks to an object, using the keys of the
|
|
@@ -140,7 +140,7 @@ export declare function getValueFromEntropySource(hook: (source?: string | undef
|
|
|
140
140
|
* of these permissions to use the interface management RPC methods.
|
|
141
141
|
*/
|
|
142
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 = "
|
|
143
|
+
export declare const HD_KEYRING = "hd";
|
|
144
144
|
/**
|
|
145
145
|
* Get the mnemonic for a given entropy source. If no source is
|
|
146
146
|
* provided, the primary HD keyring's mnemonic will be returned.
|
|
@@ -149,7 +149,7 @@ export declare const HD_KEYRING = "HD Key Tree";
|
|
|
149
149
|
* @param source - The ID of the entropy source keyring.
|
|
150
150
|
* @returns The mnemonic.
|
|
151
151
|
*/
|
|
152
|
-
export declare function getMnemonic(messenger: Messenger<string,
|
|
152
|
+
export declare function getMnemonic(messenger: Messenger<string, KeyringControllerWithKeyringV2UnsafeAction>, source?: string | undefined): Promise<Uint8Array>;
|
|
153
153
|
/**
|
|
154
154
|
* Get the mnemonic seed for a given entropy source. If no source is
|
|
155
155
|
* provided, the primary HD keyring's mnemonic seed will be returned.
|
|
@@ -158,6 +158,6 @@ export declare function getMnemonic(messenger: Messenger<string, KeyringControll
|
|
|
158
158
|
* @param source - The ID of the entropy source keyring.
|
|
159
159
|
* @returns The mnemonic seed.
|
|
160
160
|
*/
|
|
161
|
-
export declare function getMnemonicSeed(messenger: Messenger<string,
|
|
161
|
+
export declare function getMnemonicSeed(messenger: Messenger<string, KeyringControllerWithKeyringV2UnsafeAction>, source?: string | undefined): Promise<Uint8Array>;
|
|
162
162
|
export {};
|
|
163
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;AAChD,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,UAAU,EAAE,8BAA8B;AAYxD,OAAO,EAAE,cAAc,EAAE,+BAAqB;AAC9C,OAAO,KAAK,EAAE,
|
|
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,OA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AC,EACxC,KAAK,EACL,oBAAoB,EACpB,IAAI,EACJ,sBAAsB,GACvB,EAAE,mBAAmB,uBAerB;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,eAAe,CAAC,EACpC,KAAK,EACL,IAAI,EACJ,IAAI,EACJ,sBAAsB,GACvB,EAAE,eAAe,uBAejB;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,WAMtD;AAED,eAAO,MAAM,cAAc,sDAMzB,CAAC;AAEH;;;;;;;;GAQG;AACH,wBAAsB,yBAAyB,CAC7C,IAAI,EAAE,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,KAAK,OAAO,CAAC,UAAU,CAAC,EAC1D,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,GAC1B,OAAO,CAAC,UAAU,CAAC,CAiBrB;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,mKAOjB,CAAC;AAEX,eAAO,MAAM,UAAU,OAAO,CAAC;AAE/B;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAC/B,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,0CAA0C,CAAC,EACxE,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,GAC1B,OAAO,CAAC,UAAU,CAAC,CA4CrB;AAED;;;;;;;GAOG;AACH,wBAAsB,eAAe,CACnC,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,0CAA0C,CAAC,EACxE,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,GAC1B,OAAO,CAAC,UAAU,CAAC,CAyCrB"}
|
package/dist/utils.mjs
CHANGED
|
@@ -216,7 +216,7 @@ export const UI_PERMISSIONS = [
|
|
|
216
216
|
SnapEndowments.TransactionInsight,
|
|
217
217
|
SnapEndowments.SignatureInsight,
|
|
218
218
|
];
|
|
219
|
-
export const HD_KEYRING = '
|
|
219
|
+
export const HD_KEYRING = 'hd';
|
|
220
220
|
/**
|
|
221
221
|
* Get the mnemonic for a given entropy source. If no source is
|
|
222
222
|
* provided, the primary HD keyring's mnemonic will be returned.
|
|
@@ -227,7 +227,7 @@ export const HD_KEYRING = 'HD Key Tree';
|
|
|
227
227
|
*/
|
|
228
228
|
export async function getMnemonic(messenger, source) {
|
|
229
229
|
if (!source) {
|
|
230
|
-
const mnemonic = (await messenger.call('KeyringController:
|
|
230
|
+
const mnemonic = (await messenger.call('KeyringController:withKeyringV2Unsafe', {
|
|
231
231
|
type: HD_KEYRING,
|
|
232
232
|
index: 0,
|
|
233
233
|
}, async ({ keyring }) => keyring.mnemonic));
|
|
@@ -237,7 +237,7 @@ export async function getMnemonic(messenger, source) {
|
|
|
237
237
|
return mnemonic;
|
|
238
238
|
}
|
|
239
239
|
try {
|
|
240
|
-
const keyringData = await messenger.call('KeyringController:
|
|
240
|
+
const keyringData = await messenger.call('KeyringController:withKeyringV2Unsafe', {
|
|
241
241
|
id: source,
|
|
242
242
|
}, async ({ keyring }) => ({
|
|
243
243
|
type: keyring.type,
|
|
@@ -264,7 +264,7 @@ export async function getMnemonic(messenger, source) {
|
|
|
264
264
|
*/
|
|
265
265
|
export async function getMnemonicSeed(messenger, source) {
|
|
266
266
|
if (!source) {
|
|
267
|
-
const seed = (await messenger.call('KeyringController:
|
|
267
|
+
const seed = (await messenger.call('KeyringController:withKeyringV2Unsafe', {
|
|
268
268
|
type: HD_KEYRING,
|
|
269
269
|
index: 0,
|
|
270
270
|
}, async ({ keyring }) => keyring.seed));
|
|
@@ -274,7 +274,7 @@ export async function getMnemonicSeed(messenger, source) {
|
|
|
274
274
|
return seed;
|
|
275
275
|
}
|
|
276
276
|
try {
|
|
277
|
-
const keyringData = await messenger.call('KeyringController:
|
|
277
|
+
const keyringData = await messenger.call('KeyringController:withKeyringV2Unsafe', {
|
|
278
278
|
id: source,
|
|
279
279
|
}, async ({ keyring }) => ({
|
|
280
280
|
type: keyring.type,
|
package/dist/utils.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3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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"]}
|
|
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,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nB,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 { KeyringControllerWithKeyringV2UnsafeAction } 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';\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, KeyringControllerWithKeyringV2UnsafeAction>,\n source?: string | undefined,\n): Promise<Uint8Array> {\n if (!source) {\n const mnemonic = (await messenger.call(\n 'KeyringController:withKeyringV2Unsafe',\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:withKeyringV2Unsafe',\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, KeyringControllerWithKeyringV2UnsafeAction>,\n source?: string | undefined,\n): Promise<Uint8Array> {\n if (!source) {\n const seed = (await messenger.call(\n 'KeyringController:withKeyringV2Unsafe',\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:withKeyringV2Unsafe',\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": "17.0.0",
|
|
4
4
|
"description": "MetaMask Snaps JSON-RPC method implementations",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -58,10 +58,10 @@
|
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
60
|
"@metamask/key-tree": "^10.1.1",
|
|
61
|
-
"@metamask/permission-controller": "^13.1.
|
|
61
|
+
"@metamask/permission-controller": "^13.1.1",
|
|
62
62
|
"@metamask/rpc-errors": "^7.0.3",
|
|
63
63
|
"@metamask/snaps-sdk": "^11.1.1",
|
|
64
|
-
"@metamask/snaps-utils": "^12.2.
|
|
64
|
+
"@metamask/snaps-utils": "^12.2.1",
|
|
65
65
|
"@metamask/superstruct": "^3.2.1",
|
|
66
66
|
"@metamask/utils": "^11.11.0",
|
|
67
67
|
"@noble/hashes": "^1.7.1",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
},
|
|
70
70
|
"devDependencies": {
|
|
71
71
|
"@lavamoat/allow-scripts": "^4.0.0",
|
|
72
|
-
"@metamask/auto-changelog": "^6.1.
|
|
72
|
+
"@metamask/auto-changelog": "^6.1.1",
|
|
73
73
|
"@metamask/json-rpc-engine": "^10.5.0",
|
|
74
74
|
"@metamask/messenger": "^1.2.0",
|
|
75
75
|
"@swc/core": "1.11.31",
|