add docs to ethers-signer

This commit is contained in:
Georgios Konstantopoulos 2020-05-29 00:16:21 +03:00
parent 5489f08835
commit 2bba40a788
No known key found for this signature in database
GPG Key ID: FA607837CD26EDBC
3 changed files with 46 additions and 11 deletions

View File

@ -6,6 +6,9 @@ use ethers_types::{Address, BlockNumber, NameOrAddress, TransactionRequest, TxHa
use std::ops::Deref; use std::ops::Deref;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
/// A client provides an interface for signing and broadcasting locally signed transactions
/// It Derefs to `Provider`, which allows interacting with the Ethereum JSON-RPC provider
/// via the same API.
pub struct Client<'a, P, N, S> { pub struct Client<'a, P, N, S> {
pub(crate) provider: &'a Provider<P, N>, pub(crate) provider: &'a Provider<P, N>,
pub(crate) signer: Option<S>, pub(crate) signer: Option<S>,
@ -26,8 +29,7 @@ where
P: JsonRpcClient, P: JsonRpcClient,
N: Network, N: Network,
{ {
/// Signs the transaction and then broadcasts its RLP encoding via the `eth_sendRawTransaction` /// Signs and broadcasts the transaction
/// API
pub async fn send_transaction( pub async fn send_transaction(
&self, &self,
mut tx: TransactionRequest, mut tx: TransactionRequest,
@ -92,6 +94,7 @@ where
Ok(()) Ok(())
} }
/// Returns the client's address
pub fn address(&self) -> Address { pub fn address(&self) -> Address {
self.signer self.signer
.as_ref() .as_ref()
@ -99,6 +102,7 @@ where
.unwrap_or_default() .unwrap_or_default()
} }
/// Returns a reference to the client's provider
pub fn provider(&self) -> &Provider<P, N> { pub fn provider(&self) -> &Provider<P, N> {
self.provider self.provider
} }

View File

@ -1,14 +1,42 @@
//! Ethereum compatible signers //! # ethers-signers
//! //!
//! Currently supported: //! Provides a unified interface for locally signing transactions and interacting
//! - [x] Private Key //! with the Ethereum JSON-RPC. You can implement the `Signer` trait to extend
//! - [ ] Encrypted Json //! functionality to other signers such as Hardware Security Modules, KMS etc.
//! - [ ] Ledger
//! - [ ] Trezor
//! //!
//! Implement the `Signer` trait to add support for new signers, e.g. with Ledger. //! ```ignore
//! # use anyhow::Result;
//! # use ethers::{providers::HttpProvider, signers::MainnetWallet, types::TransactionRequest};
//! # use std::convert::TryFrom;
//! # async fn main() -> Result<()> {
//! // connect to the network
//! let provider = HttpProvider::try_from("http://localhost:8545")?;
//! //!
//! TODO: We might need a `SignerAsync` trait for HSM use cases? //! // instantiate the wallet and connect it to the provider to get a client
//! let client = "dcf2cbdd171a21c480aa7f53d77f31bb102282b3ff099c78e3118b37348c72f7"
//! .parse::<MainnetWallet>()?
//! .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)
//! let hash = client.send_transaction(tx, None).await?;
//!
//! // get the mined tx
//! let tx = client.get_transaction(hash).await?;
//!
//! // get the receipt
//! let receipt = client.get_transaction_receipt(tx.hash).await?;
//!
//! println!("{}", serde_json::to_string(&tx)?);
//! println!("{}", serde_json::to_string(&receipt)?);
//!
//! # Ok(())
//! # }
// TODO: We might need a `SignerAsync` trait for HSM use cases?
mod wallet; mod wallet;
pub use wallet::Wallet; pub use wallet::Wallet;

View File

@ -9,11 +9,14 @@ use ethers_types::{
use std::{marker::PhantomData, str::FromStr}; use std::{marker::PhantomData, str::FromStr};
/// A keypair /// An Ethereum keypair
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Wallet<N> { pub struct Wallet<N> {
/// The Wallet's private Key
pub private_key: PrivateKey, pub private_key: PrivateKey,
/// The Wallet's public Key
pub public_key: PublicKey, pub public_key: PublicKey,
/// The wallet's address
pub address: Address, pub address: Address,
network: PhantomData<N>, network: PhantomData<N>,
} }