wallet: stored config (F7), WalletStatus snapshot, pay/tip readiness guard
The only input path for the wallet was env. Add the same three remote-mode
fields as stored config so the F7 screen can set them and headless runs read
one source of truth:
- `wallet_config` table in the encrypted store (one row; the login is sealed
with the store data key under `onionwire/wallet-config/v1`, on top of
SQLCipher). Empty URL clears it. `Store::load_wallet_config` /
`save_wallet_config`.
- `WalletConfig { rpc_url, rpc_login: Zeroizing<String>, no_spend }` with a
redacting `Debug`; `Wallet::from_config`, and `Wallet::for_boot` which
prefers stored config, falls back to env, and refuses to dial a bad stored
endpoint. Node::start* now reads the store before constructing the wallet.
- `WalletStatus { configured, connected, last_error, balance_line,
receive_address }`: `connected` only ever comes from a probe
(`Wallet::status`, get_address + get_balance); config alone leaves it false.
`Node::wallet_status` is the cheap draw-path read, `wallet_refresh` the
probe that folds into it.
- One-shot `Wallet::ping(rpc_url, rpc_login) -> PingResult { ok, status,
error }` for the Test-connection button: same loopback/digest rules, errors
redacted and capped.
- `readiness(configured, connected, no_spend, is_tip) -> Readiness` plus
`Node::wallet_readiness` (async, block_on-friendly): not configured →
NOT_CONFIGURED_MSG; /tip on a no-spend wallet → VIEW_ONLY_MSG before any
dial; configured but not connected → NOT_CONNECTED_MSG; else ready.
- `Node::wallet_save_config` persists, validates first, then re-reads so the
live wallet and snapshot follow a Save without a restart.
- `Node::wallet` is now a Mutex<Wallet> (cloned out before each await) so the
wallet can be re-pointed mid-session.
Local-mode fields stay reserved: the child lifecycle is t_69ce8e46 and is not
wired here. docs/WALLET.md documents the stored config, boot precedence and
the exact guard copy.
Tests: 38 in tests/wallet.rs including exact-refusal-copy, boot precedence,
sealed-on-disk login, ping sanitization and connected-only-after-probe.
This commit is contained in:
parent
7cd316b65f
commit
d7e91f850a
5 changed files with 961 additions and 101 deletions
181
src/node.rs
181
src/node.rs
|
|
@ -24,7 +24,7 @@ use crate::ratelimit::TokenBucket;
|
|||
use crate::sanitize;
|
||||
use crate::session::{self, Keys};
|
||||
use crate::store::{Friend, FriendProfile, Message, PaymentWrite, Store};
|
||||
use crate::wallet::{self, Wallet};
|
||||
use crate::wallet::{self, Readiness, Wallet, WalletConfig, WalletStatus};
|
||||
|
||||
pub struct RotateResult {
|
||||
pub notified: usize,
|
||||
|
|
@ -77,7 +77,12 @@ pub struct Node {
|
|||
hs: Mutex<Option<HsHandle>>,
|
||||
onion: Mutex<String>,
|
||||
keys: Mutex<Keys>,
|
||||
wallet: Wallet,
|
||||
/// Behind a lock because the F7 screen can re-point it at a saved config
|
||||
/// without a restart. Clone it out (`wallet_snapshot`) before awaiting: a
|
||||
/// `std::sync::Mutex` guard must not live across an `.await`.
|
||||
wallet: Mutex<Wallet>,
|
||||
/// Last probe result, for the draw path. Cheap to read, never dials.
|
||||
wallet_status: Mutex<WalletStatus>,
|
||||
incoming_limit: Mutex<TokenBucket>,
|
||||
inbox: Mutex<file::Inbox>,
|
||||
outbox: Arc<Outbox>,
|
||||
|
|
@ -111,6 +116,10 @@ impl Node {
|
|||
passphrase: &str,
|
||||
) -> Result<Arc<Self>, String> {
|
||||
let store = Store::open_at_with_passphrase(&home, passphrase).map_err(|e| e.to_string())?;
|
||||
// Stored config first (the F7 screen's), env as the migration path.
|
||||
let stored_wallet = store.load_wallet_config().map_err(|e| e.to_string())?;
|
||||
let wallet = Wallet::for_boot(stored_wallet.as_ref());
|
||||
let wallet_status = WalletStatus::idle(wallet.configured());
|
||||
let me = store.self_identity().map_err(|e| e.to_string())?;
|
||||
let keys = Keys::from_self(&me).map_err(|e| e.to_string())?;
|
||||
let nickname = store.hs_nickname().map_err(|e| e.to_string())?;
|
||||
|
|
@ -139,7 +148,8 @@ impl Node {
|
|||
hs: Mutex::new(None),
|
||||
onion: Mutex::new(onion.clone()),
|
||||
keys: Mutex::new(keys),
|
||||
wallet: Wallet::from_env(),
|
||||
wallet: Mutex::new(wallet),
|
||||
wallet_status: Mutex::new(wallet_status),
|
||||
incoming_limit: Mutex::new(TokenBucket::default()),
|
||||
inbox: Mutex::new(inbox),
|
||||
outbox: Arc::clone(&outbox),
|
||||
|
|
@ -251,7 +261,8 @@ impl Node {
|
|||
/// `sending`; `Outbox::reset_stale` requeues those on the next unlock, the
|
||||
/// same recovery the parent uses for a process that died mid-attempt.
|
||||
async fn drain(&self, budget: Duration) -> Result<FlushOutcome, String> {
|
||||
let attempted = match tokio::time::timeout(budget, self.outbox.flush_all(unix_now())).await {
|
||||
let attempted = match tokio::time::timeout(budget, self.outbox.flush_all(unix_now())).await
|
||||
{
|
||||
Ok(Ok(rows)) => Some(rows),
|
||||
Ok(Err(e)) => return Err(e),
|
||||
Err(_) => {
|
||||
|
|
@ -447,19 +458,113 @@ impl Node {
|
|||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// The live wallet, cloned out of its lock. Every wallet call goes through
|
||||
/// this so a `std::sync::Mutex` guard never lives across an `.await`, and so
|
||||
/// the F7 screen can re-point the wallet mid-session.
|
||||
fn wallet_snapshot(&self) -> Wallet {
|
||||
self.wallet
|
||||
.lock()
|
||||
.map(|w| w.clone())
|
||||
.unwrap_or_else(|_| Wallet::disabled())
|
||||
}
|
||||
|
||||
/// The snapshot the TUI draws. Cheap: no dial, no lock on the RPC. It says
|
||||
/// "configured, not probed" (`connected == false`) until a probe has run.
|
||||
pub fn wallet_status(&self) -> WalletStatus {
|
||||
self.wallet_status
|
||||
.lock()
|
||||
.map(|s| s.clone())
|
||||
.unwrap_or_else(|_| WalletStatus::idle(self.wallet_snapshot().configured()))
|
||||
}
|
||||
|
||||
/// Probe the configured endpoint once and fold the result into the snapshot.
|
||||
/// This is the only thing that can flip `connected` to true.
|
||||
pub async fn wallet_refresh(&self) -> WalletStatus {
|
||||
let w = self.wallet_snapshot();
|
||||
let status = w.status().await;
|
||||
if let Ok(mut slot) = self.wallet_status.lock() {
|
||||
*slot = status.clone();
|
||||
}
|
||||
status
|
||||
}
|
||||
|
||||
/// The pay/tip readiness guard. Blocking-friendly: the TUI calls this from
|
||||
/// its key handler with `rt.block_on`, like `send_pay`/`send_tip` do.
|
||||
///
|
||||
/// It never dials Tor. It also never dials the wallet when the answer is
|
||||
/// already known locally — an unconfigured wallet and a `/tip` on a
|
||||
/// view-only wallet are refused before any socket is opened.
|
||||
pub async fn wallet_readiness(&self, is_tip: bool) -> Readiness {
|
||||
let w = self.wallet_snapshot();
|
||||
if !w.configured() {
|
||||
return Readiness::NotConfigured;
|
||||
}
|
||||
if is_tip && w.no_spend() {
|
||||
return Readiness::NoSpend;
|
||||
}
|
||||
let status = self.wallet_refresh().await;
|
||||
wallet::readiness(status.configured, status.connected, w.no_spend(), is_tip)
|
||||
}
|
||||
|
||||
/// The config screen's Test-connection button: one shot against the endpoint
|
||||
/// the user is typing. It does not touch the live wallet or the snapshot —
|
||||
/// a test of an untyped endpoint is not the wallet we are running.
|
||||
pub async fn wallet_test_connection(
|
||||
&self,
|
||||
rpc_url: &str,
|
||||
rpc_login: &str,
|
||||
) -> wallet::PingResult {
|
||||
Wallet::ping(rpc_url, rpc_login).await
|
||||
}
|
||||
|
||||
/// The config the F7 screen should show: the stored one, or the env-derived
|
||||
/// one when only env set the wallet (the migration path).
|
||||
pub fn wallet_config(&self) -> Option<WalletConfig> {
|
||||
if let Ok(store) = self.store.lock()
|
||||
&& let Ok(Some(cfg)) = store.load_wallet_config()
|
||||
{
|
||||
return Some(cfg);
|
||||
}
|
||||
self.wallet_snapshot().config()
|
||||
}
|
||||
|
||||
/// Persist the config screen's Save and re-read it: the live wallet and the
|
||||
/// snapshot both follow, so "saved · not connected" is visible without a
|
||||
/// restart. Validation happens before anything is written, so a rejected
|
||||
/// config leaves the running wallet exactly as it was.
|
||||
pub fn wallet_save_config(&self, cfg: &WalletConfig) -> Result<(), String> {
|
||||
Wallet::from_config(cfg).map_err(|e| wallet::redact(&e.to_string(), cfg.secret()))?;
|
||||
{
|
||||
let store = self.store.lock().map_err(|e| e.to_string())?;
|
||||
store.save_wallet_config(cfg).map_err(|e| e.to_string())?;
|
||||
}
|
||||
// Re-read from the store rather than trusting the caller's copy.
|
||||
let stored = self
|
||||
.store
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?
|
||||
.load_wallet_config()
|
||||
.map_err(|e| e.to_string())?;
|
||||
let wallet = Wallet::for_boot(stored.as_ref());
|
||||
let configured = wallet.configured();
|
||||
*self.wallet.lock().map_err(|e| e.to_string())? = wallet;
|
||||
*self.wallet_status.lock().map_err(|e| e.to_string())? = WalletStatus::idle(configured);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Current wallet balance as a human-readable XMR string, or `off` when the
|
||||
/// wallet is not configured.
|
||||
pub async fn wallet_balance(&self) -> String {
|
||||
if !self.wallet.configured() {
|
||||
let w = self.wallet_snapshot();
|
||||
if !w.configured() {
|
||||
return "off".into();
|
||||
}
|
||||
match self.wallet.get_balance().await {
|
||||
match w.get_balance().await {
|
||||
Ok(b) => crate::pay::atomic_to_xmr_str(&b),
|
||||
Err(_) => "err".into(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn wipe_messages(&self) -> Result<(), String> {
|
||||
self.store
|
||||
.lock()
|
||||
|
|
@ -560,8 +665,9 @@ impl Node {
|
|||
|
||||
/// Pay selected friend via wallet RPC, then signed receipt.
|
||||
pub async fn tip(&self, friend_pk: &[u8], atomic: &str, _memo: &str) -> Result<(), String> {
|
||||
if !self.wallet.configured() {
|
||||
return Err("wallet not connected — set ONIONWIRE_WALLET_RPC".into());
|
||||
let w = self.wallet_snapshot();
|
||||
if !w.configured() {
|
||||
return Err(wallet::NOT_CONFIGURED_MSG.into());
|
||||
}
|
||||
let address = self
|
||||
.friend_profile(friend_pk)?
|
||||
|
|
@ -574,8 +680,7 @@ impl Node {
|
|||
let amount: u64 = atomic
|
||||
.parse()
|
||||
.map_err(|_| "amount too large for wallet RPC".to_string())?;
|
||||
let txid = self
|
||||
.wallet
|
||||
let txid = w
|
||||
.transfer(&address, amount)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
|
@ -614,8 +719,9 @@ impl Node {
|
|||
}
|
||||
|
||||
async fn invoice_address(&self) -> Result<String, String> {
|
||||
if self.wallet.configured()
|
||||
&& let Ok(addr) = self.wallet.create_address().await
|
||||
let w = self.wallet_snapshot();
|
||||
if w.configured()
|
||||
&& let Ok(addr) = w.create_address().await
|
||||
&& pay::check_address(&addr).is_ok()
|
||||
{
|
||||
return Ok(addr);
|
||||
|
|
@ -634,10 +740,9 @@ impl Node {
|
|||
/// how to wrap it.
|
||||
pub async fn wallet_receive_address(&self) -> Result<String, String> {
|
||||
let profile_addr = self.self_profile()?.xmr_addr;
|
||||
if self
|
||||
.wallet
|
||||
.configured()
|
||||
&& let Ok(addr) = self.wallet.get_address().await
|
||||
let w = self.wallet_snapshot();
|
||||
if w.configured()
|
||||
&& let Ok(addr) = w.get_address().await
|
||||
&& pay::check_address(&addr).is_ok()
|
||||
{
|
||||
return Ok(addr);
|
||||
|
|
@ -653,8 +758,9 @@ impl Node {
|
|||
/// configured. Calls `pay::atomic_to_xmr_str` so the UI never parses
|
||||
/// piconero itself.
|
||||
pub async fn wallet_balance_line(&self) -> String {
|
||||
if self.wallet.configured() {
|
||||
match self.wallet.get_balance().await {
|
||||
let w = self.wallet_snapshot();
|
||||
if w.configured() {
|
||||
match w.get_balance().await {
|
||||
Ok(b) if !b.is_empty() => {
|
||||
let xmr = pay::atomic_to_xmr_str(&b);
|
||||
if xmr == "0" {
|
||||
|
|
@ -674,11 +780,15 @@ impl Node {
|
|||
/// Recent in/out transfers for the wallet screen, newest first, capped to a
|
||||
/// small window. Returns the same `TransferRow` shape the existing tests
|
||||
/// already cover.
|
||||
pub async fn wallet_recent_transfers(&self, cap: usize) -> Result<Vec<wallet::TransferRow>, String> {
|
||||
if !self.wallet.configured() {
|
||||
pub async fn wallet_recent_transfers(
|
||||
&self,
|
||||
cap: usize,
|
||||
) -> Result<Vec<wallet::TransferRow>, String> {
|
||||
let w = self.wallet_snapshot();
|
||||
if !w.configured() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let rows = self.wallet.get_transfers().await.map_err(|e| e.to_string())?;
|
||||
let rows = w.get_transfers().await.map_err(|e| e.to_string())?;
|
||||
let mut rows = rows;
|
||||
rows.sort_by(|a, b| b.txid.cmp(&a.txid));
|
||||
Ok(rows.into_iter().take(cap).collect())
|
||||
|
|
@ -687,11 +797,11 @@ impl Node {
|
|||
/// Create a new spend-capable wallet. Returns the seed once for backup.
|
||||
/// The user never pastes a key on this path.
|
||||
pub async fn wallet_create(&self, password: &str) -> Result<String, String> {
|
||||
if !self.wallet.configured() {
|
||||
return Err("wallet not connected — set ONIONWIRE_WALLET_RPC".into());
|
||||
let w = self.wallet_snapshot();
|
||||
if !w.configured() {
|
||||
return Err(wallet::NOT_CONFIGURED_MSG.into());
|
||||
}
|
||||
self.wallet
|
||||
.create_wallet(wallet::DEFAULT_WALLET_FILENAME, password)
|
||||
w.create_wallet(wallet::DEFAULT_WALLET_FILENAME, password)
|
||||
.await
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
|
@ -699,11 +809,11 @@ impl Node {
|
|||
/// Import a wallet from an existing 25-word seed. This is the only path
|
||||
/// that takes a user-supplied secret.
|
||||
pub async fn wallet_restore(&self, password: &str, seed: &str) -> Result<(), String> {
|
||||
if !self.wallet.configured() {
|
||||
return Err("wallet not connected — set ONIONWIRE_WALLET_RPC".into());
|
||||
let w = self.wallet_snapshot();
|
||||
if !w.configured() {
|
||||
return Err(wallet::NOT_CONFIGURED_MSG.into());
|
||||
}
|
||||
self.wallet
|
||||
.restore_wallet(wallet::DEFAULT_WALLET_FILENAME, password, seed)
|
||||
w.restore_wallet(wallet::DEFAULT_WALLET_FILENAME, password, seed)
|
||||
.await
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
|
@ -837,7 +947,10 @@ impl Node {
|
|||
|
||||
/// Pending and failed outbox rows for a friend, for UI surfacing.
|
||||
/// Delivered rows are dropped, so a message absent here was sent.
|
||||
pub fn outbox_pending(&self, friend_pk: &[u8]) -> Result<Vec<crate::store::OutboxEntry>, String> {
|
||||
pub fn outbox_pending(
|
||||
&self,
|
||||
friend_pk: &[u8],
|
||||
) -> Result<Vec<crate::store::OutboxEntry>, String> {
|
||||
self.outbox.pending(friend_pk)
|
||||
}
|
||||
|
||||
|
|
@ -1059,9 +1172,10 @@ impl Node {
|
|||
// Honest prerequisite: an unsynced wallet cannot see the tx, so a
|
||||
// get_transfers "miss" would be a lie. Until the chain is ready the
|
||||
// receipt stays verified=0 — that is the correct state, not an error.
|
||||
let ready = self.wallet.configured() && self.wallet.can_verify_receipts().await;
|
||||
let w = self.wallet_snapshot();
|
||||
let ready = w.configured() && w.can_verify_receipts().await;
|
||||
if ready {
|
||||
match self.wallet.get_transfers().await {
|
||||
match w.get_transfers().await {
|
||||
Ok(rows)
|
||||
if wallet::transfers_match(
|
||||
&rows,
|
||||
|
|
@ -1223,4 +1337,3 @@ pub fn dir_contains_bytes(dir: &Path, needle: &[u8]) -> bool {
|
|||
}
|
||||
false
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue