Merge 316afb1eca into merged_master (Bitcoin PR bitcoin/bitcoin#25218)

This commit is contained in:
James Dorfman 2024-10-15 21:23:33 +00:00
commit e22bfb3011
22 changed files with 248 additions and 240 deletions

36
src/wallet/scriptpubkeyman.cpp Normal file → Executable file
View file

@ -21,27 +21,22 @@ namespace wallet {
//! Value for the first BIP 32 hardened derivation. Can be used as a bit mask and as a value. See BIP 32 for more details.
const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000;
bool LegacyScriptPubKeyMan::GetNewDestination(const OutputType type, CTxDestination& dest, bilingual_str& error)
BResult<CTxDestination> LegacyScriptPubKeyMan::GetNewDestination(const OutputType type)
{
if (LEGACY_OUTPUT_TYPES.count(type) == 0) {
error = _("Error: Legacy wallets only support the \"legacy\", \"p2sh-segwit\", and \"bech32\" address types");
return false;
return _("Error: Legacy wallets only support the \"legacy\", \"p2sh-segwit\", and \"bech32\" address types");;
}
assert(type != OutputType::BECH32M);
LOCK(cs_KeyStore);
error.clear();
// Generate a new key that is added to wallet
CPubKey new_key;
if (!GetKeyFromPool(new_key, type)) {
error = _("Error: Keypool ran out, please call keypoolrefill first");
return false;
return _("Error: Keypool ran out, please call keypoolrefill first");
}
LearnRelatedScripts(new_key, type);
dest = GetDestinationForKey(new_key, type);
return true;
return GetDestinationForKey(new_key, type);
}
typedef std::vector<unsigned char> valtype;
@ -1664,12 +1659,11 @@ std::set<CKeyID> LegacyScriptPubKeyMan::GetKeys() const
return set_address;
}
bool DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type, CTxDestination& dest, bilingual_str& error)
BResult<CTxDestination> DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type)
{
// Returns true if this descriptor supports getting new addresses. Conditions where we may be unable to fetch them (e.g. locked) are caught later
if (!CanGetAddresses()) {
error = _("No addresses available");
return false;
return _("No addresses available");
}
{
LOCK(cs_desc_man);
@ -1687,15 +1681,14 @@ bool DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type, CTxDest
std::vector<CScript> scripts_temp;
if (m_wallet_descriptor.range_end <= m_max_cached_index && !TopUp(1)) {
// We can't generate anymore keys
error = _("Error: Keypool ran out, please call keypoolrefill first");
return false;
return _("Error: Keypool ran out, please call keypoolrefill first");
}
if (!m_wallet_descriptor.descriptor->ExpandFromCache(m_wallet_descriptor.next_index, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
// We can't generate anymore keys
error = _("Error: Keypool ran out, please call keypoolrefill first");
return false;
return _("Error: Keypool ran out, please call keypoolrefill first");
}
CTxDestination dest;
std::optional<OutputType> out_script_type = m_wallet_descriptor.descriptor->GetOutputType();
if (out_script_type && out_script_type == type) {
ExtractDestination(scripts_temp[0], dest);
@ -1704,7 +1697,7 @@ bool DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type, CTxDest
}
m_wallet_descriptor.next_index++;
WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor);
return true;
return dest;
}
}
@ -1775,9 +1768,14 @@ bool DescriptorScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, Walle
bool DescriptorScriptPubKeyMan::GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error)
{
LOCK(cs_desc_man);
bool result = GetNewDestination(type, address, error);
auto op_dest = GetNewDestination(type);
index = m_wallet_descriptor.next_index - 1;
return result;
if (op_dest) {
address = op_dest.GetObj();
} else {
error = op_dest.GetError();
}
return op_dest.HasRes();
}
void DescriptorScriptPubKeyMan::ReturnDestination(int64_t index, bool internal, const CTxDestination& addr)