17
17
//! - Wallet and channel states are persisted to disk.
18
18
//! - Gossip is retrieved over the P2P network.
19
19
20
- #![ deny( missing_docs) ]
21
20
#![ deny( broken_intra_doc_links) ]
22
21
#![ deny( private_intra_doc_links) ]
23
22
#![ allow( bare_trait_objects) ]
@@ -62,7 +61,7 @@ use lightning_persister::FilesystemPersister;
62
61
use lightning_net_tokio:: SocketDescriptor ;
63
62
64
63
use lightning_invoice:: utils:: DefaultRouter ;
65
- use lightning_invoice:: { payment, Currency , Invoice } ;
64
+ use lightning_invoice:: { payment, Currency , Invoice , SignedRawInvoice } ;
66
65
67
66
use bdk:: bitcoin:: secp256k1:: Secp256k1 ;
68
67
use bdk:: blockchain:: esplora:: EsploraBlockchain ;
@@ -72,10 +71,11 @@ use bdk::template::Bip84;
72
71
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
73
72
use bitcoin:: hashes:: Hash ;
74
73
use bitcoin:: secp256k1:: PublicKey ;
75
- use bitcoin:: BlockHash ;
74
+ use bitcoin:: { Address , BlockHash } ;
76
75
77
76
use rand:: Rng ;
78
77
78
+ use core:: str:: FromStr ;
79
79
use std:: collections:: HashMap ;
80
80
use std:: convert:: TryFrom ;
81
81
use std:: fs;
@@ -84,6 +84,8 @@ use std::sync::atomic::{AtomicBool, Ordering};
84
84
use std:: sync:: { Arc , Mutex , RwLock } ;
85
85
use std:: time:: { Duration , Instant , SystemTime } ;
86
86
87
+ uniffi_macros:: include_scaffolding!( "ldk_lite" ) ;
88
+
87
89
// The used 'stop gap' parameter used by BDK's wallet sync. This seems to configure the threshold
88
90
// number of blocks after which BDK stops looking for scripts belonging to the wallet.
89
91
const BDK_CLIENT_STOP_GAP : usize = 20 ;
@@ -184,7 +186,7 @@ impl Builder {
184
186
}
185
187
186
188
/// Builds an [`LdkLite`] instance according to the options previously configured.
187
- pub fn build ( & self ) -> LdkLite {
189
+ pub fn build ( & self ) -> Arc < LdkLite > {
188
190
let config = Arc :: new ( self . config . clone ( ) ) ;
189
191
190
192
let ldk_data_dir = format ! ( "{}/ldk" , & config. storage_dir_path. clone( ) ) ;
@@ -397,7 +399,7 @@ impl Builder {
397
399
398
400
let running = RwLock :: new ( None ) ;
399
401
400
- LdkLite {
402
+ Arc :: new ( LdkLite {
401
403
running,
402
404
config,
403
405
chain_access,
@@ -414,7 +416,7 @@ impl Builder {
414
416
inbound_payments,
415
417
outbound_payments,
416
418
peer_store,
417
- }
419
+ } )
418
420
}
419
421
}
420
422
@@ -453,7 +455,7 @@ impl LdkLite {
453
455
/// Starts the necessary background tasks, such as handling events coming from user input,
454
456
/// LDK/BDK, and the peer-to-peer network. After this returns, the [`LdkLite`] instance can be
455
457
/// 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 > {
457
459
// Acquire a run lock and hold it until we're setup.
458
460
let mut run_lock = self . running . write ( ) . unwrap ( ) ;
459
461
if run_lock. is_some ( ) {
@@ -467,7 +469,7 @@ impl LdkLite {
467
469
}
468
470
469
471
/// 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 > {
471
473
let mut run_lock = self . running . write ( ) . unwrap ( ) ;
472
474
if run_lock. is_none ( ) {
473
475
return Err ( Error :: NotRunning ) ;
@@ -638,7 +640,7 @@ impl LdkLite {
638
640
}
639
641
640
642
/// 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 > {
642
644
if self . running . read ( ) . unwrap ( ) . is_none ( ) {
643
645
return Err ( Error :: NotRunning ) ;
644
646
}
@@ -1006,5 +1008,94 @@ pub(crate) type NetworkGraph = gossip::NetworkGraph<Arc<FilesystemLogger>>;
1006
1008
1007
1009
pub ( crate ) type PaymentInfoStorage = Mutex < HashMap < PaymentHash , PaymentInfo > > ;
1008
1010
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
+ }
1076
+
1077
+ /// The channel identifier as derived from the funding transaction, or the `temporary_channel_id`
1078
+ /// if the channel has not been established yet.
1079
+ ///
1080
+ /// See <https://github.com/lightningdevkit/rust-lightning/pull/1790> for more information.
1081
+ #[ derive( Debug , Clone ) ]
1082
+ pub struct ChannelId ( [ u8 ; 32 ] ) ;
1083
+
1084
+ impl UniffiCustomTypeConverter for ChannelId {
1085
+ type Builtin = String ;
1086
+
1087
+ fn into_custom ( val : Self :: Builtin ) -> uniffi:: Result < Self > {
1088
+ if let Some ( hex_vec) = hex_utils:: to_vec ( & val) {
1089
+ if hex_vec. len ( ) == 32 {
1090
+ let mut channel_id = [ 0u8 ; 32 ] ;
1091
+ channel_id. copy_from_slice ( & hex_vec[ ..] ) ;
1092
+ return Ok ( Self ( channel_id) ) ;
1093
+ }
1094
+ }
1095
+ Err ( Error :: ChannelIdInvalid . into ( ) )
1096
+ }
1097
+
1098
+ fn from_custom ( obj : Self ) -> Self :: Builtin {
1099
+ hex_utils:: to_string ( & obj. 0 )
1100
+ }
1101
+ }
0 commit comments