@@ -71,6 +71,7 @@ use lightning_invoice::{payment, Currency, Invoice};
71
71
use bdk:: blockchain:: esplora:: EsploraBlockchain ;
72
72
use bdk:: blockchain:: { GetBlockHash , GetHeight } ;
73
73
use bdk:: database:: MemoryDatabase ;
74
+ use bdk:: keys:: bip39;
74
75
75
76
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
76
77
use bitcoin:: hashes:: Hash ;
@@ -115,18 +116,28 @@ pub struct LdkLiteConfig {
115
116
pub default_cltv_expiry_delta : u32 ,
116
117
}
117
118
119
+ #[ derive( Debug , Clone ) ]
120
+ enum LdkLiteWalletEntropy {
121
+ SeedFile ( String ) ,
122
+ SeedBytes ( [ u8 ; 64 ] ) ,
123
+ Bip39Mnemonic ( bip39:: Mnemonic ) ,
124
+ }
125
+
118
126
/// A builder for an [`LdkLite`] instance, allowing to set some configuration and module choices from
119
127
/// the getgo.
120
128
#[ derive( Debug , Clone ) ]
121
129
pub struct LdkLiteBuilder {
122
130
config : LdkLiteConfig ,
131
+ wallet_entropy : LdkLiteWalletEntropy ,
123
132
}
124
133
125
134
impl LdkLiteBuilder {
126
135
/// Creates a new builder instance with the default configuration.
127
136
pub fn new ( ) -> Self {
128
137
// Set the config defaults
129
138
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) ;
130
141
let esplora_server_url = "https://blockstream.info/api" . to_string ( ) ;
131
142
let network = bitcoin:: Network :: Testnet ;
132
143
let listening_port = 9735 ;
@@ -140,12 +151,33 @@ impl LdkLiteBuilder {
140
151
default_cltv_expiry_delta,
141
152
} ;
142
153
143
- Self { config }
154
+ Self { config, wallet_entropy }
144
155
}
145
156
146
157
/// Creates a new builder instance from an [`LdkLiteConfig`].
147
158
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
149
181
}
150
182
151
183
/// Sets the used storage directory path.
@@ -204,8 +236,13 @@ impl LdkLiteBuilder {
204
236
let logger = Arc :: new ( FilesystemLogger :: new ( log_file_path) ) ;
205
237
206
238
// 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) ?;
209
246
210
247
let bdk_wallet = bdk:: Wallet :: new (
211
248
bdk:: template:: Bip84 ( xprv. clone ( ) , bdk:: KeychainKind :: External ) ,
@@ -236,7 +273,8 @@ impl LdkLiteBuilder {
236
273
237
274
// Step 5: Initialize the KeysManager
238
275
let cur = SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) ?;
239
- let keys_manager = Arc :: new ( KeysManager :: new ( & seed, cur. as_secs ( ) , cur. subsec_nanos ( ) ) ) ;
276
+ let ldk_seed: [ u8 ; 32 ] = xprv. private_key . secret_bytes ( ) ;
277
+ let keys_manager = Arc :: new ( KeysManager :: new ( & ldk_seed, cur. as_secs ( ) , cur. subsec_nanos ( ) ) ) ;
240
278
241
279
// Step 6: Read ChannelMonitor state from disk
242
280
let mut channel_monitors = persister. read_channelmonitors ( keys_manager. clone ( ) ) ?;
0 commit comments