From 2f2b81329d4e3285c5328489a51d103f155ad733 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Tue, 2 Apr 2024 11:07:52 -0400 Subject: [PATCH 1/5] feat(transactions): encode eip1559 --- Cargo.toml | 2 + .../transactions/examples/encode_decode.rs | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 examples/transactions/examples/encode_decode.rs diff --git a/Cargo.toml b/Cargo.toml index 424a2993..51fee210 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,6 +83,8 @@ use_self = "allow" [workspace.dependencies] alloy = { git = "https://github.com/alloy-rs/alloy", rev = "27d5ba9", features = [ "contract", + "consensus", + "eips", "network", "node-bindings", "providers", diff --git a/examples/transactions/examples/encode_decode.rs b/examples/transactions/examples/encode_decode.rs new file mode 100644 index 00000000..74b8c1d3 --- /dev/null +++ b/examples/transactions/examples/encode_decode.rs @@ -0,0 +1,37 @@ +use alloy::{ + consensus::{SignableTransaction, TxEip1559}, + eips::eip2930::AccessList, + primitives::{address, b256, hex, Signature, TxKind, U256}, +}; +use eyre::Result; +#[tokio::main] +async fn main() -> Result<()> { + // EIP1559 transaction + let signer = address!("DD6B8b3dC6B7AD97db52F08a275FF4483e024CEa"); + let hash = b256!("0ec0b6a2df4d87424e5f6ad2a654e27aaeb7dac20ae9e8385cc09087ad532ee0"); + let tx = TxEip1559 { + chain_id: 1, + nonce: 0x42, + gas_limit: 44386, + to: TxKind::Call( address!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6")), + value: U256::from(0_u64), + input: hex!("a22cb4650000000000000000000000005eee75727d804a2b13038928d36f8b188945a57a0000000000000000000000000000000000000000000000000000000000000000").into(), + max_fee_per_gas: 0x4a817c800, + max_priority_fee_per_gas: 0x3b9aca00, + access_list: AccessList::default(), + }; + + let sig = Signature::from_scalars_and_parity( + b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"), + b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"), + false, + ) + .unwrap(); + + let signed_tx = tx.into_signed(sig); + + // Match hash + assert_eq!(*signed_tx.hash(), hash); + // TODO: Recover Signer - Enable feature k256 on consensus + Ok(()) +} From 4b20ae22ef9a409262a865415a9b8081b2e7f9e0 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Tue, 4 Jun 2024 21:03:17 +0530 Subject: [PATCH 2/5] recover signer --- Cargo.toml | 1 + examples/transactions/examples/encode_decode.rs | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index da431be3..0f318082 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,6 +83,7 @@ unnecessary_struct_initialization = "allow" alloy = { git = "https://github.com/alloy-rs/alloy", rev = "dc56a76", features = [ "consensus", "contract", + "k256", "eips", "kzg", "network", diff --git a/examples/transactions/examples/encode_decode.rs b/examples/transactions/examples/encode_decode.rs index 74b8c1d3..7ff19ebd 100644 --- a/examples/transactions/examples/encode_decode.rs +++ b/examples/transactions/examples/encode_decode.rs @@ -1,3 +1,4 @@ +//! Encode and decode transactions use alloy::{ consensus::{SignableTransaction, TxEip1559}, eips::eip2930::AccessList, @@ -32,6 +33,9 @@ async fn main() -> Result<()> { // Match hash assert_eq!(*signed_tx.hash(), hash); - // TODO: Recover Signer - Enable feature k256 on consensus + + let recovered_signer = signed_tx.recover_signer().unwrap(); + assert_eq!(recovered_signer, signer); + Ok(()) } From d12ce0315fd1f68f14493cbd65656e8f990d1f4a Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Tue, 4 Jun 2024 21:06:34 +0530 Subject: [PATCH 3/5] nit --- examples/transactions/examples/encode_decode.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/transactions/examples/encode_decode.rs b/examples/transactions/examples/encode_decode.rs index 7ff19ebd..6be9be31 100644 --- a/examples/transactions/examples/encode_decode.rs +++ b/examples/transactions/examples/encode_decode.rs @@ -7,9 +7,12 @@ use alloy::{ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { - // EIP1559 transaction - let signer = address!("DD6B8b3dC6B7AD97db52F08a275FF4483e024CEa"); + // EIP1559 transaction: let hash = b256!("0ec0b6a2df4d87424e5f6ad2a654e27aaeb7dac20ae9e8385cc09087ad532ee0"); + + // Signer of the transaction. + let signer = address!("DD6B8b3dC6B7AD97db52F08a275FF4483e024CEa"); + let tx = TxEip1559 { chain_id: 1, nonce: 0x42, From af8cd0a52d069aea9e785d24760621557455e611 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Fri, 7 Jun 2024 15:24:04 +0200 Subject: [PATCH 4/5] improve docs --- ...ode_decode.rs => encode_decode_eip1559.rs} | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) rename examples/transactions/examples/{encode_decode.rs => encode_decode_eip1559.rs} (54%) diff --git a/examples/transactions/examples/encode_decode.rs b/examples/transactions/examples/encode_decode_eip1559.rs similarity index 54% rename from examples/transactions/examples/encode_decode.rs rename to examples/transactions/examples/encode_decode_eip1559.rs index 6be9be31..44ff6fd5 100644 --- a/examples/transactions/examples/encode_decode.rs +++ b/examples/transactions/examples/encode_decode_eip1559.rs @@ -1,43 +1,46 @@ -//! Encode and decode transactions +//! Example showing how to encode and decode an [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559) transaction. + use alloy::{ consensus::{SignableTransaction, TxEip1559}, eips::eip2930::AccessList, primitives::{address, b256, hex, Signature, TxKind, U256}, }; use eyre::Result; + #[tokio::main] async fn main() -> Result<()> { // EIP1559 transaction: - let hash = b256!("0ec0b6a2df4d87424e5f6ad2a654e27aaeb7dac20ae9e8385cc09087ad532ee0"); + let tx_hash = b256!("0ec0b6a2df4d87424e5f6ad2a654e27aaeb7dac20ae9e8385cc09087ad532ee0"); // Signer of the transaction. let signer = address!("DD6B8b3dC6B7AD97db52F08a275FF4483e024CEa"); - let tx = TxEip1559 { + // Construct the EIP-1559 transaction. + let tx = TxEip1559 { chain_id: 1, nonce: 0x42, gas_limit: 44386, to: TxKind::Call( address!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6")), value: U256::from(0_u64), - input: hex!("a22cb4650000000000000000000000005eee75727d804a2b13038928d36f8b188945a57a0000000000000000000000000000000000000000000000000000000000000000").into(), + input: hex!("a22cb4650000000000000000000000005eee75727d804a2b13038928d36f8b188945a57a0000000000000000000000000000000000000000000000000000000000000000").into(), max_fee_per_gas: 0x4a817c800, max_priority_fee_per_gas: 0x3b9aca00, access_list: AccessList::default(), }; - let sig = Signature::from_scalars_and_parity( + // Construct the signature of the transaction. + let signature = Signature::from_scalars_and_parity( b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"), b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"), false, - ) - .unwrap(); - - let signed_tx = tx.into_signed(sig); + )?; - // Match hash - assert_eq!(*signed_tx.hash(), hash); + // Convert the transaction into a signed transaction. + let signed_tx = tx.into_signed(signature); + assert_eq!(*signed_tx.hash(), tx_hash); - let recovered_signer = signed_tx.recover_signer().unwrap(); + // Recover the signer from the signed transaction to ensure it matches the expected signer. + let recovered_signer = signed_tx.recover_signer()?; assert_eq!(recovered_signer, signer); Ok(()) From cfaeb3accbdc9f0dc99f3b8b28c40a2becdc38d0 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Fri, 7 Jun 2024 15:40:14 +0200 Subject: [PATCH 5/5] rename sign_transaction --- README.md | 2 +- .../examples/{sign_transaction.rs => send_raw_transaction.rs} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename examples/transactions/examples/{sign_transaction.rs => send_raw_transaction.rs} (100%) diff --git a/README.md b/README.md index a03952bd..656051fe 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ This repository contains the following examples: - [x] [Trace transaction](./examples/transactions/examples/trace_transaction.rs) - [x] [Transfer ERC20 token](./examples/transactions/examples/transfer_erc20.rs) - [x] [Transfer ETH](./examples/transactions/examples/transfer_eth.rs) - - [x] [Sign and send a raw transaction](./examples/transactions/examples/sign_transaction.rs) + - [x] [Sign and send a raw transaction](./examples/transactions/examples/send_raw_transaction.rs) - [x] [Send EIP-1559 transaction](./examples/transactions/examples/send_eip1559_transaction.rs) - [x] [Send legacy transaction](./examples/transactions/examples/send_legacy_transaction.rs) - [x] [Send EIP-4844 transaction](./examples/transactions/examples/send_eip4844_transaction.rs) diff --git a/examples/transactions/examples/sign_transaction.rs b/examples/transactions/examples/send_raw_transaction.rs similarity index 100% rename from examples/transactions/examples/sign_transaction.rs rename to examples/transactions/examples/send_raw_transaction.rs