Skip to content

Commit

Permalink
feat(node-bindings): expose anvil wallet (#1994)
Browse files Browse the repository at this point in the history
* feat(`node-bindings`): expose anvil wallet

* nit

* fix
  • Loading branch information
yash-atreya authored Feb 3, 2025
1 parent 0be4762 commit 370dbef
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
3 changes: 3 additions & 0 deletions crates/node-bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ workspace = true
[dependencies]
alloy-primitives = { workspace = true, features = ["std", "k256", "serde"] }
alloy-genesis.workspace = true
alloy-network.workspace = true
alloy-signer-local.workspace = true
alloy-signer.workspace = true
k256.workspace = true
rand.workspace = true
serde_json = { workspace = true, features = ["std"] }
Expand Down
32 changes: 32 additions & 0 deletions crates/node-bindings/src/nodes/anvil.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! Utilities for launching an Anvil instance.
use crate::NodeError;
use alloy_network::EthereumWallet;
use alloy_primitives::{hex, Address, ChainId};
use alloy_signer::Signer;
use alloy_signer_local::LocalSigner;
use k256::{ecdsa::SigningKey, SecretKey as K256SecretKey};
use std::{
ffi::OsString,
Expand Down Expand Up @@ -29,6 +32,7 @@ pub struct AnvilInstance {
child: Child,
private_keys: Vec<K256SecretKey>,
addresses: Vec<Address>,
wallet: Option<EthereumWallet>,
ipc_path: Option<String>,
port: u16,
chain_id: Option<ChainId>,
Expand Down Expand Up @@ -92,6 +96,11 @@ impl AnvilInstance {
pub fn ws_endpoint_url(&self) -> Url {
Url::parse(&self.ws_endpoint()).unwrap()
}

/// Returns the [`EthereumWallet`] of this instance generated from anvil dev accounts.
pub fn wallet(&self) -> Option<EthereumWallet> {
self.wallet.clone()
}
}

impl Drop for AnvilInstance {
Expand Down Expand Up @@ -328,6 +337,7 @@ impl Anvil {
let mut addresses = Vec::new();
let mut is_private_key = false;
let mut chain_id = None;
let mut wallet = None;
loop {
if start + Duration::from_millis(self.timeout.unwrap_or(ANVIL_STARTUP_TIMEOUT_MILLIS))
<= Instant::now()
Expand Down Expand Up @@ -367,6 +377,27 @@ impl Anvil {
chain_id = Some(chain);
};
}

if !private_keys.is_empty() {
let (default, remaining) = private_keys.split_first().unwrap();
let pks = remaining
.iter()
.map(|key| {
let mut signer = LocalSigner::from(key.clone());
signer.set_chain_id(chain_id);
signer
})
.collect::<Vec<_>>();

let mut default_signer = LocalSigner::from(default.clone());
default_signer.set_chain_id(chain_id);
let mut w = EthereumWallet::new(default_signer);

for pk in pks {
w.register_signer(pk);
}
wallet = Some(w);
}
}

if self.keep_stdout {
Expand All @@ -378,6 +409,7 @@ impl Anvil {
child,
private_keys,
addresses,
wallet,
ipc_path: self.ipc_path,
port,
chain_id: self.chain_id.or(chain_id),
Expand Down
10 changes: 1 addition & 9 deletions crates/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ alloy-json-rpc.workspace = true
alloy-network.workspace = true
alloy-network-primitives.workspace = true
alloy-node-bindings = { workspace = true, optional = true }
alloy-signer-local = { workspace = true, optional = true }
alloy-signer = { workspace = true, optional = true }
alloy-rpc-client.workspace = true
alloy-rpc-types-admin = { workspace = true, optional = true }
alloy-rpc-types-anvil = { workspace = true, optional = true }
Expand Down Expand Up @@ -116,13 +114,7 @@ reqwest-rustls-tls = ["alloy-transport-http?/reqwest-rustls-tls"]
reqwest-native-tls = ["alloy-transport-http?/reqwest-native-tls"]
admin-api = ["dep:alloy-rpc-types-admin"]
anvil-api = ["dep:alloy-rpc-types-anvil"]
anvil-node = [
"anvil-api",
"reqwest",
"dep:alloy-node-bindings",
"dep:alloy-signer-local",
"dep:alloy-signer",
]
anvil-node = ["anvil-api", "reqwest", "dep:alloy-node-bindings"]
debug-api = ["dep:alloy-rpc-types-trace", "dep:alloy-rpc-types-debug"]
erc4337-api = []
engine-api = ["dep:alloy-rpc-types-engine"]
Expand Down
17 changes: 4 additions & 13 deletions crates/provider/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,22 +447,13 @@ impl<L, F> ProviderBuilder<L, F, Ethereum> {
crate::layers::AnvilProvider<crate::provider::RootProvider>,
>,
{
use alloy_signer::Signer;

let anvil_layer = crate::layers::AnvilLayer::from(f(Default::default()));
let url = anvil_layer.endpoint_url();

let default_keys = anvil_layer.instance().keys().to_vec();
let (default_key, remaining_keys) =
default_keys.split_first().ok_or(alloy_node_bindings::NodeError::NoKeysAvailable)?;

let default_signer = alloy_signer_local::LocalSigner::from(default_key.clone())
.with_chain_id(Some(anvil_layer.instance().chain_id()));
let mut wallet = alloy_network::EthereumWallet::from(default_signer);

for key in remaining_keys {
wallet.register_signer(alloy_signer_local::LocalSigner::from(key.clone()))
}
let wallet = anvil_layer
.instance()
.wallet()
.ok_or(alloy_node_bindings::NodeError::NoKeysAvailable)?;

let rpc_client = ClientBuilder::default().http(url);

Expand Down

0 comments on commit 370dbef

Please sign in to comment.