Skip to content

Commit 1bdbcbc

Browse files
committed
Add prompt_pin and send_pin HWI command
1 parent 5e88904 commit 1bdbcbc

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

src/interface.rs

+30
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,36 @@ impl HWIClient {
497497
})
498498
}
499499

500+
/// Prompt PIN on a device
501+
pub fn prompt_pin(&self) -> Result<(), Error> {
502+
Python::with_gil(|py| {
503+
let func_args = (&self.hw_client,);
504+
let output = self
505+
.hwilib
506+
.commands
507+
.getattr(py, "prompt_pin")?
508+
.call1(py, func_args)?;
509+
let output = self.hwilib.json_dumps.call1(py, (output,))?;
510+
let status: HWIStatus = deserialize_obj!(&output.to_string())?;
511+
status.into()
512+
})
513+
}
514+
515+
/// Send PIN to a device
516+
pub fn send_pin(&self, pin: &str) -> Result<(), Error> {
517+
Python::with_gil(|py| {
518+
let func_args = (&self.hw_client, pin);
519+
let output = self
520+
.hwilib
521+
.commands
522+
.getattr(py, "send_pin")?
523+
.call1(py, func_args)?;
524+
let output = self.hwilib.json_dumps.call1(py, (output,))?;
525+
let status: HWIStatus = deserialize_obj!(&output.to_string())?;
526+
status.into()
527+
})
528+
}
529+
500530
/// Get the installed version of hwilib. Returns None if hwi is not installed.
501531
pub fn get_version() -> Option<String> {
502532
Python::with_gil(|py| {

src/lib.rs

+37-2
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ pub mod types;
4141

4242
#[cfg(test)]
4343
mod tests {
44-
use crate::types::{self, HWIDeviceType, TESTNET};
44+
use crate::types::{self, HWIChain, HWIDevice, HWIDeviceType, TESTNET};
4545
use crate::HWIClient;
4646
use std::collections::BTreeMap;
4747
use std::str::FromStr;
4848

49-
use bitcoin::bip32::{DerivationPath, KeySource};
49+
use bitcoin::bip32::{DerivationPath, Fingerprint, KeySource};
5050
use bitcoin::locktime::absolute;
5151
use bitcoin::psbt::{Input, Output};
5252
use bitcoin::{secp256k1, Transaction};
@@ -435,6 +435,41 @@ mod tests {
435435
}
436436
}
437437

438+
#[test]
439+
#[serial]
440+
#[ignore = "Needs a Trezor One device for the test"]
441+
fn test_prompt_pin_to_trezor_device() {
442+
let client = HWIClient::find_device(
443+
None,
444+
Some(HWIDeviceType::Trezor),
445+
None,
446+
false,
447+
Network::Testnet,
448+
)
449+
.unwrap();
450+
client.prompt_pin().unwrap();
451+
}
452+
453+
#[test]
454+
#[serial]
455+
#[ignore = "Needs a Trezor One device for the test"]
456+
fn test_send_pin_to_trezor_device() {
457+
let client = HWIClient::get_client(
458+
&HWIDevice {
459+
device_type: HWIDeviceType::Trezor,
460+
model: "trezor_1".to_string(),
461+
path: "webusb:000:1:2".to_string(),
462+
needs_pin_sent: true,
463+
needs_passphrase_sent: false,
464+
fingerprint: Fingerprint::from_str("00000000").unwrap(),
465+
},
466+
false,
467+
HWIChain::from(Network::Testnet),
468+
)
469+
.unwrap();
470+
client.send_pin("123456").unwrap();
471+
}
472+
438473
#[test]
439474
#[serial]
440475
#[ignore]

0 commit comments

Comments
 (0)