Skip to content

Commit 4930ccb

Browse files
committed
key: replace ConversionError with NonDefiniteKeyError
The new error type is now more descriptive and a strict superset of the old one. Furthermore, the new name is more appropriate since this error typically arises during construction of a DefiniteDescriptorKey rather than during conversion between different key types.
1 parent 627d963 commit 4930ccb

File tree

3 files changed

+23
-50
lines changed

3 files changed

+23
-50
lines changed

src/descriptor/key.rs

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -689,33 +689,6 @@ impl From<PublicKey> for DescriptorPublicKey {
689689
}
690690
}
691691

692-
/// Descriptor key conversion error
693-
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
694-
pub enum ConversionError {
695-
/// Attempted to convert a key with hardened derivations to a bitcoin public key
696-
HardenedChild,
697-
/// Attempted to convert a key with multiple derivation paths to a bitcoin public key
698-
MultiKey,
699-
}
700-
701-
impl fmt::Display for ConversionError {
702-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
703-
f.write_str(match *self {
704-
Self::HardenedChild => "hardened child step in bip32 path",
705-
Self::MultiKey => "multiple existing keys",
706-
})
707-
}
708-
}
709-
710-
#[cfg(feature = "std")]
711-
impl error::Error for ConversionError {
712-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
713-
match self {
714-
Self::HardenedChild | Self::MultiKey => None,
715-
}
716-
}
717-
}
718-
719692
impl DescriptorPublicKey {
720693
/// The fingerprint of the master key associated with this key, `0x00000000` if none.
721694
pub fn master_fingerprint(&self) -> bip32::Fingerprint {
@@ -849,7 +822,10 @@ impl DescriptorPublicKey {
849822
///
850823
/// - If `index` is hardened.
851824
/// - If the key contains multi-path derivations
852-
pub fn at_derivation_index(self, index: u32) -> Result<DefiniteDescriptorKey, ConversionError> {
825+
pub fn at_derivation_index(
826+
self,
827+
index: u32,
828+
) -> Result<DefiniteDescriptorKey, NonDefiniteKeyError> {
853829
let definite = match self {
854830
DescriptorPublicKey::Single(_) => self,
855831
DescriptorPublicKey::XPub(xpub) => {
@@ -858,12 +834,12 @@ impl DescriptorPublicKey {
858834
Wildcard::Unhardened => xpub.derivation_path.into_child(
859835
bip32::ChildNumber::from_normal_idx(index)
860836
.ok()
861-
.ok_or(ConversionError::HardenedChild)?,
837+
.ok_or(NonDefiniteKeyError::HardenedStep)?,
862838
),
863839
Wildcard::Hardened => xpub.derivation_path.into_child(
864840
bip32::ChildNumber::from_hardened_idx(index)
865841
.ok()
866-
.ok_or(ConversionError::HardenedChild)?,
842+
.ok_or(NonDefiniteKeyError::HardenedStep)?,
867843
),
868844
};
869845
DescriptorPublicKey::XPub(DescriptorXKey {
@@ -873,7 +849,7 @@ impl DescriptorPublicKey {
873849
wildcard: Wildcard::None,
874850
})
875851
}
876-
DescriptorPublicKey::MultiXPub(_) => return Err(ConversionError::MultiKey),
852+
DescriptorPublicKey::MultiXPub(_) => return Err(NonDefiniteKeyError::Multipath),
877853
};
878854

879855
Ok(DefiniteDescriptorKey::new(definite)

src/descriptor/mod.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ pub mod checksum;
5353
mod key;
5454

5555
pub use self::key::{
56-
ConversionError, DefiniteDescriptorKey, DerivPaths, DescriptorKeyParseError,
57-
DescriptorMultiXKey, DescriptorPublicKey, DescriptorSecretKey, DescriptorXKey, InnerXKey,
58-
MalformedKeyDataKind, SinglePriv, SinglePub, SinglePubKey, Wildcard,
56+
DefiniteDescriptorKey, DerivPaths, DescriptorKeyParseError, DescriptorMultiXKey,
57+
DescriptorPublicKey, DescriptorSecretKey, DescriptorXKey, InnerXKey, MalformedKeyDataKind,
58+
NonDefiniteKeyError, SinglePriv, SinglePub, SinglePubKey, Wildcard,
5959
};
6060

6161
/// Alias type for a map of public key to secret key
@@ -660,17 +660,14 @@ impl Descriptor<DescriptorPublicKey> {
660660
pub fn at_derivation_index(
661661
&self,
662662
index: u32,
663-
) -> Result<Descriptor<DefiniteDescriptorKey>, ConversionError> {
663+
) -> Result<Descriptor<DefiniteDescriptorKey>, NonDefiniteKeyError> {
664664
struct Derivator(u32);
665665

666666
impl Translator<DescriptorPublicKey> for Derivator {
667667
type TargetPk = DefiniteDescriptorKey;
668-
type Error = ConversionError;
668+
type Error = NonDefiniteKeyError;
669669

670-
fn pk(
671-
&mut self,
672-
pk: &DescriptorPublicKey,
673-
) -> Result<DefiniteDescriptorKey, ConversionError> {
670+
fn pk(&mut self, pk: &DescriptorPublicKey) -> Result<Self::TargetPk, Self::Error> {
674671
pk.clone().at_derivation_index(self.0)
675672
}
676673

@@ -711,7 +708,7 @@ impl Descriptor<DescriptorPublicKey> {
711708
&self,
712709
secp: &secp256k1::Secp256k1<C>,
713710
index: u32,
714-
) -> Result<Descriptor<bitcoin::PublicKey>, ConversionError> {
711+
) -> Result<Descriptor<bitcoin::PublicKey>, NonDefiniteKeyError> {
715712
Ok(self.at_derivation_index(index)?.derived_descriptor(secp))
716713
}
717714

@@ -852,7 +849,7 @@ impl Descriptor<DescriptorPublicKey> {
852849
secp: &secp256k1::Secp256k1<C>,
853850
script_pubkey: &Script,
854851
range: Range<u32>,
855-
) -> Result<Option<(u32, Descriptor<bitcoin::PublicKey>)>, ConversionError> {
852+
) -> Result<Option<(u32, Descriptor<bitcoin::PublicKey>)>, NonDefiniteKeyError> {
856853
let range = if self.has_wildcard() { range } else { 0..1 };
857854

858855
for i in range {

src/psbt/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -925,14 +925,14 @@ pub trait PsbtInputExt {
925925
fn update_with_descriptor_unchecked(
926926
&mut self,
927927
descriptor: &Descriptor<DefiniteDescriptorKey>,
928-
) -> Result<Descriptor<bitcoin::PublicKey>, descriptor::ConversionError>;
928+
) -> Result<Descriptor<bitcoin::PublicKey>, descriptor::NonDefiniteKeyError>;
929929
}
930930

931931
impl PsbtInputExt for psbt::Input {
932932
fn update_with_descriptor_unchecked(
933933
&mut self,
934934
descriptor: &Descriptor<DefiniteDescriptorKey>,
935-
) -> Result<Descriptor<bitcoin::PublicKey>, descriptor::ConversionError> {
935+
) -> Result<Descriptor<bitcoin::PublicKey>, descriptor::NonDefiniteKeyError> {
936936
let (derived, _) = update_item_with_descriptor_helper(self, descriptor, None)?;
937937
Ok(derived)
938938
}
@@ -959,14 +959,14 @@ pub trait PsbtOutputExt {
959959
fn update_with_descriptor_unchecked(
960960
&mut self,
961961
descriptor: &Descriptor<DefiniteDescriptorKey>,
962-
) -> Result<Descriptor<bitcoin::PublicKey>, descriptor::ConversionError>;
962+
) -> Result<Descriptor<bitcoin::PublicKey>, descriptor::NonDefiniteKeyError>;
963963
}
964964

965965
impl PsbtOutputExt for psbt::Output {
966966
fn update_with_descriptor_unchecked(
967967
&mut self,
968968
descriptor: &Descriptor<DefiniteDescriptorKey>,
969-
) -> Result<Descriptor<bitcoin::PublicKey>, descriptor::ConversionError> {
969+
) -> Result<Descriptor<bitcoin::PublicKey>, descriptor::NonDefiniteKeyError> {
970970
let (derived, _) = update_item_with_descriptor_helper(self, descriptor, None)?;
971971
Ok(derived)
972972
}
@@ -1083,10 +1083,10 @@ fn update_item_with_descriptor_helper<F: PsbtFields>(
10831083
check_script: Option<&Script>,
10841084
// We return an extra boolean here to indicate an error with `check_script`. We do this
10851085
// because the error is "morally" a UtxoUpdateError::MismatchedScriptPubkey, but some
1086-
// callers expect a `descriptor::ConversionError`, which cannot be produced from a
1086+
// callers expect a `descriptor::NonDefiniteKeyError`, which cannot be produced from a
10871087
// `UtxoUpdateError`, and those callers can't get this error anyway because they pass
10881088
// `None` for `check_script`.
1089-
) -> Result<(Descriptor<bitcoin::PublicKey>, bool), descriptor::ConversionError> {
1089+
) -> Result<(Descriptor<bitcoin::PublicKey>, bool), descriptor::NonDefiniteKeyError> {
10901090
let secp = Secp256k1::verification_only();
10911091

10921092
// 1. Derive the descriptor, recording each key derivation in a map from xpubs
@@ -1182,7 +1182,7 @@ pub enum UtxoUpdateError {
11821182
/// The unsigned transaction didn't have an input at that index
11831183
MissingInputUtxo,
11841184
/// Derivation error
1185-
DerivationError(descriptor::ConversionError),
1185+
DerivationError(descriptor::NonDefiniteKeyError),
11861186
/// The PSBT's `witness_utxo` and/or `non_witness_utxo` were invalid or missing
11871187
UtxoCheck,
11881188
/// The PSBT's `witness_utxo` and/or `non_witness_utxo` had a script_pubkey that did not match
@@ -1231,7 +1231,7 @@ pub enum OutputUpdateError {
12311231
/// The raw unsigned transaction didn't have an output at that index
12321232
MissingTxOut,
12331233
/// Derivation error
1234-
DerivationError(descriptor::ConversionError),
1234+
DerivationError(descriptor::NonDefiniteKeyError),
12351235
/// The output's script_pubkey did not match the descriptor
12361236
MismatchedScriptPubkey,
12371237
}

0 commit comments

Comments
 (0)