Add watch-only Bitcoin public keys and address validation
Bitcoin wallet stack, part 2 of 5 · #5620. Merged into main. Part 3 (#4712) uses these public-key and address checks.
Review order: #4613 and #4712 (after merged #4615) → #5621 → #5622. Native stack with one diff per layer · Dependency table.
Summary
Part 2 of the split of #3753 agreed in https://github.com/we-promise/sure/pull/3753#issuecomment-6248037835.
Onchain::BitcoinPublicKey:
- parses a mainnet account-level xpub or zpub and derives BIP84 (native SegWit) receive and change addresses with BIP32 public derivation (CKDpub);
- accepts a hardened account node at depth 3 (BIP84
m/84'/0'/n') or, at depth 1, onlym/0'(Electrum segwit seeds export their zpub there, and it derives the same addresses); - validates standard mainnet addresses (P2PKH, P2SH, P2WPKH, P2WSH, P2TR) by checksum;
- rejects private keys (xprv/zprv), other networks, other depths, non-hardened nodes, depth-1 nodes other than
m/0'and corrupt checksums before any derivation.
Onchain::BitcoinAdapter (the existing single-address flow) now uses the same checksum validation and canonicalization instead of its own regexes. A mistyped address used to link and then sync as empty; now it is refused. Mixed-case Bech32 input is rejected, as BIP173 requires, instead of being silently lowercased.
It uses Ruby's OpenSSL (secp256k1, HMAC-SHA512, RIPEMD160) and the bech32 gem instead of a full Bitcoin library. bech32's only dependency, thor, is already bundled, so the lockfile adds one gem.
Key derivation has no caller yet. The wallet models in part 3 use it.
Validation
test/models/onchain/bitcoin_public_key_test.rbpins the BIP84 and BIP32 public test vectors, each accepted and rejected address type, the accepted key depths, canonicalization and the derivation guards.test/models/onchain/bitcoin_adapter_test.rbadds a mistyped address that the old regex accepted.- Full unit suite locally on the latest
mainfor the first commit, run per directory (one parallel run hangs in my Docker setup): 13,724 runs, 0 failures, 0 errors. For the follow-ups: every test file that touches on-chain code, 454 runs, 0 failures. - RuboCop: no offenses.
Summary by cubic
Adds Onchain::BitcoinPublicKey, a watch-only model that parses mainnet account-level xpub/zpub keys, derives BIP84 native SegWit receive and change addresses via BIP32 public derivation, and validates standard mainnet address types (P2PKH, P2SH, P2WPKH, P2WSH, P2TR) by checksum. Private keys, other networks, wrong depths, and corrupt checksums are rejected before any derivation. Part 2 of the stacked split of #3753.
Onchain::BitcoinAdapternow delegates address validation and canonicalization to this model, so mistyped addresses fail the checksum instead of linking then syncing as empty; the duplicate address rules are gone.- Accepts hardened account nodes at depth 3 (BIP84
m/84'/0'/n') or Electrum's depth-1 segwit accountm/0'only; any other hardened depth-one node, such as a BIP84 purpose key atm/84', is rejected.
Dependencies
- Adds
bech321.5.1; its only dependencythoris already bundled, so the lockfile gains one gem. - Derivation and hashing use Ruby's OpenSSL (secp256k1, HMAC-SHA512, RIPEMD160) rather than a full Bitcoin library.
Summary by CodeRabbit
- New Features
- Added support for deriving mainnet Native SegWit receive and change addresses from account-level public keys.
- Added public key fingerprinting and validation for standard mainnet Bitcoin address types, including legacy, SegWit, and Taproot.
- Added consistent normalization for uniformly uppercase SegWit addresses.
- Bug Fixes
- Improved rejection of malformed addresses, including invalid checksums, unsupported networks, and invalid casing. Address validation also trims surrounding whitespace.