Skip to content

Commit bb3c8bf

Browse files
committed
f Allow to specify the seed bytes or a seed file location
1 parent 26d50a3 commit bb3c8bf

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

src/io_utils.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Config, FilesystemLogger, NetworkGraph, Scorer};
1+
use crate::{Config, FilesystemLogger, NetworkGraph, Scorer, WALLET_KEYS_SEED_LEN};
22

33
use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParameters};
44
use lightning::util::ser::ReadableArgs;
@@ -10,16 +10,19 @@ use std::io::{BufReader, Write};
1010
use std::path::Path;
1111
use std::sync::Arc;
1212

13-
pub(crate) fn read_or_generate_seed_file(config: &Config) -> [u8; 32] {
14-
let keys_seed_path = format!("{}/keys_seed", config.storage_dir_path);
13+
pub(crate) fn read_or_generate_seed_file(keys_seed_path: &str) -> [u8; WALLET_KEYS_SEED_LEN] {
1514
if Path::new(&keys_seed_path).exists() {
1615
let seed = fs::read(keys_seed_path).expect("Failed to read keys seed file");
17-
assert_eq!(seed.len(), 32);
18-
let mut key = [0; 32];
16+
assert_eq!(
17+
seed.len(),
18+
WALLET_KEYS_SEED_LEN,
19+
"Failed to read keys seed file: unexpected length"
20+
);
21+
let mut key = [0; WALLET_KEYS_SEED_LEN];
1922
key.copy_from_slice(&seed);
2023
key
2124
} else {
22-
let mut key = [0; 32];
25+
let mut key = [0; WALLET_KEYS_SEED_LEN];
2326
thread_rng().fill_bytes(&mut key);
2427

2528
let mut f = fs::File::create(keys_seed_path).expect("Failed to create keys seed file");

src/lib.rs

+46-6
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ const LDK_PAYMENT_RETRY_TIMEOUT: Duration = Duration::from_secs(10);
109109
// The time in between peer reconnection attempts.
110110
const 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.
114117
pub 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)]
142151
pub struct Builder {
143152
config: Config,
153+
entropy_source: Option<WalletEntropySource>,
144154
}
145155

146156
impl 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

Comments
 (0)