Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pin commands #90

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ impl HWIClient {
arg,
r#"
import logging
logging.basicConfig(level=arg)
logging.basicConfig(level=arg)
"#
);
Ok(())
Expand Down Expand Up @@ -526,4 +526,32 @@ impl HWIClient {
))
}
}

pub fn prompt_pin(&self) -> Result<(), Error> {
Python::with_gil(|py| {
let func_args = (&self.hw_client,);
let output = self
.hwilib
.commands
.getattr(py, "prompt_pin")?
.call1(py, func_args)?;
let output = self.hwilib.json_dumps.call1(py, (output,))?;
let status: HWIStatus = deserialize_obj!(&output.to_string())?;
status.into()
})
}

pub fn send_pin(&self, pin: String) -> Result<(), Error> {
Python::with_gil(|py| {
let func_args = (&self.hw_client, pin);
let output = self
.hwilib
.commands
.getattr(py, "send_pin")?
.call1(py, func_args)?;
let output = self.hwilib.json_dumps.call1(py, (output,))?;
let status: HWIStatus = deserialize_obj!(&output.to_string())?;
status.into()
})
}
}
56 changes: 55 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod tests {
use crate::types::{self, HWIDeviceType, TESTNET};
use crate::HWIClient;
use std::collections::BTreeMap;
use std::io::stdin;
use std::str::FromStr;

use bitcoin::bip32::{DerivationPath, KeySource};
Expand Down Expand Up @@ -208,7 +209,10 @@ mod tests {
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));
hd_keypaths.insert(
pk.public_key,
(device.fingerprint.unwrap(), derivation_path),
);

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

Expand Down Expand Up @@ -440,4 +444,54 @@ mod tests {
fn test_install_hwi() {
HWIClient::install_hwilib(Some("2.1.1")).unwrap();
}

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

#[test]
#[serial]
fn test_send_pin() {
let devices = HWIClient::enumerate().unwrap();
let unsupported = [
HWIDeviceType::Ledger,
HWIDeviceType::Coldcard,
HWIDeviceType::Jade,
HWIDeviceType::BitBox01,
HWIDeviceType::BitBox02,
];
for device in devices {
let device = device.unwrap();
if unsupported.contains(&device.device_type) {
// These devices don't support send_pin
continue;
}
// Set the device to a state where pin can be sent
let client = HWIClient::get_client(&device, true, TESTNET).unwrap();
client.prompt_pin().unwrap();

let mut s = String::new();
stdin().read_line(&mut s).expect("Please Enter Pin");
let device_pin: String = s.split_whitespace().map(str::to_string).collect();
client.send_pin(String::from(device_pin)).unwrap();
}
}
}
51 changes: 32 additions & 19 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,33 +181,46 @@ pub struct HWIDevice {
pub path: String,
pub needs_pin_sent: bool,
pub needs_passphrase_sent: bool,
pub fingerprint: Fingerprint,
pub fingerprint: Option<Fingerprint>,
}

impl TryFrom<HWIDeviceInternal> for HWIDevice {
type Error = Error;

fn try_from(h: HWIDeviceInternal) -> Result<HWIDevice, Error> {
match h.error {
let needs_pin_sent = h.needs_pin_sent.unwrap_or_default();
let result = match h.error {
Some(e) => {
let code = h.code.and_then(|c| ErrorCode::try_from(c).ok());
Err(Error::Hwi(e, code))
if code == Some(ErrorCode::DeviceNotReady) && needs_pin_sent {
None
} else {
Some(Error::Hwi(e, code))
}
}
None => None,
};
match result {
Some(error) => Err(error),
None => {
let fingerprint = if needs_pin_sent {
None
} else {
Some(h.fingerprint.expect("Fingerprint should be here"))
};
Ok(HWIDevice {
device_type: HWIDeviceType::from(
h.device_type.expect("Device type should be here"),
),
model: h.model.expect("Model should be here"),
path: h.path.expect("Path should be here"),
needs_pin_sent: h.needs_pin_sent.expect("needs_pin_sent should be here"),
needs_passphrase_sent: h
.needs_passphrase_sent
.expect("needs_passphrase_sent should be here"),
fingerprint,
})
}
// When HWIDeviceInternal contains errors, some fields might be missing
// (depending on the error, hwi might not be able to know all of them).
// When there's no error though, all the fields must be present, and
// for this reason we expect here.
None => Ok(HWIDevice {
device_type: HWIDeviceType::from(
h.device_type.expect("Device type should be here"),
),
model: h.model.expect("Model should be here"),
path: h.path.expect("Path should be here"),
needs_pin_sent: h.needs_pin_sent.expect("needs_pin_sent should be here"),
needs_passphrase_sent: h
.needs_passphrase_sent
.expect("needs_passphrase_sent should be here"),
fingerprint: h.fingerprint.expect("Fingerprint should be here"),
}),
}
}
}
Expand Down
Loading