@@ -1084,6 +1084,13 @@ CPubKey LegacyScriptPubKeyMan::GenerateNewKey(WalletBatch &batch, CHDChain& hd_c
1084
1084
return pubkey;
1085
1085
}
1086
1086
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
+
1087
1094
void LegacyScriptPubKeyMan::DeriveNewChildKey (WalletBatch &batch, CKeyMetadata& metadata, CKey& secret, CHDChain& hd_chain, bool internal)
1088
1095
{
1089
1096
// for now we use a fixed keypath scheme of m/0'/0'/k
@@ -1101,27 +1108,27 @@ void LegacyScriptPubKeyMan::DeriveNewChildKey(WalletBatch &batch, CKeyMetadata&
1101
1108
1102
1109
// derive m/0'
1103
1110
// 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 );
1105
1112
1106
1113
// derive m/0'/0' (external chain) OR m/0'/1' (internal chain)
1107
1114
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 );
1109
1116
1110
1117
// derive child key at next index, skip keys already known to the wallet
1111
1118
do {
1112
1119
// always derive hardened keys
1113
1120
// childIndex | BIP32_HARDENED_KEY_LIMIT = derive childIndex in hardened child-index-range
1114
1121
// example: 1 | BIP32_HARDENED_KEY_LIMIT == 0x80000001 == 2147483649
1115
1122
if (internal) {
1116
- chainChildKey. Derive (childKey , hd_chain.nInternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
1123
+ DeriveExtKey (chainChildKey , hd_chain.nInternalChainCounter | BIP32_HARDENED_KEY_LIMIT, childKey );
1117
1124
metadata.hdKeypath = " m/0'/1'/" + ToString (hd_chain.nInternalChainCounter ) + " '" ;
1118
1125
metadata.key_origin .path .push_back (0 | BIP32_HARDENED_KEY_LIMIT);
1119
1126
metadata.key_origin .path .push_back (1 | BIP32_HARDENED_KEY_LIMIT);
1120
1127
metadata.key_origin .path .push_back (hd_chain.nInternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
1121
1128
hd_chain.nInternalChainCounter ++;
1122
1129
}
1123
1130
else {
1124
- chainChildKey. Derive (childKey , hd_chain.nExternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
1131
+ DeriveExtKey (chainChildKey , hd_chain.nExternalChainCounter | BIP32_HARDENED_KEY_LIMIT, childKey );
1125
1132
metadata.hdKeypath = " m/0'/0'/" + ToString (hd_chain.nExternalChainCounter ) + " '" ;
1126
1133
metadata.key_origin .path .push_back (0 | BIP32_HARDENED_KEY_LIMIT);
1127
1134
metadata.key_origin .path .push_back (0 | BIP32_HARDENED_KEY_LIMIT);
0 commit comments