From d3dab9fc2b44a490aa613c9303324939d890e5c7 Mon Sep 17 00:00:00 2001 From: Vid Kersic Date: Thu, 24 Nov 2022 21:22:14 +0100 Subject: [PATCH] Bundler wallet (#2) --- .gitignore | 4 +++ Cargo.toml | 4 +++ Makefile | 5 ++- README.md | 14 +++++--- bin/bundler-rpc.rs | 4 +-- bin/bundler.rs | 32 ++++++------------- bin/create-wallet.rs | 25 +++++++++++++++ src/bundler/bundler.rs | 11 +++++++ src/bundler/mod.rs | 1 + src/lib.rs | 1 + src/models/mod.rs | 1 + src/models/wallet.rs | 31 ++++++++++++++++++ ...0xD00D3EEc454D05d3d9bB48532BabED0c89941f17 | 1 - 13 files changed, 104 insertions(+), 30 deletions(-) create mode 100644 bin/create-wallet.rs create mode 100644 src/bundler/bundler.rs create mode 100644 src/bundler/mod.rs create mode 100644 src/models/wallet.rs delete mode 100644 src/res/bundler/0xD00D3EEc454D05d3d9bB48532BabED0c89941f17 diff --git a/.gitignore b/.gitignore index ea8c4bf7..3de95e19 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ +# Rust /target + +# bundler wallets +/src/res/bundler/0x* \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 6c051f99..52970d9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,3 +45,7 @@ name = "bundler-uopool" [[bin]] path = "bin/bundler-rpc.rs" name = "bundler-rpc" + +[[bin]] +path = "bin/create-wallet.rs" +name = "create-wallet" diff --git a/Makefile b/Makefile index 321e6425..872ff4a6 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ run-bundler: - cargo run -- --mnemonic-file ./src/res/bundler/0xD00D3EEc454D05d3d9bB48532BabED0c89941f17 + cargo run -- --mnemonic-file ./src/res/bundler/0xF78bB01dFd478608F5738fB0560642b2806D295E run-bundler-uopool: cargo run --bin bundler-uopool @@ -7,5 +7,8 @@ run-bundler-uopool: run-bundler-rpc: cargo run --bin bundler-rpc +run-create-wallet: + cargo run --bin create-wallet -- --output-folder ./src/res/bundler + cargo-fmt: cargo fmt --all diff --git a/README.md b/README.md index 01ca7cd4..e3671f64 100644 --- a/README.md +++ b/README.md @@ -12,19 +12,25 @@ For more information: https://hackmd.io/@Vid201/aa-bundler-rust ## How to run? -Bundler: +Create wallet for bundler: ```bash -cargo run -- --mnemonic-folder ./src/res/bundler +cargo run --bin create-wallet -- --output-folder ./src/res/bundler ``` -User operation pool: +Run bundler (with user operation pool and JSON-RPC API): + +```bash +cargo run -- --mnemonic-file ./src/res/bundler/0xF78bB01dFd478608F5738fB0560642b2806D295E +``` + +Run only user operation pool: ```bash cargo run --bin bundler-uopool ``` -Bundler RPC: +Run only JSON-RPC API: ```bash cargo run --bin bundler-rpc diff --git a/bin/bundler-rpc.rs b/bin/bundler-rpc.rs index f3a81553..32a96215 100644 --- a/bin/bundler-rpc.rs +++ b/bin/bundler-rpc.rs @@ -9,7 +9,7 @@ use aa_bundler::rpc::{eth::EthApiServerImpl, eth_api::EthApiServer}; #[derive(Parser)] #[clap( name = "aa-bundler-rpc", - about = "RPC server for EIP-4337 Account Abstraction Bundler" + about = "JSON-RPC server for EIP-4337 Account Abstraction Bundler" )] pub struct Opt { #[clap(long, default_value = "127.0.0.1:4337")] @@ -36,7 +36,7 @@ async fn main() -> Result<()> { .unwrap(); let _jsonrpc_server_handle = jsonrpc_server.start(api.clone())?; - info!("JSONRPC server listening on {}", opt.rpc_listen_address); + info!("JSON-RPC server listening on {}", opt.rpc_listen_address); pending().await } diff --git a/bin/bundler.rs b/bin/bundler.rs index 6849d77a..3bf9cb0d 100644 --- a/bin/bundler.rs +++ b/bin/bundler.rs @@ -1,10 +1,10 @@ -use aa_bundler::rpc::{eth::EthApiServerImpl, eth_api::EthApiServer}; +use aa_bundler::{ + bundler::bundler::Bundler, + models::wallet::Wallet, + rpc::{eth::EthApiServerImpl, eth_api::EthApiServer}, +}; use anyhow::Result; use clap::Parser; -use ethers::{ - prelude::rand, - signers::{coins_bip39::English, MnemonicBuilder}, -}; use expanded_pathbuf::ExpandedPathBuf; use jsonrpsee::{core::server::rpc_module::Methods, server::ServerBuilder, tracing::info}; use std::{future::pending, panic}; @@ -16,10 +16,7 @@ use std::{future::pending, panic}; )] pub struct Opt { #[clap(long)] - pub mnemonic_file: Option, - - #[clap(long, default_value = "./src/res/bundler")] - pub mnemonic_folder: ExpandedPathBuf, + pub mnemonic_file: ExpandedPathBuf, // #[clap(long, default_value = "127.0.0.1:3000")] // pub grpc_listen_address: String, @@ -52,19 +49,10 @@ fn main() -> Result<()> { rt.block_on(async move { info!("Starting AA - Bundler"); - // TODO: move this to bundler package - let wallet = if let Some(mnemonic_file) = opt.mnemonic_file { - MnemonicBuilder::::default() - .phrase(mnemonic_file.to_path_buf()) - .build()? - } else { - let mut rng = rand::thread_rng(); - MnemonicBuilder::::default() - .write_to(opt.mnemonic_folder.to_path_buf()) - .build_random(&mut rng)? - }; + let wallet = Wallet::from_file(opt.mnemonic_file); + println!("{:?}", wallet.signer); - println!("{:?}", wallet); + let bundler = Bundler::new(wallet); if !opt.no_uopool { aa_bundler::uopool::run(opt.uopool_opts).await?; @@ -88,7 +76,7 @@ fn main() -> Result<()> { .unwrap(); let _jsonrpc_server_handle = jsonrpc_server.start(api.clone()).unwrap(); - info!("JSONRPC server listening on {}", opt.rpc_listen_address); + info!("JSON-RPC server listening on {}", opt.rpc_listen_address); pending::<()>().await } diff --git a/bin/create-wallet.rs b/bin/create-wallet.rs new file mode 100644 index 00000000..0563e4f0 --- /dev/null +++ b/bin/create-wallet.rs @@ -0,0 +1,25 @@ +use aa_bundler::models::wallet::Wallet; +use anyhow::Result; +use clap::Parser; +use expanded_pathbuf::ExpandedPathBuf; + +#[derive(Parser)] +#[clap( + name = "aa-bundler-create-wallet", + about = "Bundler's wallet creation for EIP-4337 Account Abstraction" +)] +pub struct Opt { + #[clap(long, default_value = "./src/res/bundler")] + pub output_folder: ExpandedPathBuf, +} + +fn main() -> Result<()> { + let opt: Opt = Opt::parse(); + + tracing_subscriber::fmt::init(); + + let wallet = Wallet::new(opt.output_folder); + println!("{:?}", wallet.signer); + + Ok(()) +} diff --git a/src/bundler/bundler.rs b/src/bundler/bundler.rs new file mode 100644 index 00000000..b1e967a3 --- /dev/null +++ b/src/bundler/bundler.rs @@ -0,0 +1,11 @@ +use crate::models::wallet::Wallet; + +pub struct Bundler { + pub wallet: Wallet, +} + +impl Bundler { + pub fn new(wallet: Wallet) -> Self { + Self { wallet } + } +} diff --git a/src/bundler/mod.rs b/src/bundler/mod.rs new file mode 100644 index 00000000..f6b97184 --- /dev/null +++ b/src/bundler/mod.rs @@ -0,0 +1 @@ +pub mod bundler; diff --git a/src/lib.rs b/src/lib.rs index a38f7d4f..d9a8b45e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod bundler; pub mod models; pub mod rpc; pub mod types; diff --git a/src/models/mod.rs b/src/models/mod.rs index ea09c2f7..d6eaecd5 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1 +1,2 @@ pub mod chainspec; +pub mod wallet; diff --git a/src/models/wallet.rs b/src/models/wallet.rs new file mode 100644 index 00000000..c116da67 --- /dev/null +++ b/src/models/wallet.rs @@ -0,0 +1,31 @@ +use ethers::{ + prelude::{k256::ecdsa::SigningKey, rand}, + signers::{coins_bip39::English, MnemonicBuilder}, +}; +use expanded_pathbuf::ExpandedPathBuf; + +pub struct Wallet { + pub signer: ethers::signers::Wallet, +} + +impl Wallet { + pub fn new(output_path: ExpandedPathBuf) -> Self { + let mut rng = rand::thread_rng(); + + Self { + signer: MnemonicBuilder::::default() + .write_to(output_path.to_path_buf()) + .build_random(&mut rng) + .unwrap(), + } + } + + pub fn from_file(input_path: ExpandedPathBuf) -> Self { + Self { + signer: MnemonicBuilder::::default() + .phrase(input_path.to_path_buf()) + .build() + .unwrap(), + } + } +} diff --git a/src/res/bundler/0xD00D3EEc454D05d3d9bB48532BabED0c89941f17 b/src/res/bundler/0xD00D3EEc454D05d3d9bB48532BabED0c89941f17 deleted file mode 100644 index a211a71c..00000000 --- a/src/res/bundler/0xD00D3EEc454D05d3d9bB48532BabED0c89941f17 +++ /dev/null @@ -1 +0,0 @@ -pass erase drink consider keen bid lottery country stool actress sword asthma \ No newline at end of file