aboutsummaryrefslogtreecommitdiff
path: root/src/transaction/eip7702.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/transaction/eip7702.rs')
-rw-r--r--src/transaction/eip7702.rs204
1 files changed, 204 insertions, 0 deletions
diff --git a/src/transaction/eip7702.rs b/src/transaction/eip7702.rs
new file mode 100644
index 0000000..8e2fd93
--- /dev/null
+++ b/src/transaction/eip7702.rs
@@ -0,0 +1,204 @@
+//! EIP-7702 Ethereum transaction type definition and RLP encoding.
+
+use crate::{
+ account::Signature, serialization, transaction::accesslist::AccessList, transaction::rlp,
+};
+use ethaddr::Address;
+use ethdigest::Digest;
+use ethnum::U256;
+use serde::Deserialize;
+
+/// An EIP-7702 Ethereum transaction.
+#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
+pub struct Eip7702Transaction {
+ /// The chain ID for the transaction.
+ #[serde(rename = "chainId", with = "ethnum::serde::permissive")]
+ pub chain_id: U256,
+ /// The nonce for the transaction.
+ #[serde(with = "ethnum::serde::permissive")]
+ pub nonce: U256,
+ /// The maximum priority fee in Wei for the transaction.
+ #[serde(rename = "maxPriorityFeePerGas", with = "ethnum::serde::permissive")]
+ pub max_priority_fee_per_gas: U256,
+ /// The maximum gas price in Wei for the transaction.
+ #[serde(rename = "maxFeePerGas", with = "ethnum::serde::permissive")]
+ pub max_fee_per_gas: U256,
+ /// The gas limit for the transaction.
+ #[serde(with = "ethnum::serde::permissive")]
+ pub gas: U256,
+ /// The target address for the transaction.
+ pub to: Address,
+ /// The amount of Ether to send with the transaction.
+ #[serde(with = "ethnum::serde::permissive")]
+ pub value: U256,
+ /// The calldata to use for the transaction.
+ #[serde(with = "serialization::bytes")]
+ pub data: Vec<u8>,
+ /// List of addresses and storage keys that the transaction plans to access.
+ #[serde(default, rename = "accessList")]
+ pub access_list: AccessList,
+ /// List of delegation authorizations
+ #[serde(rename = "blobVersionedHashes")]
+ pub blob_versioned_hashes: Vec<Digest>,
+}
+
+impl Eip7702Transaction {
+ /// Returns the RLP encoded transaction with an optional signature.
+ pub fn rlp_encode(&self, signature: Option<Signature>) -> Vec<u8> {
+ let fields = [
+ rlp::uint(self.chain_id),
+ rlp::uint(self.nonce),
+ rlp::uint(self.max_priority_fee_per_gas),
+ rlp::uint(self.max_fee_per_gas),
+ rlp::uint(self.gas),
+ rlp::bytes(&*self.to),
+ rlp::uint(self.value),
+ rlp::bytes(&self.data),
+ self.access_list.rlp_encode(),
+ rlp::uint(self.max_fee_per_blob_gas),
+ rlp::iter(
+ self.blob_versioned_hashes
+ .iter()
+ .map(|hash| rlp::bytes(hash.as_ref())),
+ ),
+ ];
+
+ let tail = signature.map(|signature| {
+ [
+ rlp::uint(signature.y_parity()),
+ rlp::uint(signature.r()),
+ rlp::uint(signature.s()),
+ ]
+ });
+
+ [
+ &[0x02][..],
+ &rlp::iter(fields.iter().chain(tail.iter().flatten())),
+ ]
+ .concat()
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::transaction::accesslist::StorageSlot;
+ use ethaddr::address;
+ use ethdigest::keccak;
+ use ethnum::AsU256 as _;
+ use hex_literal::hex;
+ use serde_json::json;
+
+ #[test]
+ fn deserialize_json() {
+ let mut tx = json!({
+ "chainId": "0xff",
+ "nonce": 42,
+ "maxPriorityFeePerGas": 13.37e9,
+ "maxFeePerGas": 42e9,
+ "gas": 21000,
+ "value": "13370000000000000000",
+ "data": "0x",
+ "maxFeePerBlobGas": 10000,
+ "blobVersionedHashes": [
+ keccak!(b"some"),
+ keccak!(b"hash"),
+ ],
+ });
+ assert_eq!(
+ serde_json::from_value::<Eip7702Transaction>(tx.clone()).unwrap(),
+ Eip7702Transaction {
+ chain_id: 255.as_u256(),
+ nonce: 42.as_u256(),
+ max_priority_fee_per_gas: 13.37e9.as_u256(),
+ max_fee_per_gas: 42e9.as_u256(),
+ gas: 21_000.as_u256(),
+ to: None,
+ value: 13.37e18.as_u256(),
+ data: vec![],
+ access_list: AccessList::default(),
+ }
+ );
+
+ tx["to"] = json!("0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
+ tx["accessList"] = json!([[
+ "0x0000000000000000000000000000000000000000",
+ ["0x0000000000000000000000000000000000000000000000000000000000000000",],
+ ]]);
+ let deserialized = serde_json::from_value::<Eip7702Transaction>(tx).unwrap();
+ assert_eq!(
+ deserialized.to.unwrap(),
+ address!("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF"),
+ );
+ assert_eq!(
+ deserialized.access_list,
+ AccessList(vec![(Address::default(), vec![StorageSlot::default()])]),
+ );
+ }
+
+ #[test]
+ fn encode() {
+ assert_eq!(
+ Eip7702Transaction {
+ chain_id: 1.as_u256(),
+ nonce: 66.as_u256(),
+ max_priority_fee_per_gas: 28e9.as_u256(),
+ max_fee_per_gas: 42e9.as_u256(),
+ gas: 30_000.as_u256(),
+ to: Some(address!("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF")),
+ value: 13.37e18.as_u256(),
+ data: vec![],
+ access_list: AccessList::default(),
+ }
+ .rlp_encode(None),
+ hex!(
+ "02f10142850684ee18008509c765240082753094deadbeefdeadbeefdeadbeefdeadbeefde
+ adbeef88b98bc829a6f9000080c0"
+ )
+ .to_owned(),
+ );
+ assert_eq!(
+ Eip7702Transaction {
+ chain_id: 1.as_u256(),
+ nonce: 777.as_u256(),
+ max_priority_fee_per_gas: 28e9.as_u256(),
+ max_fee_per_gas: 42e9.as_u256(),
+ gas: 100_000.as_u256(),
+ to: None,
+ value: 0.as_u256(),
+ data: hex!(
+ "363d3d373d3d3d363d73deadbeefdeadbeefdeadbeefdeadbeefdeadbeef5af43d82803e90
+ 3d91602b57fd5bf3"
+ )
+ .to_vec(),
+ access_list: AccessList(vec![
+ (
+ address!("0x1111111111111111111111111111111111111111"),
+ vec![
+ StorageSlot(hex!(
+ "a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0"
+ )),
+ StorageSlot(hex!(
+ "a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1"
+ )),
+ ],
+ ),
+ (
+ address!("0x2222222222222222222222222222222222222222"),
+ vec![],
+ ),
+ ]),
+ }
+ .rlp_encode(None),
+ hex!(
+ "02f8b801820309850684ee18008509c7652400830186a08080ad363d3d373d3d
+ 3d363d73deadbeefdeadbeefdeadbeefdeadbeefdeadbeef5af43d82803e903d
+ 91602b57fd5bf3f872f859941111111111111111111111111111111111111111
+ f842a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0
+ a0a0a0a0a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1
+ a1a1a1a1d6942222222222222222222222222222222222222222c0"
+ )
+ .to_vec(),
+ );
+ }
+}