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) ]
@@ -62,7 +61,7 @@ use lightning_persister::FilesystemPersister;
6261use lightning_net_tokio:: SocketDescriptor ;
6362
6463use lightning_invoice:: utils:: DefaultRouter ;
65- use lightning_invoice:: { payment, Currency , Invoice } ;
64+ use lightning_invoice:: { payment, Currency , Invoice , SignedRawInvoice } ;
6665
6766use bdk:: bitcoin:: secp256k1:: Secp256k1 ;
6867use bdk:: blockchain:: esplora:: EsploraBlockchain ;
@@ -72,10 +71,11 @@ use bdk::template::Bip84;
7271use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
7372use bitcoin:: hashes:: Hash ;
7473use bitcoin:: secp256k1:: PublicKey ;
75- use bitcoin:: BlockHash ;
74+ use bitcoin:: { Address , BlockHash } ;
7675
7776use rand:: Rng ;
7877
78+ use core:: str:: FromStr ;
7979use std:: collections:: HashMap ;
8080use std:: convert:: TryFrom ;
8181use std:: fs;
@@ -84,6 +84,8 @@ use std::sync::atomic::{AtomicBool, Ordering};
8484use std:: sync:: { Arc , Mutex , RwLock } ;
8585use std:: time:: { Duration , Instant , SystemTime } ;
8686
87+ uniffi_macros:: include_scaffolding!( "ldk_lite" ) ;
88+
8789// The used 'stop gap' parameter used by BDK's wallet sync. This seems to configure the threshold
8890// number of blocks after which BDK stops looking for scripts belonging to the wallet.
8991const BDK_CLIENT_STOP_GAP : usize = 20 ;
@@ -184,7 +186,7 @@ impl Builder {
184186 }
185187
186188 /// Builds an [`LdkLite`] instance according to the options previously configured.
187- pub fn build ( & self ) -> LdkLite {
189+ pub fn build ( & self ) -> Arc < LdkLite > {
188190 let config = Arc :: new ( self . config . clone ( ) ) ;
189191
190192 let ldk_data_dir = format ! ( "{}/ldk" , & config. storage_dir_path. clone( ) ) ;
@@ -397,7 +399,7 @@ impl Builder {
397399
398400 let running = RwLock :: new ( None ) ;
399401
400- LdkLite {
402+ Arc :: new ( LdkLite {
401403 running,
402404 config,
403405 chain_access,
@@ -414,7 +416,7 @@ impl Builder {
414416 inbound_payments,
415417 outbound_payments,
416418 peer_store,
417- }
419+ } )
418420 }
419421}
420422
@@ -453,7 +455,7 @@ impl LdkLite {
453455 /// Starts the necessary background tasks, such as handling events coming from user input,
454456 /// LDK/BDK, and the peer-to-peer network. After this returns, the [`LdkLite`] instance can be
455457 /// controlled via the provided API methods in a thread-safe manner.
456- pub fn start ( & mut self ) -> Result < ( ) , Error > {
458+ pub fn start ( & self ) -> Result < ( ) , Error > {
457459 // Acquire a run lock and hold it until we're setup.
458460 let mut run_lock = self . running . write ( ) . unwrap ( ) ;
459461 if run_lock. is_some ( ) {
@@ -467,7 +469,7 @@ impl LdkLite {
467469 }
468470
469471 /// Disconnects all peers, stops all running background tasks, and shuts down [`LdkLite`].
470- pub fn stop ( & mut self ) -> Result < ( ) , Error > {
472+ pub fn stop ( & self ) -> Result < ( ) , Error > {
471473 let mut run_lock = self . running . write ( ) . unwrap ( ) ;
472474 if run_lock. is_none ( ) {
473475 return Err ( Error :: NotRunning ) ;
@@ -638,7 +640,7 @@ impl LdkLite {
638640 }
639641
640642 /// Retrieve a new on-chain/funding address.
641- pub fn new_funding_address ( & mut self ) -> Result < bitcoin :: Address , Error > {
643+ pub fn new_funding_address ( & self ) -> Result < Address , Error > {
642644 if self . running . read ( ) . unwrap ( ) . is_none ( ) {
643645 return Err ( Error :: NotRunning ) ;
644646 }
@@ -1006,5 +1008,68 @@ pub(crate) type NetworkGraph = gossip::NetworkGraph<Arc<FilesystemLogger>>;
10061008
10071009pub ( crate ) type PaymentInfoStorage = Mutex < HashMap < PaymentHash , PaymentInfo > > ;
10081010
1009- #[ cfg( test) ]
1010- mod tests { }
1011+ impl UniffiCustomTypeConverter for PublicKey {
1012+ type Builtin = String ;
1013+
1014+ fn into_custom ( val : Self :: Builtin ) -> uniffi:: Result < Self > {
1015+ if let Ok ( key) = PublicKey :: from_str ( & val) {
1016+ return Ok ( key) ;
1017+ }
1018+
1019+ Err ( Error :: PublicKeyInvalid . into ( ) )
1020+ }
1021+
1022+ fn from_custom ( obj : Self ) -> Self :: Builtin {
1023+ obj. to_string ( )
1024+ }
1025+ }
1026+
1027+ impl UniffiCustomTypeConverter for Address {
1028+ type Builtin = String ;
1029+
1030+ fn into_custom ( val : Self :: Builtin ) -> uniffi:: Result < Self > {
1031+ if let Ok ( addr) = Address :: from_str ( & val) {
1032+ return Ok ( addr) ;
1033+ }
1034+
1035+ Err ( Error :: AddressInvalid . into ( ) )
1036+ }
1037+
1038+ fn from_custom ( obj : Self ) -> Self :: Builtin {
1039+ obj. to_string ( )
1040+ }
1041+ }
1042+
1043+ impl UniffiCustomTypeConverter for Invoice {
1044+ type Builtin = String ;
1045+
1046+ fn into_custom ( val : Self :: Builtin ) -> uniffi:: Result < Self > {
1047+ if let Ok ( signed) = val. parse :: < SignedRawInvoice > ( ) {
1048+ if let Ok ( invoice) = Invoice :: from_signed ( signed) {
1049+ return Ok ( invoice) ;
1050+ }
1051+ }
1052+
1053+ Err ( Error :: InvoiceInvalid . into ( ) )
1054+ }
1055+
1056+ fn from_custom ( obj : Self ) -> Self :: Builtin {
1057+ obj. to_string ( )
1058+ }
1059+ }
1060+
1061+ impl UniffiCustomTypeConverter for PaymentHash {
1062+ type Builtin = String ;
1063+
1064+ fn into_custom ( val : Self :: Builtin ) -> uniffi:: Result < Self > {
1065+ if let Ok ( hash) = Sha256 :: from_str ( & val) {
1066+ Ok ( PaymentHash ( hash. into_inner ( ) ) )
1067+ } else {
1068+ Err ( Error :: PaymentHashInvalid . into ( ) )
1069+ }
1070+ }
1071+
1072+ fn from_custom ( obj : Self ) -> Self :: Builtin {
1073+ Sha256 :: from_slice ( & obj. 0 ) . unwrap ( ) . to_string ( )
1074+ }
1075+ }
0 commit comments