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