@@ -1856,46 +1856,52 @@ impl KeysManager {
1856
1856
///
1857
1857
/// [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
1858
1858
pub fn new ( seed : & [ u8 ; 32 ] , starting_time_secs : u64 , starting_time_nanos : u32 ) -> Self {
1859
+ // Constants for key derivation path indices used in this function.
1860
+ const NODE_SECRET_INDEX : ChildNumber = ChildNumber :: Hardened { index : 0 } ;
1861
+ const DESTINATION_SCRIPT_INDEX : ChildNumber = ChildNumber :: Hardened { index : ( 1 ) } ;
1862
+ const SHUTDOWN_PUBKEY_INDEX : ChildNumber = ChildNumber :: Hardened { index : ( 2 ) } ;
1863
+ const CHANNEL_MASTER_KEY_INDEX : ChildNumber = ChildNumber :: Hardened { index : ( 3 ) } ;
1864
+ const INBOUND_PAYMENT_KEY_INDEX : ChildNumber = ChildNumber :: Hardened { index : ( 5 ) } ;
1865
+ const PEER_STORAGE_KEY_INDEX : ChildNumber = ChildNumber :: Hardened { index : ( 6 ) } ;
1866
+
1859
1867
let secp_ctx = Secp256k1 :: new ( ) ;
1860
1868
// Note that when we aren't serializing the key, network doesn't matter
1861
1869
match Xpriv :: new_master ( Network :: Testnet , seed) {
1862
1870
Ok ( master_key) => {
1863
1871
let node_secret = master_key
1864
- . derive_priv ( & secp_ctx, & ChildNumber :: from_hardened_idx ( 0 ) . unwrap ( ) )
1872
+ . derive_priv ( & secp_ctx, & NODE_SECRET_INDEX )
1865
1873
. expect ( "Your RNG is busted" )
1866
1874
. private_key ;
1867
1875
let node_id = PublicKey :: from_secret_key ( & secp_ctx, & node_secret) ;
1868
- let destination_script = match master_key
1869
- . derive_priv ( & secp_ctx, & ChildNumber :: from_hardened_idx ( 1 ) . unwrap ( ) )
1870
- {
1871
- Ok ( destination_key) => {
1872
- let wpubkey_hash = WPubkeyHash :: hash (
1873
- & Xpub :: from_priv ( & secp_ctx, & destination_key) . to_pub ( ) . to_bytes ( ) ,
1874
- ) ;
1875
- Builder :: new ( )
1876
- . push_opcode ( opcodes:: all:: OP_PUSHBYTES_0 )
1877
- . push_slice ( & wpubkey_hash. to_byte_array ( ) )
1878
- . into_script ( )
1879
- } ,
1880
- Err ( _) => panic ! ( "Your RNG is busted" ) ,
1881
- } ;
1882
- let shutdown_pubkey = match master_key
1883
- . derive_priv ( & secp_ctx, & ChildNumber :: from_hardened_idx ( 2 ) . unwrap ( ) )
1884
- {
1885
- Ok ( shutdown_key) => Xpub :: from_priv ( & secp_ctx, & shutdown_key) . public_key ,
1886
- Err ( _) => panic ! ( "Your RNG is busted" ) ,
1887
- } ;
1876
+ let destination_script =
1877
+ match master_key. derive_priv ( & secp_ctx, & DESTINATION_SCRIPT_INDEX ) {
1878
+ Ok ( destination_key) => {
1879
+ let wpubkey_hash = WPubkeyHash :: hash (
1880
+ & Xpub :: from_priv ( & secp_ctx, & destination_key) . to_pub ( ) . to_bytes ( ) ,
1881
+ ) ;
1882
+ Builder :: new ( )
1883
+ . push_opcode ( opcodes:: all:: OP_PUSHBYTES_0 )
1884
+ . push_slice ( & wpubkey_hash. to_byte_array ( ) )
1885
+ . into_script ( )
1886
+ } ,
1887
+ Err ( _) => panic ! ( "Your RNG is busted" ) ,
1888
+ } ;
1889
+ let shutdown_pubkey =
1890
+ match master_key. derive_priv ( & secp_ctx, & SHUTDOWN_PUBKEY_INDEX ) {
1891
+ Ok ( shutdown_key) => Xpub :: from_priv ( & secp_ctx, & shutdown_key) . public_key ,
1892
+ Err ( _) => panic ! ( "Your RNG is busted" ) ,
1893
+ } ;
1888
1894
let channel_master_key = master_key
1889
- . derive_priv ( & secp_ctx, & ChildNumber :: from_hardened_idx ( 3 ) . unwrap ( ) )
1895
+ . derive_priv ( & secp_ctx, & CHANNEL_MASTER_KEY_INDEX )
1890
1896
. expect ( "Your RNG is busted" ) ;
1891
1897
let inbound_payment_key: SecretKey = master_key
1892
- . derive_priv ( & secp_ctx, & ChildNumber :: from_hardened_idx ( 5 ) . unwrap ( ) )
1898
+ . derive_priv ( & secp_ctx, & INBOUND_PAYMENT_KEY_INDEX )
1893
1899
. expect ( "Your RNG is busted" )
1894
1900
. private_key ;
1895
1901
let mut inbound_pmt_key_bytes = [ 0 ; 32 ] ;
1896
1902
inbound_pmt_key_bytes. copy_from_slice ( & inbound_payment_key[ ..] ) ;
1897
- let peer_storage_key: SecretKey = master_key
1898
- . derive_priv ( & secp_ctx, & ChildNumber :: from_hardened_idx ( 6 ) . unwrap ( ) )
1903
+ let peer_storage_key = master_key
1904
+ . derive_priv ( & secp_ctx, & PEER_STORAGE_KEY_INDEX )
1899
1905
. expect ( "Your RNG is busted" )
1900
1906
. private_key ;
1901
1907
0 commit comments