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