Skip to content

Commit b401767

Browse files
WIP
1 parent e499084 commit b401767

File tree

7 files changed

+58
-2387
lines changed

7 files changed

+58
-2387
lines changed

lightning-liquidity/src/lsps5/client.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,12 @@ where
457457
if diff > ten_minutes {
458458
return Err(LSPS5ClientError::InvalidTimestamp(signature_timestamp.to_rfc3339()));
459459
}
460-
460+
println!(
461+
"LSPS5: Received notification from {} at {}",
462+
counterparty_node_id,
463+
signature_timestamp.to_rfc3339()
464+
);
465+
println!("LSPS5: Notification: {:?}", notification);
461466
let message = format!(
462467
"LSPS5: DO NOT SIGN THIS MESSAGE MANUALLY: LSP: At {} I notify {:?}",
463468
signature_timestamp.to_rfc3339(),
@@ -519,16 +524,14 @@ where
519524
/// - `counterparty_node_id`: the LSP’s public key, used to verify the signature.
520525
/// - `timestamp`: ISO8601 time when the LSP created the notification.
521526
/// - `signature`: the zbase32-encoded LN signature over timestamp+body.
522-
/// - `notification_json`: the JSON string of the JSON-RPC notification object.
527+
/// - `notification`: the [`WebhookNotification`] received from the LSP.
523528
///
524-
/// On success, emits [`LSPS5ClientEvent::WebhookNotificationReceived`]
525-
/// and returns the parsed [`WebhookNotification`].
529+
/// On success, emits [`LSPS5ClientEvent::WebhookNotificationReceived`].
526530
///
527531
/// Failure reasons include:
528532
/// - Timestamp too old (drift > 10 minutes)
529533
/// - Replay attack detected (signature reused)
530534
/// - Invalid signature (crypto check fails)
531-
/// - JSON parse errors for malformed `notification_json`
532535
///
533536
/// Clients should call this method upon receiving a [`LSPS5ServiceEvent::SendWebhookNotification`]
534537
/// event, before taking action on the notification. This guarantees that only authentic,
@@ -539,30 +542,28 @@ where
539542
/// [`WebhookNotification`]: super::msgs::WebhookNotification
540543
pub fn parse_webhook_notification(
541544
&self, counterparty_node_id: PublicKey, timestamp: &LSPSDateTime, signature: &str,
542-
notification_json: &str,
543-
) -> Result<WebhookNotification, LSPS5ClientError> {
544-
let event_queue_notifier = self.pending_events.notifier();
545-
let notification: WebhookNotification = serde_json::from_str(notification_json)
546-
.map_err(|e| LSPS5ClientError::DeserializeError(e.to_string()))?;
547-
548-
self.check_signature_exists(signature)?;
549-
550-
self.store_signature(signature.to_string());
551-
545+
notification: &WebhookNotification,
546+
) -> Result<(), LSPS5ClientError> {
552547
match self.verify_notification_signature(
553548
counterparty_node_id,
554549
timestamp,
555550
signature,
556551
&notification,
557552
) {
558553
Ok(signature_valid) => {
554+
let event_queue_notifier = self.pending_events.notifier();
555+
556+
self.check_signature_exists(signature)?;
557+
558+
self.store_signature(signature.to_string());
559+
559560
event_queue_notifier.enqueue(LSPS5ClientEvent::WebhookNotificationReceived {
560561
counterparty_node_id,
561562
notification: notification.clone(),
562563
timestamp: timestamp.clone(),
563564
signature_valid,
564565
});
565-
Ok(notification)
566+
Ok(())
566567
},
567568
Err(e) => Err(e),
568569
}

lightning-liquidity/src/lsps5/msgs.rs

+41
Original file line numberDiff line numberDiff line change
@@ -941,4 +941,45 @@ mod tests {
941941
}
942942
}
943943
}
944+
945+
#[test]
946+
fn test_all_notification_methods_from_spec() {
947+
let methods = [
948+
("lsps5.webhook_registered", WebhookNotificationMethod::LSPS5WebhookRegistered, "{}"),
949+
("lsps5.payment_incoming", WebhookNotificationMethod::LSPS5PaymentIncoming, "{}"),
950+
(
951+
"lsps5.expiry_soon",
952+
WebhookNotificationMethod::LSPS5ExpirySoon { timeout: 144 },
953+
"{\"timeout\":144}",
954+
),
955+
(
956+
"lsps5.liquidity_management_request",
957+
WebhookNotificationMethod::LSPS5LiquidityManagementRequest,
958+
"{}",
959+
),
960+
(
961+
"lsps5.onion_message_incoming",
962+
WebhookNotificationMethod::LSPS5OnionMessageIncoming,
963+
"{}",
964+
),
965+
];
966+
967+
for (method_name, method_enum, params_json) in methods {
968+
let json = format!(
969+
r#"{{"jsonrpc":"2.0","method":"{}","params":{}}}"#,
970+
method_name, params_json
971+
);
972+
973+
let notification: WebhookNotification = serde_json::from_str(&json).unwrap();
974+
975+
assert_eq!(notification.method, method_enum);
976+
977+
let serialized = serde_json::to_string(&notification).unwrap();
978+
assert!(serialized.contains(&format!("\"method\":\"{}\"", method_name)));
979+
980+
if method_name == "lsps5.expiry_soon" {
981+
assert!(serialized.contains("\"timeout\":144"));
982+
}
983+
}
984+
}
944985
}

0 commit comments

Comments
 (0)