Skip to content

Commit

Permalink
Add Uniffi bindings for LSPS1 API
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Dec 4, 2024
1 parent 5e8c263 commit d0eb977
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
73 changes: 73 additions & 0 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface Builder {
void set_chain_source_bitcoind_rpc(string rpc_host, u16 rpc_port, string rpc_user, string rpc_password);
void set_gossip_source_p2p();
void set_gossip_source_rgs(string rgs_server_url);
void set_liquidity_source_lsps1(PublicKey node_id, SocketAddress address, string? token);
void set_liquidity_source_lsps2(PublicKey node_id, SocketAddress address, string? token);
void set_storage_dir_path(string storage_dir_path);
void set_network(Network network);
Expand Down Expand Up @@ -78,6 +79,7 @@ interface Node {
SpontaneousPayment spontaneous_payment();
OnchainPayment onchain_payment();
UnifiedQrPayment unified_qr_payment();
Lsps1Liquidity lsps1_liquidity();
[Throws=NodeError]
void connect(PublicKey node_id, SocketAddress address, boolean persist);
[Throws=NodeError]
Expand Down Expand Up @@ -171,6 +173,13 @@ interface UnifiedQrPayment {
QrPaymentResult send([ByRef]string uri_str);
};

interface Lsps1Liquidity {
[Throws=NodeError]
LSPS1OrderStatus request_channel(u64 lsp_balance_sat, u64 client_balance_sat, u32 channel_expiry_blocks, boolean announce_channel);
[Throws=NodeError]
LSPS1OrderStatus check_order_status(OrderId order_id);
};

[Error]
enum NodeError {
"AlreadyRunning",
Expand Down Expand Up @@ -217,6 +226,8 @@ enum NodeError {
"InvalidUri",
"InvalidQuantity",
"InvalidNodeAlias",
"InvalidDateTime",
"InvalidFeeRate",
"DuplicatePayment",
"UnsupportedCurrency",
"InsufficientFunds",
Expand Down Expand Up @@ -362,6 +373,59 @@ dictionary SendingParameters {
u8? max_channel_saturation_power_of_half;
};

dictionary LSPS1OrderStatus {
OrderId order_id;
OrderParameters order_params;
PaymentInfo payment_options;
ChannelOrderInfo? channel_state;
};

dictionary OrderParameters {
u64 lsp_balance_sat;
u64 client_balance_sat;
u16 required_channel_confirmations;
u16 funding_confirms_within_blocks;
u32 channel_expiry_blocks;
string? token;
boolean announce_channel;
};

dictionary PaymentInfo {
Bolt11PaymentInfo? bolt11;
OnchainPaymentInfo? onchain;
};

dictionary Bolt11PaymentInfo {
PaymentState state;
DateTime expires_at;
u64 fee_total_sat;
u64 order_total_sat;
Bolt11Invoice invoice;
};

dictionary OnchainPaymentInfo {
PaymentState state;
DateTime expires_at;
u64 fee_total_sat;
u64 order_total_sat;
Address address;
u16? min_onchain_payment_confirmations;
FeeRate min_fee_for_0conf;
Address? refund_onchain_address;
};

dictionary ChannelOrderInfo {
DateTime funded_at;
OutPoint funding_outpoint;
DateTime expires_at;
};

enum PaymentState {
"ExpectPayment",
"Paid",
"Refunded",
};

[Enum]
interface MaxTotalRoutingFeeLimit {
None ();
Expand Down Expand Up @@ -614,3 +678,12 @@ typedef string UntrustedString;

[Custom]
typedef string NodeAlias;

[Custom]
typedef string OrderId;

[Custom]
typedef string DateTime;

[Custom]
typedef string FeeRate;
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ pub enum Error {
InvalidQuantity,
/// The given node alias is invalid.
InvalidNodeAlias,
/// The given date time is invalid.
InvalidDateTime,
/// The given fee rate is invalid.
InvalidFeeRate,
/// A payment with the given hash has already been initiated.
DuplicatePayment,
/// The provided offer was denonminated in an unsupported currency.
Expand Down Expand Up @@ -169,6 +173,8 @@ impl fmt::Display for Error {
Self::InvalidUri => write!(f, "The given URI is invalid."),
Self::InvalidQuantity => write!(f, "The given quantity is invalid."),
Self::InvalidNodeAlias => write!(f, "The given node alias is invalid."),
Self::InvalidDateTime => write!(f, "The given date time is invalid."),
Self::InvalidFeeRate => write!(f, "The given fee rate is invalid."),
Self::DuplicatePayment => {
write!(f, "A payment with the given hash has already been initiated.")
},
Expand Down
47 changes: 46 additions & 1 deletion src/uniffi_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub use crate::config::{
default_config, AnchorChannelsConfig, EsploraSyncConfig, MaxDustHTLCExposure,
};
pub use crate::graph::{ChannelInfo, ChannelUpdateInfo, NodeAnnouncementInfo, NodeInfo};
pub use crate::liquidity::LSPS1OrderStatus;
pub use crate::payment::store::{LSPFeeLimits, PaymentDirection, PaymentKind, PaymentStatus};
pub use crate::payment::{MaxTotalRoutingFeeLimit, QrPaymentResult, SendingParameters};

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

pub use lightning_invoice::Bolt11Invoice;

pub use bitcoin::{Address, BlockHash, Network, OutPoint, Txid};
pub use lightning_liquidity::lsps1::msgs::ChannelInfo as ChannelOrderInfo;
pub use lightning_liquidity::lsps1::msgs::{
Bolt11PaymentInfo, OnchainPaymentInfo, OrderId, OrderParameters, PaymentInfo, PaymentState,
};

pub use bitcoin::{Address, BlockHash, FeeRate, Network, OutPoint, Txid};

pub use bip39::Mnemonic;

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

pub type DateTime = chrono::DateTime<chrono::Utc>;

use crate::UniffiCustomTypeConverter;

use crate::builder::sanitize_alias;
Expand Down Expand Up @@ -343,3 +351,40 @@ impl UniffiCustomTypeConverter for NodeAlias {
obj.to_string()
}
}

impl UniffiCustomTypeConverter for OrderId {
type Builtin = String;

fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
Ok(Self(val))
}

fn from_custom(obj: Self) -> Self::Builtin {
obj.0
}
}

impl UniffiCustomTypeConverter for DateTime {
type Builtin = String;

fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
Ok(DateTime::from_str(&val).map_err(|_| Error::InvalidDateTime)?)
}

fn from_custom(obj: Self) -> Self::Builtin {
obj.to_rfc3339()
}
}

/// FIXME TODO
impl UniffiCustomTypeConverter for FeeRate {
type Builtin = String;

fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
Ok(FeeRate::from_str(&val).map_err(|_| Error::InvalidFeeRate)?)
}

fn from_custom(obj: Self) -> Self::Builtin {
obj.to_string()
}
}

0 comments on commit d0eb977

Please sign in to comment.