Skip to content

Commit 5f8a27a

Browse files
docs: improve bitcoin package doc (#1869)
1 parent 77b59b5 commit 5f8a27a

File tree

3 files changed

+75
-38
lines changed

3 files changed

+75
-38
lines changed

packages/bitcoin/README.md

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,79 @@
1-
# Light Wallet | Packages | Bitcoin
1+
# **Bitcoin Observable Wallet**
22

3-
A Bitcoin blockchain package
3+
A lightweight TypeScript SDK that turns a set of HD keys into a fully-functional, observable Bitcoin wallet.
44

5-
## Modules
5+
It exposes RxJS streams (balance$, utxos$, transactionHistory$) so front-ends can react to on-chain changes in real time, it provides several extension points to fetch blocks, UTXOs and fees, coin-selection, PSBT construction, and Secp256k1 signing. Currently the package comes with default implementation for these interfaces using Maestro Bitcoin API (https://www.gomaestro.org/) for data fetching and @bitcoinerlab/secp256k1, coinselect and bitcoinjs-lib for transaction serialization and signing.
6+
7+
---
8+
9+
## **Modules**
10+
11+
The package is organized in 5 modules:
12+
13+
14+
<p align="center">
15+
<img align="middle" src="./assets/modules_chart.png"/>
16+
</p>
17+
18+
### **Modules Description**
19+
20+
| Module | Purpose |
21+
|--------------------------------------------|-----------------------------------------------------------------------------------------------------------|
22+
| **common/** | Pure, stateless helpers that can be reused by every other module. No network or side-effects. |
23+
| `common/address.ts` | Convert compressed pubkeys to P2PKH, P2SH-P2WPKH, P2WPKH and P2TR addresses; validate addresses. |
24+
| `common/constants.ts` | Shared byte-sizes (input/output/overhead), dust threshold, etc. |
25+
| `common/info.ts` | Type that groups the five account-level XPUBs produced for each address type. |
26+
| `common/keyDerivation.ts` | BIP-39/BIP-32/Electrum seed derivation, account & child key derivation, Taproot tweaks. |
27+
| `common/network.ts` | Enum for Mainnet / Testnet and related helpers. |
28+
| `common/taproot.ts` | Low-level functions to compute BIP-341 tagged hashes and tweak x-only pub/priv keys. |
29+
| `common/transaction.ts` | UnsignedTransaction / SignedTransaction type definitions. |
30+
| **input-selection/** | Pluggable coin-selection strategies. |
31+
| `input-selection/InputSelector.ts` | Interface describing the contract for any selector implementation. |
32+
| `input-selection/GreedyInputSelector.ts` | Deterministic greedy algorithm that selects largest UTXOs until target + fee is met. |
33+
| **providers/** | Abstract access to the Bitcoin network and on-chain data. |
34+
| `providers/BlockchainDataProvider.ts` | Interface for reading chain state, mempool, fee rates, and broadcasting TXs. |
35+
| `providers/MaestroBitcoinDataProvider.ts` | Concrete HTTP client against Maestro’s REST API. |
36+
| `providers/StubBitcoinDataProvider.ts` | In-memory mock that returns deterministic data for unit tests. |
37+
| `providers/BlockchainInputResolver.ts` | Helper that maps a (txid, vout) pair to a full UTxO by calling the provider. |
38+
| **tx-builder/** | Everything needed to construct a PSBT ready for signing. |
39+
| `tx-builder/TransactionBuilder.ts` | Accepts outputs, change address, selector, builds PSBT, estimates vBytes & fee. |
40+
| `tx-builder/utils.ts` | Constants & helpers specific to size/fee calculation inside the builder. |
41+
| **wallet/** | Stateful, observable wallet core and signer. |
42+
| `wallet/BitcoinWallet.ts` | Polls provider, maintains `balance$`, `utxos$`, history & pending TXs via RxJS. |
43+
| `wallet/BitcoinSigner.ts` | Thin wrapper around `@bitcoinerlab/secp256k1` for PSBT input signing + key zeroisation. |
44+
| `index.ts` | Package entry point. |
45+
46+
## **High Level Overview**
47+
48+
### Common
49+
50+
Handles all deterministic key derivation (BIP-32, Electrum), address transformation, and Taproot tweaking. It is strictly pure code with no network or disk I/O. It contains some common types shared across all other modules.
51+
52+
53+
### Provider
54+
55+
A thin abstraction (BlockchainDataProvider) encodes everything the wallet needs from a chain service: block tip, UTXOs, mempool, fee estimates, and broadcast. The provider isolates all network I/O and normalises raw REST/RPC responses into the package’s own types (BlockInfo, UTxO, TransactionHistoryEntry, …).
56+
This module also provides an interface and a concrete implementation of BlockchainInputResolver used only by the transaction-analysis utilities (e.g. turning a raw hex TX into a TransactionHistoryEntry).
57+
58+
### Wallet State
59+
60+
BitcoinWallet orchestrates polling and state aggregation. It owns a set of RxJS Subjects (utxos$, balance$, transactionHistory$, pendingTransactions$) that higher layers subscribe to.
61+
62+
A poll controller observable allows host applications (e.g. browser tab visibility) to dynamically pause/resume network activity without tearing down the wallet instance.
63+
64+
Each Subject’s latest value is replayed to new subscribers, so consumers see a consistent view even if they attach late.
65+
66+
This wallet function is purely to track wallet state; it does not provide transaction building or signing capabilities. This separation of concern simplifies integration, because often we need to perform these operations (state tracking and tx building signing) in different contexts (I.E background and context script on browser extensions).
67+
68+
### Transaction Building
69+
70+
All transaction construction and signing is implemented as pure, deterministic functions, and lives entirely outside the BitcoinWallet class.
71+
This separation means you can run “state‐tracking” in one JS context (e.g. a Web Worker or background script) and “build & sign” in another (e.g. a content script, mobile UI, or headless Node process).
72+
73+
This module provides:
74+
75+
- **Input** selection Extension point for input selection, comes with a GreedyInputSelector that implements a simple, deterministic algorithm.
76+
- **Builder** TransactionBuilder composes PSBTs, computes vBytes, and enforces dust rules.
77+
- **Signer** BitcoinSigner wraps @bitcoinerlab/secp256k1, signs each PSBT input, and scrubs the private key from memory.
678

7-
- [wallet]
879

9-
[wallet]: ./src/wallet
35.3 KB
Loading

packages/bitcoin/src/wallet/README.md

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)