1717//! - Wallet and channel states are persisted to disk.
1818//! - Gossip is retrieved over the P2P network.
1919
20- #![ deny( missing_docs) ]
2120#![ deny( broken_intra_doc_links) ]
2221#![ deny( private_intra_doc_links) ]
2322#![ allow( bare_trait_objects) ]
@@ -64,7 +63,7 @@ use lightning_transaction_sync::EsploraSyncClient;
6463use lightning_net_tokio:: SocketDescriptor ;
6564
6665use lightning:: routing:: router:: DefaultRouter ;
67- use lightning_invoice:: { payment, Currency , Invoice } ;
66+ use lightning_invoice:: { payment, Currency , Invoice , SignedRawInvoice } ;
6867
6968use bdk:: bitcoin:: secp256k1:: Secp256k1 ;
7069use bdk:: blockchain:: esplora:: EsploraBlockchain ;
@@ -74,10 +73,11 @@ use bdk::template::Bip84;
7473use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
7574use bitcoin:: hashes:: Hash ;
7675use bitcoin:: secp256k1:: PublicKey ;
77- use bitcoin:: BlockHash ;
76+ use bitcoin:: { Address , BlockHash } ;
7877
7978use rand:: Rng ;
8079
80+ use core:: str:: FromStr ;
8181use std:: collections:: HashMap ;
8282use std:: convert:: { TryFrom , TryInto } ;
8383use std:: default:: Default ;
@@ -87,6 +87,8 @@ use std::sync::atomic::{AtomicBool, Ordering};
8787use std:: sync:: { Arc , Mutex , RwLock } ;
8888use std:: time:: { Duration , Instant , SystemTime } ;
8989
90+ uniffi_macros:: include_scaffolding!( "ldk_lite" ) ;
91+
9092// The used 'stop gap' parameter used by BDK's wallet sync. This seems to configure the threshold
9193// number of blocks after which BDK stops looking for scripts belonging to the wallet.
9294const BDK_CLIENT_STOP_GAP : usize = 20 ;
@@ -194,7 +196,7 @@ impl Builder {
194196 }
195197
196198 /// Builds an [`LdkLite`] instance according to the options previously configured.
197- pub fn build ( & self ) -> LdkLite {
199+ pub fn build ( & self ) -> Arc < LdkLite > {
198200 let config = Arc :: new ( self . config . clone ( ) ) ;
199201
200202 let ldk_data_dir = format ! ( "{}/ldk" , & config. storage_dir_path. clone( ) ) ;
@@ -427,7 +429,7 @@ impl Builder {
427429
428430 let running = RwLock :: new ( None ) ;
429431
430- LdkLite {
432+ Arc :: new ( LdkLite {
431433 running,
432434 config,
433435 wallet,
@@ -446,7 +448,7 @@ impl Builder {
446448 inbound_payments,
447449 outbound_payments,
448450 peer_store,
449- }
451+ } )
450452 }
451453}
452454
@@ -488,7 +490,7 @@ impl LdkLite {
488490 /// Starts the necessary background tasks, such as handling events coming from user input,
489491 /// LDK/BDK, and the peer-to-peer network. After this returns, the [`LdkLite`] instance can be
490492 /// controlled via the provided API methods in a thread-safe manner.
491- pub fn start ( & mut self ) -> Result < ( ) , Error > {
493+ pub fn start ( & self ) -> Result < ( ) , Error > {
492494 // Acquire a run lock and hold it until we're setup.
493495 let mut run_lock = self . running . write ( ) . unwrap ( ) ;
494496 if run_lock. is_some ( ) {
@@ -502,7 +504,7 @@ impl LdkLite {
502504 }
503505
504506 /// Disconnects all peers, stops all running background tasks, and shuts down [`LdkLite`].
505- pub fn stop ( & mut self ) -> Result < ( ) , Error > {
507+ pub fn stop ( & self ) -> Result < ( ) , Error > {
506508 let mut run_lock = self . running . write ( ) . unwrap ( ) ;
507509 if run_lock. is_none ( ) {
508510 return Err ( Error :: NotRunning ) ;
@@ -696,7 +698,7 @@ impl LdkLite {
696698 }
697699
698700 /// Retrieve a new on-chain/funding address.
699- pub fn new_funding_address ( & mut self ) -> Result < bitcoin :: Address , Error > {
701+ pub fn new_funding_address ( & self ) -> Result < Address , Error > {
700702 let funding_address = self . wallet . get_new_address ( ) ?;
701703 log_info ! ( self . logger, "Generated new funding address: {}" , funding_address) ;
702704 Ok ( funding_address)
@@ -1100,3 +1102,106 @@ pub(crate) type OnionMessenger = lightning::onion_message::OnionMessenger<
11001102 Arc < FilesystemLogger > ,
11011103 IgnoringMessageHandler ,
11021104> ;
1105+
1106+ impl UniffiCustomTypeConverter for PublicKey {
1107+ type Builtin = String ;
1108+
1109+ fn into_custom ( val : Self :: Builtin ) -> uniffi:: Result < Self > {
1110+ if let Ok ( key) = PublicKey :: from_str ( & val) {
1111+ return Ok ( key) ;
1112+ }
1113+
1114+ Err ( Error :: PublicKeyInvalid . into ( ) )
1115+ }
1116+
1117+ fn from_custom ( obj : Self ) -> Self :: Builtin {
1118+ obj. to_string ( )
1119+ }
1120+ }
1121+
1122+ impl UniffiCustomTypeConverter for Address {
1123+ type Builtin = String ;
1124+
1125+ fn into_custom ( val : Self :: Builtin ) -> uniffi:: Result < Self > {
1126+ if let Ok ( addr) = Address :: from_str ( & val) {
1127+ return Ok ( addr) ;
1128+ }
1129+
1130+ Err ( Error :: AddressInvalid . into ( ) )
1131+ }
1132+
1133+ fn from_custom ( obj : Self ) -> Self :: Builtin {
1134+ obj. to_string ( )
1135+ }
1136+ }
1137+
1138+ impl UniffiCustomTypeConverter for Invoice {
1139+ type Builtin = String ;
1140+
1141+ fn into_custom ( val : Self :: Builtin ) -> uniffi:: Result < Self > {
1142+ if let Ok ( signed) = val. parse :: < SignedRawInvoice > ( ) {
1143+ if let Ok ( invoice) = Invoice :: from_signed ( signed) {
1144+ return Ok ( invoice) ;
1145+ }
1146+ }
1147+
1148+ Err ( Error :: InvoiceInvalid . into ( ) )
1149+ }
1150+
1151+ fn from_custom ( obj : Self ) -> Self :: Builtin {
1152+ obj. to_string ( )
1153+ }
1154+ }
1155+
1156+ impl UniffiCustomTypeConverter for PaymentHash {
1157+ type Builtin = String ;
1158+
1159+ fn into_custom ( val : Self :: Builtin ) -> uniffi:: Result < Self > {
1160+ if let Ok ( hash) = Sha256 :: from_str ( & val) {
1161+ Ok ( PaymentHash ( hash. into_inner ( ) ) )
1162+ } else {
1163+ Err ( Error :: PaymentHashInvalid . into ( ) )
1164+ }
1165+ }
1166+
1167+ fn from_custom ( obj : Self ) -> Self :: Builtin {
1168+ Sha256 :: from_slice ( & obj. 0 ) . unwrap ( ) . to_string ( )
1169+ }
1170+ }
1171+
1172+ #[ derive( Debug , Clone , PartialEq ) ]
1173+ pub struct ChannelId ( [ u8 ; 32 ] ) ;
1174+
1175+ impl UniffiCustomTypeConverter for ChannelId {
1176+ type Builtin = String ;
1177+
1178+ fn into_custom ( val : Self :: Builtin ) -> uniffi:: Result < Self > {
1179+ if let Some ( hex_vec) = hex_utils:: to_vec ( & val) {
1180+ if hex_vec. len ( ) == 32 {
1181+ let mut channel_id = [ 0u8 ; 32 ] ;
1182+ channel_id. copy_from_slice ( & hex_vec[ ..] ) ;
1183+ return Ok ( Self ( channel_id) ) ;
1184+ }
1185+ }
1186+ Err ( Error :: ChannelIdInvalid . into ( ) )
1187+ }
1188+
1189+ fn from_custom ( obj : Self ) -> Self :: Builtin {
1190+ hex_utils:: to_string ( & obj. 0 )
1191+ }
1192+ }
1193+
1194+ #[ derive( Debug , Clone , PartialEq ) ]
1195+ pub struct UserChannelId ( u128 ) ;
1196+
1197+ impl UniffiCustomTypeConverter for UserChannelId {
1198+ type Builtin = String ;
1199+
1200+ fn into_custom ( val : Self :: Builtin ) -> uniffi:: Result < Self > {
1201+ Ok ( UserChannelId ( u128:: from_str ( & val) . map_err ( |_| Error :: ChannelIdInvalid ) ?) )
1202+ }
1203+
1204+ fn from_custom ( obj : Self ) -> Self :: Builtin {
1205+ obj. 0 . to_string ( )
1206+ }
1207+ }
0 commit comments