Fixed HDNodeWallet mnemonic assertion.

This commit is contained in:
Richard Moore 2022-11-09 05:57:00 -05:00
parent e09c14495f
commit 6f40dfdf77

@ -3,7 +3,7 @@ import { VoidSigner } from "../providers/index.js";
import { computeAddress } from "../transaction/index.js"; import { computeAddress } from "../transaction/index.js";
import { import {
concat, dataSlice, decodeBase58, defineProperties, encodeBase58, concat, dataSlice, decodeBase58, defineProperties, encodeBase58,
getBytes, hexlify, getBytes, hexlify, isBytesLike,
getNumber, toBigInt, toHex, getNumber, toBigInt, toHex,
assertPrivate, assert, assertArgument assertPrivate, assert, assertArgument
} from "../utils/index.js"; } from "../utils/index.js";
@ -74,9 +74,7 @@ type HDNodeLike<T> = { depth: number, deriveChild: (i: number) => T };
function derivePath<T extends HDNodeLike<T>>(node: T, path: string): T { function derivePath<T extends HDNodeLike<T>>(node: T, path: string): T {
const components = path.split("/"); const components = path.split("/");
if (components.length === 0 || (components[0] === "m" && node.depth !== 0)) { assertArgument(components.length > 0 && (components[0] === "m" || node.depth > 0), "invalid path", "path", path);
throw new Error("invalid path - " + path);
}
if (components[0] === "m") { components.shift(); } if (components[0] === "m") { components.shift(); }
@ -86,16 +84,16 @@ function derivePath<T extends HDNodeLike<T>>(node: T, path: string): T {
if (component.match(/^[0-9]+'$/)) { if (component.match(/^[0-9]+'$/)) {
const index = parseInt(component.substring(0, component.length - 1)); const index = parseInt(component.substring(0, component.length - 1));
if (index >= HardenedBit) { throw new Error("invalid path index - " + component); } assertArgument(index < HardenedBit, "invalid path index", `path[${ i }]`, component);
result = result.deriveChild(HardenedBit + index); result = result.deriveChild(HardenedBit + index);
} else if (component.match(/^[0-9]+$/)) { } else if (component.match(/^[0-9]+$/)) {
const index = parseInt(component); const index = parseInt(component);
if (index >= HardenedBit) { throw new Error("invalid path index - " + component); } assertArgument(index < HardenedBit, "invalid path index", `path[${ i }]`, component);
result = result.deriveChild(index); result = result.deriveChild(index);
} else { } else {
throw new Error("invalid path component - " + component); assertArgument(false, "invalid path component", `path[${ i }]`, component);
} }
} }
@ -143,7 +141,7 @@ export class HDNodeWallet extends BaseWallet {
// - Mainnet: public=0x0488B21E, private=0x0488ADE4 // - Mainnet: public=0x0488B21E, private=0x0488ADE4
// - Testnet: public=0x043587CF, private=0x04358394 // - Testnet: public=0x043587CF, private=0x04358394
if (this.depth >= 256) { throw new Error("Depth too large!"); } assert(this.depth < 256, "Depth too deep", "UNSUPPORTED_OPERATION", { operation: "extendedKey" });
return encodeBase58Check(concat([ return encodeBase58Check(concat([
"0x0488ADE4", zpad(this.depth, 1), this.parentFingerprint, "0x0488ADE4", zpad(this.depth, 1), this.parentFingerprint,
@ -162,7 +160,7 @@ export class HDNodeWallet extends BaseWallet {
deriveChild(_index: Numeric): HDNodeWallet { deriveChild(_index: Numeric): HDNodeWallet {
const index = getNumber(_index, "index"); const index = getNumber(_index, "index");
if (index > 0xffffffff) { throw new Error("invalid index - " + String(index)); } assertArgument(index <= 0xffffffff, "invalid index", "index", index);
// Base path // Base path
let path = this.path; let path = this.path;
@ -184,10 +182,10 @@ export class HDNodeWallet extends BaseWallet {
} }
static #fromSeed(_seed: BytesLike, mnemonic: null | Mnemonic): HDNodeWallet { static #fromSeed(_seed: BytesLike, mnemonic: null | Mnemonic): HDNodeWallet {
assertArgument(isBytesLike(_seed), "invalid seed", "seed", "[REDACTED]");
const seed = getBytes(_seed, "seed"); const seed = getBytes(_seed, "seed");
if (seed.length < 16 || seed.length > 64) { assertArgument(seed.length >= 16 && seed.length <= 64 , "invalid seed", "seed", "[REDACTED]");
throw new Error("invalid seed");
}
const I = getBytes(computeHmac("sha512", MasterSecret, seed)); const I = getBytes(computeHmac("sha512", MasterSecret, seed));
const signingKey = new SigningKey(hexlify(I.slice(0, 32))); const signingKey = new SigningKey(hexlify(I.slice(0, 32)));
@ -285,7 +283,7 @@ export class HDNodeVoidWallet extends VoidSigner {
// - Mainnet: public=0x0488B21E, private=0x0488ADE4 // - Mainnet: public=0x0488B21E, private=0x0488ADE4
// - Testnet: public=0x043587CF, private=0x04358394 // - Testnet: public=0x043587CF, private=0x04358394
if (this.depth >= 256) { throw new Error("Depth too large!"); } assert(this.depth < 256, "Depth too deep", "UNSUPPORTED_OPERATION", { operation: "extendedKey" });
return encodeBase58Check(concat([ return encodeBase58Check(concat([
"0x0488B21E", "0x0488B21E",
@ -301,7 +299,7 @@ export class HDNodeVoidWallet extends VoidSigner {
deriveChild(_index: Numeric): HDNodeVoidWallet { deriveChild(_index: Numeric): HDNodeVoidWallet {
const index = getNumber(_index, "index"); const index = getNumber(_index, "index");
if (index > 0xffffffff) { throw new Error("invalid index - " + String(index)); } assertArgument(index <= 0xffffffff, "invalid index", "index", index);
// Base path // Base path
let path = this.path; let path = this.path;