@@ -1856,46 +1856,52 @@ impl KeysManager {
18561856 ///
18571857 /// [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
18581858 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+
18591867 let secp_ctx = Secp256k1 :: new ( ) ;
18601868 // Note that when we aren't serializing the key, network doesn't matter
18611869 match Xpriv :: new_master ( Network :: Testnet , seed) {
18621870 Ok ( master_key) => {
18631871 let node_secret = master_key
1864- . derive_priv ( & secp_ctx, & ChildNumber :: from_hardened_idx ( 0 ) . unwrap ( ) )
1872+ . derive_priv ( & secp_ctx, & NODE_SECRET_INDEX )
18651873 . expect ( "Your RNG is busted" )
18661874 . private_key ;
18671875 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+ } ;
18881894 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 )
18901896 . expect ( "Your RNG is busted" ) ;
18911897 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 )
18931899 . expect ( "Your RNG is busted" )
18941900 . private_key ;
18951901 let mut inbound_pmt_key_bytes = [ 0 ; 32 ] ;
18961902 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 )
18991905 . expect ( "Your RNG is busted" )
19001906 . private_key ;
19011907
0 commit comments