From 556fdd91d9b6bf7db4041bb099e66b2080e1a985 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Wed, 14 Feb 2024 13:11:54 -0500 Subject: [PATCH] Throw an error when attempting to derive from a master path from a non-master node (#4551). --- src.ts/wallet/hdwallet.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src.ts/wallet/hdwallet.ts b/src.ts/wallet/hdwallet.ts index 67b35cb2e8..119b0cedd5 100644 --- a/src.ts/wallet/hdwallet.ts +++ b/src.ts/wallet/hdwallet.ts @@ -87,9 +87,12 @@ type HDNodeLike = { depth: number, deriveChild: (i: number) => T }; function derivePath>(node: T, path: string): T { const components = path.split("/"); - assertArgument(components.length > 0 && (components[0] === "m" || node.depth > 0), "invalid path", "path", path); + assertArgument(components.length > 0, "invalid path", "path", path); - if (components[0] === "m") { components.shift(); } + if (components[0] === "m") { + assertArgument(node.depth === 0, `cannot derive root path (i.e. path starting with "m/") for a node at non-zero depth ${ node.depth }`, "path", path); + components.shift(); + } let result: T = node; for (let i = 0; i < components.length; i++) {