Skip to content

Commit cfd066c

Browse files
emhanemattsse
andauthored
chore(sdk): SignedTransaction abstraction (paradigmxyz#11432)
Co-authored-by: Matthias Seitz <[email protected]>
1 parent cb60482 commit cfd066c

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed

crates/primitives-traits/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub mod receipt;
2424
pub use receipt::Receipt;
2525

2626
pub mod transaction;
27-
pub use transaction::Transaction;
27+
pub use transaction::{signed::SignedTransaction, Transaction};
2828

2929
mod integer_list;
3030
pub use integer_list::{IntegerList, IntegerListError};

crates/primitives-traits/src/mod.rs

Whitespace-only changes.

crates/primitives-traits/src/transaction.rs renamed to crates/primitives-traits/src/transaction/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Transaction abstraction
22
3+
pub mod signed;
4+
35
use alloc::fmt;
46

57
use reth_codecs::Compact;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//! API of a signed transaction.
2+
3+
use alloc::fmt;
4+
use core::hash::Hash;
5+
6+
use alloy_consensus::Transaction;
7+
use alloy_eips::eip2718::{Decodable2718, Encodable2718};
8+
use alloy_primitives::{keccak256, Address, TxHash, B256};
9+
10+
/// A signed transaction.
11+
pub trait SignedTransaction:
12+
fmt::Debug
13+
+ Clone
14+
+ PartialEq
15+
+ Eq
16+
+ Hash
17+
+ Send
18+
+ Sync
19+
+ serde::Serialize
20+
+ for<'a> serde::Deserialize<'a>
21+
+ alloy_rlp::Encodable
22+
+ alloy_rlp::Decodable
23+
+ Encodable2718
24+
+ Decodable2718
25+
{
26+
/// Transaction type that is signed.
27+
type Transaction: Transaction;
28+
29+
/// Signature type that results from signing transaction.
30+
type Signature;
31+
32+
/// Returns reference to transaction hash.
33+
fn tx_hash(&self) -> &TxHash;
34+
35+
/// Returns reference to transaction.
36+
fn transaction(&self) -> &Self::Transaction;
37+
38+
/// Returns reference to signature.
39+
fn signature(&self) -> &Self::Signature;
40+
41+
/// Recover signer from signature and hash.
42+
///
43+
/// Returns `None` if the transaction's signature is invalid following [EIP-2](https://eips.ethereum.org/EIPS/eip-2), see also `reth_primitives::transaction::recover_signer`.
44+
///
45+
/// Note:
46+
///
47+
/// This can fail for some early ethereum mainnet transactions pre EIP-2, use
48+
/// [`Self::recover_signer_unchecked`] if you want to recover the signer without ensuring that
49+
/// the signature has a low `s` value.
50+
fn recover_signer(&self) -> Option<Address>;
51+
52+
/// Recover signer from signature and hash _without ensuring that the signature has a low `s`
53+
/// value_.
54+
///
55+
/// Returns `None` if the transaction's signature is invalid, see also
56+
/// `reth_primitives::transaction::recover_signer_unchecked`.
57+
fn recover_signer_unchecked(&self) -> Option<Address>;
58+
59+
/// Create a new signed transaction from a transaction and its signature.
60+
///
61+
/// This will also calculate the transaction hash using its encoding.
62+
fn from_transaction_and_signature(
63+
transaction: Self::Transaction,
64+
signature: Self::Signature,
65+
) -> Self;
66+
67+
/// Calculate transaction hash, eip2728 transaction does not contain rlp header and start with
68+
/// tx type.
69+
fn recalculate_hash(&self) -> B256 {
70+
keccak256(self.encoded_2718())
71+
}
72+
}

crates/primitives/src/traits/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! Abstractions of primitive data types
2+
3+
pub mod block;
4+
pub mod transaction;
5+
6+
pub use block::{body::BlockBody, Block};
7+
pub use transaction::signed::SignedTransaction;
8+
9+
pub use alloy_consensus::BlockHeader;

0 commit comments

Comments
 (0)