Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit e7d64b4

Browse files
committed
Update message types to include payment options
.. as of LSPS#101.
1 parent e57703c commit e7d64b4

File tree

3 files changed

+122
-70
lines changed

3 files changed

+122
-70
lines changed

src/lsps1/client.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,12 @@ where
447447
return Err(e);
448448
}
449449

450-
let total_fees = response.payment.fee_total_sat + response.order.client_balance_sat;
450+
// FIXME: Account for the individual payment types properly.
451+
let total_fees = response.payment.bolt11.as_ref().map_or(0, |p| p.fee_total_sat)
452+
+ response.order.client_balance_sat;
451453
let max_channel_fees_msat = self.config.max_channel_fees_msat.unwrap_or(u64::MAX);
452454

453-
if total_fees == response.payment.order_total_sat
455+
if total_fees == response.payment.bolt11.as_ref().map_or(0, |p| p.order_total_sat)
454456
&& total_fees < max_channel_fees_msat
455457
{
456458
self.pending_events.enqueue(Event::LSPS1Client(

src/lsps1/msgs.rs

Lines changed: 114 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,8 @@ pub struct OptionsSupported {
4343
pub min_required_channel_confirmations: u16,
4444
/// The smallest number of blocks in which the LSP can confirm the funding transaction.
4545
pub min_funding_confirms_within_blocks: u16,
46-
/// The minimum number of block confirmations before the LSP accepts an on-chain payment as confirmed.
47-
pub min_onchain_payment_confirmations: Option<u16>,
4846
/// Indicates if the LSP supports zero reserve.
4947
pub supports_zero_channel_reserve: bool,
50-
/// Indicates the minimum amount of satoshi that is required for the LSP to accept a payment
51-
/// on-chain.
52-
#[serde(with = "string_amount_option")]
53-
pub min_onchain_payment_size_sat: Option<u64>,
5448
/// The maximum number of blocks a channel can be leased for.
5549
pub max_channel_expiry_blocks: u32,
5650
/// The minimum number of satoshi that the client MUST request.
@@ -77,6 +71,7 @@ pub struct OptionsSupported {
7771
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
7872
pub struct GetInfoResponse {
7973
/// All options supported by the LSP.
74+
#[serde(flatten)]
8075
pub options: OptionsSupported,
8176
}
8277

@@ -127,8 +122,6 @@ pub struct CreateOrderResponse {
127122
pub order: OrderParams,
128123
/// The datetime when the order was created
129124
pub created_at: chrono::DateTime<Utc>,
130-
/// The datetime when the order expires.
131-
pub expires_at: chrono::DateTime<Utc>,
132125
/// The current state of the order.
133126
pub order_state: OrderState,
134127
/// Contains details about how to pay for the order.
@@ -152,34 +145,58 @@ pub enum OrderState {
152145
/// Details regarding how to pay for an order.
153146
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
154147
pub struct PaymentInfo {
148+
/// A Lightning payment using BOLT 11.
149+
pub bolt11: Option<Bolt11PaymentInfo>,
150+
/// An onchain payment.
151+
pub onchain: Option<OnchainPaymentInfo>,
152+
}
153+
154+
/// A Lightning payment using BOLT 11.
155+
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
156+
pub struct Bolt11PaymentInfo {
155157
/// Indicates the current state of the payment.
156-
pub state: PaymentState,
158+
pub state: Bolt11PaymentState,
159+
/// The datetime when the payment option expires.
160+
pub expires_at: chrono::DateTime<Utc>,
157161
/// The total fee the LSP will charge to open this channel in satoshi.
158162
#[serde(with = "string_amount")]
159163
pub fee_total_sat: u64,
160-
/// What the client needs to pay in total to open the requested channel.
164+
/// The amount the client needs to pay to have the requested channel openend.
161165
#[serde(with = "string_amount")]
162166
pub order_total_sat: u64,
163167
/// A BOLT11 invoice the client can pay to have to channel opened.
164-
pub bolt11_invoice: Bolt11Invoice,
168+
pub invoice: Bolt11Invoice,
169+
}
170+
171+
/// An onchain payment.
172+
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
173+
pub struct OnchainPaymentInfo {
174+
/// Indicates the current state of the payment.
175+
pub state: OnchainPaymentState,
176+
/// The datetime when the payment option expires.
177+
pub expires_at: chrono::DateTime<Utc>,
178+
/// The total fee the LSP will charge to open this channel in satoshi.
179+
#[serde(with = "string_amount")]
180+
pub fee_total_sat: u64,
181+
/// The amount the client needs to pay to have the requested channel openend.
182+
#[serde(with = "string_amount")]
183+
pub order_total_sat: u64,
165184
/// An on-chain address the client can send [`Self::order_total_sat`] to to have the channel
166185
/// opened.
167-
pub onchain_address: Address<NetworkUnchecked>,
186+
pub address: Address<NetworkUnchecked>,
168187
/// The minimum number of block confirmations that are required for the on-chain payment to be
169188
/// considered confirmed.
170189
pub min_onchain_payment_confirmations: Option<u16>,
171190
/// The minimum fee rate for the on-chain payment in case the client wants the payment to be
172191
/// confirmed without a confirmation.
173192
#[serde(with = "u32_fee_rate")]
174193
pub min_fee_for_0conf: FeeRate,
175-
/// Details regarding a detected on-chain payment.
176-
pub onchain_payment: Option<OnchainPayment>,
177194
}
178195

179-
/// The state of an [`PaymentInfo`].
196+
/// The state of a BOLT 11 payment.
180197
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
181198
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
182-
pub enum PaymentState {
199+
pub enum Bolt11PaymentState {
183200
/// A payment is expected.
184201
ExpectPayment,
185202
/// A Lighting payment has arrived, but the preimage has not been released yet.
@@ -188,6 +205,20 @@ pub enum PaymentState {
188205
Paid,
189206
/// The payment has been refunded.
190207
Refunded,
208+
/// The payment has been cancelled.
209+
Cancelled,
210+
}
211+
212+
/// The state of an onchain payment.
213+
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
214+
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
215+
pub enum OnchainPaymentState {
216+
/// A payment is expected.
217+
ExpectPayment,
218+
/// A sufficient payment has been received.
219+
Paid,
220+
/// The payment has been refunded.
221+
Refunded,
191222
}
192223

193224
/// Details regarding a detected on-chain payment.
@@ -287,9 +318,7 @@ mod tests {
287318
fn options_supported_serialization() {
288319
let min_required_channel_confirmations = 0;
289320
let min_funding_confirms_within_blocks = 6;
290-
let min_onchain_payment_confirmations = Some(6);
291321
let supports_zero_channel_reserve = true;
292-
let min_onchain_payment_size_sat = Some(100_000);
293322
let max_channel_expiry_blocks = 144;
294323
let min_initial_client_balance_sat = 10_000_000;
295324
let max_initial_client_balance_sat = 100_000_000;
@@ -301,9 +330,7 @@ mod tests {
301330
let options_supported = OptionsSupported {
302331
min_required_channel_confirmations,
303332
min_funding_confirms_within_blocks,
304-
min_onchain_payment_confirmations,
305333
supports_zero_channel_reserve,
306-
min_onchain_payment_size_sat,
307334
max_channel_expiry_blocks,
308335
min_initial_client_balance_sat,
309336
max_initial_client_balance_sat,
@@ -313,7 +340,7 @@ mod tests {
313340
max_channel_balance_sat,
314341
};
315342

316-
let json_str = r#"{"max_channel_balance_sat":"100000000","max_channel_expiry_blocks":144,"max_initial_client_balance_sat":"100000000","max_initial_lsp_balance_sat":"100000000","min_channel_balance_sat":"100000","min_funding_confirms_within_blocks":6,"min_initial_client_balance_sat":"10000000","min_initial_lsp_balance_sat":"100000","min_onchain_payment_confirmations":6,"min_onchain_payment_size_sat":"100000","min_required_channel_confirmations":0,"supports_zero_channel_reserve":true}"#;
343+
let json_str = r#"{"max_channel_balance_sat":"100000000","max_channel_expiry_blocks":144,"max_initial_client_balance_sat":"100000000","max_initial_lsp_balance_sat":"100000000","min_channel_balance_sat":"100000","min_funding_confirms_within_blocks":6,"min_initial_client_balance_sat":"10000000","min_initial_lsp_balance_sat":"100000","min_required_channel_confirmations":0,"supports_zero_channel_reserve":true}"#;
317344

318345
assert_eq!(json_str, serde_json::json!(options_supported).to_string());
319346
assert_eq!(options_supported, serde_json::from_str(json_str).unwrap());
@@ -326,20 +353,16 @@ mod tests {
326353
let _get_info_request: GetInfoRequest = serde_json::from_str(json_str).unwrap();
327354

328355
let json_str = r#"{
329-
"options": {
330-
"min_required_channel_confirmations": 0,
331-
"min_funding_confirms_within_blocks" : 6,
332-
"min_onchain_payment_confirmations": null,
333-
"supports_zero_channel_reserve": true,
334-
"min_onchain_payment_size_sat": null,
335-
"max_channel_expiry_blocks": 20160,
336-
"min_initial_client_balance_sat": "20000",
337-
"max_initial_client_balance_sat": "100000000",
338-
"min_initial_lsp_balance_sat": "0",
339-
"max_initial_lsp_balance_sat": "100000000",
340-
"min_channel_balance_sat": "50000",
341-
"max_channel_balance_sat": "100000000"
342-
}
356+
"min_required_channel_confirmations": 0,
357+
"min_funding_confirms_within_blocks" : 6,
358+
"supports_zero_channel_reserve": true,
359+
"max_channel_expiry_blocks": 20160,
360+
"min_initial_client_balance_sat": "20000",
361+
"max_initial_client_balance_sat": "100000000",
362+
"min_initial_lsp_balance_sat": "0",
363+
"max_initial_lsp_balance_sat": "100000000",
364+
"min_channel_balance_sat": "50000",
365+
"max_channel_balance_sat": "100000000"
343366
}"#;
344367
let _get_info_response: GetInfoResponse = serde_json::from_str(json_str).unwrap();
345368

@@ -355,6 +378,46 @@ mod tests {
355378
}"#;
356379
let _create_order_request: CreateOrderRequest = serde_json::from_str(json_str).unwrap();
357380

381+
let json_str = r#"{
382+
"state" : "EXPECT_PAYMENT",
383+
"expires_at": "2025-01-01T00:00:00Z",
384+
"fee_total_sat": "8888",
385+
"order_total_sat": "200888",
386+
"invoice": "lnbc252u1p3aht9ysp580g4633gd2x9lc5al0wd8wx0mpn9748jeyz46kqjrpxn52uhfpjqpp5qgf67tcqmuqehzgjm8mzya90h73deafvr4m5705l5u5l4r05l8cqdpud3h8ymm4w3jhytnpwpczqmt0de6xsmre2pkxzm3qydmkzdjrdev9s7zhgfaqxqyjw5qcqpjrzjqt6xptnd85lpqnu2lefq4cx070v5cdwzh2xlvmdgnu7gqp4zvkus5zapryqqx9qqqyqqqqqqqqqqqcsq9q9qyysgqen77vu8xqjelum24hgjpgfdgfgx4q0nehhalcmuggt32japhjuksq9jv6eksjfnppm4hrzsgyxt8y8xacxut9qv3fpyetz8t7tsymygq8yzn05"
387+
}"#;
388+
let _bolt11_payment: Bolt11PaymentInfo = serde_json::from_str(json_str).unwrap();
389+
390+
let json_str = r#"{
391+
"state": "EXPECT_PAYMENT",
392+
"expires_at": "2025-01-01T00:00:00Z",
393+
"fee_total_sat": "9999",
394+
"order_total_sat": "200999",
395+
"address": "bc1p5uvtaxzkjwvey2tfy49k5vtqfpjmrgm09cvs88ezyy8h2zv7jhas9tu4yr",
396+
"min_onchain_payment_confirmations": 1,
397+
"min_fee_for_0conf": 253
398+
}"#;
399+
let _onchain_payment: OnchainPaymentInfo = serde_json::from_str(json_str).unwrap();
400+
401+
let json_str = r#"{
402+
"bolt11": {
403+
"state" : "EXPECT_PAYMENT",
404+
"expires_at": "2025-01-01T00:00:00Z",
405+
"fee_total_sat": "8888",
406+
"order_total_sat": "200888",
407+
"invoice": "lnbc252u1p3aht9ysp580g4633gd2x9lc5al0wd8wx0mpn9748jeyz46kqjrpxn52uhfpjqpp5qgf67tcqmuqehzgjm8mzya90h73deafvr4m5705l5u5l4r05l8cqdpud3h8ymm4w3jhytnpwpczqmt0de6xsmre2pkxzm3qydmkzdjrdev9s7zhgfaqxqyjw5qcqpjrzjqt6xptnd85lpqnu2lefq4cx070v5cdwzh2xlvmdgnu7gqp4zvkus5zapryqqx9qqqyqqqqqqqqqqqcsq9q9qyysgqen77vu8xqjelum24hgjpgfdgfgx4q0nehhalcmuggt32japhjuksq9jv6eksjfnppm4hrzsgyxt8y8xacxut9qv3fpyetz8t7tsymygq8yzn05"
408+
},
409+
"onchain": {
410+
"state": "EXPECT_PAYMENT",
411+
"expires_at": "2025-01-01T00:00:00Z",
412+
"fee_total_sat": "9999",
413+
"order_total_sat": "200999",
414+
"address": "bc1p5uvtaxzkjwvey2tfy49k5vtqfpjmrgm09cvs88ezyy8h2zv7jhas9tu4yr",
415+
"min_onchain_payment_confirmations": 1,
416+
"min_fee_for_0conf": 253
417+
}
418+
}"#;
419+
let _payment: PaymentInfo = serde_json::from_str(json_str).unwrap();
420+
358421
let json_str = r#"{
359422
"order_id": "bb4b5d0a-8334-49d8-9463-90a6d413af7c",
360423
"lsp_balance_sat": "5000000",
@@ -364,18 +427,25 @@ mod tests {
364427
"channel_expiry_blocks": 12,
365428
"token": "",
366429
"created_at": "2012-04-23T18:25:43.511Z",
367-
"expires_at": "2015-01-25T19:29:44.612Z",
368430
"announce_channel": true,
369431
"order_state": "CREATED",
370432
"payment": {
371-
"state": "EXPECT_PAYMENT",
372-
"fee_total_sat": "8888",
373-
"order_total_sat": "2008888",
374-
"bolt11_invoice": "lnbc252u1p3aht9ysp580g4633gd2x9lc5al0wd8wx0mpn9748jeyz46kqjrpxn52uhfpjqpp5qgf67tcqmuqehzgjm8mzya90h73deafvr4m5705l5u5l4r05l8cqdpud3h8ymm4w3jhytnpwpczqmt0de6xsmre2pkxzm3qydmkzdjrdev9s7zhgfaqxqyjw5qcqpjrzjqt6xptnd85lpqnu2lefq4cx070v5cdwzh2xlvmdgnu7gqp4zvkus5zapryqqx9qqqyqqqqqqqqqqqcsq9q9qyysgqen77vu8xqjelum24hgjpgfdgfgx4q0nehhalcmuggt32japhjuksq9jv6eksjfnppm4hrzsgyxt8y8xacxut9qv3fpyetz8t7tsymygq8yzn05",
375-
"onchain_address": "bc1p5uvtaxzkjwvey2tfy49k5vtqfpjmrgm09cvs88ezyy8h2zv7jhas9tu4yr",
376-
"min_onchain_payment_confirmations": 0,
377-
"min_fee_for_0conf": 253,
378-
"onchain_payment": null
433+
"bolt11": {
434+
"state": "EXPECT_PAYMENT",
435+
"expires_at": "2015-01-25T19:29:44.612Z",
436+
"fee_total_sat": "8888",
437+
"order_total_sat": "2008888",
438+
"invoice" : "lnbc252u1p3aht9ysp580g4633gd2x9lc5al0wd8wx0mpn9748jeyz46kqjrpxn52uhfpjqpp5qgf67tcqmuqehzgjm8mzya90h73deafvr4m5705l5u5l4r05l8cqdpud3h8ymm4w3jhytnpwpczqmt0de6xsmre2pkxzm3qydmkzdjrdev9s7zhgfaqxqyjw5qcqpjrzjqt6xptnd85lpqnu2lefq4cx070v5cdwzh2xlvmdgnu7gqp4zvkus5zapryqqx9qqqyqqqqqqqqqqqcsq9q9qyysgqen77vu8xqjelum24hgjpgfdgfgx4q0nehhalcmuggt32japhjuksq9jv6eksjfnppm4hrzsgyxt8y8xacxut9qv3fpyetz8t7tsymygq8yzn05"
439+
},
440+
"onchain": {
441+
"state": "EXPECT_PAYMENT",
442+
"expires_at": "2015-01-25T19:29:44.612Z",
443+
"fee_total_sat": "9999",
444+
"order_total_sat": "2009999",
445+
"address": "bc1qvmsy0f3yyes6z9jvddk8xqwznndmdwapvrc0xrmhd3vqj5rhdrrq6hz49h",
446+
"min_fee_for_0conf": 253,
447+
"min_onchain_payment_confirmations": 0
448+
}
379449
},
380450
"channel": null
381451
}"#;
@@ -386,22 +456,6 @@ mod tests {
386456
}"#;
387457
let _get_order_request: GetOrderRequest = serde_json::from_str(json_str).unwrap();
388458

389-
let json_str = r#"{
390-
"state": "EXPECT_PAYMENT",
391-
"fee_total_sat": "8888",
392-
"order_total_sat": "2008888",
393-
"bolt11_invoice": "lnbc252u1p3aht9ysp580g4633gd2x9lc5al0wd8wx0mpn9748jeyz46kqjrpxn52uhfpjqpp5qgf67tcqmuqehzgjm8mzya90h73deafvr4m5705l5u5l4r05l8cqdpud3h8ymm4w3jhytnpwpczqmt0de6xsmre2pkxzm3qydmkzdjrdev9s7zhgfaqxqyjw5qcqpjrzjqt6xptnd85lpqnu2lefq4cx070v5cdwzh2xlvmdgnu7gqp4zvkus5zapryqqx9qqqyqqqqqqqqqqqcsq9q9qyysgqen77vu8xqjelum24hgjpgfdgfgx4q0nehhalcmuggt32japhjuksq9jv6eksjfnppm4hrzsgyxt8y8xacxut9qv3fpyetz8t7tsymygq8yzn05",
394-
"onchain_address": "bc1p5uvtaxzkjwvey2tfy49k5vtqfpjmrgm09cvs88ezyy8h2zv7jhas9tu4yr",
395-
"min_onchain_payment_confirmations": 1,
396-
"min_fee_for_0conf": 253,
397-
"onchain_payment": {
398-
"outpoint": "0301e0480b374b32851a9462db29dc19fe830a7f7d7a88b81612b9d42099c0ae:1",
399-
"sat": "1200",
400-
"confirmed": false
401-
}
402-
}"#;
403-
let _payment: PaymentInfo = serde_json::from_str(json_str).unwrap();
404-
405459
let json_str = r#"{
406460
"funded_at": "2012-04-23T18:25:43.511Z",
407461
"funding_outpoint": "0301e0480b374b32851a9462db29dc19fe830a7f7d7a88b81612b9d42099c0ae:0",

src/lsps1/service.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ impl OutboundRequestState {
7474
struct OutboundLSPS1Config {
7575
order: OrderParams,
7676
created_at: chrono::DateTime<Utc>,
77-
expires_at: chrono::DateTime<Utc>,
7877
payment: PaymentInfo,
7978
}
8079

@@ -85,12 +84,12 @@ struct OutboundCRChannel {
8584

8685
impl OutboundCRChannel {
8786
fn new(
88-
order: OrderParams, created_at: chrono::DateTime<Utc>, expires_at: chrono::DateTime<Utc>,
89-
order_id: OrderId, payment: PaymentInfo,
87+
order: OrderParams, created_at: chrono::DateTime<Utc>, order_id: OrderId,
88+
payment: PaymentInfo,
9089
) -> Self {
9190
Self {
9291
state: OutboundRequestState::OrderCreated { order_id },
93-
config: OutboundLSPS1Config { order, created_at, expires_at, payment },
92+
config: OutboundLSPS1Config { order, created_at, payment },
9493
}
9594
}
9695
fn awaiting_payment(&mut self) -> Result<(), LightningError> {
@@ -239,7 +238,7 @@ where
239238
/// [`LSPS1ServiceEvent::RequestForPaymentDetails`]: crate::lsps1::event::LSPS1ServiceEvent::RequestForPaymentDetails
240239
pub fn send_payment_details(
241240
&self, request_id: RequestId, counterparty_node_id: &PublicKey, payment: PaymentInfo,
242-
created_at: chrono::DateTime<Utc>, expires_at: chrono::DateTime<Utc>,
241+
created_at: chrono::DateTime<Utc>,
243242
) -> Result<(), APIError> {
244243
let (result, response) = {
245244
let outer_state_lock = self.per_peer_state.read().unwrap();
@@ -254,7 +253,6 @@ where
254253
let channel = OutboundCRChannel::new(
255254
params.order.clone(),
256255
created_at.clone(),
257-
expires_at.clone(),
258256
order_id.clone(),
259257
payment.clone(),
260258
);
@@ -266,7 +264,6 @@ where
266264
order_id,
267265
order_state: OrderState::Created,
268266
created_at,
269-
expires_at,
270267
payment,
271268
channel: None,
272269
});
@@ -386,7 +383,6 @@ where
386383
order: config.order.clone(),
387384
order_state,
388385
created_at: config.created_at,
389-
expires_at: config.expires_at,
390386
payment: config.payment.clone(),
391387
channel,
392388
});

0 commit comments

Comments
 (0)