1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Bitcoin Dev Kit
// Written in 2020 by Alekos Filini <alekos.filini@gmail.com>
//
// Copyright (c) 2020-2021 Bitcoin Dev Kit Developers
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.

//! Descriptor errors
use core::fmt;

/// Errors related to the parsing and usage of descriptors
#[derive(Debug, PartialEq)]
pub enum Error {
    /// Invalid HD Key path, such as having a wildcard but a length != 1
    InvalidHdKeyPath,
    /// The provided descriptor doesn't match its checksum
    InvalidDescriptorChecksum,
    /// The descriptor contains hardened derivation steps on public extended keys
    HardenedDerivationXpub,
    /// The descriptor contains multipath keys
    MultiPath,
    /// Error thrown while working with [`keys`](crate::keys)
    Key(crate::keys::KeyError),
    /// Error while extracting and manipulating policies
    Policy(crate::descriptor::policy::PolicyError),

    /// Invalid byte found in the descriptor checksum
    InvalidDescriptorCharacter(u8),

    /// BIP32 error
    Bip32(bitcoin::bip32::Error),
    /// Error during base58 decoding
    Base58(bitcoin::base58::Error),
    /// Key-related error
    Pk(bitcoin::key::ParsePublicKeyError),
    /// Miniscript error
    Miniscript(miniscript::Error),
    /// Hex decoding error
    Hex(bitcoin::hex::HexToBytesError),
    /// The provided wallet descriptors are identical
    ExternalAndInternalAreTheSame,
}

impl From<crate::keys::KeyError> for Error {
    fn from(key_error: crate::keys::KeyError) -> Error {
        match key_error {
            crate::keys::KeyError::Miniscript(inner) => Error::Miniscript(inner),
            crate::keys::KeyError::Bip32(inner) => Error::Bip32(inner),
            e => Error::Key(e),
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidHdKeyPath => write!(f, "Invalid HD key path"),
            Self::InvalidDescriptorChecksum => {
                write!(f, "The provided descriptor doesn't match its checksum")
            }
            Self::HardenedDerivationXpub => write!(
                f,
                "The descriptor contains hardened derivation steps on public extended keys"
            ),
            Self::MultiPath => write!(
                f,
                "The descriptor contains multipath keys, which are not supported yet"
            ),
            Self::Key(err) => write!(f, "Key error: {}", err),
            Self::Policy(err) => write!(f, "Policy error: {}", err),
            Self::InvalidDescriptorCharacter(char) => {
                write!(f, "Invalid descriptor character: {}", char)
            }
            Self::Bip32(err) => write!(f, "BIP32 error: {}", err),
            Self::Base58(err) => write!(f, "Base58 error: {}", err),
            Self::Pk(err) => write!(f, "Key-related error: {}", err),
            Self::Miniscript(err) => write!(f, "Miniscript error: {}", err),
            Self::Hex(err) => write!(f, "Hex decoding error: {}", err),
            Self::ExternalAndInternalAreTheSame => {
                write!(f, "External and internal descriptors are the same")
            }
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl From<bitcoin::bip32::Error> for Error {
    fn from(err: bitcoin::bip32::Error) -> Self {
        Error::Bip32(err)
    }
}

impl From<bitcoin::base58::Error> for Error {
    fn from(err: bitcoin::base58::Error) -> Self {
        Error::Base58(err)
    }
}

impl From<bitcoin::key::ParsePublicKeyError> for Error {
    fn from(err: bitcoin::key::ParsePublicKeyError) -> Self {
        Error::Pk(err)
    }
}

impl From<miniscript::Error> for Error {
    fn from(err: miniscript::Error) -> Self {
        Error::Miniscript(err)
    }
}

impl From<bitcoin::hex::HexToBytesError> for Error {
    fn from(err: bitcoin::hex::HexToBytesError) -> Self {
        Error::Hex(err)
    }
}

impl From<crate::descriptor::policy::PolicyError> for Error {
    fn from(err: crate::descriptor::policy::PolicyError) -> Self {
        Error::Policy(err)
    }
}