mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-10 04:33:59 +01:00
a0abcbd382doc: Mention multipath specifier (Ava Chow)0019f61fc5tests: Test importing of multipath descriptors (Ava Chow)f97d5c137dwallet, rpc: Allow importdescriptors to import multipath descriptors (Ava Chow)32dcbca3fbrpc: Allow importmulti to import multipath descriptors correctly (Ava Chow)64dfe3ce4bwallet: Move internal to be per key when importing (Ava Chow)1692245525tests: Multipath descriptors for scantxoutset and deriveaddresses (Ava Chow)cddc0ba9a9rpc: Have deriveaddresses derive receiving and change (Ava Chow)360456cd22tests: Multipath descriptors for getdescriptorinfo (Ava Chow)a90eee444ctests: Add unit tests for multipath descriptors (Ava Chow)1bbf46e2dadescriptors: Change Parse to return vector of descriptors (Ava Chow)0d640c6f02descriptors: Have ParseKeypath handle multipath specifiers (Ava Chow)a5f39b1034descriptors: Change ParseScript to return vector of descriptors (Ava Chow)0d55deae15descriptors: Add DescriptorImpl::Clone (Ava Chow)7e86541f72descriptors: Add PubkeyProvider::Clone (Ava Chow) Pull request description: It is convenient to have a descriptor which specifies both receiving and change addresses in a single string. However, as discussed in https://github.com/bitcoin/bitcoin/issues/17190#issuecomment-895515768, it is not feasible to use a generic multipath specification like BIP 88 due to combinatorial blow up and that it would result in unexpected descriptors. To resolve that problem, this PR proposes a targeted solution which allows only a single pair of 2 derivation indexes to be inserted in the place of a single derivation index. So instead of two descriptor `wpkh(xpub.../0/0/*)` and `wpkh(xpub.../0/1/*)` to represent receive and change addresses, this could be written as `wpkh(xpub.../0/<0;1>/*)`. The multipath specifier is of the form `<NUM;NUM>`. Each `NUM` can have its own hardened specifier, e.g. `<0;1h>` is valid. The multipath specifier can also only appear in one path index in the derivation path. This results in the parser returning two descriptors. The first descriptor uses the first `NUM` in all pairs present, and the second uses the second `NUM`. In our implementation, if a multipath descriptor is not provided, a pair is still returned, but the second element is just `nullptr`. The wallet will not output the multipath descriptors (yet). Furthermore, when a multipath descriptor is imported, it is expanded to the two descriptors and each imported on its own, with the second descriptor being implicitly for internal (change) addresses. There is no change to how the wallet stores or outputs descriptors (yet). Note that the path specifier is different from what was proposed. It uses angle brackets and the semicolon because these are unused characters available in the character set and I wanted to avoid conflicts with characters already in use in descriptors. Closes #17190 ACKs for top commit: darosior: re-ACKa0abcbd382mjdietzx: reACKa0abcbd382pythcoiner: reACKa0abcbdfurszy: Code review ACKa0abcbdglozow: light code review ACKa0abcbd382Tree-SHA512: 84ea40b3fd1b762194acd021cae018c2f09b98e595f5e87de5c832c265cfe8a6d0bc4dae25785392fa90db0f6301ddf9aea787980a29c74f81d04b711ac446c2
196 lines
5.8 KiB
C++
196 lines
5.8 KiB
C++
// Copyright (c) 2021-2022 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <wallet/test/util.h>
|
|
|
|
#include <chain.h>
|
|
#include <key.h>
|
|
#include <key_io.h>
|
|
#include <streams.h>
|
|
#include <test/util/setup_common.h>
|
|
#include <validationinterface.h>
|
|
#include <wallet/context.h>
|
|
#include <wallet/wallet.h>
|
|
#include <wallet/walletdb.h>
|
|
|
|
#include <memory>
|
|
|
|
namespace wallet {
|
|
std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key)
|
|
{
|
|
auto wallet = std::make_unique<CWallet>(&chain, "", CreateMockableWalletDatabase());
|
|
{
|
|
LOCK2(wallet->cs_wallet, ::cs_main);
|
|
wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash());
|
|
}
|
|
{
|
|
LOCK(wallet->cs_wallet);
|
|
wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
|
|
wallet->SetupDescriptorScriptPubKeyMans();
|
|
|
|
FlatSigningProvider provider;
|
|
std::string error;
|
|
auto descs = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false);
|
|
assert(descs.size() == 1);
|
|
auto& desc = descs.at(0);
|
|
WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1);
|
|
if (!wallet->AddWalletDescriptor(w_desc, provider, "", false)) assert(false);
|
|
}
|
|
WalletRescanReserver reserver(*wallet);
|
|
reserver.reserve();
|
|
CWallet::ScanResult result = wallet->ScanForWalletTransactions(cchain.Genesis()->GetBlockHash(), /*start_height=*/0, /*max_height=*/{}, reserver, /*fUpdate=*/false, /*save_progress=*/false);
|
|
assert(result.status == CWallet::ScanResult::SUCCESS);
|
|
assert(result.last_scanned_block == cchain.Tip()->GetBlockHash());
|
|
assert(*result.last_scanned_height == cchain.Height());
|
|
assert(result.last_failed_block.IsNull());
|
|
return wallet;
|
|
}
|
|
|
|
std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags)
|
|
{
|
|
bilingual_str error;
|
|
std::vector<bilingual_str> warnings;
|
|
auto wallet = CWallet::Create(context, "", std::move(database), create_flags, error, warnings);
|
|
NotifyWalletLoaded(context, wallet);
|
|
if (context.chain) {
|
|
wallet->postInitProcess();
|
|
}
|
|
return wallet;
|
|
}
|
|
|
|
std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context)
|
|
{
|
|
DatabaseOptions options;
|
|
options.create_flags = WALLET_FLAG_DESCRIPTORS;
|
|
DatabaseStatus status;
|
|
bilingual_str error;
|
|
std::vector<bilingual_str> warnings;
|
|
auto database = MakeWalletDatabase("", options, status, error);
|
|
return TestLoadWallet(std::move(database), context, options.create_flags);
|
|
}
|
|
|
|
void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet)
|
|
{
|
|
// Calls SyncWithValidationInterfaceQueue
|
|
wallet->chain().waitForNotificationsIfTipChanged({});
|
|
wallet->m_chain_notifications_handler.reset();
|
|
WaitForDeleteWallet(std::move(wallet));
|
|
}
|
|
|
|
std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database)
|
|
{
|
|
return std::make_unique<MockableDatabase>(dynamic_cast<MockableDatabase&>(database).m_records);
|
|
}
|
|
|
|
std::string getnewaddress(CWallet& w)
|
|
{
|
|
constexpr auto output_type = OutputType::BECH32;
|
|
return EncodeDestination(getNewDestination(w, output_type));
|
|
}
|
|
|
|
CTxDestination getNewDestination(CWallet& w, OutputType output_type)
|
|
{
|
|
return *Assert(w.GetNewDestination(output_type, ""));
|
|
}
|
|
|
|
MockableCursor::MockableCursor(const MockableData& records, bool pass, Span<const std::byte> prefix)
|
|
{
|
|
m_pass = pass;
|
|
std::tie(m_cursor, m_cursor_end) = records.equal_range(BytePrefix{prefix});
|
|
}
|
|
|
|
DatabaseCursor::Status MockableCursor::Next(DataStream& key, DataStream& value)
|
|
{
|
|
if (!m_pass) {
|
|
return Status::FAIL;
|
|
}
|
|
if (m_cursor == m_cursor_end) {
|
|
return Status::DONE;
|
|
}
|
|
key.clear();
|
|
value.clear();
|
|
const auto& [key_data, value_data] = *m_cursor;
|
|
key.write(key_data);
|
|
value.write(value_data);
|
|
m_cursor++;
|
|
return Status::MORE;
|
|
}
|
|
|
|
bool MockableBatch::ReadKey(DataStream&& key, DataStream& value)
|
|
{
|
|
if (!m_pass) {
|
|
return false;
|
|
}
|
|
SerializeData key_data{key.begin(), key.end()};
|
|
const auto& it = m_records.find(key_data);
|
|
if (it == m_records.end()) {
|
|
return false;
|
|
}
|
|
value.clear();
|
|
value.write(it->second);
|
|
return true;
|
|
}
|
|
|
|
bool MockableBatch::WriteKey(DataStream&& key, DataStream&& value, bool overwrite)
|
|
{
|
|
if (!m_pass) {
|
|
return false;
|
|
}
|
|
SerializeData key_data{key.begin(), key.end()};
|
|
SerializeData value_data{value.begin(), value.end()};
|
|
auto [it, inserted] = m_records.emplace(key_data, value_data);
|
|
if (!inserted && overwrite) { // Overwrite if requested
|
|
it->second = value_data;
|
|
inserted = true;
|
|
}
|
|
return inserted;
|
|
}
|
|
|
|
bool MockableBatch::EraseKey(DataStream&& key)
|
|
{
|
|
if (!m_pass) {
|
|
return false;
|
|
}
|
|
SerializeData key_data{key.begin(), key.end()};
|
|
m_records.erase(key_data);
|
|
return true;
|
|
}
|
|
|
|
bool MockableBatch::HasKey(DataStream&& key)
|
|
{
|
|
if (!m_pass) {
|
|
return false;
|
|
}
|
|
SerializeData key_data{key.begin(), key.end()};
|
|
return m_records.count(key_data) > 0;
|
|
}
|
|
|
|
bool MockableBatch::ErasePrefix(Span<const std::byte> prefix)
|
|
{
|
|
if (!m_pass) {
|
|
return false;
|
|
}
|
|
auto it = m_records.begin();
|
|
while (it != m_records.end()) {
|
|
auto& key = it->first;
|
|
if (key.size() < prefix.size() || std::search(key.begin(), key.end(), prefix.begin(), prefix.end()) != key.begin()) {
|
|
it++;
|
|
continue;
|
|
}
|
|
it = m_records.erase(it);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records)
|
|
{
|
|
return std::make_unique<MockableDatabase>(records);
|
|
}
|
|
|
|
MockableDatabase& GetMockableDatabase(CWallet& wallet)
|
|
{
|
|
return dynamic_cast<MockableDatabase&>(wallet.GetDatabase());
|
|
}
|
|
} // namespace wallet
|