Skip to content

Commit

Permalink
Make payer and zap searchable
Browse files Browse the repository at this point in the history
  • Loading branch information
arronzhang committed Sep 4, 2023
1 parent 07a15d5 commit a580b7b
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 137 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ tokio = { version = "1.29.1", features = [
jsonwebtoken = "8.3.0"
hex = { version = "0.4.3", features = ["serde"] }
serde-aux = { version = "4.2.0", default-features = false }
nostr-sdk = { version = "0.22.0" }
nostr-sdk = { version = "0.23.0" }
base64 = "0.21.2"
governor = "0.6.0"
futures = "0.3.28"
Expand Down
17 changes: 15 additions & 2 deletions entity/src/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,24 @@ pub struct Model {

/// LUD-18 payerdata
#[sea_orm(column_type = "Text")]
pub payer_data: Option<String>,
pub payer: Option<String>,
pub payer_name: Option<String>,
pub payer_email: Option<String>,
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
pub payer_pubkey: Option<Vec<u8>>,

/// NIP-57 zap
/// NIP-57 zap, zap event event is stored in the description field
pub zap: bool,
pub zap_status: i32,
/// zap from user
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
pub zap_from: Option<Vec<u8>>,
/// zap to user pubkey
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
pub zap_pubkey: Option<Vec<u8>>,
/// zap to event id
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
pub zap_event: Option<Vec<u8>>,
/// NIP-57 zap receipt event
#[sea_orm(column_type = "Text")]
pub zap_receipt: Option<String>,
Expand Down
24 changes: 23 additions & 1 deletion migration/src/m20230714_040219_create_invoice_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,14 @@ impl MigrationTrait for Migration {
.not_null(),
)
.col(ColumnDef::new(invoice::Column::Comment).text().null())
.col(ColumnDef::new(invoice::Column::PayerData).text().null())
.col(ColumnDef::new(invoice::Column::Payer).text().null())
.col(ColumnDef::new(invoice::Column::PayerName).string().null())
.col(ColumnDef::new(invoice::Column::PayerEmail).string().null())
.col(
ColumnDef::new(invoice::Column::PayerPubkey)
.binary_len(32)
.null(),
)
.col(
ColumnDef::new(invoice::Column::Zap)
.boolean()
Expand All @@ -158,6 +165,21 @@ impl MigrationTrait for Migration {
.not_null()
.default(0),
)
.col(
ColumnDef::new(invoice::Column::ZapFrom)
.binary_len(32)
.null(),
)
.col(
ColumnDef::new(invoice::Column::ZapPubkey)
.binary_len(32)
.null(),
)
.col(
ColumnDef::new(invoice::Column::ZapEvent)
.binary_len(32)
.null(),
)
.col(ColumnDef::new(invoice::Column::ZapReceipt).text().null())
.to_owned(),
)
Expand Down
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl AppState {
let mut service = Service::new(conf.0, conf.1, conn);
// set donation receiver
if let Some(prikey) = &setting.donation.privkey {
let keys = Keys::new(prikey.clone().into());
let keys = Keys::new((*prikey).into());
service.donation_receiver = Some(keys.public_key().serialize().to_vec());
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ pub enum Error {
#[error(transparent)]
NostrEvent(#[from] nostr_sdk::event::Error),
#[error(transparent)]
NostrEventId(#[from] nostr_sdk::event::id::Error),
#[error(transparent)]
NostrUnsignedEvent(#[from] nostr_sdk::event::unsigned::Error),
#[error(transparent)]
AddrParseError(#[from] std::net::AddrParseError),
Expand Down
60 changes: 50 additions & 10 deletions src/lnurl.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
//! lnurl api

use crate::{full_uri_from_req, AppState, Error, InvoiceExtra, Result};
use crate::{
full_uri_from_req,
service::{InvoicePayer, InvoiceZap},
AppState, Error, InvoiceExtra, Result,
};
use actix_web::{
get, http::StatusCode, http::Uri, web, HttpRequest, HttpResponse, Responder, ResponseError,
Scope,
};
use entity::invoice;
use nostr_sdk::{
prelude::FromBech32, secp256k1::XOnlyPublicKey, Client, Event, EventId, Keys, Kind, Options,
Tag, Timestamp, UnsignedEvent,
prelude::{verify_delegation_signature, FromBech32},
secp256k1::XOnlyPublicKey,
Client, Event, EventId, Keys, Kind, Options, Tag, Timestamp, UnsignedEvent,
};
use sea_orm::{
ActiveModelTrait, ColumnTrait, DbConn, EntityTrait, FromQueryResult, QueryFilter, QuerySelect,
Expand Down Expand Up @@ -152,6 +157,10 @@ pub async fn create_invoice(
}

let payer_data = query.payerdata.clone();
let mut payer = None;
if let Some(data) = &payer_data {
payer = Some(serde_json::from_str::<InvoicePayer>(data).map_err(Error::from)?);
}

let comment = query.comment.clone();
if let Some(comment) = &comment {
Expand All @@ -175,21 +184,46 @@ pub async fn create_invoice(
}

let mut relays = vec![];
let mut event_id = None;
let mut e_count = 0;
let mut pubkey = None;
let mut p_count = 0;
let mut amount = None;
let mut delegator = None;
for tag in &event.tags {
match tag {
Tag::Relays(r) => {
relays = r.clone();
}
Tag::PubKey(_, _) => {
Tag::PubKey(p, _) => {
pubkey = Some(*p);
p_count += 1;
}
Tag::Event(_, _, _) => {
Tag::Event(e, _, _) => {
event_id = Some(*e);
e_count += 1;
}
Tag::Amount(num) => amount = Some(*num),
Tag::Delegation {
delegator_pk,
conditions,
sig,
} => {
// validate nip26
if verify_delegation_signature(
*delegator_pk,
*sig,
event.pubkey,
conditions.clone(),
)
.is_err()
{
return Err(LnurlError::Invalid(
"Nostr event delegation is invalid".to_owned(),
));
}
delegator = Some(*delegator_pk);
}
_ => {}
}
}
Expand Down Expand Up @@ -217,21 +251,27 @@ pub async fn create_invoice(
));
}
}
// nip26 delegation support.
let from = Some(delegator.unwrap_or(event.pubkey));
let extra = InvoiceExtra {
source: invoice::Source::Zaps,
zap: true,
zap: Some(InvoiceZap {
from,
pubkey,
event: event_id,
}),
comment,
zap_receipt: None,
payer_data,
payer,
};
(event_str, extra)
} else {
let extra = InvoiceExtra {
source: invoice::Source::Lnurlp,
zap: false,
zap: None,
comment,
zap_receipt: None,
payer_data,
payer,
};
// lud06, lud18 description hash
let memo = format!(
Expand Down Expand Up @@ -372,7 +412,7 @@ async fn send_receipt(
tags.push(t.clone());
}

let kind = Kind::Zap;
let kind = Kind::ZapReceipt;
let content = "".to_owned();

let created_at: Timestamp = Timestamp::from(invoice.paid_at as u64);
Expand Down
4 changes: 2 additions & 2 deletions src/nwc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl Nwc {
};

for url in &self.state.setting.nwc.relays {
self.client.add_relay(url, proxy).await?;
self.client.add_relay(url.as_str(), proxy).await?;
}
self.client.connect().await;

Expand Down Expand Up @@ -294,7 +294,7 @@ impl Nwc {
res
});
}
Ok(())
Ok(false)
})
.await?;
Ok(())
Expand Down
Loading

0 comments on commit a580b7b

Please sign in to comment.