2020-06-20 13:55:07 +00:00
|
|
|
//! Provides a unified interface for locally signing transactions and interacting
|
|
|
|
//! with the Ethereum JSON-RPC. You can implement the `Signer` trait to extend
|
|
|
|
//! functionality to other signers such as Hardware Security Modules, KMS etc.
|
|
|
|
//!
|
|
|
|
//! ```no_run
|
|
|
|
//! # use ethers::{
|
|
|
|
//! providers::{Http, Provider},
|
|
|
|
//! signers::Wallet,
|
|
|
|
//! core::types::TransactionRequest
|
|
|
|
//! };
|
|
|
|
//! # use std::convert::TryFrom;
|
|
|
|
//! # async fn foo() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
//! // connect to the network
|
|
|
|
//! let provider = Provider::<Http>::try_from("http://localhost:8545")?;
|
|
|
|
//!
|
|
|
|
//! // instantiate the wallet and connect it to the provider to get a client
|
|
|
|
//! let client = "dcf2cbdd171a21c480aa7f53d77f31bb102282b3ff099c78e3118b37348c72f7"
|
|
|
|
//! .parse::<Wallet>()?
|
|
|
|
//! .connect(provider);
|
|
|
|
//!
|
|
|
|
//! // create a transaction
|
|
|
|
//! let tx = TransactionRequest::new()
|
|
|
|
//! .to("vitalik.eth") // this will use ENS
|
|
|
|
//! .value(10000);
|
|
|
|
//!
|
|
|
|
//! // send it! (this will resolve the ENS name to an address under the hood)
|
2020-06-22 08:44:08 +00:00
|
|
|
//! let tx_hash = client.send_transaction(tx, None).await?;
|
2020-06-20 13:55:07 +00:00
|
|
|
//!
|
|
|
|
//! // get the receipt
|
2020-06-22 08:44:08 +00:00
|
|
|
//! let receipt = client.pending_transaction(tx_hash).await?;
|
2020-06-20 13:55:07 +00:00
|
|
|
//!
|
|
|
|
//! // get the mined tx
|
|
|
|
//! let tx = client.get_transaction(receipt.transaction_hash).await?;
|
|
|
|
//!
|
|
|
|
//! println!("{}", serde_json::to_string(&tx)?);
|
|
|
|
//! println!("{}", serde_json::to_string(&receipt)?);
|
|
|
|
//!
|
|
|
|
//! # Ok(())
|
|
|
|
//! # }
|
2020-05-26 10:24:19 +00:00
|
|
|
mod wallet;
|
|
|
|
pub use wallet::Wallet;
|
|
|
|
|
2020-09-07 10:26:42 +00:00
|
|
|
mod nonce_manager;
|
|
|
|
pub(crate) use nonce_manager::NonceManager;
|
|
|
|
|
2020-05-26 10:24:19 +00:00
|
|
|
mod client;
|
2020-06-01 22:27:23 +00:00
|
|
|
pub use client::{Client, ClientError};
|
2020-05-26 10:24:19 +00:00
|
|
|
|
2020-05-31 16:01:34 +00:00
|
|
|
use ethers_core::types::{Address, Signature, Transaction, TransactionRequest};
|
2020-06-10 10:34:48 +00:00
|
|
|
use ethers_providers::Http;
|
2020-05-26 10:24:19 +00:00
|
|
|
use std::error::Error;
|
|
|
|
|
|
|
|
/// Trait for signing transactions and messages
|
|
|
|
///
|
|
|
|
/// Implement this trait to support different signing modes, e.g. Ledger, hosted etc.
|
2020-05-31 21:17:50 +00:00
|
|
|
// TODO: We might need a `SignerAsync` trait for HSM use cases?
|
2020-06-17 06:38:04 +00:00
|
|
|
pub trait Signer: Clone + Send + Sync {
|
2020-06-01 23:00:58 +00:00
|
|
|
type Error: Error + Into<ClientError>;
|
2020-05-26 10:24:19 +00:00
|
|
|
/// Signs the hash of the provided message after prefixing it
|
|
|
|
fn sign_message<S: AsRef<[u8]>>(&self, message: S) -> Signature;
|
|
|
|
|
|
|
|
/// Signs the transaction
|
|
|
|
fn sign_transaction(&self, message: TransactionRequest) -> Result<Transaction, Self::Error>;
|
|
|
|
|
|
|
|
/// Returns the signer's Ethereum Address
|
|
|
|
fn address(&self) -> Address;
|
2020-05-26 09:37:31 +00:00
|
|
|
}
|
2020-05-27 15:43:43 +00:00
|
|
|
|
2020-05-31 21:17:50 +00:00
|
|
|
/// An HTTP client configured to work with ANY blockchain without replay protection
|
2020-06-10 10:34:48 +00:00
|
|
|
pub type HttpClient = Client<Http, Wallet>;
|