From 9b9c08cffe6edc5f56c3679ddd95c0ece3257f06 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Sun, 9 Jun 2024 13:13:02 -0500 Subject: [PATCH] Add a tag to zap receipt --- src/invoice.rs | 77 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/src/invoice.rs b/src/invoice.rs index 4497cbb..cc77347 100644 --- a/src/invoice.rs +++ b/src/invoice.rs @@ -7,7 +7,7 @@ use fedimint_ln_client::{LightningClientModule, LnReceiveState}; use futures::StreamExt; use itertools::Itertools; use log::{error, info}; -use nostr::{Event, EventBuilder, JsonUtil}; +use nostr::{Alphabet, Event, EventBuilder, JsonUtil, Kind, SingleLetterTag, Tag, TagKind}; use nostr_sdk::Client; use serde::{Deserialize, Serialize}; use serde_json::json; @@ -155,7 +155,7 @@ async fn notify_user( // Send zap if needed if let Some(zap) = zap { let request = Event::from_json(&zap.request)?; - let event = EventBuilder::zap_receipt( + let event = create_zap_receipt( invoice.bolt11.to_string(), Some(invoice.preimage.clone()), request, @@ -171,3 +171,76 @@ async fn notify_user( info!("Sent nostr dm: {dm}"); Ok(()) } + +// Needed to copy this from rust-nostr but add the `a` tag ourselves +// Latest version of rust-nostr has this fix but we're unable to update because of other problems +fn create_zap_receipt( + invoice: String, + preimage: Option, + zap_request: Event, +) -> EventBuilder { + let mut tags = vec![ + Tag::Bolt11(invoice), + Tag::Description(zap_request.as_json()), + ]; + + // add preimage tag if provided + if let Some(pre_image_tag) = preimage { + tags.push(Tag::Preimage(pre_image_tag)) + } + + // add e tag + if let Some(tag) = zap_request + .iter_tags() + .find(|t| { + t.kind() + == TagKind::SingleLetter(SingleLetterTag { + character: Alphabet::E, + uppercase: false, + }) + }) + .cloned() + { + tags.push(tag); + } + + // add a tag + if let Some(tag) = zap_request + .iter_tags() + .find(|t| { + t.kind() + == TagKind::SingleLetter(SingleLetterTag { + character: Alphabet::A, + uppercase: false, + }) + }) + .cloned() + { + tags.push(tag); + } + + // add p tag + if let Some(tag) = zap_request + .iter_tags() + .find(|t| { + t.kind() + == TagKind::SingleLetter(SingleLetterTag { + character: Alphabet::P, + uppercase: false, + }) + }) + .cloned() + { + tags.push(tag); + } + + // add P tag + tags.push(Tag::PublicKey { + public_key: zap_request.author(), + relay_url: None, + alias: None, + uppercase: true, + }); + + EventBuilder::new(Kind::ZapReceipt, "", tags) +}