feat: tip frame (sign/verify/encode/decode), wallet balance + receive address, F6 wallet screen with QR, dispatch Kind::Tip
This commit is contained in:
parent
7b147d8ebe
commit
10d81964a5
10 changed files with 511 additions and 24 deletions
123
src/pay.rs
123
src/pay.rs
|
|
@ -35,6 +35,7 @@ pub struct Receipt {
|
|||
|
||||
const INV_PREFIX: &[u8] = b"inv ";
|
||||
const RCP_PREFIX: &[u8] = b"rcp ";
|
||||
const TIP_PREFIX: &[u8] = b"tip ";
|
||||
const PICONERO: u128 = 1_000_000_000_000;
|
||||
|
||||
/// Bitcoin-style alphabet; Monero encodes 8-byte blocks (11 chars), not raw base58.
|
||||
|
|
@ -314,6 +315,128 @@ pub fn decode_receipt(pt: &[u8]) -> Option<Receipt> {
|
|||
})
|
||||
}
|
||||
|
||||
// ---- tip (scope C: both rcp + tip frames are sent/received) ----
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Tip {
|
||||
pub txid: String,
|
||||
pub amount_atomic: String,
|
||||
pub address: String,
|
||||
pub memo: String,
|
||||
pub ts: i64,
|
||||
pub sig: Vec<u8>,
|
||||
}
|
||||
|
||||
fn check_tip_fields(txid: &str, amount_atomic: &str, address: &str, memo: &str) -> Result<()> {
|
||||
if txid.is_empty() || txid.contains('\n') {
|
||||
return Err(Error("invalid txid".into()));
|
||||
}
|
||||
parse_atomic(amount_atomic)?;
|
||||
check_address(address)?;
|
||||
if memo.contains('\n') {
|
||||
return Err(Error("memo must not contain newlines".into()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn tip_sign_msg_v1(txid: &str, amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec<u8> {
|
||||
let mut msg = Vec::from(txid.as_bytes());
|
||||
msg.push(b'\n');
|
||||
msg.extend_from_slice(amount_atomic.as_bytes());
|
||||
msg.push(b'\n');
|
||||
msg.extend_from_slice(address.as_bytes());
|
||||
msg.push(b'\n');
|
||||
msg.extend_from_slice(memo.as_bytes());
|
||||
msg.push(b'\n');
|
||||
msg.extend_from_slice(ts.to_string().as_bytes());
|
||||
msg
|
||||
}
|
||||
|
||||
pub fn sign_tip(
|
||||
identity_sk: &[u8],
|
||||
txid: &str,
|
||||
amount_atomic: &str,
|
||||
address: &str,
|
||||
memo: &str,
|
||||
ts: i64,
|
||||
) -> Result<Tip> {
|
||||
check_tip_fields(txid, amount_atomic, address, memo)?;
|
||||
let sig = sign_bytes(identity_sk, &tip_sign_msg_v1(txid, amount_atomic, address, memo, ts))?;
|
||||
Ok(Tip {
|
||||
txid: txid.to_string(),
|
||||
amount_atomic: amount_atomic.to_string(),
|
||||
address: address.to_string(),
|
||||
memo: memo.to_string(),
|
||||
ts,
|
||||
sig,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn verify_tip(identity_pk: &[u8], tip: &Tip) -> bool {
|
||||
if check_tip_fields(&tip.txid, &tip.amount_atomic, &tip.address, &tip.memo).is_err() {
|
||||
return false;
|
||||
}
|
||||
verify_bytes(
|
||||
identity_pk,
|
||||
&tip_sign_msg_v1(&tip.txid, &tip.amount_atomic, &tip.address, &tip.memo, tip.ts),
|
||||
&tip.sig,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn encode_tip(tip: &Tip) -> Vec<u8> {
|
||||
let mut out = Vec::from(TIP_PREFIX);
|
||||
out.extend_from_slice(tip.txid.as_bytes());
|
||||
out.push(b'\n');
|
||||
out.extend_from_slice(tip.amount_atomic.as_bytes());
|
||||
out.push(b'\n');
|
||||
out.extend_from_slice(tip.address.as_bytes());
|
||||
out.push(b'\n');
|
||||
out.extend_from_slice(tip.memo.as_bytes());
|
||||
out.push(b'\n');
|
||||
out.extend_from_slice(tip.ts.to_string().as_bytes());
|
||||
out.push(b'\n');
|
||||
out.extend_from_slice(to_hex(&tip.sig).as_bytes());
|
||||
out
|
||||
}
|
||||
|
||||
pub fn decode_tip(pt: &[u8]) -> Option<Tip> {
|
||||
let rest = pt.strip_prefix(TIP_PREFIX)?;
|
||||
let text = std::str::from_utf8(rest).ok()?;
|
||||
let mut parts = text.splitn(6, '\n');
|
||||
let txid = parts.next()?.to_string();
|
||||
let amount_atomic = parts.next()?.to_string();
|
||||
let address = parts.next()?.to_string();
|
||||
let memo = parts.next()?.to_string();
|
||||
let ts = parts.next()?.parse::<i64>().ok()?;
|
||||
let sig = from_hex(parts.next()?)?;
|
||||
if check_tip_fields(&txid, &amount_atomic, &address, &memo).is_err() {
|
||||
return None;
|
||||
}
|
||||
Some(Tip {
|
||||
txid,
|
||||
amount_atomic,
|
||||
address,
|
||||
memo,
|
||||
ts,
|
||||
sig,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn tip_chat_line(amount_atomic: &str, memo: &str, verified: bool) -> String {
|
||||
let xmr = atomic_to_xmr_str(amount_atomic);
|
||||
if memo.is_empty() {
|
||||
if verified {
|
||||
format!("[tip] {xmr} XMR")
|
||||
} else {
|
||||
format!("[tip] {xmr} XMR — pending")
|
||||
}
|
||||
} else if verified {
|
||||
format!("[tip] {xmr} XMR — {memo}")
|
||||
} else {
|
||||
format!("[tip] {xmr} XMR — {memo} (pending)")
|
||||
}
|
||||
}
|
||||
|
||||
fn check_invoice_fields(amount_atomic: &str, address: &str, memo: &str) -> Result<()> {
|
||||
parse_atomic(amount_atomic)?;
|
||||
check_address(address)?;
|
||||
|
|
|
|||
Loading…
Reference in a new issue