Skip to content

Commit

Permalink
Merge pull request #3593 from anoma/tomas/ledger-tcp-trans
Browse files Browse the repository at this point in the history
Add TCP transport support for Ledger wallet
  • Loading branch information
mergify[bot] authored Aug 12, 2024
2 parents 6b827ec + 5a1a69b commit dd0e04a
Show file tree
Hide file tree
Showing 16 changed files with 367 additions and 65 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/features/3593-ledger-tcp-transport.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Added support for Ledger wallet TCP transport.
([\#3593](https://github.com/anoma/namada/pull/3593))
109 changes: 104 additions & 5 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ jubjub = "0.10"
k256 = { version = "0.13.0", default-features = false, features = ["ecdsa", "pkcs8", "precomputed-tables", "serde", "std"]}
konst = { version = "0.3.8", default-features = false }
lazy_static = "1.4.0"
# TODO: upstreamed in https://github.com/ledger-community/rust-ledger/pull/9
ledger-lib = { git = "https://github.com/heliaxdev/rust-ledger", rev = "f96f4559b3237d09218f7583df01acf36034ea79", default-features = false, features = ["transport_tcp"] }
ledger-namada-rs = { git = "https://github.com/Zondax/ledger-namada", tag = "v0.0.24" }
ledger-transport = "0.10.0"
ledger-transport-hid = "0.10.0"
libc = "0.2.97"
libloading = "0.7.2"
Expand Down
2 changes: 2 additions & 0 deletions crates/apps_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ futures.workspace = true
itertools.workspace = true
lazy_static = { workspace = true, optional = true }
linkme = { workspace = true, optional = true }
ledger-lib = { workspace = true }
ledger-namada-rs.workspace = true
ledger-transport.workspace = true
ledger-transport-hid.workspace = true
masp_primitives = { workspace = true, features = ["transparent-inputs"] }
prost.workspace = true
Expand Down
39 changes: 37 additions & 2 deletions crates/apps_lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3468,6 +3468,16 @@ pub mod args {
pub const WITH_INDEXER: ArgOpt<String> = arg_opt("with-indexer");
pub const TX_PATH: Arg<PathBuf> = arg("tx-path");
pub const TX_PATH_OPT: ArgOpt<PathBuf> = TX_PATH.opt();
pub const DEVICE_TRANSPORT: ArgDefault<DeviceTransport> = arg_default(
"device-transport",
DefaultFn(|| {
if let Ok(val) = std::env::var(DEVICE_TRANSPORT_ENV_VAR) {
return DeviceTransport::from_str(&val).unwrap();
}
DeviceTransport::default()
}),
);
pub const DEVICE_TRANSPORT_ENV_VAR: &str = "NAMADA_DEVICE_TRANSPORT";

/// Global command arguments
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -7033,6 +7043,7 @@ pub mod args {
wrapper_fee_payer: self.wrapper_fee_payer.map(|x| ctx.get(&x)),
memo: self.memo,
use_device: self.use_device,
device_transport: self.device_transport,
})
}
}
Expand Down Expand Up @@ -7163,6 +7174,15 @@ pub mod args {
))
.conflicts_with(DRY_RUN_TX.name),
)
.arg(
DEVICE_TRANSPORT
.def()
.help(wrap!(
"Select transport for hardware wallet from \"hid\" \
(default) or \"tcp\"."
))
.conflicts_with(DRY_RUN_TX.name),
)
.arg(
MEMO_OPT
.def()
Expand Down Expand Up @@ -7204,6 +7224,7 @@ pub mod args {
None => TxExpiration::Default,
}
};
let device_transport = DEVICE_TRANSPORT.parse(matches);
Self {
dry_run,
dry_run_wrapper,
Expand All @@ -7227,6 +7248,7 @@ pub mod args {
output_folder,
memo,
use_device,
device_transport,
}
}
}
Expand Down Expand Up @@ -7356,22 +7378,24 @@ pub mod args {
let alias = ALIAS.parse(matches);
let alias_force = ALIAS_FORCE.parse(matches);
let unsafe_dont_encrypt = UNSAFE_DONT_ENCRYPT.parse(matches);
let use_device = USE_DEVICE.parse(matches);
let derivation_path = HD_DERIVATION_PATH.parse(matches);
let allow_non_compliant =
HD_ALLOW_NON_COMPLIANT_DERIVATION_PATH.parse(matches);
let prompt_bip39_passphrase =
HD_PROMPT_BIP39_PASSPHRASE.parse(matches);
let use_device = USE_DEVICE.parse(matches);
let device_transport = DEVICE_TRANSPORT.parse(matches);
Self {
scheme,
shielded,
alias,
alias_force,
unsafe_dont_encrypt,
use_device,
derivation_path,
allow_non_compliant,
prompt_bip39_passphrase,
use_device,
device_transport,
}
}

Expand Down Expand Up @@ -7401,6 +7425,10 @@ pub mod args {
"Derive an address and public key from the seed stored on the \
connected hardware wallet."
)))
.arg(DEVICE_TRANSPORT.def().help(wrap!(
"Select transport for hardware wallet from \"hid\" (default) \
or \"tcp\"."
)))
.arg(HD_DERIVATION_PATH.def().help(wrap!(
"HD key derivation path. Use keyword `default` to refer to a \
scheme default path:\n- m/44'/60'/0'/0/0 for the transparent \
Expand Down Expand Up @@ -8191,6 +8219,7 @@ pub mod args {
pub output: Option<PathBuf>,
pub validator_alias: Option<String>,
pub use_device: bool,
pub device_transport: DeviceTransport,
}

impl Args for SignGenesisTxs {
Expand All @@ -8199,11 +8228,13 @@ pub mod args {
let output = OUTPUT.parse(matches);
let validator_alias = ALIAS_OPT.parse(matches);
let use_device = USE_DEVICE.parse(matches);
let device_transport = DEVICE_TRANSPORT.parse(matches);
Self {
path,
output,
validator_alias,
use_device,
device_transport,
}
}

Expand All @@ -8226,6 +8257,10 @@ pub mod args {
"Derive an address and public key from the seed stored on the \
connected hardware wallet."
)))
.arg(DEVICE_TRANSPORT.def().help(wrap!(
"Select transport for hardware wallet from \"hid\" (default) \
or \"tcp\"."
)))
}
}

Expand Down
19 changes: 5 additions & 14 deletions crates/apps_lib/src/cli/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use borsh_ext::BorshSerializeExt;
use color_eyre::eyre::Result;
use itertools::sorted;
use ledger_namada_rs::{BIP44Path, NamadaApp};
use ledger_transport_hid::hidapi::HidApi;
use ledger_transport_hid::TransportNativeHID;
use masp_primitives::zip32::ExtendedFullViewingKey;
use namada_sdk::address::{Address, DecodeError};
use namada_sdk::io::Io;
Expand All @@ -31,7 +29,7 @@ use crate::cli::{args, cmds, Context};
use crate::client::utils::PRE_GENESIS_DIR;
use crate::tendermint_node::validator_key_to_json;
use crate::wallet::{
self, read_and_confirm_encryption_password, CliWalletUtils,
self, read_and_confirm_encryption_password, CliWalletUtils, WalletTransport,
};

impl CliApi {
Expand Down Expand Up @@ -446,7 +444,8 @@ async fn transparent_key_and_address_derive(
allow_non_compliant,
prompt_bip39_passphrase,
use_device,
..
shielded: _,
device_transport,
}: args::KeyDerive,
) {
let mut wallet = load_wallet(ctx);
Expand Down Expand Up @@ -485,16 +484,8 @@ async fn transparent_key_and_address_derive(
})
.0
} else {
let hidapi = HidApi::new().unwrap_or_else(|err| {
edisplay_line!(io, "Failed to create HidApi: {}", err);
cli::safe_exit(1)
});
let app = NamadaApp::new(
TransportNativeHID::new(&hidapi).unwrap_or_else(|err| {
edisplay_line!(io, "Unable to connect to Ledger: {}", err);
cli::safe_exit(1)
}),
);
let transport = WalletTransport::from_arg(device_transport);
let app = NamadaApp::new(transport);
let response = app
.get_address_and_pubkey(
&BIP44Path {
Expand Down
Loading

0 comments on commit dd0e04a

Please sign in to comment.