Skip to content

Commit

Permalink
Add a tag to zap receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Jun 9, 2024
1 parent c88748f commit 9b9c08c
Showing 1 changed file with 75 additions and 2 deletions.
77 changes: 75 additions & 2 deletions src/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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<String>,
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)
}

0 comments on commit 9b9c08c

Please sign in to comment.