Skip to content

refactor(crypto): drop btcd/btcutil dependency, inline BIP32 derivation#723

Merged
cheggaaa merged 1 commit into
mainfrom
remove-btcd-dependency
Jul 6, 2026
Merged

refactor(crypto): drop btcd/btcutil dependency, inline BIP32 derivation#723
cheggaaa merged 1 commit into
mainfrom
remove-btcd-dependency

Conversation

@cheggaaa

@cheggaaa cheggaaa commented Jul 6, 2026

Copy link
Copy Markdown
Member

Removes github.com/btcsuite/btcd and github.com/btcsuite/btcutil entirely (replaces #567).

The only usage was hdkeychain in ethereumKeyFromMnemonic (Ethereum key derivation for Any Naming Service). This PR replaces it with a ~100-line implementation in util/crypto/bip32.go built on stdlib HMAC-SHA512 and decred/dcrd/dcrec/secp256k1/v4, which was already in the dependency graph via libp2p (it just moves from indirect to direct).

Compatibility — derived keys must stay bit-for-bit identical, since user Ethereum identities depend on them. The old code used DeriveNonStandard, which carries the btcsuite issue #172 quirk (intermediate private keys shorter than 32 bytes are used unpadded in hardened derivation, affecting ~1/256 of derivation steps). The reimplementation reproduces this exactly. Verified by differential fuzzing against the old library:

  • 40,000 random seed/path derivations (851 naturally hitting the short-key quirk) — byte-identical
  • 10,455 derivations from deliberately constructed short (25–31 byte) parent keys — byte-identical
  • 500 end-to-end mnemonic → Ethereum key derivations through the public API — byte-identical
  • existing fixed test vectors (TestMnemonic_ethereumKeyFromMnemonic) pass unchanged

Why: the btcd module is a huge dependency that keeps triggering security/license red flags (e.g. the dependabot bump in #567 fails to even build, because btcec moved to a separate module in btcd ≥ 0.23).

🤖 Generated with Claude Code

The only btcsuite usage was hdkeychain in ethereumKeyFromMnemonic. Replace
it with a minimal in-house implementation (util/crypto/bip32.go) on top of
decred/secp256k1, which was already in the dependency graph via libp2p.

The reimplementation reproduces hdkeychain DeriveNonStandard bit-for-bit,
including the btcsuite issue #172 quirk (unpadded short keys in hardened
derivation), so existing Ethereum identities derive identically. Verified
by differential fuzzing against the old library: 40k random derivations,
10k forced short-key derivations, 500 end-to-end mnemonic derivations —
all byte-identical.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

New Coverage 58.6% of statements
Patch Coverage 90.0% of changed statements (36/40)

Coverage provided by https://github.com/seriousben/go-patch-cover-action

@cheggaaa cheggaaa merged commit 8c5e178 into main Jul 6, 2026
4 checks passed
@cheggaaa cheggaaa deleted the remove-btcd-dependency branch July 6, 2026 13:32
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 6, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants