Read watch-only Bitcoin wallets into a preview

VL
7d ago
+2320-7

Part 3 of #5620. Stacked on #5671. The public-key layer is already on main via #4615.

up/bitcoin-cash ← up/bitcoin-reading · Previous discussion #4712 · Fork diff.

Read a watch-only Bitcoin wallet into a preview. A wallet can have several public addresses or a BIP84 account public key. The reader discovers receive and change addresses locally and reports their combined balance in satoshis. An internal transfer counts only its fee.

Discovery saves its cursor between jobs. When a new block arrives during a slow read, the reader verifies the intervening blocks and rereads affected addresses and pending payments. A reorganization triggers a full discovery retry. The wallet keeps its last complete balance until a consistent read finishes.

Migrations

The migration creates the wallet, source, address and transaction tables. A BIP84 source requires Active Record encryption. Its xpub and zpub share a fingerprint so the same account key can be detected in either form. Keys stay out of explorer requests.

Earlier review

Removing a source or rejecting a mismatched key releases its address claims. A source's identity is read-only after creation, and a pending retry belongs to the same parent sync. Maintainer review · Key rotation · Retry ownership.

Validation

The full Rails suite passed on aa2eb4bde (18,605 tests, 9 skips). The Chromium suite passed (460 tests). RuboCop, ERB lint, Biome and Brakeman passed.


Summary by cubic

Reads watch-only Bitcoin wallets into a preview, discovering receive and change addresses locally and reporting their combined balance in satoshis. A transfer between the wallet's own addresses counts only its fee.

  • Discovery resumes between jobs and rereads affected addresses when new blocks arrive; a reorganization triggers a full retry, and the wallet keeps its last complete balance until a consistent read finishes.
  • Confirmed history pages by block height; pending activity is rechecked whenever a discovery branch starts, including unused addresses from earlier checkpoints.
  • A wallet can only track a manual crypto account, and that account cannot link another provider.
  • Provider eligibility and source removal are serialized; sources are immutable after creation, removing one releases its address claims, and manual address claims are preserved.
  • Wallet keys never leave the app; only derived addresses are sent to the explorer.

Migration

  • The migration creates the wallet, source, address, and transaction tables.
  • BIP84 sources require Active Record encryption; xpub and zpub share a fingerprint so the same account key is detected in either form.
  • Run security:backfill_encryption (version bumped to 14) to encrypt BitcoinWalletSource rows, including read-only attributes.

Summary by CodeRabbit

  • New Features
    • Added Bitcoin wallet tracking for eligible crypto accounts using manual addresses or extended public keys.
    • Wallet balances and transaction histories sync with pending transactions, discovered addresses, and blockchain updates.
  • Improvements
    • Syncs can recover from blockchain changes, incomplete data, and temporary provider issues.
    • Safeguards prevent linking accounts or addresses already connected elsewhere.
    • Extended public keys are encrypted, and wallet data is removed when its account is deleted.
    • Bitcoin wallet connection and configuration errors are translated into multiple languages.

Feedback carried over from earlier PRs

The original comments are linked below, including follow-ups addressed by a later layer.

  • Reviewer suggestion: Stop explorer history pagination at the baseline block height. #5621 review
  • Reviewer suggestion: Attach the connected wallet's provider link to sync failure diagnostics. #5622 review
  • Reviewer suggestion: Consider previously used addresses when reopening a completed discovery branch. #5622 review
  • Reviewer suggestion: Clarify whether previews should reserve addresses against the single-address flow. #5622 review