Skip to content

descriptor: replace the key module with rust-miniscript's - #117

Open
KyrylR wants to merge 12 commits into
ElementsProject:masterfrom
KyrylR:pr/107-upstream-key-errors
Open

KyrylR wants to merge 12 commits into
ElementsProject:masterfrom
KyrylR:pr/107-upstream-key-errors

Conversation

@KyrylR

@KyrylR KyrylR commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Part of #107.

As suggested in review, this deletes src/descriptor/key.rs, our fork of rust-miniscript's key module, and re-exports the key types from rust-miniscript 13.2 instead of porting its changes one by one. The types are already identical underneath, since secp256k1-zkp 0.11 re-exports secp256k1.

Commits:

  • 1–3 are the prep work: remove the extra derives from the PSET update errors (rust-miniscript 627d963), rename ConversionError to NonDefiniteKeyError, and give confidential its own copy of maybe_fmt_master_id.
  • 4–6 require rust-miniscript 13.2, parse ct() view keys in confidential, then delete key.rs and re-export.
  • 7–9 port what the new types allow: derived_descriptor becomes infallible (c169af0), the deprecated is_deriveable and derive are removed (77ed78b), and a parse_descriptor_definite fuzz target is added (f09bf48).
  • 10–12 add tests, docs and the CHANGELOG.

Breaking changes are listed in the CHANGELOG. The main ones:

  • ConversionError is now NonDefiniteKeyError, and Error::Conversion is now Error::NonDefiniteKey.
  • A definite key can no longer have a hardened step after an xpub, several derivation paths, or a derivation deeper than BIP32's maximum depth.
  • SinglePub, DescriptorXKey and DescriptorMultiXKey lose the MiniscriptKey and Display impls added in Implement MiniscriptKey for each variant of DescriptorPublicKey #100. rust-miniscript has them only on master (#938), and they can't be added back on re-exported types.

This does not close #107: ct(elip151, …) with a hardened key still panics in elip151.rs. That fix comes in a separate PR on top of this one.

Comment thread CHANGELOG.md Outdated
Comment on lines 16 to 25
- Breaking: `DescriptorKeyParseError` is a `#[non_exhaustive]` enum instead of a struct holding a string, and no longer derives `Copy`. The new `MalformedKeyDataKind` enum describes malformed key data. Messages start with a lowercase letter and follow rust-miniscript. The Elements-only `HexPrivateKey` variant reports a bad hex view key in a `ct()` descriptor. Ported from rust-miniscript 497c5a3 and bd5db6a.
- Breaking: `ConversionError` is replaced by the `#[non_exhaustive]` `NonDefiniteKeyError`, with `Wildcard`, `Multipath`, `HardenedStep` and `MaximumDepthExceeded` variants. It is returned by `DefiniteDescriptorKey::new`, `DescriptorPublicKey::at_derivation_index`, `Descriptor::at_derivation_index`, `Descriptor::derived_descriptor`, `Descriptor::find_derivation_index_for_spk`, `confidential::Descriptor::at_derivation_index` and the PSET `update_with_descriptor_unchecked` methods, and held by `UtxoUpdateError::DerivationError` and `OutputUpdateError::DerivationError`. `Error::Conversion` becomes `Error::NonDefiniteKey`. Ported from rust-miniscript 258873f, dad3532, 7b3b3a8, 4930ccb and 613ed0d.
- Breaking: `DefiniteDescriptorKey::new` is public and returns `Result<Self, NonDefiniteKeyError>`. Keys with a wildcard were already rejected. It now also rejects a hardened derivation step after an xpub (`HardenedStep`), several derivation paths (`Multipath`), and a derivation deeper than BIP32's maximum depth of 255 (`MaximumDepthExceeded`), checked in the order wildcard, hardened step, multipath, depth. `DescriptorPublicKey::at_derivation_index`, and the descriptor methods built on it, return the same errors. `DefiniteDescriptorKey::from_str` wraps them in `DescriptorKeyParseError::NonDefiniteKey`, so `Descriptor::<DefiniteDescriptorKey>::from_str` now rejects hardened and multipath keys, which previously parsed and panicked when derived, for example in `script_pubkey`. A `ct()` descriptor whose bare blinding key has a hardened wildcard now fails in `at_derivation_index` instead of `address`. Ported from rust-miniscript dad3532, 7b3b3a8, 97c948d and 613ed0d.
- Parsing an xpub or xprv whose derivation goes deeper than BIP32's maximum depth of 255 fails with `DescriptorKeyParseError::MalformedKeyData(MalformedKeyDataKind::MaximumDepthExceeded)`, so such a string never reaches the constructor. A wildcard or multipath step counts as one step, and origin steps don't count. The constructor's `NonDefiniteKeyError::MaximumDepthExceeded` is only returned for keys built in code. Ported from rust-miniscript f2862f7.
- Breaking: `DefiniteDescriptorKey::derive_public_key` returns a `bitcoin::PublicKey` and `Descriptor::<DefiniteDescriptorKey>::derived_descriptor` returns a descriptor, instead of a `Result`. Ported from rust-miniscript c169af0.
- Breaking: the deprecated `DescriptorPublicKey::is_deriveable`, `DescriptorPublicKey::derive`, `Descriptor::is_deriveable` and `Descriptor::derive` are removed. Use `has_wildcard` and `at_derivation_index`. Ported from rust-miniscript 77ed78b.
- Breaking: `UtxoUpdateError` and `OutputUpdateError` derive only `Debug`, `PartialEq`, `Eq` and `Clone`. `NonDefiniteKeyError`, `MalformedKeyDataKind` and `DescriptorKeyParseError` derive `Eq`. Ported from rust-miniscript 627d963.
- Add `DescriptorPublicKey::has_hardened_step`. Ported from rust-miniscript 7b3b3a8 and, for its doc line, 4906bed (partial).
- `DescriptorSecretKey::to_public` converts a multi-xpriv into a multi-xpub when all hardened derivation steps are shared among its paths, instead of always failing. Ported from rust-miniscript 7d3e1e8.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no way around with this changelog

Maybe the reorg will help, but the total number of already introduced changes does not help

Will be grateful for the advice regarding this

@KyrylR

KyrylR commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

If/when this merged I will be able to fully resolve the #107

@apoelstra

Copy link
Copy Markdown
Member

Hah, thanks for doing all this work. I'd suggest that we just copy/paste the whole key module, reapply the elements changes (if any) and not attempt to cherry-pick the individual changes.

Actually, what I'd suggest is that we delete the key module entirely here and replace it with re-exports from rust-miniscript. (There is a little bit of prep work needed -- we need to remove Hash/etc from our error types, we need to rename ConversionError to NonDefiniteKeyError, and we need to move the maybe_fmt_master_id somewhere other than the to-be-deleted key.rs.)

I believe that keys between rust-miniscript and elements-miniscript should be literally identical types now. Historically they needed to be distinct because secp256k1-zkp keys were different from secp256k1 keys, but that should be fixed with the next release of rust-secp-zkp. And historically we needed rust-secp-zkp to get musig2, but that shouldn't be needed either as soon as https://git.rust-bitcoin.org/rust-bitcoin/rust-secp256k1/pulls/952 merges.

@apoelstra

Copy link
Copy Markdown
Member

07aefaf needs rebase

@KyrylR
KyrylR force-pushed the pr/107-upstream-key-errors branch from 07aefaf to 93abf33 Compare November 20, 2026 11:37
@KyrylR

KyrylR commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

07aefaf needs rebase

Rebased

@KyrylR
KyrylR force-pushed the pr/107-upstream-key-errors branch from 93abf33 to f345bdf Compare November 20, 2026 11:39
KyrylR and others added 12 commits November 30, 2026 13:44
Port of upstream "psbt: remove derives from UtxoUpdateError and OutputUpdateError".

Source: rust-miniscript 627d963
Co-authored-by: Andrew Poelstra <apoelstra@wpsoftware.net>
Use the name and variant names of the equivalent rust-miniscript error, so that the key module can be replaced with rust-miniscript's
Give confidential/mod.rs a copy of a maybe_fmt_master_id, so that the key module can be replaced with rust-miniscript's, which does not export it
Port of upstream "key.rs: remove a bunch of unwrap()s which are impossible", for its descriptor and PSET part.

Source: rust-miniscript c169af0
Co-authored-by: Andrew Poelstra <apoelstra@wpsoftware.net>
Port of upstream "key.rs: remove some deprecated methods", for its descriptor part.

Source: rust-miniscript 77ed78b
Co-authored-by: Andrew Poelstra <apoelstra@wpsoftware.net>
Port of upstream "fuzz: add fuzz test for parsing definite descriptor keys".

Source: rust-miniscript f09bf48
Co-authored-by: Andrew Poelstra <apoelstra@wpsoftware.net>
@KyrylR
KyrylR force-pushed the pr/107-upstream-key-errors branch from f345bdf to fb283f3 Compare December 1, 2026 12:15
@KyrylR KyrylR changed the title descriptor: port upstream definite key and key error changes descriptor: replace the key module with rust-miniscript's Dec 1, 2026
@KyrylR

KyrylR commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Reworked as you suggested: key.rs is deleted and the key types are re-exported from rust-miniscript 13.2. One question: 13.2 doesn't have the MiniscriptKey/Display impls on SinglePub, DescriptorXKey and DescriptorMultiXKey that we shipped in 0.5.0 (#100). OK to drop them here, or would you rather backport #938 to 13.x first?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hardened xpub wildcard causes panic during ELIP151 derivation

2 participants