Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User operation pack and hash (#7) #19

Merged
merged 3 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ default-run = "bundler"
[dependencies]
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"] }
ethers = "1.0.0"
expanded-pathbuf = "0.1"
hex = { version = "0.4.3", default-features = false, features = ["std"] }
jsonrpsee = { version = "0.16", features = ["server", "macros"] }
parking_lot = "0.12"
prost = "0.11"
ron = "0.8"
serde = "1"
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ run-create-wallet:
cargo-fmt:
cargo fmt --all

cargo-test:
cargo test

fetch-thirdparty:
git submodule update --init
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ For more information: https://hackmd.io/@Vid201/aa-bundler-rust

## Prerequisites

1. [install solc](https://docs.soliditylang.org/en/v0.8.17/installing-solidity.html) >=0.8.12
1. [solc](https://docs.soliditylang.org/en/v0.8.17/installing-solidity.html) >=0.8.12

## How to run?

Expand Down
2 changes: 1 addition & 1 deletion src/rpc/eth.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{rpc::eth_api::EthApiServer, types::user_operation::UserOperation};
use async_trait::async_trait;
use ethereum_types::{Address, U64};
use ethers::types::{Address, U64};
use jsonrpsee::{core::RpcResult, tracing::info};

pub struct EthApiServerImpl {
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/eth_api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ethereum_types::{Address, U64};
use ethers::types::{Address, U64};
use jsonrpsee::{core::RpcResult, proc_macros::rpc};

use crate::types::user_operation::UserOperation;
Expand Down
169 changes: 166 additions & 3 deletions src/types/user_operation.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use bytes::Bytes;
use ethereum_types::{Address, U256};
use ethers::abi::AbiEncode;
use ethers::prelude::{EthAbiCodec, EthAbiType};
use ethers::types::{Address, Bytes, H256, U256};
use ethers::utils::keccak256;
use serde::{Deserialize, Serialize};
use std::str::FromStr;

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub type UserOperationHash = H256;

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, EthAbiCodec, EthAbiType)]
#[serde(rename_all = "camelCase")]
pub struct UserOperation {
pub sender: Address,
Expand All @@ -17,3 +22,161 @@ pub struct UserOperation {
pub paymaster_and_data: Bytes,
pub signature: Bytes,
}

impl UserOperation {
pub fn pack(&self) -> Vec<u8> {
self.clone().encode_hex().into_bytes()
}

pub fn pack_for_signature(&self) -> Vec<u8> {
zsluedem marked this conversation as resolved.
Show resolved Hide resolved
let mut encoded = String::from("0x");
let packed = hex::encode(
UserOperation {
signature: Bytes::from_str("0x").unwrap(),
..self.clone()
}
.encode(),
);
encoded.push_str(&packed[..packed.len() - 64]);
encoded.into_bytes()
}

pub fn hash(&self, entry_point_address: Address, chain_id: U256) -> UserOperationHash {
H256::from_slice(
keccak256(
[
keccak256(hex::decode(&self.pack_for_signature()[2..]).unwrap()).to_vec(),
entry_point_address.encode(),
chain_id.encode(),
]
.concat(),
)
.as_slice(),
)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn user_operation_pack() {
let user_operations = vec![
UserOperation {
sender: Address::zero(),
nonce: U256::zero(),
init_code: Bytes::default(),
call_data: Bytes::default(),
call_gas_limit: U256::zero(),
verification_gas_limit: U256::from(100000),
pre_verification_gas: U256::from(21000),
max_fee_per_gas: U256::zero(),
max_priority_fee_per_gas: U256::from(1e9 as u64),
paymaster_and_data: Bytes::default(),
signature: Bytes::default(),
},
UserOperation {
sender: "0x663F3ad617193148711d28f5334eE4Ed07016602".parse().unwrap(),
nonce: U256::zero(),
init_code: Bytes::default(),
call_data: Bytes::default(),
call_gas_limit: U256::from(200000),
verification_gas_limit: U256::from(100000),
pre_verification_gas: U256::from(21000),
max_fee_per_gas: U256::from(3000000000 as u64),
max_priority_fee_per_gas: U256::from(1000000000),
paymaster_and_data: Bytes::default(),
signature: Bytes::from_str("0x7cb39607585dee8e297d0d7a669ad8c5e43975220b6773c10a138deadbc8ec864981de4b9b3c735288a217115fb33f8326a61ddabc60a534e3b5536515c70f931c").unwrap(),
},
];
assert_eq!(user_operations[0].pack(), String::from("0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000186a000000000000000000000000000000000000000000000000000000000000052080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003b9aca0000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").into_bytes());
assert_eq!(user_operations[1].pack(), String::from("0x000000000000000000000000663f3ad617193148711d28f5334ee4ed070166020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000030d4000000000000000000000000000000000000000000000000000000000000186a0000000000000000000000000000000000000000000000000000000000000520800000000000000000000000000000000000000000000000000000000b2d05e00000000000000000000000000000000000000000000000000000000003b9aca0000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000417cb39607585dee8e297d0d7a669ad8c5e43975220b6773c10a138deadbc8ec864981de4b9b3c735288a217115fb33f8326a61ddabc60a534e3b5536515c70f931c00000000000000000000000000000000000000000000000000000000000000").into_bytes());
zsluedem marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
fn user_operation_pack_for_signature() {
let user_operations = vec![
UserOperation {
sender: Address::zero(),
nonce: U256::zero(),
init_code: Bytes::default(),
call_data: Bytes::default(),
call_gas_limit: U256::zero(),
verification_gas_limit: U256::from(100000),
pre_verification_gas: U256::from(21000),
max_fee_per_gas: U256::zero(),
max_priority_fee_per_gas: U256::from(1e9 as u64),
paymaster_and_data: Bytes::default(),
signature: Bytes::default(),
},
UserOperation {
sender: "0x663F3ad617193148711d28f5334eE4Ed07016602".parse().unwrap(),
nonce: U256::zero(),
init_code: Bytes::default(),
call_data: Bytes::default(),
call_gas_limit: U256::from(200000),
verification_gas_limit: U256::from(100000),
pre_verification_gas: U256::from(21000),
max_fee_per_gas: U256::from(3000000000 as u64),
max_priority_fee_per_gas: U256::from(1000000000),
paymaster_and_data: Bytes::default(),
signature: Bytes::from_str("0x7cb39607585dee8e297d0d7a669ad8c5e43975220b6773c10a138deadbc8ec864981de4b9b3c735288a217115fb33f8326a61ddabc60a534e3b5536515c70f931c").unwrap(),
},
];
assert_eq!(user_operations[0].pack_for_signature(), String::from("0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000186a000000000000000000000000000000000000000000000000000000000000052080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003b9aca0000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").into_bytes());
assert_eq!(user_operations[1].pack_for_signature(), String::from("0x000000000000000000000000663f3ad617193148711d28f5334ee4ed070166020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000030d4000000000000000000000000000000000000000000000000000000000000186a0000000000000000000000000000000000000000000000000000000000000520800000000000000000000000000000000000000000000000000000000b2d05e00000000000000000000000000000000000000000000000000000000003b9aca0000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").into_bytes());
}

#[test]
fn user_operation_hash() {
let user_operations = vec![
UserOperation {
sender: Address::zero(),
nonce: U256::zero(),
init_code: Bytes::default(),
call_data: Bytes::default(),
call_gas_limit: U256::zero(),
verification_gas_limit: U256::from(100000),
pre_verification_gas: U256::from(21000),
max_fee_per_gas: U256::zero(),
max_priority_fee_per_gas: U256::from(1e9 as u64),
paymaster_and_data: Bytes::default(),
signature: Bytes::default(),
},
UserOperation {
sender: "0x663F3ad617193148711d28f5334eE4Ed07016602".parse().unwrap(),
nonce: U256::zero(),
init_code: Bytes::default(),
call_data: Bytes::default(),
call_gas_limit: U256::from(200000),
verification_gas_limit: U256::from(100000),
pre_verification_gas: U256::from(21000),
max_fee_per_gas: U256::from(3000000000 as u64),
max_priority_fee_per_gas: U256::from(1000000000),
paymaster_and_data: Bytes::default(),
signature: Bytes::from_str("0x7cb39607585dee8e297d0d7a669ad8c5e43975220b6773c10a138deadbc8ec864981de4b9b3c735288a217115fb33f8326a61ddabc60a534e3b5536515c70f931c").unwrap(),
},
];
assert_eq!(
user_operations[0].hash(
"0x2DF1592238420ecFe7f2431360e224707e77fA0E"
.parse()
.unwrap(),
U256::from(1)
),
H256::from_str("0x42e145138104ec4124367ea3f7994833071b2011927290f6844d593e05011279")
.unwrap()
);
assert_eq!(
user_operations[1].hash(
"0x2DF1592238420ecFe7f2431360e224707e77fA0E"
.parse()
.unwrap(),
U256::from(1)
),
H256::from_str("0x583c8fcba470fd9da514f9482ccd31c299b0161a36b365aab353a6bfebaa0bb2")
.unwrap()
);
}
}
24 changes: 21 additions & 3 deletions src/uopool/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
use crate::uopool::{server::server::uo_pool_server::UoPoolServer, services::UoPoolService};
use crate::{
types::user_operation::{UserOperation, UserOperationHash},
uopool::{server::server::uo_pool_server::UoPoolServer, services::UoPoolService},
};
use anyhow::Result;
use clap::Parser;
use educe::Educe;
use jsonrpsee::tracing::info;
use std::{net::SocketAddr, time::Duration};
use parking_lot::RwLock;
use std::{collections::HashMap, net::SocketAddr, sync::Arc, time::Duration};

pub mod server;
pub mod services;

#[derive(Educe)]
#[educe(Debug)]
pub struct UserOperationPool {
pub pool: Arc<RwLock<HashMap<UserOperationHash, UserOperation>>>,
}

impl UserOperationPool {
pub fn new() -> Self {
Self {
pool: Default::default(),
}
}
}

#[derive(Educe, Parser)]
#[educe(Debug)]
pub struct Opts {
Expand All @@ -18,7 +36,7 @@ pub struct Opts {
pub async fn run(opts: Opts) -> Result<()> {
tokio::spawn(async move {
let mut builder = tonic::transport::Server::builder();
let svc = UoPoolServer::new(UoPoolService::new());
let svc = UoPoolServer::new(UoPoolService::new(Arc::new(UserOperationPool::new())));

info!(
"UoPool gRPC server starting on {}",
Expand Down
19 changes: 13 additions & 6 deletions src/uopool/services/uopool.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
use crate::uopool::server::server::{
uo_pool_server::UoPool, AddRequest, AddResponse, AllRequest, AllResponse, RemoveRequest,
RemoveResponse,
use std::sync::Arc;

use crate::uopool::{
server::server::{
uo_pool_server::UoPool, AddRequest, AddResponse, AllRequest, AllResponse, RemoveRequest,
RemoveResponse,
},
UserOperationPool,
};
use async_trait::async_trait;
use tonic::Response;

pub struct UoPoolService {}
pub struct UoPoolService {
uo_pool: Arc<UserOperationPool>,
}

impl UoPoolService {
pub fn new() -> Self {
Self {}
pub fn new(uo_pool: Arc<UserOperationPool>) -> Self {
Self { uo_pool }
}
}

Expand Down