diff --git a/src/rpc/output_script.cpp b/src/rpc/output_script.cpp index cb73fe544909..bb943907cfec 100644 --- a/src/rpc/output_script.cpp +++ b/src/rpc/output_script.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -185,6 +186,11 @@ static RPCMethod getdescriptorinfo() {RPCResult::Type::BOOL, "isrange", "Whether the descriptor is ranged"}, {RPCResult::Type::BOOL, "issolvable", "Whether the descriptor is solvable"}, {RPCResult::Type::BOOL, "hasprivatekeys", "Whether the input descriptor contained at least one private key"}, + {RPCResult::Type::STR, "wallet_policy_template", /*optional=*/true, "BIP-388 wallet policy template with @N key placeholders. Only present if the provided descriptor is BIP-388 policy-compatible."}, + {RPCResult::Type::ARR, "wallet_policy_keys", /*optional=*/true, "BIP-388 key information vector, one \"[fingerprint/path]xpub\" per @N placeholder in order. Only present if the provided descriptor is BIP-388 policy-compatible.", + { + {RPCResult::Type::STR, "", ""}, + }}, } }, RPCExamples{ @@ -194,28 +200,30 @@ static RPCMethod getdescriptorinfo() }, [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue { - FlatSigningProvider provider; - std::string error; - auto descs = Parse(self.Arg("descriptor"), provider, error); - if (descs.empty()) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error); - } + auto info = ParseDescriptorInfo(self.Arg("descriptor")); + if (!info) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, util::ErrorString(info).original); UniValue result(UniValue::VOBJ); - result.pushKV("descriptor", descs.at(0)->ToString()); + result.pushKV("descriptor", info->descriptor); - if (descs.size() > 1) { - UniValue multipath_descs(UniValue::VARR); - for (const auto& d : descs) { - multipath_descs.push_back(d->ToString()); - } - result.pushKV("multipath_expansion", multipath_descs); + if (!info->expansion.empty()) { + UniValue arr(UniValue::VARR); + for (const auto& d : info->expansion) arr.push_back(d); + result.pushKV("multipath_expansion", arr); + } + + result.pushKV("checksum", info->checksum); + result.pushKV("isrange", info->is_range); + result.pushKV("issolvable", info->is_solvable); + result.pushKV("hasprivatekeys", info->has_private_keys); + + if (info->wallet_policy) { + result.pushKV("wallet_policy_template", info->wallet_policy->descriptor_template); + UniValue keys_arr(UniValue::VARR); + for (const auto& k : info->wallet_policy->keys) keys_arr.push_back(k); + result.pushKV("wallet_policy_keys", keys_arr); } - result.pushKV("checksum", GetDescriptorChecksum(request.params[0].get_str())); - result.pushKV("isrange", descs.at(0)->IsRange()); - result.pushKV("issolvable", descs.at(0)->IsSolvable()); - result.pushKV("hasprivatekeys", provider.keys.size() > 0); return result; }, }; diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp index 3b73a40ccefb..295e04585e3c 100644 --- a/src/script/descriptor.cpp +++ b/src/script/descriptor.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -247,6 +248,21 @@ struct PubkeyProvider /** Get the count of keys known by this PubkeyProvider. Usually one, but may be more for key aggregation schemes */ virtual size_t GetKeyCount() const { return 1; } + + /** Return the BIP-388 wallet policy placeholder for this key, or {} on failure. */ + virtual std::string ToTemplateString(size_t& placeholder_idx, size_t& traversal_idx, const MultipathContext& ctx = {}) const + { + const std::string ret{"@" + util::ToString(placeholder_idx)}; + ++placeholder_idx; + ++traversal_idx; + return ret; + } + + /** Append the last non-hardened derivation index for each @N. */ + virtual void GetDerivationIndex(std::vector& out) const {} + + /** Append the BIP32 origin and account-level xpub for each key. */ + virtual void GetKeyOrigins(std::vector>& out) const {} }; class OriginPubkeyProvider final : public PubkeyProvider @@ -317,6 +333,30 @@ class OriginPubkeyProvider final : public PubkeyProvider { return std::make_unique(m_expr_index, m_origin, m_provider->Clone(), m_apostrophe); } + + std::string ToTemplateString(size_t& placeholder_idx, size_t& traversal_idx, const MultipathContext& ctx) const override + { + return m_provider->ToTemplateString(placeholder_idx, traversal_idx, ctx); + } + + void GetDerivationIndex(std::vector& out) const override + { + m_provider->GetDerivationIndex(out); + } + + void GetKeyOrigins(std::vector>& out) const override + { + if (m_provider->IsBIP32()) { + std::optional root = m_provider->GetRootExtPubKey(); + if (root) { + out.emplace_back(m_origin, *root); + return; + } + } + // For musig, delegate so each participant emits its own origin. + m_provider->GetKeyOrigins(out); + } + }; /** An object representing a parsed constant public key in a descriptor. */ @@ -607,6 +647,47 @@ class BIP32PubkeyProvider final : public PubkeyProvider { return std::make_unique(m_expr_index, m_root_extkey, m_path, m_derive, m_apostrophe); } + void GetDerivationIndex(std::vector& out) const override + { + if (!m_path.empty() && m_derive == DeriveType::UNHARDENED_RANGED) { + out.push_back(m_path.back()); + } + } + std::string ToTemplateString(size_t& placeholder_idx, size_t& traversal_idx, const MultipathContext& ctx) const override + { + const size_t t_idx{traversal_idx++}; + + if (!ctx.IsMultipath() || t_idx >= ctx.per_key_mn.size() || !IsRange()) { + const std::string ret{"@" + util::ToString(placeholder_idx)}; + ++placeholder_idx; + return ret; + } + + // BIP 388 requires exactly one unhardened derivation step after the account xpub. + if (m_path.size() != 1 || m_derive != DeriveType::UNHARDENED_RANGED) { + return {}; + } + + const auto [M, N] = ctx.per_key_mn[t_idx]; + const std::string suffix{(M == 0 && N == 1) + ? "/**" + : "/<" + util::ToString(M) + ";" + util::ToString(N) + ">/*"}; + + const std::string xpub_str{EncodeExtPubKey(m_root_extkey)}; + auto it{ctx.key_map.find(xpub_str)}; + if (it != ctx.key_map.end()) { + auto& entry{it->second}; + if (entry.claimed_mn.contains(M) || entry.claimed_mn.contains(N)) return {}; + entry.claimed_mn.insert(M); + entry.claimed_mn.insert(N); + return "@" + util::ToString(entry.placeholder_idx) + suffix; + } + + ctx.key_map[xpub_str] = {placeholder_idx, {M, N}}; + const std::string placeholder{"@" + util::ToString(placeholder_idx)}; + ++placeholder_idx; + return placeholder + suffix; + } }; /** PubkeyProvider for a musig() expression */ @@ -810,6 +891,87 @@ class MuSigPubkeyProvider final : public PubkeyProvider { return 1 + m_participants.size(); } + std::string ToTemplateString(size_t& placeholder_idx, size_t& traversal_idx, const MultipathContext& ctx) const override + { + // BIP-388 disallows derivation before aggregation: + // VALID: tr(musig(@0,@1)/<0;1>/*,...) + // INVALID: tr(musig(@0/<0;1>/*,@1/<0;1>/*),...) + if (m_ranged_participants) return {}; + + const size_t first_t_idx{traversal_idx}; + // We also want to know if multiple musig expressions in a descriptor are duplicate, + // if so are they disjoint? + std::vector sorted_xpubs; + sorted_xpubs.reserve(m_participants.size()); + + std::string ret{"musig("}; + + for (size_t i{0}; i < m_participants.size(); ++i) { + const auto xpub{m_participants[i]->GetRootExtPubKey()}; + if (!xpub) return {}; + + const std::string xpub_str{EncodeExtPubKey(*xpub)}; + sorted_xpubs.push_back(xpub_str); + + auto it{ctx.key_map.find(xpub_str)}; + + if (it != ctx.key_map.end()) { + // We have seen this participant before so reuse existing placeholder. + // Musig participants are arguments to the aggregate, so BIP-388 disjointness does not apply to individual participants. + ret += "@" + util::ToString(it->second.placeholder_idx); + } else { + ctx.key_map[xpub_str] = {placeholder_idx, {}}; + ret += "@" + util::ToString(placeholder_idx); + ++placeholder_idx; + } + ++traversal_idx; + + if (i < m_participants.size() - 1) ret += ','; + } + ret += ')'; + + std::sort(sorted_xpubs.begin(), sorted_xpubs.end()); + const std::string aggregate_id{util::Join(sorted_xpubs, ",")}; + + // BIP 388 requires exactly one unhardened derivation step after the account xpub. + if (ctx.IsMultipath() && m_path.size() == 1 + && m_derive == DeriveType::UNHARDENED_RANGED + && first_t_idx + m_participants.size() <= ctx.per_key_mn.size()) { + const auto [M, N] = ctx.per_key_mn[first_t_idx]; + + // Check if the representation of the aggregate key, same set of participants, order independent exists. + // If so, we enforce disjointness. + auto& claimed = ctx.aggregate_claimed_mn[aggregate_id]; + if (claimed.contains(M) || claimed.contains(N)) return {}; + claimed.insert(M); + claimed.insert(N); + + const std::string suffix{(M == 0 && N == 1) + ? "/**" + : "/<" + util::ToString(M) + ";" + util::ToString(N) + ">/*"}; + ret += suffix; + } else { + ret += FormatHDKeypath(m_path, /*apostrophe=*/false); + if (IsRangedDerivation()) ret += "/*"; + } + + return ret; + } + void GetDerivationIndex(std::vector& out) const override + { + if (IsRangedDerivation() && !m_path.empty() + && m_derive == DeriveType::UNHARDENED_RANGED) { + for (size_t i{0}; i < m_participants.size(); ++i) { + out.push_back(m_path.back()); + } + } + } + void GetKeyOrigins(std::vector>& out) const override + { + for (const auto& p : m_participants) { + p->GetKeyOrigins(out); + } + } }; /** Base class for all Descriptor implementations. */ @@ -976,6 +1138,87 @@ class DescriptorImpl : public Descriptor return ret; } + std::optional BuildWalletPolicyKeys(WalletPolicy&& policy, size_t placeholder_idx, const MultipathContext& ctx) const + { + if (ctx.IsMultipath() && !ctx.key_map.empty()) { + std::vector keys(placeholder_idx); + std::vector> origins; + GetKeyOrigins(origins); + + // resolve duplicates via key_map + for (const auto& [origin, xpub] : origins) { + const std::string xpub_str{EncodeExtPubKey(xpub)}; + auto it{ctx.key_map.find(xpub_str)}; + if (it == ctx.key_map.end()) return std::nullopt; + const size_t N{it->second.placeholder_idx}; + if (keys[N].empty()) { + keys[N] = "[" + HexStr(origin.fingerprint) + FormatHDKeypath(origin.path) + "]" + xpub_str; + } + } + + if (std::ranges::any_of(keys, [](const auto& k) { return k.empty(); })) return std::nullopt; + policy.keys = std::move(keys); + return policy; + } + + std::vector> origins; + GetKeyOrigins(origins); + + if (origins.size() != placeholder_idx) return std::nullopt; + + policy.keys.reserve(origins.size()); + for (const auto& [origin, xpub] : origins) { + policy.keys.emplace_back("[" + HexStr(origin.fingerprint) + FormatHDKeypath(origin.path) + "]" + EncodeExtPubKey(xpub)); + } + return policy; + } + + // NOLINTNEXTLINE(misc-no-recursion) + virtual bool BuildWalletPolicyTemplate(std::string& out, size_t& placeholder_idx, size_t& traversal_idx, const MultipathContext& ctx = {}) const + { + std::string extra{ToStringExtra()}; + size_t pos{extra.empty() ? 0U : 1U}; + std::string ret{m_name + "(" + extra}; + + for (const auto& pubkey : m_pubkey_args) { + if (!pubkey->IsRange()) return false; + if (pos++) ret += ','; + const std::string key_str{pubkey->ToTemplateString(placeholder_idx, traversal_idx, ctx)}; + if (key_str.empty()) return false; + ret += key_str; + } + + for (const auto& sub : m_subdescriptor_args) { + if (pos++) ret += ','; + std::string sub_out; + if (!sub->BuildWalletPolicyTemplate(sub_out, placeholder_idx, traversal_idx, ctx)) return false; + ret += sub_out; + } + + out = std::move(ret) + ")"; + return true; + } + + std::optional BuildWalletPolicy(const MultipathContext& ctx = {}) const override + { + return std::nullopt; + } + + /** Build a complete WalletPolicy from the descriptor template and key origins. + * Returns std::nullopt on failure. + */ + // NOLINTNEXTLINE(misc-no-recursion) + std::optional DoBuildWalletPolicy(const MultipathContext& ctx) const + { + WalletPolicy policy; + size_t placeholder_idx{0}; + size_t traversal_idx{0}; + if (!BuildWalletPolicyTemplate(policy.descriptor_template, placeholder_idx, traversal_idx, ctx)) { + return std::nullopt; + } + return BuildWalletPolicyKeys(std::move(policy), placeholder_idx, ctx); + } + // NOLINTNEXTLINE(misc-no-recursion) bool ExpandHelper(int pos, const SigningProvider& arg, const DescriptorCache* read_cache, std::vector& output_scripts, FlatSigningProvider& out, DescriptorCache* write_cache) const { @@ -1099,6 +1342,24 @@ class DescriptorImpl : public Descriptor } return count; } + + // NOLINTNEXTLINE(misc-no-recursion) + void GetDerivationIndex(std::vector& out) const override + { + for (const auto& p : m_pubkey_args) p->GetDerivationIndex(out); + for (const auto& s : m_subdescriptor_args) s->GetDerivationIndex(out); + } + + // NOLINTNEXTLINE(misc-no-recursion) + void GetKeyOrigins(std::vector>& out) const + { + for (const auto& p : m_pubkey_args) { + p->GetKeyOrigins(out); + } + for (const auto& arg : m_subdescriptor_args) { + arg->GetKeyOrigins(out); + } + } }; /** A parsed addr(A) descriptor. */ @@ -1225,6 +1486,11 @@ class PKHDescriptor final : public DescriptorImpl { return std::make_unique(m_pubkey_args.at(0)->Clone()); } + + std::optional BuildWalletPolicy(const MultipathContext& ctx) const override + { + return DoBuildWalletPolicy(ctx); + } }; /** A parsed wpkh(P) descriptor. */ @@ -1258,6 +1524,11 @@ class WPKHDescriptor final : public DescriptorImpl { return std::make_unique(m_pubkey_args.at(0)->Clone()); } + + std::optional BuildWalletPolicy(const MultipathContext& ctx) const override + { + return DoBuildWalletPolicy(ctx); + } }; /** A parsed combo(P) descriptor. */ @@ -1331,6 +1602,11 @@ class MultisigDescriptor final : public DescriptorImpl std::transform(m_pubkey_args.begin(), m_pubkey_args.end(), std::back_inserter(providers), [](const std::unique_ptr& p) { return p->Clone(); }); return std::make_unique(m_threshold, std::move(providers), m_sorted); } + + std::optional BuildWalletPolicy(const MultipathContext& ctx) const override + { + return DoBuildWalletPolicy(ctx); + } }; /** A parsed (sorted)multi_a(...) descriptor. Always uses x-only pubkeys. */ @@ -1377,6 +1653,11 @@ class MultiADescriptor final : public DescriptorImpl } return std::make_unique(m_threshold, std::move(providers), m_sorted); } + + std::optional BuildWalletPolicy(const MultipathContext& ctx) const override + { + return DoBuildWalletPolicy(ctx); + } }; /** A parsed sh(...) descriptor. */ @@ -1427,6 +1708,11 @@ class SHDescriptor final : public DescriptorImpl { return std::make_unique(m_subdescriptor_args.at(0)->Clone()); } + + std::optional BuildWalletPolicy(const MultipathContext& ctx) const override + { + return DoBuildWalletPolicy(ctx); + } }; /** A parsed wsh(...) descriptor. */ @@ -1468,6 +1754,11 @@ class WSHDescriptor final : public DescriptorImpl { return std::make_unique(m_subdescriptor_args.at(0)->Clone()); } + + std::optional BuildWalletPolicy(const MultipathContext& ctx) const override + { + return DoBuildWalletPolicy(ctx); + } }; /** A parsed tr(...) descriptor. */ @@ -1554,6 +1845,49 @@ class TRDescriptor final : public DescriptorImpl std::transform(m_subdescriptor_args.begin(), m_subdescriptor_args.end(), std::back_inserter(subdescs), [](const std::unique_ptr& d) { return d->Clone(); }); return std::make_unique(m_pubkey_args.at(0)->Clone(), std::move(subdescs), m_depths); } + + // NOLINTNEXTLINE(misc-no-recursion) + std::optional BuildWalletPolicy(const MultipathContext& ctx) const override + { + // BIP-388 requires key expressions to include a derivation path + // suffix (/** or //*). Non-ranged keys have no such suffix. + if (!m_pubkey_args.at(0)->IsRange()) return std::nullopt; + + WalletPolicy policy; + size_t placeholder_idx{0}; + size_t traversal_idx{0}; + + const std::string internal_key{m_pubkey_args.at(0)->ToTemplateString(placeholder_idx, traversal_idx, ctx)}; + if (internal_key.empty()) return std::nullopt; + std::string ret{"tr(" + internal_key}; + + if (!m_subdescriptor_args.empty()) { + std::string scripts; + std::vector path; + for (size_t i{0}; i < m_depths.size(); ++i) { + if (i > 0) scripts += ','; + while (static_cast(path.size()) <= m_depths[i]) { + if (path.size()) scripts += '{'; + path.push_back(false); + } + std::string sub_out; + if (!m_subdescriptor_args[i]->BuildWalletPolicyTemplate(sub_out, placeholder_idx, traversal_idx, ctx)) { + return std::nullopt; + } + scripts += sub_out; + while (!path.empty() && path.back()) { + if (path.size() > 1) scripts += '}'; + path.pop_back(); + } + if (!path.empty()) path.back() = true; + } + ret += ',' + scripts; + } + ret += ')'; + policy.descriptor_template = std::move(ret); + + return BuildWalletPolicyKeys(std::move(policy), placeholder_idx, ctx); + } }; /* We instantiate Miniscript here with a simple integer as key type. @@ -1719,6 +2053,47 @@ class MiniscriptDescriptor final : public DescriptorImpl } return std::make_unique(std::move(providers), m_node.Clone()); } + + // NOLINTNEXTLINE(misc-no-recursion) + bool BuildWalletPolicyTemplate(std::string& out, size_t& placeholder_idx, size_t& traversal_idx, const MultipathContext& ctx = {}) const override + { + // Mirrors StringMaker's interface for m_node.ToString + struct TemplateStringifier { + const std::vector>& pubkeys; + size_t& placeholder_idx; + size_t& traversal_idx; + const MultipathContext& ctx; + mutable std::map cache; + + std::optional ToString(uint32_t key, [[maybe_unused]] bool& has_priv_key) const + { + if (key >= pubkeys.size()) return std::nullopt; + // toString is called twice when WRAP_C contains pk(k) and pkh(k). + // So we end up having more keys in ctx.key_map than in the descriptor. + // since the double call reuses the same key index, we cache the result and + // return it on repeat calls to avoid double-incrementing + // traversal_idx and triggering the claimed_mn disjointness check. + auto it{cache.find(key)}; + if (it != cache.end()) return it->second; + std::string ret{pubkeys[key]->ToTemplateString( + placeholder_idx, traversal_idx, ctx)}; + if (ret.empty()) return std::nullopt; + cache[key] = ret; + return ret; + } + }; + + bool has_priv_key{false}; + auto res = m_node.ToString(TemplateStringifier{m_pubkey_args, placeholder_idx, traversal_idx, ctx, {}}, has_priv_key); + if (!res) return false; + out = *res; + return true; + } + + std::optional BuildWalletPolicy(const MultipathContext& ctx) const override + { + return DoBuildWalletPolicy(ctx); + } }; /** A parsed rawtr(...) descriptor. */ @@ -2950,6 +3325,58 @@ std::string GetDescriptorChecksum(const std::string& descriptor) return ret; } +util::Result ParseDescriptorInfo(std::string_view descriptor) +{ + FlatSigningProvider provider; + std::string error; + auto descs = Parse(descriptor, provider, error); + if (descs.empty()) return util::Error{Untranslated(error)}; + + DescriptorInfo info; + info.descriptor = descs.at(0)->ToString(); + + if (descs.size() > 1) { + info.expansion.reserve(descs.size()); + for (const auto& d : descs) { + info.expansion.push_back(d->ToString()); + } + } + + info.checksum = GetDescriptorChecksum(std::string{descriptor}); + info.is_range = descs.at(0)->IsRange(); + info.is_solvable = descs.at(0)->IsSolvable(); + info.has_private_keys = !provider.keys.empty(); + + if (descs.size() == 2) { + const auto* desc_m{static_cast(descs[0].get())}; + const auto* desc_n{static_cast(descs[1].get())}; + Assert(desc_m && desc_n); + + std::vector m_idx, n_idx; + desc_m->GetDerivationIndex(m_idx); + desc_n->GetDerivationIndex(n_idx); + + if (m_idx.size() == n_idx.size() && !m_idx.empty()) { + MultipathContext ctx; + bool valid{true}; + + for (size_t i{0}; i < m_idx.size(); ++i) { + const uint32_t M{m_idx[i]}, N{n_idx[i]}; + if (M == N) { + valid = false; + break; + } + ctx.per_key_mn.emplace_back(M, N); + } + if (valid) { + info.wallet_policy = desc_m->BuildWalletPolicy(ctx); + } + } + } + + return info; +} + std::unique_ptr InferDescriptor(const CScript& script, const SigningProvider& provider) { return InferScript(script, ParseScriptContext::TOP, provider); diff --git a/src/script/descriptor.h b/src/script/descriptor.h index 25dafca3a3be..66a131606d51 100644 --- a/src/script/descriptor.h +++ b/src/script/descriptor.h @@ -8,15 +8,19 @@ #include #include #include +#include #include #include +#include #include #include #include #include #include #include +#include +#include #include class CScript; @@ -88,6 +92,32 @@ class DescriptorCache { DescriptorCache MergeAndDiff(const DescriptorCache& other); }; +struct MultipathContext { + struct KeyEntry { + // @N placeholder for this particular key. + size_t placeholder_idx; + // Receive and change indexes (path expressions) for this key. + // Per BIP 388 all identical placeholders must have disjoint path expressions. + // e.g. If two KEY are KP//* and KP//* for the same key placeholder KP, then the sets {M, N} and {P, Q} must be disjoint. + std::unordered_set claimed_mn; + }; + + std::vector> per_key_mn; + // Mutable because ToTemplateString is called through const providers but must write to context. + // aggregate_claimed_mn: Receive and change indexes (path expressions) for this key. + mutable std::map> aggregate_claimed_mn; + mutable std::map key_map; + bool IsMultipath() const { return !per_key_mn.empty(); } +}; + + +/** BIP-388 wallet policy: template string and key information vector. */ +struct WalletPolicy { + /** Policy template with @N key placeholders. */ + std::string descriptor_template; + std::vector keys; +}; + /** \brief Interface for parsed descriptor objects. * * Descriptors are strings that describe a set of scriptPubKeys, together with @@ -200,6 +230,9 @@ struct Descriptor { /** Get the number of key expressions in this descriptor. Used only for tests */ virtual size_t GetKeyCount() const = 0; + + virtual void GetDerivationIndex(std::vector& out) const = 0; + virtual std::optional BuildWalletPolicy(const MultipathContext& ctx = {}) const = 0; }; /** Parse a `descriptor` string. Included private keys are put in `out`. @@ -220,6 +253,33 @@ std::vector> Parse(std::string_view descriptor, Flat */ std::string GetDescriptorChecksum(const std::string& descriptor); +/** Structured result from ParseDescriptorInfo. + */ +struct DescriptorInfo { + /** Descriptor string without private keys.*/ + std::string descriptor; + + /* Empty if descriptor is not multipath */ + std::vector expansion; + + std::string checksum; + + bool is_range{false}; + + bool is_solvable{false}; + + bool has_private_keys{false}; + + /** nullopt if the descriptor is not BIP-388 policy-compatible. */ + std::optional wallet_policy; +}; + +/** Parse a descriptor string into a DescriptorInfo Object + * + * @param[in] descriptor Descriptor string, with or without checksum + */ +util::Result ParseDescriptorInfo(std::string_view descriptor); + /** Find a descriptor for the specified `script`, using information from `provider` where possible. * * A non-ranged descriptor which only generates the specified script will be returned in all diff --git a/src/test/descriptor_tests.cpp b/src/test/descriptor_tests.cpp index 5e4039ad1e1c..6cace980c23d 100644 --- a/src/test/descriptor_tests.cpp +++ b/src/test/descriptor_tests.cpp @@ -1396,4 +1396,190 @@ BOOST_AUTO_TEST_CASE(unused_descriptor_test) CheckUnused("unused(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc/0h/0h/1)", "unused(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/0h/0h/1)"); } +static std::optional TestBuildWalletPolicy(const std::string& descriptor) +{ + FlatSigningProvider provider; + std::string error; + auto descs = Parse(descriptor, provider, error); + if (descs.size() != 2) return std::nullopt; + + std::vector m_idx, n_idx; + descs[0]->GetDerivationIndex(m_idx); + descs[1]->GetDerivationIndex(n_idx); + + if (m_idx.size() != n_idx.size() || m_idx.empty()) return std::nullopt; + + MultipathContext ctx; + for (size_t i{0}; i < m_idx.size(); ++i) { + if (m_idx[i] == n_idx[i]) return std::nullopt; + ctx.per_key_mn.emplace_back(m_idx[i], n_idx[i]); + } + + return descs[0]->BuildWalletPolicy(ctx); +} + +static void CheckWalletPolicy(const std::string& descriptor, + const std::string& expected_template, + const std::vector& expected_keys) +{ + auto policy = TestBuildWalletPolicy(descriptor); + BOOST_REQUIRE_MESSAGE(policy.has_value(), "Expected wallet_policy for: " + descriptor); + BOOST_CHECK_EQUAL(policy->descriptor_template, expected_template); + BOOST_REQUIRE_EQUAL(policy->keys.size(), expected_keys.size()); + for (size_t i{0}; i < expected_keys.size(); ++i) { + BOOST_CHECK_EQUAL(policy->keys.at(i), expected_keys.at(i)); + } +} + +static void CheckNoWalletPolicy(const std::string& descriptor) +{ + BOOST_CHECK_MESSAGE(!TestBuildWalletPolicy(descriptor).has_value(), + "Unexpected wallet_policy for: " + descriptor); +} + +BOOST_AUTO_TEST_CASE(descriptor_wallet_policy) +{ + // Some happypaths for BIP 388 + CheckWalletPolicy( + "pkh([6738736c/44h/0h/0h]xpub6Br37sWxruYfT8ASpCjVHKGwgdnYFEn98DwiN76i2oyY6fgH1LAPmmDcF46xjxJr22gw4jmVjTE2E3URMnRPEPYyo1zoPSUba563ESMXCeb/<0;1>/*)", + "pkh(@0/**)", + {"[6738736c/44h/0h/0h]xpub6Br37sWxruYfT8ASpCjVHKGwgdnYFEn98DwiN76i2oyY6fgH1LAPmmDcF46xjxJr22gw4jmVjTE2E3URMnRPEPYyo1zoPSUba563ESMXCeb"}); + + CheckWalletPolicy( + "wpkh([6738736c/84h/0h/2h]xpub6CRQzb8u9dmMcq5XAwwRn9gcoYCjndJkhKgD11WKzbVGd932UmrExWFxCAvRnDN3ez6ZujLmMvmLBaSWdfWVn75L83Qxu1qSX4fJNrJg2Gt/<0;1>/*)", + "wpkh(@0/**)", + {"[6738736c/84h/0h/2h]xpub6CRQzb8u9dmMcq5XAwwRn9gcoYCjndJkhKgD11WKzbVGd932UmrExWFxCAvRnDN3ez6ZujLmMvmLBaSWdfWVn75L83Qxu1qSX4fJNrJg2Gt"}); + + CheckWalletPolicy( + "sh(wpkh([6738736c/49h/0h/1h]xpub6Bex1CHWGXNNwGVKHLqNC7kcV348FxkCxpZXyCWp1k27kin8sRPayjZUKDjyQeZzGUdyeAj2emoW5zStFFUAHRgd5w8iVVbLgZ7PmjAKAm9/<0;1>/*))", + "sh(wpkh(@0/**))", + {"[6738736c/49h/0h/1h]xpub6Bex1CHWGXNNwGVKHLqNC7kcV348FxkCxpZXyCWp1k27kin8sRPayjZUKDjyQeZzGUdyeAj2emoW5zStFFUAHRgd5w8iVVbLgZ7PmjAKAm9"}); + + CheckWalletPolicy( + "wsh(sortedmulti(2,[6738736c/48h/0h/0h/2h]xpub6FC1fXFP1GXLX5TKtcjHGT4q89SDRehkQLtbKJ2PzWcvbBHtyDsJPLtpLtkGqYNYZdVVAjRQ5kug9CsapegmmeRutpP7PW4u4wVF9JfkDhw/<0;1>/*,[b2b1f0cf/48h/0h/0h/2h]xpub6EWhjpPa6FqrcaPBuGBZRJVjzGJ1ZsMygRF26RwN932Vfkn1gyCiTbECVitBjRCkexEvetLdiqzTcYimmzYxyR1BZ79KNevgt61PDcukmC7/<0;1>/*))", + "wsh(sortedmulti(2,@0/**,@1/**))", + {"[6738736c/48h/0h/0h/2h]xpub6FC1fXFP1GXLX5TKtcjHGT4q89SDRehkQLtbKJ2PzWcvbBHtyDsJPLtpLtkGqYNYZdVVAjRQ5kug9CsapegmmeRutpP7PW4u4wVF9JfkDhw", + "[b2b1f0cf/48h/0h/0h/2h]xpub6EWhjpPa6FqrcaPBuGBZRJVjzGJ1ZsMygRF26RwN932Vfkn1gyCiTbECVitBjRCkexEvetLdiqzTcYimmzYxyR1BZ79KNevgt61PDcukmC7"}); + + CheckWalletPolicy( + "wsh(multi(2,[6738736c/48h/0h/0h/2h]xpub6FC1fXFP1GXLX5TKtcjHGT4q89SDRehkQLtbKJ2PzWcvbBHtyDsJPLtpLtkGqYNYZdVVAjRQ5kug9CsapegmmeRutpP7PW4u4wVF9JfkDhw/<0;1>/*,[b2b1f0cf/48h/0h/0h/2h]xpub6EWhjpPa6FqrcaPBuGBZRJVjzGJ1ZsMygRF26RwN932Vfkn1gyCiTbECVitBjRCkexEvetLdiqzTcYimmzYxyR1BZ79KNevgt61PDcukmC7/<0;1>/*))", + "wsh(multi(2,@0/**,@1/**))", + {"[6738736c/48h/0h/0h/2h]xpub6FC1fXFP1GXLX5TKtcjHGT4q89SDRehkQLtbKJ2PzWcvbBHtyDsJPLtpLtkGqYNYZdVVAjRQ5kug9CsapegmmeRutpP7PW4u4wVF9JfkDhw", + "[b2b1f0cf/48h/0h/0h/2h]xpub6EWhjpPa6FqrcaPBuGBZRJVjzGJ1ZsMygRF26RwN932Vfkn1gyCiTbECVitBjRCkexEvetLdiqzTcYimmzYxyR1BZ79KNevgt61PDcukmC7"}); + + CheckWalletPolicy( + "tr([6738736c/86h/0h/0h]xpub6CryUDWPS28eR2cDyojB8G354izmx294BdjeSvH469Ty3o2E6Tq5VjBJCn8rWBgesvTJnyXNAJ3QpLFGuNwqFXNt3gn612raffLWfdHNkYL/<0;1>/*)", + "tr(@0/**)", + {"[6738736c/86h/0h/0h]xpub6CryUDWPS28eR2cDyojB8G354izmx294BdjeSvH469Ty3o2E6Tq5VjBJCn8rWBgesvTJnyXNAJ3QpLFGuNwqFXNt3gn612raffLWfdHNkYL"}); + + CheckWalletPolicy( + "tr(musig([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa,[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js)/<0;1>/*)", + "tr(musig(@0,@1)/**)", + {"[6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa", + "[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js"}); + + CheckWalletPolicy("tr([6738736c/86h/0h/0h]xpub6CryUDWPS28eR2cDyojB8G354izmx294BdjeSvH469Ty3o2E6Tq5VjBJCn8rWBgesvTJnyXNAJ3QpLFGuNwqFXNt3gn612raffLWfdHNkYL/<0;1>/*,{sortedmulti_a(1,[6738736c/86h/0h/0h]xpub6CryUDWPS28eR2cDyojB8G354izmx294BdjeSvH469Ty3o2E6Tq5VjBJCn8rWBgesvTJnyXNAJ3QpLFGuNwqFXNt3gn612raffLWfdHNkYL/<2;3>/*,[5a61ff8e/86h/1h/0h]xpub6DtZpc9PRL2B6pwoNGysmHAaBofDmWv5S6KQEKKGPKhf5fV62ywDtSziSApYVK3JnYY5KUSgiCwiXW5wtd8z7LNBxT9Mu5sEro8itdGfTeA/<0;1>/*),pk([5a61ff8e/86h/1h/0h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js/<0;1>/*)})", + "tr(@0/**,{sortedmulti_a(1,@0/<2;3>/*,@1/**),pk(@2/**)})", + {"[6738736c/86h/0h/0h]xpub6CryUDWPS28eR2cDyojB8G354izmx294BdjeSvH469Ty3o2E6Tq5VjBJCn8rWBgesvTJnyXNAJ3QpLFGuNwqFXNt3gn612raffLWfdHNkYL", "[5a61ff8e/86h/1h/0h]xpub6DtZpc9PRL2B6pwoNGysmHAaBofDmWv5S6KQEKKGPKhf5fV62ywDtSziSApYVK3JnYY5KUSgiCwiXW5wtd8z7LNBxT9Mu5sEro8itdGfTeA", "[5a61ff8e/86h/1h/0h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js"}); + + CheckWalletPolicy( + "tr([6738736c/86h/0h/0h]xpub6CryUDWPS28eR2cDyojB8G354izmx294BdjeSvH469Ty3o2E6Tq5VjBJCn8rWBgesvTJnyXNAJ3QpLFGuNwqFXNt3gn612raffLWfdHNkYL/<0;1>/*,{pk(musig([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa,[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js)/<0;1>/*),pk(musig([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa,[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js)/<2;3>/*)})", + "tr(@0/**,{pk(musig(@1,@2)/**),pk(musig(@1,@2)/<2;3>/*)})", + {"[6738736c/86h/0h/0h]xpub6CryUDWPS28eR2cDyojB8G354izmx294BdjeSvH469Ty3o2E6Tq5VjBJCn8rWBgesvTJnyXNAJ3QpLFGuNwqFXNt3gn612raffLWfdHNkYL", "[6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa", "[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js"}); + + CheckWalletPolicy( + "tr([6738736c/86h/0h/0h]xpub6CryUDWPS28eR2cDyojB8G354izmx294BdjeSvH469Ty3o2E6Tq5VjBJCn8rWBgesvTJnyXNAJ3QpLFGuNwqFXNt3gn612raffLWfdHNkYL/<0;1>/*,{pk(musig([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa,[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js)/<0;1>/*),pk(musig([b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js,[6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa)/<2;3>/*)})", + "tr(@0/**,{pk(musig(@1,@2)/**),pk(musig(@2,@1)/<2;3>/*)})", + {"[6738736c/86h/0h/0h]xpub6CryUDWPS28eR2cDyojB8G354izmx294BdjeSvH469Ty3o2E6Tq5VjBJCn8rWBgesvTJnyXNAJ3QpLFGuNwqFXNt3gn612raffLWfdHNkYL", "[6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa", "[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js"}); + + // miniscript + CheckWalletPolicy( + "wsh(and_v(v:pk([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa/<0;1>/*),or_d(pk([b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js/<0;1>/*),older(12960))))", + "wsh(and_v(v:pk(@0/**),or_d(pk(@1/**),older(12960))))", + {"[6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa", + "[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js"}); + + CheckWalletPolicy( + "wsh(thresh(3,pk([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa/<0;1>/*),s:pk([b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js/<0;1>/*),s:pk([a666a867/44h/0h/0h/100h]xpub6Dgsze3ujLi1EiHoCtHFMS9VLS1UheVqxrHGfP7sBJ2DBfChEUHV4MDwmxAXR2ayeytpwm3zJEU3H3pjCR6q6U5sP2p2qzAD71x9z5QShK2/<0;1>/*),sln:older(12960)))", + "wsh(thresh(3,pk(@0/**),s:pk(@1/**),s:pk(@2/**),sln:older(12960)))", + {"[6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa", + "[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js", + "[a666a867/44h/0h/0h/100h]xpub6Dgsze3ujLi1EiHoCtHFMS9VLS1UheVqxrHGfP7sBJ2DBfChEUHV4MDwmxAXR2ayeytpwm3zJEU3H3pjCR6q6U5sP2p2qzAD71x9z5QShK2"}); + + CheckWalletPolicy( + "wsh(or_d(pk([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa/<0;1>/*),and_v(v:multi(2,[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js/<0;1>/*,[a666a867/44h/0h/0h/100h]xpub6Dgsze3ujLi1EiHoCtHFMS9VLS1UheVqxrHGfP7sBJ2DBfChEUHV4MDwmxAXR2ayeytpwm3zJEU3H3pjCR6q6U5sP2p2qzAD71x9z5QShK2/<0;1>/*,[bb641298/44h/0h/0h/100h]xpub6Dz8PHFmXkYkykQ83ySkruky567XtJb9N69uXScJZqweYiQn6FyieajdiyjCvWzRZ2GoLHMRE1cwDfuJZ6461YvNRGVBJNnLA35cZrQKSRJ/<0;1>/*),older(65535))))", + "wsh(or_d(pk(@0/**),and_v(v:multi(2,@1/**,@2/**,@3/**),older(65535))))", + {"[6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa", + "[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js", + "[a666a867/44h/0h/0h/100h]xpub6Dgsze3ujLi1EiHoCtHFMS9VLS1UheVqxrHGfP7sBJ2DBfChEUHV4MDwmxAXR2ayeytpwm3zJEU3H3pjCR6q6U5sP2p2qzAD71x9z5QShK2", + "[bb641298/44h/0h/0h/100h]xpub6Dz8PHFmXkYkykQ83ySkruky567XtJb9N69uXScJZqweYiQn6FyieajdiyjCvWzRZ2GoLHMRE1cwDfuJZ6461YvNRGVBJNnLA35cZrQKSRJ"}); + + CheckWalletPolicy( + "wsh(and_v(v:pk([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa/<0;1>/*),pk([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa/<2;3>/*)))", + "wsh(and_v(v:pk(@0/**),pk(@0/<2;3>/*)))", + {"[6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa"}); + + CheckWalletPolicy( + "wsh(or_d(pk([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa/<0;1>/*),and_v(v:multi(2,[6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa/<2;3>/*,[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js/<0;1>/*),older(65535))))", + "wsh(or_d(pk(@0/**),and_v(v:multi(2,@0/<2;3>/*,@1/**),older(65535))))", + {"[6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa", + "[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js"}); + + CheckWalletPolicy( + "wsh(and_v(v:pk([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa/<0;1>/*),sha256(6c60f404f8167a38fc70eaf8aa17ac351023bef86bcb9d1086a19afe95bd5333)))", + "wsh(and_v(v:pk(@0/**),sha256(6c60f404f8167a38fc70eaf8aa17ac351023bef86bcb9d1086a19afe95bd5333)))", + {"[6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa"}); + + CheckWalletPolicy( + "wsh(c:pk_k([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa/<0;1>/*))", + "wsh(pk(@0/**))", + {"[6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa"}); + + CheckWalletPolicy( + "tr([6738736c/86h/0h/0h]xpub6CryUDWPS28eR2cDyojB8G354izmx294BdjeSvH469Ty3o2E6Tq5VjBJCn8rWBgesvTJnyXNAJ3QpLFGuNwqFXNt3gn612raffLWfdHNkYL/<0;1>/*,and_v(v:pk([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa/<0;1>/*),older(12960)))", + "tr(@0/**,and_v(v:pk(@1/**),older(12960)))", + {"[6738736c/86h/0h/0h]xpub6CryUDWPS28eR2cDyojB8G354izmx294BdjeSvH469Ty3o2E6Tq5VjBJCn8rWBgesvTJnyXNAJ3QpLFGuNwqFXNt3gn612raffLWfdHNkYL", + "[6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa"}); + + CheckWalletPolicy( + "tr([6738736c/86h/0h/0h]xpub6CryUDWPS28eR2cDyojB8G354izmx294BdjeSvH469Ty3o2E6Tq5VjBJCn8rWBgesvTJnyXNAJ3QpLFGuNwqFXNt3gn612raffLWfdHNkYL/<0;1>/*,and_v(v:pk(musig([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa,[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js)/<0;1>/*),older(12960)))", + "tr(@0/**,and_v(v:pk(musig(@1,@2)/**),older(12960)))", + {"[6738736c/86h/0h/0h]xpub6CryUDWPS28eR2cDyojB8G354izmx294BdjeSvH469Ty3o2E6Tq5VjBJCn8rWBgesvTJnyXNAJ3QpLFGuNwqFXNt3gn612raffLWfdHNkYL", + "[6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa", + "[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js"}); + + CheckWalletPolicy( + "tr(musig([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa,[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js,[a666a867/44h/0h/0h/100h]xpub6Dgsze3ujLi1EiHoCtHFMS9VLS1UheVqxrHGfP7sBJ2DBfChEUHV4MDwmxAXR2ayeytpwm3zJEU3H3pjCR6q6U5sP2p2qzAD71x9z5QShK2)/<0;1>/*,{and_v(v:pk(musig([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa,[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js)/<0;1>/*),older(12960)),{and_v(v:pk(musig([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa,[a666a867/44h/0h/0h/100h]xpub6Dgsze3ujLi1EiHoCtHFMS9VLS1UheVqxrHGfP7sBJ2DBfChEUHV4MDwmxAXR2ayeytpwm3zJEU3H3pjCR6q6U5sP2p2qzAD71x9z5QShK2)/<0;1>/*),older(12960)),and_v(v:pk(musig([b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js,[a666a867/44h/0h/0h/100h]xpub6Dgsze3ujLi1EiHoCtHFMS9VLS1UheVqxrHGfP7sBJ2DBfChEUHV4MDwmxAXR2ayeytpwm3zJEU3H3pjCR6q6U5sP2p2qzAD71x9z5QShK2)/<0;1>/*),older(12960))}})", + "tr(musig(@0,@1,@2)/**,{and_v(v:pk(musig(@0,@1)/**),older(12960)),{and_v(v:pk(musig(@0,@2)/**),older(12960)),and_v(v:pk(musig(@1,@2)/**),older(12960))}})", + {"[6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa", + "[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js", + "[a666a867/44h/0h/0h/100h]xpub6Dgsze3ujLi1EiHoCtHFMS9VLS1UheVqxrHGfP7sBJ2DBfChEUHV4MDwmxAXR2ayeytpwm3zJEU3H3pjCR6q6U5sP2p2qzAD71x9z5QShK2"}); + + // Non supported script types (BIP 388) + CheckNoWalletPolicy("wpkh([deadbeef/1/2h/3/4h]03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd)"); + CheckNoWalletPolicy("wpkh([6738736c/84h/0h/2h]xpub6CRQzb8u9dmMcq5XAwwRn9gcoYCjndJkhKgD11WKzbVGd932UmrExWFxCAvRnDN3ez6ZujLmMvmLBaSWdfWVn75L83Qxu1qSX4fJNrJg2Gt/0/*)"); + CheckNoWalletPolicy("combo([6738736c/84h/0h/2h]xpub6CRQzb8u9dmMcq5XAwwRn9gcoYCjndJkhKgD11WKzbVGd932UmrExWFxCAvRnDN3ez6ZujLmMvmLBaSWdfWVn75L83Qxu1qSX4fJNrJg2Gt/<0;1>/*)"); + CheckNoWalletPolicy("raw(76a9149a1c78a507689f6f54b847ad1cef1e614ee23f1e88ac)"); + CheckNoWalletPolicy("wsh(multi(2,[6738736c/48h/0h/0h/2h]xpub6FC1fXFP1GXLX5TKtcjHGT4q89SDRehkQLtbKJ2PzWcvbBHtyDsJPLtpLtkGqYNYZdVVAjRQ5kug9CsapegmmeRutpP7PW4u4wVF9JfkDhw/<0;1>/*,[6738736c/48h/0h/0h/2h]xpub6FC1fXFP1GXLX5TKtcjHGT4q89SDRehkQLtbKJ2PzWcvbBHtyDsJPLtpLtkGqYNYZdVVAjRQ5kug9CsapegmmeRutpP7PW4u4wVF9JfkDhw/<1;2>/*))"); + CheckNoWalletPolicy("pkh([6738736c/44h/0h/0h]xpub6Br37sWxruYfT8ASpCjVHKGwgdnYFEn98DwiN76i2oyY6fgH1LAPmmDcF46xjxJr22gw4jmVjTE2E3URMnRPEPYyo1zoPSUba563ESMXCeb)"); + // Extra unhardened path before receive/change index + CheckNoWalletPolicy("pkh([6738736c/44h/0h/0h]xpub6Br37sWxruYfT8ASpCjVHKGwgdnYFEn98DwiN76i2oyY6fgH1LAPmmDcF46xjxJr22gw4jmVjTE2E3URMnRPEPYyo1zoPSUba563ESMXCeb/0/<0;1>/*)"); + // repeated key with same path expression + CheckNoWalletPolicy("sh(multi(1,[6738736c/48h/0h/0h/2h]xpub6FC1fXFP1GXLX5TKtcjHGT4q89SDRehkQLtbKJ2PzWcvbBHtyDsJPLtpLtkGqYNYZdVVAjRQ5kug9CsapegmmeRutpP7PW4u4wVF9JfkDhw/<0;1>/*,[6738736c/48h/0h/0h/2h]xpub6FC1fXFP1GXLX5TKtcjHGT4q89SDRehkQLtbKJ2PzWcvbBHtyDsJPLtpLtkGqYNYZdVVAjRQ5kug9CsapegmmeRutpP7PW4u4wVF9JfkDhw/<0;1>/*))"); + // Non-disjoint multipath + CheckNoWalletPolicy("wsh(multi(2,[6738736c/48h/0h/0h/2h]xpub6FC1fXFP1GXLX5TKtcjHGT4q89SDRehkQLtbKJ2PzWcvbBHtyDsJPLtpLtkGqYNYZdVVAjRQ5kug9CsapegmmeRutpP7PW4u4wVF9JfkDhw/<0;1>/*,[6738736c/48h/0h/0h/2h]xpub6FC1fXFP1GXLX5TKtcjHGT4q89SDRehkQLtbKJ2PzWcvbBHtyDsJPLtpLtkGqYNYZdVVAjRQ5kug9CsapegmmeRutpP7PW4u4wVF9JfkDhw/<1;2>/*))"); + // Key without origin info + CheckNoWalletPolicy("sh(multi(1,[6738736c/48h/0h/0h/2h]xpub6FC1fXFP1GXLX5TKtcjHGT4q89SDRehkQLtbKJ2PzWcvbBHtyDsJPLtpLtkGqYNYZdVVAjRQ5kug9CsapegmmeRutpP7PW4u4wVF9JfkDhw/<0;1>/*,xpub6EWhjpPa6FqrcaPBuGBZRJVjzGJ1ZsMygRF26RwN932Vfkn1gyCiTbECVitBjRCkexEvetLdiqzTcYimmzYxyR1BZ79KNevgt61PDcukmC7/<0;1>/*))"); + // Multipath cardinality > 2 + CheckNoWalletPolicy("pkh([6738736c/44h/0h/0h]xpub6Br37sWxruYfT8ASpCjVHKGwgdnYFEn98DwiN76i2oyY6fgH1LAPmmDcF46xjxJr22gw4jmVjTE2E3URMnRPEPYyo1zoPSUba563ESMXCeb/<0;1;2>/*)"); + // Derivation before aggregation in musig + CheckNoWalletPolicy("tr(musig([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa/<0;1>/*,[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js/<0;1>/*))"); + // same aggregate, non-disjoint multipath + CheckNoWalletPolicy("tr([6738736c/86h/0h/0h]xpub6CryUDWPS28eR2cDyojB8G354izmx294BdjeSvH469Ty3o2E6Tq5VjBJCn8rWBgesvTJnyXNAJ3QpLFGuNwqFXNt3gn612raffLWfdHNkYL/<0;1>/*,{pk(musig([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa,[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js)/<0;1>/*),pk(musig([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa,[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js)/<0;1>/*)})"); + // same aggregate, non-disjoint multipath, reordered participants + CheckNoWalletPolicy("tr([6738736c/86h/0h/0h]xpub6CryUDWPS28eR2cDyojB8G354izmx294BdjeSvH469Ty3o2E6Tq5VjBJCn8rWBgesvTJnyXNAJ3QpLFGuNwqFXNt3gn612raffLWfdHNkYL/<0;1>/*,{pk(musig([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa,[b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js)/<0;1>/*),pk(musig([b2b1f0cf/44h/0h/0h/100h]xpub6EYajCJHe2CK53RLVXrN14uWoEttZgrRSaRztujsXg7yRhGtHmLBt9ot9Pd5ugfwWEu6eWyJYKSshyvZFKDXiNbBcoK42KRZbxwjRQpm5Js,[6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa)/<1;2>/*)})"); + // Repeated key in miniscript with NON-disjoint paths + CheckNoWalletPolicy("wsh(and_v(v:pk([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa/<0;1>/*),pk([6738736c/48h/0h/0h/100h]xpub6FC1fXFP1GXQpyRFfSE1vzzySqs3Vg63bzimYLeqtNUYbzA87kMNTcuy9ubr7MmavGRjW2FRYHP4WGKjwutbf1ghgkUW9H7e3ceaPLRcVwa/<1;2>/*)))"); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/src/wallet/test/walletload_tests.cpp b/src/wallet/test/walletload_tests.cpp index 8648af6b6c2d..4647ec320d94 100644 --- a/src/wallet/test/walletload_tests.cpp +++ b/src/wallet/test/walletload_tests.cpp @@ -10,6 +10,8 @@ #include +#include + namespace wallet { BOOST_AUTO_TEST_SUITE(walletload_tests) @@ -41,6 +43,8 @@ class DummyDescriptor final : public Descriptor { std::vector Warnings() const override { return {}; } uint32_t GetMaxKeyExpr() const override { return 0; } size_t GetKeyCount() const override { return 0; } + void GetDerivationIndex(std::vector& out) const override {} + std::optional BuildWalletPolicy(const MultipathContext& ctx = {}) const override { return std::nullopt; } }; BOOST_FIXTURE_TEST_CASE(wallet_load_descriptors, TestingSetup)