wallet: wire the app-owned child into the node and TUI
The module was dead code: nothing started the child, so the payment spines could only ever see an env-configured monero-wallet-rpc. Now: - Node::start_wallet_child / stop_wallet_child / wallet_child_status / wallet_child_refresh / wallet_child_config / set_wallet_child_config. The start path honours the configured absolute binary path, points the node's wallet at the child, and shutdown() stops the child rather than orphaning a spend-capable RPC behind a loopback socket. - The status is derived from the real process (try_wait) and the last probe: off / connecting / syncing / ready / exited, plus the chain-source label. A dead child is never reported ready. - ChildConfig (source, wallet_rpc_bin, arti_bin, wallet_filename) persists in the sealed wallet_child record next to the generated creds; the record is a read-modify-write so first-start creds never clobber a saved config. - TUI: F6 gains s (start) / x (stop) / p (probe) and a child status row, and the /pay and /tip key handlers now call the core readiness guard before sending.
This commit is contained in:
parent
11a021acb3
commit
440a37caf0
5 changed files with 1056 additions and 33 deletions
|
|
@ -165,6 +165,16 @@ impl WalletChild {
|
|||
&self.wallet_filename
|
||||
}
|
||||
|
||||
/// This child's own settings, in the persisted shape.
|
||||
pub fn config(&self) -> ChildConfig {
|
||||
ChildConfig {
|
||||
source: self.source.clone(),
|
||||
wallet_rpc_bin: self.wallet_rpc_bin.clone(),
|
||||
arti_bin: self.arti_bin.clone(),
|
||||
wallet_filename: self.wallet_filename.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether this configuration spawns any child.
|
||||
pub fn is_enabled(&self) -> bool {
|
||||
!matches!(self.source, ChainSource::None)
|
||||
|
|
@ -173,7 +183,15 @@ impl WalletChild {
|
|||
/// Does this host name a Tor onion service? Only `.onion` daemons need the
|
||||
/// SOCKS child; a loopback or clearnet host the user typed does not.
|
||||
pub fn is_onion_host(host: &str) -> bool {
|
||||
host.trim().to_ascii_lowercase().ends_with(".onion")
|
||||
wallet::is_onion_host(host)
|
||||
}
|
||||
|
||||
/// Is the `monero-wallet-rpc` child itself alive? The wallet RPC is the
|
||||
/// process every wallet call goes through, so this — not "some child is up"
|
||||
/// — is what decides `ready` vs `exited`.
|
||||
pub fn wallet_rpc_running(&self) -> bool {
|
||||
let _ = self.reap_exited();
|
||||
self.wallet_rpc.lock().map(|g| g.is_some()).unwrap_or(false)
|
||||
}
|
||||
|
||||
/// Is a tracked child still alive? Polls the process table; an exited child
|
||||
|
|
@ -496,39 +514,262 @@ impl WalletChild {
|
|||
|
||||
/// Load the sealed creds, or generate them once and seal them in the store.
|
||||
/// The store is the only place they live; `wallet_dir` never sees them.
|
||||
///
|
||||
/// The record is a read-modify-write: the settings written by a config
|
||||
/// screen are never clobbered by first-start credential generation.
|
||||
fn creds(&self, store: &Store) -> Result<(String, Zeroizing<String>)> {
|
||||
if let Some(raw) = store
|
||||
let existing = store
|
||||
.load_wallet_child()
|
||||
.map_err(|e| ChildError(format!("read wallet-child creds: {e}")))?
|
||||
{
|
||||
let parsed: serde_json::Value = serde_json::from_str(&raw)
|
||||
.map_err(|e| ChildError(format!("parse wallet-child creds: {e}")))?;
|
||||
let user = parsed
|
||||
.map_err(|e| ChildError(format!("read wallet-child creds: {e}")))?;
|
||||
// Persist what this child is running with, before anything is spawned.
|
||||
let record = merge_record(existing.as_deref(), self.config().record_fields());
|
||||
store
|
||||
.save_wallet_child(&record)
|
||||
.map_err(|e| ChildError(format!("seal wallet-child config: {e}")))?;
|
||||
|
||||
if let Some(raw) = existing.as_deref()
|
||||
&& let Ok(parsed) = serde_json::from_str::<serde_json::Value>(raw)
|
||||
&& let Some(user) = parsed
|
||||
.get("user")
|
||||
.and_then(|v| v.as_str())
|
||||
.filter(|s| !s.is_empty())
|
||||
.ok_or_else(|| ChildError("wallet-child creds missing user".into()))?;
|
||||
let pass = parsed
|
||||
&& let Some(pass) = parsed
|
||||
.get("pass")
|
||||
.and_then(|v| v.as_str())
|
||||
.filter(|s| !s.is_empty())
|
||||
.ok_or_else(|| ChildError("wallet-child creds missing pass".into()))?;
|
||||
{
|
||||
return Ok((user.to_string(), Zeroizing::new(pass.to_string())));
|
||||
}
|
||||
let (user, pass) = generate_creds();
|
||||
let record = serde_json::json!({
|
||||
"user": user,
|
||||
"pass": pass.as_str(),
|
||||
"source": source_to_json(&self.source),
|
||||
"wallet_filename": self.wallet_filename,
|
||||
});
|
||||
let record = merge_record(
|
||||
Some(&record),
|
||||
serde_json::json!({"user": user, "pass": pass.as_str()}),
|
||||
);
|
||||
store
|
||||
.save_wallet_child(&record.to_string())
|
||||
.save_wallet_child(&record)
|
||||
.map_err(|e| ChildError(format!("seal wallet-child creds: {e}")))?;
|
||||
Ok((user, pass))
|
||||
}
|
||||
}
|
||||
|
||||
/// Everything the app needs to spawn the wallet child, minus the RPC creds:
|
||||
/// which chain to dial, which binaries to run, which wallet file to open.
|
||||
///
|
||||
/// Persisted as part of the sealed `wallet_child` record (the generated creds
|
||||
/// live in the same record), so a configured child survives a restart without
|
||||
/// env vars. This is the shape the SDK's `ChainConfig` record maps to.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ChildConfig {
|
||||
/// The user's chain-source choice. `None` is receive-only: nothing spawns.
|
||||
pub source: ChainSource,
|
||||
/// `monero-wallet-rpc`. A bare name is resolved on `PATH`; Android passes an
|
||||
/// absolute path out of the app's native library directory.
|
||||
pub wallet_rpc_bin: PathBuf,
|
||||
/// `arti`, run as the SOCKS child for a `.onion` daemon only.
|
||||
pub arti_bin: PathBuf,
|
||||
/// Wallet file name inside `$ONIONWIRE_HOME/wallet`.
|
||||
pub wallet_filename: String,
|
||||
}
|
||||
|
||||
impl Default for ChildConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
source: ChainSource::None,
|
||||
wallet_rpc_bin: PathBuf::from(DEFAULT_WALLET_RPC_BIN),
|
||||
arti_bin: PathBuf::from(DEFAULT_ARTI_BIN),
|
||||
wallet_filename: wallet::DEFAULT_WALLET_FILENAME.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ChildConfig {
|
||||
/// Nothing spawns: the wallet screen is receive-only.
|
||||
pub fn receive_only() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Does this configuration spawn a child at all?
|
||||
pub fn is_enabled(&self) -> bool {
|
||||
!matches!(self.source, ChainSource::None)
|
||||
}
|
||||
|
||||
/// The chain-source label a status line shows.
|
||||
pub fn source_label(&self) -> String {
|
||||
self.source.label()
|
||||
}
|
||||
/// Reject a configuration that could only fail later (empty binary path,
|
||||
/// empty host, port 0) *before* it is persisted or spawned.
|
||||
pub fn validate(&self) -> Result<()> {
|
||||
if self.wallet_filename.trim().is_empty() {
|
||||
return Err(ChildError("wallet filename required".into()));
|
||||
}
|
||||
// Receive-only spawns nothing, so a binary path there is not a setting
|
||||
// to reject — a Kotlin caller legitimately leaves both empty for it.
|
||||
if matches!(self.source, ChainSource::None) {
|
||||
return Ok(());
|
||||
}
|
||||
if self.wallet_rpc_bin.as_os_str().is_empty() {
|
||||
return Err(ChildError("monero-wallet-rpc path required".into()));
|
||||
}
|
||||
if self.arti_bin.as_os_str().is_empty() {
|
||||
return Err(ChildError("arti path required".into()));
|
||||
}
|
||||
match &self.source {
|
||||
ChainSource::None => {}
|
||||
ChainSource::Local {
|
||||
monerod_bin,
|
||||
datadir,
|
||||
} => {
|
||||
if monerod_bin.as_os_str().is_empty() {
|
||||
return Err(ChildError("monerod path required for a local node".into()));
|
||||
}
|
||||
if datadir.as_os_str().is_empty() {
|
||||
return Err(ChildError(
|
||||
"monerod data dir required for a local node".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
ChainSource::Remote { host, port } => {
|
||||
if host.trim().is_empty() {
|
||||
return Err(ChildError(
|
||||
"daemon host required for a remote daemon".into(),
|
||||
));
|
||||
}
|
||||
if *port == 0 {
|
||||
return Err(ChildError(
|
||||
"daemon port required for a remote daemon".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Build the child this configuration describes. `wallet_dir` is the
|
||||
/// caller's `$ONIONWIRE_HOME/wallet`.
|
||||
pub fn build(&self, wallet_dir: PathBuf) -> Result<WalletChild> {
|
||||
self.validate()?;
|
||||
Ok(WalletChild::new(
|
||||
self.source.clone(),
|
||||
wallet_dir,
|
||||
Some(self.wallet_filename.clone()),
|
||||
)?
|
||||
.with_wallet_rpc_bin(self.wallet_rpc_bin.clone())
|
||||
.with_arti_bin(self.arti_bin.clone()))
|
||||
}
|
||||
|
||||
/// The settings half of the sealed record, for
|
||||
/// [`merge_record`] + `Store::save_wallet_child`.
|
||||
pub fn record_fields(&self) -> serde_json::Value {
|
||||
serde_json::json!({
|
||||
"source": source_to_json(&self.source),
|
||||
"wallet_rpc_bin": self.wallet_rpc_bin.display().to_string(),
|
||||
"arti_bin": self.arti_bin.display().to_string(),
|
||||
"wallet_filename": self.wallet_filename,
|
||||
})
|
||||
}
|
||||
|
||||
/// Read the stored settings back. `Ok(None)` when the record holds creds but
|
||||
/// no source block — a record written before the settings were persisted.
|
||||
pub fn from_record(raw: &str) -> Result<Option<Self>> {
|
||||
let parsed: serde_json::Value = serde_json::from_str(raw)
|
||||
.map_err(|e| ChildError(format!("parse wallet-child record: {e}")))?;
|
||||
let Some(source) = parsed.get("source") else {
|
||||
return Ok(None);
|
||||
};
|
||||
let mut cfg = Self {
|
||||
source: source_from_json(source)?,
|
||||
..Self::default()
|
||||
};
|
||||
if let Some(v) = parsed.get("wallet_rpc_bin").and_then(|v| v.as_str()) {
|
||||
cfg.wallet_rpc_bin = PathBuf::from(v);
|
||||
}
|
||||
if let Some(v) = parsed.get("arti_bin").and_then(|v| v.as_str()) {
|
||||
cfg.arti_bin = PathBuf::from(v);
|
||||
}
|
||||
if let Some(v) = parsed.get("wallet_filename").and_then(|v| v.as_str()) {
|
||||
cfg.wallet_filename = v.to_string();
|
||||
}
|
||||
Ok(Some(cfg))
|
||||
}
|
||||
}
|
||||
|
||||
/// Merge `fields` into the sealed record JSON, keeping every other key — the
|
||||
/// generated RPC creds in particular. A missing or non-object `existing` starts
|
||||
/// a fresh record.
|
||||
pub fn merge_record(existing: Option<&str>, fields: serde_json::Value) -> String {
|
||||
let mut obj = existing
|
||||
.and_then(|raw| serde_json::from_str::<serde_json::Value>(raw).ok())
|
||||
.and_then(|v| v.as_object().cloned())
|
||||
.unwrap_or_default();
|
||||
if let serde_json::Value::Object(patch) = fields {
|
||||
for (k, v) in patch {
|
||||
obj.insert(k, v);
|
||||
}
|
||||
}
|
||||
serde_json::Value::Object(obj).to_string()
|
||||
}
|
||||
|
||||
/// The observable state of the app-owned wallet child. Derived from the real
|
||||
/// process + the last probe, never from env-var presence.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum WalletChildState {
|
||||
/// No child configured, or none started yet.
|
||||
Off,
|
||||
/// The child process is up but its wallet RPC has not answered.
|
||||
Connecting,
|
||||
/// The wallet answered, but the chain is not at the tip yet.
|
||||
Syncing,
|
||||
/// Wallet RPC answered and the chain is synced.
|
||||
Ready,
|
||||
/// A started child has exited (crashed or killed). Never reported ready.
|
||||
Exited,
|
||||
}
|
||||
|
||||
impl WalletChildState {
|
||||
/// The lowercase label the SDK and the TUI show.
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
WalletChildState::Off => "off",
|
||||
WalletChildState::Connecting => "connecting",
|
||||
WalletChildState::Syncing => "syncing",
|
||||
WalletChildState::Ready => "ready",
|
||||
WalletChildState::Exited => "exited",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One snapshot of the child. Cheap to read: no dial, no lock held across work.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct WalletChildStatus {
|
||||
pub state: WalletChildState,
|
||||
/// A child process is alive right now.
|
||||
pub running: bool,
|
||||
/// A chain source is configured (not receive-only), whether or not it runs.
|
||||
pub configured: bool,
|
||||
/// The chain-source label: `receive-only` / `local node` /
|
||||
/// `remote onion daemon` / `remote daemon`.
|
||||
pub chain_source: String,
|
||||
/// The last failure, display-ready. Never carries the generated login.
|
||||
pub last_error: Option<String>,
|
||||
}
|
||||
|
||||
impl WalletChildStatus {
|
||||
/// The one-line form a TUI status row or a Kotlin toast can show. Empty when
|
||||
/// nothing is configured, so an unconfigured install renders no row.
|
||||
pub fn line(&self) -> String {
|
||||
if !self.configured && self.state == WalletChildState::Off {
|
||||
return String::new();
|
||||
}
|
||||
let mut line = format!("child {} · {}", self.state.as_str(), self.chain_source);
|
||||
if let Some(err) = &self.last_error {
|
||||
line.push_str(" · ");
|
||||
line.push_str(err);
|
||||
}
|
||||
line
|
||||
}
|
||||
}
|
||||
|
||||
/// The loopback client built from the generated URL — the production path when
|
||||
/// the app owns the child. Never `ONIONWIRE_WALLET_RPC`, never anything but
|
||||
/// numeric loopback.
|
||||
|
|
@ -604,6 +845,51 @@ fn source_to_json(source: &ChainSource) -> serde_json::Value {
|
|||
}
|
||||
}
|
||||
|
||||
fn source_from_json(value: &serde_json::Value) -> Result<ChainSource> {
|
||||
let kind = value
|
||||
.get("kind")
|
||||
.and_then(|k| k.as_str())
|
||||
.unwrap_or_default();
|
||||
match kind {
|
||||
"none" => Ok(ChainSource::None),
|
||||
"local" => {
|
||||
let monerod_bin = value
|
||||
.get("monerod_bin")
|
||||
.and_then(|v| v.as_str())
|
||||
.filter(|s| !s.is_empty())
|
||||
.ok_or_else(|| {
|
||||
ChildError("stored local chain source has no monerod path".into())
|
||||
})?;
|
||||
let datadir = value
|
||||
.get("datadir")
|
||||
.and_then(|v| v.as_str())
|
||||
.filter(|s| !s.is_empty())
|
||||
.ok_or_else(|| ChildError("stored local chain source has no data dir".into()))?;
|
||||
Ok(ChainSource::Local {
|
||||
monerod_bin: PathBuf::from(monerod_bin),
|
||||
datadir: PathBuf::from(datadir),
|
||||
})
|
||||
}
|
||||
"remote" => {
|
||||
let host = value
|
||||
.get("host")
|
||||
.and_then(|v| v.as_str())
|
||||
.filter(|s| !s.trim().is_empty())
|
||||
.ok_or_else(|| ChildError("stored remote chain source has no host".into()))?;
|
||||
let port = value
|
||||
.get("port")
|
||||
.and_then(|v| v.as_u64())
|
||||
.filter(|p| (1..=65535).contains(p))
|
||||
.ok_or_else(|| ChildError("stored remote chain source has no port".into()))?;
|
||||
Ok(ChainSource::Remote {
|
||||
host: host.to_string(),
|
||||
port: port as u16,
|
||||
})
|
||||
}
|
||||
other => Err(ChildError(format!("unknown chain source kind {other:?}"))),
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn(argv: &[String], what: &str) -> Result<Child> {
|
||||
let (bin, args) = argv
|
||||
.split_first()
|
||||
|
|
|
|||
Loading…
Reference in a new issue