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

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.
This commit is contained in:
rust-dev (hermes) 2026-09-21 10:21:48 -04:00
commit 467510f096
No known key found for this signature in database
3 changed files with 309 additions and 2 deletions

View file

@ -540,3 +540,126 @@ fn ratchet_state_survives_reopening_the_store() {
assert_eq!(store.load_ratchet(&pk(1)).expect("load"), Some(blob));
assert_eq!(store.list_messages(&pk(1)).unwrap()[0].plaintext, b"hello");
}
// ---- the wallet catalogue (`wallets`) --------------------------------------
//
// Several wallet *files* live under one identity: the row here is what makes a
// wallet survive a restart, because the wallet child's own config only ever
// describes the one it is pointed at right now.
fn new_wallet(filename: &str, name: Option<&str>) -> onionwire::NewWallet {
onionwire::NewWallet {
name: name.map(str::to_string),
identity_pubkey: pk(7),
onion_locator: "walletloc.onion".to_string(),
wallet_filename: filename.to_string(),
}
}
#[test]
fn create_wallet_stores_every_field_and_reads_it_back() {
let dir = tempfile::tempdir().expect("tempdir");
let store = Store::open_at_with_passphrase(dir.path(), "wallet-pass").expect("open");
let row = store
.create_wallet(&new_wallet("savings", Some("Savings")))
.expect("create");
assert!(row.id > 0, "id assigned: {row:?}");
assert!(row.created_at > 0, "created_at stamped: {row:?}");
assert_eq!(row.wallet_filename, "savings");
assert_eq!(row.name.as_deref(), Some("Savings"));
assert_eq!(row.identity_pubkey, pk(7));
assert_eq!(row.onion_locator, "walletloc.onion");
let got = store
.get_wallet_by_id(row.id)
.expect("get")
.expect("row exists");
assert_eq!(got, row, "the stored row is what came back");
}
#[test]
fn multiple_wallets_coexist_and_survive_a_reopen() {
let dir = tempfile::tempdir().expect("tempdir");
let (spending, savings) = {
let store = Store::open_at_with_passphrase(dir.path(), "wallet-pass").expect("open");
let spending = store
.create_wallet(&new_wallet("spending", Some("Spending")))
.expect("spending");
let savings = store
.create_wallet(&new_wallet("savings", None))
.expect("savings");
assert_ne!(spending.id, savings.id, "two wallets, two rows");
(spending.id, savings.id)
};
let store = Store::open_at_with_passphrase(dir.path(), "wallet-pass").expect("reopen");
let rows = store.list_wallets().expect("list");
assert_eq!(rows.len(), 2, "both wallets remembered: {rows:?}");
assert_eq!(rows[0].id, spending, "creation order");
assert_eq!(rows[1].id, savings);
assert_eq!(
rows[1].name, None,
"an unlabelled wallet round-trips as None"
);
assert_eq!(rows[1].wallet_filename, "savings");
}
#[test]
fn a_second_wallet_cannot_reuse_a_wallet_filename() {
let dir = tempfile::tempdir().expect("tempdir");
let store = Store::open_at_with_passphrase(dir.path(), "wallet-pass").expect("open");
let first = store
.create_wallet(&new_wallet("onionwire", None))
.expect("first");
let err = store
.create_wallet(&new_wallet("onionwire", Some("Clash")))
.expect_err("a duplicate filename must not overwrite the first wallet");
assert!(
err.to_string().contains("onionwire"),
"names the file: {err}"
);
let rows = store.list_wallets().expect("list");
assert_eq!(rows.len(), 1, "the rejected insert left one row: {rows:?}");
assert_eq!(rows[0], first, "the first wallet is untouched");
}
#[test]
fn delete_wallet_removes_only_that_row() {
let dir = tempfile::tempdir().expect("tempdir");
let store = Store::open_at_with_passphrase(dir.path(), "wallet-pass").expect("open");
let a = store.create_wallet(&new_wallet("a", None)).expect("a");
let b = store.create_wallet(&new_wallet("b", None)).expect("b");
assert!(store.delete_wallet(a.id).expect("delete"), "row was there");
assert_eq!(store.get_wallet_by_id(a.id).expect("get"), None);
let rows = store.list_wallets().expect("list");
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].id, b.id);
assert!(
!store
.delete_wallet(9999)
.expect("unknown id is not an error"),
"nothing to delete reports false"
);
}
#[test]
fn a_wallet_without_a_label_or_locator_stores_nulls_and_rejects_a_blank_filename() {
let dir = tempfile::tempdir().expect("tempdir");
let store = Store::open_at_with_passphrase(dir.path(), "wallet-pass").expect("open");
let mut blank = new_wallet("blank", Some(" "));
blank.onion_locator = String::new();
let row = store.create_wallet(&blank).expect("blank label");
assert_eq!(row.name, None, "a whitespace label is no label");
assert_eq!(row.onion_locator, "", "a locator may be empty pre-publish");
let err = store
.create_wallet(&new_wallet(" ", None))
.expect_err("an empty filename cannot name a wallet file");
assert!(err.to_string().contains("filename"), "{err}");
}