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
#[cfg(feature = "ledger")]
pub mod ledger;
#[cfg(feature = "specter")]
pub mod specter;
use async_trait::async_trait;
use bitcoin::util::{
bip32::{DerivationPath, ExtendedPubKey, Fingerprint},
psbt::PartiallySignedTransaction as Psbt,
};
use std::fmt::Debug;
use std::str::FromStr;
#[derive(Debug, Clone)]
pub enum Error {
UnsupportedVersion,
UnsupportedInput,
UnimplementedMethod,
DeviceDisconnected,
DeviceNotFound,
DeviceDidNotSign,
Device(String),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Error::UnsupportedVersion => write!(f, "Unsupported version"),
Error::UnsupportedInput => write!(f, "Unsupported input"),
Error::UnimplementedMethod => write!(f, "Unimplemented method"),
Error::DeviceDisconnected => write!(f, "Device disconnected"),
Error::DeviceNotFound => write!(f, "Device not found"),
Error::DeviceDidNotSign => write!(f, "Device did not sign"),
Error::Device(e) => write!(f, "{}", e),
}
}
}
#[async_trait]
pub trait HWI: Debug {
fn device_kind(&self) -> DeviceKind;
async fn get_version(&self) -> Result<Version, Error>;
async fn is_connected(&self) -> Result<(), Error>;
async fn get_master_fingerprint(&self) -> Result<Fingerprint, Error>;
async fn get_extended_pubkey(
&self,
path: &DerivationPath,
display: bool,
) -> Result<ExtendedPubKey, Error>;
async fn register_wallet(&self, name: &str, policy: &str) -> Result<Option<[u8; 32]>, Error>;
async fn sign_tx(&self, tx: &mut Psbt) -> Result<(), Error>;
}
#[derive(Debug, Clone)]
pub struct Version {
pub major: u32,
pub minor: u32,
pub patch: u32,
}
impl std::fmt::Display for Version {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
}
}
impl FromStr for Version {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let numbers: Vec<&str> = s.trim_matches('v').split('.').collect();
let (major, minor, patch) = if numbers.len() == 3 {
(numbers[0], numbers[1], numbers[2])
} else if numbers.len() == 2 {
(numbers[0], numbers[1], "0")
} else {
return Err(Error::UnsupportedVersion);
};
Ok(Version {
major: u32::from_str(major).map_err(|_| Error::UnsupportedVersion)?,
minor: u32::from_str(minor).map_err(|_| Error::UnsupportedVersion)?,
patch: u32::from_str(patch).map_err(|_| Error::UnsupportedVersion)?,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeviceKind {
Specter,
SpecterSimulator,
Ledger,
LedgerSimulator,
}
impl std::fmt::Display for DeviceKind {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
DeviceKind::Specter => write!(f, "specter"),
DeviceKind::SpecterSimulator => write!(f, "specter-simulator"),
DeviceKind::Ledger => write!(f, "ledger"),
DeviceKind::LedgerSimulator => write!(f, "ledger-simulator"),
}
}
}
impl std::str::FromStr for DeviceKind {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"specter" => Ok(DeviceKind::Specter),
"specter-simulator" => Ok(DeviceKind::SpecterSimulator),
"ledger" => Ok(DeviceKind::Ledger),
"ledger-simulator" => Ok(DeviceKind::LedgerSimulator),
_ => Err(()),
}
}
}