@@ -109,6 +109,9 @@ const LDK_PAYMENT_RETRY_TIMEOUT: Duration = Duration::from_secs(10);
109
109
// The time in between peer reconnection attempts.
110
110
const PEER_RECONNECTION_INTERVAL : Duration = Duration :: from_secs ( 10 ) ;
111
111
112
+ // The length in bytes of our wallets' keys seed.
113
+ const WALLET_KEYS_SEED_LEN : usize = 64 ;
114
+
112
115
#[ derive( Debug , Clone ) ]
113
116
/// Represents the configuration of an [`Node`] instance.
114
117
pub struct Config {
@@ -136,24 +139,47 @@ impl Default for Config {
136
139
}
137
140
}
138
141
142
+ #[ derive( Debug , Clone ) ]
143
+ enum WalletEntropySource {
144
+ SeedFile ( String ) ,
145
+ SeedBytes ( [ u8 ; WALLET_KEYS_SEED_LEN ] ) ,
146
+ }
147
+
139
148
/// A builder for an [`Node`] instance, allowing to set some configuration and module choices from
140
149
/// the getgo.
141
150
#[ derive( Debug , Clone ) ]
142
151
pub struct Builder {
143
152
config : Config ,
153
+ entropy_source : Option < WalletEntropySource > ,
144
154
}
145
155
146
156
impl Builder {
147
157
/// Creates a new builder instance with the default configuration.
148
158
pub fn new ( ) -> Self {
149
159
let config = Config :: default ( ) ;
150
-
151
- Self { config }
160
+ let entropy_source = None ;
161
+ Self { config, entropy_source }
152
162
}
153
163
154
164
/// Creates a new builder instance from an [`Config`].
155
165
pub fn from_config ( config : Config ) -> Self {
156
- Self { config }
166
+ let entropy_source = None ;
167
+ Self { config, entropy_source }
168
+ }
169
+
170
+ /// Configures the [`Node`] instance to source its wallet entropy from a seed file on disk.
171
+ ///
172
+ /// If the given file does not exist a new random seed file will be generated and
173
+ /// stored at the given location.
174
+ pub fn set_entropy_seed_path ( & mut self , seed_path : String ) -> & mut Self {
175
+ self . entropy_source = Some ( WalletEntropySource :: SeedFile ( seed_path) ) ;
176
+ self
177
+ }
178
+
179
+ /// Configures the [`Node`] instance to source its wallet entropy from the given seed bytes.
180
+ pub fn set_entropy_seed_bytes ( & mut self , seed_bytes : [ u8 ; WALLET_KEYS_SEED_LEN ] ) -> & mut Self {
181
+ self . entropy_source = Some ( WalletEntropySource :: SeedBytes ( seed_bytes) ) ;
182
+ self
157
183
}
158
184
159
185
/// Sets the used storage directory path.
@@ -213,8 +239,21 @@ impl Builder {
213
239
let logger = Arc :: new ( FilesystemLogger :: new ( log_file_path) ) ;
214
240
215
241
// Initialize the on-chain wallet and chain access
216
- let seed = io_utils:: read_or_generate_seed_file ( config. as_ref ( ) ) ;
217
- let xprv = bitcoin:: util:: bip32:: ExtendedPrivKey :: new_master ( config. network , & seed)
242
+ let seed_bytes = if let Some ( entropy_source) = & self . entropy_source {
243
+ // Use the configured entropy source, if the user set one.
244
+ match entropy_source {
245
+ WalletEntropySource :: SeedBytes ( bytes) => bytes. clone ( ) ,
246
+ WalletEntropySource :: SeedFile ( seed_path) => {
247
+ io_utils:: read_or_generate_seed_file ( seed_path)
248
+ }
249
+ }
250
+ } else {
251
+ // Default to read or generate from the default location generate a seed file.
252
+ let seed_path = format ! ( "{}/keys_seed" , config. storage_dir_path) ;
253
+ io_utils:: read_or_generate_seed_file ( & seed_path)
254
+ } ;
255
+
256
+ let xprv = bitcoin:: util:: bip32:: ExtendedPrivKey :: new_master ( config. network , & seed_bytes)
218
257
. expect ( "Failed to read wallet master key" ) ;
219
258
220
259
let wallet_name = bdk:: wallet:: wallet_name_from_descriptor (
@@ -263,8 +302,9 @@ impl Builder {
263
302
let cur_time = SystemTime :: now ( )
264
303
. duration_since ( SystemTime :: UNIX_EPOCH )
265
304
. expect ( "System time error: Clock may have gone backwards" ) ;
305
+ let ldk_seed_bytes: [ u8 ; 32 ] = xprv. private_key . secret_bytes ( ) ;
266
306
let keys_manager = Arc :: new ( KeysManager :: new (
267
- & seed ,
307
+ & ldk_seed_bytes ,
268
308
cur_time. as_secs ( ) ,
269
309
cur_time. subsec_nanos ( ) ,
270
310
Arc :: clone ( & wallet) ,
0 commit comments