feat(sdk): sign/encode/decode/verify invoice, receipt and tip frames
The SDK could already send an invoice or a tip through Node::pay_invoice /
Node::tip, but it could not touch the payment frames themselves: pay::sign_*
take the ed25519 identity secret and Node exposed only identity_pk(), so the
signing half lived nowhere the SDK could reach.
Add the signing surface to the node instead of exporting the key:
* Node::sign_invoice / sign_receipt / sign_tip sign with the node's own
identity key and return the core frame struct. The secret never leaves
the process and there is no API that hands it out.
* pay_invoice and tip now call those methods rather than signing inline, so
there is one signing path, not two.
SDK (crates/onionwire-sdk), all backward compatible:
* Records Invoice, Receipt, Tip mirroring src/pay.rs, sig as raw bytes so a
frame re-encodes byte-for-byte.
* Wire::sign_invoice / sign_receipt / sign_tip (async, over the node).
* Free functions encode_invoice / encode_receipt / encode_tip,
decode_invoice / decode_receipt / decode_tip (None on a foreign or
truncated frame), verify_invoice / verify_receipt / verify_tip against
the sender's pubkey hex (false on a bad key, never a panic).
Tests: 8 new SDK unit tests (record round-trips through the core encoders,
signer-key-only verification, tamper rejection, foreign/truncated frame,
typed Stopped error after shutdown). SDK 27 passed, 0 failed; SDK clippy
--all-targets -D warnings clean; root clippy --all-targets -D warnings
clean; root lib tests 14 passed; tests/pay.rs 11 passed.
This commit is contained in:
parent
e3a051a980
commit
96d582153a
2 changed files with 433 additions and 8 deletions
62
src/node.rs
62
src/node.rs
|
|
@ -520,6 +520,57 @@ impl Node {
|
|||
self.try_send(&onion, friend_pk, &prekey, &pt).await
|
||||
}
|
||||
|
||||
/// Sign an invoice with this node's identity key.
|
||||
///
|
||||
/// The signing half of [`Self::pay_invoice`], for callers that build the
|
||||
/// frame themselves (the Android SDK). The ed25519 secret never leaves the
|
||||
/// node: it is signed with here and not handed out. `amount_atomic` is
|
||||
/// piconero; `address` must be a valid Monero address; `memo` may be empty.
|
||||
pub fn sign_invoice(
|
||||
&self,
|
||||
amount_atomic: &str,
|
||||
address: &str,
|
||||
memo: &str,
|
||||
) -> Result<pay::Invoice, String> {
|
||||
let k = self.keys()?;
|
||||
pay::sign_invoice(&k.identity_sk, amount_atomic, address, memo, unix_now())
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Sign a receipt for a transfer that already happened: the signing half of
|
||||
/// [`Self::tip`]. Same key discipline as [`Self::sign_invoice`].
|
||||
pub fn sign_receipt(
|
||||
&self,
|
||||
txid: &str,
|
||||
amount_atomic: &str,
|
||||
address: &str,
|
||||
) -> Result<pay::Receipt, String> {
|
||||
let k = self.keys()?;
|
||||
pay::sign_receipt(&k.identity_sk, txid, amount_atomic, address, unix_now())
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Sign a tip frame (receipt + memo) for a transfer that already happened.
|
||||
/// This signs only — it does not move funds; that is [`Self::tip`].
|
||||
pub fn sign_tip(
|
||||
&self,
|
||||
txid: &str,
|
||||
amount_atomic: &str,
|
||||
address: &str,
|
||||
memo: &str,
|
||||
) -> Result<pay::Tip, String> {
|
||||
let k = self.keys()?;
|
||||
pay::sign_tip(
|
||||
&k.identity_sk,
|
||||
txid,
|
||||
amount_atomic,
|
||||
address,
|
||||
memo,
|
||||
unix_now(),
|
||||
)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Invoice to receive. Wallet subaddress if up, else self_profile.xmr_addr.
|
||||
pub async fn pay_invoice(
|
||||
&self,
|
||||
|
|
@ -528,9 +579,7 @@ impl Node {
|
|||
memo: &str,
|
||||
) -> Result<(), String> {
|
||||
let address = self.invoice_address().await?;
|
||||
let k = self.keys()?;
|
||||
let inv = pay::sign_invoice(&k.identity_sk, atomic, &address, memo, unix_now())
|
||||
.map_err(|e| e.to_string())?;
|
||||
let inv = self.sign_invoice(atomic, &address, memo)?;
|
||||
let pt = pay::encode_invoice(&inv);
|
||||
self.send_once(friend_pk, &pt).await?;
|
||||
let store = self.store.lock().map_err(|e| e.to_string())?;
|
||||
|
|
@ -579,12 +628,9 @@ impl Node {
|
|||
.transfer(&address, amount)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let k = self.keys()?;
|
||||
let rcp = pay::sign_receipt(&k.identity_sk, &txid, atomic, &address, unix_now())
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rcp = self.sign_receipt(&txid, atomic, &address)?;
|
||||
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 = self.sign_tip(&txid, atomic, &address, _memo)?;
|
||||
let tip_pt = pay::encode_tip(&tip);
|
||||
self.send_once(friend_pk, &rcp_pt).await?;
|
||||
self.send_once(friend_pk, &tip_pt).await?;
|
||||
|
|
|
|||
Loading…
Reference in a new issue