|
| 1 | +# Bitcoin Design |
| 2 | + |
| 3 | +**Repository:** Java.Web.Server.Telnet.Front.Java.21 |
| 4 | +**Area:** Bitcoin / BitcoinCompliant / BitcoinWalletIndexer / TraderModule |
| 5 | +**Review Date:** 2026-09-16 |
| 6 | + |
| 7 | +## Design Goals |
| 8 | + |
| 9 | +1. **Correct** — wallet balances come from Bitcoin Core, not file size or filenames. |
| 10 | +2. **Safe** — destructive wallet operations require explicit operator control. |
| 11 | +3. **Private** — wallet files and credentials are not unnecessarily copied into databases or logs. |
| 12 | +4. **Auditable** — state-changing operations have durable records. |
| 13 | +5. **Deterministic** — monetary calculations use fixed-point units such as satoshis. |
| 14 | +6. **Verifiable** — downloaded binaries and important wallet artifacts are integrity checked. |
| 15 | +7. **Modular** — UI, RPC, indexing, and persistence remain separate concerns. |
| 16 | + |
| 17 | +## Current Architecture |
| 18 | + |
| 19 | +```text |
| 20 | +Web UI (/bitcoin) |
| 21 | + | |
| 22 | + v |
| 23 | +BitcoinCompliant :6682 |
| 24 | + | |
| 25 | + +--> BitcoinBase ----> bitcoin-cli / bitcoind |
| 26 | + +--> TraderModule |
| 27 | + +--> BitcoinWalletSession |
| 28 | + +--> BitcoinWalletIndexer ----> MySQL metadata |
| 29 | +``` |
| 30 | + |
| 31 | +## Key Corrections |
| 32 | + |
| 33 | +### 1. Wallet size is not wallet balance |
| 34 | + |
| 35 | +A wallet database file contains implementation/state data. Its byte size does not represent BTC held by the wallet. |
| 36 | + |
| 37 | +The indexer must treat: |
| 38 | +- filename = metadata |
| 39 | +- file size = metadata |
| 40 | +- SHA-256 = integrity metadata |
| 41 | +- Bitcoin Core RPC balance = authoritative balance |
| 42 | + |
| 43 | +The old `100 BTC per 2 MB` calculation is removed from the design. |
| 44 | + |
| 45 | +### 2. No embedded RPC passwords |
| 46 | + |
| 47 | +Bitcoin RPC credentials must not be committed to Java or shell source. The local regtest design now relies on Bitcoin Core's normal cookie authentication path. |
| 48 | + |
| 49 | +The RPC port remains configurable through `BITCOIN_RPC_PORT`. |
| 50 | + |
| 51 | +### 3. No shell deletion of wallets |
| 52 | + |
| 53 | +The previous trader implementation contained a filesystem `rm -r` path for wallet deletion. This has been disabled. |
| 54 | + |
| 55 | +Wallet destruction is a high-impact operation and should be performed only through an explicit, separately reviewed Bitcoin Core lifecycle procedure with verified backup and operator confirmation. |
| 56 | + |
| 57 | +### 4. Monetary representation |
| 58 | + |
| 59 | +Where monetary values are stored or compared: |
| 60 | +- Prefer satoshis (`long`) for exact BTC quantities. |
| 61 | +- Do not use binary floating point for authoritative balances. |
| 62 | +- Use decimal/fixed-point types for fiat display calculations. |
| 63 | +- Keep exchange-rate source and timestamp separate from wallet state. |
| 64 | + |
| 65 | +### 5. Database model |
| 66 | + |
| 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. |
| 68 | + |
| 69 | +This prevents an artifact index from being mistaken for financial state. |
| 70 | + |
| 71 | +### 6. Wallet blob storage |
| 72 | + |
| 73 | +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. |
| 74 | + |
| 75 | +Preferred model: |
| 76 | +```text |
| 77 | +wallet artifact -> controlled filesystem |
| 78 | + +--> SHA-256 |
| 79 | + +--> size |
| 80 | + +--> modified time |
| 81 | + +--> permissions |
| 82 | +``` |
| 83 | + |
| 84 | +The database should store metadata and references, not unnecessary private wallet contents. |
| 85 | + |
| 86 | +### 7. Idempotent indexing |
| 87 | + |
| 88 | +The indexer should eventually use a unique key such as `(version, canonical_path, sha256)` and upsert/deduplication semantics. |
| 89 | + |
| 90 | +Repeated indexing should not create unlimited duplicate rows. |
| 91 | + |
| 92 | +### 8. RPC isolation |
| 93 | + |
| 94 | +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. |
| 95 | + |
| 96 | +Allowed RPC commands should be explicitly allowlisted. |
| 97 | + |
| 98 | +### 9. Transaction lifecycle |
| 99 | + |
| 100 | +```text |
| 101 | +REQUESTED -> VALIDATED -> AUTHORIZED -> SUBMITTED -> ACCEPTED -> CONFIRMED |
| 102 | +``` |
| 103 | + |
| 104 | +A returned string from `bitcoin-cli` should not by itself be interpreted as proof of confirmation. |
| 105 | + |
| 106 | +## Next Improvements |
| 107 | + |
| 108 | +### Phase A — Immediate |
| 109 | +- Add deterministic wallet-summary tests. |
| 110 | +- Add RPC connectivity/health checks. |
| 111 | +- Add command allowlisting. |
| 112 | +- Add transaction amount/address validation. |
| 113 | +- Add structured transaction result objects. |
| 114 | +- Remove remaining legacy credential/configuration references. |
| 115 | + |
| 116 | +### Phase B — Data model |
| 117 | +- Introduce normalized wallet artifact/balance/trade tables. |
| 118 | +- Add unique constraints and indexes. |
| 119 | +- Migrate away from `wallet_blob`. |
| 120 | +- Store balances in satoshis. |
| 121 | +- Store fiat valuation with explicit source/time. |
| 122 | + |
| 123 | +### Phase C — Verification |
| 124 | +- Verify Bitcoin Core release checksums and signatures. |
| 125 | +- Record binary version and verification date. |
| 126 | +- Verify configuration before starting the node. |
| 127 | +- Add startup self-test for RPC network and wallet selection. |
| 128 | + |
| 129 | +### Phase D — Testing |
| 130 | +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. |
| 131 | + |
| 132 | +## Current Design State |
| 133 | + |
| 134 | +**Architecture:** Great |
| 135 | +**Security posture:** Better after credential/removal hardening |
| 136 | +**Financial correctness:** Better after removing file-size valuation |
| 137 | +**Data model:** Better, with normalization still required |
| 138 | +**Operational safety:** Great direction; destructive operations remain intentionally restricted |
| 139 | +**Performance:** Not benchmarked |
| 140 | +**Production readiness:** Requires the Phase A–D verification work |
0 commit comments