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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::error;
use std::fmt;
use std::result;
#[cfg(feature = "f_bitcoin")]
use bitcoin;
#[cfg(feature = "f_bitcoin")]
use bitcoin::util::base58;
#[cfg(feature = "f_bitcoin")]
use bitcoin_hashes::sha256d;
use protobuf::error::ProtobufError;
#[cfg(feature = "f_bitcoin")]
use secp256k1;
use client::InteractionType;
use protos;
use transport;
#[derive(Debug)]
pub enum Error {
NoDeviceFound,
DeviceNotUnique,
TransportConnect(transport::error::Error),
TransportBeginSession(transport::error::Error),
TransportEndSession(transport::error::Error),
TransportSendMessage(transport::error::Error),
TransportReceiveMessage(transport::error::Error),
UnexpectedMessageType(protos::MessageType), Protobuf(ProtobufError),
FailureResponse(protos::Failure),
UnexpectedInteractionRequest(InteractionType),
#[cfg(feature = "f_bitcoin")]
Base58(base58::Error),
UnsupportedNetwork,
InvalidEntropy,
TxRequestInvalidIndex(usize),
#[cfg(feature = "f_bitcoin")]
TxRequestUnknownTxid(sha256d::Hash),
#[cfg(feature = "f_bitcoin")]
PsbtMissingInputTx(sha256d::Hash),
MalformedTxRequest(protos::TxRequest),
InvalidPsbt(String),
#[cfg(feature = "f_bitcoin")]
BitcoinEncode(bitcoin::consensus::encode::Error),
#[cfg(feature = "f_bitcoin")]
Secp256k1(secp256k1::Error),
}
impl From<ProtobufError> for Error {
fn from(e: ProtobufError) -> Error {
Error::Protobuf(e)
}
}
#[cfg(feature = "f_bitcoin")]
impl From<base58::Error> for Error {
fn from(e: base58::Error) -> Error {
Error::Base58(e)
}
}
#[cfg(feature = "f_bitcoin")]
impl From<bitcoin::consensus::encode::Error> for Error {
fn from(e: bitcoin::consensus::encode::Error) -> Error {
Error::BitcoinEncode(e)
}
}
#[cfg(feature = "f_bitcoin")]
impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Error {
Error::Secp256k1(e)
}
}
impl error::Error for Error {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
Error::TransportConnect(ref e) => Some(e),
Error::TransportBeginSession(ref e) => Some(e),
Error::TransportEndSession(ref e) => Some(e),
Error::TransportSendMessage(ref e) => Some(e),
Error::TransportReceiveMessage(ref e) => Some(e),
#[cfg(feature = "f_bitcoin")]
Error::Base58(ref e) => Some(e),
_ => None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::NoDeviceFound => write!(f, "Trezor device not found"),
Error::DeviceNotUnique => write!(f, "multiple Trezor devices found"),
Error::UnsupportedNetwork => write!(f, "given network is not supported"),
Error::InvalidEntropy => write!(f, "provided entropy is not 32 bytes"),
Error::TransportConnect(ref e) => write!(f, "transport connect: {}", e),
Error::TransportBeginSession(ref e) => write!(f, "transport beginning session: {}", e),
Error::TransportEndSession(ref e) => write!(f, "transport ending session: {}", e),
Error::TransportSendMessage(ref e) => write!(f, "transport sending message: {}", e),
Error::TransportReceiveMessage(ref e) => {
write!(f, "transport receiving message: {}", e)
}
Error::UnexpectedMessageType(ref t) => {
write!(f, "received unexpected message type: {:?}", t)
}
Error::Protobuf(ref e) => write!(f, "protobuf: {}", e),
Error::FailureResponse(ref e) => write!(
f,
r#"failure received: code={:?} message="{}""#,
e.get_code(),
e.get_message()
),
Error::UnexpectedInteractionRequest(ref r) => {
write!(f, "unexpected interaction request: {:?}", r)
}
#[cfg(feature = "f_bitcoin")]
Error::Base58(ref e) => fmt::Display::fmt(e, f),
Error::TxRequestInvalidIndex(ref i) => {
write!(f, "device referenced non-existing input or output index: {}", i)
}
#[cfg(feature = "f_bitcoin")]
Error::TxRequestUnknownTxid(ref txid) => {
write!(f, "device referenced unknown TXID: {}", txid)
}
#[cfg(feature = "f_bitcoin")]
Error::PsbtMissingInputTx(ref txid) => write!(f, "PSBT missing input tx: {}", txid),
Error::MalformedTxRequest(ref m) => write!(f, "malformed TxRequest: {:?}", m),
Error::InvalidPsbt(ref m) => write!(f, "invalid PSBT: {}", m),
#[cfg(feature = "f_bitcoin")]
Error::BitcoinEncode(ref e) => write!(f, "bitcoin encoding error: {}", e),
#[cfg(feature = "f_bitcoin")]
Error::Secp256k1(ref e) => write!(f, "ECDSA signature error: {}", e),
}
}
}
pub type Result<T> = result::Result<T, Error>;