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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use std::fmt;
use error::{Error, Result};
use super::Trezor;
use messages::TrezorMessage;
use protos;
pub use protos::ButtonRequest_ButtonRequestType as ButtonRequestType;
pub use protos::Features;
pub use protos::InputScriptType;
pub use protos::PinMatrixRequest_PinMatrixRequestType as PinMatrixRequestType;
pub enum WordCount {
W12 = 12,
W18 = 18,
W24 = 24,
}
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum InteractionType {
Button,
PinMatrix,
Passphrase,
PassphraseState,
}
pub type ResultHandler<'a, T, R> = dyn Fn(&'a mut Trezor, R) -> Result<T>;
pub struct ButtonRequest<'a, T, R: TrezorMessage> {
pub message: protos::ButtonRequest,
pub client: &'a mut Trezor,
pub result_handler: Box<ResultHandler<'a, T, R>>,
}
impl<'a, T, R: TrezorMessage> fmt::Debug for ButtonRequest<'a, T, R> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.message, f)
}
}
impl<'a, T, R: TrezorMessage> ButtonRequest<'a, T, R> {
pub fn request_type(&self) -> ButtonRequestType {
self.message.get_code()
}
pub fn ack(self) -> Result<TrezorResponse<'a, T, R>> {
let req = protos::ButtonAck::new();
self.client.call(req, self.result_handler)
}
}
pub struct PinMatrixRequest<'a, T, R: TrezorMessage> {
pub message: protos::PinMatrixRequest,
pub client: &'a mut Trezor,
pub result_handler: Box<ResultHandler<'a, T, R>>,
}
impl<'a, T, R: TrezorMessage> fmt::Debug for PinMatrixRequest<'a, T, R> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.message, f)
}
}
impl<'a, T, R: TrezorMessage> PinMatrixRequest<'a, T, R> {
pub fn request_type(&self) -> PinMatrixRequestType {
self.message.get_field_type()
}
pub fn ack_pin(self, pin: String) -> Result<TrezorResponse<'a, T, R>> {
let mut req = protos::PinMatrixAck::new();
req.set_pin(pin);
self.client.call(req, self.result_handler)
}
}
pub struct PassphraseRequest<'a, T, R: TrezorMessage> {
pub message: protos::PassphraseRequest,
pub client: &'a mut Trezor,
pub result_handler: Box<ResultHandler<'a, T, R>>,
}
impl<'a, T, R: TrezorMessage> fmt::Debug for PassphraseRequest<'a, T, R> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.message, f)
}
}
impl<'a, T, R: TrezorMessage> PassphraseRequest<'a, T, R> {
pub fn on_device(&self) -> bool {
self.message.get__on_device()
}
pub fn ack_passphrase(self, passphrase: String) -> Result<TrezorResponse<'a, T, R>> {
let mut req = protos::PassphraseAck::new();
req.set_passphrase(passphrase);
self.client.call(req, self.result_handler)
}
pub fn ack(self, on_device: bool) -> Result<TrezorResponse<'a, T, R>> {
let mut req = protos::PassphraseAck::new();
if on_device {
req.set_on_device(on_device);
}
self.client.call(req, self.result_handler)
}
}
#[derive(Debug)]
pub enum TrezorResponse<'a, T, R: TrezorMessage> {
Ok(T),
Failure(protos::Failure),
ButtonRequest(ButtonRequest<'a, T, R>),
PinMatrixRequest(PinMatrixRequest<'a, T, R>),
PassphraseRequest(PassphraseRequest<'a, T, R>),
}
impl<'a, T, R: TrezorMessage> fmt::Display for TrezorResponse<'a, T, R> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
TrezorResponse::Ok(ref _m) => write!(f, "Ok"), TrezorResponse::Failure(ref m) => write!(f, "Failure: {:?}", m),
TrezorResponse::ButtonRequest(ref r) => write!(f, "ButtonRequest: {:?}", r),
TrezorResponse::PinMatrixRequest(ref r) => write!(f, "PinMatrixRequest: {:?}", r),
TrezorResponse::PassphraseRequest(ref r) => write!(f, "PassphraseRequest: {:?}", r),
}
}
}
impl<'a, T, R: TrezorMessage> TrezorResponse<'a, T, R> {
pub fn ok(self) -> Result<T> {
match self {
TrezorResponse::Ok(m) => Ok(m),
TrezorResponse::Failure(m) => Err(Error::FailureResponse(m)),
TrezorResponse::ButtonRequest(_) => {
Err(Error::UnexpectedInteractionRequest(InteractionType::Button))
}
TrezorResponse::PinMatrixRequest(_) => {
Err(Error::UnexpectedInteractionRequest(InteractionType::PinMatrix))
}
TrezorResponse::PassphraseRequest(_) => {
Err(Error::UnexpectedInteractionRequest(InteractionType::Passphrase))
}
}
}
pub fn button_request(self) -> Result<ButtonRequest<'a, T, R>> {
match self {
TrezorResponse::ButtonRequest(r) => Ok(r),
TrezorResponse::Ok(_) => Err(Error::UnexpectedMessageType(R::message_type())),
TrezorResponse::Failure(m) => Err(Error::FailureResponse(m)),
TrezorResponse::PinMatrixRequest(_) => {
Err(Error::UnexpectedInteractionRequest(InteractionType::PinMatrix))
}
TrezorResponse::PassphraseRequest(_) => {
Err(Error::UnexpectedInteractionRequest(InteractionType::Passphrase))
}
}
}
pub fn pin_matrix_request(self) -> Result<PinMatrixRequest<'a, T, R>> {
match self {
TrezorResponse::PinMatrixRequest(r) => Ok(r),
TrezorResponse::Ok(_) => Err(Error::UnexpectedMessageType(R::message_type())),
TrezorResponse::Failure(m) => Err(Error::FailureResponse(m)),
TrezorResponse::ButtonRequest(_) => {
Err(Error::UnexpectedInteractionRequest(InteractionType::Button))
}
TrezorResponse::PassphraseRequest(_) => {
Err(Error::UnexpectedInteractionRequest(InteractionType::Passphrase))
}
}
}
pub fn passphrase_request(self) -> Result<PassphraseRequest<'a, T, R>> {
match self {
TrezorResponse::PassphraseRequest(r) => Ok(r),
TrezorResponse::Ok(_) => Err(Error::UnexpectedMessageType(R::message_type())),
TrezorResponse::Failure(m) => Err(Error::FailureResponse(m)),
TrezorResponse::ButtonRequest(_) => {
Err(Error::UnexpectedInteractionRequest(InteractionType::Button))
}
TrezorResponse::PinMatrixRequest(_) => {
Err(Error::UnexpectedInteractionRequest(InteractionType::PinMatrix))
}
}
}
}
pub fn handle_interaction<T, R: TrezorMessage>(resp: TrezorResponse<T, R>) -> Result<T> {
match resp {
TrezorResponse::Ok(res) => Ok(res),
TrezorResponse::Failure(_) => resp.ok(), TrezorResponse::ButtonRequest(req) => handle_interaction(req.ack()?),
TrezorResponse::PinMatrixRequest(_) => Err(Error::UnsupportedNetwork),
TrezorResponse::PassphraseRequest(req) => handle_interaction({
let on_device = req.on_device();
req.ack(!on_device)?
}),
}
}
pub struct EntropyRequest<'a> {
pub client: &'a mut Trezor,
}
impl<'a> EntropyRequest<'a> {
pub fn ack_entropy(self, entropy: Vec<u8>) -> Result<TrezorResponse<'a, (), protos::Success>> {
if entropy.len() != 32 {
return Err(Error::InvalidEntropy);
}
let mut req = protos::EntropyAck::new();
req.set_entropy(entropy);
self.client.call(req, Box::new(|_, _| Ok(())))
}
}