[][src]Struct ethers_middleware::signer::SignerMiddleware

pub struct SignerMiddleware<M, S> { /* fields omitted */ }

Middleware used for locally signing transactions, compatible with any implementer of the Signer trait.

Example

use ethers::{
    providers::{Middleware, Provider, Http},
    signers::LocalWallet,
    middleware::SignerMiddleware,
    types::{Address, TransactionRequest},
};
use std::convert::TryFrom;

let provider = Provider::<Http>::try_from("http://localhost:8545")
    .expect("could not instantiate HTTP Provider");

// Transactions will be signed with the private key below and will be broadcast
// via the eth_sendRawTransaction API)
let wallet: LocalWallet = "380eb0f3d505f087e438eca80bc4df9a7faa24f868e69fc0440261a0fc0567dc"
    .parse()?;

let mut client = SignerMiddleware::new(provider, wallet);

// You can sign messages with the key
let signed_msg = client.sign(b"hello".to_vec(), &client.address()).await?;

// ...and sign transactions
let tx = TransactionRequest::pay("vitalik.eth", 100);
let pending_tx = client.send_transaction(tx, None).await?;

// You can `await` on the pending transaction to get the receipt with a pre-specified
// number of confirmations
let receipt = pending_tx.confirmations(6).await?;

// You can connect with other wallets at runtime via the `with_signer` function
let wallet2: LocalWallet = "cd8c407233c0560f6de24bb2dc60a8b02335c959a1a17f749ce6c1ccf63d74a7"
    .parse()?;

let signed_msg2 = client.with_signer(wallet2).sign(b"hello".to_vec(), &client.address()).await?;

// This call will be made with `wallet2` since `with_signer` takes a mutable reference.
let tx2 = TransactionRequest::new()
    .to("0xd8da6bf26964af9d7eed9e03e53415d37aa96045".parse::<Address>()?)
    .value(200);
let tx_hash2 = client.send_transaction(tx2, None).await?;

Implementations

impl<M, S> SignerMiddleware<M, S> where
    M: Middleware,
    S: Signer
[src]

pub fn new(inner: M, signer: S) -> Self[src]

Creates a new client from the provider and signer.

pub fn address(&self) -> Address[src]

Returns the client's address

pub fn signer(&self) -> &S[src]

Returns a reference to the client's signer

pub fn with_signer(&self, signer: S) -> Self where
    S: Clone,
    M: Clone
[src]

Trait Implementations

impl<M: Clone, S: Clone> Clone for SignerMiddleware<M, S>[src]

impl<M: Debug, S: Debug> Debug for SignerMiddleware<M, S>[src]

impl<M, S> Middleware for SignerMiddleware<M, S> where
    M: Middleware,
    S: Signer
[src]

type Error = SignerMiddlewareError<M, S>

type Provider = M::Provider

type Inner = M

pub fn send_transaction<'life0, 'async_trait>(
    &'life0 self,
    tx: TransactionRequest,
    block: Option<BlockNumber>
) -> Pin<Box<dyn Future<Output = Result<PendingTransaction<'_, Self::Provider>, Self::Error>> + Send + 'async_trait>> where
    'life0: 'async_trait,
    Self: 'async_trait, 
[src]

Signs and broadcasts the transaction. The optional parameter block can be passed so that gas cost and nonce calculations take it into account. For simple transactions this can be left to None.

pub fn sign<'life0, 'life1, 'async_trait, T: Into<Bytes> + Send + Sync>(
    &'life0 self,
    data: T,
    __arg2: &'life1 Address
) -> Pin<Box<dyn Future<Output = Result<Signature, Self::Error>> + Send + 'async_trait>> where
    T: 'async_trait,
    'life0: 'async_trait,
    'life1: 'async_trait,
    Self: 'async_trait, 
[src]

Signs a message with the internal signer, or if none is present it will make a call to the connected node's eth_call API.

Auto Trait Implementations

impl<M, S> RefUnwindSafe for SignerMiddleware<M, S> where
    M: RefUnwindSafe,
    S: RefUnwindSafe
[src]

impl<M, S> Send for SignerMiddleware<M, S> where
    M: Send,
    S: Send
[src]

impl<M, S> Sync for SignerMiddleware<M, S> where
    M: Sync,
    S: Sync
[src]

impl<M, S> Unpin for SignerMiddleware<M, S> where
    M: Unpin,
    S: Unpin
[src]

impl<M, S> UnwindSafe for SignerMiddleware<M, S> where
    M: UnwindSafe,
    S: UnwindSafe
[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Conv for T

impl<T> FmtForward for T

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Pipe for T

impl<T> PipeAsRef for T

impl<T> PipeBorrow for T

impl<T> PipeDeref for T

impl<T> PipeRef for T

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> Tap for T

impl<T, U> TapAsRef<U> for T where
    U: ?Sized

impl<T, U> TapBorrow<U> for T where
    U: ?Sized

impl<T> TapDeref for T

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> TryConv for T

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,