Skip to content

Commit

Permalink
feat(cast): add trezor signer (#224)
Browse files Browse the repository at this point in the history
* bump ethers and add trezor feature

* add trezor signer and custom hd path
  • Loading branch information
charisma98 committed Dec 13, 2021
1 parent a5c2e4b commit 0cd9776
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 15 deletions.
68 changes: 56 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ foundry-utils = { path = "../utils" }
# ethers = "0.5"
ethers-core = { git = "https://github.com/gakonst/ethers-rs", default-features = false }
ethers-providers = { git = "https://github.com/gakonst/ethers-rs", default-features = false }
ethers-signers = { git = "https://github.com/gakonst/ethers-rs", default-features = false, features = ["ledger"] }
ethers-signers = { git = "https://github.com/gakonst/ethers-rs", default-features = false, features = ["ledger", "trezor"] }
eyre = "0.6.5"
rustc-hex = "2.1.0"
serde_json = "1.0.67"
Expand Down
3 changes: 3 additions & 0 deletions cli/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ async fn main() -> eyre::Result<()> {
cast_opts::WalletType::Local(signer) => {
cast_send(&signer, signer.address(), to, sig, args, eth.cast_async).await?;
}
cast_opts::WalletType::Trezor(signer) => {
cast_send(&signer, signer.address(), to, sig, args, eth.cast_async).await?;
}
}
} else {
let from = eth.from.expect("No ETH_FROM or signer specified");
Expand Down
28 changes: 26 additions & 2 deletions cli/src/cast_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::{convert::TryFrom, str::FromStr};
use ethers::{
middleware::SignerMiddleware,
providers::{Http, Provider},
signers::{coins_bip39::English, HDPath, Ledger, LocalWallet, MnemonicBuilder},
signers::{
coins_bip39::English, HDPath as LedgerHDPath, Ledger, LocalWallet, MnemonicBuilder, Trezor,
TrezorHDPath,
},
types::{Address, BlockId, BlockNumber, NameOrAddress, H256, U256},
};
use eyre::Result;
Expand Down Expand Up @@ -270,10 +273,21 @@ impl EthereumOpts {
provider: Provider<Http>,
) -> eyre::Result<Option<WalletType>> {
if self.wallet.ledger {
let derivation = HDPath::LedgerLive(self.wallet.mnemonic_index as usize);
let derivation = match &self.wallet.hd_path {
Some(hd_path) => LedgerHDPath::Other(hd_path.clone()),
None => LedgerHDPath::LedgerLive(self.wallet.mnemonic_index as usize),
};
let ledger = Ledger::new(derivation, chain_id.as_u64()).await?;

Ok(Some(WalletType::Ledger(SignerMiddleware::new(provider, ledger))))
} else if self.wallet.trezor {
let derivation = match &self.wallet.hd_path {
Some(hd_path) => TrezorHDPath::Other(hd_path.clone()),
None => TrezorHDPath::TrezorLive(self.wallet.mnemonic_index as usize),
};
let trezor = Trezor::new(derivation, chain_id.as_u64()).await?;

Ok(Some(WalletType::Trezor(SignerMiddleware::new(provider, trezor))))
} else {
let local = self
.wallet
Expand All @@ -293,6 +307,7 @@ impl EthereumOpts {
pub enum WalletType {
Local(SignerMiddleware<Provider<Http>, LocalWallet>),
Ledger(SignerMiddleware<Provider<Http>, Ledger>),
Trezor(SignerMiddleware<Provider<Http>, Trezor>),
}
#[derive(StructOpt, Debug, Clone)]
pub struct Wallet {
Expand All @@ -311,6 +326,15 @@ pub struct Wallet {
#[structopt(short, long = "ledger", help = "Use your Ledger hardware wallet")]
pub ledger: bool,

#[structopt(short, long = "trezor", help = "Use your Trezor hardware wallet")]
pub trezor: bool,

#[structopt(
long = "hd-path",
help = "Derivation path for your hardware wallet (trezor or ledger)"
)]
pub hd_path: Option<String>,

#[structopt(
long = "mnemonic_index",
help = "your index in the standard hd path",
Expand Down

0 comments on commit 0cd9776

Please sign in to comment.