Skip to content

Commit 418e59d

Browse files
wip. rebase needed
1 parent a62f88a commit 418e59d

File tree

6 files changed

+228
-144
lines changed

6 files changed

+228
-144
lines changed

lightning-liquidity/src/lsps0/ser.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,6 @@ impl LSPSMessage {
319319
LSPSMessage::LSPS2(LSPS2Message::Request(request_id, request)) => {
320320
Some((LSPSRequestId(request_id.0.clone()), request.into()))
321321
},
322-
// Add LSPS5
323322
LSPSMessage::LSPS5(LSPS5Message::Request(request_id, request)) => {
324323
Some((LSPSRequestId(request_id.0.clone()), request.into()))
325324
},
@@ -723,12 +722,11 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
723722
Err(de::Error::custom("Received invalid JSON-RPC object: one of method, result, or error required"))
724723
}
725724
},
726-
// Add LSPS5 methods
727725
LSPSMethod::LSPS5SetWebhook => {
728726
if let Some(error) = error {
729727
Ok(LSPSMessage::LSPS5(LSPS5Message::Response(
730728
id,
731-
LSPS5Response::SetWebhookError(error),
729+
LSPS5Response::SetWebhookError(error.into()),
732730
)))
733731
} else if let Some(result) = result {
734732
let response =
@@ -745,7 +743,7 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
745743
if let Some(error) = error {
746744
Ok(LSPSMessage::LSPS5(LSPS5Message::Response(
747745
id,
748-
LSPS5Response::ListWebhooksError(error),
746+
LSPS5Response::ListWebhooksError(error.into()),
749747
)))
750748
} else if let Some(result) = result {
751749
let response =
@@ -762,7 +760,7 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
762760
if let Some(error) = error {
763761
Ok(LSPSMessage::LSPS5(LSPS5Message::Response(
764762
id,
765-
LSPS5Response::RemoveWebhookError(error),
763+
LSPS5Response::RemoveWebhookError(error.into()),
766764
)))
767765
} else if let Some(result) = result {
768766
let response =

lightning-liquidity/src/lsps5/client.rs

+25-30
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
//! Client implementation for LSPS5 webhook registration
1111
12+
use crate::alloc::string::ToString;
1213
use crate::events::EventQueue;
1314
use crate::lsps0::ser::{LSPSDateTime, LSPSMessage, LSPSProtocolMessageHandler, LSPSRequestId};
1415
use crate::lsps5::event::LSPS5ClientEvent;
@@ -17,27 +18,25 @@ use crate::lsps5::msgs::{
1718
SetWebhookRequest, WebhookNotification,
1819
};
1920
use crate::message_queue::MessageQueue;
20-
21-
use bitcoin::secp256k1::PublicKey;
22-
use lightning::ln::msgs::{ErrorAction, LightningError};
23-
use lightning::util::message_signing;
24-
25-
use crate::sync::{Arc, Mutex, RwLock};
26-
use core::ops::Deref;
27-
use core::time::Duration;
28-
29-
use crate::alloc::string::ToString;
3021
use crate::prelude::{new_hash_map, HashMap};
31-
use alloc::string::String;
22+
use crate::sync::{Arc, Mutex, RwLock};
23+
use crate::utils::generate_request_id;
3224

3325
use super::msgs::{LSPS5AppName, LSPS5WebhookUrl};
3426
#[cfg(feature = "time")]
3527
use super::service::DefaultTimeProvider;
3628
use super::service::TimeProvider;
37-
use crate::utils::generate_request_id;
29+
3830
use alloc::collections::VecDeque;
31+
use alloc::string::String;
32+
use bitcoin::secp256k1::PublicKey;
33+
use lightning::ln::msgs::{ErrorAction, LightningError};
3934
use lightning::sign::EntropySource;
4035
use lightning::util::logger::Level;
36+
use lightning::util::message_signing;
37+
38+
use core::ops::Deref;
39+
use core::time::Duration;
4140

4241
/// Default maximum age in seconds for cached responses (1 hour)
4342
pub const DEFAULT_RESPONSE_MAX_AGE_SECS: u64 = 3600;
@@ -226,14 +225,13 @@ where
226225
pub fn set_webhook(
227226
&self, counterparty_node_id: PublicKey, app_name: String, webhook_url: String,
228227
) -> Result<LSPSRequestId, LightningError> {
229-
let app_name = LSPS5AppName::new(app_name).map_err(|e| LightningError {
230-
err: e.message,
228+
let app_name = LSPS5AppName::from_string(app_name).map_err(|e| LightningError {
229+
err: e.message(),
231230
action: ErrorAction::IgnoreAndLog(Level::Error),
232231
})?;
233232

234-
let lsps_webhook_url = LSPS5WebhookUrl::new(webhook_url).map_err(|e| LightningError {
235-
err: e.message,
236-
action: ErrorAction::IgnoreAndLog(Level::Error),
233+
let lsps_webhook_url = LSPS5WebhookUrl::from_string(webhook_url).map_err(|e| {
234+
LightningError { err: e.message(), action: ErrorAction::IgnoreAndLog(Level::Error) }
237235
})?;
238236

239237
let request_id = generate_request_id(&self.entropy_source);
@@ -304,8 +302,8 @@ where
304302
pub fn remove_webhook(
305303
&self, counterparty_node_id: PublicKey, app_name: String,
306304
) -> Result<LSPSRequestId, LightningError> {
307-
let app_name = LSPS5AppName::new(app_name).map_err(|e| LightningError {
308-
err: e.message,
305+
let app_name = LSPS5AppName::from_string(app_name).map_err(|e| LightningError {
306+
err: e.message(),
309307
action: ErrorAction::IgnoreAndLog(Level::Error),
310308
})?;
311309

@@ -361,8 +359,7 @@ where
361359
self.pending_events.enqueue(
362360
LSPS5ClientEvent::WebhookRegistrationFailed {
363361
counterparty_node_id: *counterparty_node_id,
364-
error_code: error.code,
365-
error_message: error.message,
362+
error,
366363
app_name,
367364
url: webhook_url,
368365
request_id,
@@ -396,8 +393,7 @@ where
396393
LSPS5Response::ListWebhooksError(error) => {
397394
self.pending_events.enqueue(LSPS5ClientEvent::WebhooksListFailed {
398395
counterparty_node_id: *counterparty_node_id,
399-
error_code: error.code,
400-
error_message: error.message,
396+
error,
401397
request_id,
402398
});
403399
result = Ok(());
@@ -426,8 +422,7 @@ where
426422
self.pending_events.enqueue(
427423
LSPS5ClientEvent::WebhookRemovalFailed {
428424
counterparty_node_id: *counterparty_node_id,
429-
error_code: error.code,
430-
error_message: error.message,
425+
error,
431426
app_name,
432427
request_id,
433428
},
@@ -686,8 +681,8 @@ mod tests {
686681
let (client, _, _, peer, _) = setup_test_client();
687682
const APP_NAME: &str = "test-app";
688683
const WEBHOOK_URL: &str = "https://example.com/hook";
689-
let lsps5_app_name = LSPS5AppName::new(APP_NAME.to_string()).unwrap();
690-
let lsps5_webhook_url = LSPS5WebhookUrl::new(WEBHOOK_URL.to_string()).unwrap();
684+
let lsps5_app_name = LSPS5AppName::from_string(APP_NAME.to_string()).unwrap();
685+
let lsps5_webhook_url = LSPS5WebhookUrl::from_string(WEBHOOK_URL.to_string()).unwrap();
691686
let set_req_id =
692687
client.set_webhook(peer, APP_NAME.to_string(), WEBHOOK_URL.to_string()).unwrap();
693688
let list_req_id = client.list_webhooks(peer).unwrap();
@@ -751,9 +746,9 @@ mod tests {
751746
const OLD_APP_NAME: &str = "test-app-old";
752747
const NEW_APP_NAME: &str = "test-app-new";
753748
const WEBHOOK_URL: &str = "https://example.com/hook";
754-
let lsps5_old_app_name = LSPS5AppName::new(OLD_APP_NAME.to_string()).unwrap();
755-
let lsps5_new_app_name = LSPS5AppName::new(NEW_APP_NAME.to_string()).unwrap();
756-
let lsps5_webhook_url = LSPS5WebhookUrl::new(WEBHOOK_URL.to_string()).unwrap();
749+
let lsps5_old_app_name = LSPS5AppName::from_string(OLD_APP_NAME.to_string()).unwrap();
750+
let lsps5_new_app_name = LSPS5AppName::from_string(NEW_APP_NAME.to_string()).unwrap();
751+
let lsps5_webhook_url = LSPS5WebhookUrl::from_string(WEBHOOK_URL.to_string()).unwrap();
757752
let now = time_provider.duration_since_epoch();
758753
let mut peer_state = PeerState::new(Duration::from_secs(1800), time_provider.clone());
759754
peer_state.last_cleanup = Some(LSPSDateTime::new_from_duration_since_epoch(

lightning-liquidity/src/lsps5/event.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use alloc::vec::Vec;
1616
use bitcoin::secp256k1::PublicKey;
1717

1818
use super::msgs::LSPS5AppName;
19+
use super::msgs::LSPS5Error;
1920
use super::msgs::LSPS5WebhookUrl;
2021
use super::msgs::WebhookNotification;
2122
/// An event which an bLIP-55 / LSPS5 server should take some action in response to.
@@ -41,6 +42,7 @@ pub enum LSPS5ServiceEvent {
4142
/// This URL is used by the LSP to send notifications.
4243
///
4344
/// **Note**: Ensure the URL is valid and its length does not exceed [`MAX_APP_NAME_LENGTH`].
45+
/// Also ensure that the URL points to a public host.
4446
///
4547
/// [`MAX_APP_NAME_LENGTH`]: super::msgs::MAX_APP_NAME_LENGTH
4648
url: LSPS5WebhookUrl,
@@ -146,11 +148,8 @@ pub enum LSPS5ClientEvent {
146148
WebhookRegistrationFailed {
147149
/// The node id of the LSP that rejected the registration
148150
counterparty_node_id: PublicKey,
149-
/// Error code from the LSP (500: too_long, 501: url_parse_error,
150-
/// 502: unsupported_protocol, 503: too_many_webhooks)
151-
error_code: i32,
152-
/// Error message from the LSP
153-
error_message: String,
151+
/// Error from the LSP
152+
error: LSPS5Error,
154153
/// The app name that was attempted
155154
app_name: LSPS5AppName,
156155
/// The webhook URL that was attempted
@@ -183,10 +182,8 @@ pub enum LSPS5ClientEvent {
183182
WebhooksListFailed {
184183
/// The node id of the LSP that rejected the request
185184
counterparty_node_id: PublicKey,
186-
/// Error code from the LSP
187-
error_code: i32,
188-
/// Error message from the LSP
189-
error_message: String,
185+
/// Error from the LSP
186+
error: LSPS5Error,
190187
/// The identifier of the issued bLIP-55 / LSPS5 list webhooks request
191188
///
192189
/// This can be used to track which request this event corresponds to.
@@ -215,10 +212,8 @@ pub enum LSPS5ClientEvent {
215212
WebhookRemovalFailed {
216213
/// The node id of the LSP that rejected the removal
217214
counterparty_node_id: PublicKey,
218-
/// Error code from the LSP (1010: app_name_not_found)
219-
error_code: i32,
220-
/// Error message from the LSP
221-
error_message: String,
215+
/// Error from the LSP
216+
error: LSPS5Error,
222217
/// The app name that was attempted to be removed
223218
app_name: LSPS5AppName,
224219
/// The identifier of the issued bLIP-55 / LSPS5 remove webhook request

0 commit comments

Comments
 (0)