feat(android): wallet screen and SDK payment surface
Expose wallet balance/address/transfers plus one-shot invoice and tip on the UniFFI SDK. Wire a Compose wallet route (QR, composers) without dropping wipe-chat. Ignore machine-local android/.cargo NDK paths.
This commit is contained in:
parent
dc52df2366
commit
7cf98bb9fe
12 changed files with 728 additions and 18 deletions
|
|
@ -32,12 +32,12 @@ use onionwire::Message;
|
|||
use onionwire::hs::TorState as CoreTorState;
|
||||
use onionwire::node::{FlushOutcome as CoreFlushOutcome, Node};
|
||||
use onionwire::outbox::{DEFAULT_MAX_ATTEMPTS, OutboxEntry, OutboxStatus};
|
||||
use onionwire::pay;
|
||||
use onionwire::qr;
|
||||
|
||||
uniffi::setup_scaffolding!();
|
||||
|
||||
/// Install rustls's process-level default `CryptoProvider`, exactly once.
|
||||
///
|
||||
/// Belt and braces. `Cargo.toml` pins `rustls` with the `ring` feature, which
|
||||
/// is what actually fixes the missing-provider failure: with it, rustls
|
||||
/// resolves a provider from crate features on its own. This function exists
|
||||
|
|
@ -484,6 +484,97 @@ impl Wire {
|
|||
pub async fn version(&self) -> String {
|
||||
env!("CARGO_PKG_VERSION").to_string()
|
||||
}
|
||||
|
||||
// ---- Wallet / payment surface (v1 Monero features) ----
|
||||
|
||||
/// Current wallet balance as a display string, or `"off"` / `"err"`.
|
||||
///
|
||||
/// Returns `"off"` when the wallet is not configured (no `ONIONWIRE_WALLET_RPC`),
|
||||
/// `"err"` when the wallet RPC call failed, and `"balance X.X XMR"` on success.
|
||||
/// A stopped node (shut-down handle) returns `"off"` — the wallet is not
|
||||
/// accessible without a live node, and the UI treats "off" as "wallet not
|
||||
/// available" rather than an error state.
|
||||
pub async fn wallet_balance(&self) -> String {
|
||||
match self.node() {
|
||||
Ok(node) => node.wallet_balance().await,
|
||||
Err(_) => "off".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Receive address for the wallet screen: a freshly created subaddress when
|
||||
/// the wallet is configured, else the profile `xmr_addr` fallback.
|
||||
///
|
||||
/// Returns the raw address (not a `monero:` URI) so the QR render path can
|
||||
/// decide how to wrap it. Returns `WireError.Stopped` on a shut-down handle,
|
||||
/// `WireError.Failed` on RPC/profile errors.
|
||||
pub async fn wallet_receive_address(&self) -> WResult<String> {
|
||||
let node = self.node()?;
|
||||
node.wallet_receive_address().await.map_err(WireError::new)
|
||||
}
|
||||
|
||||
/// Recent in/out transfers for the wallet screen, newest first, capped to `cap`.
|
||||
///
|
||||
/// Returns an empty list when the wallet is not configured (no transfers to show).
|
||||
/// `amount` is formatted XMR, not raw piconero.
|
||||
pub async fn wallet_recent_transfers(&self, cap: u32) -> WResult<Vec<TransferRow>> {
|
||||
let node = self.node()?;
|
||||
let rows = node
|
||||
.wallet_recent_transfers(cap as usize)
|
||||
.await
|
||||
.map_err(WireError::new)?;
|
||||
Ok(rows
|
||||
.into_iter()
|
||||
.map(|r| TransferRow {
|
||||
txid: r.txid,
|
||||
amount: pay::atomic_to_xmr_str(&r.amount),
|
||||
address: r.address,
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Send an invoice to a friend: sign an invoice for my receive address and send
|
||||
/// the frame one-shot. Peer onion down → this fails; invoices are not queued.
|
||||
///
|
||||
/// `amount_xmr` is a user-facing XMR amount (e.g. `"0.5"`) — validated and
|
||||
/// converted to piconero internally. `memo` may be empty. The invoice uses the
|
||||
/// wallet subaddress if available, else the profile `xmr_addr` fallback.
|
||||
pub async fn send_invoice(
|
||||
&self,
|
||||
pubkey_hex: String,
|
||||
amount_xmr: String,
|
||||
memo: String,
|
||||
) -> WResult<()> {
|
||||
let pk = unhex(&pubkey_hex)?;
|
||||
let atomic = pay::xmr_to_atomic(&amount_xmr).map_err(WireError::new)?;
|
||||
self.node()?
|
||||
.pay_invoice(&pk, &atomic, &memo)
|
||||
.await
|
||||
.map_err(WireError::new)
|
||||
}
|
||||
|
||||
/// Send a tip to a friend: transfer XMR from my wallet, then send a signed
|
||||
/// receipt and tip frame, both one-shot. Peer onion down → this fails after
|
||||
/// the chain transfer; tips are not queued.
|
||||
///
|
||||
/// `amount_xmr` is a user-facing XMR amount. `memo` may be empty. The
|
||||
/// recipient's Monero address is read from their profile.
|
||||
///
|
||||
/// Requires the wallet to be configured (`ONIONWIRE_WALLET_RPC`). Fails when
|
||||
/// the wallet is not connected, the friend has no Monero address, or the
|
||||
/// amount is invalid.
|
||||
pub async fn send_tip(
|
||||
&self,
|
||||
pubkey_hex: String,
|
||||
amount_xmr: String,
|
||||
memo: String,
|
||||
) -> WResult<()> {
|
||||
let pk = unhex(&pubkey_hex)?;
|
||||
let atomic = pay::xmr_to_atomic(&amount_xmr).map_err(WireError::new)?;
|
||||
self.node()?
|
||||
.tip(&pk, &atomic, &memo)
|
||||
.await
|
||||
.map_err(WireError::new)
|
||||
}
|
||||
}
|
||||
|
||||
fn from_core_flush(o: CoreFlushOutcome) -> FlushOutcome {
|
||||
|
|
@ -565,6 +656,45 @@ fn outbox_row(entry: &OutboxEntry) -> OutboxRow {
|
|||
}
|
||||
}
|
||||
|
||||
/// A Monero transfer row for the wallet screen.
|
||||
///
|
||||
/// `amount` is formatted XMR (not raw piconero) — the SDK formats it from the
|
||||
/// core's piconero string so the UI never parses atomic units.
|
||||
#[derive(Debug, Clone, uniffi::Record)]
|
||||
pub struct TransferRow {
|
||||
pub txid: String,
|
||||
pub amount: String,
|
||||
pub address: String,
|
||||
}
|
||||
|
||||
/// Convert a user-facing XMR amount string to a piconero string.
|
||||
///
|
||||
/// Accepts `"0.5"`, `"1"`, `"0.001"` etc. Returns the piconero string on
|
||||
/// success so the caller can pass it to the payment send methods. Fails on
|
||||
/// empty, non-numeric, zero, or out-of-range input.
|
||||
#[uniffi::export]
|
||||
pub fn xmr_to_atomic(amount_xmr: String) -> WResult<String> {
|
||||
pay::xmr_to_atomic(&amount_xmr).map_err(WireError::new)
|
||||
}
|
||||
|
||||
/// Format a piconero string as a human-readable XMR string.
|
||||
///
|
||||
/// The core stores amounts as piconero strings; this is what the wallet screen
|
||||
/// renders. `"0"` → `"0"`, `"1000000000000"` → `"1"`, `"150000000000"` → `"0.15"`.
|
||||
#[uniffi::export]
|
||||
pub fn atomic_to_xmr_str(amount_atomic: String) -> String {
|
||||
pay::atomic_to_xmr_str(&amount_atomic)
|
||||
}
|
||||
|
||||
/// Check whether a string is a valid Monero address.
|
||||
///
|
||||
/// Validates checksum and prefix byte. Used by the UI to reject typos before
|
||||
/// sending a payment. Both standard and integrated addresses are accepted.
|
||||
#[uniffi::export]
|
||||
pub fn check_address(address: String) -> WResult<()> {
|
||||
pay::check_address(&address).map_err(WireError::new)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
//! No Tor here on purpose: these exercise the *lifecycle* contract of the
|
||||
|
|
@ -668,7 +798,11 @@ mod tests {
|
|||
Some("sending")
|
||||
);
|
||||
assert_eq!(
|
||||
delivery_of("out", Some(OutboxStatus::Failed("no route".into()))).as_deref(),
|
||||
delivery_of(
|
||||
"out",
|
||||
Some(OutboxStatus::Failed("no route".into()))
|
||||
)
|
||||
.as_deref(),
|
||||
Some("failed: no route")
|
||||
);
|
||||
}
|
||||
|
|
@ -802,4 +936,61 @@ mod tests {
|
|||
|
||||
std::fs::remove_dir_all(&home).ok();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wallet_balance_on_stopped_handle_returns_off() {
|
||||
// A stopped/shut-down handle has no node, so the wallet is inaccessible.
|
||||
// The UI treats "off" as "wallet not available" — same as "not configured".
|
||||
assert_eq!(block_on(stopped_wire().wallet_balance()), "off");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wallet_receive_address_on_stopped_handle_is_stopped_error() {
|
||||
let err = block_on(stopped_wire().wallet_receive_address()).expect_err("must fail");
|
||||
assert!(matches!(err, WireError::Stopped { .. }), "got {err:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wallet_recent_transfers_on_stopped_handle_is_stopped_error() {
|
||||
let err =
|
||||
block_on(stopped_wire().wallet_recent_transfers(8)).expect_err("must fail");
|
||||
assert!(matches!(err, WireError::Stopped { .. }), "got {err:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xmr_to_atomic_rejects_zero() {
|
||||
assert!(xmr_to_atomic("0".to_string()).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xmr_to_atomic_accepts_one_xmr() {
|
||||
assert_eq!(xmr_to_atomic("1".to_string()).unwrap(), "1000000000000");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xmr_to_atomic_accepts_fractional_xmr() {
|
||||
assert_eq!(xmr_to_atomic("0.5".to_string()).unwrap(), "500000000000");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn atomic_to_xmr_str_formats_correctly() {
|
||||
assert_eq!(atomic_to_xmr_str("1000000000000".to_string()), "1");
|
||||
assert_eq!(atomic_to_xmr_str("500000000000".to_string()), "0.5");
|
||||
assert_eq!(atomic_to_xmr_str("0".to_string()), "0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_address_accepts_valid_address() {
|
||||
// Standard mainnet address from tests/pay.rs (same spend/view keys).
|
||||
assert!(check_address(
|
||||
"4AdUndXHHZ6cfufTMvppY6JwXNouMBzSkbLYfpAV5Usx3skxNgYeYTRj5UzqtReoS44qo9mtmXCqY45DJ852K5Jv2684Rge"
|
||||
.to_string()
|
||||
)
|
||||
.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_address_rejects_invalid_address() {
|
||||
assert!(check_address("not-an-address".to_string()).is_err());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue