Skip to content

feat: add native Bitcoin HD keyring with BIP-84 derivation#601

Open
devin-ai-integration[bot] wants to merge 3 commits intomainfrom
devin/1777413229-native-bitcoin-keyring-integration
Open

feat: add native Bitcoin HD keyring with BIP-84 derivation#601
devin-ai-integration[bot] wants to merge 3 commits intomainfrom
devin/1777413229-native-bitcoin-keyring-integration

Conversation

@devin-ai-integration
Copy link
Copy Markdown

@devin-ai-integration devin-ai-integration Bot commented Apr 28, 2026

Summary

Adds a native Bitcoin HD keyring with BIP-84 derivation for P2WPKH (native SegWit) addresses. Registers it conditionally in Engine behind the bitcoin preprocessor flag.

New module app/core/BitcoinKeyring/:

  • types.tsBitcoinKeyringState, BitcoinKeyringAccount, BitcoinNetwork enum
  • BitcoinKeyring.ts — Keyring using @metamask/key-tree (BIP-84), @noble/hashes (SHA-256 + RIPEMD-160), and bech32 for P2WPKH addresses. Supports serialize/deserialize, addAccounts, getAccounts, exportAccount, removeAccount. Signing stubs throw (TODO).
  • index.ts — Exports + bitcoinKeyringBuilder() factory
  • BitcoinKeyring.test.ts — 19 unit tests

Modified files:

  • app/constants/keyringTypes.ts — Added bitcoin = 'Bitcoin HD Key Tree'
  • app/core/Engine/Engine.ts — Conditional import + registration of Bitcoin keyring builder

Review & Testing Checklist for Human

  • Verify BIP-84 address derivation produces correct P2WPKH addresses for the test mnemonic (abandon abandon...about) — cross-reference with a known BIP-84 derivation tool
  • Verify removeAccount followed by addAccounts produces unique addresses (no index collision) — covered by test but worth manual verification
  • Confirm the bech32 import resolves correctly at runtime (it's a transitive dep via bitcoin-address-validation, pinned in resolutions)
  • Verify the ///: BEGIN:ONLY_INCLUDE_IF(bitcoin) preprocessor directives in Engine.ts are correctly placed and don't break non-bitcoin builds

Notes

  • #nextDeriveIndex tracks BIP-84 derivation index independently of #accounts array length, preventing duplicate derivation after removeAccount (fix for Devin Review finding)
  • nextDeriveIndex is persisted in serialized state with backward-compatible fallback to numberOfAccounts
  • CI failures are pre-existing: react-native-tcp GitHub tarball returns 404, blocking yarn install in all CI jobs. Not caused by this PR.
  • All local checks pass: yarn lint, tsc --noEmit (zero errors), 19/19 unit tests

Link to Devin session: https://app.devin.ai/sessions/e42aa97910c14dd69abbaae28747b0e6
Requested by: @dr-phil


Open in Devin Review

Implements a native Bitcoin keyring for P2WPKH (native SegWit) address
derivation using BIP-84. The keyring is conditionally registered in the
Engine behind the 'bitcoin' feature flag.

Changes:
- Add BitcoinKeyring class with BIP-84 key derivation using @metamask/key-tree
- Generate P2WPKH addresses via @noble/hashes (SHA-256 + RIPEMD-160) and bech32
- Support mainnet (bc1) and testnet (tb1) address generation
- Add 'bitcoin' entry to ExtendedKeyringTypes enum
- Register bitcoinKeyringBuilder in Engine (behind ONLY_INCLUDE_IF(bitcoin))
- Add comprehensive unit tests (18 tests covering all keyring operations)
- Transaction/message signing marked as TODOs (throw not-implemented)

Co-Authored-By: Phil Bedford <phil.bedford@cognition.ai>
@devin-ai-integration
Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@staging-devin-ai-integration
Copy link
Copy Markdown

Devin Review

Status Commit
⚪ Not started

Open in Devin Review (Staging)

💡 Connect your GitHub account to enable automatic code reviews.

.split('/')
.map((part) => {
const hardened = part.endsWith("'");
const index = parseInt(part.replace("'", ''), 10);
devin-ai-integration[bot]

This comment was marked as resolved.

…moveAccount

Co-Authored-By: Phil Bedford <phil.bedford@cognition.ai>
devin-ai-integration[bot]

This comment was marked as resolved.

…ialize after removeAccount

Co-Authored-By: Phil Bedford <phil.bedford@cognition.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants