diff --git a/doc/external-signer.md b/doc/external-signer.md index 8b02edd6b8ab..8e88bca38fb1 100644 --- a/doc/external-signer.md +++ b/doc/external-signer.md @@ -159,7 +159,7 @@ The command MAY complain if `--chain` is set to a test-network, but any of the B Usage: ```sh - --fingerprint --chain getdescriptors --account + --fingerprint --chain getdescriptors --account [--multipath] ``` Returns descriptors supported by the device. Example: @@ -185,6 +185,29 @@ Returns descriptors supported by the device. Example: } ``` +#### Flag `--multipath` (optional) + +With `--multipath` the command MUST instead return a flat array of [BIP 389](https://github.com/bitcoin/bips/blob/master/bip-0389.mediawiki) +multipath descriptors, each combining the receive and change chain in a single +descriptor with a `<0;1>` element: + +```sh + --fingerprint 00000000 --chain main getdescriptors --account 0 --multipath +``` + +``` +[ + "pkh([00000000/44h/0h/0h]xpub6C.../<0;1>/*)#tw6cmp0h", + "sh(wpkh([00000000/49h/0h/0h]xpub6B..../<0;1>/*))#tetlyt9c", + "wpkh([00000000/84h/0h/0h]xpub6C.../<0;1>/*)#3ryl7uz3", + "tr([00000000/86h/0h/0h]xpub6C.../<0;1>/*)#kpq6q00r" +] +``` + +Bitcoin Core passes this flag when creating a wallet with the `multipath` +option. A command that does not support the flag MUST fail rather than ignore +it, so that the wallet is not silently created from single path descriptors. + ### `displayaddress` (optional) Usage: diff --git a/src/external_signer.cpp b/src/external_signer.cpp index cb49cedc69c2..2a9e73de20ca 100644 --- a/src/external_signer.cpp +++ b/src/external_signer.cpp @@ -72,9 +72,14 @@ UniValue ExternalSigner::DisplayAddress(const std::string& descriptor) const return RunCommandParseJSON(Cat(m_command, Cat(Cat({"--fingerprint", m_fingerprint}, NetworkArg()), {"displayaddress", "--desc", descriptor})), ""); } -UniValue ExternalSigner::GetDescriptors(const int account) +UniValue ExternalSigner::GetDescriptors(const int account, const bool multipath) { - return RunCommandParseJSON(Cat(m_command, Cat(Cat({"--fingerprint", m_fingerprint}, NetworkArg()), {"getdescriptors", "--account", strprintf("%d", account)})), ""); + std::vector args{"getdescriptors", "--account", strprintf("%d", account)}; + // Signers that predate this argument exit with an error, which is what we + // want: silently falling back to single path descriptors would create a + // wallet that does not match what was asked for. + if (multipath) args.emplace_back("--multipath"); + return RunCommandParseJSON(Cat(m_command, Cat(Cat({"--fingerprint", m_fingerprint}, NetworkArg()), args)), ""); } bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::string& error) diff --git a/src/external_signer.h b/src/external_signer.h index 5131a2078f3d..a4fa4167aa76 100644 --- a/src/external_signer.h +++ b/src/external_signer.h @@ -54,10 +54,13 @@ class ExternalSigner //! Get receive and change Descriptor(s) from device for a given account. //! Calls ` --fingerprint --chain getdescriptors - //! --account `. - //! @param[in] account which BIP32 account to use (e.g. `m/44'/0'/account'`) + //! --account [--multipath]`. + //! @param[in] account which BIP32 account to use (e.g. `m/44'/0'/account'`) + //! @param[in] multipath ask for BIP 389 multipath descriptors, which combine the + //! receive and change chain. Signers that do not support this + //! are expected to fail rather than ignore the argument. //! @returns see doc/external-signer.md - UniValue GetDescriptors(int account); + UniValue GetDescriptors(int account, bool multipath = false); //! Sign PartiallySignedTransaction on the device. //! Calls ` --stdin --fingerprint --chain ` and passes the diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index 21ed8795304f..87318c0ac16a 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -365,6 +365,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "createwallet", 5, "descriptors"}, { "createwallet", 6, "load_on_startup"}, { "createwallet", 7, "external_signer"}, + { "createwallet", 8, "multipath"}, { "restorewallet", 0, "wallet_name", ParamFormat::STRING }, { "restorewallet", 1, "backup_file", ParamFormat::STRING }, { "restorewallet", 2, "load_on_startup"}, diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp index 5d9e62494726..ac848442e97d 100644 --- a/src/script/descriptor.cpp +++ b/src/script/descriptor.cpp @@ -176,6 +176,30 @@ std::string AddChecksum(const std::string& str) { return str + "#" + DescriptorC typedef std::vector KeyPath; +/** Retained multipath specifier info for a key expression that was expanded from + * a BIP 389 multipath descriptor. Each expanded sibling carries the same info, + * which allows any single sibling to reproduce the multipath string form. */ +struct MultipathInfo { + //! Index of the multipath element within the derivation path + size_t pos; + //! All values of the multipath specifier (hardened flag included), in the order they were listed + std::vector values; +}; + +/** Format a derivation path as a string, substituting the multipath specifier back in at its position. */ +static std::string FormatMultipathKeypath(const KeyPath& path, const MultipathInfo& multipath, bool apostrophe) +{ + const auto format_index{[&](uint32_t num) { + return strprintf("%u%s", num & ~BIP32_HARDENED_FLAG, (num & BIP32_HARDENED_FLAG) ? (apostrophe ? "'" : "h") : ""); + }}; + std::string ret{FormatHDKeypath({path.begin(), path.begin() + multipath.pos}, apostrophe) + "/<"}; + for (size_t i = 0; i < multipath.values.size(); ++i) { + if (i) ret += ";"; + ret += format_index(multipath.values[i]); + } + return ret + ">" + FormatHDKeypath({path.begin() + multipath.pos + 1, path.end()}, apostrophe); +} + /** Interface for public key objects in descriptors. */ struct PubkeyProvider { @@ -206,20 +230,23 @@ struct PubkeyProvider COMPAT // string calculation that mustn't change over time to stay compatible with previous software versions }; - /** Get the descriptor string form. */ - virtual std::string ToString(StringType type=StringType::PUBLIC) const = 0; + /** Get the descriptor string form. + * If multipath is set, providers expanded from a multipath descriptor emit + * the multipath specifier instead of their own path element. + */ + virtual std::string ToString(StringType type=StringType::PUBLIC, bool multipath=false) const = 0; /** Get the descriptor string form including private data (if available in arg). * If the private data is not available, the output string in the "out" parameter * will not contain any private key information, * and this function will return "false". */ - virtual bool ToPrivateString(const SigningProvider& arg, std::string& out) const = 0; + virtual bool ToPrivateString(const SigningProvider& arg, std::string& out, bool multipath=false) const = 0; /** Get the descriptor string form with the xpub at the last hardened derivation, * and always use h for hardened derivation. */ - virtual bool ToNormalizedString(const SigningProvider& arg, std::string& out, const DescriptorCache* cache = nullptr) const = 0; + virtual bool ToNormalizedString(const SigningProvider& arg, std::string& out, const DescriptorCache* cache = nullptr, bool multipath=false) const = 0; /** Derive a private key, if private data is available in arg and put it into out. */ virtual void GetPrivKey(int pos, const SigningProvider& arg, FlatSigningProvider& out) const = 0; @@ -287,18 +314,18 @@ class OriginPubkeyProvider final : public PubkeyProvider bool IsRange() const override { return m_provider->IsRange(); } size_t GetSize() const override { return m_provider->GetSize(); } bool IsBIP32() const override { return m_provider->IsBIP32(); } - std::string ToString(StringType type) const override { return "[" + OriginString(type) + "]" + m_provider->ToString(type); } - bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override + std::string ToString(StringType type, bool multipath=false) const override { return "[" + OriginString(type) + "]" + m_provider->ToString(type, multipath); } + bool ToPrivateString(const SigningProvider& arg, std::string& ret, bool multipath=false) const override { std::string sub; - bool has_priv_key{m_provider->ToPrivateString(arg, sub)}; + bool has_priv_key{m_provider->ToPrivateString(arg, sub, multipath)}; ret = "[" + OriginString(StringType::PUBLIC) + "]" + std::move(sub); return has_priv_key; } - bool ToNormalizedString(const SigningProvider& arg, std::string& ret, const DescriptorCache* cache) const override + bool ToNormalizedString(const SigningProvider& arg, std::string& ret, const DescriptorCache* cache, bool multipath=false) const override { std::string sub; - if (!m_provider->ToNormalizedString(arg, sub, cache)) return false; + if (!m_provider->ToNormalizedString(arg, sub, cache, multipath)) return false; // If m_provider is a BIP32PubkeyProvider, we may get a string formatted like a OriginPubkeyProvider // In that case, we need to strip out the leading square bracket and fingerprint from the substring, // and append that to our own origin string. @@ -357,8 +384,8 @@ class ConstPubkeyProvider final : public PubkeyProvider bool IsRange() const override { return false; } size_t GetSize() const override { return m_pubkey.size(); } bool IsBIP32() const override { return false; } - std::string ToString(StringType type) const override { return m_xonly ? HexStr(m_pubkey).substr(2) : HexStr(m_pubkey); } - bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override + std::string ToString(StringType type, bool multipath=false) const override { return m_xonly ? HexStr(m_pubkey).substr(2) : HexStr(m_pubkey); } + bool ToPrivateString(const SigningProvider& arg, std::string& ret, bool multipath=false) const override { std::optional key = GetPrivKey(arg); if (!key) { @@ -368,7 +395,7 @@ class ConstPubkeyProvider final : public PubkeyProvider ret = EncodeSecret(*key); return true; } - bool ToNormalizedString(const SigningProvider& arg, std::string& ret, const DescriptorCache* cache) const override + bool ToNormalizedString(const SigningProvider& arg, std::string& ret, const DescriptorCache* cache, bool multipath=false) const override { ret = ToString(StringType::PUBLIC); return true; @@ -409,6 +436,15 @@ class BIP32PubkeyProvider final : public PubkeyProvider DeriveType m_derive; // Whether ' or h is used in harded derivation bool m_apostrophe; + // Multipath specifier this provider was expanded from, if any + std::optional m_multipath; + + //! Format m_path, emitting the multipath specifier instead of its concrete element when requested + std::string PathToString(bool apostrophe, bool multipath) const + { + if (multipath && m_multipath) return FormatMultipathKeypath(m_path, *m_multipath, apostrophe); + return FormatHDKeypath(m_path, apostrophe); + } bool GetExtKey(const SigningProvider& arg, CExtKey& ret) const { @@ -442,7 +478,7 @@ class BIP32PubkeyProvider final : public PubkeyProvider } public: - BIP32PubkeyProvider(uint32_t exp_index, const CExtPubKey& extkey, KeyPath path, DeriveType derive, bool apostrophe) : PubkeyProvider(exp_index), m_root_extkey(extkey), m_path(std::move(path)), m_derive(derive), m_apostrophe(apostrophe) {} + BIP32PubkeyProvider(uint32_t exp_index, const CExtPubKey& extkey, KeyPath path, DeriveType derive, bool apostrophe, std::optional multipath = std::nullopt) : PubkeyProvider(exp_index), m_root_extkey(extkey), m_path(std::move(path)), m_derive(derive), m_apostrophe(apostrophe), m_multipath(std::move(multipath)) {} bool IsRange() const override { return m_derive != DeriveType::NON_RANGED; } size_t GetSize() const override { return 33; } bool IsBIP32() const override { return true; } @@ -506,39 +542,39 @@ class BIP32PubkeyProvider final : public PubkeyProvider return final_extkey.pubkey; } - std::string ToString(StringType type, bool normalized) const + std::string ToString(StringType type, bool normalized, bool multipath) const { // If StringType==COMPAT, always use the apostrophe to stay compatible with previous versions const bool use_apostrophe = (!normalized && m_apostrophe) || type == StringType::COMPAT; - std::string ret = EncodeExtPubKey(m_root_extkey) + FormatHDKeypath(m_path, /*apostrophe=*/use_apostrophe); + std::string ret = EncodeExtPubKey(m_root_extkey) + PathToString(/*apostrophe=*/use_apostrophe, multipath); if (IsRange()) { ret += "/*"; if (m_derive == DeriveType::HARDENED_RANGED) ret += use_apostrophe ? '\'' : 'h'; } return ret; } - std::string ToString(StringType type=StringType::PUBLIC) const override + std::string ToString(StringType type=StringType::PUBLIC, bool multipath=false) const override { - return ToString(type, /*normalized=*/false); + return ToString(type, /*normalized=*/false, multipath); } - bool ToPrivateString(const SigningProvider& arg, std::string& out) const override + bool ToPrivateString(const SigningProvider& arg, std::string& out, bool multipath=false) const override { CExtKey key; if (!GetExtKey(arg, key)) { - out = ToString(StringType::PUBLIC); + out = ToString(StringType::PUBLIC, multipath); return false; } - out = EncodeExtKey(key) + FormatHDKeypath(m_path, /*apostrophe=*/m_apostrophe); + out = EncodeExtKey(key) + PathToString(/*apostrophe=*/m_apostrophe, multipath); if (IsRange()) { out += "/*"; if (m_derive == DeriveType::HARDENED_RANGED) out += m_apostrophe ? '\'' : 'h'; } return true; } - bool ToNormalizedString(const SigningProvider& arg, std::string& out, const DescriptorCache* cache) const override + bool ToNormalizedString(const SigningProvider& arg, std::string& out, const DescriptorCache* cache, bool multipath=false) const override { if (m_derive == DeriveType::HARDENED_RANGED) { - out = ToString(StringType::PUBLIC, /*normalized=*/true); + out = ToString(StringType::PUBLIC, /*normalized=*/true, multipath); return true; } @@ -551,9 +587,13 @@ class BIP32PubkeyProvider final : public PubkeyProvider } // Either no derivation or all unhardened derivation if (i == -1) { - out = ToString(); + out = ToString(StringType::PUBLIC, multipath); return true; } + // A multipath element at or before the last hardened step would end up + // inside the normalized origin, where each sibling has a different + // last hardened xpub, so no single normalized multipath string exists. + if (multipath && m_multipath && m_multipath->pos <= (size_t)i) return false; // Get the path to the last hardened stup KeyOriginInfo origin; int k = 0; @@ -584,7 +624,15 @@ class BIP32PubkeyProvider final : public PubkeyProvider // Build the string std::string origin_str = HexStr(origin.fingerprint) + FormatHDKeypath(origin.path); - out = "[" + origin_str + "]" + EncodeExtPubKey(xpub) + FormatHDKeypath(end_path); + std::string end_path_str; + if (multipath && m_multipath) { + // The multipath element sits after the last hardened step; emit it + // at its position within the remaining path. + end_path_str = FormatMultipathKeypath(end_path, MultipathInfo{m_multipath->pos - (size_t)(i + 1), m_multipath->values}, /*apostrophe=*/false); + } else { + end_path_str = FormatHDKeypath(end_path); + } + out = "[" + origin_str + "]" + EncodeExtPubKey(xpub) + end_path_str; if (IsRange()) { out += "/*"; assert(m_derive == DeriveType::UNHARDENED_RANGED); @@ -610,7 +658,7 @@ class BIP32PubkeyProvider final : public PubkeyProvider } std::unique_ptr Clone() const override { - return std::make_unique(m_expr_index, m_root_extkey, m_path, m_derive, m_apostrophe); + return std::make_unique(m_expr_index, m_root_extkey, m_path, m_derive, m_apostrophe, m_multipath); } bool CanSelfExpand() const override { return !IsHardened(); } }; @@ -627,21 +675,32 @@ class MuSigPubkeyProvider final : public PubkeyProvider mutable std::unique_ptr m_aggregate_provider; mutable std::optional m_aggregate_pubkey; const DeriveType m_derive; + //! Multipath specifier the derivation path was expanded from, if any + const std::optional m_multipath; const bool m_ranged_participants; bool IsRangedDerivation() const { return m_derive != DeriveType::NON_RANGED; } + //! Format m_path, emitting the multipath specifier instead of its concrete element when requested + std::string PathToString(bool multipath) const + { + if (multipath && m_multipath) return FormatMultipathKeypath(m_path, *m_multipath, /*apostrophe=*/false); + return FormatHDKeypath(m_path); + } + public: MuSigPubkeyProvider( uint32_t exp_index, std::vector> providers, KeyPath path, - DeriveType derive + DeriveType derive, + std::optional multipath = std::nullopt ) : PubkeyProvider(exp_index), m_participants(std::move(providers)), m_path(std::move(path)), m_derive(derive), + m_multipath(std::move(multipath)), m_ranged_participants(std::any_of(m_participants.begin(), m_participants.end(), [](const auto& pubkey) { return pubkey->IsRange(); })) { if (!Assume(!(m_ranged_participants && IsRangedDerivation()))) { @@ -719,22 +778,22 @@ class MuSigPubkeyProvider final : public PubkeyProvider // musig() expressions can only be used in tr() contexts which have 32 byte xonly pubkeys size_t GetSize() const override { return 32; } - std::string ToString(StringType type=StringType::PUBLIC) const override + std::string ToString(StringType type=StringType::PUBLIC, bool multipath=false) const override { std::string out = "musig("; for (size_t i = 0; i < m_participants.size(); ++i) { const auto& pubkey = m_participants.at(i); if (i) out += ","; - out += pubkey->ToString(type); + out += pubkey->ToString(type, multipath); } out += ")"; - out += FormatHDKeypath(m_path); + out += PathToString(multipath); if (IsRangedDerivation()) { out += "/*"; } return out; } - bool ToPrivateString(const SigningProvider& arg, std::string& out) const override + bool ToPrivateString(const SigningProvider& arg, std::string& out, bool multipath=false) const override { bool any_privkeys = false; out = "musig("; @@ -742,32 +801,32 @@ class MuSigPubkeyProvider final : public PubkeyProvider const auto& pubkey = m_participants.at(i); if (i) out += ","; std::string tmp; - if (pubkey->ToPrivateString(arg, tmp)) { + if (pubkey->ToPrivateString(arg, tmp, multipath)) { any_privkeys = true; } out += tmp; } out += ")"; - out += FormatHDKeypath(m_path); + out += PathToString(multipath); if (IsRangedDerivation()) { out += "/*"; } return any_privkeys; } - bool ToNormalizedString(const SigningProvider& arg, std::string& out, const DescriptorCache* cache = nullptr) const override + bool ToNormalizedString(const SigningProvider& arg, std::string& out, const DescriptorCache* cache = nullptr, bool multipath=false) const override { out = "musig("; for (size_t i = 0; i < m_participants.size(); ++i) { const auto& pubkey = m_participants.at(i); if (i) out += ","; std::string tmp; - if (!pubkey->ToNormalizedString(arg, tmp, cache)) { + if (!pubkey->ToNormalizedString(arg, tmp, cache, multipath)) { return false; } out += tmp; } out += ")"; - out += FormatHDKeypath(m_path); + out += PathToString(multipath); if (IsRangedDerivation()) { out += "/*"; } @@ -810,7 +869,7 @@ class MuSigPubkeyProvider final : public PubkeyProvider for (const std::unique_ptr& p : m_participants) { providers.emplace_back(p->Clone()); } - return std::make_unique(m_expr_index, std::move(providers), m_path, m_derive); + return std::make_unique(m_expr_index, std::move(providers), m_path, m_derive, m_multipath); } bool IsBIP32() const override { @@ -915,7 +974,7 @@ class DescriptorImpl : public Descriptor } // NOLINTNEXTLINE(misc-no-recursion) - virtual bool ToStringSubScriptHelper(const SigningProvider* arg, std::string& ret, const StringType type, const DescriptorCache* cache = nullptr) const + virtual bool ToStringSubScriptHelper(const SigningProvider* arg, std::string& ret, const StringType type, const DescriptorCache* cache = nullptr, bool multipath = false) const { size_t pos = 0; bool is_private{type == StringType::PRIVATE}; @@ -925,7 +984,7 @@ class DescriptorImpl : public Descriptor for (const auto& scriptarg : m_subdescriptor_args) { if (pos++) ret += ","; std::string tmp; - bool subscript_res{scriptarg->ToStringHelper(arg, tmp, type, cache)}; + bool subscript_res{scriptarg->ToStringHelper(arg, tmp, type, cache, multipath)}; if (!is_private && !subscript_res) return false; any_success = any_success || subscript_res; ret += tmp; @@ -934,7 +993,7 @@ class DescriptorImpl : public Descriptor } // NOLINTNEXTLINE(misc-no-recursion) - virtual bool ToStringHelper(const SigningProvider* arg, std::string& out, const StringType type, const DescriptorCache* cache = nullptr) const + virtual bool ToStringHelper(const SigningProvider* arg, std::string& out, const StringType type, const DescriptorCache* cache = nullptr, bool multipath = false) const { std::string extra = ToStringExtra(); size_t pos = extra.size() > 0 ? 1 : 0; @@ -949,22 +1008,22 @@ class DescriptorImpl : public Descriptor std::string tmp; switch (type) { case StringType::NORMALIZED: - if (!pubkey->ToNormalizedString(*arg, tmp, cache)) return false; + if (!pubkey->ToNormalizedString(*arg, tmp, cache, multipath)) return false; break; case StringType::PRIVATE: - any_success = pubkey->ToPrivateString(*arg, tmp) || any_success; + any_success = pubkey->ToPrivateString(*arg, tmp, multipath) || any_success; break; case StringType::PUBLIC: - tmp = pubkey->ToString(); + tmp = pubkey->ToString(PubkeyProvider::StringType::PUBLIC, multipath); break; case StringType::COMPAT: - tmp = pubkey->ToString(PubkeyProvider::StringType::COMPAT); + tmp = pubkey->ToString(PubkeyProvider::StringType::COMPAT, multipath); break; } ret += tmp; } std::string subscript; - bool subscript_res{ToStringSubScriptHelper(arg, subscript, type, cache)}; + bool subscript_res{ToStringSubScriptHelper(arg, subscript, type, cache, multipath)}; if (!is_private && !subscript_res) return false; any_success = any_success || subscript_res; if (pos && subscript.size()) ret += ','; @@ -1520,7 +1579,7 @@ class TRDescriptor final : public DescriptorImpl out.tr_trees[output] = builder; return Vector(GetScriptForDestination(output)); } - bool ToStringSubScriptHelper(const SigningProvider* arg, std::string& ret, const StringType type, const DescriptorCache* cache = nullptr) const override + bool ToStringSubScriptHelper(const SigningProvider* arg, std::string& ret, const StringType type, const DescriptorCache* cache = nullptr, bool multipath = false) const override { if (m_depths.empty()) { // If there are no sub-descriptors and a PRIVATE string @@ -1543,7 +1602,7 @@ class TRDescriptor final : public DescriptorImpl path.push_back(false); } std::string tmp; - bool subscript_res{m_subdescriptor_args[pos]->ToStringHelper(arg, tmp, type, cache)}; + bool subscript_res{m_subdescriptor_args[pos]->ToStringHelper(arg, tmp, type, cache, multipath)}; if (!is_private && !subscript_res) return false; any_success = any_success || subscript_res; ret += tmp; @@ -1638,13 +1697,16 @@ class StringMaker { //! StringType to serialize keys const DescriptorImpl::StringType m_type; const DescriptorCache* m_cache; + //! Whether to emit multipath specifiers instead of the expanded path elements + const bool m_multipath; public: StringMaker(const SigningProvider* arg LIFETIMEBOUND, const std::vector>& pubkeys LIFETIMEBOUND, DescriptorImpl::StringType type, - const DescriptorCache* cache LIFETIMEBOUND) - : m_arg(arg), m_pubkeys(pubkeys), m_type(type), m_cache(cache) {} + const DescriptorCache* cache LIFETIMEBOUND, + bool multipath = false) + : m_arg(arg), m_pubkeys(pubkeys), m_type(type), m_cache(cache), m_multipath(multipath) {} std::optional ToString(uint32_t key, bool& has_priv_key) const { @@ -1652,16 +1714,16 @@ class StringMaker { has_priv_key = false; switch (m_type) { case DescriptorImpl::StringType::PUBLIC: - ret = m_pubkeys[key]->ToString(); + ret = m_pubkeys[key]->ToString(PubkeyProvider::StringType::PUBLIC, m_multipath); break; case DescriptorImpl::StringType::PRIVATE: - has_priv_key = m_pubkeys[key]->ToPrivateString(*m_arg, ret); + has_priv_key = m_pubkeys[key]->ToPrivateString(*m_arg, ret, m_multipath); break; case DescriptorImpl::StringType::NORMALIZED: - if (!m_pubkeys[key]->ToNormalizedString(*m_arg, ret, m_cache)) return {}; + if (!m_pubkeys[key]->ToNormalizedString(*m_arg, ret, m_cache, m_multipath)) return {}; break; case DescriptorImpl::StringType::COMPAT: - ret = m_pubkeys[key]->ToString(PubkeyProvider::StringType::COMPAT); + ret = m_pubkeys[key]->ToString(PubkeyProvider::StringType::COMPAT, m_multipath); break; } return ret; @@ -1710,10 +1772,10 @@ class MiniscriptDescriptor final : public DescriptorImpl } bool ToStringHelper(const SigningProvider* arg, std::string& out, const StringType type, - const DescriptorCache* cache = nullptr) const override + const DescriptorCache* cache = nullptr, bool multipath = false) const override { bool has_priv_key{false}; - auto res = m_node.ToString(StringMaker(arg, m_pubkey_args, type, cache), has_priv_key); + auto res = m_node.ToString(StringMaker(arg, m_pubkey_args, type, cache, multipath), has_priv_key); if (res) out = *res; if (type == StringType::PRIVATE) { Assume(res.has_value()); @@ -1824,9 +1886,10 @@ enum class ParseScriptContext { * @param[out] error parsing error message * @param[in] allow_multipath Allows the parsed path to use the multipath specifier * @param[out] has_hardened Records whether the path contains any hardened derivation + * @param[out] multipath_out If not null, set to the multipath specifier info when one was parsed * @returns false if parsing failed **/ -[[nodiscard]] bool ParseKeyPath(const std::vector>& split, std::vector& out, bool& apostrophe, std::string& error, bool allow_multipath, bool& has_hardened) +[[nodiscard]] bool ParseKeyPath(const std::vector>& split, std::vector& out, bool& apostrophe, std::string& error, bool allow_multipath, bool& has_hardened, std::optional* multipath_out = nullptr) { auto parse_elem = [&](std::span elem) -> std::optional { const auto parsed{ParseKeyPathElement(elem)}; @@ -1895,6 +1958,7 @@ enum class ParseScriptContext { if (!substitutes) { out.emplace_back(std::move(path)); } else { + if (multipath_out) *multipath_out = MultipathInfo{substitutes->placeholder_index, substitutes->values}; // Replace the multipath placeholder with each value while generating paths for (uint32_t substitute : substitutes->values) { KeyPath branch_path = path; @@ -1992,13 +2056,15 @@ std::vector> ParsePubkeyInner(uint32_t& key_exp_ } std::vector paths; DeriveType type = ParseDeriveType(split, apostrophe); - if (!ParseKeyPath(split, paths, apostrophe, error, /*allow_multipath=*/true)) return {}; + bool dummy_has_hardened{false}; + std::optional multipath; + if (!ParseKeyPath(split, paths, apostrophe, error, /*allow_multipath=*/true, dummy_has_hardened, &multipath)) return {}; if (extkey.key.IsValid()) { extpubkey = extkey.Neuter(); out.keys.emplace(extpubkey.pubkey.GetID(), extkey.key); } for (auto& path : paths) { - ret.emplace_back(std::make_unique(key_exp_index, extpubkey, std::move(path), type, apostrophe)); + ret.emplace_back(std::make_unique(key_exp_index, extpubkey, std::move(path), type, apostrophe, multipath)); } ++key_exp_index; return ret; @@ -2067,6 +2133,7 @@ std::vector> ParsePubkey(uint32_t& key_exp_index // Parse any derivation DeriveType deriv_type = DeriveType::NON_RANGED; std::vector derivation_multipaths; + std::optional derivation_multipath_info; if (split.size() == 2 && Const("/", split.at(1), /*skip=*/false)) { if (!all_bip32) { error = "musig(): derivation requires all participants to be xpubs or xprvs"; @@ -2084,7 +2151,7 @@ std::vector> ParsePubkey(uint32_t& key_exp_index return {}; } bool has_hardened = false; - if (!ParseKeyPath(deriv_split, derivation_multipaths, dummy, error, /*allow_multipath=*/true, has_hardened)) { + if (!ParseKeyPath(deriv_split, derivation_multipaths, dummy, error, /*allow_multipath=*/true, has_hardened, &derivation_multipath_info)) { error = "musig(): " + error; return {}; } @@ -2113,14 +2180,14 @@ std::vector> ParsePubkey(uint32_t& key_exp_index // Emplace the final MuSigPubkeyProvider into ret with the pubkey providers from the specified provider vectors index // and the path from the specified path index - const auto& emplace_final_provider = [&ret, &key_exp_index, &deriv_type, &derivation_multipaths, &providers](size_t vec_idx, size_t path_idx) -> void { + const auto& emplace_final_provider = [&ret, &key_exp_index, &deriv_type, &derivation_multipaths, &derivation_multipath_info, &providers](size_t vec_idx, size_t path_idx) -> void { KeyPath& path = derivation_multipaths.at(path_idx); std::vector> pubs; pubs.reserve(providers.size()); for (auto& vec : providers) { pubs.emplace_back(std::move(vec.at(vec_idx))); } - ret.emplace_back(std::make_unique(key_exp_index, std::move(pubs), path, deriv_type)); + ret.emplace_back(std::make_unique(key_exp_index, std::move(pubs), path, deriv_type, derivation_multipath_info)); }; if (max_multipath_len > 1 && derivation_multipaths.size() > 1) { @@ -2993,6 +3060,49 @@ uint256 DescriptorID(const Descriptor& desc) return id; } +MultipathDescriptor::MultipathDescriptor(std::vector> descs) +{ + assert(descs.size() >= 2); + m_descriptors.reserve(descs.size()); + for (auto& desc : descs) { + m_descriptors.emplace_back(std::move(desc)); + } +} + +std::string MultipathDescriptor::ToString(bool compat_format) const +{ + // All paths carry the same multipath annotations, so any single one can + // reproduce the multipath string; use the first. + const auto& desc = dynamic_cast(*m_descriptors.front()); + std::string ret; + desc.ToStringHelper(nullptr, ret, compat_format ? DescriptorImpl::StringType::COMPAT : DescriptorImpl::StringType::PUBLIC, nullptr, /*multipath=*/true); + return AddChecksum(ret); +} + +bool MultipathDescriptor::ToPrivateString(const SigningProvider& provider, std::string& out) const +{ + const auto& desc = dynamic_cast(*m_descriptors.front()); + bool has_priv_key{desc.ToStringHelper(&provider, out, DescriptorImpl::StringType::PRIVATE, nullptr, /*multipath=*/true)}; + out = AddChecksum(out); + return has_priv_key; +} + +bool MultipathDescriptor::ToNormalizedString(const SigningProvider& provider, std::string& out, const DescriptorCache* cache) const +{ + const auto& desc = dynamic_cast(*m_descriptors.front()); + if (!desc.ToStringHelper(&provider, out, DescriptorImpl::StringType::NORMALIZED, cache, /*multipath=*/true)) return false; + out = AddChecksum(out); + return true; +} + +uint256 DescriptorID(const MultipathDescriptor& desc) +{ + std::string desc_str = desc.ToString(/*compat_format=*/true); + uint256 id; + CSHA256().Write((unsigned char*)desc_str.data(), desc_str.size()).Finalize(id.begin()); + return id; +} + void DescriptorCache::CacheParentExtPubKey(uint32_t key_exp_pos, const CExtPubKey& xpub) { m_parent_xpubs[key_exp_pos] = xpub; diff --git a/src/script/descriptor.h b/src/script/descriptor.h index 26781629cbf3..fdb1e324f357 100644 --- a/src/script/descriptor.h +++ b/src/script/descriptor.h @@ -205,6 +205,36 @@ struct Descriptor { virtual size_t GetKeyCount() const = 0; }; +/** A parsed BIP 389 multipath descriptor. + * + * Holds the expanded single-path descriptors that Parse() produced for each + * element of the multipath specifier, while retaining the ability to emit the + * combined multipath string forms and a single descriptor identifier. + */ +class MultipathDescriptor +{ + //! The expanded per-path descriptors, in the order the multipath specifier lists them. + std::vector> m_descriptors; + +public: + //! descs must be the (at least two) sibling descriptors produced by a single Parse() call. + explicit MultipathDescriptor(std::vector> descs); + + //! Number of paths in the multipath specifier. + size_t PathCount() const { return m_descriptors.size(); } + //! The expanded descriptor for the given path index. + const std::shared_ptr& PathAt(size_t path) const { return m_descriptors.at(path); } + + /** Convert back to the multipath descriptor string, with checksum. */ + std::string ToString(bool compat_format = false) const; + /** Convert to the multipath private string, see Descriptor::ToPrivateString. */ + bool ToPrivateString(const SigningProvider& provider, std::string& out) const; + /** Convert to the multipath normalized string, see Descriptor::ToNormalizedString. + * Also fails when no single normalized multipath form exists, which is the + * case for hardened multipath specifiers. */ + bool ToNormalizedString(const SigningProvider& provider, std::string& out, const DescriptorCache* cache = nullptr) const; +}; + /** Parse a `descriptor` string. Included private keys are put in `out`. * * If the descriptor has a checksum, it must be valid. If `require_checksum` @@ -244,4 +274,8 @@ std::unique_ptr InferDescriptor(const CScript& script, const Signing */ uint256 DescriptorID(const Descriptor& desc); +/** DescriptorID of a multipath descriptor, computed over its combined multipath string, + * so that all paths share a single identifier. */ +uint256 DescriptorID(const MultipathDescriptor& desc); + #endif // BITCOIN_SCRIPT_DESCRIPTOR_H diff --git a/src/test/descriptor_tests.cpp b/src/test/descriptor_tests.cpp index ed410dfbb148..bf975491582d 100644 --- a/src/test/descriptor_tests.cpp +++ b/src/test/descriptor_tests.cpp @@ -533,6 +533,38 @@ void CheckMultipath(const std::string& prv, expanded_prvs.at(i), expanded_pubs.at(i), i); } + // Any single path can reproduce the multipath string forms, and all paths share one identifier. + const auto parse_multipath{[&](const std::string& desc_str, FlatSigningProvider& keys) { + std::string parse_error; + auto parsed{Parse(desc_str, keys, parse_error)}; + BOOST_REQUIRE_MESSAGE(parsed.size() == expanded_pubs.size(), desc_str + ": " + parse_error); + return MultipathDescriptor{std::move(parsed)}; + }}; + FlatSigningProvider keys_prv, keys_pub; + const MultipathDescriptor multipath_prv{parse_multipath(prv, keys_prv)}; + const MultipathDescriptor multipath_pub{parse_multipath(pub, keys_pub)}; + + BOOST_CHECK_MESSAGE(EqualDescriptor(multipath_prv.ToString(), pub), "Private ser: " + multipath_prv.ToString() + " Public desc: " + pub); + BOOST_CHECK_MESSAGE(EqualDescriptor(multipath_pub.ToString(), pub), "Public ser: " + multipath_pub.ToString() + " Public desc: " + pub); + + std::string prv_ser; + BOOST_CHECK(multipath_prv.ToPrivateString(keys_prv, prv_ser)); + BOOST_CHECK_MESSAGE(EqualDescriptor(prv_ser, prv), "Private ser: " + prv_ser + " Private desc: " + prv); + BOOST_CHECK(multipath_pub.ToPrivateString(keys_prv, prv_ser)); + BOOST_CHECK_MESSAGE(EqualDescriptor(prv_ser, prv), "Private ser: " + prv_ser + " Private desc: " + prv); + BOOST_CHECK(!multipath_pub.ToPrivateString(keys_pub, prv_ser)); + + // The expanded descriptors keep emitting their own concrete path + BOOST_CHECK_EQUAL(multipath_pub.PathCount(), expanded_pubs.size()); + for (size_t i = 0; i < expanded_pubs.size(); ++i) { + const std::string path_ser{multipath_pub.PathAt(i)->ToString()}; + BOOST_CHECK_MESSAGE(EqualDescriptor(path_ser, expanded_pubs.at(i)), "Path ser: " + path_ser + " Path desc: " + expanded_pubs.at(i)); + } + + // All paths share one identifier, which is not the identifier of any single path + BOOST_CHECK(DescriptorID(multipath_prv) == DescriptorID(multipath_pub)); + BOOST_CHECK(DescriptorID(multipath_pub) != DescriptorID(*multipath_pub.PathAt(0))); + // The descriptor for each path must be standalone. They should not share common references. Test this // by parsing a multipath descriptor expression, deallocating all but one of the descriptors and making // sure we can perform operations on it. @@ -1418,4 +1450,42 @@ BOOST_AUTO_TEST_CASE(unused_descriptor_test) CheckUnused("unused(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc/0h/0h/1)", "unused(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/0h/0h/1)"); } +BOOST_AUTO_TEST_CASE(multipath_descriptor_normalized_string) +{ + const std::string master_xprv{"xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U"}; + const std::string master_xpub{"xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB"}; + // The above, derived at 2147483647h + const std::string hardened_xpub{"xpub69H7F5dQzmVd3vPuLKtcXJziMEQByuDidnX3YdwgtNsecY5HRGtAAQC5mXTt4dsv9RzyjgDjAQs9VGVV6ydYCHnprc9vvaA5YtqWyL6hyds"}; + + // Normalized form of each multipath descriptor, or std::nullopt if it has none. + const std::vector>> cases{ + // Without hardened derivation the normalized form is the public form + {"pkh(" + master_xprv + "/<0;1>/0)", "pkh(" + master_xpub + "/<0;1>/0)"}, + // A hardened step before the multipath element moves into the origin + {"pkh(" + master_xprv + "/2147483647h/<0;1>)", "pkh([bd16bee5/2147483647h]" + hardened_xpub + "/<0;1>)"}, + // Each path of a hardened multipath specifier normalizes to a different last + // hardened xpub, so there is no single normalized multipath string + {"pkh(" + master_xprv + "/<2147483647h;0>/0)", std::nullopt}, + }; + + for (const auto& [desc_str, norm] : cases) { + FlatSigningProvider keys; + std::string error; + auto parsed{Parse(desc_str, keys, error)}; + BOOST_REQUIRE_MESSAGE(parsed.size() == 2, desc_str + ": " + error); + const MultipathDescriptor desc{std::move(parsed)}; + std::string norm_ser; + BOOST_CHECK_EQUAL(desc.ToNormalizedString(keys, norm_ser), norm.has_value()); + if (norm) BOOST_CHECK_MESSAGE(EqualDescriptor(norm_ser, *norm), "Normalized ser: " + norm_ser + " Norm. desc: " + *norm); + } + + // The identifier is computed over the compat format, which always uses the + // apostrophe, so the hardened marker of the input does not affect it. + FlatSigningProvider keys_h, keys_apostrophe; + std::string error; + const MultipathDescriptor desc_h{Parse("pkh(" + master_xprv + "/<2147483647h;0>/0)", keys_h, error)}; + const MultipathDescriptor desc_apostrophe{Parse("pkh(" + master_xprv + "/<2147483647\';0>/0)", keys_apostrophe, error)}; + BOOST_CHECK(DescriptorID(desc_h) == DescriptorID(desc_apostrophe)); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/src/wallet/export.cpp b/src/wallet/export.cpp index 9f9a9486db88..2a802686c373 100644 --- a/src/wallet/export.cpp +++ b/src/wallet/export.cpp @@ -31,13 +31,21 @@ util::Expected, std::string> ExportDescriptors(const return util::Unexpected{"Can't get descriptor string."}; } const bool is_range = wallet_descriptor.descriptor->IsRange(); + const bool is_multipath = wallet_descriptor.IsMultipath(); + // For a multipath descriptor, report the range up to the furthest path + // end and the next index of each chain. + int64_t range_end = wallet_descriptor.GetEnd(); + for (size_t path = 1; path < wallet_descriptor.NumPaths(); ++path) { + range_end = std::max(range_end, wallet_descriptor.GetEnd(path)); + } wallet_descriptors.emplace_back( descriptor, wallet_descriptor.creation_time, wallet.IsActiveScriptPubKeyMan(*desc_spk_man), wallet.IsInternalScriptPubKeyMan(desc_spk_man), - is_range ? std::optional(std::make_pair(wallet_descriptor.GetStart(), wallet_descriptor.GetEnd())) : std::nullopt, - wallet_descriptor.GetNext() + is_range ? std::optional(std::pair(wallet_descriptor.GetStart(), range_end)) : std::nullopt, + wallet_descriptor.GetNext(), + is_multipath ? std::optional(wallet_descriptor.GetNext(1)) : std::nullopt ); } return wallet_descriptors; @@ -92,7 +100,9 @@ util::Result ExportWatchOnlyWallet(const CWallet& wallet, const fs: FlatSigningProvider dummy_keys; std::string dummy_err; std::vector> descs = Parse(desc_info.descriptor, dummy_keys, dummy_err, /*require_checksum=*/true); - CHECK_NONFATAL(descs.size() == 1); // All of our descriptors should be valid, and not multipath + CHECK_NONFATAL(!descs.empty()); // All of our descriptors should be valid + // Only multipath wallets export multipath descriptors, and vice versa + CHECK_NONFATAL((descs.size() > 1) == wallet.IsWalletFlagSet(WALLET_FLAG_MULTIPATH_DESCRIPTORS)); CHECK_NONFATAL(dummy_keys.keys.size() == 0); // No private keys should be present in our exported descriptors // Get the range if there is one @@ -103,29 +113,45 @@ util::Result ExportWatchOnlyWallet(const CWallet& wallet, const fs: range_end = desc_info.range->second; } - WalletDescriptor w_desc(std::move(descs.at(0)), desc_info.creation_time, range_start, range_end, desc_info.next_index); + std::optional w_desc; + if (descs.size() > 1) { + w_desc.emplace(std::make_shared(std::move(descs)), desc_info.creation_time, range_start, range_end, desc_info.next_index); + if (desc_info.next_index_internal) w_desc->SetNext(/*path=*/1, *desc_info.next_index_internal); + } else { + w_desc.emplace(std::move(descs.at(0)), desc_info.creation_time, range_start, range_end, desc_info.next_index); + } // For descriptors that cannot self expand (i.e. needs private keys or cache), retrieve the cache - uint256 desc_id = w_desc.id; - if (!w_desc.descriptor->CanSelfExpand()) { + uint256 desc_id = w_desc->id; + if (!w_desc->descriptor->CanSelfExpand()) { DescriptorScriptPubKeyMan* desc_spkm = dynamic_cast(wallet.GetScriptPubKeyMan(desc_id)); - w_desc.cache = WITH_LOCK(desc_spkm->cs_desc_man, return desc_spkm->GetWalletDescriptor().cache); + LOCK(desc_spkm->cs_desc_man); + for (size_t path = 0; path < w_desc->NumPaths(); ++path) { + w_desc->CacheAt(path) = desc_spkm->GetWalletDescriptor().CacheAt(path); + } } // Add to the watchonly wallet - if (auto spkm_res = watchonly_wallet->AddWalletDescriptor(w_desc, dummy_keys, /*label=*/"", /*internal=*/false); !spkm_res) { + if (auto spkm_res = watchonly_wallet->AddWalletDescriptor(*w_desc, dummy_keys, /*label=*/"", /*internal=*/false); !spkm_res) { return util::Error{util::ErrorString(spkm_res)}; } // Set active spkms as active if (desc_info.active) { - // Determine whether this descriptor is internal - // This is only set for active spkms - bool internal = false; - if (desc_info.internal) { - internal = *desc_info.internal; + const OutputType output_type = *Assert(w_desc->descriptor->GetOutputType()); + if (w_desc->IsMultipath()) { + // A multipath descriptor covers both chains + watchonly_wallet->AddActiveScriptPubKeyMan(desc_id, output_type, /*internal=*/false); + watchonly_wallet->AddActiveScriptPubKeyMan(desc_id, output_type, /*internal=*/true); + } else { + // Determine whether this descriptor is internal + // This is only set for active spkms + bool internal = false; + if (desc_info.internal) { + internal = *desc_info.internal; + } + watchonly_wallet->AddActiveScriptPubKeyMan(desc_id, output_type, internal); } - watchonly_wallet->AddActiveScriptPubKeyMan(desc_id, *Assert(w_desc.descriptor->GetOutputType()), internal); } } diff --git a/src/wallet/export.h b/src/wallet/export.h index 5f2289302ccb..4a6e6d8c392c 100644 --- a/src/wallet/export.h +++ b/src/wallet/export.h @@ -23,6 +23,9 @@ struct WalletDescInfo { std::optional internal; std::optional> range; int64_t next_index; + //! The next index of the change chain; only set for multipath descriptors, + //! whose next_index refers to the receive chain + std::optional next_index_internal; }; //! Export the descriptors from a wallet so that they can be imported elsewhere diff --git a/src/wallet/external_signer_scriptpubkeyman.cpp b/src/wallet/external_signer_scriptpubkeyman.cpp index 758be582ac4a..a275ba840c2a 100644 --- a/src/wallet/external_signer_scriptpubkeyman.cpp +++ b/src/wallet/external_signer_scriptpubkeyman.cpp @@ -26,7 +26,7 @@ std::unique_ptr ExternalSignerScriptPubKeyMan::Lo return std::unique_ptr(new ExternalSignerScriptPubKeyMan(storage, descriptor, keypool_size, keys, ckeys)); } -std::unique_ptr ExternalSignerScriptPubKeyMan::CreateNew(WalletStorage& storage, WalletBatch& batch, int64_t keypool_size, std::unique_ptr desc) +std::unique_ptr ExternalSignerScriptPubKeyMan::CreateNew(WalletStorage& storage, WalletBatch& batch, int64_t keypool_size, WalletDescriptor&& w_desc) { auto spkm = std::unique_ptr(new ExternalSignerScriptPubKeyMan(storage, keypool_size)); @@ -34,11 +34,7 @@ std::unique_ptr ExternalSignerScriptPubKeyMan::Cr assert(storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)); assert(storage.IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER)); - int64_t creation_time = GetTime(); - - // Make the descriptor - WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0); - spkm->m_wallet_descriptor = w_desc; + spkm->m_wallet_descriptor = std::move(w_desc); // Store the descriptor if (!batch.WriteDescriptor(spkm->GetID(), spkm->m_wallet_descriptor)) { @@ -52,6 +48,16 @@ std::unique_ptr ExternalSignerScriptPubKeyMan::Cr return spkm; } +std::unique_ptr ExternalSignerScriptPubKeyMan::CreateNew(WalletStorage& storage, WalletBatch& batch, int64_t keypool_size, std::unique_ptr desc) +{ + return CreateNew(storage, batch, keypool_size, WalletDescriptor(std::move(desc), /*creation_time=*/GetTime(), 0, 0, 0)); +} + +std::unique_ptr ExternalSignerScriptPubKeyMan::CreateNew(WalletStorage& storage, WalletBatch& batch, int64_t keypool_size, std::shared_ptr desc) +{ + return CreateNew(storage, batch, keypool_size, WalletDescriptor(std::move(desc), /*creation_time=*/GetTime(), 0, 0, 0)); +} + util::Result ExternalSignerScriptPubKeyMan::GetExternalSigner() { const std::string command = gArgs.GetArg("-signer", ""); if (command == "") return util::Error{Untranslated("restart bitcoind with -signer=")}; diff --git a/src/wallet/external_signer_scriptpubkeyman.h b/src/wallet/external_signer_scriptpubkeyman.h index 8a3ae3df7a2b..f7e7c90779c5 100644 --- a/src/wallet/external_signer_scriptpubkeyman.h +++ b/src/wallet/external_signer_scriptpubkeyman.h @@ -25,9 +25,13 @@ class ExternalSignerScriptPubKeyMan : public DescriptorScriptPubKeyMan : DescriptorScriptPubKeyMan(storage, keypool_size) {} + //! Create an ExternalSPKM holding the given descriptor, shared by the CreateNew overloads + static std::unique_ptr CreateNew(WalletStorage& storage, WalletBatch& batch, int64_t keypool_size, WalletDescriptor&& w_desc); + public: static std::unique_ptr LoadFromStorage(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys); static std::unique_ptr CreateNew(WalletStorage& storage, WalletBatch& batch, int64_t keypool_size, std::unique_ptr desc); + static std::unique_ptr CreateNew(WalletStorage& storage, WalletBatch& batch, int64_t keypool_size, std::shared_ptr desc); static util::Result GetExternalSigner(); diff --git a/src/wallet/rpc/backup.cpp b/src/wallet/rpc/backup.cpp index 70bc3bde9aee..76b19bc380ac 100644 --- a/src/wallet/rpc/backup.cpp +++ b/src/wallet/rpc/backup.cpp @@ -231,6 +231,74 @@ static UniValue ProcessDescriptorImport(CWallet& wallet, const UniValue& data, c throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled"); } + // A multipath wallet imports the multipath descriptor as a single record + // covering both the receive and change chain, and supports nothing else. + if (wallet.IsWalletFlagSet(WALLET_FLAG_MULTIPATH_DESCRIPTORS)) { + if (parsed_descs.size() != 2) { + throw JSONRPCError(RPC_INVALID_PARAMETER, "This wallet only supports multipath descriptors with exactly two derivation paths (e.g. /<0;1>)"); + } + if (!parsed_descs.at(0)->HasScripts()) { + throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import unused() to a multipath descriptors wallet"); + } + + // Expand each path to check whether the descriptor can be derived at the first index. + for (const auto& parsed_desc : parsed_descs) { + FlatSigningProvider expand_keys; + std::vector scripts; + if (!parsed_desc->Expand(0, keys, scripts, expand_keys)) { + throw JSONRPCError(RPC_WALLET_ERROR, "Cannot expand descriptor. Probably because of hardened derivations without private keys provided"); + } + } + for (const auto& w : parsed_descs.at(0)->Warnings()) { + warnings.push_back(w); + } + + // If private keys are enabled, check some things. + if (!wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { + if (keys.keys.empty()) { + throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import descriptor without private keys to a wallet with private keys enabled"); + } + if (!parsed_descs.at(0)->HavePrivateKeys(keys)) { + warnings.push_back("Not all private keys provided. Some wallet functionality may return unexpected errors"); + } + } + + auto multipath_desc = std::make_shared(std::move(parsed_descs)); + // The wallet relies on the normalized multipath form, which does not + // exist for hardened multipath specifiers, since each path then + // normalizes to a different last hardened xpub. + std::string normalized; + if (!multipath_desc->ToNormalizedString(keys, normalized)) { + throw JSONRPCError(RPC_INVALID_PARAMETER, "Multipath specifiers with hardened derivation are not supported"); + } + + WalletDescriptor w_desc(std::move(multipath_desc), timestamp, range_start, range_end, next_index); + + // Add descriptor to the wallet + auto spk_manager_res = wallet.AddWalletDescriptor(w_desc, keys, label, /*internal=*/false); + if (!spk_manager_res) { + throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Could not add descriptor '%s': %s", descriptor, util::ErrorString(spk_manager_res).original)); + } + auto& spk_manager = spk_manager_res.value().get(); + + // The descriptor covers both chains, so (de)activate it for both + if (!w_desc.descriptor->GetOutputType()) { + if (active) warnings.push_back("Unknown output type, cannot set descriptor to active."); + } else { + for (const bool desc_internal : {false, true}) { + if (active) { + wallet.AddActiveScriptPubKeyMan(spk_manager.GetID(), *w_desc.descriptor->GetOutputType(), desc_internal); + } else { + wallet.DeactivateScriptPubKeyMan(spk_manager.GetID(), *w_desc.descriptor->GetOutputType(), desc_internal); + } + } + } + + result.pushKV("success", UniValue(true)); + PushWarnings(warnings, result); + return result; + } + for (size_t j = 0; j < parsed_descs.size(); ++j) { auto parsed_desc = std::move(parsed_descs[j]); if (parsed_descs.size() == 2) { @@ -319,6 +387,7 @@ RPCMethod importdescriptors() "importdescriptors", "Import descriptors. This will trigger a rescan of the blockchain based on the earliest timestamp of all descriptors being imported. Requires a new wallet backup.\n" "When importing descriptors with multipath key expressions, if the multipath specifier contains exactly two elements, the descriptor produced from the second element will be imported as an internal descriptor.\n" + "A wallet created with the multipath option instead imports such a descriptor as a single record covering both the receive and change chain, and only accepts multipath descriptors with exactly two derivation paths.\n" "\nNote: This call can take over an hour to complete if using an early timestamp; during that time, other rpc calls\n" "may report that the imported keys, addresses or scripts exist but related transactions are still missing.\n" "The rescan is significantly faster if block filters are available (using startup option \"-blockfilterindex=1\").\n", @@ -498,7 +567,8 @@ RPCMethod listdescriptors() {RPCResult::Type::NUM, "", "Range end inclusive"}, }}, {RPCResult::Type::NUM, "next", /*optional=*/true, "Same as next_index field. Kept for compatibility reason."}, - {RPCResult::Type::NUM, "next_index", /*optional=*/true, "The next index to generate addresses from; defined only for ranged descriptors"}, + {RPCResult::Type::NUM, "next_index", /*optional=*/true, "The next index to generate addresses from; defined only for ranged descriptors. For multipath descriptors this refers to the receive chain"}, + {RPCResult::Type::NUM, "next_index_internal", /*optional=*/true, "The next index of the change chain; defined only for multipath descriptors"}, }}, }} }}, @@ -546,6 +616,9 @@ RPCMethod listdescriptors() spk.pushKV("range", std::move(range)); spk.pushKV("next", info.next_index); spk.pushKV("next_index", info.next_index); + if (info.next_index_internal.has_value()) { + spk.pushKV("next_index_internal", info.next_index_internal.value()); + } } descriptors.push_back(std::move(spk)); } diff --git a/src/wallet/rpc/util.cpp b/src/wallet/rpc/util.cpp index 77a8745ced7e..0f355dc88f2c 100644 --- a/src/wallet/rpc/util.cpp +++ b/src/wallet/rpc/util.cpp @@ -115,7 +115,7 @@ void PushParentDescriptors(const CWallet& wallet, const CScript& script_pubkey, for (const auto& desc: wallet.GetWalletDescriptors(script_pubkey)) { std::string desc_str; FlatSigningProvider dummy_provider; - if (!CHECK_NONFATAL(desc.descriptor->ToNormalizedString(dummy_provider, desc_str, &desc.cache))) continue; + if (!CHECK_NONFATAL(desc.descriptor->ToNormalizedString(dummy_provider, desc_str, &desc.CacheAt()))) continue; parent_descs.push_back(desc_str); } entry.pushKV("parent_descs", std::move(parent_descs)); diff --git a/src/wallet/rpc/wallet.cpp b/src/wallet/rpc/wallet.cpp index 054ab6b76310..e4b515e248d4 100644 --- a/src/wallet/rpc/wallet.cpp +++ b/src/wallet/rpc/wallet.cpp @@ -366,6 +366,7 @@ static RPCMethod createwallet() {"descriptors", RPCArg::Type::BOOL, RPCArg::Default{true}, "If set, must be \"true\""}, {"load_on_startup", RPCArg::Type::BOOL, RPCArg::Optional::OMITTED, "Save wallet name to persistent settings and load on startup. True to add wallet to startup list, false to remove, null to leave unchanged."}, {"external_signer", RPCArg::Type::BOOL, RPCArg::Default{false}, "Use an external signer such as a hardware wallet. Requires -signer to be configured. Wallet creation will fail if keys cannot be fetched. Requires disable_private_keys and descriptors set to true."}, + {"multipath", RPCArg::Type::BOOL, RPCArg::Default{false}, "Store descriptors in multipath form. The wallet keeps one BIP 389 multipath descriptor per output type, covering both the receive and change chain, and only supports such descriptors. Combined with external_signer, the signer must support the getdescriptors --multipath argument."}, }, RPCResult{ RPCResult::Type::OBJ, "", "", @@ -419,6 +420,9 @@ static RPCMethod createwallet() throw JSONRPCError(RPC_WALLET_ERROR, "Compiled without external signing support (required for external signing)"); #endif } + if (!request.params[8].isNull() && request.params[8].get_bool()) { + flags |= WALLET_FLAG_MULTIPATH_DESCRIPTORS; + } DatabaseOptions options; DatabaseStatus status; @@ -777,10 +781,15 @@ static RPCMethod createwalletdescriptor() UniValue internal_only{options["internal"]}; UniValue hdkey{options["hdkey"]}; + const bool multipath_wallet = pwallet->IsWalletFlagSet(WALLET_FLAG_MULTIPATH_DESCRIPTORS); + if (multipath_wallet && !internal_only.isNull()) { + throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot specify 'internal' for a multipath descriptors wallet, its descriptors always cover both chains"); + } + std::vector internals; if (internal_only.isNull()) { internals.push_back(false); - internals.push_back(true); + if (!multipath_wallet) internals.push_back(true); } else { internals.push_back(internal_only.get_bool()); } @@ -811,9 +820,9 @@ static RPCMethod createwalletdescriptor() std::vector> spkms; WalletBatch batch{pwallet->GetDatabase()}; for (bool internal : internals) { - WalletDescriptor w_desc = GenerateWalletDescriptor(xpub, *output_type, internal); - uint256 w_id = DescriptorID(*w_desc.descriptor); - if (!pwallet->GetScriptPubKeyMan(w_id)) { + WalletDescriptor w_desc = multipath_wallet ? GenerateMultipathWalletDescriptor(xpub, *output_type) + : GenerateWalletDescriptor(xpub, *output_type, internal); + if (!pwallet->GetScriptPubKeyMan(w_desc.id)) { spkms.emplace_back(pwallet->SetupDescriptorScriptPubKeyMan(batch, active_hdkey, *output_type, internal)); } } diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp index 30742753602e..e1ca4e74f977 100644 --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -871,50 +871,51 @@ std::unique_ptr DescriptorScriptPubKeyMan::GenerateNe return spkm; } -void DescriptorScriptPubKeyMan::IncIndex() +void DescriptorScriptPubKeyMan::IncIndex(size_t path) { AssertLockHeld(cs_desc_man); const auto old_can = CanGetAddresses(); - m_wallet_descriptor.IncNext(); + m_wallet_descriptor.IncNext(path); const auto new_can = CanGetAddresses(); if (old_can != new_can) { NotifyCanGetAddressesChanged(); } } -void DescriptorScriptPubKeyMan::DecIndex() +void DescriptorScriptPubKeyMan::DecIndex(size_t path) { AssertLockHeld(cs_desc_man); const auto old_can = CanGetAddresses(); - m_wallet_descriptor.DecNext(); + m_wallet_descriptor.DecNext(path); const auto new_can = CanGetAddresses(); if (old_can != new_can) { NotifyCanGetAddressesChanged(); } } -void DescriptorScriptPubKeyMan::SetRangeEnd(int32_t end) +void DescriptorScriptPubKeyMan::SetRangeEnd(size_t path, int32_t end) { AssertLockHeld(cs_desc_man); const auto old_can = CanGetAddresses(); - m_wallet_descriptor.SetEnd(end); + m_wallet_descriptor.SetEnd(path, end); const auto new_can = CanGetAddresses(); if (old_can != new_can) { NotifyCanGetAddressesChanged(); } } -util::Result DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type) +util::Result DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type, bool internal) { // 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()) { + if (!CanGetAddresses(internal)) { return util::Error{_("No addresses available")}; } { LOCK(cs_desc_man); + const size_t path = PathOf(internal); assert(m_wallet_descriptor.descriptor->IsSingleType()); // This is a combo descriptor which should not be an active descriptor std::optional desc_addr_type = m_wallet_descriptor.descriptor->GetOutputType(); assert(desc_addr_type); @@ -927,11 +928,11 @@ util::Result DescriptorScriptPubKeyMan::GetNewDestination(const // Get the scriptPubKey from the descriptor FlatSigningProvider out_keys; std::vector scripts_temp; - if (m_wallet_descriptor.GetEnd() <= m_max_cached_index && !TopUp(1)) { + if (m_wallet_descriptor.GetEnd(path) <= m_max_cached_index.at(path) && !TopUp(1)) { // We can't generate anymore keys return util::Error{_("Error: Keypool ran out, please call keypoolrefill first")}; } - if (!m_wallet_descriptor.descriptor->ExpandFromCache(m_wallet_descriptor.GetNext(), m_wallet_descriptor.cache, scripts_temp, out_keys)) { + if (!m_wallet_descriptor.DescAt(path).ExpandFromCache(m_wallet_descriptor.GetNext(path), m_wallet_descriptor.CacheAt(path), scripts_temp, out_keys)) { // We can't generate anymore keys return util::Error{_("Error: Keypool ran out, please call keypoolrefill first")}; } @@ -940,7 +941,7 @@ util::Result DescriptorScriptPubKeyMan::GetNewDestination(const if (!ExtractDestination(scripts_temp[0], dest)) { return util::Error{_("Error: Cannot extract destination from the generated scriptpubkey")}; // shouldn't happen } - IncIndex(); + IncIndex(path); WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor); return dest; } @@ -1010,17 +1011,18 @@ bool DescriptorScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, Walle util::Result DescriptorScriptPubKeyMan::GetReservedDestination(const OutputType type, bool internal, int64_t& index) { LOCK(cs_desc_man); - auto op_dest = GetNewDestination(type); - index = m_wallet_descriptor.GetNext() - 1; + auto op_dest = GetNewDestination(type, internal); + index = m_wallet_descriptor.GetNext(PathOf(internal)) - 1; return op_dest; } void DescriptorScriptPubKeyMan::ReturnDestination(int64_t index, bool internal, const CTxDestination& addr) { LOCK(cs_desc_man); + const size_t path = PathOf(internal); // Only return when the index was the most recent - if (m_wallet_descriptor.GetNext() - 1 == index) { - DecIndex(); + if (m_wallet_descriptor.GetNext(path) - 1 == index) { + DecIndex(path); } WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor); } @@ -1086,6 +1088,7 @@ bool DescriptorScriptPubKeyMan::TopUp(unsigned int size) bool DescriptorScriptPubKeyMan::TopUpWithDB(WalletBatch& batch, unsigned int size) { LOCK(cs_desc_man); + InitMaxCachedIndex(); std::set new_spks; unsigned int target_size; if (size > 0) { @@ -1094,53 +1097,58 @@ bool DescriptorScriptPubKeyMan::TopUpWithDB(WalletBatch& batch, unsigned int siz target_size = m_keypool_size; } - // Calculate the new range_end - int32_t new_range_end = std::max(m_wallet_descriptor.GetNext() + (int32_t)target_size, m_wallet_descriptor.GetEnd()); - - // If the descriptor is not ranged, we actually just want to fill the first cache item - if (!m_wallet_descriptor.descriptor->IsRange()) { - new_range_end = 1; - } - FlatSigningProvider provider; provider.keys = GetKeys(); uint256 id = GetID(); - for (int32_t i = m_max_cached_index + 1; i < new_range_end; ++i) { - FlatSigningProvider out_keys; - std::vector scripts_temp; - DescriptorCache temp_cache; - // Maybe we have a cached xpub and we can expand from the cache first - if (!m_wallet_descriptor.descriptor->ExpandFromCache(i, m_wallet_descriptor.cache, scripts_temp, out_keys)) { - if (!m_wallet_descriptor.descriptor->Expand(i, provider, scripts_temp, out_keys, &temp_cache)) return false; - } - // Add all of the scriptPubKeys to the scriptPubKey set - new_spks.insert(scripts_temp.begin(), scripts_temp.end()); - for (const CScript& script : scripts_temp) { - m_map_script_pub_keys[script] = i; - } - for (const auto& pk_pair : out_keys.pubkeys) { - const CPubKey& pubkey = pk_pair.second; - if (m_map_pubkeys.contains(pubkey)) { - // We don't need to give an error here. - // It doesn't matter which of many valid indexes the pubkey has, we just need an index where we can derive it and its private key - continue; - } - m_map_pubkeys[pubkey] = i; + for (size_t path = 0; path < m_wallet_descriptor.NumPaths(); ++path) { + // Calculate the new range_end + int32_t new_range_end = std::max(m_wallet_descriptor.GetNext(path) + (int32_t)target_size, m_wallet_descriptor.GetEnd(path)); + + // If the descriptor is not ranged, we actually just want to fill the first cache item + if (!m_wallet_descriptor.descriptor->IsRange()) { + new_range_end = 1; } - // Merge and write the cache - DescriptorCache new_items = m_wallet_descriptor.cache.MergeAndDiff(temp_cache); - if (!batch.WriteDescriptorCacheItems(id, new_items)) { - throw std::runtime_error(std::string(__func__) + ": writing cache items failed"); + + for (int32_t i = m_max_cached_index.at(path) + 1; i < new_range_end; ++i) { + FlatSigningProvider out_keys; + std::vector scripts_temp; + DescriptorCache temp_cache; + // Maybe we have a cached xpub and we can expand from the cache first + if (!m_wallet_descriptor.DescAt(path).ExpandFromCache(i, m_wallet_descriptor.CacheAt(path), scripts_temp, out_keys)) { + if (!m_wallet_descriptor.DescAt(path).Expand(i, provider, scripts_temp, out_keys, &temp_cache)) return false; + } + // Add all of the scriptPubKeys to the scriptPubKey set + new_spks.insert(scripts_temp.begin(), scripts_temp.end()); + for (const CScript& script : scripts_temp) { + m_map_script_pub_keys[script] = {i, (uint8_t)path}; + } + for (const auto& pk_pair : out_keys.pubkeys) { + const CPubKey& pubkey = pk_pair.second; + if (m_map_pubkeys.contains(pubkey)) { + // We don't need to give an error here. + // It doesn't matter which of many valid indexes the pubkey has, we just need an index where we can derive it and its private key + continue; + } + m_map_pubkeys[pubkey] = {i, (uint8_t)path}; + } + // Merge and write the cache + DescriptorCache new_items = m_wallet_descriptor.CacheAt(path).MergeAndDiff(temp_cache); + bool cache_written = m_wallet_descriptor.IsMultipath() ? + batch.WriteMultipathDescriptorCacheItems(id, path, new_items) : + batch.WriteDescriptorCacheItems(id, new_items); + if (!cache_written) { + throw std::runtime_error(std::string(__func__) + ": writing cache items failed"); + } + m_max_cached_index.at(path)++; } - m_max_cached_index++; + SetRangeEnd(path, new_range_end); + + // By this point, the cache size should be the size of the entire range + assert(m_wallet_descriptor.GetEnd(path) - 1 == m_max_cached_index.at(path)); } - SetRangeEnd(new_range_end); batch.WriteDescriptor(GetID(), m_wallet_descriptor); - // By this point, the cache size should be the size of the entire range - assert(m_wallet_descriptor.GetEnd() - 1 == m_max_cached_index); - m_storage.TopUpCallback(new_spks, this); return true; } @@ -1150,19 +1158,21 @@ std::vector DescriptorScriptPubKeyMan::MarkUnusedAddresses(co LOCK(cs_desc_man); std::vector result; if (IsMine(script)) { - int32_t index = m_map_script_pub_keys[script]; - if (index >= m_wallet_descriptor.GetNext()) { + const auto [index, path] = m_map_script_pub_keys[script]; + // For a multipath descriptor, report which chain the address belongs to + const std::optional internal = m_wallet_descriptor.IsMultipath() ? std::optional(path == 1) : std::nullopt; + if (index >= m_wallet_descriptor.GetNext(path)) { WalletLogPrintf("%s: Detected a used keypool item at index %d, mark all keypool items up to this item as used\n", __func__, index); auto out_keys = std::make_unique(); std::vector scripts_temp; - while (index >= m_wallet_descriptor.GetNext()) { - if (!m_wallet_descriptor.descriptor->ExpandFromCache(m_wallet_descriptor.GetNext(), m_wallet_descriptor.cache, scripts_temp, *out_keys)) { + while (index >= m_wallet_descriptor.GetNext(path)) { + if (!m_wallet_descriptor.DescAt(path).ExpandFromCache(m_wallet_descriptor.GetNext(path), m_wallet_descriptor.CacheAt(path), scripts_temp, *out_keys)) { throw std::runtime_error(std::string(__func__) + ": Unable to expand descriptor from cache"); } CTxDestination dest; ExtractDestination(scripts_temp[0], dest); - result.push_back({dest, std::nullopt}); - IncIndex(); + result.push_back({dest, internal}); + IncIndex(path); } } if (!TopUp()) { @@ -1220,7 +1230,11 @@ void DescriptorScriptPubKeyMan::SetupDescriptorGeneration(WalletBatch& batch, co Assert(m_storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)); Assert(!m_wallet_descriptor.descriptor); - m_wallet_descriptor = GenerateWalletDescriptor(master_key.Neuter(), addr_type, internal); + if (m_storage.IsWalletFlagSet(WALLET_FLAG_MULTIPATH_DESCRIPTORS)) { + m_wallet_descriptor = GenerateMultipathWalletDescriptor(master_key.Neuter(), addr_type); + } else { + m_wallet_descriptor = GenerateWalletDescriptor(master_key.Neuter(), addr_type, internal); + } // Store the master private key, and descriptor if (!AddDescriptorKeyWithDB(batch, master_key.key, master_key.key.GetPubKey())) { @@ -1252,9 +1266,10 @@ bool DescriptorScriptPubKeyMan::CanGetAddresses(bool internal) const // We can only give out addresses from descriptors that are single type (not combo), ranged, // and either have cached keys or can generate more keys (ignoring encryption) LOCK(cs_desc_man); + const size_t path = PathOf(internal); return m_wallet_descriptor.descriptor->IsSingleType() && m_wallet_descriptor.descriptor->IsRange() && - (HavePrivateKeys() || m_wallet_descriptor.GetNext() < m_wallet_descriptor.GetEnd() || m_wallet_descriptor.descriptor->CanSelfExpand()); + (HavePrivateKeys() || m_wallet_descriptor.GetNext(path) < m_wallet_descriptor.GetEnd(path) || m_wallet_descriptor.descriptor->CanSelfExpand()); } bool DescriptorScriptPubKeyMan::HavePrivateKeys() const @@ -1272,7 +1287,24 @@ bool DescriptorScriptPubKeyMan::HaveCryptedKeys() const unsigned int DescriptorScriptPubKeyMan::GetKeyPoolSize() const { LOCK(cs_desc_man); - return m_wallet_descriptor.GetEnd() - m_wallet_descriptor.GetNext(); + unsigned int size{0}; + for (size_t path = 0; path < m_wallet_descriptor.NumPaths(); ++path) { + size += m_wallet_descriptor.GetEnd(path) - m_wallet_descriptor.GetNext(path); + } + return size; +} + +unsigned int DescriptorScriptPubKeyMan::GetKeyPoolSize(bool internal) const +{ + LOCK(cs_desc_man); + const size_t path = PathOf(internal); + return m_wallet_descriptor.GetEnd(path) - m_wallet_descriptor.GetNext(path); +} + +bool DescriptorScriptPubKeyMan::IsMultipathDescriptor() const +{ + LOCK(cs_desc_man); + return m_wallet_descriptor.IsMultipath(); } int64_t DescriptorScriptPubKeyMan::GetTimeFirstKey() const @@ -1285,58 +1317,56 @@ std::unique_ptr DescriptorScriptPubKeyMan::GetSigningProvid { LOCK(cs_desc_man); - // Find the index of the script + // Find the position of the script auto it = m_map_script_pub_keys.find(script); if (it == m_map_script_pub_keys.end()) { return nullptr; } - int32_t index = it->second; - return GetSigningProvider(index, include_private); + return GetSigningProvider(it->second, include_private); } std::unique_ptr DescriptorScriptPubKeyMan::GetSigningProvider(const CPubKey& pubkey) const { LOCK(cs_desc_man); - // Find index of the pubkey + // Find position of the pubkey auto it = m_map_pubkeys.find(pubkey); if (it == m_map_pubkeys.end()) { return nullptr; } - int32_t index = it->second; // Always try to get the signing provider with private keys. This function should only be called during signing anyways - std::unique_ptr out = GetSigningProvider(index, true); + std::unique_ptr out = GetSigningProvider(it->second, true); if (!out->HaveKey(pubkey.GetID())) { return nullptr; } return out; } -std::unique_ptr DescriptorScriptPubKeyMan::GetSigningProvider(int32_t index, bool include_private) const +std::unique_ptr DescriptorScriptPubKeyMan::GetSigningProvider(DescriptorPosition pos, bool include_private) const { AssertLockHeld(cs_desc_man); std::unique_ptr out_keys = std::make_unique(); // Fetch SigningProvider from cache to avoid re-deriving - auto it = m_map_signing_providers.find(index); + auto it = m_map_signing_providers.find({pos.path, pos.index}); if (it != m_map_signing_providers.end()) { out_keys->Merge(FlatSigningProvider{it->second}); } else { // Get the scripts, keys, and key origins for this script std::vector scripts_temp; - if (!m_wallet_descriptor.descriptor->ExpandFromCache(index, m_wallet_descriptor.cache, scripts_temp, *out_keys)) return nullptr; + if (!m_wallet_descriptor.DescAt(pos.path).ExpandFromCache(pos.index, m_wallet_descriptor.CacheAt(pos.path), scripts_temp, *out_keys)) return nullptr; // Cache SigningProvider so we don't need to re-derive if we need this SigningProvider again - m_map_signing_providers[index] = *out_keys; + m_map_signing_providers[{pos.path, pos.index}] = *out_keys; } if (HavePrivateKeys() && include_private) { FlatSigningProvider master_provider; master_provider.keys = GetKeys(); - m_wallet_descriptor.descriptor->ExpandPrivate(index, master_provider, *out_keys); + m_wallet_descriptor.DescAt(pos.path).ExpandPrivate(pos.index, master_provider, *out_keys); // Always include musig_secnonces as this descriptor may have a participant private key // but not a musig() descriptor @@ -1510,31 +1540,34 @@ uint256 DescriptorScriptPubKeyMan::GetID() const void DescriptorScriptPubKeyMan::Load() { LOCK(cs_desc_man); + InitMaxCachedIndex(); std::set new_spks; - for (int32_t i = m_wallet_descriptor.GetStart(); i < m_wallet_descriptor.GetEnd(); ++i) { - FlatSigningProvider out_keys; - std::vector scripts_temp; - if (!m_wallet_descriptor.descriptor->ExpandFromCache(i, m_wallet_descriptor.cache, scripts_temp, out_keys)) { - throw std::runtime_error("Error: Unable to expand wallet descriptor from cache"); - } - // Add all of the scriptPubKeys to the scriptPubKey set - new_spks.insert(scripts_temp.begin(), scripts_temp.end()); - for (const CScript& script : scripts_temp) { - if (m_map_script_pub_keys.contains(script)) { - throw std::runtime_error(strprintf("Error: Already loaded script at index %d as being at index %d", i, m_map_script_pub_keys[script])); + for (size_t path = 0; path < m_wallet_descriptor.NumPaths(); ++path) { + for (int32_t i = m_wallet_descriptor.GetStart(); i < m_wallet_descriptor.GetEnd(path); ++i) { + FlatSigningProvider out_keys; + std::vector scripts_temp; + if (!m_wallet_descriptor.DescAt(path).ExpandFromCache(i, m_wallet_descriptor.CacheAt(path), scripts_temp, out_keys)) { + throw std::runtime_error("Error: Unable to expand wallet descriptor from cache"); } - m_map_script_pub_keys[script] = i; - } - for (const auto& pk_pair : out_keys.pubkeys) { - const CPubKey& pubkey = pk_pair.second; - if (m_map_pubkeys.contains(pubkey)) { - // We don't need to give an error here. - // It doesn't matter which of many valid indexes the pubkey has, we just need an index where we can derive it and its private key - continue; + // Add all of the scriptPubKeys to the scriptPubKey set + new_spks.insert(scripts_temp.begin(), scripts_temp.end()); + for (const CScript& script : scripts_temp) { + if (m_map_script_pub_keys.contains(script)) { + throw std::runtime_error(strprintf("Error: Already loaded script at index %d as being at index %d", i, m_map_script_pub_keys[script].index)); + } + m_map_script_pub_keys[script] = {i, (uint8_t)path}; + } + for (const auto& pk_pair : out_keys.pubkeys) { + const CPubKey& pubkey = pk_pair.second; + if (m_map_pubkeys.contains(pubkey)) { + // We don't need to give an error here. + // It doesn't matter which of many valid indexes the pubkey has, we just need an index where we can derive it and its private key + continue; + } + m_map_pubkeys[pubkey] = {i, (uint8_t)path}; } - m_map_pubkeys[pubkey] = i; + m_max_cached_index.at(path)++; } - m_max_cached_index++; } // Make sure the wallet knows about our new spks m_storage.TopUpCallback(new_spks, this); @@ -1571,15 +1604,18 @@ std::unordered_set DescriptorScriptPubKeyMan::GetScrip std::unordered_set script_pub_keys; script_pub_keys.reserve(m_map_script_pub_keys.size()); - for (auto const& [script_pub_key, index] : m_map_script_pub_keys) { - if (index >= minimum_index) script_pub_keys.insert(script_pub_key); + for (auto const& [script_pub_key, pos] : m_map_script_pub_keys) { + if (pos.index >= minimum_index) script_pub_keys.insert(script_pub_key); } return script_pub_keys; } int32_t DescriptorScriptPubKeyMan::GetEndRange() const { - return m_max_cached_index + 1; + // For a multipath descriptor, return the lowest cached end over all paths, + // so that fast rescan filters never skip scripts of the path that is behind. + if (m_max_cached_index.empty()) return 0; + return *std::min_element(m_max_cached_index.begin(), m_max_cached_index.end()) + 1; } bool DescriptorScriptPubKeyMan::GetDescriptorString(std::string& out, const bool priv) const @@ -1593,10 +1629,15 @@ bool DescriptorScriptPubKeyMan::GetDescriptorString(std::string& out, const bool // For the private version, always return the master key to avoid // exposing child private keys. The risk implications of exposing child // private keys together with the parent xpub may be non-obvious for users. + if (m_wallet_descriptor.IsMultipath()) return m_wallet_descriptor.multipath->ToPrivateString(provider, out); return m_wallet_descriptor.descriptor->ToPrivateString(provider, out); } - return m_wallet_descriptor.descriptor->ToNormalizedString(provider, out, &m_wallet_descriptor.cache); + // For a multipath descriptor the last hardened xpubs are identical across + // paths (hardened multipath specifiers are rejected), so the first path's + // cache suffices for normalization. + if (m_wallet_descriptor.IsMultipath()) return m_wallet_descriptor.multipath->ToNormalizedString(provider, out, &m_wallet_descriptor.CacheAt(0)); + return m_wallet_descriptor.descriptor->ToNormalizedString(provider, out, &m_wallet_descriptor.CacheAt()); } void DescriptorScriptPubKeyMan::UpgradeDescriptorCache() @@ -1605,9 +1646,11 @@ void DescriptorScriptPubKeyMan::UpgradeDescriptorCache() if (m_storage.IsLocked() || m_storage.IsWalletFlagSet(WALLET_FLAG_LAST_HARDENED_XPUB_CACHED)) { return; } + // Multipath descriptors postdate the last hardened xpub cache upgrade, so they never need it + if (m_wallet_descriptor.IsMultipath()) return; // Skip if we have the last hardened xpub cache - if (m_wallet_descriptor.cache.GetCachedLastHardenedExtPubKeys().size() > 0) { + if (m_wallet_descriptor.CacheAt().GetCachedLastHardenedExtPubKeys().size() > 0) { return; } @@ -1622,7 +1665,7 @@ void DescriptorScriptPubKeyMan::UpgradeDescriptorCache() } // Cache the last hardened xpubs - DescriptorCache diff = m_wallet_descriptor.cache.MergeAndDiff(temp_cache); + DescriptorCache diff = m_wallet_descriptor.CacheAt().MergeAndDiff(temp_cache); if (!WalletBatch(m_storage.GetDatabase()).WriteDescriptorCacheItems(GetID(), diff)) { throw std::runtime_error(std::string(__func__) + ": writing cache items failed"); } @@ -1638,7 +1681,7 @@ util::Result DescriptorScriptPubKeyMan::UpdateWalletDescriptor(WalletDescr m_map_pubkeys.clear(); m_map_script_pub_keys.clear(); - m_max_cached_index = -1; + m_max_cached_index.clear(); m_wallet_descriptor = descriptor; WalletBatch batch(m_storage.GetDatabase()); @@ -1677,13 +1720,15 @@ bool DescriptorScriptPubKeyMan::CanUpdateToWalletDescriptor(const WalletDescript return true; } - if (descriptor.GetStart() > m_wallet_descriptor.GetStart() || - descriptor.GetEnd() < m_wallet_descriptor.GetEnd()) { - // Use inclusive range for error - error = strprintf("new range must include current range = [%d,%d]", - m_wallet_descriptor.GetStart(), - m_wallet_descriptor.GetEnd() - 1); - return false; + for (size_t path = 0; path < m_wallet_descriptor.NumPaths(); ++path) { + if (descriptor.GetStart() > m_wallet_descriptor.GetStart() || + descriptor.GetEnd(path) < m_wallet_descriptor.GetEnd(path)) { + // Use inclusive range for error + error = strprintf("new range must include current range = [%d,%d]", + m_wallet_descriptor.GetStart(), + m_wallet_descriptor.GetEnd(path) - 1); + return false; + } } return true; diff --git a/src/wallet/scriptpubkeyman.h b/src/wallet/scriptpubkeyman.h index 5c977b12d404..c157a64bf40a 100644 --- a/src/wallet/scriptpubkeyman.h +++ b/src/wallet/scriptpubkeyman.h @@ -84,7 +84,7 @@ class ScriptPubKeyMan public: explicit ScriptPubKeyMan(WalletStorage& storage) : m_storage(storage) {} virtual ~ScriptPubKeyMan() = default; - virtual util::Result GetNewDestination(const OutputType type) { return util::Error{Untranslated("Not supported")}; } + virtual util::Result GetNewDestination(const OutputType type, bool internal) { return util::Error{Untranslated("Not supported")}; } virtual bool IsMine(const CScript& script) const { return false; } //! Check that the given decryption key is valid for this ScriptPubKeyMan, i.e. it decrypts all of the keys handled by it. @@ -272,12 +272,23 @@ class LegacySigningProvider : public SigningProvider class DescriptorScriptPubKeyMan : public ScriptPubKeyMan { private: - using ScriptPubKeyMap = std::map; // Map of scripts to descriptor range index - using PubKeyMap = std::map; // Map of pubkeys involved in scripts to descriptor range index + //! Position of a script within a (possibly multipath) wallet descriptor + struct DescriptorPosition { + int32_t index; // Descriptor range index + uint8_t path; // Derivation path of a multipath descriptor; 0 for single path descriptors + }; + using ScriptPubKeyMap = std::map; // Map of scripts to descriptor position + using PubKeyMap = std::map; // Map of pubkeys involved in scripts to descriptor position ScriptPubKeyMap m_map_script_pub_keys GUARDED_BY(cs_desc_man); PubKeyMap m_map_pubkeys GUARDED_BY(cs_desc_man); - int32_t m_max_cached_index = -1; + //! Highest cached descriptor range index, per derivation path + std::vector m_max_cached_index; + + //! Map the wallet's internal (change) flag to the descriptor path index + size_t PathOf(bool internal) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man) { return m_wallet_descriptor.IsMultipath() && internal ? 1 : 0; } + //! Ensure m_max_cached_index has one entry per descriptor path + void InitMaxCachedIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man) { m_max_cached_index.resize(m_wallet_descriptor.NumPaths(), -1); } KeyMap m_map_keys GUARDED_BY(cs_desc_man); CryptedKeyMap m_map_crypted_keys GUARDED_BY(cs_desc_man); @@ -313,11 +324,11 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan KeyMap GetKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man); // Cached FlatSigningProviders to avoid regenerating them each time they are needed. - mutable std::map m_map_signing_providers; + mutable std::map, FlatSigningProvider> m_map_signing_providers; // Fetch the SigningProvider for the given script and optionally include private keys std::unique_ptr GetSigningProvider(const CScript& script, bool include_private = false) const; - // Fetch the SigningProvider for a given index and optionally include private keys. Called by the above functions. - std::unique_ptr GetSigningProvider(int32_t index, bool include_private = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man); + // Fetch the SigningProvider for a given position and optionally include private keys. Called by the above functions. + std::unique_ptr GetSigningProvider(DescriptorPosition pos, bool include_private = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man); void Load(); @@ -337,9 +348,9 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan {} WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man); - void IncIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man); - void DecIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man); - void SetRangeEnd(int32_t end) EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man); + void IncIndex(size_t path) EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man); + void DecIndex(size_t path) EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man); + void SetRangeEnd(size_t path, int32_t end) EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man); //! Same as 'TopUp' but designed for use within a batch transaction context bool TopUpWithDB(WalletBatch& batch, unsigned int size = 0); @@ -352,7 +363,7 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan mutable RecursiveMutex cs_desc_man; - util::Result GetNewDestination(OutputType type) override; + util::Result GetNewDestination(OutputType type, bool internal) override; bool IsMine(const CScript& script) const override; bool CheckDecryptionKey(const CKeyingMaterial& master_key) override; @@ -378,6 +389,11 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan bool HaveCryptedKeys() const override; unsigned int GetKeyPoolSize() const override; + //! Get the keypool size for a single chain of a multipath descriptor + unsigned int GetKeyPoolSize(bool internal) const; + + //! Whether this manages a multipath descriptor, covering both the receive and change chain + bool IsMultipathDescriptor() const; int64_t GetTimeFirstKey() const override; diff --git a/src/wallet/test/fuzz/scriptpubkeyman.cpp b/src/wallet/test/fuzz/scriptpubkeyman.cpp index 32ba797f9576..52910663d359 100644 --- a/src/wallet/test/fuzz/scriptpubkeyman.cpp +++ b/src/wallet/test/fuzz/scriptpubkeyman.cpp @@ -162,7 +162,7 @@ FUZZ_TARGET(scriptpubkeyman, .init = initialize_spkm) if (wallet_desc.descriptor->IsSingleType()) { auto output_type{wallet_desc.descriptor->GetOutputType()}; if (output_type.has_value()) { - auto dest{spk_manager->GetNewDestination(*output_type)}; + auto dest{spk_manager->GetNewDestination(*output_type, fuzzed_data_provider.ConsumeBool())}; if (dest) { assert(IsValidDestination(*dest)); assert(spk_manager->IsHDEnabled()); diff --git a/src/wallet/test/scriptpubkeyman_tests.cpp b/src/wallet/test/scriptpubkeyman_tests.cpp index 74389a549673..3b32c384e1ab 100644 --- a/src/wallet/test/scriptpubkeyman_tests.cpp +++ b/src/wallet/test/scriptpubkeyman_tests.cpp @@ -38,6 +38,62 @@ BOOST_AUTO_TEST_CASE(DescriptorScriptPubKeyManTests) BOOST_CHECK(signprov_keypath_nums_h == nullptr); } +BOOST_AUTO_TEST_CASE(desc_spkm_multipath) +{ + CWallet keystore(m_node.chain.get(), "", CreateMockableWalletDatabase()); + keystore.SetWalletFlag(WALLET_FLAG_DESCRIPTORS); + keystore.SetWalletFlag(WALLET_FLAG_MULTIPATH_DESCRIPTORS); + + CExtKey master_key; + master_key.SetSeed(std::array{}); + const std::string desc_str = "wpkh(" + EncodeExtKey(master_key) + "/84h/<0;1>/*)"; + + FlatSigningProvider keys; + std::string error; + auto parsed_descs = Parse(desc_str, keys, error); + BOOST_REQUIRE_EQUAL(parsed_descs.size(), 2U); + WalletDescriptor w_desc(std::make_shared(std::move(parsed_descs)), /*creation_time=*/1, /*range_start=*/0, /*range_end=*/10, /*next_index=*/0); + + LOCK(keystore.cs_wallet); + DescriptorScriptPubKeyMan& spkm = Assert(keystore.AddWalletDescriptor(w_desc, keys, /*label=*/"", /*internal=*/false))->get(); + BOOST_CHECK(spkm.IsMultipathDescriptor()); + + // Receive and change addresses are handed out from separate chains with independent counters + const CTxDestination receive1 = *Assert(spkm.GetNewDestination(OutputType::BECH32, /*internal=*/false)); + const CTxDestination change1 = *Assert(spkm.GetNewDestination(OutputType::BECH32, /*internal=*/true)); + const CTxDestination receive2 = *Assert(spkm.GetNewDestination(OutputType::BECH32, /*internal=*/false)); + BOOST_CHECK(receive1 != change1); + BOOST_CHECK(receive1 != receive2); + { + LOCK(spkm.cs_desc_man); + BOOST_CHECK_EQUAL(spkm.GetWalletDescriptor().GetNext(/*path=*/0), 2); + BOOST_CHECK_EQUAL(spkm.GetWalletDescriptor().GetNext(/*path=*/1), 1); + } + // The receive chain handed out one address more than the change chain + BOOST_CHECK_EQUAL(spkm.GetKeyPoolSize(/*internal=*/false) + 1, spkm.GetKeyPoolSize(/*internal=*/true)); + BOOST_CHECK_EQUAL(spkm.GetKeyPoolSize(), spkm.GetKeyPoolSize(/*internal=*/false) + spkm.GetKeyPoolSize(/*internal=*/true)); + + // The addresses match the expansions of the corresponding path + FlatSigningProvider expand_keys; + auto expand_descs = Parse(desc_str, expand_keys, error); + const auto expect_script{[&](size_t path, int32_t index, const CTxDestination& dest) { + std::vector spks; + FlatSigningProvider out; + BOOST_REQUIRE(expand_descs.at(path)->Expand(index, expand_keys, spks, out)); + BOOST_CHECK(GetScriptForDestination(dest) == spks.at(0)); + }}; + expect_script(0, 0, receive1); + expect_script(0, 1, receive2); + expect_script(1, 0, change1); + + // Scripts of both chains are recognized and solvable + for (const CTxDestination& dest : {receive1, change1, receive2}) { + const CScript script = GetScriptForDestination(dest); + BOOST_CHECK(spkm.IsMine(script)); + BOOST_CHECK(spkm.GetSolvingProvider(script) != nullptr); + } +} + BOOST_AUTO_TEST_CASE(desc_spkm_topup_fail) { // Attempting to construct a DescriptorSPKM that cannot be topped up (hardened derivation without private keys) diff --git a/src/wallet/test/walletdb_tests.cpp b/src/wallet/test/walletdb_tests.cpp index b3741f09895a..fe33f3a8366b 100644 --- a/src/wallet/test/walletdb_tests.cpp +++ b/src/wallet/test/walletdb_tests.cpp @@ -4,6 +4,8 @@ #include #include +#include