@@ -109,6 +109,9 @@ const LDK_PAYMENT_RETRY_TIMEOUT: Duration = Duration::from_secs(10);
109109// The time in between peer reconnection attempts.
110110const PEER_RECONNECTION_INTERVAL : Duration = Duration :: from_secs ( 10 ) ;
111111
112+ // The length in bytes of our wallets' keys seed.
113+ const WALLET_KEYS_SEED_LEN : usize = 64 ;
114+
112115#[ derive( Debug , Clone ) ]
113116/// Represents the configuration of an [`Node`] instance.
114117pub struct Config {
@@ -136,24 +139,47 @@ impl Default for Config {
136139 }
137140}
138141
142+ #[ derive( Debug , Clone ) ]
143+ enum WalletEntropySource {
144+ SeedFile ( String ) ,
145+ SeedBytes ( [ u8 ; WALLET_KEYS_SEED_LEN ] ) ,
146+ }
147+
139148/// A builder for an [`Node`] instance, allowing to set some configuration and module choices from
140149/// the getgo.
141150#[ derive( Debug , Clone ) ]
142151pub struct Builder {
143152 config : Config ,
153+ entropy_source : Option < WalletEntropySource > ,
144154}
145155
146156impl Builder {
147157 /// Creates a new builder instance with the default configuration.
148158 pub fn new ( ) -> Self {
149159 let config = Config :: default ( ) ;
150-
151- Self { config }
160+ let entropy_source = None ;
161+ Self { config, entropy_source }
152162 }
153163
154164 /// Creates a new builder instance from an [`Config`].
155165 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
157183 }
158184
159185 /// Sets the used storage directory path.
@@ -213,8 +239,21 @@ impl Builder {
213239 let logger = Arc :: new ( FilesystemLogger :: new ( log_file_path) ) ;
214240
215241 // 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)
218257 . expect ( "Failed to read wallet master key" ) ;
219258
220259 let wallet_name = bdk:: wallet:: wallet_name_from_descriptor (
@@ -263,8 +302,9 @@ impl Builder {
263302 let cur_time = SystemTime :: now ( )
264303 . duration_since ( SystemTime :: UNIX_EPOCH )
265304 . expect ( "System time error: Clock may have gone backwards" ) ;
305+ let ldk_seed_bytes: [ u8 ; 32 ] = xprv. private_key . secret_bytes ( ) ;
266306 let keys_manager = Arc :: new ( KeysManager :: new (
267- & seed ,
307+ & ldk_seed_bytes ,
268308 cur_time. as_secs ( ) ,
269309 cur_time. subsec_nanos ( ) ,
270310 Arc :: clone ( & wallet) ,
0 commit comments