@@ -73,6 +73,7 @@ use bdk::blockchain::esplora::EsploraBlockchain;
73
73
use bdk:: blockchain:: { GetBlockHash , GetHeight } ;
74
74
use bdk:: sled;
75
75
use bdk:: template:: Bip84 ;
76
+ use bdk:: keys:: bip39;
76
77
77
78
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
78
79
use bitcoin:: hashes:: Hash ;
@@ -114,18 +115,28 @@ pub struct LdkLiteConfig {
114
115
pub default_cltv_expiry_delta : u32 ,
115
116
}
116
117
118
+ #[ derive( Debug , Clone ) ]
119
+ enum LdkLiteWalletEntropy {
120
+ SeedFile ( String ) ,
121
+ SeedBytes ( [ u8 ; 64 ] ) ,
122
+ Bip39Mnemonic ( bip39:: Mnemonic ) ,
123
+ }
124
+
117
125
/// A builder for an [`LdkLite`] instance, allowing to set some configuration and module choices from
118
126
/// the getgo.
119
127
#[ derive( Debug , Clone ) ]
120
128
pub struct LdkLiteBuilder {
121
129
config : LdkLiteConfig ,
130
+ wallet_entropy : LdkLiteWalletEntropy ,
122
131
}
123
132
124
133
impl LdkLiteBuilder {
125
134
/// Creates a new builder instance with the default configuration.
126
135
pub fn new ( ) -> Self {
127
136
// Set the config defaults
128
137
let storage_dir_path = "/tmp/ldk_lite/" . to_string ( ) ;
138
+ let seed_path = format ! ( "{}/keys_seed" , storage_dir_path) ;
139
+ let wallet_entropy = LdkLiteWalletEntropy :: SeedFile ( seed_path) ;
129
140
let esplora_server_url = "https://blockstream.info/api" . to_string ( ) ;
130
141
let network = bitcoin:: Network :: Testnet ;
131
142
let listening_port = 9735 ;
@@ -139,12 +150,33 @@ impl LdkLiteBuilder {
139
150
default_cltv_expiry_delta,
140
151
} ;
141
152
142
- Self { config }
153
+ Self { config, wallet_entropy }
143
154
}
144
155
145
156
/// Creates a new builder instance from an [`LdkLiteConfig`].
146
157
pub fn from_config ( config : LdkLiteConfig ) -> Self {
147
- Self { config }
158
+ let seed_path = format ! ( "{}/keys_seed" , config. storage_dir_path) ;
159
+ let wallet_entropy = LdkLiteWalletEntropy :: SeedFile ( seed_path) ;
160
+ Self { config, wallet_entropy }
161
+ }
162
+
163
+ /// Configures [`LdkLite`] to source its wallet entropy from a seed file on disk.
164
+ pub fn set_entropy_seed_path ( & mut self , seed_path : String ) -> & mut Self {
165
+ self . wallet_entropy = LdkLiteWalletEntropy :: SeedFile ( seed_path) ;
166
+ self
167
+ }
168
+
169
+ /// Configures [`LdkLite`] to source its wallet entropy from a [`Bip39`] mnemonic code.
170
+ pub fn set_entropy_bip39_mnemonic ( & mut self , mnemonic_str : String ) -> & mut Self {
171
+ let mnemonic = bip39:: Mnemonic :: from_str ( mnemonic_str) . unwrap ( ) ;
172
+ self . wallet_entropy = LdkLiteWalletEntropy :: Bip39Mnemonic ( mnemonic) ;
173
+ self
174
+ }
175
+
176
+ /// Configures [`LdkLite`] to source its wallet entropy from the given seed bytes.
177
+ pub fn set_entropy_seed_bytes ( & mut self , seed_bytes : [ u8 ; 64 ] ) -> & mut Self {
178
+ self . wallet_entropy = LdkLiteWalletEntropy :: SeedBytes ( seed_bytes) ;
179
+ self
148
180
}
149
181
150
182
/// Sets the used storage directory path.
@@ -203,8 +235,13 @@ impl LdkLiteBuilder {
203
235
let logger = Arc :: new ( FilesystemLogger :: new ( log_file_path) ) ;
204
236
205
237
// Step 1: Initialize the on-chain wallet and chain access
206
- let seed = io:: read_or_generate_seed_file ( Arc :: clone ( & config) ) ?;
207
- let xprv = bitcoin:: util:: bip32:: ExtendedPrivKey :: new_master ( config. network , & seed) ?;
238
+ let seed_bytes = match self . wallet_entropy {
239
+ LdkLiteWalletEntropy :: SeedBytes ( bytes) => bytes,
240
+ LdkLiteWalletEntropy :: SeedFile ( seed_path) => io:: read_or_generate_seed_file ( seed_path) ?,
241
+ LdkLiteWalletEntropy :: Bip39Mnemonic ( mnemonic) => mnemonic. to_seed ( ) ,
242
+ } ;
243
+
244
+ let xprv = bitcoin:: util:: bip32:: ExtendedPrivKey :: new_master ( config. network , & seed_bytes) ?;
208
245
209
246
let wallet_name = bdk:: wallet:: wallet_name_from_descriptor (
210
247
Bip84 ( xprv. clone ( ) , bdk:: KeychainKind :: External ) ,
@@ -244,7 +281,8 @@ impl LdkLiteBuilder {
244
281
245
282
// Step 5: Initialize the KeysManager
246
283
let cur = SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) ?;
247
- let keys_manager = Arc :: new ( KeysManager :: new ( & seed, cur. as_secs ( ) , cur. subsec_nanos ( ) ) ) ;
284
+ let ldk_seed: [ u8 ; 32 ] = xprv. private_key . secret_bytes ( ) ;
285
+ let keys_manager = Arc :: new ( KeysManager :: new ( & ldk_seed, cur. as_secs ( ) , cur. subsec_nanos ( ) ) ) ;
248
286
249
287
// Step 6: Read ChannelMonitor state from disk
250
288
let mut channel_monitors = persister. read_channelmonitors ( keys_manager. clone ( ) ) ?;
0 commit comments