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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Rust wrapper for the [Bitcoin Hardware Wallet Interface](https://github.com/bitcoin-core/HWI/).
//!
//! # Example - display address with path
//! ```no_run
//! use bitcoin::bip32::{ChildNumber, DerivationPath};
//! use hwi::error::Error;
//! use hwi::interface::HWIClient;
//! use hwi::types;
//! use std::str::FromStr;
//!
//! fn main() -> Result<(), Error> {
//!     // Find information about devices
//!     let mut devices = HWIClient::enumerate()?;
//!     if devices.is_empty() {
//!         panic!("No device found!");
//!     }
//!     let device = devices.remove(0)?;
//!     // Create a client for a device
//!     let client = HWIClient::get_client(&device, true, bitcoin::Network::Testnet.into())?;
//!     // Display the address from path
//!     let derivation_path = DerivationPath::from_str("m/44'/1'/0'/0/0").unwrap();
//!     let hwi_address =
//!         client.display_address_with_path(&derivation_path, types::HWIAddressType::Tap)?;
//!     println!("{}", hwi_address.address.assume_checked());
//!     Ok(())
//! }
//! ```

#[cfg(test)]
#[macro_use]
extern crate serial_test;

pub use interface::HWIClient;

#[cfg(feature = "doctest")]
pub mod doctest;
pub mod error;
pub mod interface;
pub mod types;

#[cfg(test)]
mod tests {
    use crate::types::{self, HWIDeviceType, TESTNET};
    use crate::HWIClient;
    use std::collections::BTreeMap;
    use std::str::FromStr;

    use bitcoin::bip32::{DerivationPath, KeySource};
    use bitcoin::locktime::absolute;
    use bitcoin::psbt::{Input, Output};
    use bitcoin::{secp256k1, Transaction};
    use bitcoin::{transaction, Amount, Network, TxIn, TxOut};

    #[cfg(feature = "miniscript")]
    use miniscript::{Descriptor, DescriptorPublicKey};

    #[test]
    #[serial]
    fn test_enumerate() {
        let devices = HWIClient::enumerate().unwrap();
        assert!(devices.len() > 0);
    }

    #[test]
    #[serial]
    #[ignore]
    fn test_find_trezor_device() {
        HWIClient::find_device(
            None,
            Some(HWIDeviceType::Trezor),
            None,
            false,
            Network::Testnet,
        )
        .unwrap();
    }

    fn get_first_device() -> HWIClient {
        let devices = HWIClient::enumerate().unwrap();
        let device = devices
            .first()
            .expect("No devices found. Either plug in a hardware wallet, or start a simulator.")
            .as_ref()
            .expect("Error when opening the first device");
        HWIClient::get_client(&device, true, TESTNET).unwrap()
    }

    #[test]
    #[serial]
    fn test_get_master_xpub() {
        let client = get_first_device();
        client
            .get_master_xpub(types::HWIAddressType::Wit, 0)
            .unwrap();
    }

    #[test]
    #[serial]
    fn test_get_xpub() {
        let client = get_first_device();
        let derivation_path = DerivationPath::from_str("m/44'/1'/0'/0/0").unwrap();
        client.get_xpub(&derivation_path, false).unwrap();
    }

    #[test]
    #[serial]
    fn test_sign_message() {
        let client = get_first_device();
        let derivation_path = DerivationPath::from_str("m/44'/1'/0'/0/0").unwrap();
        client
            .sign_message("I love BDK wallet", &derivation_path)
            .unwrap();
    }

    #[test]
    #[serial]
    fn test_get_string_descriptors() {
        let client = get_first_device();
        let account = Some(10);
        let descriptor = client.get_descriptors::<String>(account).unwrap();
        assert!(descriptor.internal.len() > 0);
        assert!(descriptor.receive.len() > 0);
    }

    #[test]
    #[serial]
    fn test_display_address_with_string_desc() {
        let client = get_first_device();
        let descriptor = client.get_descriptors::<String>(None).unwrap();
        let descriptor = descriptor.receive.first().unwrap();
        client.display_address_with_desc(descriptor).unwrap();
    }

    #[test]
    #[serial]
    #[cfg(feature = "miniscript")]
    fn test_get_miniscript_descriptors() {
        let client = get_first_device();
        let account = Some(10);
        let descriptor = client
            .get_descriptors::<Descriptor<DescriptorPublicKey>>(account)
            .unwrap();
        assert!(descriptor.internal.len() > 0);
        assert!(descriptor.receive.len() > 0);
    }

    #[test]
    #[serial]
    #[cfg(feature = "miniscript")]
    fn test_display_address_with_miniscript_desc() {
        let client = get_first_device();
        let descriptor = client
            .get_descriptors::<Descriptor<DescriptorPublicKey>>(None)
            .unwrap();
        let descriptor = descriptor.receive.first().unwrap();
        client.display_address_with_desc(descriptor).unwrap();
    }

    #[test]
    #[serial]
    fn test_display_address_with_path_legacy() {
        let client = get_first_device();
        let derivation_path = DerivationPath::from_str("m/44'/1'/0'/0/0").unwrap();
        client
            .display_address_with_path(&derivation_path, types::HWIAddressType::Legacy)
            .unwrap();
    }

    #[test]
    #[serial]
    fn test_display_address_with_path_nested_segwit() {
        let client = get_first_device();
        let derivation_path = DerivationPath::from_str("m/49'/1'/0'/0/0").unwrap();

        client
            .display_address_with_path(&derivation_path, types::HWIAddressType::Sh_Wit)
            .unwrap();
    }

    #[test]
    #[serial]
    fn test_display_address_with_path_native_segwit() {
        let client = get_first_device();
        let derivation_path = DerivationPath::from_str("m/84'/1'/0'/0/0").unwrap();

        client
            .display_address_with_path(&derivation_path, types::HWIAddressType::Wit)
            .unwrap();
    }

    // TODO: HWI 2.0.2 doesn't support displayaddress with taproot
    // #[test]
    // #[serial]
    // fn test_display_address_with_path_taproot() {}

    #[test]
    #[serial]
    fn test_sign_tx() {
        let devices = HWIClient::enumerate().unwrap();
        let device = devices.first().unwrap().as_ref().unwrap();
        let client = HWIClient::get_client(&device, true, TESTNET).unwrap();
        let derivation_path = DerivationPath::from_str("m/44'/1'/0'/0/0").unwrap();

        let address = client
            .display_address_with_path(&derivation_path, types::HWIAddressType::Legacy)
            .unwrap();

        let pk = client.get_xpub(&derivation_path, true).unwrap();
        let mut hd_keypaths: BTreeMap<secp256k1::PublicKey, KeySource> = Default::default();
        // Here device fingerprint is same as master xpub fingerprint
        hd_keypaths.insert(pk.public_key, (device.fingerprint, derivation_path));

        let script_pubkey = address.address.assume_checked().script_pubkey();

        let previous_tx = Transaction {
            version: transaction::Version::ONE,
            lock_time: absolute::LockTime::from_consensus(0),
            input: vec![TxIn::default()],
            output: vec![TxOut {
                value: Amount::from_sat(100),
                script_pubkey: script_pubkey.clone(),
            }],
        };

        let previous_txin = TxIn {
            previous_output: bitcoin::OutPoint {
                txid: previous_tx.txid(),
                vout: Default::default(),
            },
            ..Default::default()
        };
        let psbt = bitcoin::Psbt {
            unsigned_tx: Transaction {
                version: transaction::Version::ONE,
                lock_time: absolute::LockTime::from_consensus(0),
                input: vec![previous_txin],
                output: vec![TxOut {
                    value: Amount::from_sat(50),
                    script_pubkey: script_pubkey,
                }],
            },
            xpub: Default::default(),
            version: Default::default(),
            proprietary: Default::default(),
            unknown: Default::default(),

            inputs: vec![Input {
                non_witness_utxo: Some(previous_tx),
                witness_utxo: None,
                bip32_derivation: hd_keypaths,
                ..Default::default()
            }],
            outputs: vec![Output::default()],
        };
        let client = get_first_device();
        client.sign_tx(&psbt).unwrap();
    }

    #[test]
    #[serial]
    fn test_get_keypool() {
        let client = get_first_device();
        let keypool = true;
        let internal = false;
        let address_type = types::HWIAddressType::Legacy;
        let account = Some(8);
        let derivation_path = DerivationPath::from_str("m/44'/1'/0'").unwrap();
        let start = 1;
        let end = 5;
        client
            .get_keypool(
                keypool,
                internal,
                address_type,
                false,
                account,
                Some(&derivation_path),
                start,
                end,
            )
            .unwrap();

        let keypool = true;
        let internal = true;
        let address_type = types::HWIAddressType::Wit;
        let account = None;
        let start = 1;
        let end = 8;
        client
            .get_keypool(
                keypool,
                internal,
                address_type,
                false,
                account,
                None,
                start,
                end,
            )
            .unwrap();

        let keypool = false;
        let internal = true;
        let address_type = types::HWIAddressType::Sh_Wit;
        let account = Some(1);
        let start = 0;
        let end = 10;
        client
            .get_keypool(
                keypool,
                internal,
                address_type,
                false,
                account,
                Some(&derivation_path),
                start,
                end,
            )
            .unwrap();
    }

    #[test]
    #[serial]
    #[ignore]
    fn test_install_udev_rules() {
        if cfg!(target_os = "linux") {
            HWIClient::install_udev_rules(None, None).unwrap()
        }
    }

    #[test]
    #[serial]
    fn test_set_log_level() {
        HWIClient::set_log_level(types::LogLevel::DEBUG).unwrap();
        test_enumerate();
    }

    #[test]
    #[serial]
    fn test_toggle_passphrase() {
        let devices = HWIClient::enumerate().unwrap();
        let unsupported = [
            HWIDeviceType::Ledger,
            HWIDeviceType::BitBox01,
            HWIDeviceType::Coldcard,
            HWIDeviceType::Jade,
        ];
        for device in devices {
            let device = device.unwrap();
            if unsupported.contains(&device.device_type) {
                // These devices don't support togglepassphrase
                continue;
            }
            let client = HWIClient::get_client(&device, true, TESTNET).unwrap();
            client.toggle_passphrase().unwrap();
            break;
        }
    }

    #[test]
    #[serial]
    fn test_get_version() {
        HWIClient::get_version().unwrap();
    }

    #[test]
    #[serial]
    #[ignore]
    // At the moment (hwi v2.1.1 and trezor-firmware core v2.5.2) work only with physical devices and NOT emulators!
    fn test_setup_trezor_device() {
        let client = HWIClient::find_device(
            None,
            Some(HWIDeviceType::Trezor),
            None,
            false,
            Network::Testnet,
        )
        .unwrap();
        client.setup_device(Some("My Label"), None).unwrap();
    }

    #[test]
    #[serial]
    #[ignore]
    // At the moment (hwi v2.1.1 and trezor-firmware core v2.5.2) work only with physical devices and NOT emulators!
    fn test_restore_trezor_device() {
        let client = HWIClient::find_device(
            None,
            Some(HWIDeviceType::Trezor),
            None,
            false,
            Network::Testnet,
        )
        .unwrap();
        client.restore_device(Some("My Label"), None).unwrap();
    }

    #[test]
    #[serial]
    fn test_backup_device() {
        let devices = HWIClient::enumerate().unwrap();
        let supported = [
            HWIDeviceType::BitBox01,
            HWIDeviceType::BitBox02,
            HWIDeviceType::Coldcard,
        ];
        for device in devices {
            let device = device.unwrap();
            if supported.contains(&device.device_type) {
                let client = HWIClient::get_client(&device, true, TESTNET).unwrap();
                client.backup_device(Some("My Label"), None).unwrap();
            }
        }
    }

    #[test]
    #[serial]
    #[ignore]
    fn test_wipe_device() {
        let devices = HWIClient::enumerate().unwrap();
        let unsupported = [
            HWIDeviceType::Ledger,
            HWIDeviceType::Coldcard,
            HWIDeviceType::Jade,
        ];
        for device in devices {
            let device = device.unwrap();
            if unsupported.contains(&device.device_type) {
                // These devices don't support wipe
                continue;
            }
            let client = HWIClient::get_client(&device, true, TESTNET).unwrap();
            client.wipe_device().unwrap();
        }
    }

    #[test]
    #[serial]
    #[ignore]
    fn test_install_hwi() {
        HWIClient::install_hwilib(Some("2.1.1")).unwrap();
    }
}