feat: Monero invoice/receipt frames, wallet RPC, /pay /tip
All checks were successful
ci / test (push) Successful in 2m50s
All checks were successful
ci / test (push) Successful in 2m50s
Signed inv/rcp codecs keyed by identity, payments table with unverified incoming receipts, optional JSON-RPC sidecar (loopback/.onion HTTP only), and TUI /pay /tip. OnionWire still holds no spend keys.
This commit is contained in:
parent
d304cc4c3b
commit
1a9b54d138
11 changed files with 1318 additions and 14 deletions
218
src/node.rs
218
src/node.rs
|
|
@ -13,10 +13,12 @@ use crate::dispatch::{self, Kind};
|
|||
use crate::frame;
|
||||
use crate::hs::{self, Client, HS_PORT};
|
||||
use crate::loc;
|
||||
use crate::pay;
|
||||
use crate::profile;
|
||||
use crate::qr;
|
||||
use crate::session::{self, Keys};
|
||||
use crate::store::{Friend, FriendProfile, Message, Store};
|
||||
use crate::store::{Friend, FriendProfile, Message, PaymentWrite, Store};
|
||||
use crate::wallet::{self, Wallet};
|
||||
|
||||
pub struct RotateResult {
|
||||
pub notified: usize,
|
||||
|
|
@ -41,6 +43,7 @@ pub struct Node {
|
|||
hs: Mutex<Option<HsHandle>>,
|
||||
onion: Mutex<String>,
|
||||
keys: Keys,
|
||||
wallet: Wallet,
|
||||
}
|
||||
|
||||
impl Node {
|
||||
|
|
@ -67,6 +70,7 @@ impl Node {
|
|||
hs: Mutex::new(None),
|
||||
onion: Mutex::new(onion),
|
||||
keys,
|
||||
wallet: Wallet::from_env(),
|
||||
});
|
||||
let rend = spawn_rend(Arc::clone(&node), rend);
|
||||
*node.hs.lock().map_err(|e| e.to_string())? = Some(HsHandle { _svc: svc, rend });
|
||||
|
|
@ -202,6 +206,119 @@ impl Node {
|
|||
self.try_send(&onion, friend_pk, &prekey, &pt).await
|
||||
}
|
||||
|
||||
/// Invoice to receive. Wallet subaddress if up, else self_profile.xmr_addr.
|
||||
pub async fn pay_invoice(
|
||||
&self,
|
||||
friend_pk: &[u8],
|
||||
atomic: &str,
|
||||
memo: &str,
|
||||
) -> Result<(), String> {
|
||||
let address = self.invoice_address().await?;
|
||||
let inv = pay::sign_invoice(&self.keys.identity_sk, atomic, &address, memo, unix_now())
|
||||
.map_err(|e| e.to_string())?;
|
||||
let pt = pay::encode_invoice(&inv);
|
||||
self.send_once(friend_pk, &pt).await?;
|
||||
let store = self.store.lock().map_err(|e| e.to_string())?;
|
||||
store
|
||||
.insert_payment(
|
||||
friend_pk,
|
||||
PaymentWrite {
|
||||
dir: "out",
|
||||
kind: "invoice",
|
||||
amount_atomic: atomic,
|
||||
address: &address,
|
||||
memo,
|
||||
txid: None,
|
||||
verified: false,
|
||||
},
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
store
|
||||
.append_message(
|
||||
friend_pk,
|
||||
"out",
|
||||
pay::invoice_chat_line(atomic, memo).as_bytes(),
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Pay selected friend via wallet RPC, then signed receipt.
|
||||
pub async fn tip(&self, friend_pk: &[u8], atomic: &str, _memo: &str) -> Result<(), String> {
|
||||
if !self.wallet.configured() {
|
||||
return Err("wallet not connected — set ONIONWIRE_WALLET_RPC".into());
|
||||
}
|
||||
let address = self
|
||||
.friend_profile(friend_pk)?
|
||||
.map(|p| p.xmr_addr)
|
||||
.unwrap_or_default();
|
||||
if address.is_empty() {
|
||||
return Err("friend has no Monero address — they must /profile".into());
|
||||
}
|
||||
pay::check_address(&address).map_err(|e| e.to_string())?;
|
||||
let amount: u64 = atomic
|
||||
.parse()
|
||||
.map_err(|_| "amount too large for wallet RPC".to_string())?;
|
||||
let txid = self
|
||||
.wallet
|
||||
.transfer(&address, amount)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rcp = pay::sign_receipt(&self.keys.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 store = self.store.lock().map_err(|e| e.to_string())?;
|
||||
store
|
||||
.insert_payment(
|
||||
friend_pk,
|
||||
PaymentWrite {
|
||||
dir: "out",
|
||||
kind: "receipt",
|
||||
amount_atomic: atomic,
|
||||
address: &address,
|
||||
memo: "",
|
||||
txid: Some(&txid),
|
||||
verified: true,
|
||||
},
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
store
|
||||
.append_message(
|
||||
friend_pk,
|
||||
"out",
|
||||
pay::receipt_chat_line(atomic, true).as_bytes(),
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn invoice_address(&self) -> Result<String, String> {
|
||||
if self.wallet.configured()
|
||||
&& let Ok(addr) = self.wallet.create_address().await
|
||||
&& pay::check_address(&addr).is_ok()
|
||||
{
|
||||
return Ok(addr);
|
||||
}
|
||||
let profile_addr = self.self_profile()?.xmr_addr;
|
||||
if profile_addr.is_empty() {
|
||||
return Err("no receive address — set profile xmr or ONIONWIRE_WALLET_RPC".into());
|
||||
}
|
||||
pay::check_address(&profile_addr).map_err(|e| e.to_string())?;
|
||||
Ok(profile_addr)
|
||||
}
|
||||
|
||||
async fn send_once(&self, friend_pk: &[u8], plaintext: &[u8]) -> Result<(), String> {
|
||||
let (onion, prekey) = {
|
||||
let f = self.friend(friend_pk)?;
|
||||
if f.prekey.len() != 32 {
|
||||
return Err("friend missing prekey".into());
|
||||
}
|
||||
(f.onion, f.prekey)
|
||||
};
|
||||
self.try_send(&onion, friend_pk, &prekey, plaintext).await
|
||||
}
|
||||
|
||||
pub async fn connect_onion(&self, onion: &str) -> Result<(), String> {
|
||||
tokio::time::timeout(
|
||||
Duration::from_secs(20),
|
||||
|
|
@ -404,9 +521,106 @@ impl Node {
|
|||
.map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
Kind::Invoice | Kind::Receipt | Kind::Ping | Kind::Drop => Ok(()),
|
||||
Kind::Invoice => self.ingest_invoice(&sess.peer_identity, &pt).await,
|
||||
Kind::Receipt => self.ingest_receipt(&sess.peer_identity, &pt).await,
|
||||
Kind::Ping | Kind::Drop => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn ingest_invoice(&self, peer: &[u8], pt: &[u8]) -> Result<(), String> {
|
||||
let Some(inv) = pay::decode_invoice(pt) else {
|
||||
return Ok(());
|
||||
};
|
||||
if !pay::verify_invoice(peer, &inv) {
|
||||
eprintln!("inv dropped (bad sig)");
|
||||
return Ok(());
|
||||
}
|
||||
let store = self.store.lock().map_err(|e| e.to_string())?;
|
||||
store
|
||||
.insert_payment(
|
||||
peer,
|
||||
PaymentWrite {
|
||||
dir: "in",
|
||||
kind: "invoice",
|
||||
amount_atomic: &inv.amount_atomic,
|
||||
address: &inv.address,
|
||||
memo: &inv.memo,
|
||||
txid: None,
|
||||
verified: false,
|
||||
},
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
store
|
||||
.append_message(
|
||||
peer,
|
||||
"in",
|
||||
pay::invoice_chat_line(&inv.amount_atomic, &inv.memo).as_bytes(),
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn ingest_receipt(&self, peer: &[u8], pt: &[u8]) -> Result<(), String> {
|
||||
let Some(rcp) = pay::decode_receipt(pt) else {
|
||||
return Ok(());
|
||||
};
|
||||
if !pay::verify_receipt(peer, &rcp) {
|
||||
eprintln!("rcp dropped (bad sig)");
|
||||
return Ok(());
|
||||
}
|
||||
let id = {
|
||||
let store = self.store.lock().map_err(|e| e.to_string())?;
|
||||
store
|
||||
.insert_payment(
|
||||
peer,
|
||||
PaymentWrite {
|
||||
dir: "in",
|
||||
kind: "receipt",
|
||||
amount_atomic: &rcp.amount_atomic,
|
||||
address: &rcp.address,
|
||||
memo: "",
|
||||
txid: Some(&rcp.txid),
|
||||
verified: false,
|
||||
},
|
||||
)
|
||||
.map_err(|e| e.to_string())?
|
||||
};
|
||||
let mut verified = false;
|
||||
if self.wallet.configured() {
|
||||
match self.wallet.get_transfers().await {
|
||||
Ok(rows)
|
||||
if wallet::transfers_match(
|
||||
&rows,
|
||||
&rcp.txid,
|
||||
&rcp.amount_atomic,
|
||||
&rcp.address,
|
||||
) =>
|
||||
{
|
||||
self.store
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?
|
||||
.mark_verified(id)
|
||||
.map_err(|e| e.to_string())?;
|
||||
verified = true;
|
||||
}
|
||||
Ok(_) => {}
|
||||
Err(e) => eprintln!("receipt unverified: {e}"),
|
||||
}
|
||||
}
|
||||
if !verified {
|
||||
eprintln!("receipt unverified");
|
||||
}
|
||||
self.store
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?
|
||||
.append_message(
|
||||
peer,
|
||||
"in",
|
||||
pay::receipt_chat_line(&rcp.amount_atomic, verified).as_bytes(),
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_rend(
|
||||
|
|
|
|||
Loading…
Reference in a new issue