From f8f4cfdc90fd63c1857729ec5e43c674b5d6787b Mon Sep 17 00:00:00 2001 From: elnosh Date: Thu, 2 May 2024 16:39:55 -0500 Subject: [PATCH] add support for lnurl comments --- .gitignore | 1 + .../2024-05-02-213721_add_lnurlp_comments/down.sql | 1 + .../2024-05-02-213721_add_lnurlp_comments/up.sql | 1 + src/invoice.rs | 1 + src/lnurlp.rs | 10 +++++++++- src/models/invoice.rs | 2 ++ src/models/schema.rs | 2 ++ 7 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 migrations/2024-05-02-213721_add_lnurlp_comments/down.sql create mode 100644 migrations/2024-05-02-213721_add_lnurlp_comments/up.sql diff --git a/.gitignore b/.gitignore index 3fe745e..85a7910 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ target/ .env .fedimint-test-dir .test +.idea # These are backup files generated by rustfmt **/*.rs.bk diff --git a/migrations/2024-05-02-213721_add_lnurlp_comments/down.sql b/migrations/2024-05-02-213721_add_lnurlp_comments/down.sql new file mode 100644 index 0000000..73e0dfb --- /dev/null +++ b/migrations/2024-05-02-213721_add_lnurlp_comments/down.sql @@ -0,0 +1 @@ +ALTER TABLE "invoice" DROP COLUMN "lnurlp_comment"; \ No newline at end of file diff --git a/migrations/2024-05-02-213721_add_lnurlp_comments/up.sql b/migrations/2024-05-02-213721_add_lnurlp_comments/up.sql new file mode 100644 index 0000000..3c030a8 --- /dev/null +++ b/migrations/2024-05-02-213721_add_lnurlp_comments/up.sql @@ -0,0 +1 @@ +ALTER TABLE "invoice" ADD COLUMN "lnurlp_comment" VARCHAR(100); diff --git a/src/invoice.rs b/src/invoice.rs index 4497cbb..f81691e 100644 --- a/src/invoice.rs +++ b/src/invoice.rs @@ -146,6 +146,7 @@ async fn notify_user( "bolt11": invoice.bolt11, "preimage": invoice.preimage, "zap_request": zap.as_ref().map(|z| z.request.clone()), + "lnurlp_comment": invoice.lnurlp_comment }) .to_string(), None, diff --git a/src/lnurlp.rs b/src/lnurlp.rs index b0f50f5..90fc1dd 100644 --- a/src/lnurlp.rs +++ b/src/lnurlp.rs @@ -37,7 +37,7 @@ pub async fn well_known_lnurlp( max_sendable: Amount { msats: MAX_AMOUNT }, min_sendable: Amount { msats: MIN_AMOUNT }, metadata: calc_metadata(&name, &state.domain_no_http()), - comment_allowed: None, + comment_allowed: Some(MAX_COMMENT_LEN), tag: LnurlType::PayRequest, status: LnurlStatus::Ok, nostr_pubkey: Some(state.nostr_sk.public_key()), @@ -49,6 +49,7 @@ pub async fn well_known_lnurlp( const MAX_AMOUNT: u64 = 100_000_000 * 1_000; // 1 BTC const MIN_AMOUNT: u64 = 5_000; // 5 sats +const MAX_COMMENT_LEN: i32 = 100; pub async fn lnurl_callback( state: &State, @@ -86,6 +87,12 @@ pub async fn lnurl_callback( return Err(anyhow::anyhow!("Invalid nostr event")); } + if let Some(comment) = params.comment.as_ref() { + if comment.len() > MAX_COMMENT_LEN as usize { + return Err(anyhow::anyhow!("Comment too long")) + } + } + let federation_id = FederationId::from_str(&user.federation_id) .map_err(|e| anyhow::anyhow!("Internal error: Invalid federation_id: {e}"))?; @@ -134,6 +141,7 @@ pub async fn lnurl_callback( bolt11: pr.to_string(), amount: amount_msats as i64, state: InvoiceState::Pending as i32, + lnurlp_comment: params.comment }; let created_invoice = state.db.insert_new_invoice(new_invoice)?; diff --git a/src/models/invoice.rs b/src/models/invoice.rs index 0912d04..5255040 100644 --- a/src/models/invoice.rs +++ b/src/models/invoice.rs @@ -19,6 +19,7 @@ pub struct Invoice { pub bolt11: String, pub amount: i64, pub state: i32, + pub lnurlp_comment: Option } impl Invoice { @@ -74,6 +75,7 @@ pub struct NewInvoice { pub bolt11: String, pub amount: i64, pub state: i32, + pub lnurlp_comment: Option } impl NewInvoice { diff --git a/src/models/schema.rs b/src/models/schema.rs index 5e4e720..64c0dd5 100644 --- a/src/models/schema.rs +++ b/src/models/schema.rs @@ -33,6 +33,8 @@ diesel::table! { bolt11 -> Varchar, amount -> Int8, state -> Int4, + #[max_length = 100] + lnurlp_comment -> Nullable, } }