Skip to content

Commit d3599c2

Browse files
committed
spkman: don't ignore the return value when deriving an extended key
1 parent 6d8707b commit d3599c2

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,13 @@ CPubKey LegacyScriptPubKeyMan::GenerateNewKey(WalletBatch &batch, CHDChain& hd_c
10841084
return pubkey;
10851085
}
10861086

1087+
//! Try to derive an extended key, throw if it fails.
1088+
static void DeriveExtKey(CExtKey& key_in, unsigned int index, CExtKey& key_out) {
1089+
if (!key_in.Derive(key_out, index)) {
1090+
throw std::runtime_error("Could not derive extended key");
1091+
}
1092+
}
1093+
10871094
void LegacyScriptPubKeyMan::DeriveNewChildKey(WalletBatch &batch, CKeyMetadata& metadata, CKey& secret, CHDChain& hd_chain, bool internal)
10881095
{
10891096
// for now we use a fixed keypath scheme of m/0'/0'/k
@@ -1101,27 +1108,27 @@ void LegacyScriptPubKeyMan::DeriveNewChildKey(WalletBatch &batch, CKeyMetadata&
11011108

11021109
// derive m/0'
11031110
// use hardened derivation (child keys >= 0x80000000 are hardened after bip32)
1104-
masterKey.Derive(accountKey, BIP32_HARDENED_KEY_LIMIT);
1111+
DeriveExtKey(masterKey, BIP32_HARDENED_KEY_LIMIT, accountKey);
11051112

11061113
// derive m/0'/0' (external chain) OR m/0'/1' (internal chain)
11071114
assert(internal ? m_storage.CanSupportFeature(FEATURE_HD_SPLIT) : true);
1108-
accountKey.Derive(chainChildKey, BIP32_HARDENED_KEY_LIMIT+(internal ? 1 : 0));
1115+
DeriveExtKey(accountKey, BIP32_HARDENED_KEY_LIMIT+(internal ? 1 : 0), chainChildKey);
11091116

11101117
// derive child key at next index, skip keys already known to the wallet
11111118
do {
11121119
// always derive hardened keys
11131120
// childIndex | BIP32_HARDENED_KEY_LIMIT = derive childIndex in hardened child-index-range
11141121
// example: 1 | BIP32_HARDENED_KEY_LIMIT == 0x80000001 == 2147483649
11151122
if (internal) {
1116-
chainChildKey.Derive(childKey, hd_chain.nInternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
1123+
DeriveExtKey(chainChildKey, hd_chain.nInternalChainCounter | BIP32_HARDENED_KEY_LIMIT, childKey);
11171124
metadata.hdKeypath = "m/0'/1'/" + ToString(hd_chain.nInternalChainCounter) + "'";
11181125
metadata.key_origin.path.push_back(0 | BIP32_HARDENED_KEY_LIMIT);
11191126
metadata.key_origin.path.push_back(1 | BIP32_HARDENED_KEY_LIMIT);
11201127
metadata.key_origin.path.push_back(hd_chain.nInternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
11211128
hd_chain.nInternalChainCounter++;
11221129
}
11231130
else {
1124-
chainChildKey.Derive(childKey, hd_chain.nExternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
1131+
DeriveExtKey(chainChildKey, hd_chain.nExternalChainCounter | BIP32_HARDENED_KEY_LIMIT, childKey);
11251132
metadata.hdKeypath = "m/0'/0'/" + ToString(hd_chain.nExternalChainCounter) + "'";
11261133
metadata.key_origin.path.push_back(0 | BIP32_HARDENED_KEY_LIMIT);
11271134
metadata.key_origin.path.push_back(0 | BIP32_HARDENED_KEY_LIMIT);

0 commit comments

Comments
 (0)