File tree Expand file tree Collapse file tree 3 files changed +37
-1
lines changed Expand file tree Collapse file tree 3 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -124,6 +124,34 @@ CPubKey CWallet::GenerateNewKey()
124124    return  pubkey;
125125}
126126
127+ void  CWallet::DeriveBlindingKey (CKeyMetadata& metadata, CKey& secret)
128+ {
129+     //  for now we use a fixed keypath scheme of m/0'/0'/k
130+     CKey key;                      // master key seed (256bit)
131+     CExtKey masterKey;             // hd master key
132+     CExtKey accountKey;            // key at m/0'
133+     CExtKey externalChainChildKey; // key at m/0'/0'
134+     CExtKey childKey;              // key at m/0'/0'/<n>'
135+ 
136+     //  try to get the master key
137+     if  (!GetKey (hdChain.masterKeyID , key))
138+         throw  std::runtime_error (std::string (__func__) + " : Master key not found" 
139+ 
140+     masterKey.SetMaster (key.begin (), key.size ());
141+ 
142+     //  derive m/0'
143+     //  use hardened derivation (child keys >= 0x80000000 are hardened after bip32)
144+     masterKey.Derive (accountKey, BIP32_HARDENED_KEY_LIMIT);
145+ 
146+     //  derive m/0'/1'
147+     accountKey.Derive (externalChainChildKey, 1  | BIP32_HARDENED_KEY_LIMIT);
148+ 
149+     metadata.hdKeypath  = " m/0'/1'" 
150+     metadata.hdMasterKeyID  = hdChain.masterKeyID ;
151+     secret = externalChainChildKey.key ;
152+     return ;
153+ }
154+ 
127155void  CWallet::DeriveNewChildKey (CKeyMetadata& metadata, CKey& secret)
128156{
129157    //  for now we use a fixed keypath scheme of m/0'/0'/k
Original file line number Diff line number Diff line change @@ -796,6 +796,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
796796     */  
797797    CPubKey GenerateNewKey ();
798798    void  DeriveNewChildKey (CKeyMetadata& metadata, CKey& secret);
799+     // ! Derives static blinding key at m/0'/1'
800+     void  DeriveBlindingKey (CKeyMetadata& metadata, CKey& secret);
799801    // ! Adds a key to the store, and saves it to disk.
800802    bool  AddKeyPubKey (const  CKey& key, const  CPubKey &pubkey) override ;
801803    // ! Adds a key to the store, without saving it to disk (used by LoadWallet)
Original file line number Diff line number Diff line change @@ -686,7 +686,13 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
686686
687687    if  (result == DB_LOAD_OK && pwallet->blinding_derivation_key .IsNull ()) {
688688        CKey key;
689-         key.MakeNewKey (true );
689+         if  (pwallet->IsHDEnabled ()) {
690+             int64_t  nCreationTime = GetTime ();
691+             CKeyMetadata metadata (nCreationTime);
692+             pwallet->DeriveBlindingKey (metadata, key);
693+         } else  {
694+             key.MakeNewKey (true );
695+         }
690696        uint256 keybin;
691697        memcpy (keybin.begin (), key.begin (), key.size ());
692698        pwallet->blinding_derivation_key  = keybin;
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments