Skip to content

Commit 21f2aa2

Browse files
committed
ensure the published zap receipt is stored locally
1 parent 7603a24 commit 21f2aa2

File tree

4 files changed

+51
-41
lines changed

4 files changed

+51
-41
lines changed

crates/breez-sdk/core/src/lnurl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub trait LnurlServerClient: Send + Sync {
8484
async fn publish_zap_receipt(
8585
&self,
8686
request: &PublishZapReceiptRequest,
87-
) -> Result<bool, LnurlServerError>;
87+
) -> Result<String, LnurlServerError>;
8888
}
8989

9090
#[derive(Debug, Clone, thiserror::Error)]
@@ -393,7 +393,7 @@ impl LnurlServerClient for ReqwestLnurlServerClient {
393393
async fn publish_zap_receipt(
394394
&self,
395395
request: &PublishZapReceiptRequest,
396-
) -> Result<bool, LnurlServerError> {
396+
) -> Result<String, LnurlServerError> {
397397
let spark_address = self.wallet.get_spark_address().map_err(|e| {
398398
LnurlServerError::SigningError(format!("Failed to get spark address: {e}"))
399399
})?;
@@ -436,7 +436,7 @@ impl LnurlServerClient for ReqwestLnurlServerClient {
436436
"failed to deserialize response json: {e}"
437437
))
438438
})?;
439-
return Ok(body.published);
439+
return Ok(body.zap_receipt);
440440
}
441441
other => {
442442
return Err(LnurlServerError::Network {

crates/breez-sdk/core/src/sdk.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,19 +397,22 @@ impl BreezSdk {
397397
};
398398

399399
// Publish the zap receipt via the server
400-
if let Err(e) = lnurl_server_client
400+
let zap_receipt = match lnurl_server_client
401401
.publish_zap_receipt(&PublishZapReceiptRequest {
402402
payment_hash: payment_hash.clone(),
403403
zap_receipt: zap_receipt.clone(),
404404
})
405405
.await
406406
{
407-
error!(
408-
"Failed to publish zap receipt for payment {}: {}",
409-
payment.id, e
410-
);
411-
continue;
412-
}
407+
Ok(zap_receipt) => zap_receipt,
408+
Err(e) => {
409+
error!(
410+
"Failed to publish zap receipt for payment {}: {}",
411+
payment.id, e
412+
);
413+
continue;
414+
}
415+
};
413416

414417
if let Err(e) = self
415418
.storage

crates/breez-sdk/lnurl-models/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub struct PublishZapReceiptRequest {
7373
#[derive(Debug, Serialize, Deserialize)]
7474
pub struct PublishZapReceiptResponse {
7575
pub published: bool,
76+
pub zap_receipt: String,
7677
}
7778

7879
pub fn sanitize_username(username: &str) -> String {

crates/breez-sdk/lnurl/src/routes.rs

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,15 @@ where
331331

332332
// Check if zap receipt already exists
333333
let mut published = false;
334-
if zap.zap_event.is_some() {
334+
if let Some(zap_receipt) = &zap.zap_event {
335335
debug!(
336336
"Zap receipt already exists for payment hash {}",
337337
payment_hash
338338
);
339-
return Ok(Json(PublishZapReceiptResponse { published }));
339+
return Ok(Json(PublishZapReceiptResponse {
340+
published,
341+
zap_receipt: zap_receipt.clone(),
342+
}));
340343
}
341344

342345
// Parse the zap request to get relay info
@@ -404,13 +407,6 @@ where
404407
}
405408
};
406409

407-
// The nostr keys are not really needed here, but we use them to create the client
408-
let publish_nostr_keys = match &state.nostr_keys {
409-
Some(keys) => keys.clone(),
410-
None => Keys::generate(),
411-
};
412-
let nostr_client = nostr_sdk::Client::new(publish_nostr_keys);
413-
414410
let relays = zap_request
415411
.tags
416412
.iter()
@@ -424,32 +420,39 @@ where
424420
.flatten()
425421
.collect::<Vec<_>>();
426422

427-
if relays.is_empty() {
428-
debug!(
429-
"No relays to publish zap receipt for payment hash {}",
430-
payment_hash
431-
);
432-
return Ok(Json(PublishZapReceiptResponse { published }));
433-
}
434-
435-
for r in &relays {
436-
if let Err(e) = nostr_client.add_relay(r).await {
437-
warn!("Failed to add relay {r}: {e}");
423+
if !relays.is_empty() {
424+
// The nostr keys are not really needed here, but we use them to create the client
425+
let publish_nostr_keys = match &state.nostr_keys {
426+
Some(keys) => keys.clone(),
427+
None => Keys::generate(),
428+
};
429+
let nostr_client = nostr_sdk::Client::new(publish_nostr_keys);
430+
for r in &relays {
431+
if let Err(e) = nostr_client.add_relay(r).await {
432+
warn!("Failed to add relay {r}: {e}");
433+
}
438434
}
439-
}
440435

441-
nostr_client.connect().await;
436+
nostr_client.connect().await;
442437

443-
if let Err(e) = nostr_client.send_event(&zap_receipt).await {
444-
error!("Failed to publish zap receipt to relays: {e}");
445-
} else {
446-
debug!("Published zap receipt to {} relays", relays.len());
447-
published = true;
448-
}
438+
if let Err(e) = nostr_client.send_event(&zap_receipt).await {
439+
error!("Failed to publish zap receipt to relays: {e}");
440+
} else {
441+
debug!("Published zap receipt to {} relays", relays.len());
442+
published = !relays.is_empty();
443+
}
449444

450-
nostr_client.disconnect().await;
445+
nostr_client.disconnect().await;
446+
}
451447

452-
zap.zap_event = Some(payload.zap_receipt);
448+
let zap_receipt_json = zap_receipt.try_as_json().map_err(|e| {
449+
error!("failed to serialize zap receipt: {}", e);
450+
(
451+
StatusCode::INTERNAL_SERVER_ERROR,
452+
Json(json!({"error": "internal server error"})),
453+
)
454+
})?;
455+
zap.zap_event = Some(zap_receipt_json.clone());
453456
zap.updated_at = now_millis();
454457
state.db.upsert_zap(&zap).await.map_err(|e| {
455458
error!("failed to save zap receipt: {}", e);
@@ -459,7 +462,10 @@ where
459462
)
460463
})?;
461464

462-
Ok(Json(PublishZapReceiptResponse { published }))
465+
Ok(Json(PublishZapReceiptResponse {
466+
published,
467+
zap_receipt: zap_receipt_json,
468+
}))
463469
}
464470

465471
pub async fn handle_lnurl_pay(

0 commit comments

Comments
 (0)