From d3dab9fc2b44a490aa613c9303324939d890e5c7 Mon Sep 17 00:00:00 2001 From: Vid Kersic Date: Thu, 24 Nov 2022 21:22:14 +0100 Subject: [PATCH 1/2] 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 From 8b4794de68f83f653cfc249706c1b8eceac84fc2 Mon Sep 17 00:00:00 2001 From: Vid Kersic Date: Fri, 25 Nov 2022 11:15:10 +0100 Subject: [PATCH 2/2] changed all println! to info!, different default output path --- .gitignore | 5 +---- Cargo.lock | 1 + Cargo.toml | 1 + Makefile | 4 ++-- README.md | 4 ++-- bin/bundler.rs | 2 +- bin/create-wallet.rs | 18 ++++++++++++++---- src/models/wallet.rs | 3 +++ src/res/bundler/.gitkeep | 0 src/rpc/eth.rs | 6 +++--- 10 files changed, 28 insertions(+), 16 deletions(-) delete mode 100644 src/res/bundler/.gitkeep diff --git a/.gitignore b/.gitignore index 3de95e19..dac30f6d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,2 @@ # Rust -/target - -# bundler wallets -/src/res/bundler/0x* \ No newline at end of file +/target \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 005b6ef3..02d5e3e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,6 +20,7 @@ dependencies = [ "async-trait", "bytes", "clap", + "dirs", "educe", "ethereum-interfaces", "ethereum-types 0.14.0", diff --git a/Cargo.toml b/Cargo.toml index 52970d9d..98a03e73 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ anyhow = "1" async-trait = "0.1" bytes = { version = "1", features = ["serde"] } clap = { version = "4", features = ["derive"] } +dirs = "4.0" educe = { version = "0.4", features = ["Debug", "Default"] } ethereum-interfaces = { git = "https://github.com/ledgerwatch/interfaces" } ethereum-types = { version = "0.14", features = ["codec"] } diff --git a/Makefile b/Makefile index 872ff4a6..1d589dac 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ run-bundler: - cargo run -- --mnemonic-file ./src/res/bundler/0xF78bB01dFd478608F5738fB0560642b2806D295E + cargo run -- --mnemonic-file ${HOME}/.aa-bundler/0x129D197b2a989C6798601A49D89a4AEC822A17a3 run-bundler-uopool: cargo run --bin bundler-uopool @@ -8,7 +8,7 @@ run-bundler-rpc: cargo run --bin bundler-rpc run-create-wallet: - cargo run --bin create-wallet -- --output-folder ./src/res/bundler + cargo run --bin create-wallet -- --output-path ${HOME}/.aa-bundler cargo-fmt: cargo fmt --all diff --git a/README.md b/README.md index e3671f64..00be94d3 100644 --- a/README.md +++ b/README.md @@ -15,13 +15,13 @@ For more information: https://hackmd.io/@Vid201/aa-bundler-rust Create wallet for bundler: ```bash -cargo run --bin create-wallet -- --output-folder ./src/res/bundler +cargo run --bin create-wallet -- --output-path ${HOME}/.aa-bundler ``` Run bundler (with user operation pool and JSON-RPC API): ```bash -cargo run -- --mnemonic-file ./src/res/bundler/0xF78bB01dFd478608F5738fB0560642b2806D295E +cargo run -- --mnemonic-file ${HOME}/.aa-bundler/0x129D197b2a989C6798601A49D89a4AEC822A17a3 ``` Run only user operation pool: diff --git a/bin/bundler.rs b/bin/bundler.rs index 3bf9cb0d..4c3664b5 100644 --- a/bin/bundler.rs +++ b/bin/bundler.rs @@ -50,7 +50,7 @@ fn main() -> Result<()> { info!("Starting AA - Bundler"); let wallet = Wallet::from_file(opt.mnemonic_file); - println!("{:?}", wallet.signer); + info!("{:?}", wallet.signer); let bundler = Bundler::new(wallet); diff --git a/bin/create-wallet.rs b/bin/create-wallet.rs index 0563e4f0..fd846ac5 100644 --- a/bin/create-wallet.rs +++ b/bin/create-wallet.rs @@ -1,7 +1,10 @@ use aa_bundler::models::wallet::Wallet; use anyhow::Result; use clap::Parser; +use dirs::home_dir; use expanded_pathbuf::ExpandedPathBuf; +use jsonrpsee::tracing::info; +use std::str::FromStr; #[derive(Parser)] #[clap( @@ -9,8 +12,8 @@ use expanded_pathbuf::ExpandedPathBuf; about = "Bundler's wallet creation for EIP-4337 Account Abstraction" )] pub struct Opt { - #[clap(long, default_value = "./src/res/bundler")] - pub output_folder: ExpandedPathBuf, + #[clap(long)] + pub output_path: Option, } fn main() -> Result<()> { @@ -18,8 +21,15 @@ fn main() -> Result<()> { tracing_subscriber::fmt::init(); - let wallet = Wallet::new(opt.output_folder); - println!("{:?}", wallet.signer); + let path = if let Some(output_path) = opt.output_path { + output_path + } else { + ExpandedPathBuf::from_str(home_dir().unwrap().join(".aa-bundler").to_str().unwrap()) + .unwrap() + }; + + let wallet = Wallet::new(path); + info!("{:?}", wallet.signer); Ok(()) } diff --git a/src/models/wallet.rs b/src/models/wallet.rs index c116da67..1dec1df1 100644 --- a/src/models/wallet.rs +++ b/src/models/wallet.rs @@ -3,6 +3,7 @@ use ethers::{ signers::{coins_bip39::English, MnemonicBuilder}, }; use expanded_pathbuf::ExpandedPathBuf; +use std::fs; pub struct Wallet { pub signer: ethers::signers::Wallet, @@ -12,6 +13,8 @@ impl Wallet { pub fn new(output_path: ExpandedPathBuf) -> Self { let mut rng = rand::thread_rng(); + fs::create_dir_all(output_path.to_path_buf()).unwrap(); + Self { signer: MnemonicBuilder::::default() .write_to(output_path.to_path_buf()) diff --git a/src/res/bundler/.gitkeep b/src/res/bundler/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/src/rpc/eth.rs b/src/rpc/eth.rs index c0871f1d..9106216e 100644 --- a/src/rpc/eth.rs +++ b/src/rpc/eth.rs @@ -1,7 +1,7 @@ use crate::{rpc::eth_api::EthApiServer, types::user_operation::UserOperation}; use async_trait::async_trait; use ethereum_types::{Address, U64}; -use jsonrpsee::core::RpcResult; +use jsonrpsee::{core::RpcResult, tracing::info}; pub struct EthApiServerImpl { pub call_gas_limit: u64, @@ -22,8 +22,8 @@ impl EthApiServer for EthApiServerImpl { user_operation: UserOperation, entry_point: Address, ) -> RpcResult { - println!("{:?}", user_operation); - println!("{:?}", entry_point); + info!("{:?}", user_operation); + info!("{:?}", entry_point); Ok(true) } }