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
pub mod command {
    use async_hwi::{
        bitbox::{api::runtime, BitBox02, PairingBitbox02WithLocalCache},
        coldcard,
        jade::{self, Jade},
        ledger::{HidApi, Ledger, LedgerSimulator, TransportHID},
        specter::{Specter, SpecterSimulator},
        HWI,
    };
    use bitcoin::{hashes::hex::FromHex, Network};
    use std::error::Error;

    pub struct Wallet<'a> {
        pub name: Option<&'a String>,
        pub policy: Option<&'a String>,
        pub hmac: Option<&'a String>,
    }

    pub async fn list(
        network: Network,
        wallet: Option<Wallet<'_>>,
    ) -> Result<Vec<Box<dyn HWI + Send>>, Box<dyn Error>> {
        let mut hws = Vec::new();

        if let Ok(device) = SpecterSimulator::try_connect().await {
            hws.push(device.into());
        }

        if let Ok(devices) = Specter::enumerate().await {
            for device in devices {
                hws.push(device.into());
            }
        }

        match Jade::enumerate().await {
            Err(e) => println!("{:?}", e),
            Ok(devices) => {
                for device in devices {
                    let device = device.with_network(network);
                    if let Ok(info) = device.get_info().await {
                        if info.jade_state == jade::api::JadeState::Locked {
                            if let Err(e) = device.auth().await {
                                eprintln!("auth {:?}", e);
                                continue;
                            }
                        }

                        hws.push(device.into());
                    }
                }
            }
        }

        if let Ok(device) = LedgerSimulator::try_connect().await {
            hws.push(device.into());
        }

        let api = Box::new(HidApi::new().unwrap());

        for device_info in api.device_list() {
            if async_hwi::bitbox::is_bitbox02(device_info) {
                if let Ok(device) = device_info.open_device(&api) {
                    if let Ok(device) =
                        PairingBitbox02WithLocalCache::<runtime::TokioRuntime>::connect(
                            device, None,
                        )
                        .await
                    {
                        if let Ok((device, _)) = device.wait_confirm().await {
                            let mut bb02 = BitBox02::from(device).with_network(network);
                            if let Some(ref policy) = wallet.as_ref().map(|w| w.policy).flatten() {
                                bb02 = bb02.with_policy(policy)?;
                            }
                            hws.push(bb02.into());
                        }
                    }
                }
            }
            if device_info.vendor_id() == coldcard::api::COINKITE_VID
                && device_info.product_id() == coldcard::api::CKCC_PID
            {
                if let Some(sn) = device_info.serial_number() {
                    if let Ok((cc, _)) = coldcard::api::Coldcard::open(&api, sn, None) {
                        let mut hw = coldcard::Coldcard::from(cc);
                        if let Some(ref wallet) = wallet {
                            hw = hw.with_wallet_name(
                                wallet
                                    .name
                                    .ok_or::<Box<dyn Error>>(
                                        "coldcard requires a wallet name".into(),
                                    )?
                                    .to_string(),
                            );
                        }
                        hws.push(hw.into())
                    }
                }
            }
        }

        for detected in Ledger::<TransportHID>::enumerate(&api) {
            if let Ok(mut device) = Ledger::<TransportHID>::connect(&api, detected) {
                if let Some(ref wallet) = wallet {
                    let hmac = if let Some(s) = wallet.hmac {
                        let mut h = [b'\0'; 32];
                        h.copy_from_slice(&Vec::from_hex(s)?);
                        Some(h)
                    } else {
                        None
                    };
                    device = device.with_wallet(
                        wallet
                            .name
                            .ok_or::<Box<dyn Error>>("ledger requires a wallet name".into())?,
                        wallet
                            .policy
                            .ok_or::<Box<dyn Error>>("ledger requires a wallet policy".into())?,
                        hmac,
                    )?;
                }
                hws.push(device.into());
            }
        }

        Ok(hws)
    }
}