diff --git a/crates/onionwire-sdk/src/lib.rs b/crates/onionwire-sdk/src/lib.rs index 6238005..c03ad35 100644 --- a/crates/onionwire-sdk/src/lib.rs +++ b/crates/onionwire-sdk/src/lib.rs @@ -34,6 +34,8 @@ use onionwire::node::{FlushOutcome as CoreFlushOutcome, Node}; use onionwire::outbox::{DEFAULT_MAX_ATTEMPTS, OutboxEntry, OutboxStatus}; use onionwire::pay; use onionwire::qr; +use onionwire::wallet::ChainSource; +use onionwire::wallet_child::ChildConfig; uniffi::setup_scaffolding!(); @@ -595,6 +597,65 @@ impl Wire { .map_err(WireError::new) } + /// Start the app-owned wallet child (`monero-wallet-rpc`, plus `monerod` for + /// a local chain source and `arti proxy` for a `.onion` daemon) and point + /// this node's wallet at it, then probe it once and return what it reached. + /// + /// The spawn itself is synchronous inside the node: argv is built, the + /// process is spawned, and a `.onion` source waits up to 20s for the SOCKS + /// listener. Call it off the main thread, like `send_tip`. + /// + /// Idempotent: a live child is reported, not respawned. Fails with + /// `WireError.Failed` when no chain source is configured + /// (`set_chain_source`), when a binary cannot be spawned, or when the SOCKS + /// listener never came up. `WireError.Stopped` after `shutdown`. + /// + /// The returned state is `connecting` at best on the first call — the wallet + /// RPC is not answering the instant the process exists. Report it as it is, + /// and call [`Wire::wallet_status`] (or start again) for the later states. + pub async fn start_wallet_child(&self) -> WResult { + let node = self.node()?; + node.start_wallet_child().map_err(WireError::new)?; + Ok(child_status(node.wallet_child_refresh().await)) + } + + /// Stop the wallet child and its daemon/SOCKS siblings. Idempotent: stopping + /// what is not running is `Ok(())`. The wallet falls back to the stored or + /// env `monero-wallet-rpc` config, if any. + pub fn stop_wallet_child(&self) -> WResult<()> { + self.node()?.stop_wallet_child().map_err(WireError::new) + } + + /// The last observable wallet-child state. Never dials, never spawns: + /// `state` is `off` / `connecting` / `syncing` / `ready` / `exited`, and + /// `ready` is only ever written by a probe that saw a synced chain. + /// + /// Synchronous on purpose — it is safe to call from a UI refresh loop. + pub fn wallet_status(&self) -> WResult { + Ok(child_status(self.node()?.wallet_child_status())) + } + + /// The chain-source + binary settings the child spawns with, as stored. + /// Returns the defaults (receive-only, binaries by name) when nothing was + /// ever set, so a settings screen can prefill from it safely. + pub fn chain_source(&self) -> WResult { + Ok(chain_config(&self.node()?.wallet_child_config())) + } + + /// Set the chain source and the binaries the child runs. Validated before + /// anything is written: a rejected config leaves the stored one untouched + /// and a running child untouched (the change applies on the next start). + /// + /// This is a stored setting, not a start: call [`Wire::start_wallet_child`] + /// after it. `wallet_rpc_bin` / `arti_bin` take absolute paths on Android + /// (`context.applicationInfo.nativeLibraryDir`); the core never resolves a + /// binary the caller named explicitly. + pub fn set_chain_source(&self, config: ChainConfig) -> WResult<()> { + self.node()? + .set_wallet_child_config(&child_config(config)) + .map_err(WireError::new) + } + /// Sign an invoice frame with this node's identity key. /// /// The ed25519 secret stays inside the node: this is the only way the SDK @@ -658,6 +719,78 @@ fn from_core_flush(o: CoreFlushOutcome) -> FlushOutcome { } } +/// The core child settings a [`ChainConfig`] describes. +fn child_config(cfg: ChainConfig) -> ChildConfig { + let source = match cfg.kind { + ChainChoice::None => ChainSource::None, + ChainChoice::Local => ChainSource::Local { + monerod_bin: PathBuf::from(cfg.monerod_bin), + datadir: PathBuf::from(cfg.monerod_datadir), + }, + ChainChoice::Remote => ChainSource::Remote { + host: cfg.host, + port: cfg.port, + }, + }; + ChildConfig { + source, + wallet_rpc_bin: PathBuf::from(cfg.wallet_rpc_bin), + arti_bin: PathBuf::from(cfg.arti_bin), + wallet_filename: cfg.wallet_filename, + } +} + +/// And back, for the Kotlin prefill. +fn chain_config(cfg: &ChildConfig) -> ChainConfig { + let (kind, host, port, monerod_bin, monerod_datadir) = match &cfg.source { + ChainSource::None => ( + ChainChoice::None, + String::new(), + 0, + String::new(), + String::new(), + ), + ChainSource::Local { + monerod_bin, + datadir, + } => ( + ChainChoice::Local, + String::new(), + 0, + monerod_bin.display().to_string(), + datadir.display().to_string(), + ), + ChainSource::Remote { host, port } => ( + ChainChoice::Remote, + host.clone(), + *port, + String::new(), + String::new(), + ), + }; + ChainConfig { + kind, + monerod_bin, + monerod_datadir, + host, + port, + wallet_rpc_bin: cfg.wallet_rpc_bin.display().to_string(), + arti_bin: cfg.arti_bin.display().to_string(), + wallet_filename: cfg.wallet_filename.clone(), + } +} + +/// The core child snapshot as the SDK record. +fn child_status(status: onionwire::wallet_child::WalletChildStatus) -> WalletChildStatus { + WalletChildStatus { + state: status.state.as_str().to_string(), + running: status.running, + configured: status.configured, + chain_source: status.chain_source, + last_error: status.last_error, + } +} + fn hex(bytes: &[u8]) -> String { let mut s = String::with_capacity(bytes.len() * 2); for b in bytes { @@ -740,6 +873,67 @@ pub struct TransferRow { pub address: String, } +/// Where the app-owned wallet child gets its chain from. Mirrors the core's +/// `ChainSource`: nothing is implied — `Remote` is a daemon the user supplied. +#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)] +pub enum ChainChoice { + /// Receive-only: no child, no daemon, no spend. + None, + /// The app spawns `monerod` on this device. + Local, + /// A daemon address the user supplied (loopback, `.onion`, or clearnet). + Remote, +} + +/// The wallet-child settings, as the Kotlin side sets them. +/// +/// One record for all three kinds: the fields a kind does not use are ignored +/// by that kind (`None` needs no binaries, `Local` needs `monerod_bin` and +/// `monerod_datadir`, `Remote` needs `host` and `port`). Persisted in the +/// encrypted store, so it survives a restart without env vars. +#[derive(Debug, Clone, uniffi::Record)] +pub struct ChainConfig { + pub kind: ChainChoice, + /// `Local` only. Absolute path on Android (`nativeLibraryDir`); a bare name + /// resolves on `PATH` on desktop. + pub monerod_bin: String, + /// `Local` only. Data directory for the local daemon. + pub monerod_datadir: String, + /// `Remote` only. Daemon host. + pub host: String, + /// `Remote` only. Daemon port. + pub port: u16, + /// `monero-wallet-rpc`. Absolute path on Android, else resolved on `PATH`. + pub wallet_rpc_bin: String, + /// `arti`, run as the SOCKS child for a `.onion` daemon only. + pub arti_bin: String, + /// Wallet file name inside `$ONIONWIRE_HOME/wallet`. + pub wallet_filename: String, +} + +/// One snapshot of the app-owned wallet child. +/// +/// Derived from the real child process plus the last probe, never from env-var +/// presence. `state` is one of `off`, `connecting`, `syncing`, `ready`, +/// `exited`. `chain_source` is the label of the configured source: +/// `receive-only`, `local node`, `remote onion daemon`, or `remote daemon`. +#[derive(Debug, Clone, uniffi::Record)] +pub struct WalletChildStatus { + /// `off` until the child is started and again after it is stopped; `exited` + /// when a started child has died; `connecting` while its wallet RPC has not + /// answered; `syncing` once it has but the chain is behind; `ready` when + /// both hold. `ready` is never reported for a dead child. + pub state: String, + /// A `monero-wallet-rpc` child is alive right now. + pub running: bool, + /// A chain source is configured (anything but receive-only). + pub configured: bool, + /// The chain-source label. + pub chain_source: String, + /// The last failure, display-ready. Never carries the generated login. + pub last_error: Option, +} + /// One signed invoice frame: "pay me `amount_atomic` piconero at `address`". /// /// Mirrors `pay::Invoice`. `sig` is the raw 64-byte ed25519 signature, not hex,