Skip to content

Commit 6c29bbf

Browse files
committed
Introduce constants for derivation path indices
This commit replaces magic numbers with descriptive constant names for the indices used in key derivation paths within the `new` function. - Added constants: - `NODE_SECRET_INDEX` - `DESTINATION_SCRIPT_INDEX` - `SHUTDOWN_PUBKEY_INDEX` - `CHANNEL_MASTER_KEY_INDEX` - `INBOUND_PAYMENT_KEY_INDEX` - `PEER_STORAGE_KEY_INDEX`
1 parent 1686208 commit 6c29bbf

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

lightning/src/sign/mod.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,18 +1860,30 @@ impl KeysManager {
18601860
///
18611861
/// [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
18621862
pub fn new(seed: &[u8; 32], starting_time_secs: u64, starting_time_nanos: u32) -> Self {
1863+
// Constants for key derivation path indices used in this function.
1864+
const NODE_SECRET_INDEX: u32 = 0;
1865+
const DESTINATION_SCRIPT_INDEX: u32 = 1;
1866+
const SHUTDOWN_PUBKEY_INDEX: u32 = 2;
1867+
const CHANNEL_MASTER_KEY_INDEX: u32 = 3;
1868+
const INBOUND_PAYMENT_KEY_INDEX: u32 = 5;
1869+
const PEER_STORAGE_KEY_INDEX: u32 = 6;
1870+
18631871
let secp_ctx = Secp256k1::new();
18641872
// Note that when we aren't serializing the key, network doesn't matter
18651873
match Xpriv::new_master(Network::Testnet, seed) {
18661874
Ok(master_key) => {
18671875
let node_secret = master_key
1868-
.derive_priv(&secp_ctx, &ChildNumber::from_hardened_idx(0).unwrap())
1876+
.derive_priv(
1877+
&secp_ctx,
1878+
&ChildNumber::from_hardened_idx(NODE_SECRET_INDEX).unwrap(),
1879+
)
18691880
.expect("Your RNG is busted")
18701881
.private_key;
18711882
let node_id = PublicKey::from_secret_key(&secp_ctx, &node_secret);
1872-
let destination_script = match master_key
1873-
.derive_priv(&secp_ctx, &ChildNumber::from_hardened_idx(1).unwrap())
1874-
{
1883+
let destination_script = match master_key.derive_priv(
1884+
&secp_ctx,
1885+
&ChildNumber::from_hardened_idx(DESTINATION_SCRIPT_INDEX).unwrap(),
1886+
) {
18751887
Ok(destination_key) => {
18761888
let wpubkey_hash = WPubkeyHash::hash(
18771889
&Xpub::from_priv(&secp_ctx, &destination_key).to_pub().to_bytes(),
@@ -1883,23 +1895,33 @@ impl KeysManager {
18831895
},
18841896
Err(_) => panic!("Your RNG is busted"),
18851897
};
1886-
let shutdown_pubkey = match master_key
1887-
.derive_priv(&secp_ctx, &ChildNumber::from_hardened_idx(2).unwrap())
1888-
{
1898+
let shutdown_pubkey = match master_key.derive_priv(
1899+
&secp_ctx,
1900+
&ChildNumber::from_hardened_idx(SHUTDOWN_PUBKEY_INDEX).unwrap(),
1901+
) {
18891902
Ok(shutdown_key) => Xpub::from_priv(&secp_ctx, &shutdown_key).public_key,
18901903
Err(_) => panic!("Your RNG is busted"),
18911904
};
18921905
let channel_master_key = master_key
1893-
.derive_priv(&secp_ctx, &ChildNumber::from_hardened_idx(3).unwrap())
1906+
.derive_priv(
1907+
&secp_ctx,
1908+
&ChildNumber::from_hardened_idx(CHANNEL_MASTER_KEY_INDEX).unwrap(),
1909+
)
18941910
.expect("Your RNG is busted");
18951911
let inbound_payment_key: SecretKey = master_key
1896-
.derive_priv(&secp_ctx, &ChildNumber::from_hardened_idx(5).unwrap())
1912+
.derive_priv(
1913+
&secp_ctx,
1914+
&ChildNumber::from_hardened_idx(INBOUND_PAYMENT_KEY_INDEX).unwrap(),
1915+
)
18971916
.expect("Your RNG is busted")
18981917
.private_key;
18991918
let mut inbound_pmt_key_bytes = [0; 32];
19001919
inbound_pmt_key_bytes.copy_from_slice(&inbound_payment_key[..]);
1901-
let peer_storage_key: SecretKey = master_key
1902-
.derive_priv(&secp_ctx, &ChildNumber::from_hardened_idx(6).unwrap())
1920+
let peer_storage_key = master_key
1921+
.derive_priv(
1922+
&secp_ctx,
1923+
&ChildNumber::from_hardened_idx(PEER_STORAGE_KEY_INDEX).unwrap(),
1924+
)
19031925
.expect("Your RNG is busted")
19041926
.private_key;
19051927

0 commit comments

Comments
 (0)