In b2205211a62f0b65b71941e2e12fe8fb3919de89 util: Store hardened character in KeyPathElement: worth elaborating in the commit message why you're doing this. E.g.:
It's important for descriptor ID stability that we track whether hardened derivation used an h or apostrophe. Unify this tracking in KeyPathElement.
Note that this breaks downgrading a mixed usage miniscript:
diff --git a/test/functional/wallet_backwards_compatibility.py b/test/functional/wallet_backwards_compatibility.py
index 0d94d9107a..1e6ecc418c 100755
--- a/test/functional/wallet_backwards_compatibility.py
+++ b/test/functional/wallet_backwards_compatibility.py
@@ -340,4 +340,14 @@ class BackwardsCompatibilityTest(BitcoinTestFramework):
assert_equal(res[0]["success"], True)
+ # A key expression that mixes both hardened markers. Older releases recompute
+ # the descriptor ID when loading, and print such an expression with a single
+ # marker.
+ node_master.createwallet(wallet_name="miniscript_mixed")
+ wallet = node_master.get_wallet_rpc("miniscript_mixed")
+ miniscript_mixed = miniscript_desc.replace("[beefdead/4h/5h]", "[beefdead/4'/5h]")
+ assert miniscript_mixed != miniscript_desc
+ res = wallet.importdescriptors([{"desc": descsum_create(miniscript_mixed), "timestamp":"now"}])
+ assert_equal(res[0]["success"], True)
+
# Unload wallets and copy to older nodes:
node_master_wallets_dir = node_master.wallets_path
@@ -346,4 +356,5 @@ class BackwardsCompatibilityTest(BitcoinTestFramework):
node_master.unloadwallet("w3")
node_master.unloadwallet("miniscript")
+ node_master.unloadwallet("miniscript_mixed")
for node in previous_nodes:
@@ -359,9 +370,10 @@ class BackwardsCompatibilityTest(BitcoinTestFramework):
for node in descriptors_nodes:
self.log.info(f"- {node.version}")
- for wallet_name in ["w1", "w2", "w3", "miniscript"]:
+ for wallet_name in ["w1", "w2", "w3", "miniscript", "miniscript_mixed"]:
+ is_miniscript = wallet_name in ("miniscript", "miniscript_mixed")
if self.major_version_less_than(node, 22) and wallet_name == "w1":
# Descriptor wallets created after 0.21 have taproot descriptors which 0.21 does not support, tested below
continue
- if self.major_version_less_than(node, 24) and wallet_name == "miniscript":
+ if self.major_version_less_than(node, 24) and is_miniscript:
# Miniscript was introduced in 24.0
continue
@@ -372,5 +384,5 @@ class BackwardsCompatibilityTest(BitcoinTestFramework):
# result in wallets that cannot be loaded into 31.0 and 31.1.
# These wallets will emit a "Wallet corrupted" error.
- if wallet_name == "miniscript" and n.version in (310000, 310100):
+ if is_miniscript and n.version in (310000, 310100):
assert_raises_rpc_error(-4, "Wallet corrupted", n.loadwallet, wallet_name)
continue
@@ -400,5 +412,5 @@ class BackwardsCompatibilityTest(BitcoinTestFramework):
assert_equal(info['private_keys_enabled'], True)
assert_equal(info['keypoolsize'], 0)
- elif wallet_name == "miniscript":
+ elif is_miniscript:
descs = wallet.listdescriptors()["descriptors"]
assert any(desc["desc"].startswith("wsh(or_b(pk") for desc in descs), "Miniscript descriptor missing"
We could refuse to import those, but BIP380 permits it.
Or restore the previous behavior:
diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp
index 1cc3cf7c2d..6e98a22189 100644
--- a/src/script/descriptor.cpp
+++ b/src/script/descriptor.cpp
@@ -1887,4 +1887,5 @@ enum class ParseScriptContext {
* [@param](/bitcoin-bitcoin/contributor/param/)[in] split BIP32 path string, using either ' or h for hardened derivation
* [@param](/bitcoin-bitcoin/contributor/param/)[out] out Vector of parsed key paths
+ * [@param](/bitcoin-bitcoin/contributor/param/)[out] hardened only updated if hardened derivation is found
* [@param](/bitcoin-bitcoin/contributor/param/)[out] error parsing error message
* [@param](/bitcoin-bitcoin/contributor/param/)[in] allow_multipath Allows the parsed path to use the multipath specifier
@@ -1892,5 +1893,5 @@ enum class ParseScriptContext {
* [@returns](/bitcoin-bitcoin/contributor/returns/) false if parsing failed
**/
-[[nodiscard]] bool ParseKeyPath(const std::vector<std::span<const char>>& split, std::vector<KeyPath>& out, std::string& error, bool allow_multipath, bool& has_hardened)
+[[nodiscard]] bool ParseKeyPath(const std::vector<std::span<const char>>& split, std::vector<KeyPath>& out, char& hardened, std::string& error, bool allow_multipath, bool& has_hardened)
{
auto parse_elem = [&](std::span<const char> elem) -> std::optional<KeyPathElement> {
@@ -1902,4 +1903,5 @@ enum class ParseScriptContext {
if (parsed->IsHardened()) {
has_hardened = true;
+ hardened = elem.back();
}
return *parsed;
@@ -1970,8 +1972,8 @@ enum class ParseScriptContext {
}
-[[nodiscard]] bool ParseKeyPath(const std::vector<std::span<const char>>& split, std::vector<KeyPath>& out, std::string& error, bool allow_multipath)
+[[nodiscard]] bool ParseKeyPath(const std::vector<std::span<const char>>& split, std::vector<KeyPath>& out, char& hardened, std::string& error, bool allow_multipath)
{
bool dummy;
- return ParseKeyPath(split, out, error, allow_multipath, /*has_hardened=*/dummy);
+ return ParseKeyPath(split, out, hardened, error, allow_multipath, /*has_hardened=*/dummy);
}
@@ -1995,5 +1997,5 @@ static DeriveType ParseDeriveType(std::vector<std::span<const char>>& split, cha
/** Parse a public key that excludes origin information. */
-std::vector<std::unique_ptr<PubkeyProvider>> ParsePubkeyInner(uint32_t& key_exp_index, const std::span<const char>& sp, ParseScriptContext ctx, FlatSigningProvider& out, std::string& error)
+std::vector<std::unique_ptr<PubkeyProvider>> ParsePubkeyInner(uint32_t& key_exp_index, const std::span<const char>& sp, ParseScriptContext ctx, FlatSigningProvider& out, char& hardened, std::string& error)
{
std::vector<std::unique_ptr<PubkeyProvider>> ret;
@@ -2059,7 +2061,6 @@ std::vector<std::unique_ptr<PubkeyProvider>> ParsePubkeyInner(uint32_t& key_exp_
}
std::vector<KeyPath> paths;
- char hardened{'h'};
DeriveType type = ParseDeriveType(split, hardened);
- if (!ParseKeyPath(split, paths, error, /*allow_multipath=*/true)) return {};
+ if (!ParseKeyPath(split, paths, hardened, error, /*allow_multipath=*/true)) return {};
if (extkey.key.IsValid()) {
extpubkey = extkey.Neuter();
@@ -2067,4 +2068,5 @@ std::vector<std::unique_ptr<PubkeyProvider>> ParsePubkeyInner(uint32_t& key_exp_
}
for (auto& path : paths) {
+ path.SetHardenedChar(hardened);
ret.emplace_back(std::make_unique<BIP32PubkeyProvider>(key_exp_index, extpubkey, std::move(path), type, hardened));
}
@@ -2153,5 +2155,5 @@ std::vector<std::unique_ptr<PubkeyProvider>> ParsePubkey(uint32_t& key_exp_index
}
bool has_hardened = false;
- if (!ParseKeyPath(deriv_split, derivation_multipaths, error, /*allow_multipath=*/true, has_hardened)) {
+ if (!ParseKeyPath(deriv_split, derivation_multipaths, dummy, error, /*allow_multipath=*/true, has_hardened)) {
error = "musig(): " + error;
return {};
@@ -2227,7 +2229,9 @@ std::vector<std::unique_ptr<PubkeyProvider>> ParsePubkey(uint32_t& key_exp_index
return {};
}
- // This is set if either the origin or path suffix contains a hardened derivation.
+ // A key expression is always written with a single hardened marker: that of the last
+ // hardened step parsed. Descriptor IDs of wallets with Miniscript descriptors depend on this.
+ char hardened{'h'};
if (origin_split.size() == 1) {
- return ParsePubkeyInner(key_exp_index, origin_split[0], ctx, out, error);
+ return ParsePubkeyInner(key_exp_index, origin_split[0], ctx, out, hardened, error);
}
if (origin_split[0].empty() || origin_split[0][0] != '[') {
@@ -2252,8 +2256,9 @@ std::vector<std::unique_ptr<PubkeyProvider>> ParsePubkey(uint32_t& key_exp_index
std::copy_n(fpr_bytes.begin(), info.fingerprint.size(), info.fingerprint.begin());
std::vector<KeyPath> path;
- if (!ParseKeyPath(slash_split, path, error, /*allow_multipath=*/false)) return {};
+ if (!ParseKeyPath(slash_split, path, hardened, error, /*allow_multipath=*/false)) return {};
info.path = path.at(0);
- auto providers = ParsePubkeyInner(key_exp_index, origin_split[1], ctx, out, error);
+ auto providers = ParsePubkeyInner(key_exp_index, origin_split[1], ctx, out, hardened, error);
if (providers.empty()) return {};
+ info.path.SetHardenedChar(hardened);
ret.reserve(providers.size());
for (auto& prov : providers) {