@metamask/key-tree
Advanced tools
Comparing version 6.1.0 to 6.2.0
@@ -16,6 +16,7 @@ "use strict"; | ||
exports.validateBIP44Depth = exports.BIP44Node = void 0; | ||
const utils_1 = require("@metamask/utils"); | ||
const constants_1 = require("./constants"); | ||
const extended_keys_1 = require("./extended-keys"); | ||
const SLIP10Node_1 = require("./SLIP10Node"); | ||
const utils_1 = require("./utils"); | ||
const utils_2 = require("./utils"); | ||
/** | ||
@@ -268,9 +269,12 @@ * A wrapper for BIP-44 Hierarchical Deterministic (HD) tree nodes, i.e. | ||
const currentDepth = startingDepth + index; | ||
if (currentDepth === constants_1.MIN_BIP_44_DEPTH) { | ||
if (!(nodeToken instanceof Uint8Array) && | ||
!constants_1.BIP_39_PATH_REGEX.test(nodeToken)) { | ||
throw new Error('Invalid derivation path: The "m" / seed node (depth 0) must be a BIP-39 node.'); | ||
} | ||
return; | ||
} | ||
(0, utils_1.assert)(typeof nodeToken === 'string'); | ||
// eslint-disable-next-line default-case | ||
switch (currentDepth) { | ||
case constants_1.MIN_BIP_44_DEPTH: | ||
if (!constants_1.BIP_39_PATH_REGEX.test(nodeToken)) { | ||
throw new Error('Invalid derivation path: The "m" / seed node (depth 0) must be a BIP-39 node.'); | ||
} | ||
break; | ||
case 1: | ||
@@ -282,3 +286,3 @@ if (nodeToken !== constants_1.BIP44PurposeNodeToken) { | ||
case 2: | ||
if (!constants_1.BIP_32_PATH_REGEX.test(nodeToken) || !(0, utils_1.isHardened)(nodeToken)) { | ||
if (!constants_1.BIP_32_PATH_REGEX.test(nodeToken) || !(0, utils_2.isHardened)(nodeToken)) { | ||
throw new Error('Invalid derivation path: The "coin_type" node (depth 2) must be a hardened BIP-32 node.'); | ||
@@ -288,3 +292,3 @@ } | ||
case 3: | ||
if (!constants_1.BIP_32_PATH_REGEX.test(nodeToken) || !(0, utils_1.isHardened)(nodeToken)) { | ||
if (!constants_1.BIP_32_PATH_REGEX.test(nodeToken) || !(0, utils_2.isHardened)(nodeToken)) { | ||
throw new Error('Invalid derivation path: The "account" node (depth 3) must be a hardened BIP-32 node.'); | ||
@@ -291,0 +295,0 @@ } |
@@ -8,3 +8,4 @@ export declare const BYTES_KEY_LENGTH = 32; | ||
export declare type AnonymizedBIP39Node = 'm'; | ||
export declare type BIP39Node = `bip39:${string}`; | ||
export declare type BIP39StringNode = `bip39:${string}`; | ||
export declare type BIP39Node = BIP39StringNode | Uint8Array; | ||
export declare type HardenedBIP32Node = `bip32:${number}'`; | ||
@@ -11,0 +12,0 @@ export declare type UnhardenedBIP32Node = `bip32:${number}`; |
@@ -45,13 +45,22 @@ "use strict"; | ||
validatePathSegment(path, Boolean(node === null || node === void 0 ? void 0 : node.privateKey) || Boolean(node === null || node === void 0 ? void 0 : node.publicKey), depth); | ||
// derive through each part of path | ||
// `pathSegment` needs to be cast to `string[]` because `HDPathTuple.reduce()` doesn't work | ||
return await path.reduce(async (promise, pathNode) => { | ||
// Derive through each part of path. `pathSegment` needs to be cast because | ||
// `HDPathTuple.reduce()` doesn't work. Note that the first element of the | ||
// path can be a Uint8Array. | ||
return await path.reduce(async (promise, pathNode, index) => { | ||
const derivedNode = await promise; | ||
const [pathType, pathPart] = pathNode.split(':'); | ||
(0, utils_1.assert)(hasDeriver(pathType), `Unknown derivation type: "${pathType}".`); | ||
const deriver = derivers_1.derivers[pathType]; | ||
return await deriver.deriveChildKey({ | ||
path: pathPart, | ||
if (typeof pathNode === 'string') { | ||
const [pathType, pathPart] = pathNode.split(':'); | ||
(0, utils_1.assert)(hasDeriver(pathType), `Unknown derivation type: "${pathType}".`); | ||
const deriver = derivers_1.derivers[pathType]; | ||
return await deriver.deriveChildKey({ | ||
path: pathPart, | ||
node: derivedNode, | ||
curve: (0, curves_1.getCurveByName)(curve), | ||
}); | ||
} | ||
// Only the first path segment can be a Uint8Array. | ||
(0, utils_1.assert)(index === 0, getMalformedError()); | ||
return await derivers_1.derivers.bip39.deriveChildKey({ | ||
path: pathNode, | ||
node: derivedNode, | ||
curve: (0, curves_1.getCurveByName)(curve), | ||
}); | ||
@@ -87,8 +96,14 @@ }, Promise.resolve(node)); | ||
if (index === 0) { | ||
startsWithBip39 = constants_1.BIP_39_PATH_REGEX.test(node); | ||
if (!startsWithBip39 && !constants_1.BIP_32_PATH_REGEX.test(node)) { | ||
startsWithBip39 = | ||
node instanceof Uint8Array || constants_1.BIP_39_PATH_REGEX.test(node); | ||
if ( | ||
// TypeScript is unable to infer that `node` is a string here, so we | ||
// need to explicitly check it again. | ||
!(node instanceof Uint8Array) && | ||
!startsWithBip39 && | ||
!constants_1.BIP_32_PATH_REGEX.test(node)) { | ||
throw getMalformedError(); | ||
} | ||
} | ||
else if (!constants_1.BIP_32_PATH_REGEX.test(node)) { | ||
else if (node instanceof Uint8Array || !constants_1.BIP_32_PATH_REGEX.test(node)) { | ||
throw getMalformedError(); | ||
@@ -95,0 +110,0 @@ } |
@@ -57,2 +57,3 @@ "use strict"; | ||
async function deriveChildKey({ path, node, curve = curves_1.secp256k1, }) { | ||
(0, utils_1.assert)(typeof path === 'string', 'Invalid path: Must be a string.'); | ||
const isHardened = path.includes(`'`); | ||
@@ -59,0 +60,0 @@ if (!isHardened && !curve.deriveUnhardenedKeys) { |
import { DeriveChildKeyArgs } from '.'; | ||
import { BIP39Node } from '../constants'; | ||
import { BIP39StringNode } from '../constants'; | ||
import { Curve } from '../curves'; | ||
@@ -11,3 +11,3 @@ import { SLIP10Node } from '../SLIP10Node'; | ||
*/ | ||
export declare function bip39MnemonicToMultipath(mnemonic: string): BIP39Node; | ||
export declare function bip39MnemonicToMultipath(mnemonic: string): BIP39StringNode; | ||
/** | ||
@@ -14,0 +14,0 @@ * Create a {@link SLIP10Node} from a BIP-39 mnemonic phrase. |
@@ -14,3 +14,3 @@ import { Curve } from '../curves'; | ||
export declare type DeriveChildKeyArgs = { | ||
path: string; | ||
path: Uint8Array | string; | ||
curve?: Curve; | ||
@@ -17,0 +17,0 @@ node?: SLIP10Node; |
@@ -149,2 +149,13 @@ import { BIP32Node, ChangeHDPathString, CoinTypeHDPathString, CoinTypeToAddressTuple, HardenedBIP32Node, UnhardenedBIP32Node } from './constants'; | ||
export declare const getFingerprint: (publicKey: Uint8Array) => number; | ||
/** | ||
* Get a secret recovery phrase (or mnemonic phrase) in string form as a | ||
* `Uint8Array`. The secret recovery phrase is split into words, and each word | ||
* is converted to a number using the BIP-39 word list. The numbers are then | ||
* converted to bytes, and the bytes are concatenated into a single | ||
* `Uint8Array`. | ||
* | ||
* @param mnemonicPhrase - The secret recovery phrase to convert. | ||
* @returns The `Uint8Array` corresponding to the secret recovery phrase. | ||
*/ | ||
export declare function mnemonicPhraseToBytes(mnemonicPhrase: string): Uint8Array; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getFingerprint = exports.encodeBase58check = exports.decodeBase58check = exports.getBytes = exports.isValidInteger = exports.isValidBytesKey = exports.nullableHexStringToBytes = exports.hexStringToBytes = exports.isHardened = exports.isValidBIP32Index = exports.validateBIP32Index = exports.getBIP32NodeToken = exports.getUnhardenedBIP32NodeToken = exports.getHardenedBIP32NodeToken = exports.getBIP44CoinTypeToAddressPathTuple = exports.getBIP44ChangePathString = exports.getBIP44CoinTypePathString = void 0; | ||
exports.mnemonicPhraseToBytes = exports.getFingerprint = exports.encodeBase58check = exports.decodeBase58check = exports.getBytes = exports.isValidInteger = exports.isValidBytesKey = exports.nullableHexStringToBytes = exports.hexStringToBytes = exports.isHardened = exports.isValidBIP32Index = exports.validateBIP32Index = exports.getBIP32NodeToken = exports.getUnhardenedBIP32NodeToken = exports.getHardenedBIP32NodeToken = exports.getBIP44CoinTypeToAddressPathTuple = exports.getBIP44ChangePathString = exports.getBIP44CoinTypePathString = void 0; | ||
const english_1 = require("@metamask/scure-bip39/dist/wordlists/english"); | ||
const utils_1 = require("@metamask/utils"); | ||
@@ -264,2 +265,22 @@ const ripemd160_1 = require("@noble/hashes/ripemd160"); | ||
exports.getFingerprint = getFingerprint; | ||
/** | ||
* Get a secret recovery phrase (or mnemonic phrase) in string form as a | ||
* `Uint8Array`. The secret recovery phrase is split into words, and each word | ||
* is converted to a number using the BIP-39 word list. The numbers are then | ||
* converted to bytes, and the bytes are concatenated into a single | ||
* `Uint8Array`. | ||
* | ||
* @param mnemonicPhrase - The secret recovery phrase to convert. | ||
* @returns The `Uint8Array` corresponding to the secret recovery phrase. | ||
*/ | ||
function mnemonicPhraseToBytes(mnemonicPhrase) { | ||
const words = mnemonicPhrase.split(' '); | ||
const indices = words.map((word) => { | ||
const index = english_1.wordlist.indexOf(word); | ||
(0, utils_1.assert)(index !== -1, `Invalid mnemonic phrase: Unknown word "${word}".`); | ||
return index; | ||
}); | ||
return new Uint8Array(new Uint16Array(indices).buffer); | ||
} | ||
exports.mnemonicPhraseToBytes = mnemonicPhraseToBytes; | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "@metamask/key-tree", | ||
"version": "6.1.0", | ||
"version": "6.2.0", | ||
"description": "An interface over BIP-32 and BIP-39 key derivation paths.", | ||
@@ -5,0 +5,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
258173
3008