-
-
Notifications
You must be signed in to change notification settings - Fork 665
/
interaction.rs
57 lines (51 loc) · 1.89 KB
/
interaction.rs
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
use std::io;
use bitcoin::{bip32, network::Network, Address};
use trezor_client::{Error, TrezorMessage, TrezorResponse};
fn handle_interaction<T, R: TrezorMessage>(resp: TrezorResponse<T, R>) -> Result<T, Error> {
match resp {
TrezorResponse::Ok(res) => Ok(res),
TrezorResponse::Failure(_) => resp.ok(), // assering ok() returns the failure error
TrezorResponse::ButtonRequest(req) => handle_interaction(req.ack()?),
TrezorResponse::PinMatrixRequest(req) => {
println!("Enter PIN");
let mut pin = String::new();
if io::stdin().read_line(&mut pin).unwrap() != 5 {
println!("must enter pin, received: {}", pin);
}
// trim newline
handle_interaction(req.ack_pin(pin[..4].to_owned())?)
}
TrezorResponse::PassphraseRequest(req) => {
println!("Enter passphrase");
let mut pass = String::new();
io::stdin().read_line(&mut pass).unwrap();
// trim newline
handle_interaction(req.ack_passphrase(pass[..pass.len() - 1].to_owned())?)
}
}
}
fn do_main() -> Result<(), trezor_client::Error> {
// init with debugging
let mut trezor = trezor_client::unique(true)?;
trezor.init_device(None)?;
let xpub = handle_interaction(
trezor.get_public_key(
&vec![
bip32::ChildNumber::from_hardened_idx(0).unwrap(),
bip32::ChildNumber::from_hardened_idx(0).unwrap(),
bip32::ChildNumber::from_hardened_idx(0).unwrap(),
]
.into(),
trezor_client::protos::InputScriptType::SPENDADDRESS,
Network::Testnet,
true,
)?,
)?;
println!("{}", xpub);
println!("{:?}", xpub);
println!("{}", Address::p2pkh(&xpub.to_pub(), Network::Testnet));
Ok(())
}
fn main() {
do_main().unwrap()
}