store: a wallet catalogue in the encrypted db (wallets table + CRUD) #141

Merged
sirius merged 1 commit from wt/t_64c9ba3c into main 2026-09-21 15:09:46 +00:00
Owner

What

A created wallet was not remembered: a wallet existed only as a file under $ONIONWIRE_HOME/wallet plus the single-row wallet_child config describing the one the child is currently pointed at. There was no list, so a restart had nothing to read back, and a second create had no distinct file name to use.

This lands the schema half (card t_64c9ba3c) of the decomposition of #140:

  • new table wallets (id, name, identity_pubkey, onion_locator, wallet_filename, created_at), created by the additive CREATE TABLE IF NOT EXISTS batch on every open, so an existing install gains it in place
  • Store::create_wallet(&NewWallet) -> WalletRecord (returns the stored row, id + created_at assigned), get_wallet_by_id, list_wallets (oldest first, id breaks a created_at tie), delete_wallet (bool)
  • NewWallet / WalletRecord re-exported from the crate root

Schema decisions

  • wallet_filename is UNIQUE. Creating a second wallet is not a rewrite of the first: it gets its own file name, otherwise both wallets open the same file and the second create reads as a failure. A duplicate returns a named error and leaves the first row untouched.
  • identity_pubkey is NOT unique: identity is the pubkey and it is the roster key, while a wallet is one file. Several wallets may belong to one identity.
  • onion_locator is a locator, not an identity: it rotates (F4) and is allowed empty before the HS publishes. Stale locators are expected, never treated as an error.
  • name is optional; a blank or whitespace-only label is stored as SQL NULL, never as an empty string.
  • delete_wallet drops the catalogue row only. The wallet file on disk and recorded payments stay put.

Crate choice

No new dependency. The store is already rusqlite with the bundled-sqlcipher-vendored-openssl feature and a raw PRAGMA key as the first statement after open; sqlx has no SQLCipher path, so adding it would mean a second connection path into the db that is not encrypted. rusqlite stays.

Tests

TDD, RED first (the new tests did not compile: E0422 no struct NewWallet, E0599 no method create_wallet/list_wallets/get_wallet_by_id).

  • tests/store.rs: fields round-trip; two wallets coexist and survive a reopen; a duplicate filename is rejected and the first row is intact; delete removes only that row and an unknown id reports false; blank label plus a blank-filename rejection.
  • src/store.rs mod at_rest: a store with the wallets table dropped re-gains it on reopen (the upgrade path) and the API works on the recreated table.

Verification on this branch:

cargo test --locked        # all targets ok, 0 failed
cargo clippy --locked --all-targets -- -D warnings   # clean
rustfmt --edition 2024 --check tests/store.rs src/store.rs  # 0 diffs from my hunks

Scope note: this PR is schema + CRUD only. The creation path (t_511023b6), the wallet-selection screen (t_14ac0a3f) and the wiring (t_895853ad) are separate cards; the store API above is what they build on.

No braces in this body on purpose (Forgejo consumes template syntax in PR bodies).

## What A created wallet was not remembered: a wallet existed only as a file under `$ONIONWIRE_HOME/wallet` plus the single-row `wallet_child` config describing the one the child is currently pointed at. There was no list, so a restart had nothing to read back, and a second create had no distinct file name to use. This lands the schema half (card t_64c9ba3c) of the decomposition of #140: - new table `wallets` (id, name, identity_pubkey, onion_locator, wallet_filename, created_at), created by the additive `CREATE TABLE IF NOT EXISTS` batch on every open, so an existing install gains it in place - `Store::create_wallet(&NewWallet) -> WalletRecord` (returns the stored row, id + created_at assigned), `get_wallet_by_id`, `list_wallets` (oldest first, id breaks a created_at tie), `delete_wallet` (bool) - `NewWallet` / `WalletRecord` re-exported from the crate root ## Schema decisions - `wallet_filename` is UNIQUE. Creating a second wallet is not a rewrite of the first: it gets its own file name, otherwise both wallets open the same file and the second create reads as a failure. A duplicate returns a named error and leaves the first row untouched. - `identity_pubkey` is NOT unique: identity is the pubkey and it is the roster key, while a wallet is one file. Several wallets may belong to one identity. - `onion_locator` is a locator, not an identity: it rotates (F4) and is allowed empty before the HS publishes. Stale locators are expected, never treated as an error. - `name` is optional; a blank or whitespace-only label is stored as SQL NULL, never as an empty string. - `delete_wallet` drops the catalogue row only. The wallet file on disk and recorded payments stay put. ## Crate choice No new dependency. The store is already `rusqlite` with the `bundled-sqlcipher-vendored-openssl` feature and a raw `PRAGMA key` as the first statement after open; `sqlx` has no SQLCipher path, so adding it would mean a second connection path into the db that is not encrypted. `rusqlite` stays. ## Tests TDD, RED first (the new tests did not compile: `E0422 no struct NewWallet`, `E0599 no method create_wallet/list_wallets/get_wallet_by_id`). - `tests/store.rs`: fields round-trip; two wallets coexist and survive a reopen; a duplicate filename is rejected and the first row is intact; delete removes only that row and an unknown id reports false; blank label plus a blank-filename rejection. - `src/store.rs` `mod at_rest`: a store with the `wallets` table dropped re-gains it on reopen (the upgrade path) and the API works on the recreated table. Verification on this branch: ``` cargo test --locked # all targets ok, 0 failed cargo clippy --locked --all-targets -- -D warnings # clean rustfmt --edition 2024 --check tests/store.rs src/store.rs # 0 diffs from my hunks ``` Scope note: this PR is schema + CRUD only. The creation path (`t_511023b6`), the wallet-selection screen (`t_14ac0a3f`) and the wiring (`t_895853ad`) are separate cards; the store API above is what they build on. No braces in this body on purpose (Forgejo consumes template syntax in PR bodies).
store: a wallet catalogue in the encrypted db
All checks were successful
ci / test (pull_request) Successful in 5m48s
ci / sdk (pull_request) Successful in 1m34s
android-ci / android (pull_request) Successful in 12m4s
ci / fuzz (pull_request) Successful in 18m24s
467510f096
Wallets were only ever a file in $ONIONWIRE_HOME/wallet plus the
single-row wallet_child config for the one the child is currently
pointed at, so a created wallet was not remembered and a second create
had nowhere to go.

Add a 'wallets' table (id, name, identity_pubkey, onion_locator,
wallet_filename, created_at) and CRUD on Store: create_wallet,
get_wallet_by_id, list_wallets, delete_wallet. wallet_filename is
UNIQUE so a second wallet cannot silently reuse the first one's file,
and identity_pubkey is deliberately not unique (identity is the roster
key, a wallet is a file; the locator may rotate or be empty).

No new crate: the store is already rusqlite + SQLCipher with a raw
PRAGMA key, and sqlx has no SQLCipher path, so a second pool would be a
second unencrypted way in.

Tests: 5 store cases in tests/store.rs (round-trip, coexistence across
a reopen, duplicate filename, delete, blank label/filename) plus the
at_rest upgrade case in src/store.rs.
sirius merged commit 948d9b4b0c into main 2026-09-21 15:09:46 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
sirius/onionwire!141
No description provided.