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

Commit 3aa158e

Browse files
committed
Added CRchannelmanager
1 parent 28db84c commit 3aa158e

File tree

8 files changed

+971
-107
lines changed

8 files changed

+971
-107
lines changed

src/channel_request/channel_manager.rs

Lines changed: 809 additions & 48 deletions
Large diffs are not rendered by default.

src/channel_request/event.rs

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,59 @@
1+
use bitcoin::secp256k1::PublicKey;
12

3+
use crate::transport::msgs::RequestId;
4+
use super::msgs::{OptionsSupported, ChannelInfo, Order, Payment};
25

36
#[derive(Clone, Debug, PartialEq, Eq)]
47
pub enum Event {
5-
// Request from the client for server info.
6-
GetInfo {},
7-
8+
89
// Information from the LSP regarding fees and channel parameters.
9-
GetInfoResponse {},
10+
// The client should validate if the LSP's parameters supported match
11+
// requirements.
12+
GetInfoResponse {
13+
channel_id: u128,
14+
15+
request_id: RequestId,
16+
17+
/// The node id of the LSP that provided this response.
18+
counterparty_node_id: PublicKey,
19+
20+
version: Vec<u16>,
21+
22+
website: String,
23+
24+
options_supported: OptionsSupported
25+
},
1026

1127
// Channel opening request from the client after selecting the LSP with desired parameters.
1228
// Client selects the fee and channel parameters and requests the LSP to create a channel.
13-
CreateChannel {},
29+
// LSP should check the validity of the create channel order.
30+
CreateInvoice {
31+
request_id: RequestId,
1432

15-
// LSP accepts the request parameters and sends an onchain address and invoice along with channel
16-
// parameters to the client.
17-
Order {},
33+
counterparty_node_id: PublicKey,
34+
35+
order: Order,
36+
},
1837

19-
// Client confirms the parameters and pays the LSP through onchain or lightning payment.
20-
PaymentforChannel {},
38+
// LSP accepts the request parameters and sends an onchain address and invoice along with channel
39+
// parameters to the client. After payment by the client this event should be updated,
40+
// to show the LSP to poll for the payment now.
41+
PayforChannel {
42+
request_id: RequestId,
43+
counterparty_node_id: PublicKey,
44+
order: Order,
45+
payment: Payment,
46+
channel: Option<ChannelInfo>,
47+
},
2148

22-
// Waits for the payment to confirm and till then the order status can be retrived.
23-
GetOrderStatus {},
2449

25-
// Checks whether the payment is confirmed and updates the order.
26-
ConfirmPayment {},
50+
UpdatePaymentStatus {},
2751

28-
// On payment confirmation, channel is opened.
52+
// On payment confirmation, channel is opened. After payment confirms,
53+
// LSP should open a channel and open to client.
2954
OpenChannel {},
3055

3156
// If order fails, refund is initiated.
57+
//
3258
Refund {},
3359
}

src/channel_request/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
//! Types and primitives that implement the LSPS1: Channel Request specification.
1111
pub mod channel_manager;
1212
pub mod event;
13-
pub mod msgs;
13+
pub mod msgs;
14+
mod utils;

src/channel_request/msgs.rs

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11

2+
use std::convert::TryFrom;
3+
24
use serde::{Deserialize, Serialize};
35

46
//use bitcoin::hashes::hmac::{Hmac, HmacEngine};
57
//use bitcoin::hashes::sha256::Hash as Sha256;
68
//use bitcoin::hashes::{Hash, HashEngine};
7-
use crate::transport::msgs::{RequestId, ResponseError};
9+
use crate::transport::msgs::{RequestId, ResponseError, LSPSMessage};
810
use crate::utils;
911

1012
pub(crate) const LSPS1_GETINFO_METHOD_NAME: &str = "lsps1.getinfo";
1113
pub(crate) const LSPS1_CREATE_ORDER_METHOD_NAME: &str = "lsps1.create_order";
1214
pub(crate) const LSPS1_GET_ORDER_METHOD_NAME: &str = "lsps1.get_order";
1315

16+
17+
pub(crate) const REFUND_ONCHAIN_ADDRESS: bool = false;
18+
19+
// Create a const to show preferred way for user payment
20+
// Should this be set everytime before payment?
21+
// Ask user for lighting or onchain and then set the const to
22+
// lightning or onchain
23+
24+
25+
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Hash)]
26+
pub struct OrderId(pub String);
27+
28+
1429
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)]
1530
#[serde(default)]
1631
pub struct GetInfoRequest {}
@@ -35,72 +50,56 @@ pub struct OptionsSupported {
3550
pub struct GetInfoResponse {
3651
pub supported_versions: Vec<u16>,
3752
pub website: String,
38-
pub options: Vec<OptionsSupported>,
53+
pub options: OptionsSupported,
3954
}
4055

41-
impl GetInfoResponse {
42-
fn is_valid(&self) -> bool {
43-
// TODO check validity of min < max for every pair
44-
// Use of hmacengine/hash for checking
45-
1
46-
}
47-
}
4856

4957
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
5058
pub struct CreateOrderRequest {
51-
pub api_version: u8,
52-
pub lsp_balance_sat: String,
53-
pub client_balance_sat: String,
54-
pub confirms_within_blocks: u8,
55-
pub channel_expiry_blocks: u8,
56-
pub token: String,
57-
// String of Onchain address, maybe object- String/bech32,
58-
pub refund_onchain_address: String,
59-
pub announce_channel: bool,
60-
}
61-
62-
impl CreateOrderRequest {
63-
fn is_valid(&self, req: &GetInfoResponse) -> bool {
64-
// implement the conditions
65-
}
59+
pub order: Order,
6660
}
6761

6862
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
6963
pub struct Order {
70-
pub order_id: String,
64+
pub order_id: Option<OrderId>,
7165
pub api_version: u16,
7266
pub lsp_balance_sat: u64,
7367
pub client_balance_sat: u64,
7468
pub confirms_within_blocks: u32,
7569
pub channel_expiry_blocks: u32,
7670
pub token: String,
77-
pub created_at: String,
78-
pub expires_at: String,
7971
pub announce_channel: bool,
72+
pub refund_onchain_address: Option<String>,
8073
pub order_state: OrderState,
81-
pub payment: Payment,
82-
pub channel: Option<Channel>,
8374
}
8475

85-
8676
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
87-
pub struct CreateOrderResponse{
88-
pub response: Order,
77+
pub struct CreateOrderResponse {
78+
pub order: Order,
79+
pub created_at: String,
80+
pub expires_at: String,
81+
pub payment: Payment,
82+
pub channel: Option<ChannelInfo>,
8983
}
9084

9185
impl CreateOrderResponse {
92-
fn new(request: &CreateOrderRequest) -> () {
86+
// import datetime and set to time to creaetd_at.
87+
pub fn new(order: &mut Order, fee: u64, bolt11_invoice: String,
88+
onchain_address: String, options: OptionsSupported) -> Self {
9389
// Few of the parameters are mirrored from the orderrequest.
94-
}
95-
96-
fn is_valid(&self) -> () {
97-
// Check the following conditions
98-
90+
91+
let response = CreateOrderResponse {
92+
order: request.order,
93+
payment,
94+
channel: None,
95+
};
96+
response
9997
}
10098
}
10199

102100
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
103101
pub enum OrderState {
102+
Requested,
104103
Created,
105104
Completed,
106105
Failed,
@@ -134,8 +133,8 @@ pub struct OnchainPayment{
134133
}
135134

136135
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
137-
pub struct Channel {
138-
pub state: ChannelState,
136+
pub struct ChannelInfo {
137+
pub state: ChannelStatus,
139138
pub funded_at: String,
140139
pub funding_outpoint: String,
141140
pub scid: Option<String>,
@@ -145,15 +144,15 @@ pub struct Channel {
145144
}
146145

147146
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
148-
pub enum ChannelState {
147+
pub enum ChannelStatus {
149148
Opening,
150149
Opened,
151150
Closed,
152151
}
153152

154153
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
155154
pub struct GetOrderRequest {
156-
pub order_id: String,
155+
pub order_id: OrderId,
157156
}
158157

159158
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
@@ -193,3 +192,21 @@ pub enum Message {
193192
Request(RequestId, Request),
194193
Response(RequestId, Response),
195194
}
195+
196+
impl TryFrom<LSPSMessage> for Message {
197+
type Error = ();
198+
199+
fn try_from(message: LSPSMessage) -> Result<Self, Self::Error> {
200+
if let LSPSMessage::LSPS1(message) = message {
201+
return Ok(message);
202+
}
203+
204+
Err(())
205+
}
206+
}
207+
208+
impl From<Message> for LSPSMessage {
209+
fn from(message: Message) -> Self {
210+
LSPSMessage::LSPS1(message)
211+
}
212+
}

src/channel_request/utils.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
// Need to add more conditions
3+
4+
pub fn check_if_valid(value: u128, upper: u128, lower: u128) -> () {
5+
let range = lower..upper;
6+
assert!(range.contains(&value));
7+
}
8+

src/events.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use crate::channel_request;
2+
3+
/// An Event which you should probably take some action in response to.
4+
#[derive(Debug, Clone, PartialEq, Eq)]
5+
pub enum Event {
6+
/// A LSPS1 (JIT Channel) protocol event
7+
LSPS1(channel_request::event::Event),
8+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ mod channel_request;
2222
mod jit_channel;
2323
mod transport;
2424
mod utils;
25+
pub mod events;
2526

2627
pub use transport::message_handler::{LiquidityManager, LiquidityProviderConfig};

src/transport/msgs.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl wire::Type for RawLSPSMessage {
3737
}
3838
}
3939

40-
#[derive(Clone, Debug, PartialEq, Eq)]
40+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
4141
pub struct RequestId(pub String);
4242

4343
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
@@ -88,6 +88,7 @@ impl TryFrom<LSPSMessage> for LSPS0Message {
8888
match message {
8989
LSPSMessage::Invalid => Err(()),
9090
LSPSMessage::LSPS0(message) => Ok(message),
91+
LSPSMessage::LSPS1(_) => Err(()),
9192
}
9293
}
9394
}
@@ -160,6 +161,47 @@ impl Serialize for LSPSMessage {
160161
}
161162
}
162163
}
164+
165+
LSPSMessage::LSPS1(channel_request::msgs::Message::Request(request_id, request)) => {
166+
jsonrpc_object.serialize_field(JSONRPC_ID_FIELD_KEY, &request_id.0)?;
167+
jsonrpc_object.serialize_field(JSONRPC_METHOD_FIELD_KEY, request.method())?;
168+
169+
match request {
170+
channel_request::msgs::Request::GetInfo(params) => {
171+
jsonrpc_object.serialize_field(JSONRPC_PARAMS_FIELD_KEY, params)?
172+
}
173+
channel_request::msgs::Request::CreateOrder(params) => {
174+
jsonrpc_object.serialize_field(JSONRPC_PARAMS_FIELD_KEY, params)?
175+
}
176+
channel_request::msgs::Request::GetOrder(params) => {
177+
jsonrpc_object.serialize_field(JSONRPC_PARAMS_FIELD_KEY, params)?
178+
}
179+
}
180+
}
181+
LSPSMessage::LSPS1(channel_request::msgs::Message::Response(request_id, response)) => {
182+
jsonrpc_object.serialize_field(JSONRPC_ID_FIELD_KEY, &request_id.0)?;
183+
184+
match response {
185+
channel_request::msgs::Response::GetInfo(result) => {
186+
jsonrpc_object.serialize_field(JSONRPC_RESULT_FIELD_KEY, result)?
187+
}
188+
channel_request::msgs::Response::GetInfoError(error) => {
189+
jsonrpc_object.serialize_field(JSONRPC_RESULT_FIELD_KEY, error)?
190+
}
191+
channel_request::msgs::Response::CreateOrder(result) => {
192+
jsonrpc_object.serialize_field(JSONRPC_ERROR_FIELD_KEY, result)?
193+
}
194+
channel_request::msgs::Response::OrderError(error) => {
195+
jsonrpc_object.serialize_field(JSONRPC_RESULT_FIELD_KEY, error)?
196+
}
197+
channel_request::msgs::Response::GetOrder(result) => {
198+
jsonrpc_object.serialize_field(JSONRPC_ERROR_FIELD_KEY, result)?
199+
}
200+
&channel_request::msgs::Response::GetOrderError(error) => {
201+
jsonrpc_object.serialize_field(JSONRPC_ERROR_FIELD_KEY, error)?
202+
}
203+
}
204+
}
163205
LSPSMessage::Invalid => {
164206
let error = ResponseError {
165207
code: JSONRPC_INVALID_MESSAGE_ERROR_CODE,

0 commit comments

Comments
 (0)