feat: tip frame (sign/verify/encode/decode), wallet balance + receive address, F6 wallet screen with QR, dispatch Kind::Tip
Some checks failed
ci / test (push) Failing after 9m39s
ci / sdk (push) Failing after 20s

This commit is contained in:
agent 2026-09-12 22:53:58 -04:00
commit 10d81964a5
No known key found for this signature in database
10 changed files with 511 additions and 24 deletions

View file

@ -447,6 +447,19 @@ impl Node {
.map_err(|e| e.to_string())
}
/// Current wallet balance as a human-readable XMR string, or `off` when the
/// wallet is not configured.
pub async fn wallet_balance(&self) -> String {
if !self.wallet.configured() {
return "off".into();
}
match self.wallet.get_balance().await {
Ok(b) => crate::pay::atomic_to_xmr_str(&b),
Err(_) => "err".into(),
}
}
pub fn wipe_messages(&self) -> Result<(), String> {
self.store
.lock()
@ -569,8 +582,12 @@ impl Node {
let k = self.keys()?;
let rcp = pay::sign_receipt(&k.identity_sk, &txid, atomic, &address, unix_now())
.map_err(|e| e.to_string())?;
let pt = pay::encode_receipt(&rcp);
self.send_once(friend_pk, &pt).await?;
let rcp_pt = pay::encode_receipt(&rcp);
let tip = pay::sign_tip(&k.identity_sk, &txid, atomic, &address, _memo, unix_now())
.map_err(|e| e.to_string())?;
let tip_pt = pay::encode_tip(&tip);
self.send_once(friend_pk, &rcp_pt).await?;
self.send_once(friend_pk, &tip_pt).await?;
let store = self.store.lock().map_err(|e| e.to_string())?;
store
.insert_payment(
@ -611,6 +628,63 @@ impl Node {
Ok(profile_addr)
}
/// 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.
pub async fn wallet_receive_address(&self) -> Result<String, String> {
let profile_addr = self.self_profile()?.xmr_addr;
if self
.wallet
.configured()
&& let Ok(addr) = self.wallet.get_address().await
&& pay::check_address(&addr).is_ok()
{
return Ok(addr);
}
if !profile_addr.is_empty() {
pay::check_address(&profile_addr).map_err(|e| e.to_string())?;
return Ok(profile_addr);
}
Err("no receive address — set profile xmr or ONIONWIRE_WALLET_RPC".into())
}
/// Formatted balance for the wallet screen, or "off" when the wallet is not
/// configured. Calls `pay::atomic_to_xmr_str` so the UI never parses
/// piconero itself.
pub async fn wallet_balance_line(&self) -> String {
if self.wallet.configured() {
match self.wallet.get_balance().await {
Ok(b) if !b.is_empty() => {
let xmr = pay::atomic_to_xmr_str(&b);
if xmr == "0" {
"balance 0 XMR".to_string()
} else {
format!("balance {xmr} XMR")
}
}
Ok(_) => "balance —".to_string(),
Err(_) => "balance —".to_string(),
}
} else {
"wallet off".to_string()
}
}
/// Recent in/out transfers for the wallet screen, newest first, capped to a
/// small window. Returns the same `TransferRow` shape the existing tests
/// already cover.
pub async fn wallet_recent_transfers(&self, cap: usize) -> Result<Vec<wallet::TransferRow>, String> {
if !self.wallet.configured() {
return Ok(Vec::new());
}
let rows = self.wallet.get_transfers().await.map_err(|e| e.to_string())?;
let mut rows = rows;
rows.sort_by(|a, b| b.txid.cmp(&a.txid));
Ok(rows.into_iter().take(cap).collect())
}
async fn send_once(&self, friend_pk: &[u8], plaintext: &[u8]) -> Result<(), String> {
self.ensure_running()?;
let (onion, prekey) = {
@ -860,6 +934,7 @@ impl Node {
}
Kind::Invoice => self.ingest_invoice(&sess.peer_identity, &pt).await,
Kind::Receipt => self.ingest_receipt(&sess.peer_identity, &pt).await,
Kind::Tip => self.ingest_tip(&sess.peer_identity, &pt).await,
Kind::File => self.ingest_file(&sess.peer_identity, &pt),
Kind::Ping | Kind::Drop => Ok(()),
}
@ -993,6 +1068,42 @@ impl Node {
.map_err(|e| e.to_string())?;
Ok(())
}
async fn ingest_tip(&self, peer: &[u8], pt: &[u8]) -> Result<(), String> {
let Some(tip) = pay::decode_tip(pt) else {
return Ok(());
};
if !pay::verify_tip(peer, &tip) {
eprintln!("tip dropped (bad sig)");
return Ok(());
}
self.store
.lock()
.map_err(|e| e.to_string())?
.insert_payment(
peer,
PaymentWrite {
dir: "in",
kind: "tip",
amount_atomic: &tip.amount_atomic,
address: &tip.address,
memo: &tip.memo,
txid: Some(&tip.txid),
verified: false,
},
)
.map_err(|e| e.to_string())?;
self.store
.lock()
.map_err(|e| e.to_string())?
.append_message(
peer,
"in",
pay::tip_chat_line(&tip.amount_atomic, &tip.memo, false).as_bytes(),
)
.map_err(|e| e.to_string())?;
Ok(())
}
}
fn spawn_rend(