@metamask/key-tree
Advanced tools
Comparing version 5.0.2 to 6.0.0
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" /> | ||
import { BIP39Node, BIP44Depth, BIP44PurposeNodeToken, CoinTypeHDPathString, HardenedBIP32Node } from './constants'; | ||
@@ -96,9 +95,9 @@ import { BIP44Node, BIP44NodeInterface, JsonBIP44Node } from './BIP44Node'; | ||
get depth(): BIP44Depth; | ||
get privateKeyBuffer(): Buffer | undefined; | ||
get publicKeyBuffer(): Buffer; | ||
get chainCodeBuffer(): Buffer; | ||
get privateKeyBytes(): Uint8Array | undefined; | ||
get publicKeyBytes(): Uint8Array; | ||
get chainCodeBytes(): Uint8Array; | ||
get privateKey(): string | undefined; | ||
get publicKey(): string; | ||
get compressedPublicKey(): string; | ||
get compressedPublicKeyBuffer(): Buffer; | ||
get compressedPublicKeyBytes(): Uint8Array; | ||
get chainCode(): string; | ||
@@ -153,3 +152,3 @@ get address(): string; | ||
export declare function deriveBIP44AddressKey(parentKeyOrNode: BIP44CoinTypeNode | JsonBIP44CoinTypeNode | string, { account, change, address_index }: CoinTypeToAddressIndices): Promise<BIP44Node>; | ||
declare type BIP44AddressKeyDeriver = { | ||
export declare type BIP44AddressKeyDeriver = { | ||
/** | ||
@@ -201,2 +200,1 @@ * @param address_index - The `address_index` value. | ||
export declare function getBIP44AddressKeyDeriver(node: BIP44CoinTypeNode | JsonBIP44CoinTypeNode | string, accountAndChangeIndices?: Omit<CoinTypeToAddressIndices, 'address_index'>): Promise<BIP44AddressKeyDeriver>; | ||
export {}; |
@@ -41,3 +41,3 @@ "use strict"; | ||
this.coin_type = coin_type; | ||
this.path = utils_1.getBIP44CoinTypePathString(coin_type); | ||
this.path = (0, utils_1.getBIP44CoinTypePathString)(coin_type); | ||
Object.freeze(this); | ||
@@ -70,5 +70,5 @@ } | ||
parentFingerprint: json.parentFingerprint, | ||
chainCode: utils_1.hexStringToBuffer(json.chainCode), | ||
privateKey: utils_1.nullableHexStringToBuffer(json.privateKey), | ||
publicKey: utils_1.hexStringToBuffer(json.publicKey), | ||
chainCode: (0, utils_1.hexStringToBytes)(json.chainCode), | ||
privateKey: (0, utils_1.nullableHexStringToBytes)(json.privateKey), | ||
publicKey: (0, utils_1.hexStringToBytes)(json.publicKey), | ||
}); | ||
@@ -131,10 +131,10 @@ return new BIP44CoinTypeNode(node, coin_type); | ||
} | ||
get privateKeyBuffer() { | ||
return __classPrivateFieldGet(this, _BIP44CoinTypeNode_node, "f").privateKeyBuffer; | ||
get privateKeyBytes() { | ||
return __classPrivateFieldGet(this, _BIP44CoinTypeNode_node, "f").privateKeyBytes; | ||
} | ||
get publicKeyBuffer() { | ||
return __classPrivateFieldGet(this, _BIP44CoinTypeNode_node, "f").publicKeyBuffer; | ||
get publicKeyBytes() { | ||
return __classPrivateFieldGet(this, _BIP44CoinTypeNode_node, "f").publicKeyBytes; | ||
} | ||
get chainCodeBuffer() { | ||
return __classPrivateFieldGet(this, _BIP44CoinTypeNode_node, "f").chainCodeBuffer; | ||
get chainCodeBytes() { | ||
return __classPrivateFieldGet(this, _BIP44CoinTypeNode_node, "f").chainCodeBytes; | ||
} | ||
@@ -150,4 +150,4 @@ get privateKey() { | ||
} | ||
get compressedPublicKeyBuffer() { | ||
return __classPrivateFieldGet(this, _BIP44CoinTypeNode_node, "f").compressedPublicKeyBuffer; | ||
get compressedPublicKeyBytes() { | ||
return __classPrivateFieldGet(this, _BIP44CoinTypeNode_node, "f").compressedPublicKeyBytes; | ||
} | ||
@@ -199,3 +199,3 @@ get chainCode() { | ||
async deriveBIP44AddressKey({ account = 0, change = 0, address_index, }) { | ||
return await __classPrivateFieldGet(this, _BIP44CoinTypeNode_node, "f").derive(utils_1.getBIP44CoinTypeToAddressPathTuple({ account, change, address_index })); | ||
return await __classPrivateFieldGet(this, _BIP44CoinTypeNode_node, "f").derive((0, utils_1.getBIP44CoinTypeToAddressPathTuple)({ account, change, address_index })); | ||
} | ||
@@ -250,3 +250,3 @@ toJSON() { | ||
async function deriveBIP44AddressKey(parentKeyOrNode, { account = 0, change = 0, address_index }) { | ||
const path = utils_1.getBIP44CoinTypeToAddressPathTuple({ | ||
const path = (0, utils_1.getBIP44CoinTypeToAddressPathTuple)({ | ||
account, | ||
@@ -257,3 +257,3 @@ change, | ||
const node = await getNode(parentKeyOrNode); | ||
const childNode = await SLIP10Node_1.deriveChildNode({ | ||
const childNode = await (0, SLIP10Node_1.deriveChildNode)({ | ||
path, | ||
@@ -288,6 +288,6 @@ node, | ||
const actualNode = await getNode(node); | ||
const accountNode = utils_1.getHardenedBIP32NodeToken(account); | ||
const changeNode = utils_1.getBIP32NodeToken(change); | ||
const accountNode = (0, utils_1.getHardenedBIP32NodeToken)(account); | ||
const changeNode = (0, utils_1.getBIP32NodeToken)(change); | ||
const bip44AddressKeyDeriver = async (address_index, isHardened = false) => { | ||
const slip10Node = await SLIP10Node_1.deriveChildNode({ | ||
const slip10Node = await (0, SLIP10Node_1.deriveChildNode)({ | ||
path: [ | ||
@@ -297,4 +297,4 @@ accountNode, | ||
isHardened | ||
? utils_1.getHardenedBIP32NodeToken(address_index) | ||
: utils_1.getUnhardenedBIP32NodeToken(address_index), | ||
? (0, utils_1.getHardenedBIP32NodeToken)(address_index) | ||
: (0, utils_1.getUnhardenedBIP32NodeToken)(address_index), | ||
], | ||
@@ -306,3 +306,3 @@ node: actualNode, | ||
bip44AddressKeyDeriver.coin_type = actualNode.coin_type; | ||
bip44AddressKeyDeriver.path = utils_1.getBIP44ChangePathString(actualNode.path, { | ||
bip44AddressKeyDeriver.path = (0, utils_1.getBIP44ChangePathString)(actualNode.path, { | ||
account, | ||
@@ -309,0 +309,0 @@ change, |
@@ -1,14 +0,13 @@ | ||
/// <reference types="node" /> | ||
import { BIP44Depth, PartialHDPathTuple, RootedSLIP10PathTuple } from './constants'; | ||
import { SLIP10Node } from './SLIP10Node'; | ||
import { SupportedCurve } from './curves'; | ||
declare type BIP44ExtendedKeyOptions = { | ||
export declare type BIP44ExtendedKeyOptions = { | ||
readonly depth: number; | ||
readonly parentFingerprint: number; | ||
readonly index: number; | ||
readonly chainCode: Buffer | string; | ||
readonly privateKey?: Buffer | string; | ||
readonly publicKey?: Buffer | string; | ||
readonly chainCode: Uint8Array | string; | ||
readonly privateKey?: Uint8Array | string; | ||
readonly publicKey?: Uint8Array | string; | ||
}; | ||
declare type BIP44DerivationPathOptions = { | ||
export declare type BIP44DerivationPathOptions = { | ||
readonly derivationPath: RootedSLIP10PathTuple; | ||
@@ -123,9 +122,9 @@ }; | ||
get depth(): BIP44Depth; | ||
get privateKeyBuffer(): Buffer | undefined; | ||
get publicKeyBuffer(): Buffer; | ||
get chainCodeBuffer(): Buffer; | ||
get privateKeyBytes(): Uint8Array | undefined; | ||
get publicKeyBytes(): Uint8Array; | ||
get chainCodeBytes(): Uint8Array; | ||
get privateKey(): string | undefined; | ||
get publicKey(): string; | ||
get compressedPublicKey(): string; | ||
get compressedPublicKeyBuffer(): Buffer; | ||
get compressedPublicKeyBytes(): Uint8Array; | ||
get chainCode(): string; | ||
@@ -175,2 +174,1 @@ get address(): string; | ||
export declare function validateBIP44Depth(depth: unknown): asserts depth is BIP44Depth; | ||
export {}; |
@@ -61,3 +61,3 @@ "use strict"; | ||
if (typeof options === 'string') { | ||
const extendedKey = extended_keys_1.decodeExtendedKey(options); | ||
const extendedKey = (0, extended_keys_1.decodeExtendedKey)(options); | ||
const { chainCode, depth, parentFingerprint, index } = extendedKey; | ||
@@ -128,10 +128,10 @@ if (extendedKey.version === extended_keys_1.PRIVATE_KEY_VERSION) { | ||
} | ||
get privateKeyBuffer() { | ||
return __classPrivateFieldGet(this, _BIP44Node_node, "f").privateKeyBuffer; | ||
get privateKeyBytes() { | ||
return __classPrivateFieldGet(this, _BIP44Node_node, "f").privateKeyBytes; | ||
} | ||
get publicKeyBuffer() { | ||
return __classPrivateFieldGet(this, _BIP44Node_node, "f").publicKeyBuffer; | ||
get publicKeyBytes() { | ||
return __classPrivateFieldGet(this, _BIP44Node_node, "f").publicKeyBytes; | ||
} | ||
get chainCodeBuffer() { | ||
return __classPrivateFieldGet(this, _BIP44Node_node, "f").chainCodeBuffer; | ||
get chainCodeBytes() { | ||
return __classPrivateFieldGet(this, _BIP44Node_node, "f").chainCodeBytes; | ||
} | ||
@@ -147,4 +147,4 @@ get privateKey() { | ||
} | ||
get compressedPublicKeyBuffer() { | ||
return __classPrivateFieldGet(this, _BIP44Node_node, "f").compressedPublicKeyBuffer; | ||
get compressedPublicKeyBytes() { | ||
return __classPrivateFieldGet(this, _BIP44Node_node, "f").compressedPublicKeyBytes; | ||
} | ||
@@ -174,8 +174,8 @@ get chainCode() { | ||
index: this.index, | ||
chainCode: this.chainCodeBuffer, | ||
chainCode: this.chainCodeBytes, | ||
}; | ||
if (this.privateKeyBuffer) { | ||
return extended_keys_1.encodeExtendedKey(Object.assign(Object.assign({}, data), { version: extended_keys_1.PRIVATE_KEY_VERSION, privateKey: this.privateKeyBuffer })); | ||
if (this.privateKeyBytes) { | ||
return (0, extended_keys_1.encodeExtendedKey)(Object.assign(Object.assign({}, data), { version: extended_keys_1.PRIVATE_KEY_VERSION, privateKey: this.privateKeyBytes })); | ||
} | ||
return extended_keys_1.encodeExtendedKey(Object.assign(Object.assign({}, data), { version: extended_keys_1.PUBLIC_KEY_VERSION, publicKey: this.publicKeyBuffer })); | ||
return (0, extended_keys_1.encodeExtendedKey)(Object.assign(Object.assign({}, data), { version: extended_keys_1.PUBLIC_KEY_VERSION, publicKey: this.publicKeyBytes })); | ||
} | ||
@@ -245,3 +245,3 @@ get curve() { | ||
function validateBIP44Depth(depth) { | ||
SLIP10Node_1.validateBIP32Depth(depth); | ||
(0, SLIP10Node_1.validateBIP32Depth)(depth); | ||
if (depth < constants_1.MIN_BIP_44_DEPTH || depth > constants_1.MAX_BIP_44_DEPTH) { | ||
@@ -269,2 +269,3 @@ throw new Error(`Invalid HD tree path depth: The depth must be a positive integer N such that 0 <= N <= 5. Received: "${depth}"`); | ||
const currentDepth = startingDepth + index; | ||
// eslint-disable-next-line default-case | ||
switch (currentDepth) { | ||
@@ -282,3 +283,3 @@ case constants_1.MIN_BIP_44_DEPTH: | ||
case 2: | ||
if (!constants_1.BIP_32_PATH_REGEX.test(nodeToken) || !utils_1.isHardened(nodeToken)) { | ||
if (!constants_1.BIP_32_PATH_REGEX.test(nodeToken) || !(0, utils_1.isHardened)(nodeToken)) { | ||
throw new Error('Invalid derivation path: The "coin_type" node (depth 2) must be a hardened BIP-32 node.'); | ||
@@ -288,3 +289,3 @@ } | ||
case 3: | ||
if (!constants_1.BIP_32_PATH_REGEX.test(nodeToken) || !utils_1.isHardened(nodeToken)) { | ||
if (!constants_1.BIP_32_PATH_REGEX.test(nodeToken) || !(0, utils_1.isHardened)(nodeToken)) { | ||
throw new Error('Invalid derivation path: The "account" node (depth 3) must be a hardened BIP-32 node.'); | ||
@@ -303,5 +304,2 @@ } | ||
break; | ||
/* istanbul ignore next: should be impossible in our usage */ | ||
default: | ||
throw new Error(`Invalid derivation path: The path exceeds the maximum BIP-44 depth.`); | ||
} | ||
@@ -308,0 +306,0 @@ }); |
@@ -1,4 +0,2 @@ | ||
export declare const BUFFER_EXTENDED_KEY_LENGTH = 64; | ||
export declare const BUFFER_KEY_LENGTH = 32; | ||
export declare const HEXADECIMAL_KEY_LENGTH = 64; | ||
export declare const BYTES_KEY_LENGTH = 32; | ||
export declare const MIN_BIP_44_DEPTH = 0; | ||
@@ -28,8 +26,8 @@ export declare const MAX_BIP_44_DEPTH = 5; | ||
export declare const BIP_32_HARDENED_OFFSET = 2147483648; | ||
declare type HDPathString0 = AnonymizedBIP39Node; | ||
declare type HDPathString1 = `${HDPathString0} / ${HardenedBIP32Node}`; | ||
declare type HDPathString2 = `${HDPathString1} / ${HardenedBIP32Node}`; | ||
declare type HDPathString3 = `${HDPathString2} / ${HardenedBIP32Node}`; | ||
declare type HDPathString4 = `${HDPathString3} / ${BIP32Node}`; | ||
declare type HDPathString5 = `${HDPathString4} / ${BIP32Node}`; | ||
export declare type HDPathString0 = AnonymizedBIP39Node; | ||
export declare type HDPathString1 = `${HDPathString0} / ${HardenedBIP32Node}`; | ||
export declare type HDPathString2 = `${HDPathString1} / ${HardenedBIP32Node}`; | ||
export declare type HDPathString3 = `${HDPathString2} / ${HardenedBIP32Node}`; | ||
export declare type HDPathString4 = `${HDPathString3} / ${BIP32Node}`; | ||
export declare type HDPathString5 = `${HDPathString4} / ${BIP32Node}`; | ||
export declare type CoinTypeHDPathString = HDPathString2; | ||
@@ -39,24 +37,43 @@ export declare type ChangeHDPathString = HDPathString4; | ||
export declare type HDPathString = HDPathString0 | HDPathString1 | HDPathString2 | HDPathString3 | HDPathString4 | HDPathString5; | ||
declare type RootedHDPathTuple0 = readonly [BIP39Node]; | ||
declare type RootedHDPathTuple1 = readonly [...RootedHDPathTuple0, HardenedBIP32Node]; | ||
declare type RootedHDPathTuple2 = readonly [...RootedHDPathTuple1, HardenedBIP32Node]; | ||
declare type RootedHDPathTuple3 = readonly [...RootedHDPathTuple2, HardenedBIP32Node]; | ||
declare type RootedHDPathTuple4 = readonly [...RootedHDPathTuple3, BIP32Node]; | ||
declare type RootedHDPathTuple5 = readonly [...RootedHDPathTuple4, BIP32Node]; | ||
export declare type RootedHDPathTuple0 = readonly [BIP39Node]; | ||
export declare type RootedHDPathTuple1 = readonly [ | ||
...RootedHDPathTuple0, | ||
HardenedBIP32Node | ||
]; | ||
export declare type RootedHDPathTuple2 = readonly [ | ||
...RootedHDPathTuple1, | ||
HardenedBIP32Node | ||
]; | ||
export declare type RootedHDPathTuple3 = readonly [ | ||
...RootedHDPathTuple2, | ||
HardenedBIP32Node | ||
]; | ||
export declare type RootedHDPathTuple4 = readonly [...RootedHDPathTuple3, BIP32Node]; | ||
export declare type RootedHDPathTuple5 = readonly [...RootedHDPathTuple4, BIP32Node]; | ||
export declare type RootedHDPathTuple = RootedHDPathTuple0 | RootedHDPathTuple1 | RootedHDPathTuple2 | RootedHDPathTuple3 | RootedHDPathTuple4 | RootedHDPathTuple5; | ||
declare type PartialHDPathTuple1 = readonly [HardenedBIP32Node]; | ||
declare type PartialHDPathTuple2 = readonly [...PartialHDPathTuple1, HardenedBIP32Node]; | ||
declare type PartialHDPathTuple3 = readonly [...PartialHDPathTuple2, HardenedBIP32Node]; | ||
declare type PartialHDPathTuple4 = readonly [...PartialHDPathTuple3, BIP32Node]; | ||
declare type PartialHDPathTuple5 = readonly [...PartialHDPathTuple4, BIP32Node]; | ||
declare type PartialHDPathTuple6 = readonly [BIP32Node]; | ||
declare type PartialHDPathTuple7 = readonly [BIP32Node, BIP32Node]; | ||
declare type PartialHDPathTuple8 = readonly [HardenedBIP32Node, BIP32Node, BIP32Node]; | ||
declare type PartialHDPathTuple9 = readonly [HardenedBIP32Node, BIP32Node]; | ||
declare type PartialHDPathTuple10 = readonly [ | ||
export declare type PartialHDPathTuple1 = readonly [HardenedBIP32Node]; | ||
export declare type PartialHDPathTuple2 = readonly [ | ||
...PartialHDPathTuple1, | ||
HardenedBIP32Node | ||
]; | ||
export declare type PartialHDPathTuple3 = readonly [ | ||
...PartialHDPathTuple2, | ||
HardenedBIP32Node | ||
]; | ||
export declare type PartialHDPathTuple4 = readonly [...PartialHDPathTuple3, BIP32Node]; | ||
export declare type PartialHDPathTuple5 = readonly [...PartialHDPathTuple4, BIP32Node]; | ||
export declare type PartialHDPathTuple6 = readonly [BIP32Node]; | ||
export declare type PartialHDPathTuple7 = readonly [BIP32Node, BIP32Node]; | ||
export declare type PartialHDPathTuple8 = readonly [ | ||
HardenedBIP32Node, | ||
BIP32Node, | ||
BIP32Node | ||
]; | ||
export declare type PartialHDPathTuple9 = readonly [HardenedBIP32Node, BIP32Node]; | ||
export declare type PartialHDPathTuple10 = readonly [ | ||
HardenedBIP32Node, | ||
HardenedBIP32Node, | ||
BIP32Node | ||
]; | ||
declare type PartialHDPathTuple11 = readonly [ | ||
export declare type PartialHDPathTuple11 = readonly [ | ||
HardenedBIP32Node, | ||
@@ -77,2 +94,1 @@ HardenedBIP32Node, | ||
export declare type FullHDPathTuple = RootedHDPathTuple5; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.BIP_32_HARDENED_OFFSET = exports.BIP_39_PATH_REGEX = exports.BIP_32_PATH_REGEX = exports.BIP44PurposeNodeToken = exports.MAX_BIP_44_DEPTH = exports.MIN_BIP_44_DEPTH = exports.HEXADECIMAL_KEY_LENGTH = exports.BUFFER_KEY_LENGTH = exports.BUFFER_EXTENDED_KEY_LENGTH = void 0; | ||
exports.BUFFER_EXTENDED_KEY_LENGTH = 64; | ||
exports.BUFFER_KEY_LENGTH = 32; | ||
exports.HEXADECIMAL_KEY_LENGTH = 64; | ||
exports.BIP_32_HARDENED_OFFSET = exports.BIP_39_PATH_REGEX = exports.BIP_32_PATH_REGEX = exports.BIP44PurposeNodeToken = exports.MAX_BIP_44_DEPTH = exports.MIN_BIP_44_DEPTH = exports.BYTES_KEY_LENGTH = void 0; | ||
exports.BYTES_KEY_LENGTH = 32; | ||
exports.MIN_BIP_44_DEPTH = 0; | ||
@@ -8,0 +6,0 @@ exports.MAX_BIP_44_DEPTH = 5; |
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" /> | ||
import * as secp256k1 from './secp256k1'; | ||
@@ -17,7 +16,7 @@ import * as ed25519 from './ed25519'; | ||
}; | ||
getPublicKey: (privateKey: Uint8Array | string | bigint, compressed?: boolean) => Buffer | Promise<Buffer>; | ||
isValidPrivateKey: (privateKey: Uint8Array | string | bigint) => boolean; | ||
publicAdd: (publicKey: Buffer, tweak: Buffer) => Buffer; | ||
compressPublicKey: (publicKey: Buffer) => Buffer; | ||
decompressPublicKey: (publicKey: Buffer) => Buffer; | ||
getPublicKey: (privateKey: Uint8Array, compressed?: boolean) => Uint8Array | Promise<Uint8Array>; | ||
isValidPrivateKey: (privateKey: Uint8Array) => boolean; | ||
publicAdd: (publicKey: Uint8Array, tweak: Uint8Array) => Uint8Array; | ||
compressPublicKey: (publicKey: Uint8Array) => Uint8Array; | ||
decompressPublicKey: (publicKey: Uint8Array) => Uint8Array; | ||
}; | ||
@@ -24,0 +23,0 @@ /** |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -6,0 +10,0 @@ if (k2 === undefined) k2 = k; |
@@ -1,11 +0,10 @@ | ||
/// <reference types="node" /> | ||
export { CURVE as curve } from '@noble/ed25519'; | ||
export declare const name = "ed25519"; | ||
export declare const secret: Buffer; | ||
export declare const secret: Uint8Array; | ||
export declare const isValidPrivateKey: (_privateKey: Uint8Array | string | bigint) => boolean; | ||
export declare const deriveUnhardenedKeys = false; | ||
export declare const publicKeyLength = 33; | ||
export declare const getPublicKey: (privateKey: Uint8Array | string | bigint, _compressed?: boolean | undefined) => Promise<Buffer>; | ||
export declare const publicAdd: (_publicKey: Buffer, _tweak: Buffer) => Buffer; | ||
export declare const compressPublicKey: (publicKey: Buffer) => Buffer; | ||
export declare const decompressPublicKey: (publicKey: Buffer) => Buffer; | ||
export declare const getPublicKey: (privateKey: Uint8Array, _compressed?: boolean) => Promise<Uint8Array>; | ||
export declare const publicAdd: (_publicKey: Uint8Array, _tweak: Uint8Array) => Uint8Array; | ||
export declare const compressPublicKey: (publicKey: Uint8Array) => Uint8Array; | ||
export declare const decompressPublicKey: (publicKey: Uint8Array) => Uint8Array; |
@@ -5,2 +5,3 @@ "use strict"; | ||
const ed25519_1 = require("@noble/ed25519"); | ||
const utils_1 = require("@metamask/utils"); | ||
var ed25519_2 = require("@noble/ed25519"); | ||
@@ -11,3 +12,3 @@ Object.defineProperty(exports, "curve", { enumerable: true, get: function () { return ed25519_2.CURVE; } }); | ||
// https://github.com/satoshilabs/slips/blob/133ea52a8e43d338b98be208907e144277e44c0e/slip-0010.md#master-key-generation | ||
exports.secret = Buffer.from('ed25519 seed', 'utf8'); | ||
exports.secret = (0, utils_1.stringToBytes)('ed25519 seed'); | ||
// All private keys are valid for ed25519: | ||
@@ -20,4 +21,4 @@ // https://github.com/satoshilabs/slips/blob/133ea52a8e43d338b98be208907e144277e44c0e/slip-0010.md#master-key-generation | ||
const getPublicKey = async (privateKey, _compressed) => { | ||
const publicKey = await ed25519_1.getPublicKey(privateKey); | ||
return Buffer.concat([Buffer.alloc(1, 0), publicKey]); | ||
const publicKey = await (0, ed25519_1.getPublicKey)(privateKey); | ||
return (0, utils_1.concatBytes)([new Uint8Array([0]), publicKey]); | ||
}; | ||
@@ -24,0 +25,0 @@ exports.getPublicKey = getPublicKey; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -6,0 +10,0 @@ if (k2 === undefined) k2 = k; |
@@ -1,11 +0,10 @@ | ||
/// <reference types="node" /> | ||
export { CURVE as curve } from '@noble/secp256k1'; | ||
export declare const isValidPrivateKey: (privateKey: number | bigint | (string | Uint8Array)) => boolean; | ||
export declare const name = "secp256k1"; | ||
export declare const secret: Buffer; | ||
export declare const secret: Uint8Array; | ||
export declare const deriveUnhardenedKeys = true; | ||
export declare const publicKeyLength = 65; | ||
export declare const getPublicKey: (privateKey: Uint8Array | string | bigint, compressed?: boolean | undefined) => Buffer; | ||
export declare const publicAdd: (publicKey: Buffer, tweak: Buffer) => Buffer; | ||
export declare const compressPublicKey: (publicKey: Uint8Array) => Buffer; | ||
export declare const decompressPublicKey: (publicKey: Uint8Array) => Buffer; | ||
export declare const isValidPrivateKey: (privateKey: Uint8Array) => boolean; | ||
export declare const getPublicKey: (privateKey: Uint8Array, compressed?: boolean) => Uint8Array; | ||
export declare const publicAdd: (publicKey: Uint8Array, tweak: Uint8Array) => Uint8Array; | ||
export declare const compressPublicKey: (publicKey: Uint8Array) => Uint8Array; | ||
export declare const decompressPublicKey: (publicKey: Uint8Array) => Uint8Array; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.decompressPublicKey = exports.compressPublicKey = exports.publicAdd = exports.getPublicKey = exports.publicKeyLength = exports.deriveUnhardenedKeys = exports.secret = exports.name = exports.isValidPrivateKey = exports.curve = void 0; | ||
exports.decompressPublicKey = exports.compressPublicKey = exports.publicAdd = exports.getPublicKey = exports.isValidPrivateKey = exports.publicKeyLength = exports.deriveUnhardenedKeys = exports.secret = exports.name = exports.curve = void 0; | ||
const secp256k1_1 = require("@noble/secp256k1"); | ||
const utils_1 = require("@metamask/utils"); | ||
var secp256k1_2 = require("@noble/secp256k1"); | ||
Object.defineProperty(exports, "curve", { enumerable: true, get: function () { return secp256k1_2.CURVE; } }); | ||
exports.isValidPrivateKey = secp256k1_1.utils.isValidPrivateKey; | ||
exports.name = 'secp256k1'; | ||
@@ -12,6 +12,10 @@ // Secret is defined in BIP-32 and SLIP-10: | ||
// https://github.com/satoshilabs/slips/blob/133ea52a8e43d338b98be208907e144277e44c0e/slip-0010.md#master-key-generation | ||
exports.secret = Buffer.from('Bitcoin seed', 'utf8'); | ||
exports.secret = (0, utils_1.stringToBytes)('Bitcoin seed'); | ||
exports.deriveUnhardenedKeys = true; | ||
exports.publicKeyLength = 65; | ||
const getPublicKey = (privateKey, compressed) => Buffer.from(secp256k1_1.getPublicKey(privateKey, compressed)); | ||
const isValidPrivateKey = (privateKey) => { | ||
return secp256k1_1.utils.isValidPrivateKey(privateKey); | ||
}; | ||
exports.isValidPrivateKey = isValidPrivateKey; | ||
const getPublicKey = (privateKey, compressed) => (0, secp256k1_1.getPublicKey)(privateKey, compressed); | ||
exports.getPublicKey = getPublicKey; | ||
@@ -25,3 +29,3 @@ const publicAdd = (publicKey, tweak) => { | ||
newPoint.assertValidity(); | ||
return Buffer.from(newPoint.toRawBytes(false)); | ||
return newPoint.toRawBytes(false); | ||
}; | ||
@@ -31,3 +35,3 @@ exports.publicAdd = publicAdd; | ||
const point = secp256k1_1.Point.fromHex(publicKey); | ||
return Buffer.from(point.toRawBytes(true)); | ||
return point.toRawBytes(true); | ||
}; | ||
@@ -39,5 +43,5 @@ exports.compressPublicKey = compressPublicKey; | ||
const point = secp256k1_1.Point.fromHex(publicKey); | ||
return Buffer.from(point.toRawBytes(false)); | ||
return point.toRawBytes(false); | ||
}; | ||
exports.decompressPublicKey = decompressPublicKey; | ||
//# sourceMappingURL=secp256k1.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validatePathSegment = exports.deriveKeyFromPath = void 0; | ||
const utils_1 = require("@metamask/utils"); | ||
const constants_1 = require("./constants"); | ||
@@ -50,6 +51,3 @@ const derivers_1 = require("./derivers"); | ||
const [pathType, pathPart] = pathNode.split(':'); | ||
/* istanbul ignore if: should be impossible */ | ||
if (!hasDeriver(pathType)) { | ||
throw new Error(`Unknown derivation type: "${pathType}"`); | ||
} | ||
(0, utils_1.assert)(hasDeriver(pathType), `Unknown derivation type: "${pathType}".`); | ||
const deriver = derivers_1.derivers[pathType]; | ||
@@ -59,3 +57,3 @@ return await deriver.deriveChildKey({ | ||
node: derivedNode, | ||
curve: curves_1.getCurveByName(curve), | ||
curve: (0, curves_1.getCurveByName)(curve), | ||
}); | ||
@@ -62,0 +60,0 @@ }, Promise.resolve(node)); |
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" /> | ||
import { Curve } from '../curves'; | ||
@@ -12,7 +11,7 @@ import { SLIP10Node } from '../SLIP10Node'; | ||
* | ||
* @param key - The `address_index` private key buffer to convert to an Ethereum | ||
* @param key - The `address_index` private key bytes to convert to an Ethereum | ||
* address. | ||
* @returns The Ethereum address corresponding to the given key. | ||
*/ | ||
export declare function privateKeyToEthAddress(key: Buffer): Buffer; | ||
export declare function privateKeyToEthAddress(key: Uint8Array): Uint8Array; | ||
/** | ||
@@ -25,7 +24,7 @@ * Converts a BIP-32 public key to an Ethereum address. | ||
* | ||
* @param key - The `address_index` public key buffer to convert to an Ethereum | ||
* @param key - The `address_index` public key bytes to convert to an Ethereum | ||
* address. | ||
* @returns The Ethereum address corresponding to the given key. | ||
*/ | ||
export declare function publicKeyToEthAddress(key: Buffer): Buffer; | ||
export declare function publicKeyToEthAddress(key: Uint8Array): Uint8Array; | ||
/** | ||
@@ -44,4 +43,4 @@ * Derive a BIP-32 child key with a given path from a parent key. | ||
* | ||
* @param privateKeyBuffer - The private key as 32 byte Uint8Array. | ||
* @param tweakBuffer - The tweak as 32 byte Uint8Array. | ||
* @param privateKeyBytes - The private key as 32 byte Uint8Array. | ||
* @param tweakBytes - The tweak as 32 byte Uint8Array. | ||
* @param curve - The curve to use. | ||
@@ -51,2 +50,2 @@ * @throws If the private key or tweak is invalid. | ||
*/ | ||
export declare function privateAdd(privateKeyBuffer: Uint8Array, tweakBuffer: Uint8Array, curve: Curve): Buffer; | ||
export declare function privateAdd(privateKeyBytes: Uint8Array, tweakBytes: Uint8Array, curve: Curve): Uint8Array; |
@@ -7,4 +7,5 @@ "use strict"; | ||
const sha512_1 = require("@noble/hashes/sha512"); | ||
const utils_1 = require("@metamask/utils"); | ||
const constants_1 = require("../constants"); | ||
const utils_1 = require("../utils"); | ||
const utils_2 = require("../utils"); | ||
const curves_1 = require("../curves"); | ||
@@ -19,3 +20,3 @@ const SLIP10Node_1 = require("../SLIP10Node"); | ||
* | ||
* @param key - The `address_index` private key buffer to convert to an Ethereum | ||
* @param key - The `address_index` private key bytes to convert to an Ethereum | ||
* address. | ||
@@ -25,5 +26,3 @@ * @returns The Ethereum address corresponding to the given key. | ||
function privateKeyToEthAddress(key) { | ||
if (!Buffer.isBuffer(key) || !utils_1.isValidBufferKey(key, constants_1.BUFFER_KEY_LENGTH)) { | ||
throw new Error('Invalid key: The key must be a 32-byte, non-zero Buffer.'); | ||
} | ||
(0, utils_1.assert)(key instanceof Uint8Array && (0, utils_2.isValidBytesKey)(key, constants_1.BYTES_KEY_LENGTH), 'Invalid key: The key must be a 32-byte, non-zero Uint8Array.'); | ||
const publicKey = curves_1.secp256k1.getPublicKey(key, false); | ||
@@ -40,3 +39,3 @@ return publicKeyToEthAddress(publicKey); | ||
* | ||
* @param key - The `address_index` public key buffer to convert to an Ethereum | ||
* @param key - The `address_index` public key bytes to convert to an Ethereum | ||
* address. | ||
@@ -46,7 +45,5 @@ * @returns The Ethereum address corresponding to the given key. | ||
function publicKeyToEthAddress(key) { | ||
if (!Buffer.isBuffer(key) || | ||
!utils_1.isValidBufferKey(key, curves_1.secp256k1.publicKeyLength)) { | ||
throw new Error('Invalid key: The key must be a 65-byte, non-zero Buffer.'); | ||
} | ||
return Buffer.from(sha3_1.keccak_256(key.slice(1)).slice(-20)); | ||
(0, utils_1.assert)(key instanceof Uint8Array && | ||
(0, utils_2.isValidBytesKey)(key, curves_1.secp256k1.publicKeyLength), 'Invalid key: The key must be a 65-byte, non-zero Uint8Array.'); | ||
return (0, sha3_1.keccak_256)(key.slice(1)).slice(-20); | ||
} | ||
@@ -82,5 +79,5 @@ exports.publicKeyToEthAddress = publicKeyToEthAddress; | ||
} | ||
if (node.privateKeyBuffer) { | ||
if (node.privateKeyBytes) { | ||
const secretExtension = await deriveSecretExtension({ | ||
privateKey: node.privateKeyBuffer, | ||
privateKey: node.privateKeyBytes, | ||
childIndex, | ||
@@ -91,4 +88,4 @@ isHardened, | ||
const { privateKey, chainCode } = await generateKey({ | ||
privateKey: node.privateKeyBuffer, | ||
chainCode: node.chainCodeBuffer, | ||
privateKey: node.privateKeyBytes, | ||
chainCode: node.chainCodeBytes, | ||
secretExtension, | ||
@@ -108,9 +105,8 @@ curve, | ||
const publicExtension = await derivePublicExtension({ | ||
parentPublicKey: node.compressedPublicKeyBuffer, | ||
parentPublicKey: node.compressedPublicKeyBytes, | ||
childIndex, | ||
curve, | ||
}); | ||
const { publicKey, chainCode } = generatePublicKey({ | ||
publicKey: node.compressedPublicKeyBuffer, | ||
chainCode: node.chainCodeBuffer, | ||
publicKey: node.compressedPublicKeyBytes, | ||
chainCode: node.chainCodeBytes, | ||
publicExtension, | ||
@@ -140,18 +136,16 @@ curve, | ||
// Hardened child | ||
const indexBuffer = Buffer.allocUnsafe(4); | ||
indexBuffer.writeUInt32BE(childIndex + constants_1.BIP_32_HARDENED_OFFSET, 0); | ||
const pk = privateKey; | ||
const zb = Buffer.alloc(1, 0); | ||
return Buffer.concat([zb, pk, indexBuffer]); | ||
const indexBytes = new Uint8Array(4); | ||
const view = new DataView(indexBytes.buffer); | ||
view.setUint32(0, childIndex + constants_1.BIP_32_HARDENED_OFFSET, false); | ||
return (0, utils_1.concatBytes)([new Uint8Array([0]), privateKey, indexBytes]); | ||
} | ||
// Normal child | ||
const indexBuffer = Buffer.allocUnsafe(4); | ||
indexBuffer.writeUInt32BE(childIndex, 0); | ||
const parentPublicKey = await curve.getPublicKey(privateKey, true); | ||
return Buffer.concat([parentPublicKey, indexBuffer]); | ||
return derivePublicExtension({ parentPublicKey, childIndex }); | ||
} | ||
async function derivePublicExtension({ parentPublicKey, childIndex, }) { | ||
const indexBuffer = Buffer.alloc(4); | ||
indexBuffer.writeUInt32BE(childIndex, 0); | ||
return Buffer.concat([parentPublicKey, indexBuffer]); | ||
const indexBytes = new Uint8Array(4); | ||
const view = new DataView(indexBytes.buffer); | ||
view.setUint32(0, childIndex, false); | ||
return (0, utils_1.concatBytes)([parentPublicKey, indexBytes]); | ||
} | ||
@@ -161,4 +155,4 @@ /** | ||
* | ||
* @param privateKeyBuffer - The private key as 32 byte Uint8Array. | ||
* @param tweakBuffer - The tweak as 32 byte Uint8Array. | ||
* @param privateKeyBytes - The private key as 32 byte Uint8Array. | ||
* @param tweakBytes - The tweak as 32 byte Uint8Array. | ||
* @param curve - The curve to use. | ||
@@ -168,13 +162,14 @@ * @throws If the private key or tweak is invalid. | ||
*/ | ||
function privateAdd(privateKeyBuffer, tweakBuffer, curve) { | ||
const privateKey = utils_1.bytesToNumber(privateKeyBuffer); | ||
const tweak = utils_1.bytesToNumber(tweakBuffer); | ||
function privateAdd(privateKeyBytes, tweakBytes, curve) { | ||
const privateKey = (0, utils_1.bytesToBigInt)(privateKeyBytes); | ||
const tweak = (0, utils_1.bytesToBigInt)(tweakBytes); | ||
if (tweak >= curve.curve.n) { | ||
throw new Error('Invalid tweak: Tweak is larger than the curve order.'); | ||
} | ||
const added = curves_1.mod(privateKey + tweak, curve.curve.n); | ||
if (!curve.isValidPrivateKey(added)) { | ||
const added = (0, curves_1.mod)(privateKey + tweak, curve.curve.n); | ||
const bytes = (0, utils_1.hexToBytes)(added.toString(16).padStart(64, '0')); | ||
if (!curve.isValidPrivateKey(bytes)) { | ||
throw new Error('Invalid private key or tweak: The resulting private key is invalid.'); | ||
} | ||
return utils_1.hexStringToBuffer(added.toString(16).padStart(64, '0')); | ||
return bytes; | ||
} | ||
@@ -189,5 +184,5 @@ exports.privateAdd = privateAdd; | ||
async function generateKey({ privateKey, chainCode, secretExtension, curve, }) { | ||
const entropy = hmac_1.hmac(sha512_1.sha512, chainCode, secretExtension); | ||
const keyMaterial = Buffer.from(entropy.slice(0, 32)); | ||
const childChainCode = Buffer.from(entropy.slice(32)); | ||
const entropy = (0, hmac_1.hmac)(sha512_1.sha512, chainCode, secretExtension); | ||
const keyMaterial = entropy.slice(0, 32); | ||
const childChainCode = entropy.slice(32); | ||
// If curve is ed25519: The returned child key ki is parse256(IL). | ||
@@ -204,12 +199,12 @@ // https://github.com/satoshilabs/slips/blob/133ea52a8e43d338b98be208907e144277e44c0e/slip-0010.md#private-parent-key--private-child-key | ||
function generatePublicKey({ publicKey, chainCode, publicExtension, curve, }) { | ||
const entropy = hmac_1.hmac(sha512_1.sha512, chainCode, publicExtension); | ||
const entropy = (0, hmac_1.hmac)(sha512_1.sha512, chainCode, publicExtension); | ||
const keyMaterial = entropy.slice(0, 32); | ||
const childChainCode = entropy.slice(32); | ||
// This function may fail if the resulting key is invalid. | ||
const childPublicKey = curve.publicAdd(publicKey, Buffer.from(keyMaterial)); | ||
const childPublicKey = curve.publicAdd(publicKey, keyMaterial); | ||
return { | ||
publicKey: Buffer.from(childPublicKey), | ||
chainCode: Buffer.from(childChainCode), | ||
publicKey: childPublicKey, | ||
chainCode: childChainCode, | ||
}; | ||
} | ||
//# sourceMappingURL=bip32.js.map |
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" /> | ||
import { BIP39Node } from '../constants'; | ||
@@ -20,2 +19,2 @@ import { Curve } from '../curves'; | ||
*/ | ||
export declare function createBip39KeyFromSeed(seed: Buffer, curve?: Curve): Promise<SLIP10Node>; | ||
export declare function createBip39KeyFromSeed(seed: Uint8Array, curve?: Curve): Promise<SLIP10Node>; |
@@ -23,3 +23,3 @@ "use strict"; | ||
async function deriveChildKey({ path, curve, }) { | ||
return createBip39KeyFromSeed(Buffer.from(bip39_1.mnemonicToSeedSync(path)), curve); | ||
return createBip39KeyFromSeed(await (0, bip39_1.mnemonicToSeed)(path), curve); | ||
} | ||
@@ -33,6 +33,6 @@ exports.deriveChildKey = deriveChildKey; | ||
async function createBip39KeyFromSeed(seed, curve = curves_1.secp256k1) { | ||
const key = Buffer.from(hmac_1.hmac(sha512_1.sha512, curve.secret, seed)); | ||
const key = (0, hmac_1.hmac)(sha512_1.sha512, curve.secret, seed); | ||
const privateKey = key.slice(0, 32); | ||
const chainCode = key.slice(32); | ||
const masterFingerprint = utils_1.getFingerprint(await curve.getPublicKey(privateKey, true)); | ||
const masterFingerprint = (0, utils_1.getFingerprint)(await curve.getPublicKey(privateKey, true)); | ||
return SLIP10Node_1.SLIP10Node.fromExtendedKey({ | ||
@@ -39,0 +39,0 @@ privateKey, |
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" /> | ||
import { Curve } from '../curves'; | ||
@@ -10,5 +9,5 @@ import { SLIP10Node } from '../SLIP10Node'; | ||
*/ | ||
privateKey?: Buffer; | ||
publicKey: Buffer; | ||
chainCode: Buffer; | ||
privateKey?: Uint8Array; | ||
publicKey: Uint8Array; | ||
chainCode: Uint8Array; | ||
}; | ||
@@ -15,0 +14,0 @@ export declare type DeriveChildKeyArgs = { |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -6,0 +10,0 @@ if (k2 === undefined) k2 = k; |
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" /> | ||
export declare const PUBLIC_KEY_VERSION = 76067358; | ||
@@ -14,11 +13,11 @@ export declare const PRIVATE_KEY_VERSION = 76066276; | ||
index: number; | ||
chainCode: Buffer; | ||
chainCode: Uint8Array; | ||
}; | ||
declare type ExtendedPublicKey = ExtendedKeyLike & { | ||
version: typeof PUBLIC_KEY_VERSION; | ||
publicKey: Buffer; | ||
publicKey: Uint8Array; | ||
}; | ||
declare type ExtendedPrivateKey = ExtendedKeyLike & { | ||
version: typeof PRIVATE_KEY_VERSION; | ||
privateKey: Buffer; | ||
privateKey: Uint8Array; | ||
}; | ||
@@ -25,0 +24,0 @@ export declare type ExtendedKey = ExtendedPublicKey | ExtendedPrivateKey; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.encodeExtendedKey = exports.decodeExtendedKey = exports.PRIVATE_KEY_VERSION = exports.PUBLIC_KEY_VERSION = void 0; | ||
const utils_1 = require("./utils"); | ||
const utils_1 = require("@metamask/utils"); | ||
const utils_2 = require("./utils"); | ||
const BIP44Node_1 = require("./BIP44Node"); | ||
@@ -19,21 +20,23 @@ const secp256k1_1 = require("./curves/secp256k1"); | ||
const decodeExtendedKey = (extendedKey) => { | ||
const buffer = utils_1.decodeBase58check(extendedKey); | ||
if (buffer.length !== 78) { | ||
throw new Error(`Invalid extended key: Expected a length of 78, got ${buffer.length}.`); | ||
const bytes = (0, utils_2.decodeBase58check)(extendedKey); | ||
if (bytes.length !== 78) { | ||
throw new Error(`Invalid extended key: Expected a length of 78, got ${bytes.length}.`); | ||
} | ||
const version = buffer.readUInt32BE(0); | ||
const depth = buffer.readUInt8(4); | ||
BIP44Node_1.validateBIP44Depth(depth); | ||
const parentFingerprint = buffer.readUInt32BE(5); | ||
const index = buffer.readUInt32BE(9); | ||
const chainCode = buffer.slice(13, 45); | ||
if (!utils_1.isValidBufferKey(chainCode, 32)) { | ||
throw new Error(`Invalid extended key: Chain code must be a 32-byte non-zero Buffer.`); | ||
const view = (0, utils_1.createDataView)(bytes); | ||
const version = view.getUint32(0, false); | ||
const depth = view.getUint8(4); | ||
(0, BIP44Node_1.validateBIP44Depth)(depth); | ||
const parentFingerprint = view.getUint32(5, false); | ||
const index = view.getUint32(9, false); | ||
const chainCode = bytes.slice(13, 45); | ||
if (!(0, utils_2.isValidBytesKey)(chainCode, 32)) { | ||
throw new Error(`Invalid extended key: Chain code must be a 32-byte non-zero byte array.`); | ||
} | ||
const key = buffer.slice(45, 78); | ||
if (!utils_1.isValidBufferKey(key, 33)) { | ||
throw new Error(`Invalid extended key: Key must be a 33-byte non-zero Buffer.`); | ||
const key = bytes.slice(45, 78); | ||
if (!(0, utils_2.isValidBytesKey)(key, 33)) { | ||
throw new Error(`Invalid extended key: Key must be a 33-byte non-zero byte array.`); | ||
} | ||
const keyView = (0, utils_1.createDataView)(key); | ||
if (version === exports.PUBLIC_KEY_VERSION) { | ||
if (key.readUInt8(0) !== 0x02 && key.readUInt8(0) !== 0x03) { | ||
if (keyView.getUint8(0) !== 0x02 && keyView.getUint8(0) !== 0x03) { | ||
throw new Error(`Invalid extended key: Public key must start with 0x02 or 0x03.`); | ||
@@ -47,7 +50,7 @@ } | ||
chainCode, | ||
publicKey: secp256k1_1.decompressPublicKey(key), | ||
publicKey: (0, secp256k1_1.decompressPublicKey)(key), | ||
}; | ||
} | ||
if (version === exports.PRIVATE_KEY_VERSION) { | ||
if (key.readUInt8(0) !== 0x00) { | ||
if (keyView.getUint8(0) !== 0x00) { | ||
throw new Error(`Invalid extended key: Private key must start with 0x00.`); | ||
@@ -74,20 +77,21 @@ } | ||
const { version, depth, parentFingerprint, index, chainCode } = extendedKey; | ||
const buffer = Buffer.alloc(78); | ||
buffer.writeUInt32BE(version, 0); | ||
buffer.writeUInt8(depth, 4); | ||
buffer.writeUInt32BE(parentFingerprint, 5); | ||
buffer.writeUInt32BE(index, 9); | ||
chainCode.copy(buffer, 13); | ||
const bytes = new Uint8Array(78); | ||
const view = (0, utils_1.createDataView)(bytes); | ||
view.setUint32(0, version, false); | ||
view.setUint8(4, depth); | ||
view.setUint32(5, parentFingerprint, false); | ||
view.setUint32(9, index, false); | ||
bytes.set(chainCode, 13); | ||
if (extendedKey.version === exports.PUBLIC_KEY_VERSION) { | ||
const { publicKey } = extendedKey; | ||
const compressedPublicKey = secp256k1_1.compressPublicKey(publicKey); | ||
compressedPublicKey.copy(buffer, 45); | ||
const compressedPublicKey = (0, secp256k1_1.compressPublicKey)(publicKey); | ||
bytes.set(compressedPublicKey, 45); | ||
} | ||
if (extendedKey.version === exports.PRIVATE_KEY_VERSION) { | ||
const { privateKey } = extendedKey; | ||
privateKey.copy(buffer, 46); | ||
bytes.set(privateKey, 46); | ||
} | ||
return utils_1.encodeBase58check(buffer); | ||
return (0, utils_2.encodeBase58check)(bytes); | ||
}; | ||
exports.encodeExtendedKey = encodeExtendedKey; | ||
//# sourceMappingURL=extended-keys.js.map |
@@ -1,10 +0,6 @@ | ||
export { BIP44Node, BIP44NodeInterface, JsonBIP44Node } from './BIP44Node'; | ||
export { SLIP10Node, SLIP10NodeInterface, JsonSLIP10Node } from './SLIP10Node'; | ||
export { secp256k1, ed25519 } from './curves'; | ||
export { BIP44CoinTypeNode, BIP44CoinTypeNodeInterface, BIP_44_COIN_TYPE_DEPTH, CoinTypeHDPathTuple, deriveBIP44AddressKey, getBIP44AddressKeyDeriver, JsonBIP44CoinTypeNode, } from './BIP44CoinTypeNode'; | ||
export { MIN_BIP_44_DEPTH, MAX_BIP_44_DEPTH, BIP44Depth, BIP44PurposeNodeToken, BIP32Node, BIP39Node, } from './constants'; | ||
/** | ||
* The {@link Buffer} accessible to `@metamask/key-tree`, re-exported in case | ||
* of module resolution issues. | ||
*/ | ||
export { Buffer as PackageBuffer } from 'buffer'; | ||
export { BIP44Node, BIP44NodeInterface, JsonBIP44Node, BIP44ExtendedKeyOptions, BIP44DerivationPathOptions, } from './BIP44Node'; | ||
export { SLIP10Node, SLIP10NodeInterface, JsonSLIP10Node, SLIP10NodeConstructorOptions, SLIP10ExtendedKeyOptions, SLIP10DerivationPathOptions, } from './SLIP10Node'; | ||
export { secp256k1, ed25519, SupportedCurve } from './curves'; | ||
export { BIP44CoinTypeNode, BIP44CoinTypeNodeInterface, BIP_44_COIN_TYPE_DEPTH, CoinTypeHDPathTuple, deriveBIP44AddressKey, getBIP44AddressKeyDeriver, JsonBIP44CoinTypeNode, BIP44AddressKeyDeriver, } from './BIP44CoinTypeNode'; | ||
export * from './constants'; | ||
export { CoinTypeToAddressIndices } from './utils'; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PackageBuffer = exports.BIP44PurposeNodeToken = exports.MAX_BIP_44_DEPTH = exports.MIN_BIP_44_DEPTH = exports.getBIP44AddressKeyDeriver = exports.deriveBIP44AddressKey = exports.BIP_44_COIN_TYPE_DEPTH = exports.BIP44CoinTypeNode = exports.ed25519 = exports.secp256k1 = exports.SLIP10Node = exports.BIP44Node = void 0; | ||
exports.getBIP44AddressKeyDeriver = exports.deriveBIP44AddressKey = exports.BIP_44_COIN_TYPE_DEPTH = exports.BIP44CoinTypeNode = exports.ed25519 = exports.secp256k1 = exports.SLIP10Node = exports.BIP44Node = void 0; | ||
var BIP44Node_1 = require("./BIP44Node"); | ||
@@ -16,12 +30,3 @@ Object.defineProperty(exports, "BIP44Node", { enumerable: true, get: function () { return BIP44Node_1.BIP44Node; } }); | ||
Object.defineProperty(exports, "getBIP44AddressKeyDeriver", { enumerable: true, get: function () { return BIP44CoinTypeNode_1.getBIP44AddressKeyDeriver; } }); | ||
var constants_1 = require("./constants"); | ||
Object.defineProperty(exports, "MIN_BIP_44_DEPTH", { enumerable: true, get: function () { return constants_1.MIN_BIP_44_DEPTH; } }); | ||
Object.defineProperty(exports, "MAX_BIP_44_DEPTH", { enumerable: true, get: function () { return constants_1.MAX_BIP_44_DEPTH; } }); | ||
Object.defineProperty(exports, "BIP44PurposeNodeToken", { enumerable: true, get: function () { return constants_1.BIP44PurposeNodeToken; } }); | ||
/** | ||
* The {@link Buffer} accessible to `@metamask/key-tree`, re-exported in case | ||
* of module resolution issues. | ||
*/ | ||
var buffer_1 = require("buffer"); | ||
Object.defineProperty(exports, "PackageBuffer", { enumerable: true, get: function () { return buffer_1.Buffer; } }); | ||
__exportStar(require("./constants"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" /> | ||
import { RootedSLIP10PathTuple, SLIP10PathTuple } from './constants'; | ||
@@ -47,12 +46,12 @@ import { SupportedCurve } from './curves'; | ||
export declare type SLIP10NodeInterface = JsonSLIP10Node & { | ||
chainCodeBuffer: Buffer; | ||
chainCodeBytes: Uint8Array; | ||
/** | ||
* The private key for this node, as a Node.js Buffer or browser-equivalent. | ||
* The private key for this node, as a {@link Uint8Array}. | ||
* May be undefined if this node is a public key only node. | ||
*/ | ||
privateKeyBuffer?: Buffer; | ||
privateKeyBytes?: Uint8Array; | ||
/** | ||
* The public key for this node, as a Node.js Buffer or browser-equivalent. | ||
* The public key for this node, as a {@link Uint8Array}. | ||
*/ | ||
publicKeyBuffer: Buffer; | ||
publicKeyBytes: Uint8Array; | ||
/** | ||
@@ -63,3 +62,3 @@ * @returns A JSON-compatible representation of this node's data fields. | ||
}; | ||
declare type SLIP10NodeConstructorOptions = { | ||
export declare type SLIP10NodeConstructorOptions = { | ||
readonly depth: number; | ||
@@ -69,8 +68,8 @@ readonly masterFingerprint?: number; | ||
readonly index: number; | ||
readonly chainCode: Buffer; | ||
readonly privateKey?: Buffer; | ||
readonly publicKey: Buffer; | ||
readonly chainCode: Uint8Array; | ||
readonly privateKey?: Uint8Array; | ||
readonly publicKey: Uint8Array; | ||
readonly curve: SupportedCurve; | ||
}; | ||
declare type SLIP10ExtendedKeyOptions = { | ||
export declare type SLIP10ExtendedKeyOptions = { | ||
readonly depth: number; | ||
@@ -80,8 +79,8 @@ readonly masterFingerprint?: number; | ||
readonly index: number; | ||
readonly chainCode: string | Buffer; | ||
readonly privateKey?: string | Buffer; | ||
readonly publicKey?: string | Buffer; | ||
readonly chainCode: string | Uint8Array; | ||
readonly privateKey?: string | Uint8Array; | ||
readonly publicKey?: string | Uint8Array; | ||
readonly curve: SupportedCurve; | ||
}; | ||
declare type SLIP10DerivationPathOptions = { | ||
export declare type SLIP10DerivationPathOptions = { | ||
readonly derivationPath: RootedSLIP10PathTuple; | ||
@@ -107,2 +106,5 @@ readonly curve: SupportedCurve; | ||
* @param depth - The depth of the node. | ||
* @param masterFingerprint - The fingerprint of the master node, i.e., the | ||
* node at depth 0. May be undefined if this node was created from an extended | ||
* key. | ||
* @param parentFingerprint - The fingerprint of the parent key, or 0 if | ||
@@ -145,10 +147,10 @@ * the node is a master node. | ||
readonly index: number; | ||
readonly chainCodeBuffer: Buffer; | ||
readonly privateKeyBuffer?: Buffer; | ||
readonly publicKeyBuffer: Buffer; | ||
readonly chainCodeBytes: Uint8Array; | ||
readonly privateKeyBytes?: Uint8Array; | ||
readonly publicKeyBytes: Uint8Array; | ||
constructor({ depth, masterFingerprint, parentFingerprint, index, chainCode, privateKey, publicKey, curve, }: SLIP10NodeConstructorOptions); | ||
get chainCode(): string; | ||
get chainCode(): `0x${string}`; | ||
get privateKey(): string | undefined; | ||
get publicKey(): string; | ||
get compressedPublicKeyBuffer(): Buffer; | ||
get compressedPublicKeyBytes(): Uint8Array; | ||
get compressedPublicKey(): string; | ||
@@ -155,0 +157,0 @@ get address(): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.deriveChildNode = exports.validateParentFingerprint = exports.validateBIP32Depth = exports.SLIP10Node = void 0; | ||
const utils_1 = require("@metamask/utils"); | ||
const constants_1 = require("./constants"); | ||
@@ -8,3 +9,3 @@ const curves_1 = require("./curves"); | ||
const bip32_1 = require("./derivers/bip32"); | ||
const utils_1 = require("./utils"); | ||
const utils_2 = require("./utils"); | ||
class SLIP10Node { | ||
@@ -16,5 +17,5 @@ constructor({ depth, masterFingerprint, parentFingerprint, index, chainCode, privateKey, publicKey, curve, }) { | ||
this.index = index; | ||
this.chainCodeBuffer = chainCode; | ||
this.privateKeyBuffer = privateKey; | ||
this.publicKeyBuffer = publicKey; | ||
this.chainCodeBytes = chainCode; | ||
this.privateKeyBytes = privateKey; | ||
this.publicKeyBytes = publicKey; | ||
this.curve = curve; | ||
@@ -41,2 +42,5 @@ Object.freeze(this); | ||
* @param depth - The depth of the node. | ||
* @param masterFingerprint - The fingerprint of the master node, i.e., the | ||
* node at depth 0. May be undefined if this node was created from an extended | ||
* key. | ||
* @param parentFingerprint - The fingerprint of the parent key, or 0 if | ||
@@ -52,9 +56,9 @@ * the node is a master node. | ||
static async fromExtendedKey({ depth, masterFingerprint, parentFingerprint, index, privateKey, publicKey, chainCode, curve, }) { | ||
const chainCodeBuffer = utils_1.getBuffer(chainCode, constants_1.BUFFER_KEY_LENGTH); | ||
const chainCodeBytes = (0, utils_2.getBytes)(chainCode, constants_1.BYTES_KEY_LENGTH); | ||
validateCurve(curve); | ||
validateBIP32Depth(depth); | ||
utils_1.validateBIP32Index(index); | ||
(0, utils_2.validateBIP32Index)(index); | ||
validateParentFingerprint(parentFingerprint); | ||
if (privateKey) { | ||
const privateKeyBuffer = utils_1.getBuffer(privateKey, constants_1.BUFFER_KEY_LENGTH); | ||
const privateKeyBytes = (0, utils_2.getBytes)(privateKey, constants_1.BYTES_KEY_LENGTH); | ||
return new SLIP10Node({ | ||
@@ -65,5 +69,5 @@ depth, | ||
index, | ||
chainCode: chainCodeBuffer, | ||
privateKey: privateKeyBuffer, | ||
publicKey: await curves_1.getCurveByName(curve).getPublicKey(privateKey), | ||
chainCode: chainCodeBytes, | ||
privateKey: privateKeyBytes, | ||
publicKey: await (0, curves_1.getCurveByName)(curve).getPublicKey(privateKeyBytes), | ||
curve, | ||
@@ -73,3 +77,3 @@ }); | ||
if (publicKey) { | ||
const publicKeyBuffer = utils_1.getBuffer(publicKey, curves_1.getCurveByName(curve).publicKeyLength); | ||
const publicKeyBytes = (0, utils_2.getBytes)(publicKey, (0, curves_1.getCurveByName)(curve).publicKeyLength); | ||
return new SLIP10Node({ | ||
@@ -80,4 +84,4 @@ depth, | ||
index, | ||
chainCode: chainCodeBuffer, | ||
publicKey: publicKeyBuffer, | ||
chainCode: chainCodeBytes, | ||
publicKey: publicKeyBytes, | ||
curve, | ||
@@ -117,3 +121,3 @@ }); | ||
} | ||
return await derivation_1.deriveKeyFromPath({ | ||
return await (0, derivation_1.deriveKeyFromPath)({ | ||
path: derivationPath, | ||
@@ -125,16 +129,18 @@ depth: derivationPath.length - 1, | ||
get chainCode() { | ||
return this.chainCodeBuffer.toString('hex'); | ||
return (0, utils_1.bytesToHex)(this.chainCodeBytes); | ||
} | ||
get privateKey() { | ||
var _a; | ||
return (_a = this.privateKeyBuffer) === null || _a === void 0 ? void 0 : _a.toString('hex'); | ||
if (this.privateKeyBytes) { | ||
return (0, utils_1.bytesToHex)(this.privateKeyBytes); | ||
} | ||
return undefined; | ||
} | ||
get publicKey() { | ||
return this.publicKeyBuffer.toString('hex'); | ||
return (0, utils_1.bytesToHex)(this.publicKeyBytes); | ||
} | ||
get compressedPublicKeyBuffer() { | ||
return curves_1.getCurveByName(this.curve).compressPublicKey(this.publicKeyBuffer); | ||
get compressedPublicKeyBytes() { | ||
return (0, curves_1.getCurveByName)(this.curve).compressPublicKey(this.publicKeyBytes); | ||
} | ||
get compressedPublicKey() { | ||
return this.compressedPublicKeyBuffer.toString('hex'); | ||
return (0, utils_1.bytesToHex)(this.compressedPublicKeyBytes); | ||
} | ||
@@ -145,6 +151,6 @@ get address() { | ||
} | ||
return `0x${bip32_1.publicKeyToEthAddress(this.publicKeyBuffer).toString('hex')}`; | ||
return (0, utils_1.bytesToHex)((0, bip32_1.publicKeyToEthAddress)(this.publicKeyBytes)); | ||
} | ||
get fingerprint() { | ||
return utils_1.getFingerprint(this.compressedPublicKeyBuffer); | ||
return (0, utils_2.getFingerprint)(this.compressedPublicKeyBytes); | ||
} | ||
@@ -160,4 +166,4 @@ /** | ||
index: this.index, | ||
chainCode: this.chainCodeBuffer, | ||
publicKey: this.publicKeyBuffer, | ||
chainCode: this.chainCodeBytes, | ||
publicKey: this.publicKeyBytes, | ||
curve: this.curve, | ||
@@ -217,3 +223,3 @@ }); | ||
function validateBIP32Depth(depth) { | ||
if (!utils_1.isValidInteger(depth)) { | ||
if (!(0, utils_2.isValidInteger)(depth)) { | ||
throw new Error(`Invalid HD tree path depth: The depth must be a positive integer. Received: "${depth}".`); | ||
@@ -230,3 +236,3 @@ } | ||
function validateParentFingerprint(parentFingerprint) { | ||
if (!utils_1.isValidInteger(parentFingerprint)) { | ||
if (!(0, utils_2.isValidInteger)(parentFingerprint)) { | ||
throw new Error(`Invalid parent fingerprint: The fingerprint must be a positive integer. Received: "${parentFingerprint}".`); | ||
@@ -251,3 +257,3 @@ } | ||
validateBIP32Depth(newDepth); | ||
return await derivation_1.deriveKeyFromPath({ | ||
return await (0, derivation_1.deriveKeyFromPath)({ | ||
path, | ||
@@ -254,0 +260,0 @@ node, |
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" /> | ||
import { BIP32Node, ChangeHDPathString, CoinTypeHDPathString, CoinTypeToAddressTuple, HardenedBIP32Node, UnhardenedBIP32Node } from './constants'; | ||
@@ -93,34 +92,20 @@ /** | ||
/** | ||
* @param hexString - The hexadecimal string to strip. | ||
* @returns The hexadecimal string, without a `0x`-prefix, if any. | ||
*/ | ||
export declare function stripHexPrefix(hexString: string): string; | ||
/** | ||
* Tests whether the specified string is a valid hexadecimal string. The string | ||
* may or may not be `0x`-prefixed, and the test is case-insensitive. | ||
* | ||
* @param hexString - The string to test. | ||
* @returns Whether the specified string is a valid hexadecimal string. The | ||
* string may or may not be `0x`-prefixed. | ||
*/ | ||
export declare function isValidHexString(hexString: string): boolean; | ||
/** | ||
* @param hexString - The hexadecimal string to convert. | ||
* @returns The {@link Buffer} corresponding to the hexadecimal string. | ||
* @returns The `Uint8Array` corresponding to the hexadecimal string. | ||
*/ | ||
export declare function hexStringToBuffer(hexString: string | Buffer): Buffer; | ||
export declare function hexStringToBytes(hexString: string | Uint8Array): Uint8Array; | ||
/** | ||
* @param hexString - The hexadecimal string to convert. | ||
* @returns The {@link Buffer} corresponding to the hexadecimal string. | ||
* @returns The `Uint8Array` corresponding to the hexadecimal string. | ||
*/ | ||
export declare function nullableHexStringToBuffer(hexString?: string | Buffer): Buffer | undefined; | ||
export declare function nullableHexStringToBytes(hexString?: string | Uint8Array): Uint8Array | undefined; | ||
/** | ||
* Tests whether the specified {@link Buffer} is a valid BIP-32 key. | ||
* A valid buffer key is 64 bytes long and has at least one non-zero byte. | ||
* Tests whether the specified `Uint8Array` is a valid BIP-32 key. | ||
* A valid bytes key is 64 bytes long and has at least one non-zero byte. | ||
* | ||
* @param buffer - The {@link Buffer} to test. | ||
* @param expectedLength - The expected length of the buffer. | ||
* @returns Whether the buffer represents a valid BIP-32 key. | ||
* @param bytes - The `Uint8Array` to test. | ||
* @param expectedLength - The expected length of the Uint8Array. | ||
* @returns Whether the Uint8Array represents a valid BIP-32 key. | ||
*/ | ||
export declare function isValidBufferKey(buffer: Buffer, expectedLength: number): boolean; | ||
export declare function isValidBytesKey(bytes: Uint8Array, expectedLength: number): boolean; | ||
/** | ||
@@ -134,19 +119,12 @@ * Tests whether the specified number is a valid integer equal to or greater than 0. | ||
/** | ||
* Get a BigInt from a byte array. | ||
* | ||
* @param bytes - The byte array to get the BigInt for. | ||
* @returns The byte array as BigInt. | ||
*/ | ||
export declare function bytesToNumber(bytes: Uint8Array): bigint; | ||
/** | ||
* Get a Buffer from a hexadecimal string or Buffer. Validates that the | ||
* length of the Buffer matches the specified length, and that the buffer | ||
* Get a `Uint8Array` from a hexadecimal string or `Uint8Array`. Validates that the | ||
* length of the `Uint8Array` matches the specified length, and that the `Uint8Array` | ||
* is not empty. | ||
* | ||
* @param value - The value to convert to a Buffer. | ||
* @param length - The length to validate the Buffer against. | ||
* @param value - The value to convert to a `Uint8Array`. | ||
* @param length - The length to validate the `Uint8Array` against. | ||
*/ | ||
export declare function getBuffer(value: unknown, length: number): Buffer; | ||
export declare const decodeBase58check: (value: string) => Buffer; | ||
export declare const encodeBase58check: (value: Buffer) => string; | ||
export declare function getBytes(value: unknown, length: number): Uint8Array; | ||
export declare const decodeBase58check: (value: string) => Uint8Array; | ||
export declare const encodeBase58check: (value: Uint8Array) => string; | ||
/** | ||
@@ -157,3 +135,3 @@ * Get the fingerprint of a compressed public key as number. | ||
*/ | ||
export declare const getFingerprint: (publicKey: Buffer) => number; | ||
export declare const getFingerprint: (publicKey: Uint8Array) => number; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getFingerprint = exports.encodeBase58check = exports.decodeBase58check = exports.getBuffer = exports.bytesToNumber = exports.isValidInteger = exports.isValidBufferKey = exports.nullableHexStringToBuffer = exports.hexStringToBuffer = exports.isValidHexString = exports.stripHexPrefix = exports.isHardened = exports.isValidBIP32Index = exports.validateBIP32Index = exports.getBIP32NodeToken = exports.getUnhardenedBIP32NodeToken = exports.getHardenedBIP32NodeToken = exports.getBIP44CoinTypeToAddressPathTuple = exports.getBIP44ChangePathString = exports.getBIP44CoinTypePathString = void 0; | ||
const utils_1 = require("@noble/hashes/utils"); | ||
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 base_1 = require("@scure/base"); | ||
const sha256_1 = require("@noble/hashes/sha256"); | ||
const ripemd160_1 = require("@noble/hashes/ripemd160"); | ||
const utils_1 = require("@metamask/utils"); | ||
const constants_1 = require("./constants"); | ||
@@ -134,59 +134,36 @@ /** | ||
/** | ||
* @param hexString - The hexadecimal string to strip. | ||
* @returns The hexadecimal string, without a `0x`-prefix, if any. | ||
*/ | ||
function stripHexPrefix(hexString) { | ||
return hexString.replace(/^0x/iu, ''); | ||
} | ||
exports.stripHexPrefix = stripHexPrefix; | ||
/** | ||
* Tests whether the specified string is a valid hexadecimal string. The string | ||
* may or may not be `0x`-prefixed, and the test is case-insensitive. | ||
* | ||
* @param hexString - The string to test. | ||
* @returns Whether the specified string is a valid hexadecimal string. The | ||
* string may or may not be `0x`-prefixed. | ||
*/ | ||
function isValidHexString(hexString) { | ||
return /^(?:0x)?[a-f0-9]+$/iu.test(hexString); | ||
} | ||
exports.isValidHexString = isValidHexString; | ||
/** | ||
* @param hexString - The hexadecimal string to convert. | ||
* @returns The {@link Buffer} corresponding to the hexadecimal string. | ||
* @returns The `Uint8Array` corresponding to the hexadecimal string. | ||
*/ | ||
function hexStringToBuffer(hexString) { | ||
if (Buffer.isBuffer(hexString)) { | ||
function hexStringToBytes(hexString) { | ||
if (hexString instanceof Uint8Array) { | ||
return hexString; | ||
} | ||
if (typeof hexString !== 'string' || !isValidHexString(hexString)) { | ||
throw new Error(`Invalid hex string: "${hexString}".`); | ||
} | ||
return Buffer.from(stripHexPrefix(hexString), 'hex'); | ||
return (0, utils_1.hexToBytes)(hexString); | ||
} | ||
exports.hexStringToBuffer = hexStringToBuffer; | ||
exports.hexStringToBytes = hexStringToBytes; | ||
/** | ||
* @param hexString - The hexadecimal string to convert. | ||
* @returns The {@link Buffer} corresponding to the hexadecimal string. | ||
* @returns The `Uint8Array` corresponding to the hexadecimal string. | ||
*/ | ||
function nullableHexStringToBuffer(hexString) { | ||
function nullableHexStringToBytes(hexString) { | ||
if (hexString !== undefined) { | ||
return hexStringToBuffer(hexString); | ||
return hexStringToBytes(hexString); | ||
} | ||
return undefined; | ||
} | ||
exports.nullableHexStringToBuffer = nullableHexStringToBuffer; | ||
exports.nullableHexStringToBytes = nullableHexStringToBytes; | ||
/** | ||
* Tests whether the specified {@link Buffer} is a valid BIP-32 key. | ||
* A valid buffer key is 64 bytes long and has at least one non-zero byte. | ||
* Tests whether the specified `Uint8Array` is a valid BIP-32 key. | ||
* A valid bytes key is 64 bytes long and has at least one non-zero byte. | ||
* | ||
* @param buffer - The {@link Buffer} to test. | ||
* @param expectedLength - The expected length of the buffer. | ||
* @returns Whether the buffer represents a valid BIP-32 key. | ||
* @param bytes - The `Uint8Array` to test. | ||
* @param expectedLength - The expected length of the Uint8Array. | ||
* @returns Whether the Uint8Array represents a valid BIP-32 key. | ||
*/ | ||
function isValidBufferKey(buffer, expectedLength) { | ||
if (buffer.length !== expectedLength) { | ||
function isValidBytesKey(bytes, expectedLength) { | ||
if (bytes.length !== expectedLength) { | ||
return false; | ||
} | ||
for (const byte of buffer) { | ||
for (const byte of bytes) { | ||
if (byte !== 0) { | ||
@@ -198,3 +175,3 @@ return true; | ||
} | ||
exports.isValidBufferKey = isValidBufferKey; | ||
exports.isValidBytesKey = isValidBytesKey; | ||
/** | ||
@@ -211,44 +188,31 @@ * Tests whether the specified number is a valid integer equal to or greater than 0. | ||
/** | ||
* Get a BigInt from a byte array. | ||
* | ||
* @param bytes - The byte array to get the BigInt for. | ||
* @returns The byte array as BigInt. | ||
*/ | ||
function bytesToNumber(bytes) { | ||
return BigInt(`0x${utils_1.bytesToHex(bytes)}`); | ||
} | ||
exports.bytesToNumber = bytesToNumber; | ||
/** | ||
* Get a Buffer from a hexadecimal string or Buffer. Validates that the | ||
* length of the Buffer matches the specified length, and that the buffer | ||
* Get a `Uint8Array` from a hexadecimal string or `Uint8Array`. Validates that the | ||
* length of the `Uint8Array` matches the specified length, and that the `Uint8Array` | ||
* is not empty. | ||
* | ||
* @param value - The value to convert to a Buffer. | ||
* @param length - The length to validate the Buffer against. | ||
* @param value - The value to convert to a `Uint8Array`. | ||
* @param length - The length to validate the `Uint8Array` against. | ||
*/ | ||
function getBuffer(value, length) { | ||
if (value instanceof Buffer) { | ||
validateBuffer(value, length); | ||
function getBytes(value, length) { | ||
if (value instanceof Uint8Array) { | ||
validateBytes(value, length); | ||
return value; | ||
} | ||
if (typeof value === 'string') { | ||
if (!isValidHexString(value)) { | ||
throw new Error(`Invalid value: Must be a valid hex string of length: ${length * 2}.`); | ||
} | ||
const buffer = hexStringToBuffer(value); | ||
validateBuffer(buffer, length); | ||
return buffer; | ||
const bytes = (0, utils_1.hexToBytes)(value); | ||
validateBytes(bytes, length); | ||
return bytes; | ||
} | ||
throw new Error(`Invalid value: Expected a Buffer or hexadecimal string.`); | ||
throw new Error(`Invalid value: Expected an instance of Uint8Array or hexadecimal string.`); | ||
} | ||
exports.getBuffer = getBuffer; | ||
function validateBuffer(buffer, length) { | ||
if (!isValidBufferKey(buffer, length)) { | ||
throw new Error(`Invalid value: Must be a non-zero ${length}-byte buffer.`); | ||
exports.getBytes = getBytes; | ||
function validateBytes(bytes, length) { | ||
if (!isValidBytesKey(bytes, length)) { | ||
throw new Error(`Invalid value: Must be a non-zero ${length}-byte byte array.`); | ||
} | ||
} | ||
const decodeBase58check = (value) => { | ||
const base58Check = base_1.base58check(sha256_1.sha256); | ||
const base58Check = (0, base_1.base58check)(sha256_1.sha256); | ||
try { | ||
return Buffer.from(base58Check.decode(value)); | ||
return base58Check.decode(value); | ||
} | ||
@@ -261,3 +225,3 @@ catch (_a) { | ||
const encodeBase58check = (value) => { | ||
const base58Check = base_1.base58check(sha256_1.sha256); | ||
const base58Check = (0, base_1.base58check)(sha256_1.sha256); | ||
return base58Check.encode(value); | ||
@@ -272,8 +236,10 @@ }; | ||
const getFingerprint = (publicKey) => { | ||
if (!isValidBufferKey(publicKey, 33)) { | ||
throw new Error(`Invalid public key: The key must be a 33-byte, non-zero Buffer.`); | ||
if (!isValidBytesKey(publicKey, 33)) { | ||
throw new Error(`Invalid public key: The key must be a 33-byte, non-zero byte array.`); | ||
} | ||
return Buffer.from(ripemd160_1.ripemd160(sha256_1.sha256(publicKey))).readUInt32BE(0); | ||
const bytes = (0, ripemd160_1.ripemd160)((0, sha256_1.sha256)(publicKey)); | ||
const view = (0, utils_1.createDataView)(bytes); | ||
return view.getUint32(0, false); | ||
}; | ||
exports.getFingerprint = getFingerprint; | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "@metamask/key-tree", | ||
"version": "5.0.2", | ||
"version": "6.0.0", | ||
"description": "An interface over BIP-32 and BIP-39 key derivation paths.", | ||
@@ -18,9 +18,9 @@ "repository": { | ||
"build:clean": "rimraf dist && yarn build", | ||
"build:docs": "typedoc", | ||
"lint": "yarn lint:eslint && yarn lint:misc --check", | ||
"lint:eslint": "eslint . --cache --ext js,ts", | ||
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write", | ||
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' --ignore-path .gitignore", | ||
"prepublishOnly": "yarn build:clean && yarn lint && yarn test", | ||
"setup": "yarn install", | ||
"test": "jest", | ||
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' '!.yarnrc.yml' --ignore-path .gitignore --no-error-on-unmatched-pattern", | ||
"prepack": "./scripts/prepack.sh", | ||
"test": "jest && jest-it-up", | ||
"test:watch": "jest --watch" | ||
@@ -40,2 +40,3 @@ }, | ||
"dependencies": { | ||
"@metamask/utils": "^3.3.0", | ||
"@noble/ed25519": "^1.6.0", | ||
@@ -65,2 +66,3 @@ "@noble/hashes": "^1.0.0", | ||
"jest": "^27.2.5", | ||
"jest-it-up": "^2.0.2", | ||
"lint-staged": "^13.0.3", | ||
@@ -72,3 +74,4 @@ "prettier": "^2.4.1", | ||
"ts-jest": "^27.0.6", | ||
"typescript": "4.3.5" | ||
"typedoc": "^0.23.19", | ||
"typescript": "~4.7.4" | ||
}, | ||
@@ -75,0 +78,0 @@ "packageManager": "yarn@3.2.3", |
@@ -81,3 +81,3 @@ # @metamask/key-tree | ||
// These are Node.js Buffer representations of the extended private keys for | ||
// These are Uint8Array representations of the extended private keys for | ||
// the respective addresses. | ||
@@ -84,0 +84,0 @@ |
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
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
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
241760
6
26
2828
+ Added@metamask/utils@^3.3.0
+ Added@metamask/utils@3.6.0(transitive)
+ Added@types/debug@4.1.12(transitive)
+ Added@types/ms@0.7.34(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addedms@2.1.3(transitive)
+ Addedsemver@7.6.3(transitive)
+ Addedsuperstruct@1.0.4(transitive)