Skip to content

Commit

Permalink
wallet: move ImportDescriptors to util
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoerg committed Apr 22, 2024
1 parent 3310a96 commit 1849c06
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
31 changes: 0 additions & 31 deletions src/wallet/test/fuzz/notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,37 +53,6 @@ void initialize_setup()
g_setup = testing_setup.get();
}

void ImportDescriptors(CWallet& wallet, const std::string& seed_insecure)
{
const std::vector<std::string> DESCS{
"pkh(%s/%s/*)",
"sh(wpkh(%s/%s/*))",
"tr(%s/%s/*)",
"wpkh(%s/%s/*)",
};

for (const std::string& desc_fmt : DESCS) {
for (bool internal : {true, false}) {
const auto descriptor{(strprintf)(desc_fmt, "[5aa9973a/66h/4h/2h]" + seed_insecure, int{internal})};

FlatSigningProvider keys;
std::string error;
auto parsed_desc = Parse(descriptor, keys, error, /*require_checksum=*/false);
assert(parsed_desc);
assert(error.empty());
assert(parsed_desc->IsRange());
assert(parsed_desc->IsSingleType());
assert(!keys.keys.empty());
WalletDescriptor w_desc{std::move(parsed_desc), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/1, /*next_index=*/0};
assert(!wallet.GetDescriptorScriptPubKeyMan(w_desc));
LOCK(wallet.cs_wallet);
auto spk_manager{wallet.AddWalletDescriptor(w_desc, keys, /*label=*/"", internal)};
assert(spk_manager);
wallet.AddActiveScriptPubKeyMan(spk_manager->GetID(), *Assert(w_desc.descriptor->GetOutputType()), internal);
}
}
}

/**
* Wraps a descriptor wallet for fuzzing.
*/
Expand Down
31 changes: 31 additions & 0 deletions src/wallet/test/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,37 @@ void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet)
UnloadWallet(std::move(wallet));
}

void ImportDescriptors(CWallet& wallet, const std::string& seed_insecure)
{
const std::vector<std::string> DESCS{
"pkh(%s/%s/*)",
"sh(wpkh(%s/%s/*))",
"tr(%s/%s/*)",
"wpkh(%s/%s/*)",
};

for (const std::string& desc_fmt : DESCS) {
for (bool internal : {true, false}) {
const auto descriptor{(strprintf)(desc_fmt, "[5aa9973a/66h/4h/2h]" + seed_insecure, int{internal})};

FlatSigningProvider keys;
std::string error;
auto parsed_desc = Parse(descriptor, keys, error, /*require_checksum=*/false);
assert(parsed_desc);
assert(error.empty());
assert(parsed_desc->IsRange());
assert(parsed_desc->IsSingleType());
assert(!keys.keys.empty());
WalletDescriptor w_desc{std::move(parsed_desc), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/1, /*next_index=*/0};
assert(!wallet.GetDescriptorScriptPubKeyMan(w_desc));
LOCK(wallet.cs_wallet);
auto spk_manager{wallet.AddWalletDescriptor(w_desc, keys, /*label=*/"", internal)};
assert(spk_manager);
wallet.AddActiveScriptPubKeyMan(spk_manager->GetID(), *Assert(w_desc.descriptor->GetOutputType()), internal);
}
}
}

std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database)
{
return std::make_unique<MockableDatabase>(dynamic_cast<MockableDatabase&>(database).m_records);
Expand Down
2 changes: 2 additions & 0 deletions src/wallet/test/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ static const DatabaseFormat DATABASE_FORMATS[] = {

const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";

void ImportDescriptors(CWallet& wallet, const std::string& seed_insecure);

std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);

std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
Expand Down

0 comments on commit 1849c06

Please sign in to comment.