wallet: unlocked balance, restore height, outgoing rows, send, view-only import
All checks were successful
ci / test (pull_request) Successful in 6m39s
ci / sdk (pull_request) Successful in 1m31s
android-ci / android (pull_request) Successful in 12m57s
ci / fuzz (pull_request) Successful in 5m3s

`get_balance` carries two numbers and the wallet screen only showed one:
`Node::wallet_balance_line` now reads both out of a single answer
(`balance {total} XMR · unlocked {unlocked} XMR`, the `balance 0 XMR` prefix
kept, total-only when the RPC reports no unlocked half), and
`Node::wallet_unlocked_balance` reports the unlocked half on its own with
`wallet_balance`'s `off` / `err` sentinels. `wallet_balance` keeps its shape.

`get_transfers` sends `out: true` and parses the `out` bucket, so `TransferRow`
carries `direction` (`in`, `out`, `pending`; a pending item's own `type` wins)
and the screen can tell a spent transfer from a received one.

`Wallet::restore_wallet` / `Node::wallet_restore` take an optional
`restore_height` beside the seed offset: blank omits the field, a typed `0` is
sent as `0`, `Some(n)` is a JSON number, and a non-numeric height is a local
refusal (`restore height must be a block number`). A height after the first
receive hides the older funds and this version has no rescan — the TUI says so
on its own step, whose footer is not the offset's.

`Node::wallet_send` spends to a pasted address: `check_address`, then
`xmr_to_atomic`, then `Wallet::transfer` — no friend, no `tip` frame, no chat
line, no payment row. The outgoing transfer list is the record. `pay::check_address`
and `Wallet::transfer` own the gates, so a view-only or unsynced refusal is the
string they already return.

`Wallet::import_view_only` / `Node::wallet_import_view_only` watch an address
through `generate_from_keys` with no `spendkey` in the request at all: same
filename/password refusals, `check_address`, a 64-hex view key or a local
refusal, no 64th catalogue column. The view key is typed into a hidden entry
(twice), is never sealed, never logged and never on an argv, and the screen
wipes it on every exit.

TUI: `n` sends (address, amount, then `SEND`; Esc spends nothing), `y` copies
the receive address over OSC 52, `v` imports a view-only wallet (no seed, so no
`SEEN` gate), and the seed import gained a `restore height` step between the
offset and the file passphrase. The wallet footer and its help line carry the
new keys and keep every old token. The F6 body renders in the flexible region —
a bordered block in the two-row slot it used drew its borders and clipped the
whole body, address and balance line included.

SDK: `wallet_unlocked_balance`, `wallet_send`, `wallet_view_only`,
`import_seed(password, seed, offset, height)` and
`import_view_only(password, address, view_key, height)`, plus
`TransferRow.direction`. `wallet_balance`, `import_wallet` and
`import_wallet_with_offset` are untouched.

Docs: WALLET (unlocked vs total, restore height, outgoing rows, local send,
view-only import), THREAT_MODEL (a view key entered at import is a secret and a
view-only file is not a spend authority), TUI (footer + the new keys and steps),
README (one sentence).

Tests: recorded-request cases in `tests/wallet.rs` (height as a number and
omitted when blank, `generate_from_keys` carries the view key and no spendkey,
`get_transfers` asks for `out`, `wallet_send` reaches `transfer` and never
`tip`, a bad view key never dials), node-level line/unlocked pairs from one
dial, and TUI step tests for the send, copy, height and view-only flows.

No new crate, no Cargo change, no android/ change.
This commit is contained in:
agent 2026-09-25 02:23:09 -04:00
commit 93deaa5309
No known key found for this signature in database
9 changed files with 1891 additions and 58 deletions

View file

@ -498,7 +498,10 @@ impl Wire {
/// 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.
/// `"err"` when the wallet RPC call failed, and the amount in XMR as a bare
/// number on success (the core's `atomic_to_xmr_str`, e.g. `"0.5"`) — the
/// sentinels are what a caller branches on, so the number is not wrapped in
/// any label here.
/// 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.
@ -509,6 +512,59 @@ impl Wire {
}
}
/// The spendable part of the balance, as a display string with exactly the
/// same sentinels as [`Wire::wallet_balance`]: `"off"` when the wallet is not
/// configured or the node is gone, `"err"` when the RPC did not answer with
/// a number, else the bare XMR number. The rest of the balance is either
/// locked (unconfirmed income) or immature (recent coinbase), so a UI shows
/// both: funds that exist, and funds that can move now.
pub async fn wallet_unlocked_balance(&self) -> String {
match self.node() {
Ok(node) => node.wallet_unlocked_balance().await,
Err(_) => "off".to_string(),
}
}
/// Is the wallet file the child has open **watch-only** (imported from an
/// address and a view key, so it holds no spend key)?
///
/// Proven over RPC: `query_key` for the spend key is refused with monero's
/// `WALLET_RPC_ERROR_CODE_WATCH_ONLY` (`-29`) exactly on a wallet that
/// cannot spend; a spend key that does come back is discarded inside the
/// node, never returned and never logged. An unconfigured wallet — or a
/// stopped node — is an error, **not** `true`: claiming a watch-only wallet
/// exists when there is none would be a lie. `WireError.Failed` carries the
/// core's own not-configured message, `WireError.Stopped` a shut-down node.
///
/// The Kotlin binding is `Wire.walletViewOnly()`.
pub async fn wallet_view_only(&self) -> WResult<bool> {
self.node()?
.wallet_view_only()
.await
.map_err(WireError::new)
}
/// Send XMR to a pasted address. Returns the transaction hash.
///
/// A **local** transfer on this node's own wallet: no friend, no `tip`
/// frame, no chat line and no payment row — the wallet's outgoing transfer
/// list is the record. `address` and `amount_xmr` are checked here first, so
/// an empty field, an invalid address or an unparsable amount fails without
/// touching the RPC (`WireError.Failed` with the core's own message).
///
/// The spend gates are the core's and are surfaced as they are: a wallet
/// configured `ONIONWIRE_WALLET_NO_SPEND=1` or a watch-only file is refused
/// with the view-only message, and a wallet that has not reached the chain
/// tip is refused rather than allowed to fail opaquely.
///
/// The Kotlin binding is `Wire.walletSend(address, amountXmr)`.
pub async fn wallet_send(&self, address: String, amount_xmr: String) -> WResult<String> {
self.node()?
.wallet_send(&address, &amount_xmr)
.await
.map_err(WireError::new)
}
/// Receive address for the wallet screen: a freshly created subaddress when
/// the wallet is configured, else the profile `xmr_addr` fallback.
///
@ -536,6 +592,7 @@ impl Wire {
txid: r.txid,
amount: pay::atomic_to_xmr_str(&r.amount),
address: r.address,
direction: r.direction,
})
.collect())
}
@ -770,7 +827,7 @@ impl Wire {
/// seed-offset passphrase.
pub async fn import_wallet(&self, password: String, seed: String) -> WResult<()> {
self.node()?
.wallet_restore(&password, &seed, None)
.wallet_restore(&password, &seed, None, None)
.await
.map_err(WireError::new)
.map(|_| ())
@ -800,7 +857,78 @@ impl Wire {
Some(offset)
};
self.node()?
.wallet_restore(&password, &seed, offset)
.wallet_restore(&password, &seed, offset, None)
.await
.map_err(WireError::new)
.map(|_| ())
}
/// Import a wallet from an existing 25-word seed, with the two optional
/// inputs the core takes beside it: Monero's seed-offset passphrase and a
/// restore height.
///
/// This is the call a first-run import screen makes: it supersedes
/// [`Wire::import_wallet`] and [`Wire::import_wallet_with_offset`] without
/// changing either of them, so an existing caller keeps compiling.
///
/// `seed_offset` is Monero's second spend secret (not the file passphrase):
/// blank or whitespace means "no offset" and the request carries no
/// `seed_offset` field. `restore_height` is a block number as typed:
/// blank means "scan from 0" and the request carries no `restore_height`
/// field either — never a `0`. Anything else that is not a block number is
/// refused locally with `restore height must be a block number`.
///
/// A height *after* the first receive hides the older funds, and this
/// version has no rescan to undo that: the UI has to say so, not fix it.
pub async fn import_seed(
&self,
password: String,
seed: String,
seed_offset: String,
restore_height: String,
) -> WResult<()> {
let offset = seed_offset.trim();
let offset = if offset.is_empty() {
None
} else {
Some(offset)
};
let height = restore_height_arg(&restore_height)?;
self.node()?
.wallet_restore(&password, &seed, offset, height)
.await
.map_err(WireError::new)
.map(|_| ())
}
/// Import an address to **watch**: `generate_from_keys` with the address and
/// its private view key, and no spend key at all. The wallet file this
/// creates can see incoming transfers and cannot spend — it is not a spend
/// authority, and `Wire::walletSend` on it is refused with the view-only
/// message.
///
/// `view_key` is trimmed and must be 64 hex characters; the request never
/// carries a `spendkey` field, and the key is neither stored nor sealed
/// (`password` is the wallet *file* passphrase and is what gets sealed, so
/// the file reopens on the next start). A wrong-shape key is refused locally
/// with `view key must be 64 hex characters` and the typed key is never part
/// of that message.
///
/// `restore_height` behaves exactly as in [`Wire::import_seed`]: blank omits
/// the field, and a non-numeric value is the same local refusal.
///
/// The Kotlin binding is `Wire.importViewOnly(password, address, viewKey,
/// restoreHeight)`.
pub async fn import_view_only(
&self,
password: String,
address: String,
view_key: String,
restore_height: String,
) -> WResult<()> {
let height = restore_height_arg(&restore_height)?;
self.node()?
.wallet_import_view_only(&password, &address, &view_key, height)
.await
.map_err(WireError::new)
.map(|_| ())
@ -1247,6 +1375,11 @@ pub struct TransferRow {
pub txid: String,
pub amount: String,
pub address: String,
/// Which bucket the row came from: `in`, `out`, or `pending` — a pending
/// item that names its own `type` carries that instead. A client renders the
/// sign of the amount from this, and must not treat it as a dedupe key: a
/// txid may appear in two buckets and both rows are real.
pub direction: String,
}
/// One row of the saved-wallet catalogue, for the wallet picker.
@ -1602,6 +1735,23 @@ pub fn verify_tip(pubkey_hex: String, tip: Tip) -> bool {
}
}
/// A typed restore height as the core's `Option<u64>`.
///
/// Blank (or whitespace) is `None` — "scan from the beginning", and the request
/// carries no `restore_height` field at all. A typed `0` is `Some(0)` and *is*
/// sent: the user asked for it. Anything that is not a block number is refused
/// here, before any dial; the message says what a block number is and never
/// echoes the typed text back.
fn restore_height_arg(text: &str) -> WResult<Option<u64>> {
let text = text.trim();
if text.is_empty() {
return Ok(None);
}
text.parse::<u64>()
.map(Some)
.map_err(|_| WireError::new("restore height must be a block number"))
}
/// Convert a user-facing XMR amount string to a piconero string.
///
/// Accepts `"0.5"`, `"1"`, `"0.001"` etc. Returns the piconero string on
@ -1877,6 +2027,60 @@ mod tests {
assert_eq!(block_on(stopped_wire().wallet_balance()), "off");
}
#[test]
fn wallet_unlocked_balance_on_stopped_handle_returns_off() {
assert_eq!(block_on(stopped_wire().wallet_unlocked_balance()), "off");
}
#[test]
fn wallet_send_on_stopped_handle_is_stopped_error() {
let err = block_on(stopped_wire().wallet_send(
"4AdUndXHHZ6cfufTMvppY6JwXNouMBzSkbLYfpAV5Usx3skxNgYeYTRj5UzqtReoS44qo9mtmXCqY45DJ852K5Jv2684Rge"
.to_string(),
"1".to_string(),
))
.expect_err("must fail");
assert!(matches!(err, WireError::Stopped { .. }), "got {err:?}");
}
#[test]
fn wallet_view_only_on_stopped_handle_is_stopped_error() {
let err = block_on(stopped_wire().wallet_view_only()).expect_err("must fail");
assert!(matches!(err, WireError::Stopped { .. }), "got {err:?}");
}
/// A blank height is "no field", a typed number is that number, and a typo
/// is a local refusal — never a silent `0`.
#[test]
fn restore_height_arg_blanks_omits_and_refuses_a_typo() {
assert_eq!(restore_height_arg("").unwrap(), None);
assert_eq!(restore_height_arg(" ").unwrap(), None);
assert_eq!(restore_height_arg("3210000").unwrap(), Some(3_210_000));
assert_eq!(restore_height_arg(" 42 ").unwrap(), Some(42));
assert_eq!(
restore_height_arg("0").unwrap(),
Some(0),
"a typed 0 is what the user asked for, not an omission"
);
let err = restore_height_arg("block 42").expect_err("not a block number");
assert_eq!(err.to_string(), "restore height must be a block number");
}
/// The import refuses a bad height before it ever reaches the node, so the
/// message is the SDK's own and no dial is implied.
#[test]
fn import_seed_refuses_a_non_numeric_height_locally() {
let err = block_on(stopped_wire().import_seed(
"pw".to_string(),
"lumber tacit lemon ivory risk".to_string(),
String::new(),
"not-a-height".to_string(),
))
.expect_err("must fail");
assert_eq!(err.to_string(), "restore height must be a block number");
assert!(matches!(err, WireError::Failed { .. }), "got {err:?}");
}
#[test]
fn wallet_receive_address_on_stopped_handle_is_stopped_error() {
let err = block_on(stopped_wire().wallet_receive_address()).expect_err("must fail");