2021-08-09 00:31:11 +00:00
|
|
|
use ethers_core::types::{
|
2022-03-14 11:27:06 +00:00
|
|
|
transaction::{eip2718::TypedTransaction, eip2930::AccessListWithGasUsed},
|
|
|
|
Address, BlockId, Bytes, Signature, U256,
|
2021-08-09 00:31:11 +00:00
|
|
|
};
|
|
|
|
use ethers_providers::{maybe, FromErr, Middleware, PendingTransaction};
|
2020-10-02 08:41:16 +00:00
|
|
|
use ethers_signers::Signer;
|
2020-09-24 21:33:09 +00:00
|
|
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
/// Middleware used for locally signing transactions, compatible with any implementer
|
|
|
|
/// of the [`Signer`] trait.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```no_run
|
2021-08-28 21:06:29 +00:00
|
|
|
/// use ethers_providers::{Middleware, Provider, Http};
|
|
|
|
/// use ethers_signers::LocalWallet;
|
|
|
|
/// use ethers_middleware::SignerMiddleware;
|
|
|
|
/// use ethers_core::types::{Address, TransactionRequest};
|
2020-09-24 21:33:09 +00:00
|
|
|
/// use std::convert::TryFrom;
|
|
|
|
///
|
|
|
|
/// # async fn foo() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
/// 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)
|
2020-10-02 08:41:16 +00:00
|
|
|
/// let wallet: LocalWallet = "380eb0f3d505f087e438eca80bc4df9a7faa24f868e69fc0440261a0fc0567dc"
|
2020-09-24 21:33:09 +00:00
|
|
|
/// .parse()?;
|
|
|
|
///
|
2020-10-08 15:56:36 +00:00
|
|
|
/// let mut client = SignerMiddleware::new(provider, wallet);
|
2020-09-24 21:33:09 +00:00
|
|
|
///
|
2020-10-08 15:56:36 +00:00
|
|
|
/// // You can sign messages with the key
|
2020-09-24 21:33:09 +00:00
|
|
|
/// let signed_msg = client.sign(b"hello".to_vec(), &client.address()).await?;
|
|
|
|
///
|
2020-10-08 15:56:36 +00:00
|
|
|
/// // ...and sign transactions
|
2020-09-24 21:33:09 +00:00
|
|
|
/// let tx = TransactionRequest::pay("vitalik.eth", 100);
|
2020-12-17 11:26:01 +00:00
|
|
|
/// let pending_tx = client.send_transaction(tx, None).await?;
|
2020-09-24 21:33:09 +00:00
|
|
|
///
|
|
|
|
/// // You can `await` on the pending transaction to get the receipt with a pre-specified
|
|
|
|
/// // number of confirmations
|
2020-12-17 11:26:01 +00:00
|
|
|
/// let receipt = pending_tx.confirmations(6).await?;
|
2020-09-24 21:33:09 +00:00
|
|
|
///
|
|
|
|
/// // You can connect with other wallets at runtime via the `with_signer` function
|
2020-10-02 08:41:16 +00:00
|
|
|
/// let wallet2: LocalWallet = "cd8c407233c0560f6de24bb2dc60a8b02335c959a1a17f749ce6c1ccf63d74a7"
|
2020-09-24 21:33:09 +00:00
|
|
|
/// .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?;
|
|
|
|
///
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
///
|
2022-04-10 17:55:30 +00:00
|
|
|
/// [`Signer`]: ethers_signers::Signer
|
2020-10-08 15:56:36 +00:00
|
|
|
pub struct SignerMiddleware<M, S> {
|
2020-09-24 21:33:09 +00:00
|
|
|
pub(crate) inner: M,
|
|
|
|
pub(crate) signer: S,
|
|
|
|
pub(crate) address: Address,
|
|
|
|
}
|
|
|
|
|
2020-10-08 15:56:36 +00:00
|
|
|
impl<M: Middleware, S: Signer> FromErr<M::Error> for SignerMiddlewareError<M, S> {
|
|
|
|
fn from(src: M::Error) -> SignerMiddlewareError<M, S> {
|
|
|
|
SignerMiddlewareError::MiddlewareError(src)
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
/// Error thrown when the client interacts with the blockchain
|
2020-10-08 15:56:36 +00:00
|
|
|
pub enum SignerMiddlewareError<M: Middleware, S: Signer> {
|
2020-09-24 21:33:09 +00:00
|
|
|
#[error("{0}")]
|
|
|
|
/// Thrown when the internal call to the signer fails
|
|
|
|
SignerError(S::Error),
|
|
|
|
|
|
|
|
#[error("{0}")]
|
|
|
|
/// Thrown when an internal middleware errors
|
|
|
|
MiddlewareError(M::Error),
|
|
|
|
|
|
|
|
/// Thrown if the `nonce` field is missing
|
|
|
|
#[error("no nonce was specified")]
|
|
|
|
NonceMissing,
|
|
|
|
/// Thrown if the `gas_price` field is missing
|
|
|
|
#[error("no gas price was specified")]
|
|
|
|
GasPriceMissing,
|
|
|
|
/// Thrown if the `gas` field is missing
|
|
|
|
#[error("no gas was specified")]
|
|
|
|
GasMissing,
|
2021-07-27 21:23:25 +00:00
|
|
|
/// Thrown if a signature is requested from a different address
|
|
|
|
#[error("specified from address is not signer")]
|
|
|
|
WrongSigner,
|
2022-01-30 19:21:16 +00:00
|
|
|
/// Thrown if the signer's chain_id is different than the chain_id of the transaction
|
|
|
|
#[error("specified chain_id is different than the signer's chain_id")]
|
|
|
|
DifferentChainID,
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper functions for locally signing transactions
|
2020-10-08 15:56:36 +00:00
|
|
|
impl<M, S> SignerMiddleware<M, S>
|
2020-09-24 21:33:09 +00:00
|
|
|
where
|
|
|
|
M: Middleware,
|
|
|
|
S: Signer,
|
|
|
|
{
|
|
|
|
/// Creates a new client from the provider and signer.
|
2022-04-10 17:55:30 +00:00
|
|
|
/// Sets the address of this middleware to the address of the signer.
|
|
|
|
/// The chain_id of the signer will not be set to the chain id of the provider. If the signer
|
|
|
|
/// passed here is initialized with a different chain id, then the client may throw errors, or
|
|
|
|
/// methods like `sign_transaction` may error.
|
|
|
|
/// To automatically set the signer's chain id, see `new_with_provider_chain`.
|
|
|
|
///
|
|
|
|
/// [`Middleware`] ethers_providers::Middleware
|
|
|
|
/// [`Signer`] ethers_signers::Signer
|
2020-09-24 21:33:09 +00:00
|
|
|
pub fn new(inner: M, signer: S) -> Self {
|
|
|
|
let address = signer.address();
|
2021-10-29 12:29:35 +00:00
|
|
|
SignerMiddleware { inner, signer, address }
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|
|
|
|
|
2022-04-10 17:55:30 +00:00
|
|
|
/// Signs and returns the RLP encoding of the signed transaction.
|
|
|
|
/// If the transaction does not have a chain id set, it sets it to the signer's chain id.
|
|
|
|
/// Returns an error if the transaction's existing chain id does not match the signer's chain
|
|
|
|
/// id.
|
2020-09-24 21:33:09 +00:00
|
|
|
async fn sign_transaction(
|
|
|
|
&self,
|
2021-08-09 00:31:11 +00:00
|
|
|
tx: TypedTransaction,
|
2021-08-08 22:49:23 +00:00
|
|
|
) -> Result<Bytes, SignerMiddlewareError<M, S>> {
|
2022-01-30 19:21:16 +00:00
|
|
|
// compare chain_id and use signer's chain_id if the tranasaction's chain_id is None,
|
|
|
|
// return an error if they are not consistent
|
|
|
|
let chain_id = self.signer.chain_id();
|
|
|
|
match tx.chain_id() {
|
|
|
|
Some(id) if id.as_u64() != chain_id => {
|
|
|
|
return Err(SignerMiddlewareError::DifferentChainID)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let mut tx = tx.clone();
|
|
|
|
tx.set_chain_id(chain_id);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2021-10-29 12:29:35 +00:00
|
|
|
let signature =
|
|
|
|
self.signer.sign_transaction(&tx).await.map_err(SignerMiddlewareError::SignerError)?;
|
2020-09-24 21:33:09 +00:00
|
|
|
|
2021-08-08 22:49:23 +00:00
|
|
|
// Return the raw rlp-encoded signed transaction
|
2022-01-30 19:21:16 +00:00
|
|
|
Ok(tx.rlp_signed(&signature))
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the client's address
|
|
|
|
pub fn address(&self) -> Address {
|
|
|
|
self.address
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a reference to the client's signer
|
|
|
|
pub fn signer(&self) -> &S {
|
|
|
|
&self.signer
|
|
|
|
}
|
|
|
|
|
2022-04-10 17:55:30 +00:00
|
|
|
/// Builds a SignerMiddleware with the given Signer.
|
2021-12-19 04:28:38 +00:00
|
|
|
#[must_use]
|
2020-09-24 21:33:09 +00:00
|
|
|
pub fn with_signer(&self, signer: S) -> Self
|
|
|
|
where
|
|
|
|
S: Clone,
|
|
|
|
M: Clone,
|
|
|
|
{
|
|
|
|
let mut this = self.clone();
|
|
|
|
this.address = signer.address();
|
|
|
|
this.signer = signer;
|
|
|
|
this
|
|
|
|
}
|
2022-03-14 11:27:06 +00:00
|
|
|
|
2022-04-10 17:55:30 +00:00
|
|
|
/// Creates a new client from the provider and signer.
|
|
|
|
/// Sets the address of this middleware to the address of the signer.
|
|
|
|
/// Sets the chain id of the signer to the chain id of the inner [`Middleware`] passed in,
|
|
|
|
/// using the [`Signer`]'s implementation of with_chain_id.
|
|
|
|
///
|
|
|
|
/// [`Middleware`] ethers_providers::Middleware
|
|
|
|
/// [`Signer`] ethers_signers::Signer
|
|
|
|
pub async fn new_with_provider_chain(
|
|
|
|
inner: M,
|
|
|
|
signer: S,
|
|
|
|
) -> Result<Self, SignerMiddlewareError<M, S>> {
|
|
|
|
let address = signer.address();
|
|
|
|
let chain_id =
|
|
|
|
inner.get_chainid().await.map_err(|e| SignerMiddlewareError::MiddlewareError(e))?;
|
|
|
|
let signer = signer.with_chain_id(chain_id.as_u64());
|
|
|
|
Ok(SignerMiddleware { inner, signer, address })
|
|
|
|
}
|
|
|
|
|
2022-03-14 11:27:06 +00:00
|
|
|
fn set_tx_from_if_none(&self, tx: &TypedTransaction) -> TypedTransaction {
|
|
|
|
let mut tx = tx.clone();
|
|
|
|
if tx.from().is_none() {
|
|
|
|
tx.set_from(self.address);
|
|
|
|
}
|
|
|
|
tx
|
|
|
|
}
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-23 09:56:44 +00:00
|
|
|
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
|
|
|
|
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
|
2020-10-08 15:56:36 +00:00
|
|
|
impl<M, S> Middleware for SignerMiddleware<M, S>
|
2020-09-24 21:33:09 +00:00
|
|
|
where
|
|
|
|
M: Middleware,
|
|
|
|
S: Signer,
|
|
|
|
{
|
2020-10-08 15:56:36 +00:00
|
|
|
type Error = SignerMiddlewareError<M, S>;
|
2020-09-24 21:33:09 +00:00
|
|
|
type Provider = M::Provider;
|
|
|
|
type Inner = M;
|
|
|
|
|
|
|
|
fn inner(&self) -> &M {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
|
2021-08-09 00:31:11 +00:00
|
|
|
/// Returns the client's address
|
|
|
|
fn default_sender(&self) -> Option<Address> {
|
|
|
|
Some(self.address)
|
|
|
|
}
|
|
|
|
|
2021-01-28 06:51:53 +00:00
|
|
|
/// `SignerMiddleware` is instantiated with a signer.
|
|
|
|
async fn is_signer(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-11-14 12:26:02 +00:00
|
|
|
async fn sign_transaction(
|
|
|
|
&self,
|
|
|
|
tx: &TypedTransaction,
|
|
|
|
_: Address,
|
|
|
|
) -> Result<Signature, Self::Error> {
|
|
|
|
Ok(self.signer.sign_transaction(tx).await.map_err(SignerMiddlewareError::SignerError)?)
|
|
|
|
}
|
|
|
|
|
2021-08-09 00:31:11 +00:00
|
|
|
/// Helper for filling a transaction's nonce using the wallet
|
|
|
|
async fn fill_transaction(
|
|
|
|
&self,
|
|
|
|
tx: &mut TypedTransaction,
|
|
|
|
block: Option<BlockId>,
|
|
|
|
) -> Result<(), Self::Error> {
|
|
|
|
// get the `from` field's nonce if it's set, else get the signer's nonce
|
|
|
|
let from = if tx.from().is_some() && tx.from() != Some(&self.address()) {
|
|
|
|
*tx.from().unwrap()
|
|
|
|
} else {
|
|
|
|
self.address
|
|
|
|
};
|
|
|
|
tx.set_from(from);
|
|
|
|
|
2022-01-30 19:21:16 +00:00
|
|
|
// get the signer's chain_id if the transaction does not set it
|
|
|
|
let chain_id = self.signer.chain_id();
|
|
|
|
if tx.chain_id().is_none() {
|
|
|
|
tx.set_chain_id(chain_id);
|
|
|
|
}
|
|
|
|
|
2021-08-09 00:31:11 +00:00
|
|
|
let nonce = maybe(tx.nonce().cloned(), self.get_transaction_count(from, block)).await?;
|
|
|
|
tx.set_nonce(nonce);
|
|
|
|
self.inner()
|
|
|
|
.fill_transaction(tx, block)
|
|
|
|
.await
|
|
|
|
.map_err(SignerMiddlewareError::MiddlewareError)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-09-24 21:33:09 +00:00
|
|
|
/// 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`.
|
2021-08-09 00:31:11 +00:00
|
|
|
async fn send_transaction<T: Into<TypedTransaction> + Send + Sync>(
|
2020-09-24 21:33:09 +00:00
|
|
|
&self,
|
2021-08-09 00:31:11 +00:00
|
|
|
tx: T,
|
2021-03-16 19:46:07 +00:00
|
|
|
block: Option<BlockId>,
|
2020-12-17 11:26:01 +00:00
|
|
|
) -> Result<PendingTransaction<'_, Self::Provider>, Self::Error> {
|
2021-08-09 00:31:11 +00:00
|
|
|
let mut tx = tx.into();
|
|
|
|
|
|
|
|
// fill any missing fields
|
|
|
|
self.fill_transaction(&mut tx, block).await?;
|
|
|
|
|
2021-07-27 21:23:25 +00:00
|
|
|
// If the from address is set and is not our signer, delegate to inner
|
2021-08-09 00:31:11 +00:00
|
|
|
if tx.from().is_some() && tx.from() != Some(&self.address()) {
|
2021-07-27 21:23:25 +00:00
|
|
|
return self
|
|
|
|
.inner
|
|
|
|
.send_transaction(tx, block)
|
|
|
|
.await
|
2021-10-29 12:29:35 +00:00
|
|
|
.map_err(SignerMiddlewareError::MiddlewareError)
|
2021-07-27 21:23:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 21:33:09 +00:00
|
|
|
// if we have a nonce manager set, we should try handling the result in
|
|
|
|
// case there was a nonce mismatch
|
|
|
|
let signed_tx = self.sign_transaction(tx).await?;
|
|
|
|
|
|
|
|
// Submit the raw transaction
|
|
|
|
self.inner
|
2021-08-08 22:49:23 +00:00
|
|
|
.send_raw_transaction(signed_tx)
|
2020-09-24 21:33:09 +00:00
|
|
|
.await
|
2020-10-08 15:56:36 +00:00
|
|
|
.map_err(SignerMiddlewareError::MiddlewareError)
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
async fn sign<T: Into<Bytes> + Send + Sync>(
|
|
|
|
&self,
|
|
|
|
data: T,
|
|
|
|
_: &Address,
|
|
|
|
) -> Result<Signature, Self::Error> {
|
2021-10-29 12:29:35 +00:00
|
|
|
self.signer.sign_message(data.into()).await.map_err(SignerMiddlewareError::SignerError)
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|
2022-03-14 11:27:06 +00:00
|
|
|
|
|
|
|
async fn estimate_gas(&self, tx: &TypedTransaction) -> Result<U256, Self::Error> {
|
|
|
|
let tx = self.set_tx_from_if_none(tx);
|
|
|
|
self.inner.estimate_gas(&tx).await.map_err(SignerMiddlewareError::MiddlewareError)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn create_access_list(
|
|
|
|
&self,
|
|
|
|
tx: &TypedTransaction,
|
|
|
|
block: Option<BlockId>,
|
|
|
|
) -> Result<AccessListWithGasUsed, Self::Error> {
|
|
|
|
let tx = self.set_tx_from_if_none(tx);
|
|
|
|
self.inner
|
|
|
|
.create_access_list(&tx, block)
|
|
|
|
.await
|
|
|
|
.map_err(SignerMiddlewareError::MiddlewareError)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn call(
|
|
|
|
&self,
|
|
|
|
tx: &TypedTransaction,
|
|
|
|
block: Option<BlockId>,
|
|
|
|
) -> Result<Bytes, Self::Error> {
|
|
|
|
let tx = self.set_tx_from_if_none(tx);
|
|
|
|
self.inner().call(&tx, block).await.map_err(SignerMiddlewareError::MiddlewareError)
|
|
|
|
}
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-23 09:56:44 +00:00
|
|
|
#[cfg(all(test, not(feature = "celo"), not(target_arch = "wasm32")))]
|
2020-09-24 21:33:09 +00:00
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-08-09 00:31:11 +00:00
|
|
|
use ethers_core::{
|
|
|
|
types::TransactionRequest,
|
|
|
|
utils::{self, keccak256, Ganache},
|
|
|
|
};
|
2021-08-28 21:06:29 +00:00
|
|
|
use ethers_providers::Provider;
|
|
|
|
use ethers_signers::LocalWallet;
|
2020-09-24 21:33:09 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn signs_tx() {
|
|
|
|
// retrieved test vector from:
|
|
|
|
// https://web3js.readthedocs.io/en/v1.2.0/web3-eth-accounts.html#eth-accounts-signtransaction
|
|
|
|
let tx = TransactionRequest {
|
|
|
|
from: None,
|
2021-10-29 12:29:35 +00:00
|
|
|
to: Some("F0109fC8DF283027b6285cc889F5aA624EaC1F55".parse::<Address>().unwrap().into()),
|
2020-09-24 21:33:09 +00:00
|
|
|
value: Some(1_000_000_000.into()),
|
|
|
|
gas: Some(2_000_000.into()),
|
|
|
|
nonce: Some(0.into()),
|
|
|
|
gas_price: Some(21_000_000_000u128.into()),
|
|
|
|
data: None,
|
2022-01-30 19:21:16 +00:00
|
|
|
chain_id: None,
|
2021-08-09 00:31:11 +00:00
|
|
|
}
|
|
|
|
.into();
|
2020-09-24 21:33:09 +00:00
|
|
|
let chain_id = 1u64;
|
|
|
|
|
2022-04-10 17:55:30 +00:00
|
|
|
// Signer middlewares now rely on a working provider which it can query the chain id from,
|
|
|
|
// so we make sure ganache is started with the chain id that the expected tx was signed
|
|
|
|
// with
|
|
|
|
let ganache =
|
|
|
|
Ganache::new().args(vec!["--chain.chainId".to_string(), chain_id.to_string()]).spawn();
|
|
|
|
let provider = Provider::try_from(ganache.endpoint()).unwrap();
|
2020-09-24 21:33:09 +00:00
|
|
|
let key = "4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318"
|
2020-10-02 08:41:16 +00:00
|
|
|
.parse::<LocalWallet>()
|
2020-09-24 21:33:09 +00:00
|
|
|
.unwrap()
|
2021-07-29 20:22:25 +00:00
|
|
|
.with_chain_id(chain_id);
|
2020-10-08 15:56:36 +00:00
|
|
|
let client = SignerMiddleware::new(provider, key);
|
2020-09-24 21:33:09 +00:00
|
|
|
|
|
|
|
let tx = client.sign_transaction(tx).await.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-08-08 22:49:23 +00:00
|
|
|
keccak256(&tx)[..],
|
|
|
|
hex::decode("de8db924885b0803d2edc335f745b2b8750c8848744905684c20b987443a9593")
|
2020-09-24 21:33:09 +00:00
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
|
2020-12-31 17:19:14 +00:00
|
|
|
let expected_rlp = Bytes::from(hex::decode("f869808504e3b29200831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a0c9cf86333bcb065d140032ecaab5d9281bde80f21b9687b3e94161de42d51895a0727a108a0b8d101465414033c3f705a9c7b826e596766046ee1183dbc8aeaa68").unwrap());
|
2021-08-08 22:49:23 +00:00
|
|
|
assert_eq!(tx, expected_rlp);
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|
2021-07-27 21:23:25 +00:00
|
|
|
|
2022-04-10 17:55:30 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn ganache_consistent_chainid() {
|
|
|
|
let ganache = Ganache::new().spawn();
|
|
|
|
let provider = Provider::try_from(ganache.endpoint()).unwrap();
|
|
|
|
let chain_id = provider.get_chainid().await.unwrap();
|
|
|
|
assert_eq!(chain_id, U256::from(1337));
|
|
|
|
|
|
|
|
// Intentionally do not set the chain id here so we ensure that the signer pulls the
|
|
|
|
// provider's chain id.
|
|
|
|
let key = LocalWallet::new(&mut rand::thread_rng());
|
|
|
|
|
|
|
|
// combine the provider and wallet and test that the chain id is the same for both the
|
|
|
|
// signer returned by the middleware and through the middleware itself.
|
|
|
|
let client = SignerMiddleware::new_with_provider_chain(provider, key).await.unwrap();
|
|
|
|
let middleware_chainid = client.get_chainid().await.unwrap();
|
|
|
|
assert_eq!(chain_id, middleware_chainid);
|
|
|
|
|
|
|
|
let signer = client.signer();
|
|
|
|
let signer_chainid = signer.chain_id();
|
|
|
|
assert_eq!(chain_id.as_u64(), signer_chainid);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn ganache_consistent_chainid_not_default() {
|
|
|
|
let ganache = Ganache::new().args(vec!["--chain.chainId", "13371337"]).spawn();
|
|
|
|
let provider = Provider::try_from(ganache.endpoint()).unwrap();
|
|
|
|
let chain_id = provider.get_chainid().await.unwrap();
|
|
|
|
assert_eq!(chain_id, U256::from(13371337));
|
|
|
|
|
|
|
|
// Intentionally do not set the chain id here so we ensure that the signer pulls the
|
|
|
|
// provider's chain id.
|
|
|
|
let key = LocalWallet::new(&mut rand::thread_rng());
|
|
|
|
|
|
|
|
// combine the provider and wallet and test that the chain id is the same for both the
|
|
|
|
// signer returned by the middleware and through the middleware itself.
|
|
|
|
let client = SignerMiddleware::new_with_provider_chain(provider, key).await.unwrap();
|
|
|
|
let middleware_chainid = client.get_chainid().await.unwrap();
|
|
|
|
assert_eq!(chain_id, middleware_chainid);
|
|
|
|
|
|
|
|
let signer = client.signer();
|
|
|
|
let signer_chainid = signer.chain_id();
|
|
|
|
assert_eq!(chain_id.as_u64(), signer_chainid);
|
|
|
|
}
|
|
|
|
|
2021-07-27 21:23:25 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn handles_tx_from_field() {
|
2021-08-08 22:49:23 +00:00
|
|
|
let ganache = Ganache::new().spawn();
|
|
|
|
let acc = ganache.addresses()[0];
|
|
|
|
let provider = Provider::try_from(ganache.endpoint()).unwrap();
|
2021-07-29 20:22:25 +00:00
|
|
|
let key = LocalWallet::new(&mut rand::thread_rng()).with_chain_id(1u32);
|
2021-08-08 22:49:23 +00:00
|
|
|
provider
|
|
|
|
.send_transaction(
|
|
|
|
TransactionRequest::pay(key.address(), utils::parse_ether(1u64).unwrap()).from(acc),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.await
|
2021-07-27 21:23:25 +00:00
|
|
|
.unwrap();
|
2022-04-10 17:55:30 +00:00
|
|
|
let client = SignerMiddleware::new_with_provider_chain(provider, key).await.unwrap();
|
2021-07-27 21:23:25 +00:00
|
|
|
|
2021-08-08 22:49:23 +00:00
|
|
|
let request = TransactionRequest::new();
|
2021-07-27 21:23:25 +00:00
|
|
|
|
|
|
|
// signing a TransactionRequest with a from field of None should yield
|
|
|
|
// a signed transaction from the signer address
|
|
|
|
let request_from_none = request.clone();
|
2021-10-29 12:29:35 +00:00
|
|
|
let hash = *client.send_transaction(request_from_none, None).await.unwrap();
|
2021-08-08 22:49:23 +00:00
|
|
|
let tx = client.get_transaction(hash).await.unwrap().unwrap();
|
|
|
|
assert_eq!(tx.from, client.address());
|
2021-07-27 21:23:25 +00:00
|
|
|
|
|
|
|
// signing a TransactionRequest with the signer as the from address
|
|
|
|
// should yield a signed transaction from the signer
|
|
|
|
let request_from_signer = request.clone().from(client.address());
|
2021-10-29 12:29:35 +00:00
|
|
|
let hash = *client.send_transaction(request_from_signer, None).await.unwrap();
|
2021-08-08 22:49:23 +00:00
|
|
|
let tx = client.get_transaction(hash).await.unwrap().unwrap();
|
|
|
|
assert_eq!(tx.from, client.address());
|
2021-07-27 21:23:25 +00:00
|
|
|
|
|
|
|
// signing a TransactionRequest with a from address that is not the
|
2021-08-08 22:49:23 +00:00
|
|
|
// signer should result in the default ganache account being used
|
|
|
|
let request_from_other = request.from(acc);
|
2021-10-29 12:29:35 +00:00
|
|
|
let hash = *client.send_transaction(request_from_other, None).await.unwrap();
|
2021-08-08 22:49:23 +00:00
|
|
|
let tx = client.get_transaction(hash).await.unwrap().unwrap();
|
|
|
|
assert_eq!(tx.from, acc);
|
2021-07-27 21:23:25 +00:00
|
|
|
}
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|