GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Error instead of overflowing depth byte when deriving past BIP32 max depth by SachinMeier · Pull Request #130 · RiverFinancial/bitcoinex

Conversation

@SachinMeier

@SachinMeier SachinMeier commented Aug 5, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

Bug

BIP32 encodes an extended key's depth as a single byte, so a depth-255 key cannot have children. incr/1 did not account for this: deriving a child of a depth-255 key encoded depth 256 as 2 bytes, producing a 79-byte payload that parse_extended_key/1 rejects. The derivation returned the misleading {:error, "error parsing key"}.

Fix

Guard both derivation paths with an explicit check (@max_depth <<255>>):

  • derive_private_child/2: new function head returning {:error, "cannot derive child: parent is at max depth (255)"}.
  • derive_public_child/2: the same check as the first cond clause, so both the xprv (CKDpriv+neuter) and xpub (CKDpub) paths error before any depth arithmetic.

derive_child_key/2 and derive_extended_key/2 delegate to these and already propagate error tuples.

Independent of #127 (the child-key padding fix); expect a trivial CHANGELOG.md conflict for whichever merges second.

Tests

  • Depth-255 xprv and xpub (real BIP32 vector-1 keys with the depth byte rewritten and re-serialized): all five entry points (derive_private_child, derive_public_child from xprv and xpub, derive_child_key, derive_extended_key) return the new error. Fails against the unfixed code.
  • Boundary: depth-254 keys still derive children successfully with depth == <<255>>.

🤖 Generated with Claude Code

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@SachinMeier
SachinMeier marked this pull request as ready for review August 5, 2026 18:38
@SachinMeier
SachinMeier force-pushed the sachin--bip32-max-depth-error branch from e7521af to 1d25519 Compare August 5, 2026 18:41
@SachinMeier
SachinMeier changed the base branch from sachin--fix-bip32-child-key-padding to master August 5, 2026 18:42
jamesob
jamesob reviewed Aug 6, 2026
View reviewed changes
Comment thread lib/extendedkey.ex Outdated
@spec derive_public_child(t(), non_neg_integer) :: {:ok, t()} | {:error, String.t()}
def derive_public_child(xkey, idx) do
cond do
xkey.depth == @max_depth ->

@jamesob jamesob Aug 6, 2026

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any value in >= vs. ==?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

SachinMeier reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
SachinMeier added a commit that referenced this pull request Aug 7, 2026
@SachinMeier @claude
Fix max-depth guard to reject depths above 255, not just equal to it
a652a10
The depth-255 check compared against the literal <<255>> binary, so a key
whose depth binary had already carried past one byte slipped through
(James' review on #130).

The unified `next_depth_valid/1` guard now matches on the binary
representation: a depth is derivable only if it is exactly one byte and
not <<0xFF>>. Anything longer is already above max, which is the shape
incr/1 produces once it carries past 255. Guarding on `depth >>> 8` does
not work here since depth is a binary, not an integer -- the shift raises
inside the guard, which silently fails the clause and disables the check.

Test coverage for the above-max case builds the multi-byte depth directly;
such a depth cannot round-trip through serialize/parse, and <<256>>
truncates to <<0>>.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
SachinMeier added a commit that referenced this pull request Aug 18, 2026
@SachinMeier @claude
Fix max-depth guard to reject depths above 255, not just equal to it
f91aea2
The depth-255 check compared against the literal <<255>> binary, so a key
whose depth binary had already carried past one byte slipped through
(James' review on #130).

The unified `next_depth_valid/1` guard now matches on the binary
representation: a depth is derivable only if it is exactly one byte and
not <<0xFF>>. Anything longer is already above max, which is the shape
incr/1 produces once it carries past 255. Guarding on `depth >>> 8` does
not work here since depth is a binary, not an integer -- the shift raises
inside the guard, which silently fails the clause and disables the check.

Test coverage for the above-max case builds the multi-byte depth directly;
such a depth cannot round-trip through serialize/parse, and <<256>>
truncates to <<0>>.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@SachinMeier
SachinMeier force-pushed the sachin--bip32-max-depth-error branch from a652a10 to f91aea2 Compare August 18, 2026 16:41
SachinMeier and others added 3 commits November 10, 2026 21:00
@SachinMeier @claude
Return error when deriving child of a depth-255 extended key
e4f789c
BIP32 depth is a single byte. Deriving a child of a depth-255 key
overflowed incr/1 into a 2-byte depth encoding, producing an
unparseable serialization and the misleading error "error parsing
key". Guard both derivation paths and return a clear error instead.

Co-Authored-By: Claude Fable 5 <[email protected]>
@SachinMeier @claude
Fix max-depth guard to reject depths above 255, not just equal to it
a0647bf
The depth-255 check compared against the literal <<255>> binary, so a key
whose depth binary had already carried past one byte slipped through
(James' review on #130).

The unified `next_depth_valid/1` guard now matches on the binary
representation: a depth is derivable only if it is exactly one byte and
not <<0xFF>>. Anything longer is already above max, which is the shape
incr/1 produces once it carries past 255. Guarding on `depth >>> 8` does
not work here since depth is a binary, not an integer -- the shift raises
inside the guard, which silently fails the clause and disables the check.

Test coverage for the above-max case builds the multi-byte depth directly;
such a depth cannot round-trip through serialize/parse, and <<256>>
truncates to <<0>>.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@SachinMeier @claude
Make derivation guards private with defguardp
740b031
next_depth_valid/1 and idx_valid/1 are internal-only; defguard exported
them as public macros in the module API.

Co-Authored-By: Claude Fable 5 <[email protected]>
@sachinmeier-bot
sachinmeier-bot Bot force-pushed the sachin--bip32-max-depth-error branch from fe74943 to 740b031 Compare November 11, 2026 02:03
davidcaseria
davidcaseria approved these changes Nov 11, 2026
View reviewed changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@davidcaseria davidcaseria davidcaseria approved these changes
+1 more reviewer
@jamesob jamesob jamesob left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@SachinMeier @jamesob @davidcaseria
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.