Skip to content

Commit 9e5745a

Browse files
authored
Replace hardcoded mnemonic with a randomly generated one (#230)
* Replace hardcoded mnemonic with a randomly generated one * fix fmt failure
1 parent d44dfa3 commit 9e5745a

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,6 @@ serde_json = "1.0"
125125
# benchmarking
126126
criterion = "0.5"
127127
ethers = "2.0.14"
128+
129+
# rand
130+
rand = "0.8.5"

examples/transactions/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ workspace = true
1616
alloy = { workspace = true, features = ["eip712"]}
1717

1818
eyre.workspace = true
19+
rand.workspace = true
1920
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }

examples/transactions/examples/permit2_signature_transfer.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
//! Example of how to transfer ERC20 tokens from one account to another using a signed permit.
22
3-
use std::str::FromStr;
4-
53
use alloy::{
64
network::EthereumWallet,
75
node_bindings::Anvil,
86
primitives::{Address, U256},
97
providers::{Provider, ProviderBuilder},
10-
signers::{local::PrivateKeySigner, Signer},
8+
signers::{
9+
local::{
10+
coins_bip39::{English, Mnemonic},
11+
PrivateKeySigner,
12+
},
13+
Signer,
14+
},
1115
sol,
1216
sol_types::eip712_domain,
1317
};
1418
use eyre::Result;
19+
use std::str::FromStr;
1520

1621
// Codegen from artifact.
1722
sol!(
@@ -65,13 +70,10 @@ async fn main() -> Result<()> {
6570
// Ensure `anvil` is available in $PATH.
6671
let rpc_url = "https://reth-ethereum.ithaca.xyz/rpc";
6772
// NOTE: ⚠️ Due to changes in EIP-7702 (see: https://getfoundry.sh/anvil/overview/#eip-7702-and-default-accounts),
68-
// the default mnemonic cannot be used for signature-based testing.
69-
// Instead, we use a custom mnemonic generated via:
70-
// `anvil --mnemonic-random 12 --fork-url $RPC_URL`
71-
let anvil = Anvil::new()
72-
.fork(rpc_url)
73-
.mnemonic("tunnel tiger ankle life lift risk wash material promote damp sadness middle")
74-
.try_spawn()?;
73+
// the default mnemonic cannot be used for signature-based testing. Instead, we use a custom
74+
// mnemonic.
75+
let mnemonic = generate_mnemonic()?;
76+
let anvil = Anvil::new().fork(rpc_url).mnemonic(mnemonic).try_spawn()?;
7577

7678
// Set up signers from the first two default Anvil accounts (Alice, Bob).
7779
let alice: PrivateKeySigner = anvil.keys()[8].clone().into();
@@ -146,3 +148,9 @@ async fn main() -> Result<()> {
146148

147149
Ok(())
148150
}
151+
152+
fn generate_mnemonic() -> Result<String> {
153+
let mut rng = rand::thread_rng();
154+
let mnemonic = Mnemonic::<English>::new_with_count(&mut rng, 12)?.to_phrase();
155+
Ok(mnemonic)
156+
}

examples/wallets/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ gcloud-sdk = { version = "0.27", features = [
2323
"google-cloud-kms-v1",
2424
"google-longrunning",
2525
] }
26-
rand = "0.8.5"
26+
rand.workspace = true
2727
serde = { workspace = true, features = ["derive"] }
2828
tempfile = "3.19"
2929
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }

0 commit comments

Comments
 (0)