Skip to content

Allows deriving keys that can't be seriliazed due to overflowing depth #92

Closed as not planned

Description

@quapka

Hi,
extended keys at depths of 256 and higher can be derived. But cannot be serialized. This is compliant with BIP32 specification, but is allowing the derivation of such keys intentional?

See the following tests for examples:

diff --git a/test/index.js b/test/index.js
index 1f16f65..6b9096a 100644
--- a/test/index.js
+++ b/test/index.js
@@ -226,6 +226,38 @@ tape('fromSeed', (t) => {
   t.end()
 })

+tape('derives keys with overflowing depth', (t) => {
+  let hd = BIP32.fromSeed(Buffer.from('000102030405060708090a0b0c0d0e0f', 'hex'))
+
+  for (let i = 0; i < 255; i++) {
+    hd = hd.derive(0);
+  }
+  // Extended key at 255th is serializable
+  t.equal(hd.depth, BIP32.fromBase58(hd.toBase58()).depth);
+
+  hd = hd.derive(0);
+  t.equal(hd.depth, 256);
+
+  // throws ERR_OUT_OF_RANGE
+  hd.toBase58();
+  t.end()
+})
+
+tape('derives keys from path with overflowing depth', (t) => {
+  let hd = BIP32.fromSeed(Buffer.from('000102030405060708090a0b0c0d0e0f', 'hex'))
+
+  hd = hd.derivePath("m" + "/0".repeat(255));
+  // Extended key at 255th is serializable
+  t.equal(hd.depth, BIP32.fromBase58(hd.toBase58()).depth);
+
+  hd = hd.derive(0);
+  // Extended key at 256th depth that overflows a byte
+  t.equal(hd.depth, 256);
+
+  // throws ERR_OUT_OF_RANGE
+  hd.toBase58();
+  t.end()
+})
+
 tape('ecdsa', (t) => {
   let seed = Buffer.alloc(32, 1)
   let hash = Buffer.alloc(32, 2)

Similar cases in other libraries can be seen in bitcoin/bitcoin#32201.

Activity

  1. junderw commented on Apr 6, 2025

    @junderw
    Member

    This is not intentional, but it is also not a problem for normal use.

    Since the BIP makes no restrictions on the depth outside the implied limit of the serialization, I see no reason to limit the depth.

    If the BIP32 document changes I will consider fixing it.

    Thanks for the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions