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

[wip] migrate to alloy providers and signers #7425

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
739 changes: 481 additions & 258 deletions Cargo.lock

Large diffs are not rendered by default.

40 changes: 19 additions & 21 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ codegen-units = 1
[profile.release.package]
mdbook.opt-level = 1
protobuf.opt-level = 1
rusoto_core.opt-level = 1
rusoto_credential.opt-level = 1
rusoto_kms.opt-level = 1
toml_edit.opt-level = 1
trezor-client.opt-level = 1

Expand Down Expand Up @@ -159,29 +156,30 @@ ethers-middleware = { version = "2.0.14", default-features = false }
ethers-solc = { version = "2.0.14", default-features = false }

## alloy
alloy-consensus = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-contract = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-eips = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-genesis = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-json-rpc = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-network = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-node-bindings = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-provider = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-pubsub = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-rpc-client = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-rpc-trace-types = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-rpc-types = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-signer = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-signer-wallet = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-transport = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-transport-http = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-transport-ipc = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-transport-ws = { git = "https://github.com/klkvr/alloy", rev = "376583d" }
alloy-consensus = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-contract = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-eips = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-genesis = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-json-rpc = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-network = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-node-bindings = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-provider = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-pubsub = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-rpc-client = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-rpc-trace-types = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-rpc-types = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-signer = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-signer-wallet = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-transport = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-transport-http = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-transport-ipc = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-transport-ws = { git = "https://github.com/klkvr/alloy", rev = "376583d", default-features = false }
alloy-primitives = { version = "0.6.4", features = ["getrandom"] }
alloy-dyn-abi = "0.6.4"
alloy-json-abi = "0.6.4"
alloy-sol-types = "0.6.4"
syn-solidity = "0.6.4"

alloy-chains = "0.1"

alloy-rlp = "0.3.3"
Expand Down
5 changes: 2 additions & 3 deletions crates/cast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ serde_json.workspace = true
serde.workspace = true

# aws
rusoto_core = { version = "0.48", default-features = false }
rusoto_kms = { version = "0.48", default-features = false }
aws-sdk-kms = { version = "1", default-features = false }

# bin
foundry-cli.workspace = true
Expand Down Expand Up @@ -91,7 +90,7 @@ criterion = "0.5"
[features]
default = ["rustls"]
rustls = ["foundry-cli/rustls", "foundry-wallets/rustls"]
openssl = ["foundry-cli/openssl", "foundry-wallets/openssl"]
openssl = ["foundry-cli/openssl"]
asm-keccak = ["alloy-primitives/asm-keccak"]

[[bench]]
Expand Down
2 changes: 1 addition & 1 deletion crates/cast/bin/cmd/wallet/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl ListArgs {
.available_senders(self.max_senders.unwrap())
.await?
.iter()
.for_each(|sender| println!("{} ({})", sender.to_alloy(), $label));
.for_each(|sender| println!("{} ({})", sender, $label));
}
}
Err(e) => {
Expand Down
11 changes: 6 additions & 5 deletions crates/cast/bin/cmd/wallet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloy_dyn_abi::TypedData;
use alloy_primitives::{Address, Signature};
use alloy_signer::{
coins_bip39::{English, Mnemonic},
Expand All @@ -7,7 +8,7 @@ use clap::Parser;
use ethers_core::types::transaction::eip712::TypedData;
use ethers_signers::Signer;
use eyre::{Context, Result};
use foundry_common::{fs, types::ToAlloy};
use foundry_common::fs;
use foundry_config::Config;
use foundry_wallets::{RawWalletOpts, WalletOpts, WalletSigner};
use rand::thread_rng;
Expand Down Expand Up @@ -258,7 +259,7 @@ impl WalletSubcommands {
.signer()
.await?;
let addr = wallet.address();
println!("{}", addr.to_alloy().to_checksum(None));
println!("{}", addr.to_checksum(None));
}
WalletSubcommands::Sign { message, data, from_file, no_hash, wallet } => {
let wallet = wallet.signer().await?;
Expand All @@ -270,11 +271,11 @@ impl WalletSubcommands {
// data is a json string
serde_json::from_str(&message)?
};
wallet.sign_typed_data(&typed_data).await?
wallet.sign_typed_data(&typed_data, typed_data.domain()).await?
} else if no_hash {
wallet.sign_hash(&message.parse()?).await?
wallet.sign_hash(message.parse()?).await?
} else {
wallet.sign_message(Self::hex_str_to_bytes(&message)?).await?
wallet.sign_message(&Self::hex_str_to_bytes(&message)?).await?
};
println!("0x{sig}");
}
Expand Down
1 change: 1 addition & 0 deletions crates/cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub use tx::{TxBuilder, TxBuilderOutput, TxBuilderPeekOutput};

use foundry_common::abi::encode_function_args_packed;
pub use foundry_evm::*;
pub use tx::{TxBuilder, TxBuilderOutput};
pub use rusoto_core::{
credential::ChainProvider as AwsChainProvider, region::Region as AwsRegion,
request::HttpClient as AwsHttpClient, Client as AwsClient,
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ tempfile = "3.7"
[features]
default = ["rustls"]
rustls = ["ethers-providers/rustls", "foundry-wallets/rustls"]
openssl = ["ethers-providers/openssl", "foundry-compilers/openssl", "foundry-wallets/openssl"]
openssl = ["ethers-providers/openssl", "foundry-compilers/openssl"]
2 changes: 1 addition & 1 deletion crates/forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ rustls = [
"reqwest/rustls-tls",
"reqwest/rustls-tls-native-roots",
]
openssl = ["foundry-cli/openssl", "reqwest/default-tls", "foundry-wallets/openssl"]
openssl = ["foundry-cli/openssl", "reqwest/default-tls"]
asm-keccak = ["alloy-primitives/asm-keccak"]

[[bench]]
Expand Down
2 changes: 1 addition & 1 deletion crates/script/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl ScriptArgs {
.wallets
.private_keys()?
.filter(|pks| pks.len() == 1)
.map(|pks| pks.first().unwrap().address().to_alloy());
.map(|pks| pks.first().unwrap().address());
Ok(maybe_sender)
}

Expand Down
16 changes: 8 additions & 8 deletions crates/wallets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ repository.workspace = true

[dependencies]
alloy-primitives.workspace = true
alloy-signer = { workspace = true, features = ["eip712"] }
alloy-signer-aws.workspace = true
alloy-signer-ledger.workspace = true
alloy-signer-trezor.workspace = true
alloy-sol-types.workspace = true

ethers-core.workspace = true
ethers-providers.workspace = true
ethers-signers = { workspace = true, features = ["aws", "ledger", "trezor"] }

rusoto_core = { version = "0.48", default-features = false }
rusoto_kms = { version = "0.48", default-features = false }
aws-sdk-kms = { version = "1", default-features = false }
aws-config = "1"

foundry-config.workspace = true
foundry-common.workspace = true
Expand All @@ -38,5 +39,4 @@ tokio = { version = "1", features = ["macros"] }

[features]
default = ["rustls"]
rustls = ["ethers-providers/rustls", "rusoto_core/rustls"]
openssl = ["ethers-providers/openssl"]
rustls = ["aws-sdk-kms/rustls"]
7 changes: 6 additions & 1 deletion crates/wallets/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use ethers_signers::{AwsSignerError, LedgerError, TrezorError, WalletError};
use alloy_signer::{k256::ecdsa, WalletError};
use alloy_signer_aws::AwsSignerError;
use alloy_signer_ledger::LedgerError;
use alloy_signer_trezor::TrezorError;
use hex::FromHexError;

#[derive(Debug, thiserror::Error)]
Expand All @@ -23,6 +26,8 @@ pub enum WalletSignerError {
Io(#[from] std::io::Error),
#[error(transparent)]
InvalidHex(#[from] FromHexError),
#[error(transparent)]
Ecdsa(#[from] ecdsa::Error),
#[error("{0} cannot sign raw hashes")]
CannotSignRawHash(&'static str),
}
12 changes: 5 additions & 7 deletions crates/wallets/src/multi_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use crate::{
wallet_signer::{PendingSigner, WalletSigner},
};
use alloy_primitives::Address;
use alloy_signer::Signer;
use clap::Parser;
use derive_builder::Builder;
use ethers_signers::Signer;
use eyre::Result;
use foundry_common::types::ToAlloy;
use foundry_config::Config;
use serde::Serialize;
use std::{collections::HashMap, iter::repeat, path::PathBuf};
Expand All @@ -24,15 +23,14 @@ pub struct MultiWallet {

impl MultiWallet {
pub fn new(pending_signers: Vec<PendingSigner>, signers: Vec<WalletSigner>) -> Self {
let signers =
signers.into_iter().map(|signer| (signer.address().to_alloy(), signer)).collect();
let signers = signers.into_iter().map(|signer| (signer.address(), signer)).collect();
Self { pending_signers, signers }
}

fn maybe_unlock_pending(&mut self) -> Result<()> {
for pending in self.pending_signers.drain(..) {
let signer = pending.unlock()?;
self.signers.insert(signer.address().to_alloy(), signer);
self.signers.insert(signer.address(), signer);
}
Ok(())
}
Expand All @@ -48,7 +46,7 @@ impl MultiWallet {
}

pub fn add_signer(&mut self, signer: WalletSigner) {
self.signers.insert(signer.address().to_alloy(), signer);
self.signers.insert(signer.address(), signer);
}
}

Expand Down Expand Up @@ -386,7 +384,7 @@ impl MultiWalletOpts {
.collect::<Vec<_>>();

for key in aws_keys {
let aws_signer = WalletSigner::from_aws(&key).await?;
let aws_signer = WalletSigner::from_aws(key).await?;
wallets.push(aws_signer)
}

Expand Down
42 changes: 19 additions & 23 deletions crates/wallets/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
use crate::{error::PrivateKeyError, PendingSigner, WalletSigner};
use ethers_signers::{HDPath as LedgerHDPath, LocalWallet, TrezorHDPath, WalletError};
use alloy_primitives::B256;
use alloy_signer::LocalWallet;
use alloy_signer_ledger::HDPath as LedgerHDPath;
use alloy_signer_trezor::TrezorHDPath;
use eyre::{Context, Result};
use foundry_config::Config;
use std::{
fs,
path::{Path, PathBuf},
str::FromStr,
};

fn ensure_pk_not_env(pk: &str) -> Result<()> {
if !pk.starts_with("0x") && std::env::var(pk).is_ok() {
return Err(PrivateKeyError::ExistsAsEnvVar(pk.to_string()).into());
}
Ok(())
}

/// Validates and sanitizes user inputs, returning configured [WalletSigner].
pub fn create_private_key_signer(private_key: &str) -> Result<WalletSigner> {
let privk = private_key.trim().strip_prefix("0x").unwrap_or(private_key);
match LocalWallet::from_str(privk) {

let Ok(private_key) = hex::decode(privk) else {
ensure_pk_not_env(privk)?;
eyre::bail!("Failed to decode private key")
};

match LocalWallet::from_bytes(&B256::from_slice(&private_key)) {
Ok(pk) => Ok(WalletSigner::Local(pk)),
Err(err) => {
// helper closure to check if pk was meant to be an env var, this usually happens if
// `$` is missing
let ensure_not_env = |pk: &str| {
// check if pk was meant to be an env var
if !pk.starts_with("0x") && std::env::var(pk).is_ok() {
// SAFETY: at this point we know the user actually wanted to use an env var
// and most likely forgot the `$` anchor, so the
// `private_key` here is an unresolved env var
return Err(PrivateKeyError::ExistsAsEnvVar(pk.to_string()))
}
Ok(())
};
match err {
WalletError::HexError(err) => {
ensure_not_env(private_key)?;
return Err(PrivateKeyError::InvalidHex(err).into());
}
WalletError::EcdsaError(_) => ensure_not_env(private_key)?,
_ => {}
};
ensure_pk_not_env(privk)?;
eyre::bail!("Failed to create wallet from private key: {err}")
}
}
Expand Down
7 changes: 3 additions & 4 deletions crates/wallets/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::{raw_wallet::RawWalletOpts, utils, wallet_signer::WalletSigner};
use alloy_primitives::Address;
use alloy_signer::Signer;
use clap::Parser;
use ethers_signers::Signer;
use eyre::Result;
use foundry_common::types::ToAlloy;
use serde::Serialize;

/// The wallet options can either be:
Expand Down Expand Up @@ -95,7 +94,7 @@ impl WalletOpts {
.await?
} else if self.aws {
let key_id = std::env::var("AWS_KMS_KEY_ID")?;
WalletSigner::from_aws(&key_id).await?
WalletSigner::from_aws(key_id).await?
} else if let Some(raw_wallet) = self.raw.signer()? {
raw_wallet
} else if let Some(path) = utils::maybe_get_keystore_path(
Expand Down Expand Up @@ -139,7 +138,7 @@ of the unlocked account you want to use, or provide the --from flag with the add
if let Some(from) = self.from {
from
} else if let Ok(signer) = self.signer().await {
signer.address().to_alloy()
signer.address()
} else {
Address::ZERO
}
Expand Down
Loading
Loading