Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(network): impl NetworkWallet<AnyNetwork> #1631

Merged
merged 5 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion crates/network/src/ethereum/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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;
Expand Down Expand Up @@ -139,3 +139,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) => match t {
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
Loading