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
use bitcoin::network::constants::Network; use bitcoin::util::bip32;
use bitcoin::util::psbt;
use bitcoin::Address;
use flows::sign_tx::SignTxProgress;
use secp256k1;
use unicode_normalization::UnicodeNormalization;
use utils;
use protos;
pub use protos::InputScriptType;
use super::{Trezor, TrezorResponse};
use crate::error::Result;
impl Trezor {
pub fn get_public_key(
&mut self,
path: &bip32::DerivationPath,
script_type: InputScriptType,
network: Network,
show_display: bool,
) -> Result<TrezorResponse<bip32::ExtendedPubKey, protos::PublicKey>> {
let mut req = protos::GetPublicKey::new();
req.set_address_n(utils::convert_path(path));
req.set_show_display(show_display);
req.set_coin_name(utils::coin_name(network)?);
req.set_script_type(script_type);
self.call(req, Box::new(|_, m| Ok(m.get_xpub().parse()?)))
}
pub fn get_address(
&mut self,
path: &bip32::DerivationPath,
script_type: InputScriptType,
network: Network,
show_display: bool,
) -> Result<TrezorResponse<Address, protos::Address>> {
let mut req = protos::GetAddress::new();
req.set_address_n(utils::convert_path(path));
req.set_coin_name(utils::coin_name(network)?);
req.set_show_display(show_display);
req.set_script_type(script_type);
self.call(req, Box::new(|_, m| Ok(m.get_address().parse()?)))
}
pub fn sign_tx(
&mut self,
psbt: &psbt::PartiallySignedTransaction,
network: Network,
) -> Result<TrezorResponse<SignTxProgress, protos::TxRequest>> {
let tx = &psbt.global.unsigned_tx;
let mut req = protos::SignTx::new();
req.set_inputs_count(tx.input.len() as u32);
req.set_outputs_count(tx.output.len() as u32);
req.set_coin_name(utils::coin_name(network)?);
req.set_version(tx.version);
req.set_lock_time(tx.lock_time);
self.call(req, Box::new(|c, m| Ok(SignTxProgress::new(c, m))))
}
pub fn sign_message(
&mut self,
message: String,
path: &bip32::DerivationPath,
script_type: InputScriptType,
network: Network,
) -> Result<TrezorResponse<(Address, secp256k1::RecoverableSignature), protos::MessageSignature>>
{
let mut req = protos::SignMessage::new();
req.set_address_n(utils::convert_path(path));
let msg_bytes = message.nfc().collect::<String>().into_bytes();
req.set_message(msg_bytes);
req.set_coin_name(utils::coin_name(network)?);
req.set_script_type(script_type);
self.call(
req,
Box::new(|_, m| {
let address = m.get_address().parse()?;
let signature = utils::parse_recoverable_signature(m.get_signature())?;
Ok((address, signature))
}),
)
}
}