@@ -73,6 +73,7 @@ use bdk::blockchain::esplora::EsploraBlockchain;
7373use bdk:: blockchain:: { GetBlockHash , GetHeight } ;
7474use bdk:: sled;
7575use bdk:: template:: Bip84 ;
76+ use bdk:: keys:: bip39;
7677
7778use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
7879use bitcoin:: hashes:: Hash ;
@@ -115,18 +116,28 @@ pub struct LdkLiteConfig {
115116 pub default_cltv_expiry_delta : u32 ,
116117}
117118
119+ #[ derive( Debug , Clone ) ]
120+ enum LdkLiteWalletEntropy {
121+ SeedFile ( String ) ,
122+ SeedBytes ( [ u8 ; 64 ] ) ,
123+ Bip39Mnemonic ( bip39:: Mnemonic ) ,
124+ }
125+
118126/// A builder for an [`LdkLite`] instance, allowing to set some configuration and module choices from
119127/// the getgo.
120128#[ derive( Debug , Clone ) ]
121129pub struct LdkLiteBuilder {
122130 config : LdkLiteConfig ,
131+ wallet_entropy : LdkLiteWalletEntropy ,
123132}
124133
125134impl LdkLiteBuilder {
126135 /// Creates a new builder instance with the default configuration.
127136 pub fn new ( ) -> Self {
128137 // Set the config defaults
129138 let storage_dir_path = "/tmp/ldk_lite/" . to_string ( ) ;
139+ let seed_path = format ! ( "{}/keys_seed" , storage_dir_path) ;
140+ let wallet_entropy = LdkLiteWalletEntropy :: SeedFile ( seed_path) ;
130141 let esplora_server_url = "https://blockstream.info/api" . to_string ( ) ;
131142 let network = bitcoin:: Network :: Testnet ;
132143 let listening_port = 9735 ;
@@ -140,12 +151,33 @@ impl LdkLiteBuilder {
140151 default_cltv_expiry_delta,
141152 } ;
142153
143- Self { config }
154+ Self { config, wallet_entropy }
144155 }
145156
146157 /// Creates a new builder instance from an [`LdkLiteConfig`].
147158 pub fn from_config ( config : LdkLiteConfig ) -> Self {
148- Self { config }
159+ let seed_path = format ! ( "{}/keys_seed" , config. storage_dir_path) ;
160+ let wallet_entropy = LdkLiteWalletEntropy :: SeedFile ( seed_path) ;
161+ Self { config, wallet_entropy }
162+ }
163+
164+ /// Configures [`LdkLite`] to source its wallet entropy from a seed file on disk.
165+ pub fn set_entropy_seed_path ( & mut self , seed_path : String ) -> & mut Self {
166+ self . wallet_entropy = LdkLiteWalletEntropy :: SeedFile ( seed_path) ;
167+ self
168+ }
169+
170+ /// Configures [`LdkLite`] to source its wallet entropy from a [`Bip39`] mnemonic code.
171+ pub fn set_entropy_bip39_mnemonic ( & mut self , mnemonic_str : String ) -> & mut Self {
172+ let mnemonic = bip39:: Mnemonic :: from_str ( mnemonic_str) . unwrap ( ) ;
173+ self . wallet_entropy = LdkLiteWalletEntropy :: Bip39Mnemonic ( mnemonic) ;
174+ self
175+ }
176+
177+ /// Configures [`LdkLite`] to source its wallet entropy from the given seed bytes.
178+ pub fn set_entropy_seed_bytes ( & mut self , seed_bytes : [ u8 ; 64 ] ) -> & mut Self {
179+ self . wallet_entropy = LdkLiteWalletEntropy :: SeedBytes ( seed_bytes) ;
180+ self
149181 }
150182
151183 /// Sets the used storage directory path.
@@ -204,8 +236,13 @@ impl LdkLiteBuilder {
204236 let logger = Arc :: new ( FilesystemLogger :: new ( log_file_path) ) ;
205237
206238 // Step 1: Initialize the on-chain wallet and chain access
207- let seed = io:: read_or_generate_seed_file ( Arc :: clone ( & config) ) ?;
208- let xprv = bitcoin:: util:: bip32:: ExtendedPrivKey :: new_master ( config. network , & seed) ?;
239+ let seed_bytes = match self . wallet_entropy {
240+ LdkLiteWalletEntropy :: SeedBytes ( bytes) => bytes,
241+ LdkLiteWalletEntropy :: SeedFile ( seed_path) => io:: read_or_generate_seed_file ( seed_path) ?,
242+ LdkLiteWalletEntropy :: Bip39Mnemonic ( mnemonic) => mnemonic. to_seed ( ) ,
243+ } ;
244+
245+ let xprv = bitcoin:: util:: bip32:: ExtendedPrivKey :: new_master ( config. network , & seed_bytes) ?;
209246
210247 let wallet_name = bdk:: wallet:: wallet_name_from_descriptor (
211248 Bip84 ( xprv. clone ( ) , bdk:: KeychainKind :: External ) ,
@@ -245,7 +282,8 @@ impl LdkLiteBuilder {
245282
246283 // Step 5: Initialize the KeysManager
247284 let cur = SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) ?;
248- let keys_manager = Arc :: new ( KeysManager :: new ( & seed, cur. as_secs ( ) , cur. subsec_nanos ( ) ) ) ;
285+ let ldk_seed: [ u8 ; 32 ] = xprv. private_key . secret_bytes ( ) ;
286+ let keys_manager = Arc :: new ( KeysManager :: new ( & ldk_seed, cur. as_secs ( ) , cur. subsec_nanos ( ) ) ) ;
249287
250288 // Step 6: Read ChannelMonitor state from disk
251289 let mut channel_monitors = persister. read_channelmonitors ( keys_manager. clone ( ) ) ?;
0 commit comments