mirror of
https://github.com/lightningdevkit/rust-lightning.git
synced 2025-02-22 22:36:36 +01:00
Prefix BLIP-51/LSPS1 message types to avoid naming collisions
This commit is contained in:
parent
f9c477f80a
commit
6b720fd88b
4 changed files with 111 additions and 101 deletions
|
@ -11,8 +11,9 @@
|
|||
|
||||
use super::event::LSPS1ClientEvent;
|
||||
use super::msgs::{
|
||||
CreateOrderRequest, CreateOrderResponse, GetInfoRequest, GetInfoResponse, GetOrderRequest,
|
||||
LSPS1Message, LSPS1Request, LSPS1Response, OrderId, OrderParameters,
|
||||
LSPS1CreateOrderRequest, LSPS1CreateOrderResponse, LSPS1GetInfoRequest, LSPS1GetInfoResponse,
|
||||
LSPS1GetOrderRequest, LSPS1Message, LSPS1OrderId, LSPS1OrderParams, LSPS1Request,
|
||||
LSPS1Response,
|
||||
};
|
||||
use crate::message_queue::MessageQueue;
|
||||
|
||||
|
@ -94,14 +95,15 @@ where
|
|||
peer_state_lock.pending_get_info_requests.insert(request_id.clone());
|
||||
}
|
||||
|
||||
let request = LSPS1Request::GetInfo(GetInfoRequest {});
|
||||
let request = LSPS1Request::GetInfo(LSPS1GetInfoRequest {});
|
||||
let msg = LSPS1Message::Request(request_id.clone(), request).into();
|
||||
self.pending_messages.enqueue(&counterparty_node_id, msg);
|
||||
request_id
|
||||
}
|
||||
|
||||
fn handle_get_info_response(
|
||||
&self, request_id: RequestId, counterparty_node_id: &PublicKey, result: GetInfoResponse,
|
||||
&self, request_id: RequestId, counterparty_node_id: &PublicKey,
|
||||
result: LSPS1GetInfoResponse,
|
||||
) -> Result<(), LightningError> {
|
||||
let outer_state_lock = self.per_peer_state.write().unwrap();
|
||||
|
||||
|
@ -184,7 +186,7 @@ where
|
|||
///
|
||||
/// The client agrees to paying channel fees according to the provided parameters.
|
||||
pub fn create_order(
|
||||
&self, counterparty_node_id: &PublicKey, order: OrderParameters,
|
||||
&self, counterparty_node_id: &PublicKey, order: LSPS1OrderParams,
|
||||
refund_onchain_address: Option<Address>,
|
||||
) -> RequestId {
|
||||
let (request_id, request_msg) = {
|
||||
|
@ -195,8 +197,10 @@ where
|
|||
let mut peer_state_lock = inner_state_lock.lock().unwrap();
|
||||
|
||||
let request_id = crate::utils::generate_request_id(&self.entropy_source);
|
||||
let request =
|
||||
LSPS1Request::CreateOrder(CreateOrderRequest { order, refund_onchain_address });
|
||||
let request = LSPS1Request::CreateOrder(LSPS1CreateOrderRequest {
|
||||
order,
|
||||
refund_onchain_address,
|
||||
});
|
||||
let msg = LSPS1Message::Request(request_id.clone(), request).into();
|
||||
peer_state_lock.pending_create_order_requests.insert(request_id.clone());
|
||||
|
||||
|
@ -212,7 +216,7 @@ where
|
|||
|
||||
fn handle_create_order_response(
|
||||
&self, request_id: RequestId, counterparty_node_id: &PublicKey,
|
||||
response: CreateOrderResponse,
|
||||
response: LSPS1CreateOrderResponse,
|
||||
) -> Result<(), LightningError> {
|
||||
let outer_state_lock = self.per_peer_state.read().unwrap();
|
||||
match outer_state_lock.get(counterparty_node_id) {
|
||||
|
@ -302,7 +306,7 @@ where
|
|||
///
|
||||
/// [`LSPS1ClientEvent::OrderStatus`]: crate::lsps1::event::LSPS1ClientEvent::OrderStatus
|
||||
pub fn check_order_status(
|
||||
&self, counterparty_node_id: &PublicKey, order_id: OrderId,
|
||||
&self, counterparty_node_id: &PublicKey, order_id: LSPS1OrderId,
|
||||
) -> RequestId {
|
||||
let (request_id, request_msg) = {
|
||||
let mut outer_state_lock = self.per_peer_state.write().unwrap();
|
||||
|
@ -314,7 +318,8 @@ where
|
|||
let request_id = crate::utils::generate_request_id(&self.entropy_source);
|
||||
peer_state_lock.pending_get_order_requests.insert(request_id.clone());
|
||||
|
||||
let request = LSPS1Request::GetOrder(GetOrderRequest { order_id: order_id.clone() });
|
||||
let request =
|
||||
LSPS1Request::GetOrder(LSPS1GetOrderRequest { order_id: order_id.clone() });
|
||||
let msg = LSPS1Message::Request(request_id.clone(), request).into();
|
||||
|
||||
(request_id, Some(msg))
|
||||
|
@ -329,7 +334,7 @@ where
|
|||
|
||||
fn handle_get_order_response(
|
||||
&self, request_id: RequestId, counterparty_node_id: &PublicKey,
|
||||
response: CreateOrderResponse,
|
||||
response: LSPS1CreateOrderResponse,
|
||||
) -> Result<(), LightningError> {
|
||||
let outer_state_lock = self.per_peer_state.read().unwrap();
|
||||
match outer_state_lock.get(counterparty_node_id) {
|
||||
|
|
|
@ -9,8 +9,8 @@
|
|||
|
||||
//! Contains bLIP-51 / LSPS1 event types
|
||||
|
||||
use super::msgs::OrderId;
|
||||
use super::msgs::{ChannelInfo, LSPS1Options, OrderParameters, PaymentInfo};
|
||||
use super::msgs::LSPS1OrderId;
|
||||
use super::msgs::{LSPS1ChannelInfo, LSPS1Options, LSPS1OrderParams, LSPS1PaymentInfo};
|
||||
|
||||
use crate::lsps0::ser::{RequestId, ResponseError};
|
||||
|
||||
|
@ -78,13 +78,13 @@ pub enum LSPS1ClientEvent {
|
|||
/// The node id of the LSP.
|
||||
counterparty_node_id: PublicKey,
|
||||
/// The id of the channel order.
|
||||
order_id: OrderId,
|
||||
order_id: LSPS1OrderId,
|
||||
/// The order created by client and approved by LSP.
|
||||
order: OrderParameters,
|
||||
order: LSPS1OrderParams,
|
||||
/// The details regarding payment of the order
|
||||
payment: PaymentInfo,
|
||||
payment: LSPS1PaymentInfo,
|
||||
/// The details regarding state of the channel ordered.
|
||||
channel: Option<ChannelInfo>,
|
||||
channel: Option<LSPS1ChannelInfo>,
|
||||
},
|
||||
/// Information from the LSP about the status of a previously created order.
|
||||
///
|
||||
|
@ -102,13 +102,13 @@ pub enum LSPS1ClientEvent {
|
|||
/// The node id of the LSP.
|
||||
counterparty_node_id: PublicKey,
|
||||
/// The id of the channel order.
|
||||
order_id: OrderId,
|
||||
order_id: LSPS1OrderId,
|
||||
/// The order created by client and approved by LSP.
|
||||
order: OrderParameters,
|
||||
order: LSPS1OrderParams,
|
||||
/// The details regarding payment of the order
|
||||
payment: PaymentInfo,
|
||||
payment: LSPS1PaymentInfo,
|
||||
/// The details regarding state of the channel ordered.
|
||||
channel: Option<ChannelInfo>,
|
||||
channel: Option<LSPS1ChannelInfo>,
|
||||
},
|
||||
/// A request previously issued via [`LSPS1ClientHandler::create_order`] or [`LSPS1ClientHandler::check_order_status`].
|
||||
/// failed as the LSP returned an error response.
|
||||
|
@ -151,7 +151,7 @@ pub enum LSPS1ServiceEvent {
|
|||
/// The node id of the client making the information request.
|
||||
counterparty_node_id: PublicKey,
|
||||
/// The order requested by the client.
|
||||
order: OrderParameters,
|
||||
order: LSPS1OrderParams,
|
||||
},
|
||||
/// A request from client to check the status of the payment.
|
||||
///
|
||||
|
@ -169,7 +169,7 @@ pub enum LSPS1ServiceEvent {
|
|||
/// The node id of the client making the information request.
|
||||
counterparty_node_id: PublicKey,
|
||||
/// The order id of order with pending payment.
|
||||
order_id: OrderId,
|
||||
order_id: LSPS1OrderId,
|
||||
},
|
||||
/// If error is encountered, refund the amount if paid by the client.
|
||||
Refund {
|
||||
|
@ -178,6 +178,6 @@ pub enum LSPS1ServiceEvent {
|
|||
/// The node id of the client making the information request.
|
||||
counterparty_node_id: PublicKey,
|
||||
/// The order id of the refunded order.
|
||||
order_id: OrderId,
|
||||
order_id: LSPS1OrderId,
|
||||
},
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ pub(crate) const LSPS1_CREATE_ORDER_REQUEST_ORDER_MISMATCH_ERROR_CODE: i32 = 100
|
|||
|
||||
/// The identifier of an order.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Hash)]
|
||||
pub struct OrderId(pub String);
|
||||
pub struct LSPS1OrderId(pub String);
|
||||
|
||||
/// A request made to an LSP to retrieve the supported options.
|
||||
///
|
||||
|
@ -36,7 +36,7 @@ pub struct OrderId(pub String);
|
|||
/// more information.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)]
|
||||
#[serde(default)]
|
||||
pub struct GetInfoRequest {}
|
||||
pub struct LSPS1GetInfoRequest {}
|
||||
|
||||
/// An object representing the supported protocol options.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
|
@ -69,9 +69,9 @@ pub struct LSPS1Options {
|
|||
pub max_channel_balance_sat: u64,
|
||||
}
|
||||
|
||||
/// A response to a [`GetInfoRequest`].
|
||||
/// A response to a [`LSPS1GetInfoRequest`].
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
pub struct GetInfoResponse {
|
||||
pub struct LSPS1GetInfoResponse {
|
||||
/// All options supported by the LSP.
|
||||
#[serde(flatten)]
|
||||
pub options: LSPS1Options,
|
||||
|
@ -83,10 +83,10 @@ pub struct GetInfoResponse {
|
|||
/// specification](https://github.com/lightning/blips/blob/master/blip-0051.md#2-lsps1create_order)
|
||||
/// for more information.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
pub struct CreateOrderRequest {
|
||||
pub struct LSPS1CreateOrderRequest {
|
||||
/// The order made.
|
||||
#[serde(flatten)]
|
||||
pub order: OrderParameters,
|
||||
pub order: LSPS1OrderParams,
|
||||
/// The address where the LSP will send the funds if the order fails.
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
|
@ -96,7 +96,7 @@ pub struct CreateOrderRequest {
|
|||
|
||||
/// An object representing an bLIP-51 / LSPS1 channel order.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
pub struct OrderParameters {
|
||||
pub struct LSPS1OrderParams {
|
||||
/// Indicates how many satoshi the LSP will provide on their side.
|
||||
#[serde(with = "string_amount")]
|
||||
pub lsp_balance_sat: u64,
|
||||
|
@ -118,28 +118,28 @@ pub struct OrderParameters {
|
|||
pub announce_channel: bool,
|
||||
}
|
||||
|
||||
/// A response to a [`CreateOrderRequest`].
|
||||
/// A response to a [`LSPS1CreateOrderRequest`].
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
pub struct CreateOrderResponse {
|
||||
pub struct LSPS1CreateOrderResponse {
|
||||
/// The id of the channel order.
|
||||
pub order_id: OrderId,
|
||||
pub order_id: LSPS1OrderId,
|
||||
/// The parameters of channel order.
|
||||
#[serde(flatten)]
|
||||
pub order: OrderParameters,
|
||||
pub order: LSPS1OrderParams,
|
||||
/// The datetime when the order was created
|
||||
pub created_at: chrono::DateTime<Utc>,
|
||||
/// The current state of the order.
|
||||
pub order_state: OrderState,
|
||||
pub order_state: LSPS1OrderState,
|
||||
/// Contains details about how to pay for the order.
|
||||
pub payment: PaymentInfo,
|
||||
pub payment: LSPS1PaymentInfo,
|
||||
/// Contains information about the channel state.
|
||||
pub channel: Option<ChannelInfo>,
|
||||
pub channel: Option<LSPS1ChannelInfo>,
|
||||
}
|
||||
|
||||
/// An object representing the state of an order.
|
||||
/// An object representing the status of an order.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||
pub enum OrderState {
|
||||
pub enum LSPS1OrderState {
|
||||
/// The order has been created.
|
||||
Created,
|
||||
/// The LSP has opened the channel and published the funding transaction.
|
||||
|
@ -150,18 +150,18 @@ pub enum OrderState {
|
|||
|
||||
/// Details regarding how to pay for an order.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
pub struct PaymentInfo {
|
||||
pub struct LSPS1PaymentInfo {
|
||||
/// A Lightning payment using BOLT 11.
|
||||
pub bolt11: Option<Bolt11PaymentInfo>,
|
||||
pub bolt11: Option<LSPS1Bolt11PaymentInfo>,
|
||||
/// An onchain payment.
|
||||
pub onchain: Option<OnchainPaymentInfo>,
|
||||
pub onchain: Option<LSPS1OnchainPaymentInfo>,
|
||||
}
|
||||
|
||||
/// A Lightning payment using BOLT 11.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
pub struct Bolt11PaymentInfo {
|
||||
pub struct LSPS1Bolt11PaymentInfo {
|
||||
/// Indicates the current state of the payment.
|
||||
pub state: PaymentState,
|
||||
pub state: LSPS1PaymentState,
|
||||
/// The datetime when the payment option expires.
|
||||
pub expires_at: chrono::DateTime<Utc>,
|
||||
/// The total fee the LSP will charge to open this channel in satoshi.
|
||||
|
@ -176,9 +176,9 @@ pub struct Bolt11PaymentInfo {
|
|||
|
||||
/// An onchain payment.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
pub struct OnchainPaymentInfo {
|
||||
pub struct LSPS1OnchainPaymentInfo {
|
||||
/// Indicates the current state of the payment.
|
||||
pub state: PaymentState,
|
||||
pub state: LSPS1PaymentState,
|
||||
/// The datetime when the payment option expires.
|
||||
pub expires_at: chrono::DateTime<Utc>,
|
||||
/// The total fee the LSP will charge to open this channel in satoshi.
|
||||
|
@ -211,7 +211,7 @@ pub struct OnchainPaymentInfo {
|
|||
/// been deprecated and `REFUNDED` should be used instead.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||
pub enum PaymentState {
|
||||
pub enum LSPS1PaymentState {
|
||||
/// A payment is expected.
|
||||
ExpectPayment,
|
||||
/// A sufficient payment has been received.
|
||||
|
@ -223,7 +223,7 @@ pub enum PaymentState {
|
|||
|
||||
/// Details regarding a detected on-chain payment.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
pub struct OnchainPayment {
|
||||
pub struct LSPS1OnchainPayment {
|
||||
/// The outpoint of the payment.
|
||||
pub outpoint: String,
|
||||
/// The amount of satoshi paid.
|
||||
|
@ -235,7 +235,7 @@ pub struct OnchainPayment {
|
|||
|
||||
/// Details regarding the state of an ordered channel.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
pub struct ChannelInfo {
|
||||
pub struct LSPS1ChannelInfo {
|
||||
/// The datetime when the funding transaction has been published.
|
||||
pub funded_at: chrono::DateTime<Utc>,
|
||||
/// The outpoint of the funding transaction.
|
||||
|
@ -250,36 +250,36 @@ pub struct ChannelInfo {
|
|||
/// specification](https://github.com/lightning/blips/blob/master/blip-0051.md#21-lsps1get_order)
|
||||
/// for more information.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
pub struct GetOrderRequest {
|
||||
pub struct LSPS1GetOrderRequest {
|
||||
/// The id of the order.
|
||||
pub order_id: OrderId,
|
||||
pub order_id: LSPS1OrderId,
|
||||
}
|
||||
|
||||
/// An enum that captures all the valid JSON-RPC requests in the bLIP-51 / LSPS1 protocol.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum LSPS1Request {
|
||||
/// A request to learn about the options supported by the LSP.
|
||||
GetInfo(GetInfoRequest),
|
||||
GetInfo(LSPS1GetInfoRequest),
|
||||
/// A request to create a channel order.
|
||||
CreateOrder(CreateOrderRequest),
|
||||
CreateOrder(LSPS1CreateOrderRequest),
|
||||
/// A request to query a previously created channel order.
|
||||
GetOrder(GetOrderRequest),
|
||||
GetOrder(LSPS1GetOrderRequest),
|
||||
}
|
||||
|
||||
/// An enum that captures all the valid JSON-RPC responses in the bLIP-51 / LSPS1 protocol.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum LSPS1Response {
|
||||
/// A successful response to a [`GetInfoRequest`].
|
||||
GetInfo(GetInfoResponse),
|
||||
/// An error response to a [`GetInfoRequest`].
|
||||
/// A successful response to a [`LSPS1GetInfoRequest`].
|
||||
GetInfo(LSPS1GetInfoResponse),
|
||||
/// An error response to a [`LSPS1GetInfoRequest`].
|
||||
GetInfoError(ResponseError),
|
||||
/// A successful response to a [`CreateOrderRequest`].
|
||||
CreateOrder(CreateOrderResponse),
|
||||
/// An error response to a [`CreateOrderRequest`].
|
||||
/// A successful response to a [`LSPS1CreateOrderRequest`].
|
||||
CreateOrder(LSPS1CreateOrderResponse),
|
||||
/// An error response to a [`LSPS1CreateOrderRequest`].
|
||||
CreateOrderError(ResponseError),
|
||||
/// A successful response to a [`GetOrderRequest`].
|
||||
GetOrder(CreateOrderResponse),
|
||||
/// An error response to a [`GetOrderRequest`].
|
||||
/// A successful response to a [`LSPS1GetOrderRequest`].
|
||||
GetOrder(LSPS1CreateOrderResponse),
|
||||
/// An error response to a [`LSPS1GetOrderRequest`].
|
||||
GetOrderError(ResponseError),
|
||||
}
|
||||
|
||||
|
@ -351,7 +351,7 @@ mod tests {
|
|||
fn parse_spec_test_vectors() {
|
||||
// Here, we simply assert that we're able to parse all examples given in LSPS1.
|
||||
let json_str = r#"{}"#;
|
||||
let _get_info_request: GetInfoRequest = serde_json::from_str(json_str).unwrap();
|
||||
let _get_info_request: LSPS1GetInfoRequest = serde_json::from_str(json_str).unwrap();
|
||||
|
||||
let json_str = r#"{
|
||||
"min_required_channel_confirmations": 0,
|
||||
|
@ -365,7 +365,7 @@ mod tests {
|
|||
"min_channel_balance_sat": "50000",
|
||||
"max_channel_balance_sat": "100000000"
|
||||
}"#;
|
||||
let _get_info_response: GetInfoResponse = serde_json::from_str(json_str).unwrap();
|
||||
let _get_info_response: LSPS1GetInfoResponse = serde_json::from_str(json_str).unwrap();
|
||||
|
||||
let json_str = r#"{
|
||||
"lsp_balance_sat": "5000000",
|
||||
|
@ -377,7 +377,8 @@ mod tests {
|
|||
"refund_onchain_address": "bc1qvmsy0f3yyes6z9jvddk8xqwznndmdwapvrc0xrmhd3vqj5rhdrrq6hz49h",
|
||||
"announce_channel": true
|
||||
}"#;
|
||||
let _create_order_request: CreateOrderRequest = serde_json::from_str(json_str).unwrap();
|
||||
let _create_order_request: LSPS1CreateOrderRequest =
|
||||
serde_json::from_str(json_str).unwrap();
|
||||
|
||||
let json_str = r#"{
|
||||
"state" : "EXPECT_PAYMENT",
|
||||
|
@ -386,7 +387,7 @@ mod tests {
|
|||
"order_total_sat": "200888",
|
||||
"invoice": "lnbc252u1p3aht9ysp580g4633gd2x9lc5al0wd8wx0mpn9748jeyz46kqjrpxn52uhfpjqpp5qgf67tcqmuqehzgjm8mzya90h73deafvr4m5705l5u5l4r05l8cqdpud3h8ymm4w3jhytnpwpczqmt0de6xsmre2pkxzm3qydmkzdjrdev9s7zhgfaqxqyjw5qcqpjrzjqt6xptnd85lpqnu2lefq4cx070v5cdwzh2xlvmdgnu7gqp4zvkus5zapryqqx9qqqyqqqqqqqqqqqcsq9q9qyysgqen77vu8xqjelum24hgjpgfdgfgx4q0nehhalcmuggt32japhjuksq9jv6eksjfnppm4hrzsgyxt8y8xacxut9qv3fpyetz8t7tsymygq8yzn05"
|
||||
}"#;
|
||||
let _bolt11_payment: Bolt11PaymentInfo = serde_json::from_str(json_str).unwrap();
|
||||
let _bolt11_payment: LSPS1Bolt11PaymentInfo = serde_json::from_str(json_str).unwrap();
|
||||
|
||||
let json_str = r#"{
|
||||
"state": "EXPECT_PAYMENT",
|
||||
|
@ -397,7 +398,7 @@ mod tests {
|
|||
"min_onchain_payment_confirmations": 1,
|
||||
"min_fee_for_0conf": 253
|
||||
}"#;
|
||||
let _onchain_payment: OnchainPaymentInfo = serde_json::from_str(json_str).unwrap();
|
||||
let _onchain_payment: LSPS1OnchainPaymentInfo = serde_json::from_str(json_str).unwrap();
|
||||
|
||||
let json_str = r#"{
|
||||
"bolt11": {
|
||||
|
@ -417,7 +418,7 @@ mod tests {
|
|||
"min_fee_for_0conf": 253
|
||||
}
|
||||
}"#;
|
||||
let _payment: PaymentInfo = serde_json::from_str(json_str).unwrap();
|
||||
let _payment: LSPS1PaymentInfo = serde_json::from_str(json_str).unwrap();
|
||||
|
||||
let json_str = r#"{
|
||||
"order_id": "bb4b5d0a-8334-49d8-9463-90a6d413af7c",
|
||||
|
@ -451,26 +452,27 @@ mod tests {
|
|||
},
|
||||
"channel": null
|
||||
}"#;
|
||||
let _create_order_response: CreateOrderResponse = serde_json::from_str(json_str).unwrap();
|
||||
let _create_order_response: LSPS1CreateOrderResponse =
|
||||
serde_json::from_str(json_str).unwrap();
|
||||
|
||||
let json_str = r#"{
|
||||
"order_id": "bb4b5d0a-8334-49d8-9463-90a6d413af7c"
|
||||
}"#;
|
||||
let _get_order_request: GetOrderRequest = serde_json::from_str(json_str).unwrap();
|
||||
let _get_order_request: LSPS1GetOrderRequest = serde_json::from_str(json_str).unwrap();
|
||||
|
||||
let json_str = r#"{
|
||||
"funded_at": "2012-04-23T18:25:43.511Z",
|
||||
"funding_outpoint": "0301e0480b374b32851a9462db29dc19fe830a7f7d7a88b81612b9d42099c0ae:0",
|
||||
"expires_at": "2012-04-23T18:25:43.511Z"
|
||||
}"#;
|
||||
let _channel: ChannelInfo = serde_json::from_str(json_str).unwrap();
|
||||
let _channel: LSPS1ChannelInfo = serde_json::from_str(json_str).unwrap();
|
||||
|
||||
let json_str = r#""CANCELLED""#;
|
||||
let payment_state: PaymentState = serde_json::from_str(json_str).unwrap();
|
||||
assert_eq!(payment_state, PaymentState::Refunded);
|
||||
let payment_state: LSPS1PaymentState = serde_json::from_str(json_str).unwrap();
|
||||
assert_eq!(payment_state, LSPS1PaymentState::Refunded);
|
||||
|
||||
let json_str = r#""REFUNDED""#;
|
||||
let payment_state: PaymentState = serde_json::from_str(json_str).unwrap();
|
||||
assert_eq!(payment_state, PaymentState::Refunded);
|
||||
let payment_state: LSPS1PaymentState = serde_json::from_str(json_str).unwrap();
|
||||
assert_eq!(payment_state, LSPS1PaymentState::Refunded);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,9 +11,10 @@
|
|||
|
||||
use super::event::LSPS1ServiceEvent;
|
||||
use super::msgs::{
|
||||
ChannelInfo, CreateOrderRequest, CreateOrderResponse, GetInfoResponse, GetOrderRequest,
|
||||
LSPS1Message, LSPS1Options, LSPS1Request, LSPS1Response, OrderId, OrderParameters, OrderState,
|
||||
PaymentInfo, LSPS1_CREATE_ORDER_REQUEST_ORDER_MISMATCH_ERROR_CODE,
|
||||
LSPS1ChannelInfo, LSPS1CreateOrderRequest, LSPS1CreateOrderResponse, LSPS1GetInfoResponse,
|
||||
LSPS1GetOrderRequest, LSPS1Message, LSPS1Options, LSPS1OrderId, LSPS1OrderParams,
|
||||
LSPS1OrderState, LSPS1PaymentInfo, LSPS1Request, LSPS1Response,
|
||||
LSPS1_CREATE_ORDER_REQUEST_ORDER_MISMATCH_ERROR_CODE,
|
||||
};
|
||||
use crate::message_queue::MessageQueue;
|
||||
|
||||
|
@ -54,8 +55,8 @@ impl From<ChannelStateError> for LightningError {
|
|||
|
||||
#[derive(PartialEq, Debug)]
|
||||
enum OutboundRequestState {
|
||||
OrderCreated { order_id: OrderId },
|
||||
WaitingPayment { order_id: OrderId },
|
||||
OrderCreated { order_id: LSPS1OrderId },
|
||||
WaitingPayment { order_id: LSPS1OrderId },
|
||||
Ready,
|
||||
}
|
||||
|
||||
|
@ -71,9 +72,9 @@ impl OutboundRequestState {
|
|||
}
|
||||
|
||||
struct OutboundLSPS1Config {
|
||||
order: OrderParameters,
|
||||
order: LSPS1OrderParams,
|
||||
created_at: chrono::DateTime<Utc>,
|
||||
payment: PaymentInfo,
|
||||
payment: LSPS1PaymentInfo,
|
||||
}
|
||||
|
||||
struct OutboundCRChannel {
|
||||
|
@ -83,8 +84,8 @@ struct OutboundCRChannel {
|
|||
|
||||
impl OutboundCRChannel {
|
||||
fn new(
|
||||
order: OrderParameters, created_at: chrono::DateTime<Utc>, order_id: OrderId,
|
||||
payment: PaymentInfo,
|
||||
order: LSPS1OrderParams, created_at: chrono::DateTime<Utc>, order_id: LSPS1OrderId,
|
||||
payment: LSPS1PaymentInfo,
|
||||
) -> Self {
|
||||
Self {
|
||||
state: OutboundRequestState::OrderCreated { order_id },
|
||||
|
@ -105,13 +106,13 @@ impl OutboundCRChannel {
|
|||
|
||||
#[derive(Default)]
|
||||
struct PeerState {
|
||||
outbound_channels_by_order_id: HashMap<OrderId, OutboundCRChannel>,
|
||||
outbound_channels_by_order_id: HashMap<LSPS1OrderId, OutboundCRChannel>,
|
||||
request_to_cid: HashMap<RequestId, u128>,
|
||||
pending_requests: HashMap<RequestId, LSPS1Request>,
|
||||
}
|
||||
|
||||
impl PeerState {
|
||||
fn insert_outbound_channel(&mut self, order_id: OrderId, channel: OutboundCRChannel) {
|
||||
fn insert_outbound_channel(&mut self, order_id: LSPS1OrderId, channel: OutboundCRChannel) {
|
||||
self.outbound_channels_by_order_id.insert(order_id, channel);
|
||||
}
|
||||
|
||||
|
@ -119,7 +120,7 @@ impl PeerState {
|
|||
self.request_to_cid.insert(request_id, channel_id);
|
||||
}
|
||||
|
||||
fn remove_outbound_channel(&mut self, order_id: OrderId) {
|
||||
fn remove_outbound_channel(&mut self, order_id: LSPS1OrderId) {
|
||||
self.outbound_channels_by_order_id.remove(&order_id);
|
||||
}
|
||||
}
|
||||
|
@ -166,7 +167,7 @@ where
|
|||
fn handle_get_info_request(
|
||||
&self, request_id: RequestId, counterparty_node_id: &PublicKey,
|
||||
) -> Result<(), LightningError> {
|
||||
let response = LSPS1Response::GetInfo(GetInfoResponse {
|
||||
let response = LSPS1Response::GetInfo(LSPS1GetInfoResponse {
|
||||
options: self
|
||||
.config
|
||||
.supported_options
|
||||
|
@ -184,7 +185,8 @@ where
|
|||
}
|
||||
|
||||
fn handle_create_order_request(
|
||||
&self, request_id: RequestId, counterparty_node_id: &PublicKey, params: CreateOrderRequest,
|
||||
&self, request_id: RequestId, counterparty_node_id: &PublicKey,
|
||||
params: LSPS1CreateOrderRequest,
|
||||
) -> Result<(), LightningError> {
|
||||
if !is_valid(¶ms.order, &self.config.supported_options.as_ref().unwrap()) {
|
||||
let response = LSPS1Response::CreateOrderError(ResponseError {
|
||||
|
@ -234,7 +236,7 @@ where
|
|||
///
|
||||
/// [`LSPS1ServiceEvent::RequestForPaymentDetails`]: crate::lsps1::event::LSPS1ServiceEvent::RequestForPaymentDetails
|
||||
pub fn send_payment_details(
|
||||
&self, request_id: RequestId, counterparty_node_id: &PublicKey, payment: PaymentInfo,
|
||||
&self, request_id: RequestId, counterparty_node_id: &PublicKey, payment: LSPS1PaymentInfo,
|
||||
created_at: chrono::DateTime<Utc>,
|
||||
) -> Result<(), APIError> {
|
||||
let (result, response) = {
|
||||
|
@ -256,10 +258,10 @@ where
|
|||
|
||||
peer_state_lock.insert_outbound_channel(order_id.clone(), channel);
|
||||
|
||||
let response = LSPS1Response::CreateOrder(CreateOrderResponse {
|
||||
let response = LSPS1Response::CreateOrder(LSPS1CreateOrderResponse {
|
||||
order: params.order,
|
||||
order_id,
|
||||
order_state: OrderState::Created,
|
||||
order_state: LSPS1OrderState::Created,
|
||||
created_at,
|
||||
payment,
|
||||
channel: None,
|
||||
|
@ -300,7 +302,8 @@ where
|
|||
}
|
||||
|
||||
fn handle_get_order_request(
|
||||
&self, request_id: RequestId, counterparty_node_id: &PublicKey, params: GetOrderRequest,
|
||||
&self, request_id: RequestId, counterparty_node_id: &PublicKey,
|
||||
params: LSPS1GetOrderRequest,
|
||||
) -> Result<(), LightningError> {
|
||||
let outer_state_lock = self.per_peer_state.read().unwrap();
|
||||
match outer_state_lock.get(counterparty_node_id) {
|
||||
|
@ -358,8 +361,8 @@ where
|
|||
///
|
||||
/// [`LSPS1ServiceEvent::CheckPaymentConfirmation`]: crate::lsps1::event::LSPS1ServiceEvent::CheckPaymentConfirmation
|
||||
pub fn update_order_status(
|
||||
&self, request_id: RequestId, counterparty_node_id: PublicKey, order_id: OrderId,
|
||||
order_state: OrderState, channel: Option<ChannelInfo>,
|
||||
&self, request_id: RequestId, counterparty_node_id: PublicKey, order_id: LSPS1OrderId,
|
||||
order_state: LSPS1OrderState, channel: Option<LSPS1ChannelInfo>,
|
||||
) -> Result<(), APIError> {
|
||||
let (result, response) = {
|
||||
let outer_state_lock = self.per_peer_state.read().unwrap();
|
||||
|
@ -373,7 +376,7 @@ where
|
|||
{
|
||||
let config = &outbound_channel.config;
|
||||
|
||||
let response = LSPS1Response::GetOrder(CreateOrderResponse {
|
||||
let response = LSPS1Response::GetOrder(LSPS1CreateOrderResponse {
|
||||
order_id,
|
||||
order: config.order.clone(),
|
||||
order_state,
|
||||
|
@ -411,9 +414,9 @@ where
|
|||
result
|
||||
}
|
||||
|
||||
fn generate_order_id(&self) -> OrderId {
|
||||
fn generate_order_id(&self) -> LSPS1OrderId {
|
||||
let bytes = self.entropy_source.get_secure_random_bytes();
|
||||
OrderId(utils::hex_str(&bytes[0..16]))
|
||||
LSPS1OrderId(utils::hex_str(&bytes[0..16]))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -457,7 +460,7 @@ fn check_range(min: u64, max: u64, value: u64) -> bool {
|
|||
(value >= min) && (value <= max)
|
||||
}
|
||||
|
||||
fn is_valid(order: &OrderParameters, options: &LSPS1Options) -> bool {
|
||||
fn is_valid(order: &LSPS1OrderParams, options: &LSPS1Options) -> bool {
|
||||
let bool = check_range(
|
||||
options.min_initial_client_balance_sat,
|
||||
options.max_initial_client_balance_sat,
|
||||
|
|
Loading…
Add table
Reference in a new issue