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

Disable hardware-wallets on platforms that don't support libusb #8464

Merged
merged 8 commits into from
Jun 26, 2018

Conversation

niklasad1
Copy link
Collaborator

@niklasad1 niklasad1 commented Apr 23, 2018

Hey,

This PR tries to add functionality to build parity on platforms that don't support libusb or is stable by using conditional compilation i.e,

  • libusb support -> #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
  • not libusb support -> #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]

This is really brute-force and the syntax is horrible which may be messy to read!

However, I want to know if this is something we want before putting more time on this. Moreover, this makes the --no-hardware-wallets useless on these platform because it is already disabled
arm-linux-androideabi

I have tested it by running (without errors):

$ cargo check --target x86_64-unknown-freebsd

If any FreeBSD user can test this I would appreciate it!

/cc #1425

@niklasad1 niklasad1 added A0-pleasereview 🤓 Pull request needs code review. M4-core ⛓ Core client code / Rust. M5-dependencies 🖇 Dependencies. labels Apr 23, 2018
@folsen
Copy link
Contributor

folsen commented Apr 24, 2018

That's a lot of extra noise for all that stuff. I think it's desirable for sure, but I'm not sure about where the tradeoff makes sense in terms of code ugliness/maintainability vs being able to disable it. I'm not sure what's possible theoretically in making this prettier?

Ultimately going the way of geth and having a completely separate account mgmt binary (including hw wallets) might be a good idea.

@5chdn 5chdn added this to the 1.11 milestone Apr 24, 2018
@niklasad1
Copy link
Collaborator Author

niklasad1 commented Apr 24, 2018

Right, I think it should be possible simply #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android"))] to something like #[cfg(feature = "hw_wallet")] is still noisy but a little bit better!

By using features in the crates that is using based on the target:

[target.'cfg(target_os = "linux")'.dependencies]
rpc = { path = "...", features = ["hw_wallet"] }

[target.'cfg(not(target_os = "linux"))'.dependencies]
rpc = { path = "...", features = [] }

EDIT:
That don't work at the moment rust-lang/cargo#2524 😭

@5chdn 5chdn modified the milestones: 1.11, 1.12 Apr 24, 2018
@@ -130,6 +137,7 @@ pub struct AccountProvider {
/// Accounts unlocked with rolling tokens
transient_sstore: EthMultiStore,
/// Accounts in hardware wallets.
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android"))]
hardware_store: Option<HardwareWalletManager>,
Copy link
Collaborator

@tomusdrw tomusdrw May 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it better to just change a type to a no-op for the unsupported platforms? Like so:

#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android"))]
mod hw {
 pub use hardware_wallet::{Error as HardwareError, HardwareWalletManager, KeyPath, TransactionInfo};
}
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android")))]
mod hw {
  pub enum HardwareError;
  pub struct HardwareWalletManager;
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would reduce the annotations a bit so I'm in favour!

@tomusdrw tomusdrw added A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. and removed A0-pleasereview 🤓 Pull request needs code review. labels May 7, 2018
@niklasad1 niklasad1 force-pushed the disable-hw-wallet-on-platforms-notlibusb branch from d166498 to fcb7740 Compare May 13, 2018 20:56
use super::fmt;

#[derive(Debug)]
pub enum HardwareError {}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need docs


#[derive(Debug)]
pub enum HardwareError {}
pub struct HardwareWalletManager;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need docs

@5chdn 5chdn added A0-pleasereview 🤓 Pull request needs code review. and removed A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. labels May 31, 2018
@5chdn
Copy link
Contributor

5chdn commented May 31, 2018

Needs reviews.

@niklasad1 niklasad1 force-pushed the disable-hw-wallet-on-platforms-notlibusb branch 2 times, most recently from 4ec90e7 to b55b04d Compare June 1, 2018 16:15

use super::{fmt, Address};

pub struct WalletInfo {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy-paste of hardware_wallet::WalletInfo

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this be a newtype of that?

rpc/src/lib.rs Outdated
@@ -67,7 +67,10 @@ extern crate parity_version as version;
extern crate rlp;
extern crate stats;
extern crate keccak_hash as hash;

#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed android for now because I am not sure whether android compiles or not.

I need to grab the android NDK in order to test it!

Copy link
Contributor

@tomaka tomaka Jun 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The easiest solution is docker.
Go to parity/docker/android and create the docker image: docker build -t parity-android .
You can then compile Parity with it: docker run --rm -it -v pwd:/usr/code -w /usr/code parity-android and cargo build --target=armv7-unknown-linux-androideabi.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Pierre!

@@ -713,6 +717,7 @@ fn signature(accounts: &AccountProvider, address: Address, hash: H256, password:
}

// obtain a hardware signature from the given account.
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I skipped this because it will require to implement alot of more dummy types!

@niklasad1 niklasad1 force-pushed the disable-hw-wallet-on-platforms-notlibusb branch from b55b04d to 88460f8 Compare June 2, 2018 14:30
@jtomanik
Copy link

jtomanik commented Jun 5, 2018

Hey @niklasad1 it would be good to add target_os = "ios" in places where you already have target_os = "android". This would nicely work with #7955

@niklasad1
Copy link
Collaborator Author

Hey @jtomanik thanks for the heads-up but iOS is not supported by libusb according to https://github.com/libusb/libusb/wiki# that's why I only have enabled hardware_wallet for Windows, OSX, Linux and Android

@jtomanik
Copy link

jtomanik commented Jun 5, 2018

@niklasad1 sorry misread your code, still getting used to Rust syntax

Copy link
Collaborator

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @niklasad1 that this is very brute-force-ish. My personal feeling is that for a comparatively small win – compile on FreeBSD – we're cluttering our code significantly. I can't really speak to @folsen s suggestion to extract the hardware wallet stuff to its own binary, but I sure like the sound of teasing apart orthogonal concerns whenever I hear it.
Even without a full extraction it feels like it should be possible to do this in a cleaner way, grouping together everything HW related into a single trait and implement a shim for the BSDs? Not sure if @niklasad1 tried this already and ran into issues, but would love to hear more.

.into_iter()
.map(|a| a.address)
.filter(|address| !self.blacklisted_accounts.contains(address))
.collect()
)
}

/// Returns addresses of hardware accounts.
pub fn hardware_accounts(&self) -> Result<Vec<Address>, Error> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about making this private and return Error? Seems like the only caller is hardware_accounts_info() and it a little misleading as hardware_accounts() doesn't actually ever fail (due to the map_or_else)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that is a great suggestion i.e, to return an Error but it can't be private because rpc actually calls this!


use super::{fmt, Address};

pub struct WalletInfo {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this be a newtype of that?

@niklasad1 niklasad1 added A1-onice 🌨 Pull request is reviewed well, but should not yet be merged. and removed A0-pleasereview 🤓 Pull request needs code review. labels Jun 15, 2018
@niklasad1 niklasad1 force-pushed the disable-hw-wallet-on-platforms-notlibusb branch from 3b78b39 to e03bc8d Compare June 15, 2018 21:41
@niklasad1 niklasad1 added A0-pleasereview 🤓 Pull request needs code review. and removed A1-onice 🌨 Pull request is reviewed well, but should not yet be merged. labels Jun 15, 2018
@niklasad1 niklasad1 force-pushed the disable-hw-wallet-on-platforms-notlibusb branch from e03bc8d to 6e58c04 Compare June 15, 2018 21:49
@niklasad1
Copy link
Collaborator Author

@dvdplm Totally agree I was lazy thought I needed to mock more types but this is the last attempt otherwise I will close this PR 🛩️

RE: own binary for hardware_wallet requires some refactoring for example if you want sign a transaction via UI then:

ui -> rpc -> account_manager -> hardware_wallet

It is probably pretty easy to change to communicate via message passing instead of function calls as we do now then I guess separate binaries would be possible!

Copy link
Collaborator

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this!

@5chdn
Copy link
Contributor

5chdn commented Jun 18, 2018

Needs a 2nd review.

niklasad1 and others added 8 commits June 22, 2018 17:34
* Might consume slight more memory than pure conditional compilation
flags
* Tested by it compiling succesfully with `cargo build --target=armv7--linux-androideabi`
* The binary is ~66 MB

```bash
$ size
size target/armv7-linux-androideabi/release/parity
text    	data     	bss     	dec     	hex 		filename
50676230        416200   	31456 		51123886        30c16ae 	target/armv7-linux-androideabi/release/parity
```
* Removes some conditional compilation flags
* Introduces `fake-hardware-wallet` crate
@niklasad1 niklasad1 force-pushed the disable-hw-wallet-on-platforms-notlibusb branch from f825a9a to 2011226 Compare June 22, 2018 16:04
Copy link
Contributor

@folsen folsen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also like this approach more than the previous, so LGTM.

@5chdn 5chdn added A8-looksgood 🦄 Pull request is reviewed well. and removed A0-pleasereview 🤓 Pull request needs code review. labels Jun 26, 2018
@5chdn 5chdn merged commit 1a16f33 into master Jun 26, 2018
@5chdn 5chdn deleted the disable-hw-wallet-on-platforms-notlibusb branch June 26, 2018 07:03
ordian added a commit to ordian/parity that referenced this pull request Jun 27, 2018
…rp_sync_on_light_client

* 'master' of https://github.com/paritytech/parity:
  parity: omit redundant last imported block number in light sync informant (openethereum#8962)
  Disable hardware-wallets on platforms that don't support `libusb` (openethereum#8464)
  Bump error-chain and quick_error versions (openethereum#8972)
  EVM benchmark utilities (openethereum#8944)
  parity: hide legacy options from cli --help (openethereum#8967)
  scripts: fix docker build tag on latest using master (openethereum#8952)
  Add type for passwords. (openethereum#8920)
dvdplm added a commit that referenced this pull request Jun 27, 2018
* master:
  Refactor evm Instruction to be a c-like enum (#8914)
  Fix deadlock in blockchain. (#8977)
  snap: downgrade rust to revision 1.26.2, ref snapcraft/+bug/1778530 (#8984)
  Use local parity-dapps-glue instead of crate published at crates.io (#8983)
  parity: omit redundant last imported block number in light sync informant (#8962)
  Disable hardware-wallets on platforms that don't support `libusb` (#8464)
  Bump error-chain and quick_error versions (#8972)
dvdplm added a commit that referenced this pull request Jun 27, 2018
* master:
  Refactor evm Instruction to be a c-like enum (#8914)
  Fix deadlock in blockchain. (#8977)
  snap: downgrade rust to revision 1.26.2, ref snapcraft/+bug/1778530 (#8984)
  Use local parity-dapps-glue instead of crate published at crates.io (#8983)
  parity: omit redundant last imported block number in light sync informant (#8962)
  Disable hardware-wallets on platforms that don't support `libusb` (#8464)
  Bump error-chain and quick_error versions (#8972)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A8-looksgood 🦄 Pull request is reviewed well. M4-core ⛓ Core client code / Rust. M5-dependencies 🖇 Dependencies.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants