zaps: fix invalid zaps

p tags needs to be the zap target
This commit is contained in:
William Casarin
2025-04-12 15:39:07 -07:00
parent c512cb046f
commit f0763b1278
3 changed files with 42 additions and 32 deletions

View File

@@ -129,16 +129,20 @@ fn process_new_zap_event(
fn send_note_zap( fn send_note_zap(
ndb: &Ndb, ndb: &Ndb,
txn: &Transaction, txn: &Transaction,
target: NoteZapTargetOwned, note_target: NoteZapTargetOwned,
msats: u64, msats: u64,
nsec: &[u8; 32], nsec: &[u8; 32],
relays: Vec<String>, relays: Vec<String>,
) -> Option<FetchingInvoice> { ) -> Option<FetchingInvoice> {
let address = get_users_zap_endpoint(txn, ndb, &target.zap_recipient)?; let address = get_users_zap_endpoint(txn, ndb, &note_target.zap_recipient)?;
let promise = match address { let promise = match address {
ZapAddress::Lud16(s) => fetch_invoice_lud16(s, msats, *nsec, Some(target.note_id), relays), ZapAddress::Lud16(s) => {
ZapAddress::Lud06(s) => fetch_invoice_lnurl(s, msats, *nsec, Some(target.note_id), relays), fetch_invoice_lud16(s, msats, *nsec, ZapTargetOwned::Note(note_target), relays)
}
ZapAddress::Lud06(s) => {
fetch_invoice_lnurl(s, msats, *nsec, ZapTargetOwned::Note(note_target), relays)
}
}; };
Some(promise) Some(promise)
} }
@@ -551,7 +555,7 @@ impl PromiseResponse {
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Debug, PartialEq, Eq, Clone, Hash)] #[derive(Debug, PartialEq, Eq, Clone, Hash)]
enum ZapTargetOwned { pub enum ZapTargetOwned {
Profile(Pubkey), Profile(Pubkey),
Note(NoteZapTargetOwned), Note(NoteZapTargetOwned),
} }

View File

@@ -2,4 +2,6 @@ mod cache;
mod networking; mod networking;
mod zap; mod zap;
pub use cache::{AnyZapState, NoteZapTarget, NoteZapTargetOwned, ZapTarget, ZappingError, Zaps}; pub use cache::{
AnyZapState, NoteZapTarget, NoteZapTargetOwned, ZapTarget, ZapTargetOwned, ZappingError, Zaps,
};

View File

@@ -1,5 +1,5 @@
use crate::ZapError; use crate::{zaps::ZapTargetOwned, ZapError};
use enostr::{NoteId, Pubkey}; use enostr::NoteId;
use nostrdb::NoteBuilder; use nostrdb::NoteBuilder;
use poll_promise::Promise; use poll_promise::Promise;
use serde::Deserialize; use serde::Deserialize;
@@ -60,14 +60,14 @@ fn lud16_to_lnurl(lud16: &str) -> Result<String, ZapError> {
fn make_kind_9734<'a>( fn make_kind_9734<'a>(
lnurl: &str, lnurl: &str,
msats: u64, msats: u64,
comment: &str,
sender_nsec: &[u8; 32], sender_nsec: &[u8; 32],
relays: Vec<String>, relays: Vec<String>,
recipient: &Pubkey, target: ZapTargetOwned,
event_id: Option<&NoteId>,
) -> nostrdb::Note<'a> { ) -> nostrdb::Note<'a> {
let mut builder = NoteBuilder::new().kind(9734); let mut builder = NoteBuilder::new().kind(9734);
builder = builder.start_tag().tag_str("relays"); builder = builder.content(comment).start_tag().tag_str("relays");
for relay in relays { for relay in relays {
builder = builder.tag_str(&relay) builder = builder.tag_str(&relay)
@@ -80,10 +80,21 @@ fn make_kind_9734<'a>(
builder = builder.start_tag().tag_str("lnurl").tag_str(lnurl); builder = builder.start_tag().tag_str("lnurl").tag_str(lnurl);
builder = builder.start_tag().tag_str("p").tag_str(&recipient.hex()); match target {
ZapTargetOwned::Profile(pubkey) => {
builder = builder.start_tag().tag_str("p").tag_str(&pubkey.hex());
}
if let Some(id) = event_id { ZapTargetOwned::Note(note_target) => {
builder = builder.start_tag().tag_str("e").tag_str(&id.hex()); builder = builder
.start_tag()
.tag_str("p")
.tag_str(&note_target.zap_recipient.hex());
builder = builder
.start_tag()
.tag_str("e")
.tag_str(&note_target.note_id.hex());
}
} }
builder.sign(sender_nsec).build().expect("note") builder.sign(sender_nsec).build().expect("note")
@@ -137,11 +148,11 @@ pub fn fetch_invoice_lud16(
lud16: String, lud16: String,
msats: u64, msats: u64,
sender_nsec: [u8; 32], sender_nsec: [u8; 32],
event_id: Option<NoteId>, target: ZapTargetOwned,
relays: Vec<String>, relays: Vec<String>,
) -> FetchingInvoice { ) -> FetchingInvoice {
Promise::spawn_async(tokio::spawn(async move { Promise::spawn_async(tokio::spawn(async move {
fetch_invoice_lud16_async(&lud16, msats, &sender_nsec, event_id.as_ref(), relays).await fetch_invoice_lud16_async(&lud16, msats, &sender_nsec, target, relays).await
})) }))
} }
@@ -150,7 +161,7 @@ pub fn fetch_invoice_lnurl(
lnurl: String, lnurl: String,
msats: u64, msats: u64,
sender_nsec: [u8; 32], sender_nsec: [u8; 32],
event_id: Option<NoteId>, target: ZapTargetOwned,
relays: Vec<String>, relays: Vec<String>,
) -> FetchingInvoice { ) -> FetchingInvoice {
Promise::spawn_async(tokio::spawn(async move { Promise::spawn_async(tokio::spawn(async move {
@@ -159,15 +170,7 @@ pub fn fetch_invoice_lnurl(
Err(e) => return Err(e), Err(e) => return Err(e),
}; };
fetch_invoice_lnurl_async( fetch_invoice_lnurl_async(&lnurl, &pay_req, msats, &sender_nsec, relays, target).await
&lnurl,
&pay_req,
msats,
&sender_nsec,
event_id.as_ref(),
relays,
)
.await
})) }))
} }
@@ -195,17 +198,18 @@ async fn fetch_invoice_lnurl_async(
pay_req: &LNUrlPayRequest, pay_req: &LNUrlPayRequest,
msats: u64, msats: u64,
sender_nsec: &[u8; 32], sender_nsec: &[u8; 32],
event_id: Option<&NoteId>,
relays: Vec<String>, relays: Vec<String>,
target: ZapTargetOwned,
) -> Result<FetchedInvoice, ZapError> { ) -> Result<FetchedInvoice, ZapError> {
let recipient = Pubkey::from_hex(&pay_req.nostr_pubkey) //let recipient = Pubkey::from_hex(&pay_req.nostr_pubkey)
.map_err(|e| ZapError::EndpointError(format!("invalid pubkey hex from endpoint: {e}")))?; //.map_err(|e| ZapError::EndpointError(format!("invalid pubkey hex from endpoint: {e}")))?;
let mut base_url = Url::parse(&pay_req.callback_url) let mut base_url = Url::parse(&pay_req.callback_url)
.map_err(|e| ZapError::EndpointError(format!("invalid callback url from endpoint: {e}")))?; .map_err(|e| ZapError::EndpointError(format!("invalid callback url from endpoint: {e}")))?;
let (query, noteid) = { let (query, noteid) = {
let note = make_kind_9734(lnurl, msats, sender_nsec, relays, &recipient, event_id); let comment: &str = "";
let note = make_kind_9734(lnurl, msats, comment, sender_nsec, relays, target);
let noteid = NoteId::new(*note.id()); let noteid = NoteId::new(*note.id());
let query = endpoint_query_for_invoice(&mut base_url, msats, lnurl, note)?; let query = endpoint_query_for_invoice(&mut base_url, msats, lnurl, note)?;
(query, noteid) (query, noteid)
@@ -222,14 +226,14 @@ async fn fetch_invoice_lud16_async(
lud16: &str, lud16: &str,
msats: u64, msats: u64,
sender_nsec: &[u8; 32], sender_nsec: &[u8; 32],
event_id: Option<&NoteId>, target: ZapTargetOwned,
relays: Vec<String>, relays: Vec<String>,
) -> Result<FetchedInvoice, ZapError> { ) -> Result<FetchedInvoice, ZapError> {
let pay_req = fetch_pay_req_from_lud16(lud16).await?; let pay_req = fetch_pay_req_from_lud16(lud16).await?;
let lnurl = lud16_to_lnurl(lud16)?; let lnurl = lud16_to_lnurl(lud16)?;
fetch_invoice_lnurl_async(&lnurl, &pay_req, msats, sender_nsec, event_id, relays).await fetch_invoice_lnurl_async(&lnurl, &pay_req, msats, sender_nsec, relays, target).await
} }
async fn fetch_invoice(req: &Url) -> Result<LNInvoice, ZapError> { async fn fetch_invoice(req: &Url) -> Result<LNInvoice, ZapError> {