1use core::fmt;
2use primitives::{b256, bytes, Address, Bytes, B256};
3
4pub const EIP7702_MAGIC_HASH: B256 =
6 b256!("0xeadcdba66a79ab5dce91622d1d75c8cff5cff0b96944c3bf1072cd08ce018329");
7
8pub const EIP7702_MAGIC: u16 = 0xEF01;
10
11pub static EIP7702_MAGIC_BYTES: Bytes = bytes!("ef01");
13
14pub const EIP7702_VERSION: u8 = 0;
16
17#[derive(Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
22#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
23pub struct Eip7702Bytecode {
24 pub delegated_address: Address,
26 pub version: u8,
28 pub raw: Bytes,
30}
31
32impl Eip7702Bytecode {
33 #[inline]
35 pub fn new_raw(raw: Bytes) -> Result<Self, Eip7702DecodeError> {
36 if raw.len() != 23 {
37 return Err(Eip7702DecodeError::InvalidLength);
38 }
39 if !raw.starts_with(&EIP7702_MAGIC_BYTES) {
40 return Err(Eip7702DecodeError::InvalidMagic);
41 }
42
43 if raw[2] != EIP7702_VERSION {
45 return Err(Eip7702DecodeError::UnsupportedVersion);
46 }
47
48 Ok(Self {
49 delegated_address: Address::new(raw[3..].try_into().unwrap()),
50 version: EIP7702_VERSION,
51 raw,
52 })
53 }
54
55 pub fn new(address: Address) -> Self {
57 let mut raw = EIP7702_MAGIC_BYTES.to_vec();
58 raw.push(EIP7702_VERSION);
59 raw.extend(&address);
60 Self {
61 delegated_address: address,
62 version: EIP7702_VERSION,
63 raw: raw.into(),
64 }
65 }
66
67 #[inline]
69 pub fn raw(&self) -> &Bytes {
70 &self.raw
71 }
72
73 #[inline]
75 pub fn address(&self) -> Address {
76 self.delegated_address
77 }
78}
79
80#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
82#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
83pub enum Eip7702DecodeError {
84 InvalidLength,
88 InvalidMagic,
92 UnsupportedVersion,
96}
97
98impl fmt::Display for Eip7702DecodeError {
99 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100 let s = match self {
101 Self::InvalidLength => "Eip7702 is not 23 bytes long",
102 Self::InvalidMagic => "Bytecode is not starting with 0xEF01",
103 Self::UnsupportedVersion => "Unsupported Eip7702 version.",
104 };
105 f.write_str(s)
106 }
107}
108
109impl core::error::Error for Eip7702DecodeError {}
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114
115 #[test]
116 fn sanity_decode() {
117 let raw = bytes!("ef01deadbeef");
118 assert_eq!(
119 Eip7702Bytecode::new_raw(raw),
120 Err(Eip7702DecodeError::InvalidLength)
121 );
122
123 let raw = bytes!("ef0101deadbeef00000000000000000000000000000000");
124 assert_eq!(
125 Eip7702Bytecode::new_raw(raw),
126 Err(Eip7702DecodeError::UnsupportedVersion)
127 );
128
129 let raw = bytes!("ef0100deadbeef00000000000000000000000000000000");
130 let address = raw[3..].try_into().unwrap();
131 assert_eq!(
132 Eip7702Bytecode::new_raw(raw.clone()),
133 Ok(Eip7702Bytecode {
134 delegated_address: address,
135 version: 0,
136 raw,
137 })
138 );
139 }
140
141 #[test]
142 fn create_eip7702_bytecode_from_address() {
143 let address = Address::new([0x01; 20]);
144 let bytecode = Eip7702Bytecode::new(address);
145 assert_eq!(bytecode.delegated_address, address);
146 assert_eq!(
147 bytecode.raw,
148 bytes!("ef01000101010101010101010101010101010101010101")
149 );
150 }
151}