Skip to content

Commit 01d715b

Browse files
committed
Refactor derive_xprv to make it reusble
.. and while we're at it we move the VSS child key indexes to constants.
1 parent 09bb098 commit 01d715b

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/builder.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ use std::sync::{Arc, Mutex, RwLock};
7575
use std::time::SystemTime;
7676
use vss_client::headers::{FixedHeaders, LnurlAuthToJwtProvider, VssHeaderProvider};
7777

78+
const VSS_HARDENED_CHILD_INDEX: u32 = 877;
79+
const VSS_LNURL_AUTH_HARDENED_CHILD_INDEX: u32 = 138;
80+
7881
#[derive(Debug, Clone)]
7982
enum ChainDataSourceConfig {
8083
Esplora { server_url: String, sync_config: Option<EsploraSyncConfig> },
@@ -492,10 +495,14 @@ impl NodeBuilder {
492495

493496
let config = Arc::new(self.config.clone());
494497

495-
let vss_xprv = derive_vss_xprv(config, &seed_bytes, Arc::clone(&logger))?;
498+
let vss_xprv =
499+
derive_xprv(config, &seed_bytes, VSS_HARDENED_CHILD_INDEX, Arc::clone(&logger))?;
496500

497501
let lnurl_auth_xprv = vss_xprv
498-
.derive_priv(&Secp256k1::new(), &[ChildNumber::Hardened { index: 138 }])
502+
.derive_priv(
503+
&Secp256k1::new(),
504+
&[ChildNumber::Hardened { index: VSS_LNURL_AUTH_HARDENED_CHILD_INDEX }],
505+
)
499506
.map_err(|e| {
500507
log_error!(logger, "Failed to derive VSS secret: {}", e);
501508
BuildError::KVStoreSetupFailed
@@ -557,7 +564,12 @@ impl NodeBuilder {
557564

558565
let config = Arc::new(self.config.clone());
559566

560-
let vss_xprv = derive_vss_xprv(config.clone(), &seed_bytes, Arc::clone(&logger))?;
567+
let vss_xprv = derive_xprv(
568+
config.clone(),
569+
&seed_bytes,
570+
VSS_HARDENED_CHILD_INDEX,
571+
Arc::clone(&logger),
572+
)?;
561573

562574
let vss_seed_bytes: [u8; 32] = vss_xprv.private_key.secret_bytes();
563575

@@ -1418,8 +1430,8 @@ fn seed_bytes_from_config(
14181430
}
14191431
}
14201432

1421-
fn derive_vss_xprv(
1422-
config: Arc<Config>, seed_bytes: &[u8; 64], logger: Arc<Logger>,
1433+
fn derive_xprv(
1434+
config: Arc<Config>, seed_bytes: &[u8; 64], hardened_child_index: u32, logger: Arc<Logger>,
14231435
) -> Result<Xpriv, BuildError> {
14241436
use bitcoin::key::Secp256k1;
14251437

@@ -1428,10 +1440,11 @@ fn derive_vss_xprv(
14281440
BuildError::InvalidSeedBytes
14291441
})?;
14301442

1431-
xprv.derive_priv(&Secp256k1::new(), &[ChildNumber::Hardened { index: 877 }]).map_err(|e| {
1432-
log_error!(logger, "Failed to derive VSS secret: {}", e);
1433-
BuildError::KVStoreSetupFailed
1434-
})
1443+
xprv.derive_priv(&Secp256k1::new(), &[ChildNumber::Hardened { index: hardened_child_index }])
1444+
.map_err(|e| {
1445+
log_error!(logger, "Failed to derive hardened child secret: {}", e);
1446+
BuildError::InvalidSeedBytes
1447+
})
14351448
}
14361449

14371450
/// Sanitize the user-provided node alias to ensure that it is a valid protocol-specified UTF-8 string.

0 commit comments

Comments
 (0)