Skip to content

Commit

Permalink
init foundry-wallets (#7086)
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr authored Feb 12, 2024
1 parent 93f094b commit b783a50
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 106 deletions.
34 changes: 26 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ foundry-evm-fuzz = { path = "crates/evm/fuzz" }
foundry-evm-traces = { path = "crates/evm/traces" }
foundry-macros = { path = "crates/macros" }
foundry-test-utils = { path = "crates/test-utils" }
foundry-wallets = { path = "crates/wallets" }

# solc & compilation utilities
foundry-block-explorers = { version = "0.2.3", default-features = false }
Expand Down
5 changes: 3 additions & 2 deletions crates/cast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ foundry-common.workspace = true
foundry-compilers.workspace = true
foundry-config.workspace = true
foundry-evm.workspace = true
foundry-wallets.workspace = true

alloy-dyn-abi.workspace = true
alloy-json-abi.workspace = true
Expand Down Expand Up @@ -78,8 +79,8 @@ criterion = "0.5"

[features]
default = ["rustls"]
rustls = ["foundry-cli/rustls"]
openssl = ["foundry-cli/openssl"]
rustls = ["foundry-cli/rustls", "foundry-wallets/rustls"]
openssl = ["foundry-cli/openssl", "foundry-wallets/openssl"]
asm-keccak = ["alloy-primitives/asm-keccak"]

[[bench]]
Expand Down
2 changes: 1 addition & 1 deletion crates/cast/bin/cmd/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use ethers_signers::{
LocalWallet, MnemonicBuilder, Signer,
};
use eyre::{Context, Result};
use foundry_cli::opts::{RawWallet, Wallet};
use foundry_common::{
fs,
types::{ToAlloy, ToEthers},
};
use foundry_config::Config;
use foundry_wallets::{RawWallet, Wallet};
use serde_json::json;
use std::path::Path;
use yansi::Paint;
Expand Down
14 changes: 3 additions & 11 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ foundry-common.workspace = true
foundry-config.workspace = true
foundry-debugger.workspace = true
foundry-evm.workspace = true
foundry-wallets.workspace = true

foundry-compilers = { workspace = true, features = ["full"] }

Expand All @@ -24,26 +25,17 @@ alloy-primitives.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 }

async-trait = "0.1"
clap = { version = "4", features = ["derive", "env", "unicode", "wrap_help"] }
color-eyre.workspace = true
dotenvy = "0.15"
eyre.workspace = true
hex = { workspace = true, features = ["serde"] }
indicatif = "0.17"
itertools.workspace = true
once_cell = "1"
regex = { version = "1", default-features = false }
rpassword = "7"
serde.workspace = true
strsim = "0.10"
strum = { workspace = true, features = ["derive"] }
thiserror = "1"
tokio = { version = "1", features = ["macros"] }
tracing-error = "0.2"
tracing-subscriber = { workspace = true, features = ["registry", "env-filter", "fmt"] }
Expand All @@ -55,5 +47,5 @@ tempfile = "3.7"

[features]
default = ["rustls"]
rustls = ["ethers-providers/rustls", "rusoto_core/rustls"]
openssl = ["ethers-providers/openssl", "foundry-compilers/openssl"]
rustls = ["ethers-providers/rustls", "foundry-wallets/rustls"]
openssl = ["ethers-providers/openssl", "foundry-compilers/openssl", "foundry-wallets/openssl"]
3 changes: 2 additions & 1 deletion crates/cli/src/opts/ethereum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{ChainValueParser, Wallet, WalletSigner};
use crate::opts::ChainValueParser;
use clap::Parser;
use eyre::Result;
use foundry_config::{
Expand All @@ -9,6 +9,7 @@ use foundry_config::{
},
impl_figment_convert_cast, Chain, Config,
};
use foundry_wallets::{Wallet, WalletSigner};
use serde::Serialize;
use std::borrow::Cow;

Expand Down
2 changes: 0 additions & 2 deletions crates/cli/src/opts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ mod chain;
mod dependency;
mod ethereum;
mod transaction;
mod wallet;

pub use build::*;
pub use chain::*;
pub use dependency::*;
pub use ethereum::*;
pub use transaction::*;
pub use wallet::*;
11 changes: 0 additions & 11 deletions crates/cli/src/opts/wallet/error.rs

This file was deleted.

5 changes: 3 additions & 2 deletions crates/forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ foundry-common.workspace = true
foundry-compilers = { workspace = true, features = ["full"] }
foundry-config.workspace = true
foundry-evm.workspace = true
foundry-wallets.workspace = true

ethers-contract.workspace = true
ethers-core.workspace = true
Expand Down Expand Up @@ -98,8 +99,8 @@ tracing-subscriber = { workspace = true, features = ["env-filter", "fmt"] }

[features]
default = ["rustls"]
rustls = ["foundry-cli/rustls", "reqwest/rustls-tls", "reqwest/rustls-tls-native-roots"]
openssl = ["foundry-cli/openssl", "reqwest/default-tls"]
rustls = ["foundry-cli/rustls", "foundry-wallets/rustls", "reqwest/rustls-tls", "reqwest/rustls-tls-native-roots"]
openssl = ["foundry-cli/openssl", "reqwest/default-tls", "foundry-wallets/openssl"]
asm-keccak = ["alloy-primitives/asm-keccak"]

[[bench]]
Expand Down
5 changes: 2 additions & 3 deletions crates/forge/bin/cmd/script/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ use ethers_providers::{JsonRpcClient, Middleware, Provider};
use ethers_signers::Signer;
use eyre::{bail, ContextCompat, Result, WrapErr};
use foundry_cli::{
init_progress,
opts::WalletSigner,
update_progress,
init_progress, update_progress,
utils::{has_batch_support, has_different_gas_calc},
};
use foundry_common::{
provider::ethers::{estimate_eip1559_fees, try_get_http_provider, RetryProvider},
shell,
types::{ToAlloy, ToEthers},
};
use foundry_wallets::WalletSigner;
use futures::StreamExt;
use std::{cmp::min, collections::HashSet, ops::Mul, sync::Arc};

Expand Down
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use forge::{
render_trace_arena, CallTraceDecoder, CallTraceDecoderBuilder, TraceKind, Traces,
},
};
use foundry_cli::opts::MultiWallet;
use foundry_common::{
abi::{encode_function_args, get_func},
contracts::get_contract_name,
Expand Down Expand Up @@ -47,6 +46,7 @@ use foundry_evm::{
decode,
inspectors::cheatcodes::{BroadcastableTransaction, BroadcastableTransactions},
};
use foundry_wallets::MultiWallet;
use futures::future;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
Expand Down
38 changes: 38 additions & 0 deletions crates/wallets/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "foundry-wallets"

version.workspace = true
edition.workspace = true
rust-version.workspace = true
authors.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true

[dependencies]
alloy-primitives.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 }

foundry-config.workspace = true
foundry-common.workspace = true

async-trait = "0.1"
clap = { version = "4", features = ["derive", "env", "unicode", "wrap_help"] }
eyre.workspace = true
hex = { workspace = true, features = ["serde"] }
itertools.workspace = true
rpassword = "7"
serde.workspace = true
thiserror = "1"
tracing.workspace = true

[features]
default = ["rustls"]
rustls = ["ethers-providers/rustls", "rusoto_core/rustls"]
openssl = ["ethers-providers/openssl"]
22 changes: 22 additions & 0 deletions crates/wallets/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use ethers_signers::{AwsSignerError, LedgerError, TrezorError, WalletError};
use hex::FromHexError;

#[derive(Debug, thiserror::Error)]
pub enum PrivateKeyError {
#[error("Failed to create wallet from private key. Private key is invalid hex: {0}")]
InvalidHex(#[from] FromHexError),
#[error("Failed to create wallet from private key. Invalid private key. But env var {0} exists. Is the `$` anchor missing?")]
ExistsAsEnvVar(String),
}

#[derive(Debug, thiserror::Error)]
pub enum WalletSignerError {
#[error(transparent)]
Local(#[from] WalletError),
#[error(transparent)]
Ledger(#[from] LedgerError),
#[error(transparent)]
Trezor(#[from] TrezorError),
#[error(transparent)]
Aws(#[from] AwsSignerError),
}
11 changes: 11 additions & 0 deletions crates/wallets/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[macro_use]
extern crate tracing;

pub mod error;
pub mod multi_wallet;
pub mod raw_wallet;
pub mod wallet;

pub use multi_wallet::MultiWallet;
pub use raw_wallet::RawWallet;
pub use wallet::{Wallet, WalletSigner};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{WalletSigner, WalletTrait};
use crate::wallet::{WalletSigner, WalletTrait};
use alloy_primitives::Address;
use clap::Parser;
use ethers_providers::Middleware;
Expand Down
39 changes: 39 additions & 0 deletions crates/wallets/src/raw_wallet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use clap::Parser;
use serde::Serialize;

/// A wrapper for the raw data options for `Wallet`, extracted to also be used standalone.
/// The raw wallet options can either be:
/// 1. Private Key (cleartext in CLI)
/// 2. Private Key (interactively via secure prompt)
/// 3. Mnemonic (via file path)
#[derive(Clone, Debug, Default, Serialize, Parser)]
#[clap(next_help_heading = "Wallet options - raw", about = None, long_about = None)]
pub struct RawWallet {
/// Open an interactive prompt to enter your private key.
#[clap(long, short)]
pub interactive: bool,

/// Use the provided private key.
#[clap(long, value_name = "RAW_PRIVATE_KEY")]
pub private_key: Option<String>,

/// Use the mnemonic phrase of mnemonic file at the specified path.
#[clap(long, alias = "mnemonic-path")]
pub mnemonic: Option<String>,

/// Use a BIP39 passphrase for the mnemonic.
#[clap(long, value_name = "PASSPHRASE")]
pub mnemonic_passphrase: Option<String>,

/// The wallet derivation path.
///
/// Works with both --mnemonic-path and hardware wallets.
#[clap(long = "mnemonic-derivation-path", alias = "hd-path", value_name = "PATH")]
pub hd_path: Option<String>,

/// Use the private key from the given mnemonic index.
///
/// Used with --mnemonic-path.
#[clap(long, conflicts_with = "hd_path", default_value_t = 0, value_name = "INDEX")]
pub mnemonic_index: u32,
}
Loading

0 comments on commit b783a50

Please sign in to comment.