reth_e2e_test_utils/
wallet.rs

1use alloy_signer::Signer;
2use alloy_signer_local::{coins_bip39::English, MnemonicBuilder, PrivateKeySigner};
3
4/// One of the accounts of the genesis allocations.
5#[derive(Debug)]
6pub struct Wallet {
7    /// The signer
8    pub inner: PrivateKeySigner,
9    /// The nonce
10    pub inner_nonce: u64,
11    /// The chain id
12    pub chain_id: u64,
13    amount: usize,
14    derivation_path: Option<String>,
15}
16
17impl Wallet {
18    /// Creates a new account from one of the secret/pubkeys of the genesis allocations (test.json)
19    pub fn new(amount: usize) -> Self {
20        let inner = MnemonicBuilder::<English>::default().phrase(TEST_MNEMONIC).build().unwrap();
21        Self { inner, chain_id: 1, amount, derivation_path: None, inner_nonce: 0 }
22    }
23
24    /// Sets chain id
25    pub const fn with_chain_id(mut self, chain_id: u64) -> Self {
26        self.chain_id = chain_id;
27        self
28    }
29
30    fn get_derivation_path(&self) -> &str {
31        self.derivation_path.as_deref().unwrap_or("m/44'/60'/0'/0/")
32    }
33
34    /// Generates a list of wallets
35    pub fn wallet_gen(&self) -> Vec<PrivateKeySigner> {
36        let builder = MnemonicBuilder::<English>::default().phrase(TEST_MNEMONIC);
37
38        // use the derivation path
39        let derivation_path = self.get_derivation_path();
40
41        let mut wallets = Vec::with_capacity(self.amount);
42        for idx in 0..self.amount {
43            let builder =
44                builder.clone().derivation_path(format!("{derivation_path}{idx}")).unwrap();
45            let wallet = builder.build().unwrap().with_chain_id(Some(self.chain_id));
46            wallets.push(wallet)
47        }
48        wallets
49    }
50}
51
52const TEST_MNEMONIC: &str = "test test test test test test test test test test test junk";
53
54impl Default for Wallet {
55    fn default() -> Self {
56        Self::new(1)
57    }
58}