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