Skip to content

Commit a160bf2

Browse files
committed
Add Uniffi bindings for LSPS1 API
1 parent 5e8c263 commit a160bf2

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

bindings/ldk_node.udl

+73
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ interface Builder {
3939
void set_chain_source_bitcoind_rpc(string rpc_host, u16 rpc_port, string rpc_user, string rpc_password);
4040
void set_gossip_source_p2p();
4141
void set_gossip_source_rgs(string rgs_server_url);
42+
void set_liquidity_source_lsps1(PublicKey node_id, SocketAddress address, string? token);
4243
void set_liquidity_source_lsps2(PublicKey node_id, SocketAddress address, string? token);
4344
void set_storage_dir_path(string storage_dir_path);
4445
void set_network(Network network);
@@ -78,6 +79,7 @@ interface Node {
7879
SpontaneousPayment spontaneous_payment();
7980
OnchainPayment onchain_payment();
8081
UnifiedQrPayment unified_qr_payment();
82+
Lsps1Liquidity lsps1_liquidity();
8183
[Throws=NodeError]
8284
void connect(PublicKey node_id, SocketAddress address, boolean persist);
8385
[Throws=NodeError]
@@ -171,6 +173,13 @@ interface UnifiedQrPayment {
171173
QrPaymentResult send([ByRef]string uri_str);
172174
};
173175

176+
interface Lsps1Liquidity {
177+
[Throws=NodeError]
178+
LSPS1OrderStatus request_channel(u64 lsp_balance_sat, u64 client_balance_sat, u32 channel_expiry_blocks, boolean announce_channel);
179+
[Throws=NodeError]
180+
LSPS1OrderStatus check_order_status(OrderId order_id);
181+
};
182+
174183
[Error]
175184
enum NodeError {
176185
"AlreadyRunning",
@@ -217,6 +226,8 @@ enum NodeError {
217226
"InvalidUri",
218227
"InvalidQuantity",
219228
"InvalidNodeAlias",
229+
"InvalidTimestamp",
230+
"InvalidFeeRate",
220231
"DuplicatePayment",
221232
"UnsupportedCurrency",
222233
"InsufficientFunds",
@@ -362,6 +373,59 @@ dictionary SendingParameters {
362373
u8? max_channel_saturation_power_of_half;
363374
};
364375

376+
dictionary LSPS1OrderStatus {
377+
OrderId order_id;
378+
OrderParameters order_params;
379+
PaymentInfo payment_options;
380+
ChannelOrderInfo? channel_state;
381+
};
382+
383+
dictionary OrderParameters {
384+
u64 lsp_balance_sat;
385+
u64 client_balance_sat;
386+
u16 required_channel_confirmations;
387+
u16 funding_confirms_within_blocks;
388+
u32 channel_expiry_blocks;
389+
string? token;
390+
boolean announce_channel;
391+
};
392+
393+
dictionary PaymentInfo {
394+
Bolt11PaymentInfo? bolt11;
395+
OnchainPaymentInfo? onchain;
396+
};
397+
398+
dictionary Bolt11PaymentInfo {
399+
PaymentState state;
400+
DateTime expires_at;
401+
u64 fee_total_sat;
402+
u64 order_total_sat;
403+
Bolt11Invoice invoice;
404+
};
405+
406+
dictionary OnchainPaymentInfo {
407+
PaymentState state;
408+
DateTime expires_at;
409+
u64 fee_total_sat;
410+
u64 order_total_sat;
411+
Address address;
412+
u16? min_onchain_payment_confirmations;
413+
FeeRate min_fee_for_0conf;
414+
Address? refund_onchain_address;
415+
};
416+
417+
dictionary ChannelOrderInfo {
418+
DateTime funded_at;
419+
OutPoint funding_outpoint;
420+
DateTime expires_at;
421+
};
422+
423+
enum PaymentState {
424+
"ExpectPayment",
425+
"Paid",
426+
"Refunded",
427+
};
428+
365429
[Enum]
366430
interface MaxTotalRoutingFeeLimit {
367431
None ();
@@ -614,3 +678,12 @@ typedef string UntrustedString;
614678

615679
[Custom]
616680
typedef string NodeAlias;
681+
682+
[Custom]
683+
typedef string OrderId;
684+
685+
[Custom]
686+
typedef i64 DateTime;
687+
688+
[Custom]
689+
typedef string FeeRate;

src/error.rs

+6
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ pub enum Error {
104104
InvalidQuantity,
105105
/// The given node alias is invalid.
106106
InvalidNodeAlias,
107+
/// The given timestamp is invalid.
108+
InvalidTimestamp,
109+
/// The given fee rate is invalid.
110+
InvalidFeeRate,
107111
/// A payment with the given hash has already been initiated.
108112
DuplicatePayment,
109113
/// The provided offer was denonminated in an unsupported currency.
@@ -169,6 +173,8 @@ impl fmt::Display for Error {
169173
Self::InvalidUri => write!(f, "The given URI is invalid."),
170174
Self::InvalidQuantity => write!(f, "The given quantity is invalid."),
171175
Self::InvalidNodeAlias => write!(f, "The given node alias is invalid."),
176+
Self::InvalidTimestamp => write!(f, "The given timestamp is invalid."),
177+
Self::InvalidFeeRate => write!(f, "The given fee rate is invalid."),
172178
Self::DuplicatePayment => {
173179
write!(f, "A payment with the given hash has already been initiated.")
174180
},

src/uniffi_types.rs

+46-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub use crate::config::{
1414
default_config, AnchorChannelsConfig, EsploraSyncConfig, MaxDustHTLCExposure,
1515
};
1616
pub use crate::graph::{ChannelInfo, ChannelUpdateInfo, NodeAnnouncementInfo, NodeInfo};
17+
pub use crate::liquidity::LSPS1OrderStatus;
1718
pub use crate::payment::store::{LSPFeeLimits, PaymentDirection, PaymentKind, PaymentStatus};
1819
pub use crate::payment::{MaxTotalRoutingFeeLimit, QrPaymentResult, SendingParameters};
1920

@@ -28,12 +29,19 @@ pub use lightning::util::string::UntrustedString;
2829

2930
pub use lightning_invoice::Bolt11Invoice;
3031

31-
pub use bitcoin::{Address, BlockHash, Network, OutPoint, Txid};
32+
pub use lightning_liquidity::lsps1::msgs::ChannelInfo as ChannelOrderInfo;
33+
pub use lightning_liquidity::lsps1::msgs::{
34+
Bolt11PaymentInfo, OnchainPaymentInfo, OrderId, OrderParameters, PaymentInfo, PaymentState,
35+
};
36+
37+
pub use bitcoin::{Address, BlockHash, FeeRate, Network, OutPoint, Txid};
3238

3339
pub use bip39::Mnemonic;
3440

3541
pub use vss_client::headers::{VssHeaderProvider, VssHeaderProviderError};
3642

43+
pub type DateTime = chrono::DateTime<chrono::Utc>;
44+
3745
use crate::UniffiCustomTypeConverter;
3846

3947
use crate::builder::sanitize_alias;
@@ -343,3 +351,40 @@ impl UniffiCustomTypeConverter for NodeAlias {
343351
obj.to_string()
344352
}
345353
}
354+
355+
impl UniffiCustomTypeConverter for OrderId {
356+
type Builtin = String;
357+
358+
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
359+
Ok(Self(val))
360+
}
361+
362+
fn from_custom(obj: Self) -> Self::Builtin {
363+
obj.0
364+
}
365+
}
366+
367+
impl UniffiCustomTypeConverter for DateTime {
368+
type Builtin = i64;
369+
370+
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
371+
DateTime::from_timestamp(val, 0).ok_or(Error::InvalidTimestamp.into())
372+
}
373+
374+
fn from_custom(obj: Self) -> Self::Builtin {
375+
obj.timestamp()
376+
}
377+
}
378+
379+
/// FIXME TODO
380+
impl UniffiCustomTypeConverter for FeeRate {
381+
type Builtin = String;
382+
383+
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
384+
Ok(FeeRate::from_str(&val).map_err(|_| Error::InvalidFeeRate)?)
385+
}
386+
387+
fn from_custom(obj: Self) -> Self::Builtin {
388+
obj.to_string()
389+
}
390+
}

0 commit comments

Comments
 (0)