Merge pull request 'store: a wallet catalogue in the encrypted db (wallets table + CRUD)' (#141) from wt/t_64c9ba3c into main
All checks were successful
ci / test (push) Successful in 6m0s
ci / sdk (push) Successful in 1m35s
android-ci / android (push) Successful in 11m56s
ci / fuzz (push) Successful in 4m51s

Reviewed-on: #141
This commit is contained in:
sirius 2026-09-21 15:09:45 +00:00
commit 948d9b4b0c
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}");
}