Skip to content

Commit 04ba24b

Browse files
committed
Add invalid_token_provided API method
We add a method that allows the LSP to signal to the client the token they used was invalid. We use the `102` error code as proposed in lightning/blips#68.
1 parent b1ef515 commit 04ba24b

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

lightning-liquidity/src/lsps1/msgs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub(crate) const _LSPS1_CREATE_ORDER_REQUEST_INVALID_PARAMS_ERROR_CODE: i32 = -3
3535
pub(crate) const LSPS1_CREATE_ORDER_REQUEST_ORDER_MISMATCH_ERROR_CODE: i32 = 100;
3636
#[cfg(lsps1_service)]
3737
pub(crate) const LSPS1_GET_ORDER_REQUEST_ORDER_NOT_FOUND_ERROR_CODE: i32 = 101;
38+
pub(crate) const LSPS1_CREATE_ORDER_REQUEST_UNRECOGNIZED_OR_STALE_TOKEN_ERROR_CODE: i32 = 102;
3839

3940
/// The identifier of an order.
4041
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Hash)]

lightning-liquidity/src/lsps1/service.rs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
//! Contains the main bLIP-51 / LSPS1 server object, [`LSPS1ServiceHandler`].
1111
12-
use alloc::string::{String, ToString};
12+
use alloc::string::ToString;
1313
use alloc::vec::Vec;
1414

1515
use core::future::Future as StdFuture;
@@ -24,6 +24,7 @@ use super::msgs::{
2424
LSPS1GetOrderRequest, LSPS1Message, LSPS1Options, LSPS1OrderId, LSPS1OrderParams,
2525
LSPS1OrderState, LSPS1PaymentInfo, LSPS1Request, LSPS1Response,
2626
LSPS1_CREATE_ORDER_REQUEST_ORDER_MISMATCH_ERROR_CODE,
27+
LSPS1_CREATE_ORDER_REQUEST_UNRECOGNIZED_OR_STALE_TOKEN_ERROR_CODE,
2728
LSPS1_GET_ORDER_REQUEST_ORDER_NOT_FOUND_ERROR_CODE,
2829
};
2930
use super::peer_state::PeerState;
@@ -56,8 +57,6 @@ use bitcoin::secp256k1::PublicKey;
5657
/// Server-side configuration options for bLIP-51 / LSPS1 channel requests.
5758
#[derive(Clone, Debug)]
5859
pub struct LSPS1ServiceConfig {
59-
/// A token to be send with each channel request.
60-
pub token: Option<String>,
6160
/// The options supported by the LSP.
6261
pub supported_options: LSPS1Options,
6362
}
@@ -430,6 +429,42 @@ where
430429
Ok(())
431430
}
432431

432+
/// Used by LSP to inform a client that an order was rejected because the used token was invalid.
433+
///
434+
/// Should be called in response to receiving a [`LSPS1ServiceEvent::RequestForPaymentDetails`]
435+
/// event if the provided token is invalid.
436+
///
437+
/// [`LSPS1ServiceEvent::RequestForPaymentDetails`]: crate::lsps1::event::LSPS1ServiceEvent::RequestForPaymentDetails
438+
pub fn invalid_token_provided(
439+
&self, counterparty_node_id: PublicKey, request_id: LSPSRequestId,
440+
) -> Result<(), APIError> {
441+
let mut message_queue_notifier = self.pending_messages.notifier();
442+
443+
match self.per_peer_state.read().unwrap().get(&counterparty_node_id) {
444+
Some(inner_state_lock) => {
445+
let mut peer_state_lock = inner_state_lock.lock().unwrap();
446+
peer_state_lock.remove_request(&request_id).map_err(|e| {
447+
debug_assert!(false, "Failed to send response due to: {}", e);
448+
let err = format!("Failed to send response due to: {}", e);
449+
APIError::APIMisuseError { err }
450+
})?;
451+
452+
let response = LSPS1Response::CreateOrderError(LSPSResponseError {
453+
code: LSPS1_CREATE_ORDER_REQUEST_UNRECOGNIZED_OR_STALE_TOKEN_ERROR_CODE,
454+
message: "An unrecognized or stale token was provided".to_string(),
455+
data: None,
456+
});
457+
458+
let msg = LSPS1Message::Response(request_id, response).into();
459+
message_queue_notifier.enqueue(&counterparty_node_id, msg);
460+
Ok(())
461+
},
462+
None => Err(APIError::APIMisuseError {
463+
err: format!("No state for the counterparty exists: {}", counterparty_node_id),
464+
}),
465+
}
466+
}
467+
433468
fn handle_get_order_request(
434469
&self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey,
435470
params: LSPS1GetOrderRequest,
@@ -590,6 +625,15 @@ where
590625
}
591626
}
592627

628+
/// Used by LSP to inform a client that an order was rejected because the used token was invalid.
629+
///
630+
/// Wraps [`LSPS1ServiceHandler::invalid_token_provided`].
631+
pub fn invalid_token_provided(
632+
&self, counterparty_node_id: PublicKey, request_id: LSPSRequestId,
633+
) -> Result<(), APIError> {
634+
self.inner.invalid_token_provided(counterparty_node_id, request_id)
635+
}
636+
593637
/// Used by LSP to give details to client regarding the status of channel opening.
594638
///
595639
/// Wraps [`LSPS1ServiceHandler::update_order_status`].

lightning-liquidity/tests/lsps0_integration_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn list_protocols_integration_test() {
4949
min_channel_balance_sat: 100_000,
5050
max_channel_balance_sat: 100_000_000,
5151
};
52-
LSPS1ServiceConfig { supported_options, token: None }
52+
LSPS1ServiceConfig { supported_options }
5353
};
5454
let lsps5_service_config = LSPS5ServiceConfig::default();
5555
let service_config = LiquidityServiceConfig {

lightning-liquidity/tests/lsps1_integration_tests.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use lightning_liquidity::utils::time::TimeProvider;
3535
fn build_lsps1_configs(
3636
supported_options: LSPS1Options,
3737
) -> (LiquidityServiceConfig, LiquidityClientConfig) {
38-
let lsps1_service_config = LSPS1ServiceConfig { token: None, supported_options };
38+
let lsps1_service_config = LSPS1ServiceConfig { supported_options };
3939
let service_config = LiquidityServiceConfig {
4040
lsps1_service_config: Some(lsps1_service_config),
4141
lsps2_service_config: None,
@@ -281,7 +281,6 @@ fn lsps1_service_handler_persistence_across_restarts() {
281281
let service_config = LiquidityServiceConfig {
282282
lsps1_service_config: Some(LSPS1ServiceConfig {
283283
supported_options: supported_options.clone(),
284-
token: None,
285284
}),
286285
lsps2_service_config: None,
287286
lsps5_service_config: None,

0 commit comments

Comments
 (0)