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

@ -23,9 +23,15 @@ fn row(txid: &str, amount: &str, address: &str) -> TransferRow {
txid: txid.into(),
amount: amount.into(),
address: address.into(),
direction: "in".into(),
}
}
/// A 64-hex view key, the only shape `generate_from_keys` takes.
fn view_key() -> String {
"0123456789abcdef".repeat(4)
}
fn xmr_addr() -> String {
// Same documented mainnet standard as tests/pay.rs — F4 checksums this.
"4AdUndXHHZ6cfufTMvppY6JwXNouMBzSkbLYfpAV5Usx3skxNgYeYTRj5UzqtReoS44qo9mtmXCqY45DJ852K5Jv2684Rge"
@ -640,6 +646,7 @@ async fn restore_wallet_sends_seed_to_restore_deterministic_wallet() {
"hunter2",
"lumber tacit lemon ivory risk",
None,
None,
)
.await
.expect("restore_wallet");
@ -680,6 +687,7 @@ async fn restore_wallet_sends_a_non_empty_seed_offset() {
"hunter2",
"lumber tacit lemon ivory risk",
Some("harbor-offset"),
None,
)
.await
.expect("restore_wallet");
@ -721,6 +729,7 @@ async fn restore_wallet_omits_a_whitespace_only_seed_offset() {
"hunter2",
"lumber tacit lemon ivory risk",
Some(" "),
None,
)
.await
.expect("blank offset restore");
@ -755,6 +764,7 @@ async fn restore_wallet_trims_the_ends_of_a_seed_offset() {
"hunter2",
"lumber tacit lemon ivory risk",
Some(" harbor offset "),
None,
)
.await
.expect("trimmed offset restore");
@ -782,6 +792,7 @@ async fn a_failed_restore_does_not_echo_the_seed_offset() {
"hunter2",
"lumber tacit lemon ivory risk",
Some("harbor-offset"),
None,
)
.await
.unwrap_err()
@ -803,7 +814,7 @@ fn restore_wallet_empty_seed_is_local_error() {
.enable_all()
.build()
.unwrap()
.block_on(w.restore_wallet("onionwire", "pw", " ", None))
.block_on(w.restore_wallet("onionwire", "pw", " ", None, None))
.unwrap_err()
.to_string();
assert!(err.to_ascii_lowercase().contains("seed"), "got {err}");
@ -2040,3 +2051,269 @@ fn wallet_name_taken_fails_closed_when_the_dir_cannot_be_read() {
std::fs::write(&file, b"x").unwrap();
assert!(wallet_child::wallet_name_taken(&file, "onionwire-1").is_err());
}
// ---------------------------------------------------------------------------
// The next wallet batch, still at the request-byte level: there is no live
// `monero-wallet-rpc` on this host. Unlocked balance, restore height, outgoing
// rows and the view-only import all have to be pinned by the bytes they send.
// ---------------------------------------------------------------------------
/// `restore_height` is `restore_deterministic_wallet`'s own optional block
/// number: a JSON number when the user typed one, and no field at all when they
/// did not (never `0`, which would hide older funds).
#[tokio::test]
async fn restore_wallet_sends_a_restore_height_as_a_number() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let log = Arc::new(Mutex::new(Vec::new()));
tokio::spawn(serve_recording(
listener,
Arc::clone(&log),
Arc::new(|method| match method {
"restore_deterministic_wallet" => rpc_result(r#"{"address":"4","seed":""}"#),
_ => rpc_error(-1, "unexpected method"),
}),
));
recording_wallet(port)
.restore_wallet(
"onionwire-1",
"hunter2",
"lumber tacit lemon ivory risk",
None,
Some(3210000),
)
.await
.expect("restore_wallet");
let seen = log.lock().expect("log").clone();
let restore = seen
.iter()
.find(|r| r.contains(r#""method":"restore_deterministic_wallet""#))
.expect("restore RPC");
assert!(
restore.contains(r#""restore_height":3210000"#),
"the height must be a JSON number: {restore}"
);
assert!(
!restore.contains(r#""restore_height":"3210000""#),
"a quoted height is a string, not a block number: {restore}"
);
}
/// The default import sends no height at all. The seed offset still travels, so
/// the omission is the height's and not a lost field.
#[tokio::test]
async fn restore_wallet_omits_an_absent_restore_height() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let log = Arc::new(Mutex::new(Vec::new()));
tokio::spawn(serve_recording(
listener,
Arc::clone(&log),
Arc::new(|method| match method {
"restore_deterministic_wallet" => rpc_result(r#"{"address":"4","seed":""}"#),
_ => rpc_error(-1, "unexpected method"),
}),
));
recording_wallet(port)
.restore_wallet(
"onionwire-1",
"hunter2",
"lumber tacit lemon ivory risk",
Some("harbor-offset"),
None,
)
.await
.expect("restore_wallet");
let seen = log.lock().expect("log").clone();
let restore = seen
.iter()
.find(|r| r.contains(r#""method":"restore_deterministic_wallet""#))
.expect("restore RPC");
assert!(
!restore.contains("restore_height"),
"an absent height must not become a field: {restore}"
);
assert!(
restore.contains(r#""seed_offset":"harbor-offset""#),
"the offset is beside the height and must still be there: {restore}"
);
}
/// The wallet screen shows outgoing transfers too, so the request has to ask for
/// them and each row has to say which bucket it came from.
#[tokio::test]
async fn get_transfers_asks_for_outgoing_rows_and_labels_every_bucket() {
let addr = "8BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let log = Arc::new(Mutex::new(Vec::new()));
let body = format!(
r#"{{"in":[{{"txid":"aa","amount":1,"address":"{addr}"}}],"out":[{{"txid":"bb","amount":2,"address":"{addr}"}}],"pending":[{{"txid":"cc","amount":3,"address":"{addr}","type":"out"}}]}}"#
);
let reply: Arc<dyn Fn(&str) -> String + Send + Sync> = {
let body = body.clone();
Arc::new(move |method| match method {
"get_transfers" => rpc_result(&body),
_ => rpc_error(-1, "unexpected method"),
})
};
tokio::spawn(serve_recording(listener, Arc::clone(&log), reply));
let rows = recording_wallet(port)
.get_transfers()
.await
.expect("get_transfers");
let seen = log.lock().expect("log").clone();
let req = seen
.iter()
.find(|r| r.contains(r#""method":"get_transfers""#))
.expect("get_transfers RPC");
assert!(
req.contains(r#""out":true"#) || req.contains(r#""out": true"#),
"the request must ask for outgoing transfers: {req}"
);
let direction = |txid: &str| {
rows.iter()
.find(|r| r.txid == txid)
.map(|r| r.direction.clone())
};
assert_eq!(direction("aa").as_deref(), Some("in"));
assert_eq!(direction("bb").as_deref(), Some("out"));
assert_eq!(
direction("cc").as_deref(),
Some("out"),
"a pending item's own `type` is its direction"
);
}
/// Both numbers the wallet screen prints come out of one `get_balance` answer.
#[tokio::test]
async fn balances_reads_the_total_and_the_unlocked_from_one_call() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let log = Arc::new(Mutex::new(Vec::new()));
tokio::spawn(serve_recording(
listener,
Arc::clone(&log),
Arc::new(|method| match method {
"get_balance" => {
rpc_result(r#"{"balance":"900000000000","unlocked_balance":"450000000000"}"#)
}
_ => rpc_error(-1, "unexpected method"),
}),
));
let (total, unlocked) = recording_wallet(port).balances().await.expect("balances");
assert_eq!(total, "900000000000");
assert_eq!(unlocked, Some(450_000_000_000));
// The recorder sees the unauthenticated request that earns the 401 too, so
// count the answered ones: exactly one `get_balance` may reach the RPC.
let calls = log
.lock()
.expect("log")
.iter()
.filter(|r| r.contains(r#""method":"get_balance""#) && r.contains("Authorization:"))
.count();
assert_eq!(calls, 1, "one response carries both fields");
}
/// The view-only import is `generate_from_keys` with a view key and **no** spend
/// key: a body that carried `spendkey` would import a spend authority.
#[tokio::test]
async fn import_view_only_sends_the_view_key_and_no_spend_key() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let log = Arc::new(Mutex::new(Vec::new()));
tokio::spawn(serve_recording(
listener,
Arc::clone(&log),
Arc::new(|method| match method {
"generate_from_keys" => rpc_result(r#"{}"#),
_ => rpc_error(-1, "unexpected method"),
}),
));
let key = view_key();
recording_wallet(port)
.import_view_only("onionwire-1", "hunter2", &xmr_addr(), &key, Some(100))
.await
.expect("import_view_only");
let seen = log.lock().expect("log").clone();
let req = seen
.iter()
.find(|r| r.contains(r#""method":"generate_from_keys""#))
.expect("generate_from_keys RPC");
assert!(req.contains(&key), "view key missing: {req}");
assert!(req.contains(&xmr_addr()), "address missing: {req}");
assert!(
!req.contains("spendkey"),
"a view-only import must omit the spend key entirely: {req}"
);
assert!(
req.contains(r#""restore_height":100"#),
"the height is optional but not dropped when given: {req}"
);
}
/// A view key has exactly one shape and it is not a secret to be echoed back:
/// refusing locally also means no socket is opened.
#[tokio::test]
async fn import_view_only_refuses_a_bad_view_key_without_dialing() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let log = Arc::new(Mutex::new(Vec::new()));
tokio::spawn(serve_recording(
listener,
Arc::clone(&log),
Arc::new(|_: &str| rpc_error(-1, "unexpected method")),
));
let w = recording_wallet(port);
for bad in ["", " ", "not-hex", &"a".repeat(63), &"a".repeat(65)] {
let err = w
.import_view_only("onionwire-1", "hunter2", &xmr_addr(), bad, None)
.await
.unwrap_err()
.to_string();
assert_eq!(
err,
"view key must be 64 hex characters",
"a {}-char view key must be refused locally",
bad.len()
);
assert!(
!err.contains(bad) || bad.trim().is_empty(),
"the typed key must never be echoed: {err}"
);
}
assert!(
log.lock().expect("log").is_empty(),
"a local refusal must not dial"
);
}
/// The address is checked before the dial, like every other wallet path.
#[tokio::test]
async fn import_view_only_refuses_a_bad_address_without_dialing() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let log = Arc::new(Mutex::new(Vec::new()));
tokio::spawn(serve_recording(
listener,
Arc::clone(&log),
Arc::new(|_: &str| rpc_error(-1, "unexpected method")),
));
let err = recording_wallet(port)
.import_view_only(
"onionwire-1",
"hunter2",
"not-an-address",
&view_key(),
None,
)
.await
.unwrap_err()
.to_string();
assert_eq!(err, "invalid Monero address");
assert!(
log.lock().expect("log").is_empty(),
"a local refusal must not dial"
);
}