Support multi-address Bitcoin wallets in existing crypto accounts

Draft
VL
14d ago
+5077-100

Reference draft for the five-part Bitcoin wallet stack. The individual pull requests and their review order are tracked in #5620.

Review order: #4613 and #4712 (after merged #4615) → #5621 → #5622. Native stack with one diff per layer · Dependency table.

Problem and resulting behavior

Tracking one Bitcoin receive address under-reports an HD wallet after change moves to another address. This Preview feature connects public addresses or account-level Native SegWit xpub/zpub sources to an existing Crypto account, discovers receive and change branches, and publishes the combined BTC quantity. Multiple sources share ownership accounting, so overlapping addresses and internal transfers are counted once. Follow-up to #3081.

The wallet publishes one BTC position through Sure's shared holding and balance materializers. Cash, other assets, cost basis, valuations and earlier chart history retain the standard accounting rules. Foreign-currency entries use dated/custom FX conversion; split parents are excluded; manual/provider/calculated basis and unknown-cost rules remain intact. Disconnecting preserves the account and ledger for manual tracking.

Implementation

  • Position-only provider capabilities define which securities a publisher owns. Cash-only anchors preserve the existing cash and ledger baseline, while full-provider imports update their cash without replacing separately published positions. Superseded same-day total valuations become authoritative again after an explicit monetary/date edit.
  • BTC transfers and coverage reconciliations have zero cash amounts and explicit provenance. Confirmation, RBF, reorg recovery and source changes reconcile the dated journal without manufacturing market profit. Provider-owned quantity, deletion and remapping are guarded; unrelated manual activity and basis remain editable.
  • Discovery checkpoints, transient retries and account materialization participate in the existing Sync parent/child tree, including cancellation and completion. Jobs use the family's timezone. Network reads stay outside row-lock transactions; source membership is rechecked before publication, and later ledger data receives a follow-up account sync.
  • Current positions combine manual assets, position publishers and complete-provider snapshots. Position owners take precedence over newer complete-provider rows for the same security. Historical snapshots are not copied backward; omitted full-provider assets stay omitted. Incremental materialization preserves rows outside the requested window, and address counts use aggregate queries rather than loading every discovered address.
  • The UI polls discovery/sync automatically, separates confirmed and pending quantity, explains stale reads and pricing failures, hides HD-only inputs for address sources, and bounds the address disclosure. Existing account sync continues when Preview is disabled.

BIP84/mainnet HD discovery uses bitcoinrb 1.14.0; base64 0.2.0 is its required dependency. Gap limit defaults to 20 and supports up to 1,000, with at most 200 discovery checks per job. An xpub covers one Bitcoin account; other accounts require additional sources. Extended public keys are encrypted at rest, filtered from logs, omitted from API responses and never sent to the explorer. Official public vectors are used in tests. No seed/private-key storage, signing or broadcasting is added.

Pricing uses Sure's security and FX providers, falling back to known manual quotes. When valuation cannot be established, the previous account valuation is retained and a diagnostic is shown. The existing single-address and other-chain flows retain their standalone behavior.

Migrations and validation

Three additive Rails 8.1 migrations create the wallet/source/address/transaction tables and add valuations.cash_entry_total and valuations.superseded_at. Foreign keys and unique indexes protect wallet ownership; wallet-child foreign keys cascade for callback-free financial-data reset. Existing accounts are not automatically converted, and historical migration versions are unchanged.

Validation used isolated Docker PostgreSQL/Redis/Chromium containers and public fixtures, without production or personal wallet data:

  • Full Rails suite: 10,127 runs, 42,628 assertions, 0 failures, 0 errors, 33 existing conditional skips.
  • Full Chromium system suite: 166 runs, 846 assertions, 0 failures, 0 errors, 0 skips.
  • Focused integration/holding run including the year-long history and ownership-priority regressions: 82 runs, 281 assertions, 0 failures, 0 errors.
  • RuboCop, ERB lint (748 templates), Biome (133 files) and Brakeman (0 security warnings) passed.

Behavioral coverage includes FX/custom rates, split entries, basis/unknown/locks, cash and total valuations, backdated edits, full-provider composition, guarded holding mutations, chart history, sync-tree completion/failure/retry/cancellation, timezone boundaries, source changes and RBF/reorg recovery. Browser tests cover the Accounts-menu connection flow, automatic polling, conditional HD inputs, narrow-screen layout and durable connection/disconnection state, retained positions and manual controls. The year-long regression checks that idle sync and a backdated edit leave history outside the materialization window untouched.

GitHub Actions on final head f8a7ca29efc693e84a55dcb2aac9829794e450d9 passed: unit/integration suite, system suite, Ruby/JavaScript lint and security scans, and Pipelock. CodeRabbit completed review of this exact head with no actionable comments, Minimal merge risk, all five pre-merge checks passed and Docstring Coverage 82.66%. All eight review discussions are resolved.

Integration findings and completion checklist

Screenshots

Public test fixtures only.

Quantity preview

Connected wallet

Summary by CodeRabbit

  • New Features

    • Added preview support for connecting watch-only Bitcoin wallets to existing Crypto accounts using public addresses or Native SegWit extended public keys.
    • View wallet balances, confirmation status, discovery progress, sync freshness, and transaction history; manage sources, refresh, connect, or disconnect wallets.
    • Bitcoin wallet status is included in on-chain provider connection details.
  • Improvements

    • Mixed accounts better preserve manual activity and historical data while incorporating provider-reported positions and balances.
    • Added clearer feedback for managed positions, wallet discovery, and valuation issues.
    • Extended public keys and wallet addresses are excluded from provider status responses, and public-key values are filtered from logs.