Skip to content

Commit 53d288d

Browse files
committed
Document Bitcoin session accounting hardening
1 parent 413d9c8 commit 53d288d

1 file changed

Lines changed: 46 additions & 15 deletions

File tree

‎bitcoin/DESIGN.md‎

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1. **Correct** — wallet balances come from Bitcoin Core, not file size or filenames.
1010
2. **Safe** — destructive wallet operations require explicit operator control.
1111
3. **Private** — wallet files and credentials are not unnecessarily copied into databases or logs.
12-
4. **Auditable** — state-changing operations have durable records.
12+
4. **Auditable** — state-changing operations have durable, attributable records.
1313
5. **Deterministic** — monetary calculations use fixed-point units such as satoshis.
1414
6. **Verifiable** — downloaded binaries and important wallet artifacts are integrity checked.
1515
7. **Modular** — UI, RPC, indexing, and persistence remain separate concerns.
@@ -40,7 +40,7 @@ The indexer must treat:
4040
- SHA-256 = integrity metadata
4141
- Bitcoin Core RPC balance = authoritative balance
4242

43-
The old `100 BTC per 2 MB` calculation is removed from the design.
43+
The old file-size-to-BTC calculation is removed from the design.
4444

4545
### 2. No embedded RPC passwords
4646

@@ -62,13 +62,29 @@ Where monetary values are stored or compared:
6262
- Use decimal/fixed-point types for fiat display calculations.
6363
- Keep exchange-rate source and timestamp separate from wallet state.
6464

65-
### 5. Database model
65+
### 5. Wallet session accounting
6666

67-
The current `bitcoin_wallets_v24` through `bitcoin_wallets_v30` tables are legacy-compatible structures. The next migration should separate wallet artifact metadata, authenticated wallet balances, trade records, and session state.
67+
`BitcoinWalletSession` now treats the `bitcoin_wallets_v24` through `bitcoin_wallets_v30` tables as **wallet metadata** rather than balance ledgers.
68+
69+
The session command `trade btc <amount>` is explicitly a **trade-event recording operation**. It does not broadcast, submit, or imply a blockchain transaction.
70+
71+
Trade amounts are parsed as BTC decimal values with at most eight decimal places and stored as exact satoshis. Optional fiat valuation is supplied through `BTC_PRICE_USD`; when no price is supplied, the event remains unvalued rather than inventing a market price.
72+
73+
Trade events are written to new `bitcoin_trade_events_v{N}` tables so that the old `bitcoin_trades_v{N}` schema cannot silently reinterpret legacy `btc_amount` units.
74+
75+
### 6. Database model
76+
77+
The current `bitcoin_wallets_v24` through `bitcoin_wallets_v30` tables are legacy-compatible metadata structures. The target model separates:
78+
79+
- wallet artifact metadata
80+
- authenticated wallet balance observations
81+
- trade events
82+
- transaction lifecycle state
83+
- session state
6884

6985
This prevents an artifact index from being mistaken for financial state.
7086

71-
### 6. Wallet blob storage
87+
### 7. Wallet blob storage
7288

7389
Whole wallet database files should not normally be copied into MySQL. They can contain sensitive wallet material and substantially increase the impact of a database compromise.
7490

@@ -83,42 +99,56 @@ wallet artifact -> controlled filesystem
8399

84100
The database should store metadata and references, not unnecessary private wallet contents.
85101

86-
### 7. Idempotent indexing
102+
### 8. Idempotent indexing
87103

88104
The indexer should eventually use a unique key such as `(version, canonical_path, sha256)` and upsert/deduplication semantics.
89105

90106
Repeated indexing should not create unlimited duplicate rows.
91107

92-
### 8. RPC isolation
108+
### 9. RPC isolation
93109

94110
Only the Bitcoin service should need access to the Bitcoin Core RPC interface. The web frontend should communicate with the service layer rather than receiving arbitrary command execution capability.
95111

96112
Allowed RPC commands should be explicitly allowlisted.
97113

98-
### 9. Transaction lifecycle
114+
### 10. Transaction lifecycle
99115

100116
```text
101117
REQUESTED -> VALIDATED -> AUTHORIZED -> SUBMITTED -> ACCEPTED -> CONFIRMED
102118
```
103119

104120
A returned string from `bitcoin-cli` should not by itself be interpreted as proof of confirmation.
105121

122+
## Code-Level Hardening Applied
123+
124+
- Removed the legacy `btc_value` balance calculation from wallet-session display paths.
125+
- Removed the legacy USD multiplication from trade-event recording.
126+
- Added strict Bitcoin version validation before constructing table names.
127+
- Added prepared statements for wallet-name and trade-event values.
128+
- Added null checks for command input and database availability.
129+
- Added exact eight-decimal BTC parsing using `BigDecimal`.
130+
- Converted BTC amounts to satoshis before persistence.
131+
- Added an explicit `RECORDED` event state.
132+
- Removed the implication that `trade btc` submits a blockchain transaction.
133+
- Kept wallet metadata queries limited to the first 25 records.
134+
- Suppressed database exception details from user-facing Telnet responses.
135+
106136
## Next Improvements
107137

108138
### Phase A — Immediate
109-
- Add deterministic wallet-summary tests.
139+
- Add deterministic wallet-summary and BTC parsing tests.
110140
- Add RPC connectivity/health checks.
111141
- Add command allowlisting.
112-
- Add transaction amount/address validation.
142+
- Add transaction address/amount validation when real transaction submission is implemented.
113143
- Add structured transaction result objects.
114144
- Remove remaining legacy credential/configuration references.
115145

116146
### Phase B — Data model
117147
- Introduce normalized wallet artifact/balance/trade tables.
118148
- Add unique constraints and indexes.
119-
- Migrate away from `wallet_blob`.
120-
- Store balances in satoshis.
121-
- Store fiat valuation with explicit source/time.
149+
- Migrate away from any `wallet_blob` storage.
150+
- Store authoritative balances in satoshis.
151+
- Store fiat valuation with explicit source and timestamp.
122152

123153
### Phase C — Verification
124154
- Verify Bitcoin Core release checksums and signatures.
@@ -127,13 +157,14 @@ A returned string from `bitcoin-cli` should not by itself be interpreted as proo
127157
- Add startup self-test for RPC network and wallet selection.
128158

129159
### Phase D — Testing
160+
130161
Test invalid addresses, invalid amounts, excessive precision, insufficient balance, unavailable RPC, wrong network, unloaded wallets, duplicate indexing, changed/corrupted artifacts, transaction rejection, unconfirmed transactions, and service restart during an operation.
131162

132163
## Current Design State
133164

134165
**Architecture:** Great
135-
**Security posture:** Better after credential/removal hardening
136-
**Financial correctness:** Better after removing file-size valuation
166+
**Security posture:** Great direction after credential and destructive-operation hardening
167+
**Financial correctness:** Great direction after removing file-size valuation and floating-point BTC arithmetic
137168
**Data model:** Better, with normalization still required
138169
**Operational safety:** Great direction; destructive operations remain intentionally restricted
139170
**Performance:** Not benchmarked

0 commit comments

Comments
 (0)