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;
#[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(crate) provider: &'a Provider<P, N>,
pub(crate) signer: Option<S>,
@ -26,8 +29,7 @@ where
P: JsonRpcClient,
N: Network,
{
/// Signs the transaction and then broadcasts its RLP encoding via the `eth_sendRawTransaction`
/// API
/// Signs and broadcasts the transaction
pub async fn send_transaction(
&self,
mut tx: TransactionRequest,
@ -92,6 +94,7 @@ where
Ok(())
}
/// Returns the client's address
pub fn address(&self) -> Address {
self.signer
.as_ref()
@ -99,6 +102,7 @@ where
.unwrap_or_default()
}
/// Returns a reference to the client's provider
pub fn provider(&self) -> &Provider<P, N> {
self.provider
}

View File

@ -1,14 +1,42 @@
//! Ethereum compatible signers
//! # ethers-signers
//!
//! Currently supported:
//! - [x] Private Key
//! - [ ] Encrypted Json
//! - [ ] Ledger
//! - [ ] Trezor
//! 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.
//!
//! 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;
pub use wallet::Wallet;

View File

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