bdk_wallet/descriptor/
error.rs

1// Bitcoin Dev Kit
2// Written in 2020 by Alekos Filini <alekos.filini@gmail.com>
3//
4// Copyright (c) 2020-2021 Bitcoin Dev Kit Developers
5//
6// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
7// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
9// You may not use this file except in accordance with one or both of these
10// licenses.
11
12//! Descriptor errors
13use core::fmt;
14
15/// Errors related to the parsing and usage of descriptors
16#[derive(Debug, PartialEq)]
17pub enum Error {
18    /// Invalid HD Key path, such as having a wildcard but a length != 1
19    InvalidHdKeyPath,
20    /// The provided descriptor doesn't match its checksum
21    InvalidDescriptorChecksum,
22    /// The descriptor contains hardened derivation steps on public extended keys
23    HardenedDerivationXpub,
24    /// The descriptor contains multipath keys with an invalid number of paths (must have exactly 2
25    /// paths for receive and change)
26    MultiPath,
27    /// Error thrown while working with [`keys`](crate::keys)
28    Key(crate::keys::KeyError),
29    /// Error while extracting and manipulating policies
30    Policy(crate::descriptor::policy::PolicyError),
31
32    /// Invalid byte found in the descriptor checksum
33    InvalidDescriptorCharacter(u8),
34
35    /// BIP32 error
36    Bip32(bitcoin::bip32::Error),
37    /// Error during base58 decoding
38    Base58(bitcoin::base58::Error),
39    /// Key-related error
40    Pk(bitcoin::key::ParsePublicKeyError),
41    /// Miniscript error
42    Miniscript(miniscript::Error),
43    /// Hex decoding error
44    Hex(bitcoin::hex::HexToBytesError),
45    /// The provided wallet descriptors are identical
46    ExternalAndInternalAreTheSame,
47}
48
49impl From<crate::keys::KeyError> for Error {
50    fn from(key_error: crate::keys::KeyError) -> Error {
51        match key_error {
52            crate::keys::KeyError::Miniscript(inner) => Error::Miniscript(inner),
53            crate::keys::KeyError::Bip32(inner) => Error::Bip32(inner),
54            e => Error::Key(e),
55        }
56    }
57}
58
59impl fmt::Display for Error {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        match self {
62            Self::InvalidHdKeyPath => write!(f, "Invalid HD key path"),
63            Self::InvalidDescriptorChecksum => {
64                write!(f, "The provided descriptor doesn't match its checksum")
65            }
66            Self::HardenedDerivationXpub => write!(
67                f,
68                "The descriptor contains hardened derivation steps on public extended keys"
69            ),
70            Self::MultiPath => write!(
71                f,
72                "The descriptor contains multipath keys with invalid number of paths (must have exactly 2 paths for receive and change)"
73            ),
74            Self::Key(err) => write!(f, "Key error: {err}"),
75            Self::Policy(err) => write!(f, "Policy error: {err}"),
76            Self::InvalidDescriptorCharacter(char) => {
77                write!(f, "Invalid descriptor character: {char}")
78            }
79            Self::Bip32(err) => write!(f, "BIP32 error: {err}"),
80            Self::Base58(err) => write!(f, "Base58 error: {err}"),
81            Self::Pk(err) => write!(f, "Key-related error: {err}"),
82            Self::Miniscript(err) => write!(f, "Miniscript error: {err}"),
83            Self::Hex(err) => write!(f, "Hex decoding error: {err}"),
84            Self::ExternalAndInternalAreTheSame => {
85                write!(f, "External and internal descriptors are the same")
86            }
87        }
88    }
89}
90
91#[cfg(feature = "std")]
92impl std::error::Error for Error {}
93
94impl From<bitcoin::bip32::Error> for Error {
95    fn from(err: bitcoin::bip32::Error) -> Self {
96        Error::Bip32(err)
97    }
98}
99
100impl From<bitcoin::base58::Error> for Error {
101    fn from(err: bitcoin::base58::Error) -> Self {
102        Error::Base58(err)
103    }
104}
105
106impl From<bitcoin::key::ParsePublicKeyError> for Error {
107    fn from(err: bitcoin::key::ParsePublicKeyError) -> Self {
108        Error::Pk(err)
109    }
110}
111
112impl From<miniscript::Error> for Error {
113    fn from(err: miniscript::Error) -> Self {
114        Error::Miniscript(err)
115    }
116}
117
118impl From<bitcoin::hex::HexToBytesError> for Error {
119    fn from(err: bitcoin::hex::HexToBytesError) -> Self {
120        Error::Hex(err)
121    }
122}
123
124impl From<crate::descriptor::policy::PolicyError> for Error {
125    fn from(err: crate::descriptor::policy::PolicyError) -> Self {
126        Error::Policy(err)
127    }
128}