From 8ff9a894c0946245ba7f746b7081c67565edcc34 Mon Sep 17 00:00:00 2001 From: Georgios Konstantopoulos Date: Sun, 20 Sep 2020 21:27:00 +0300 Subject: [PATCH] chore: add example for ledger --- ethers-signers/src/ledger/types.rs | 2 -- ethers-signers/src/lib.rs | 4 +++- ethers/Cargo.toml | 1 + ethers/examples/ledger.rs | 25 +++++++++++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 ethers/examples/ledger.rs diff --git a/ethers-signers/src/ledger/types.rs b/ethers-signers/src/ledger/types.rs index 6284455b..eba06171 100644 --- a/ethers-signers/src/ledger/types.rs +++ b/ethers-signers/src/ledger/types.rs @@ -56,7 +56,6 @@ pub enum INS { #[derive(Debug, Copy, Clone, Eq, PartialEq)] #[allow(non_camel_case_types)] pub enum P1 { - CONFIRM = 0x01, NON_CONFIRM = 0x00, MORE = 0x80, } @@ -65,6 +64,5 @@ pub enum P1 { #[derive(Debug, Copy, Clone, Eq, PartialEq)] #[allow(non_camel_case_types)] pub enum P2 { - CHAINCODE = 0x01, NO_CHAINCODE = 0x00, } diff --git a/ethers-signers/src/lib.rs b/ethers-signers/src/lib.rs index 71d68030..a1c1d298 100644 --- a/ethers-signers/src/lib.rs +++ b/ethers-signers/src/lib.rs @@ -41,7 +41,9 @@ mod wallet; pub use wallet::Wallet; #[cfg(feature = "ledger")] -pub mod ledger; +mod ledger; +#[cfg(feature = "ledger")] +pub use ledger::{app::LedgerEthereum as Ledger, types::{LedgerError, DerivationType as HDPath}}; mod nonce_manager; pub(crate) use nonce_manager::NonceManager; diff --git a/ethers/Cargo.toml b/ethers/Cargo.toml index 32c738e6..1901dc8a 100644 --- a/ethers/Cargo.toml +++ b/ethers/Cargo.toml @@ -40,6 +40,7 @@ core = ["ethers-core"] contract = ["ethers-contract"] providers = ["ethers-providers"] signers = ["ethers-signers"] +ledger = ["ethers-signers/ledger"] [dependencies] ethers-contract = { version = "0.1.3", path = "../ethers-contract", optional = true } diff --git a/ethers/examples/ledger.rs b/ethers/examples/ledger.rs new file mode 100644 index 00000000..8d6282c7 --- /dev/null +++ b/ethers/examples/ledger.rs @@ -0,0 +1,25 @@ +use anyhow::Result; +use ethers::{utils::parse_ether, prelude::*}; + +#[tokio::main] +async fn main() -> Result<()> { + // Connect over websockets + let provider = Provider::new(Ws::connect("ws://localhost:8545").await?); + // Instantiate the connection to ledger with Ledger Live derivation path and + // the wallet's index. Alternatively, you may use Legacy with the wallet's + // index or supply the full HD path string. You may also provide the chain_id + // (here: mainnet) for EIP155 support. + let ledger = Ledger::new(HDPath::LedgerLive(0), Some(1)).await?; + let client = Client::new(provider, ledger).await?; + + // Create and broadcast a transaction (ENS enabled!) + // (this will require confirming the tx on the device) + let tx = TransactionRequest::new() + .to("vitalik.eth") + .value(parse_ether(10)?); + let tx_hash = client.send_transaction(tx, None).await?; + + // Get the receipt + let receipt = client.pending_transaction(tx_hash).confirmations(3).await?; + Ok(()) +}