wallet: app-owned child lifecycle + user-chosen ChainSource

The app now owns monero-wallet-rpc (and monerod when the chain source is
local) instead of requiring the user to start them and export
ONIONWIRE_WALLET_RPC.

- ChainSource enum: None / Local { monerod_bin, datadir } / Remote { host,
  port }. No default remote node; Remote is only constructed from explicit
  user input. A .onion Remote is refused without a SOCKS proxy (separate
  card).
- WalletChild in src/wallet_child.rs spawns monero-wallet-rpc with
  --rpc-bind-ip 127.0.0.1, a random free loopback port, generated
  --rpc-login, and --wallet-dir /wallet; monerod too for a
  local source. --trusted-daemon is never passed.
- Generated RPC creds are sealed in the encrypted SQLCipher store
  (wallet_child table, AEAD on top of whole-db encryption), never a
  plaintext file. The Wallet client is built from the generated loopback
  URL, not from_env.
- Readiness predicate: chain_ready() requires synchronized==true, height>0,
  height>=target_height; can_spend() and can_verify_receipts() gate on it.
  Unsynced -> receipts stay verified=0, spend refused with a clear message.
- Backend-aware timeouts: local management calls keep 5s; chain-dependent
  calls (get_info, get_transfers, transfer) get 60s (CHAIN_RPC_TIMEOUT),
  because a first sync or a remote daemon outlasts 5s and a 5s cap there
  is what left receipts permanently unverified.
- node.rs ingest_receipt gates on can_verify_receipts() instead of
  configured() alone.
- docs/WALLET.md rewritten for the child lifecycle + chain source.

Base: feat/wallet-create-import (PR #66). Do not rebase onto main.

Co-Authored-By: Hermes Agent <agent@local>
This commit is contained in:
agent 2026-09-14 18:55:07 -04:00
commit bcf7ec31c1
No known key found for this signature in database
7 changed files with 1341 additions and 60 deletions

View file

@ -257,6 +257,14 @@ impl Store {
kdf_salt BLOB NOT NULL,
wrapped_key BLOB NOT NULL
);
-- The wallet child's RPC creds + chain-source snapshot. One row,
-- sealed with the store data key (see `save_wallet_child`): the
-- creds never touch the filesystem in the clear.
CREATE TABLE IF NOT EXISTS wallet_child (
id INTEGER PRIMARY KEY CHECK (id = 1),
sealed BLOB NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS payments (
id INTEGER PRIMARY KEY,
friend_id INTEGER NOT NULL REFERENCES friends(id),
@ -310,6 +318,43 @@ impl Store {
.map_err(Into::into)
}
/// Seal the wallet child's config (RPC creds + chain-source snapshot) into
/// the encrypted store, replacing the single row.
///
/// The creds are generated per install and grant spend authority over the
/// wallet child, so they are stored the same way message bodies are: AEAD
/// with the store data key on top of the whole-db SQLCipher layer. They are
/// never written to a plaintext file beside the wallet.
pub fn save_wallet_child(&self, json: &str) -> Result<()> {
let blob = crate::backup::aead_encrypt(&self.msg_key, json.as_bytes(), WALLET_CHILD_AAD)
.map_err(Error)?;
self.conn.execute(
"INSERT INTO wallet_child (id, sealed, updated_at) VALUES (1, ?1, ?2)
ON CONFLICT(id) DO UPDATE SET sealed = excluded.sealed, updated_at = excluded.updated_at",
params![blob, unix_now()],
)?;
Ok(())
}
/// Read back the sealed wallet-child config, or `None` before the first
/// child start. A blob that does not decrypt is an error, never a guess.
pub fn load_wallet_child(&self) -> Result<Option<String>> {
let row: Option<Vec<u8>> = self
.conn
.query_row("SELECT sealed FROM wallet_child WHERE id = 1", [], |row| {
row.get(0)
})
.optional()?;
let Some(blob) = row else {
return Ok(None);
};
let plain = crate::backup::aead_decrypt(&self.msg_key, &blob, WALLET_CHILD_AAD)
.map_err(|_| Error("wallet-child config decrypt failed".into()))?;
String::from_utf8(plain)
.map(Some)
.map_err(|_| Error("wallet-child config is not utf-8".into()))
}
pub fn replace_identity_keys(
&self,
identity_sk: &[u8],
@ -1548,6 +1593,10 @@ fn unix_now() -> i64 {
.unwrap_or(0)
}
/// AAD for the sealed wallet-child row. Domain-separates it from message and
/// outbox blobs that share the same store data key.
const WALLET_CHILD_AAD: &[u8] = b"onionwire/wallet-child/v1";
fn fingerprint(pubkey: &[u8]) -> String {
pubkey.iter().map(|b| format!("{b:02x}")).collect()
}
@ -2139,4 +2188,66 @@ mod at_rest {
Some(&b"fresh-state"[..])
);
}
/// The wallet-child RPC creds grant spend authority, so they are stored
/// sealed in the encrypted store — never as a plaintext file next to the
/// wallet, and never as a plaintext row.
#[test]
fn wallet_child_creds_round_trip_sealed_and_load_none_before_first_start() {
let dir = tempfile::tempdir().unwrap();
let store = Store::open_at_with_passphrase(dir.path(), "child-pass").unwrap();
assert!(
store.load_wallet_child().unwrap().is_none(),
"a fresh store has no wallet-child config"
);
let cfg = r#"{"user":"owuser","pass":"ow-secret-pass-xyz","wallet_filename":"onionwire"}"#;
store.save_wallet_child(cfg).unwrap();
assert_eq!(store.load_wallet_child().unwrap().as_deref(), Some(cfg));
let sealed: Vec<u8> = store
.conn
.query_row("SELECT sealed FROM wallet_child WHERE id = 1", [], |r| {
r.get(0)
})
.unwrap();
assert_ne!(
sealed.as_slice(),
cfg.as_bytes(),
"row stored the creds as plaintext"
);
assert!(
!sealed
.windows(11)
.any(|w| w == b"ow-secret-p".as_slice()),
"sealed row leaked the password"
);
// Re-sealing replaces the single row instead of stacking rows.
let cfg2 = r#"{"user":"owuser","pass":"second-pass-abc","wallet_filename":"onionwire"}"#;
store.save_wallet_child(cfg2).unwrap();
assert_eq!(store.load_wallet_child().unwrap().as_deref(), Some(cfg2));
let rows: i64 = store
.conn
.query_row("SELECT count(*) FROM wallet_child", [], |r| r.get(0))
.unwrap();
assert_eq!(rows, 1, "wallet_child must hold exactly one row");
store.checkpoint_wal().unwrap();
drop(store);
for name in ["onionwire.db", "onionwire.db-wal", "onionwire.db-shm"] {
let path = dir.path().join(name);
let Ok(bytes) = std::fs::read(&path) else {
continue;
};
for needle in [
b"second-pass-abc".as_slice(),
b"ow-secret-pass-xyz".as_slice(),
] {
assert!(
!bytes.windows(needle.len()).any(|w| w == needle),
"{name} contains the wallet-child password in the clear"
);
}
}
}
}