File tree Expand file tree Collapse file tree 3 files changed +36
-1
lines changed Expand file tree Collapse file tree 3 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -123,6 +123,33 @@ CPubKey CWallet::GenerateNewKey()
123123 return pubkey;
124124}
125125
126+ void CWallet::DeriveBlindingKey (CKeyMetadata& metadata, CKey& secret)
127+ {
128+ // Key is derived at m/0'/2' to avoid collision with hd split wallets
129+ CKey key; // master key seed (256bit)
130+ CExtKey masterKey; // hd master key
131+ CExtKey accountKey; // key at m/0'
132+ CExtKey externalChainChildKey; // key at m/0'/2'
133+
134+ // try to get the master key
135+ if (!GetKey (hdChain.masterKeyID , key))
136+ throw std::runtime_error (std::string (__func__) + " : Master key not found" );
137+
138+ masterKey.SetMaster (key.begin (), key.size ());
139+
140+ // derive m/0'
141+ // use hardened derivation (child keys >= 0x80000000 are hardened after bip32)
142+ masterKey.Derive (accountKey, BIP32_HARDENED_KEY_LIMIT);
143+
144+ // derive m/0'/2'
145+ accountKey.Derive (externalChainChildKey, 2 | BIP32_HARDENED_KEY_LIMIT);
146+
147+ metadata.hdKeypath = " m/0'/2'" ;
148+ metadata.hdMasterKeyID = hdChain.masterKeyID ;
149+ secret = externalChainChildKey.key ;
150+ return ;
151+ }
152+
126153void CWallet::DeriveNewChildKey (CKeyMetadata& metadata, CKey& secret)
127154{
128155 // for now we use a fixed keypath scheme of m/0'/0'/k
Original file line number Diff line number Diff line change @@ -793,6 +793,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
793793 */
794794 CPubKey GenerateNewKey ();
795795 void DeriveNewChildKey (CKeyMetadata& metadata, CKey& secret);
796+ // ! Derives static blinding key at m/0'/2'
797+ void DeriveBlindingKey (CKeyMetadata& metadata, CKey& secret);
796798 // ! Adds a key to the store, and saves it to disk.
797799 bool AddKeyPubKey (const CKey& key, const CPubKey &pubkey) override ;
798800 // ! 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