Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
closed_device => unlocked_device
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasad1 committed Feb 13, 2018
1 parent 6b1275d commit a3704c8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
18 changes: 11 additions & 7 deletions hw/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ pub enum Error {
KeyNotFound,
/// Signing has been cancelled by user.
UserCancel,
/// Invalid Product
InvalidProduct,
/// Invalid Device
InvalidDevice,
}

impl fmt::Display for Error {
Expand All @@ -75,7 +75,7 @@ impl fmt::Display for Error {
Error::LibUsb(ref e) => write!(f, "LibUSB communication error: {}", e),
Error::KeyNotFound => write!(f, "Key not found"),
Error::UserCancel => write!(f, "Operation has been cancelled"),
Error::InvalidProduct=> write!(f, "Unsupported product was entered"),
Error::InvalidDevice => write!(f, "Unsupported product was entered"),
}
}
}
Expand Down Expand Up @@ -354,10 +354,14 @@ impl EventHandler {
}

fn is_valid_product(&self, device: &libusb::Device) -> Result<(), Error> {
let product_id = device.device_descriptor()?.product_id();
match LEDGER_PIDS.contains(&product_id) {
true => Ok(()),
false => Err(Error::InvalidProduct),
let desc = device.device_descriptor()?;
let vendor_id = desc.vendor_id();
let product_id = desc.product_id();

if vendor_id == LEDGER_VID && LEDGER_PIDS.contains(&product_id) {
Ok(())
} else {
Err(Error::InvalidDevice)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions hw/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,16 @@ impl HardwareWalletManager {
let trezor = Arc::new(trezor::Manager::new(hidapi.clone()));

// Subscribe to TREZOR V1
// Note this support only TREZOR V1 becasue TREZOR V2 has another vendorID for some reason
// Because we now only support one product only subscribe to it as the second argument `Some(1)` specifies
// Note, this support only TREZOR V1 becasue TREZOR V2 has another vendorID for some reason
// Also, we now only support one product as the second argument specifies
usb_context_trezor.register_callback(
Some(trezor::TREZOR_VID), Some(trezor::TREZOR_PIDS[0]), Some(USB_DEVICE_CLASS_DEVICE),
Box::new(trezor::EventHandler::new(Arc::downgrade(&trezor))))?;

// Subscribe to all Ledger Devices
// This means that we need to check that the given productID is supported
// None -> LIBUSB_HOTPLUG_MATCH_ANY
// http://libusb.sourceforge.net/api-1.0/group__hotplug.html#gae6c5f1add6cc754005549c7259dc35ea
// None => LIBUSB_HOTPLUG_MATCH_ANY, in other words that all are subscribed to
// More info can be found: http://libusb.sourceforge.net/api-1.0/group__hotplug.html#gae6c5f1add6cc754005549c7259dc35ea
usb_context_ledger.register_callback(
Some(ledger::LEDGER_VID), None, Some(USB_DEVICE_CLASS_DEVICE),
Box::new(ledger::EventHandler::new(Arc::downgrade(&ledger))))?;
Expand Down
20 changes: 11 additions & 9 deletions hw/src/trezor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl From<protobuf::ProtobufError> for Error {
pub struct Manager {
usb: Arc<Mutex<hidapi::HidApi>>,
devices: RwLock<Vec<Device>>,
closed_devices: RwLock<Vec<String>>,
locked_devices: RwLock<Vec<String>>,
key_path: RwLock<KeyPath>,
}

Expand All @@ -113,7 +113,7 @@ impl Manager {
Manager {
usb: hidapi,
devices: RwLock::new(Vec::new()),
closed_devices: RwLock::new(Vec::new()),
locked_devices: RwLock::new(Vec::new()),
key_path: RwLock::new(KeyPath::Ethereum),
}
}
Expand All @@ -124,7 +124,7 @@ impl Manager {
usb.refresh_devices();
let devices = usb.devices();
let mut new_devices = Vec::new();
let mut closed_devices = Vec::new();
let mut locked_devices = Vec::new();
let mut error = None;
for usb_device in devices {
let is_trezor = usb_device.vendor_id == TREZOR_VID;
Expand All @@ -143,17 +143,17 @@ impl Manager {
}
match self.read_device_info(&usb, &usb_device) {
Ok(device) => new_devices.push(device),
Err(Error::ClosedDevice(path)) => closed_devices.push(path.to_string()),
Err(Error::ClosedDevice(path)) => locked_devices.push(path.to_string()),
Err(e) => {
warn!("Error reading device: {:?}", e);
error = Some(e);
}
}
}
let count = new_devices.len();
trace!("Got devices: {:?}, closed: {:?}", new_devices, closed_devices);
trace!("Got devices: {:?}, closed: {:?}", new_devices, locked_devices);
*self.devices.write() = new_devices;
*self.closed_devices.write() = closed_devices;
*self.locked_devices.write() = locked_devices;
match error {
Some(e) => Err(e),
None => Ok(count),
Expand Down Expand Up @@ -193,7 +193,7 @@ impl Manager {
}

pub fn list_locked_devices(&self) -> Vec<String> {
(*self.closed_devices.read()).clone()
(*self.locked_devices.read()).clone()
}

/// Get wallet info.
Expand Down Expand Up @@ -418,11 +418,14 @@ impl libusb::Hotplug for EventHandler {
fn device_arrived(&mut self, _device: libusb::Device) {
println!("trezor arrived");
if let Some(t) = self.trezor.upgrade() {
// Wait for the device to boot up
// Wait for the device to boot-up
thread::sleep(Duration::from_millis(1000));
if let Err(e) = t.update_devices() {
debug!("TREZOR Connect Error: {:?}", e);
}
println!("unlocked devices: {:?}", t.list_devices());
println!("locked devices: {:?}", t.list_locked_devices());

}
}

Expand All @@ -436,7 +439,6 @@ impl libusb::Hotplug for EventHandler {
}
}


#[test]
#[ignore]
/// This test can't be run without an actual trezor device connected
Expand Down

0 comments on commit a3704c8

Please sign in to comment.