node: read-only Node::list_payments(friend_pk) for the TUI payment history #78

Merged
sirius merged 1 commit from wt/t_6fb6d365 into feat/wallet-create-import 2026-09-15 19:27:23 +00:00

View file

@ -25,7 +25,7 @@ use crate::qr;
use crate::ratelimit::TokenBucket;
use crate::sanitize;
use crate::session::{self, Keys};
use crate::store::{Friend, FriendProfile, Message, PaymentWrite, Store};
use crate::store::{Friend, FriendProfile, Message, Payment, PaymentWrite, Store};
use crate::wallet::{self, ChainSource, Readiness, Wallet, WalletConfig, WalletStatus};
use crate::wallet_child::{self, WalletChild};
@ -514,6 +514,17 @@ impl Node {
.map_err(|e| e.to_string())
}
/// Every payment row with this friend, in the store's existing order.
/// Read-only: the TUI payment-history screen reads this and never touches
/// the store itself. An unknown friend is empty history, not an error.
pub fn list_payments(&self, friend_pk: &[u8]) -> Result<Vec<Payment>, String> {
self.store
.lock()
.map_err(|e| e.to_string())?
.list_payments(friend_pk)
.map_err(|e| e.to_string())
}
/// The live wallet, cloned out of its lock. Every wallet call goes through
/// this so a `std::sync::Mutex` guard never lives across an `.await`, and so
/// the F7 screen can re-point the wallet mid-session.
@ -1702,4 +1713,116 @@ mod tests {
.expect_err("a closed port must not answer");
assert!(err.contains(&addr), "{err}");
}
// Official mainnet standard address (Monero docs), the same constant the
// integration tests use. `insert_payment` checks the checksum.
const ADDR: &str = "4AdUndXHHZ6cfufTMvppY6JwXNouMBzSkbLYfpAV5Usx3skxNgYeYTRj5UzqtReoS44qo9mtmXCqY45DJ852K5Jv2684Rge";
/// Never dials. A store-backed read must not touch it.
struct NullDeliverer;
impl crate::outbox::Deliverer for NullDeliverer {
fn deliver<'a>(
&'a self,
_onion: &'a str,
_pinned_id: &'a [u8],
_prekey: &'a [u8],
_plaintext: &'a [u8],
) -> crate::outbox::BoxFut<'a, Result<(), String>> {
Box::pin(async { Err("no tor in this test".into()) })
}
}
/// A `Node` over a real encrypted store with an *unbootstrapped* Arti
/// client: no Tor, no onion service, no outbox worker. Store-backed reads
/// (`list_payments`) work; a send would not. Cheap enough for a plain
/// `#[cfg(test)]` test, which is the only way to reach a `Node` without
/// bootstrapping Tor (`Node::start*` is the sole public constructor).
fn offline_node(home: &Path, store: Store) -> Node {
let cfg = hs::client_config(&home.join("arti"), &home.join("cache"));
let client = arti_client::TorClient::builder()
.config(cfg)
.create_unbootstrapped()
.expect("unbootstrapped arti client");
let store = Arc::new(Mutex::new(store));
let outbox = Arc::new(Outbox::new(Arc::clone(&store), Arc::new(NullDeliverer)));
Node {
home: home.to_path_buf(),
store,
client,
hs: Mutex::new(None),
onion: Mutex::new(String::new()),
keys: Mutex::new(Keys::generate()),
wallet: Mutex::new(Wallet::disabled()),
wallet_status: Mutex::new(WalletStatus::idle(false)),
child: Mutex::new(None),
incoming_limit: Mutex::new(TokenBucket::default()),
inbox: Mutex::new(file::Inbox::new(home.join("inbox"))),
outbox,
worker: Mutex::new(None),
stopped: AtomicBool::new(false),
onion_reachable: AtomicBool::new(false),
}
}
/// The payment-history screen reads through this: every row for the friend
/// with its dir/kind/amount/verification intact, and an empty history (not
/// an error) for a friend with no rows or an unknown pubkey.
#[tokio::test]
async fn list_payments_returns_the_friends_rows_and_empty_for_an_unknown_friend() {
let dir = tempfile::tempdir().expect("tempdir");
let home = dir.path().join("home");
std::fs::create_dir_all(&home).expect("home");
let store = Store::open_at_with_passphrase(&home, "onionwire-test").expect("open store");
let keys = Keys::generate();
let peer = keys.identity_pk;
store
.upsert_friend(&peer, "peer.onion", None)
.expect("friend");
// Outbound, the row shape `pay_invoice` writes.
let out_id = store
.insert_payment(
&peer,
PaymentWrite {
dir: "out",
kind: "invoice",
amount_atomic: "120000000000",
address: ADDR,
memo: "coffee",
txid: None,
verified: false,
},
)
.expect("outbound row");
let node = offline_node(&home, store);
// Inbound through the node's own ingest path, signing as the peer.
let rcp = pay::sign_receipt(&keys.identity_sk, "deadbeef", "5", ADDR, unix_now())
.expect("sign receipt");
node.ingest_receipt(&peer, &pay::encode_receipt(&rcp))
.await
.expect("ingest receipt");
let rows = node.list_payments(&peer).expect("list_payments");
assert_eq!(rows.len(), 2, "{rows:?}");
assert_eq!(rows[0].id, out_id);
assert_eq!(rows[0].dir, "out");
assert_eq!(rows[0].kind, "invoice");
assert_eq!(rows[0].amount_atomic, "120000000000");
assert_eq!(rows[0].address, ADDR);
assert_eq!(rows[0].memo, "coffee");
assert_eq!(rows[0].txid, None);
assert!(!rows[0].verified);
assert!(rows[0].created_at > 0);
assert_eq!(rows[1].dir, "in");
assert_eq!(rows[1].kind, "receipt");
assert_eq!(rows[1].amount_atomic, "5");
assert_eq!(rows[1].address, ADDR);
assert_eq!(rows[1].txid.as_deref(), Some("deadbeef"));
// No wallet is configured here, so nothing verified the receipt.
assert!(!rows[1].verified);
// Unknown pubkey: empty history, not an error.
let none = node.list_payments(&[9u8; 32]).expect("unknown friend");
assert!(none.is_empty(), "{none:?}");
}
}