Skip to content

Commit d9638e5

Browse files
committed
Overhaul the internal/external key derive switch
1 parent 1090502 commit d9638e5

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/wallet/wallet.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,19 @@ void CWallet::DeriveNewChildKey(CKeyMetadata& metadata, CKey& secret, bool inter
145145
// always derive hardened keys
146146
// childIndex | BIP32_HARDENED_KEY_LIMIT = derive childIndex in hardened child-index-range
147147
// example: 1 | BIP32_HARDENED_KEY_LIMIT == 0x80000001 == 2147483649
148-
chainChildKey.Derive(childKey, (internal ? hdChain.nInternalChainCounter : hdChain.nExternalChainCounter) | BIP32_HARDENED_KEY_LIMIT);
149-
metadata.hdKeypath = "m/0'/" + std::string(internal ? "1'/"+ std::to_string(hdChain.nInternalChainCounter) : "0'/" + std::to_string(hdChain.nExternalChainCounter)) + "'";
150-
metadata.hdMasterKeyID = hdChain.masterKeyID;
151-
// increment childkey index
152-
if (internal)
148+
if (internal) {
149+
chainChildKey.Derive(childKey, hdChain.nInternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
150+
metadata.hdKeypath = "m/0'/1'/" + std::to_string(hdChain.nInternalChainCounter) + "'";
153151
hdChain.nInternalChainCounter++;
154-
else
152+
}
153+
else {
154+
chainChildKey.Derive(childKey, hdChain.nExternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
155+
metadata.hdKeypath = "m/0'/0'/" + std::to_string(hdChain.nExternalChainCounter) + "'";
155156
hdChain.nExternalChainCounter++;
157+
}
156158
} while (HaveKey(childKey.key.GetPubKey().GetID()));
157159
secret = childKey.key;
158-
160+
metadata.hdMasterKeyID = hdChain.masterKeyID;
159161
// update the chain model in the database
160162
if (!CWalletDB(strWalletFile).WriteHDChain(hdChain))
161163
throw std::runtime_error(std::string(__func__) + ": Writing HD chain model failed");

0 commit comments

Comments
 (0)