Skip to content

Commit e94553a

Browse files
committed
feat: clean up system for types rs
1 parent eac2e1b commit e94553a

File tree

5 files changed

+58
-66
lines changed

5 files changed

+58
-66
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod v06;
22
pub mod v07;
3+
pub mod version;

crates/account-abstraction-core/core/src/entrypoints/v06.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use alloy_primitives::U256;
1+
use alloy_primitives::{U256, ChainId};
22
use alloy_rpc_types::erc4337;
33
use alloy_sol_types::{SolValue, sol};
44
sol! {
@@ -43,10 +43,10 @@ impl From<erc4337::UserOperation> for UserOperationPackedForHash {
4343
}
4444
}
4545

46-
pub fn hash_user_operation_v06(
46+
pub fn hash_user_operation(
4747
user_operation: &erc4337::UserOperation,
4848
entry_point: alloy_primitives::Address,
49-
chain_id: i32,
49+
chain_id: ChainId,
5050
) -> alloy_primitives::B256 {
5151
let packed = UserOperationPackedForHash::from(user_operation.clone());
5252
let encoded = UserOperationHashEncoded {
@@ -82,7 +82,7 @@ mod tests {
8282
};
8383

8484
let hash =
85-
hash_user_operation_v06(&userOpWithZeroedInitCode, entry_point_address_v0_6, chainId);
85+
hash_user_operation(&userOpWithZeroedInitCode, entry_point_address_v0_6, chainId);
8686
assert_eq!(
8787
hash,
8888
b256!("dca97c3b49558ab360659f6ead939773be8bf26631e61bb17045bb70dc983b2d")
@@ -113,7 +113,7 @@ mod tests {
113113
signature: bytes!("da0929f527cded8d0a1eaf2e8861d7f7e2d8160b7b13942f99dd367df4473a"),
114114
};
115115

116-
let hash = hash_user_operation_v06(
116+
let hash = hash_user_operation(
117117
&userOpWithNonZeroedInitCode,
118118
entry_point_address_v0_6,
119119
chainId,

crates/account-abstraction-core/core/src/entrypoints/v07.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use alloy_primitives::{Address, FixedBytes, U256};
1+
use alloy_primitives::{Address, FixedBytes, U256, ChainId};
22
use alloy_primitives::{Bytes, keccak256};
33
use alloy_rpc_types::erc4337;
44
use alloy_sol_types::{SolValue, sol};
@@ -68,7 +68,7 @@ impl From<erc4337::PackedUserOperation> for PackedUserOperation {
6868
let mut paymaster_and_data = paymaster.to_vec();
6969
paymaster_and_data.extend_from_slice(&pvgl);
7070
paymaster_and_data.extend_from_slice(&pogl);
71-
paymaster_and_data.extend_from_slice(&uo.paymaster_data.clone().unwrap());
71+
paymaster_and_data.extend_from_slice(&uo.paymaster_data.unwrap());
7272
Bytes::from(paymaster_and_data)
7373
} else {
7474
Bytes::new()
@@ -96,7 +96,7 @@ fn pack_u256_pair_to_bytes32(high: U256, low: U256) -> FixedBytes<32> {
9696
fn hash_packed_user_operation(
9797
puo: &PackedUserOperation,
9898
entry_point: Address,
99-
chain_id: i32,
99+
chain_id: u64,
100100
) -> FixedBytes<32> {
101101
let hash_init_code = alloy_primitives::keccak256(&puo.initCode);
102102
let hash_call_data = alloy_primitives::keccak256(&puo.callData);
@@ -124,10 +124,10 @@ fn hash_packed_user_operation(
124124
keccak256(encoded.abi_encode())
125125
}
126126

127-
pub fn hash_user_operation_v07(
127+
pub fn hash_user_operation(
128128
user_operation: &erc4337::PackedUserOperation,
129129
entry_point: Address,
130-
chain_id: i32,
130+
chain_id: ChainId,
131131
) -> FixedBytes<32> {
132132
let packed = PackedUserOperation::from(user_operation.clone());
133133
hash_packed_user_operation(&packed, entry_point, chain_id)
@@ -138,7 +138,6 @@ mod test {
138138
use super::*;
139139
use alloy_primitives::{Bytes, U256};
140140
use alloy_primitives::{address, b256, bytes, uint};
141-
use alloy_sol_types::{SolValue, sol};
142141

143142
#[test]
144143
fn test_hash() {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use alloy_primitives::{Address, B256, U256, address, keccak256};
2+
3+
4+
#[derive(Debug, Clone)]
5+
pub enum EntryPointVersion {
6+
V06,
7+
V07,
8+
}
9+
10+
impl EntryPointVersion {
11+
pub const V06_ADDRESS: Address = address!("0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789");
12+
pub const V07_ADDRESS: Address = address!("0x0000000071727De22E5E9d8BAf0edAc6f37da032");
13+
}
14+
15+
#[derive(Debug)]
16+
pub struct UnknownEntryPointAddress {
17+
pub address: Address,
18+
}
19+
20+
impl TryFrom<Address> for EntryPointVersion {
21+
type Error = UnknownEntryPointAddress;
22+
23+
fn try_from(addr: Address) -> Result<Self, Self::Error> {
24+
if addr == Self::V06_ADDRESS {
25+
Ok(EntryPointVersion::V06)
26+
} else if addr == Self::V07_ADDRESS {
27+
Ok(EntryPointVersion::V07)
28+
} else {
29+
Err(UnknownEntryPointAddress { address: addr })
30+
}
31+
}
32+
}

crates/account-abstraction-core/core/src/types.rs

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::entrypoints;
2-
use alloy_primitives::{Address, B256, U256, address, keccak256};
1+
use crate::entrypoints::{version::EntryPointVersion, v06, v07};
2+
use alloy_primitives::{Address, B256, U256, ChainId};
33
use alloy_rpc_types::erc4337;
44
pub use alloy_rpc_types::erc4337::SendUserOperationResponse;
55
use anyhow::Result;
@@ -11,69 +11,29 @@ pub enum VersionedUserOperation {
1111
UserOperation(erc4337::UserOperation),
1212
PackedUserOperation(erc4337::PackedUserOperation),
1313
}
14-
15-
#[derive(Debug, Clone)]
16-
pub enum EntryPointVersion {
17-
V06,
18-
V07,
19-
}
20-
21-
impl EntryPointVersion {
22-
pub const V06_ADDRESS: Address = address!("0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789");
23-
pub const V07_ADDRESS: Address = address!("0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789");
24-
}
25-
26-
#[derive(Debug)]
27-
pub struct UnknownEntryPointAddress {
28-
pub address: Address,
29-
}
30-
31-
impl TryFrom<Address> for EntryPointVersion {
32-
type Error = UnknownEntryPointAddress;
33-
34-
fn try_from(addr: Address) -> Result<Self, Self::Error> {
35-
if addr == Self::V06_ADDRESS {
36-
Ok(EntryPointVersion::V06)
37-
} else if addr == Self::V07_ADDRESS {
38-
Ok(EntryPointVersion::V07)
39-
} else {
40-
Err(UnknownEntryPointAddress { address: addr })
41-
}
42-
}
43-
}
44-
4514
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
46-
4715
pub struct UserOperationRequest {
4816
pub user_operation: VersionedUserOperation,
4917
pub entry_point: Address,
50-
pub chain_id: i32,
18+
pub chain_id: ChainId,
5119
}
5220

5321
impl UserOperationRequest {
5422
pub fn hash(&self) -> Result<B256> {
55-
let entry_point_version = EntryPointVersion::try_from(self.entry_point);
56-
if entry_point_version.is_err() {
57-
return Err(anyhow::anyhow!(
58-
"Unknown entry point version: {:#x}",
59-
self.entry_point
60-
));
23+
let entry_point_version = EntryPointVersion::try_from(self.entry_point)
24+
.map_err(|_| anyhow::anyhow!("Unknown entry point version: {:#x}", self.entry_point))?;
25+
26+
match (&self.user_operation, entry_point_version) {
27+
(VersionedUserOperation::UserOperation(op), EntryPointVersion::V06) => {
28+
Ok(v06::hash_user_operation(op, self.entry_point, self.chain_id))
6129
}
62-
match (&self.user_operation, entry_point_version) {
63-
(VersionedUserOperation::UserOperation(user_operation), Ok(EntryPointVersion::V06)) => {
64-
Ok(entrypoints::v06::hash_user_operation_v06(user_operation, self.entry_point, self.chain_id))
65-
}
66-
(
67-
VersionedUserOperation::PackedUserOperation(user_operation),
68-
Ok(EntryPointVersion::V07),
69-
) => Ok(entrypoints::v07::hash_user_operation_v07(user_operation, self.entry_point, self.chain_id)),
70-
_ => {
71-
return Err(anyhow::anyhow!(
72-
"Unknown entry point version: {:#x}",
73-
self.entry_point
74-
));
75-
}
30+
(VersionedUserOperation::PackedUserOperation(op), EntryPointVersion::V07) => {
31+
Ok(v07::hash_user_operation(op, self.entry_point, self.chain_id))
7632
}
33+
_ => Err(anyhow::anyhow!(
34+
"Mismatched operation type and entry point version"
35+
)),
36+
}
7737
}
7838
}
7939

0 commit comments

Comments
 (0)