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