29
29
//! use ldk_node::Builder;
30
30
//! use ldk_node::lightning_invoice::Invoice;
31
31
//! use ldk_node::bitcoin::secp256k1::PublicKey;
32
+ //! use ldk_node::bitcoin::Network;
32
33
//! use std::str::FromStr;
33
34
//!
34
35
//! fn main() {
35
- //! let node = Builder::new()
36
- //! .set_network("testnet")
37
- //! .set_esplora_server_url("https://blockstream.info/testnet/api".to_string())
38
- //! .build();
36
+ //! let mut builder = Builder::new();
37
+ //! builder.set_network(Network::Testnet);
38
+ //! builder.set_esplora_server_url("https://blockstream.info/testnet/api".to_string());
39
39
//!
40
+ //! let node = builder.build();
40
41
//! node.start().unwrap();
41
42
//!
42
43
//! let _funding_address = node.new_funding_address();
@@ -142,6 +143,8 @@ use bitcoin::hashes::Hash;
142
143
use bitcoin:: secp256k1:: PublicKey ;
143
144
use bitcoin:: Network ;
144
145
146
+ use bip39:: Mnemonic ;
147
+
145
148
use bitcoin:: { Address , BlockHash , OutPoint , Txid } ;
146
149
147
150
use rand:: Rng ;
@@ -150,7 +153,6 @@ use std::convert::TryInto;
150
153
use std:: default:: Default ;
151
154
use std:: fs;
152
155
use std:: net:: SocketAddr ;
153
- use std:: str:: FromStr ;
154
156
use std:: sync:: atomic:: { AtomicBool , Ordering } ;
155
157
use std:: sync:: { Arc , Mutex , RwLock } ;
156
158
use std:: time:: { Duration , Instant , SystemTime } ;
@@ -204,7 +206,7 @@ impl Default for Config {
204
206
enum EntropySourceConfig {
205
207
SeedFile ( String ) ,
206
208
SeedBytes ( [ u8 ; WALLET_KEYS_SEED_LEN ] ) ,
207
- Bip39Mnemonic { mnemonic : bip39 :: Mnemonic , passphrase : Option < String > } ,
209
+ Bip39Mnemonic { mnemonic : Mnemonic , passphrase : Option < String > } ,
208
210
}
209
211
210
212
#[ derive( Debug , Clone ) ]
@@ -215,106 +217,105 @@ enum GossipSourceConfig {
215
217
216
218
/// A builder for an [`Node`] instance, allowing to set some configuration and module choices from
217
219
/// the getgo.
218
- #[ derive( Debug , Clone ) ]
220
+ #[ derive( Debug ) ]
219
221
pub struct Builder {
220
- config : Config ,
221
- entropy_source_config : Option < EntropySourceConfig > ,
222
- gossip_source_config : Option < GossipSourceConfig > ,
222
+ config : Mutex < Config > ,
223
+ entropy_source_config : Mutex < Option < EntropySourceConfig > > ,
224
+ gossip_source_config : Mutex < Option < GossipSourceConfig > > ,
223
225
}
224
226
225
227
impl Builder {
226
228
/// Creates a new builder instance with the default configuration.
227
229
pub fn new ( ) -> Self {
228
- let config = Config :: default ( ) ;
229
- let entropy_source_config = None ;
230
- let gossip_source_config = None ;
230
+ let config = Mutex :: new ( Config :: default ( ) ) ;
231
+ let entropy_source_config = Mutex :: new ( None ) ;
232
+ let gossip_source_config = Mutex :: new ( None ) ;
231
233
Self { config, entropy_source_config, gossip_source_config }
232
234
}
233
235
234
236
/// Creates a new builder instance from an [`Config`].
235
237
pub fn from_config ( config : Config ) -> Self {
236
- let entropy_source_config = None ;
237
- let gossip_source_config = None ;
238
+ let config = Mutex :: new ( config) ;
239
+ let entropy_source_config = Mutex :: new ( None ) ;
240
+ let gossip_source_config = Mutex :: new ( None ) ;
238
241
Self { config, entropy_source_config, gossip_source_config }
239
242
}
240
243
241
244
/// Configures the [`Node`] instance to source its wallet entropy from a seed file on disk.
242
245
///
243
246
/// If the given file does not exist a new random seed file will be generated and
244
247
/// stored at the given location.
245
- pub fn set_entropy_seed_path ( & mut self , seed_path : String ) -> & mut Self {
246
- self . entropy_source_config = Some ( EntropySourceConfig :: SeedFile ( seed_path) ) ;
247
- self
248
+ pub fn set_entropy_seed_path ( & self , seed_path : String ) {
249
+ * self . entropy_source_config . lock ( ) . unwrap ( ) =
250
+ Some ( EntropySourceConfig :: SeedFile ( seed_path) ) ;
251
+ }
252
+
253
+ /// Configures the [`Node`] instance to source its wallet entropy from the given 64 seed bytes.
254
+ ///
255
+ /// **Note:** Panics if the length of the given `seed_bytes` differs from 64.
256
+ pub fn set_entropy_seed_bytes ( & self , seed_bytes : Vec < u8 > ) {
257
+ if seed_bytes. len ( ) != WALLET_KEYS_SEED_LEN {
258
+ panic ! ( "Failed to set seed due to invalid length." ) ;
259
+ }
260
+ let mut bytes = [ 0u8 ; WALLET_KEYS_SEED_LEN ] ;
261
+ bytes. copy_from_slice ( & seed_bytes) ;
262
+ * self . entropy_source_config . lock ( ) . unwrap ( ) = Some ( EntropySourceConfig :: SeedBytes ( bytes) ) ;
248
263
}
249
264
250
- /// Configures the [`Node`] instance to source its wallet entropy from the given seed bytes.
251
- pub fn set_entropy_seed_bytes ( & mut self , seed_bytes : [ u8 ; WALLET_KEYS_SEED_LEN ] ) -> & mut Self {
252
- self . entropy_source_config = Some ( EntropySourceConfig :: SeedBytes ( seed_bytes) ) ;
253
- self
265
+ /// Configures the [`Node`] instance to source its wallet entropy from a [BIP 39] mnemonic.
266
+ ///
267
+ /// [BIP 39]: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
268
+ pub fn set_entropy_bip39_mnemonic ( & self , mnemonic : Mnemonic , passphrase : Option < String > ) {
269
+ * self . entropy_source_config . lock ( ) . unwrap ( ) =
270
+ Some ( EntropySourceConfig :: Bip39Mnemonic { mnemonic, passphrase } ) ;
254
271
}
255
272
256
273
/// Configures the [`Node`] instance to source its gossip data from the Lightning peer-to-peer
257
274
/// network.
258
- pub fn set_gossip_source_p2p ( & mut self ) -> & mut Self {
259
- self . gossip_source_config = Some ( GossipSourceConfig :: P2PNetwork ) ;
260
- self
275
+ pub fn set_gossip_source_p2p ( & self ) {
276
+ * self . gossip_source_config . lock ( ) . unwrap ( ) = Some ( GossipSourceConfig :: P2PNetwork ) ;
261
277
}
262
278
263
279
/// Configures the [`Node`] instance to source its gossip data from the given RapidGossipSync
264
280
/// server.
265
- pub fn set_gossip_source_rgs ( & mut self , rgs_server_url : String ) -> & mut Self {
266
- self . gossip_source_config = Some ( GossipSourceConfig :: RapidGossipSync ( rgs_server_url) ) ;
267
- self
268
- }
269
-
270
- /// Configures the [`Node`] instance to source its wallet entropy from a [BIP 39] mnemonic.
271
- ///
272
- /// [BIP 39]: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
273
- pub fn set_entropy_bip39_mnemonic (
274
- & mut self , mnemonic : bip39:: Mnemonic , passphrase : Option < String > ,
275
- ) -> & mut Self {
276
- self . entropy_source_config =
277
- Some ( EntropySourceConfig :: Bip39Mnemonic { mnemonic, passphrase } ) ;
278
- self
281
+ pub fn set_gossip_source_rgs ( & self , rgs_server_url : String ) {
282
+ * self . gossip_source_config . lock ( ) . unwrap ( ) =
283
+ Some ( GossipSourceConfig :: RapidGossipSync ( rgs_server_url) ) ;
279
284
}
280
285
281
286
/// Sets the used storage directory path.
282
287
///
283
288
/// Default: `/tmp/ldk_node/`
284
- pub fn set_storage_dir_path ( & mut self , storage_dir_path : String ) -> & mut Self {
285
- self . config . storage_dir_path = storage_dir_path ;
286
- self
289
+ pub fn set_storage_dir_path ( & self , storage_dir_path : String ) {
290
+ let mut config = self . config . lock ( ) . unwrap ( ) ;
291
+ config . storage_dir_path = storage_dir_path ;
287
292
}
288
293
289
294
/// Sets the Esplora server URL.
290
295
///
291
296
/// Default: `https://blockstream.info/api`
292
- pub fn set_esplora_server_url ( & mut self , esplora_server_url : String ) -> & mut Self {
293
- self . config . esplora_server_url = esplora_server_url ;
294
- self
297
+ pub fn set_esplora_server_url ( & self , esplora_server_url : String ) {
298
+ let mut config = self . config . lock ( ) . unwrap ( ) ;
299
+ config . esplora_server_url = esplora_server_url ;
295
300
}
296
301
297
302
/// Sets the Bitcoin network used.
298
- ///
299
- /// Options: `mainnet`/`bitcoin`, `testnet`, `regtest`, `signet`
300
- ///
301
- /// Default: `regtest`
302
- pub fn set_network ( & mut self , network : & str ) -> & mut Self {
303
- self . config . network = Network :: from_str ( network) . unwrap_or ( Network :: Regtest ) ;
304
- self
303
+ pub fn set_network ( & self , network : Network ) {
304
+ let mut config = self . config . lock ( ) . unwrap ( ) ;
305
+ config. network = network;
305
306
}
306
307
307
308
/// Sets the IP address and TCP port on which [`Node`] will listen for incoming network connections.
308
309
///
309
310
/// Default: `0.0.0.0:9735`
310
- pub fn set_listening_address ( & mut self , listening_address : SocketAddr ) -> & mut Self {
311
- self . config . listening_address = Some ( listening_address ) ;
312
- self
311
+ pub fn set_listening_address ( & self , listening_address : SocketAddr ) {
312
+ let mut config = self . config . lock ( ) . unwrap ( ) ;
313
+ config . listening_address = Some ( listening_address ) ;
313
314
}
314
315
315
316
/// Builds a [`Node`] instance according to the options previously configured.
316
317
pub fn build ( & self ) -> Arc < Node > {
317
- let config = Arc :: new ( self . config . clone ( ) ) ;
318
+ let config = Arc :: new ( self . config . lock ( ) . unwrap ( ) . clone ( ) ) ;
318
319
319
320
let ldk_data_dir = format ! ( "{}/ldk" , config. storage_dir_path) ;
320
321
fs:: create_dir_all ( ldk_data_dir. clone ( ) ) . expect ( "Failed to create LDK data directory" ) ;
@@ -327,7 +328,9 @@ impl Builder {
327
328
let logger = Arc :: new ( FilesystemLogger :: new ( log_file_path) ) ;
328
329
329
330
// Initialize the on-chain wallet and chain access
330
- let seed_bytes = if let Some ( entropy_source_config) = & self . entropy_source_config {
331
+ let seed_bytes = if let Some ( entropy_source_config) =
332
+ & * self . entropy_source_config . lock ( ) . unwrap ( )
333
+ {
331
334
// Use the configured entropy source, if the user set one.
332
335
match entropy_source_config {
333
336
EntropySourceConfig :: SeedBytes ( bytes) => bytes. clone ( ) ,
@@ -534,8 +537,9 @@ impl Builder {
534
537
535
538
// Initialize the GossipSource
536
539
// Use the configured gossip source, if the user set one, otherwise default to P2PNetwork.
540
+ let gossip_source_config_lock = self . gossip_source_config . lock ( ) . unwrap ( ) ;
537
541
let gossip_source_config =
538
- self . gossip_source_config . as_ref ( ) . unwrap_or ( & GossipSourceConfig :: P2PNetwork ) ;
542
+ gossip_source_config_lock . as_ref ( ) . unwrap_or ( & GossipSourceConfig :: P2PNetwork ) ;
539
543
540
544
let gossip_source = match gossip_source_config {
541
545
GossipSourceConfig :: P2PNetwork => {
0 commit comments