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) ]
@@ -67,7 +66,7 @@ use lightning_transaction_sync::EsploraSyncClient;
6766use lightning_net_tokio:: SocketDescriptor ;
6867
6968use lightning:: routing:: router:: DefaultRouter ;
70- use lightning_invoice:: { payment, Currency , Invoice } ;
69+ use lightning_invoice:: { payment, Currency , Invoice , SignedRawInvoice } ;
7170
7271use bdk:: bitcoin:: secp256k1:: Secp256k1 ;
7372use bdk:: blockchain:: esplora:: EsploraBlockchain ;
@@ -77,10 +76,11 @@ use bdk::template::Bip84;
7776use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
7877use bitcoin:: hashes:: Hash ;
7978use bitcoin:: secp256k1:: PublicKey ;
80- use bitcoin:: BlockHash ;
79+ use bitcoin:: { Address , BlockHash } ;
8180
8281use rand:: Rng ;
8382
83+ use core:: str:: FromStr ;
8484use std:: collections:: HashMap ;
8585use std:: convert:: { TryFrom , TryInto } ;
8686use std:: default:: Default ;
@@ -90,6 +90,8 @@ use std::sync::atomic::{AtomicBool, Ordering};
9090use std:: sync:: { Arc , Mutex , RwLock } ;
9191use std:: time:: { Duration , Instant , SystemTime } ;
9292
93+ uniffi_macros:: include_scaffolding!( "ldk_lite" ) ;
94+
9395// The used 'stop gap' parameter used by BDK's wallet sync. This seems to configure the threshold
9496// number of blocks after which BDK stops looking for scripts belonging to the wallet.
9597const BDK_CLIENT_STOP_GAP : usize = 20 ;
@@ -197,7 +199,7 @@ impl Builder {
197199 }
198200
199201 /// Builds an [`LdkLite`] instance according to the options previously configured.
200- pub fn build ( & self ) -> LdkLite {
202+ pub fn build ( & self ) -> Arc < LdkLite > {
201203 let config = Arc :: new ( self . config . clone ( ) ) ;
202204
203205 let ldk_data_dir = format ! ( "{}/ldk" , & config. storage_dir_path. clone( ) ) ;
@@ -426,7 +428,7 @@ impl Builder {
426428
427429 let running = RwLock :: new ( None ) ;
428430
429- LdkLite {
431+ Arc :: new ( LdkLite {
430432 running,
431433 config,
432434 wallet,
@@ -445,7 +447,7 @@ impl Builder {
445447 inbound_payments,
446448 outbound_payments,
447449 peer_store,
448- }
450+ } )
449451 }
450452}
451453
@@ -487,7 +489,7 @@ impl LdkLite {
487489 /// Starts the necessary background tasks, such as handling events coming from user input,
488490 /// LDK/BDK, and the peer-to-peer network. After this returns, the [`LdkLite`] instance can be
489491 /// controlled via the provided API methods in a thread-safe manner.
490- pub fn start ( & mut self ) -> Result < ( ) , Error > {
492+ pub fn start ( & self ) -> Result < ( ) , Error > {
491493 // Acquire a run lock and hold it until we're setup.
492494 let mut run_lock = self . running . write ( ) . unwrap ( ) ;
493495 if run_lock. is_some ( ) {
@@ -501,7 +503,7 @@ impl LdkLite {
501503 }
502504
503505 /// Disconnects all peers, stops all running background tasks, and shuts down [`LdkLite`].
504- pub fn stop ( & mut self ) -> Result < ( ) , Error > {
506+ pub fn stop ( & self ) -> Result < ( ) , Error > {
505507 let mut run_lock = self . running . write ( ) . unwrap ( ) ;
506508 if run_lock. is_none ( ) {
507509 return Err ( Error :: NotRunning ) ;
@@ -695,7 +697,7 @@ impl LdkLite {
695697 }
696698
697699 /// Retrieve a new on-chain/funding address.
698- pub fn new_funding_address ( & mut self ) -> Result < bitcoin :: Address , Error > {
700+ pub fn new_funding_address ( & self ) -> Result < Address , Error > {
699701 let funding_address = self . wallet . get_new_address ( ) ?;
700702 log_info ! ( self . logger, "Generated new funding address: {}" , funding_address) ;
701703 Ok ( funding_address)
@@ -1086,3 +1088,106 @@ pub(crate) type NetworkGraph = gossip::NetworkGraph<Arc<FilesystemLogger>>;
10861088pub ( crate ) type PaymentInfoStorage = Mutex < HashMap < PaymentHash , PaymentInfo > > ;
10871089
10881090pub ( crate ) type OnionMessenger = SimpleArcOnionMessenger < FilesystemLogger > ;
1091+
1092+ impl UniffiCustomTypeConverter for PublicKey {
1093+ type Builtin = String ;
1094+
1095+ fn into_custom ( val : Self :: Builtin ) -> uniffi:: Result < Self > {
1096+ if let Ok ( key) = PublicKey :: from_str ( & val) {
1097+ return Ok ( key) ;
1098+ }
1099+
1100+ Err ( Error :: PublicKeyInvalid . into ( ) )
1101+ }
1102+
1103+ fn from_custom ( obj : Self ) -> Self :: Builtin {
1104+ obj. to_string ( )
1105+ }
1106+ }
1107+
1108+ impl UniffiCustomTypeConverter for Address {
1109+ type Builtin = String ;
1110+
1111+ fn into_custom ( val : Self :: Builtin ) -> uniffi:: Result < Self > {
1112+ if let Ok ( addr) = Address :: from_str ( & val) {
1113+ return Ok ( addr) ;
1114+ }
1115+
1116+ Err ( Error :: AddressInvalid . into ( ) )
1117+ }
1118+
1119+ fn from_custom ( obj : Self ) -> Self :: Builtin {
1120+ obj. to_string ( )
1121+ }
1122+ }
1123+
1124+ impl UniffiCustomTypeConverter for Invoice {
1125+ type Builtin = String ;
1126+
1127+ fn into_custom ( val : Self :: Builtin ) -> uniffi:: Result < Self > {
1128+ if let Ok ( signed) = val. parse :: < SignedRawInvoice > ( ) {
1129+ if let Ok ( invoice) = Invoice :: from_signed ( signed) {
1130+ return Ok ( invoice) ;
1131+ }
1132+ }
1133+
1134+ Err ( Error :: InvoiceInvalid . into ( ) )
1135+ }
1136+
1137+ fn from_custom ( obj : Self ) -> Self :: Builtin {
1138+ obj. to_string ( )
1139+ }
1140+ }
1141+
1142+ impl UniffiCustomTypeConverter for PaymentHash {
1143+ type Builtin = String ;
1144+
1145+ fn into_custom ( val : Self :: Builtin ) -> uniffi:: Result < Self > {
1146+ if let Ok ( hash) = Sha256 :: from_str ( & val) {
1147+ Ok ( PaymentHash ( hash. into_inner ( ) ) )
1148+ } else {
1149+ Err ( Error :: PaymentHashInvalid . into ( ) )
1150+ }
1151+ }
1152+
1153+ fn from_custom ( obj : Self ) -> Self :: Builtin {
1154+ Sha256 :: from_slice ( & obj. 0 ) . unwrap ( ) . to_string ( )
1155+ }
1156+ }
1157+
1158+ #[ derive( Debug , Clone , PartialEq ) ]
1159+ pub struct ChannelId ( [ u8 ; 32 ] ) ;
1160+
1161+ impl UniffiCustomTypeConverter for ChannelId {
1162+ type Builtin = String ;
1163+
1164+ fn into_custom ( val : Self :: Builtin ) -> uniffi:: Result < Self > {
1165+ if let Some ( hex_vec) = hex_utils:: to_vec ( & val) {
1166+ if hex_vec. len ( ) == 32 {
1167+ let mut channel_id = [ 0u8 ; 32 ] ;
1168+ channel_id. copy_from_slice ( & hex_vec[ ..] ) ;
1169+ return Ok ( Self ( channel_id) ) ;
1170+ }
1171+ }
1172+ Err ( Error :: ChannelIdInvalid . into ( ) )
1173+ }
1174+
1175+ fn from_custom ( obj : Self ) -> Self :: Builtin {
1176+ hex_utils:: to_string ( & obj. 0 )
1177+ }
1178+ }
1179+
1180+ #[ derive( Debug , Clone , PartialEq ) ]
1181+ pub struct UserChannelId ( u128 ) ;
1182+
1183+ impl UniffiCustomTypeConverter for UserChannelId {
1184+ type Builtin = String ;
1185+
1186+ fn into_custom ( val : Self :: Builtin ) -> uniffi:: Result < Self > {
1187+ Ok ( UserChannelId ( u128:: from_str ( & val) . map_err ( |_| Error :: ChannelIdInvalid ) ?) )
1188+ }
1189+
1190+ fn from_custom ( obj : Self ) -> Self :: Builtin {
1191+ obj. 0 . to_string ( )
1192+ }
1193+ }
0 commit comments