Skip to content

Commit

Permalink
feat(network): AnyNetworkWallet (#1631)
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-atreya authored Nov 7, 2024
1 parent ae9ac3b commit 0245a4e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
32 changes: 31 additions & 1 deletion crates/network/src/ethereum/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::{Network, NetworkWallet, TxSigner};
use crate::{AnyNetwork, AnyTxEnvelope, AnyTypedTransaction, Network, NetworkWallet, TxSigner};
use alloy_consensus::{SignableTransaction, TxEnvelope, TypedTransaction};
use alloy_primitives::{map::AddressHashMap, Address, PrimitiveSignature as Signature};
use std::sync::Arc;

use super::Ethereum;

/// A wallet capable of signing any transaction for the Ethereum network.
#[derive(Clone, Default)]
pub struct EthereumWallet {
Expand Down Expand Up @@ -139,3 +141,31 @@ where
}
}
}

impl NetworkWallet<AnyNetwork> for EthereumWallet {
fn default_signer_address(&self) -> Address {
self.default
}

fn has_signer_for(&self, address: &Address) -> bool {
self.signers.contains_key(address)
}

fn signer_addresses(&self) -> impl Iterator<Item = Address> {
self.signers.keys().copied()
}

#[doc(alias = "sign_tx_from")]
async fn sign_transaction_from(
&self,
sender: Address,
tx: AnyTypedTransaction,
) -> alloy_signer::Result<AnyTxEnvelope> {
match tx {
AnyTypedTransaction::Ethereum(t) => Ok(AnyTxEnvelope::Ethereum(
NetworkWallet::<Ethereum>::sign_transaction_from(self, sender, t).await?,
)),
_ => Err(alloy_signer::Error::other("cannot sign UnknownTypedTransaction")),
}
}
}
1 change: 1 addition & 0 deletions crates/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ alloy-sol-types.workspace = true
alloy-signer.workspace = true
alloy-signer-local.workspace = true
alloy-transport-http = { workspace = true, features = ["reqwest", "jwt-auth"] }
alloy-serde.workspace = true

itertools.workspace = true
reqwest.workspace = true
Expand Down
28 changes: 27 additions & 1 deletion crates/provider/src/provider/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,11 +1085,12 @@ mod tests {
use super::*;
use crate::{builder, ProviderBuilder, WalletProvider};
use alloy_consensus::Transaction;
use alloy_network::AnyNetwork;
use alloy_network::{AnyNetwork, EthereumWallet, TransactionBuilder};
use alloy_node_bindings::Anvil;
use alloy_primitives::{address, b256, bytes, keccak256};
use alloy_rpc_client::BuiltInConnectionString;
use alloy_rpc_types_eth::{request::TransactionRequest, Block};
use alloy_signer_local::PrivateKeySigner;
// For layer transport tests
#[cfg(feature = "hyper")]
use alloy_transport_http::{
Expand Down Expand Up @@ -1729,6 +1730,31 @@ mod tests {
}
}

#[tokio::test]
async fn any_network_wallet_filler() {
use alloy_serde::WithOtherFields;
let anvil = Anvil::new().spawn();
let signer: PrivateKeySigner =
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80".parse().unwrap();
let wallet = EthereumWallet::from(signer);

let provider = ProviderBuilder::new()
.with_recommended_fillers()
.network::<AnyNetwork>()
.wallet(wallet)
.on_http(anvil.endpoint_url());

let tx = TransactionRequest::default()
.with_to(address!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"))
.value(U256::from(325235));

let tx = WithOtherFields::new(tx);

let builder = provider.send_transaction(tx).await.unwrap().get_receipt().await.unwrap();

assert!(builder.status());
}

#[tokio::test]
async fn builtin_connect_boxed() {
let anvil = Anvil::new().spawn();
Expand Down

0 comments on commit 0245a4e

Please sign in to comment.