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) ]
@@ -67,7 +66,7 @@ use lightning_transaction_sync::EsploraSyncClient;
67
66
use lightning_net_tokio:: SocketDescriptor ;
68
67
69
68
use lightning:: routing:: router:: DefaultRouter ;
70
- use lightning_invoice:: { payment, Currency , Invoice } ;
69
+ use lightning_invoice:: { payment, Currency , Invoice , SignedRawInvoice } ;
71
70
72
71
use bdk:: bitcoin:: secp256k1:: Secp256k1 ;
73
72
use bdk:: blockchain:: esplora:: EsploraBlockchain ;
@@ -77,10 +76,11 @@ use bdk::template::Bip84;
77
76
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
78
77
use bitcoin:: hashes:: Hash ;
79
78
use bitcoin:: secp256k1:: PublicKey ;
80
- use bitcoin:: BlockHash ;
79
+ use bitcoin:: { Address , BlockHash } ;
81
80
82
81
use rand:: Rng ;
83
82
83
+ use core:: str:: FromStr ;
84
84
use std:: collections:: HashMap ;
85
85
use std:: convert:: { TryFrom , TryInto } ;
86
86
use std:: default:: Default ;
@@ -90,6 +90,8 @@ use std::sync::atomic::{AtomicBool, Ordering};
90
90
use std:: sync:: { Arc , Mutex , RwLock } ;
91
91
use std:: time:: { Duration , Instant , SystemTime } ;
92
92
93
+ uniffi_macros:: include_scaffolding!( "ldk_lite" ) ;
94
+
93
95
// The used 'stop gap' parameter used by BDK's wallet sync. This seems to configure the threshold
94
96
// number of blocks after which BDK stops looking for scripts belonging to the wallet.
95
97
const BDK_CLIENT_STOP_GAP : usize = 20 ;
@@ -197,7 +199,7 @@ impl Builder {
197
199
}
198
200
199
201
/// Builds an [`LdkLite`] instance according to the options previously configured.
200
- pub fn build ( & self ) -> LdkLite {
202
+ pub fn build ( & self ) -> Arc < LdkLite > {
201
203
let config = Arc :: new ( self . config . clone ( ) ) ;
202
204
203
205
let ldk_data_dir = format ! ( "{}/ldk" , & config. storage_dir_path. clone( ) ) ;
@@ -426,7 +428,7 @@ impl Builder {
426
428
427
429
let running = RwLock :: new ( None ) ;
428
430
429
- LdkLite {
431
+ Arc :: new ( LdkLite {
430
432
running,
431
433
config,
432
434
wallet,
@@ -445,7 +447,7 @@ impl Builder {
445
447
inbound_payments,
446
448
outbound_payments,
447
449
peer_store,
448
- }
450
+ } )
449
451
}
450
452
}
451
453
@@ -487,7 +489,7 @@ impl LdkLite {
487
489
/// Starts the necessary background tasks, such as handling events coming from user input,
488
490
/// LDK/BDK, and the peer-to-peer network. After this returns, the [`LdkLite`] instance can be
489
491
/// 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 > {
491
493
// Acquire a run lock and hold it until we're setup.
492
494
let mut run_lock = self . running . write ( ) . unwrap ( ) ;
493
495
if run_lock. is_some ( ) {
@@ -501,7 +503,7 @@ impl LdkLite {
501
503
}
502
504
503
505
/// 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 > {
505
507
let mut run_lock = self . running . write ( ) . unwrap ( ) ;
506
508
if run_lock. is_none ( ) {
507
509
return Err ( Error :: NotRunning ) ;
@@ -695,7 +697,7 @@ impl LdkLite {
695
697
}
696
698
697
699
/// 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 > {
699
701
let funding_address = self . wallet . get_new_address ( ) ?;
700
702
log_info ! ( self . logger, "Generated new funding address: {}" , funding_address) ;
701
703
Ok ( funding_address)
@@ -1086,3 +1088,106 @@ pub(crate) type NetworkGraph = gossip::NetworkGraph<Arc<FilesystemLogger>>;
1086
1088
pub ( crate ) type PaymentInfoStorage = Mutex < HashMap < PaymentHash , PaymentInfo > > ;
1087
1089
1088
1090
pub ( 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