remove unused GetDestinationForKey

It is only used in test. There it is problematic, because it sometimes
relies on m_default_address_type. If the default were changed to
BECH32M, those tests would fail the assert(false).

So just use PKHash{} in all tests and remove GetDestinationForKey.
This commit is contained in:
MarcoFalke
2025-05-15 14:32:58 +02:00
parent fac72fef27
commit fafee85358
6 changed files with 7 additions and 36 deletions

View File

@@ -37,7 +37,7 @@ static void WalletMigration(benchmark::Bench& bench)
for (int w = 0; w < NUM_WATCH_ONLY_ADDR; ++w) {
CKey key = GenerateRandomKey();
LOCK(wallet->cs_wallet);
const auto& dest = GetDestinationForKey(key.GetPubKey(), OutputType::LEGACY);
const PKHash dest{key.GetPubKey()};
const CScript& script = scripts_watch_only.emplace_back(GetScriptForDestination(dest));
assert(legacy_spkm->LoadWatchOnly(script));
assert(wallet->SetAddressBook(dest, strprintf("watch_%d", w), /*purpose=*/std::nullopt));

View File

@@ -46,27 +46,6 @@ const std::string& FormatOutputType(OutputType type)
assert(false);
}
CTxDestination GetDestinationForKey(const CPubKey& key, OutputType type)
{
switch (type) {
case OutputType::LEGACY: return PKHash(key);
case OutputType::P2SH_SEGWIT:
case OutputType::BECH32: {
if (!key.IsCompressed()) return PKHash(key);
CTxDestination witdest = WitnessV0KeyHash(key);
CScript witprog = GetScriptForDestination(witdest);
if (type == OutputType::P2SH_SEGWIT) {
return ScriptHash(witprog);
} else {
return witdest;
}
}
case OutputType::BECH32M:
case OutputType::UNKNOWN: {} // This function should never be used with BECH32M or UNKNOWN, so let it assert
} // no default case, so the compiler can warn about missing cases
assert(false);
}
CTxDestination AddAndGetDestinationForScript(FlatSigningProvider& keystore, const CScript& script, OutputType type)
{
// Add script to keystore

View File

@@ -32,12 +32,6 @@ static constexpr auto OUTPUT_TYPES = std::array{
std::optional<OutputType> ParseOutputType(const std::string& str);
const std::string& FormatOutputType(OutputType type);
/**
* Get a destination of the requested type (if possible) to the specified key.
* The caller must make sure LearnRelatedScripts has been called beforehand.
*/
CTxDestination GetDestinationForKey(const CPubKey& key, OutputType);
/**
* Get a destination of the requested type (if possible) to the specified script.
* This function will automatically add the script (and any other

View File

@@ -84,13 +84,11 @@ void TestAddAddressesToSendBook(interfaces::Node& node)
wallet->SetupDescriptorScriptPubKeyMans();
}
auto build_address = [&wallet]() {
auto build_address{[]() {
CKey key = GenerateRandomKey();
CTxDestination dest(GetDestinationForKey(
key.GetPubKey(), wallet->m_default_address_type));
const PKHash dest{key.GetPubKey()};
return std::make_pair(dest, QString::fromStdString(EncodeDestination(dest)));
};
}};
CTxDestination r_key_dest, s_key_dest;

View File

@@ -217,7 +217,7 @@ std::shared_ptr<CWallet> SetupDescriptorsWallet(interfaces::Node& node, TestChai
WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1);
auto spk_manager = *Assert(wallet->AddWalletDescriptor(w_desc, provider, "", false));
assert(spk_manager);
CTxDestination dest = GetDestinationForKey(test.coinbaseKey.GetPubKey(), wallet->m_default_address_type);
const PKHash dest{test.coinbaseKey.GetPubKey()};
wallet->SetAddressBook(dest, "", wallet::AddressPurpose::RECEIVE);
wallet->SetLastBlockProcessed(105, WITH_LOCK(node.context()->chainman->GetMutex(), return node.context()->chainman->ActiveChain().Tip()->GetBlockHash()));
SyncUpWallet(wallet, node);
@@ -406,7 +406,7 @@ void TestGUIWatchOnly(interfaces::Node& node, TestChain100Setup& test)
sendCoinsDialog.findChild<QLabel*>("labelBalance"));
// Set change address
sendCoinsDialog.getCoinControl()->destChange = GetDestinationForKey(test.coinbaseKey.GetPubKey(), OutputType::LEGACY);
sendCoinsDialog.getCoinControl()->destChange = PKHash{test.coinbaseKey.GetPubKey()};
// Time to reject "save" PSBT dialog ('SendCoins' locks the main thread until the dialog receives the event).
QTimer timer;

View File

@@ -174,7 +174,7 @@ FUZZ_TARGET(key, .init = initialize_key)
assert(v_solutions_ret_tx_multisig[2].size() == 1);
OutputType output_type{};
const CTxDestination tx_destination = GetDestinationForKey(pubkey, output_type);
const CTxDestination tx_destination{PKHash{pubkey}};
assert(output_type == OutputType::LEGACY);
assert(IsValidDestination(tx_destination));
assert(PKHash{pubkey} == *std::get_if<PKHash>(&tx_destination));