Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make a sensible encoding api #1496

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e72ce42
fix: make a sensible encoding api
prestwich Oct 16, 2024
c0f30a6
fix: k256 test
prestwich Oct 16, 2024
f99d477
fix: single doc hidden
prestwich Oct 16, 2024
b7dd75a
fix: missing doc
prestwich Oct 16, 2024
f1bd89a
nit: undo changed list
prestwich Oct 16, 2024
e6b337c
feat: optimize hash calculation
prestwich Oct 16, 2024
0a3eb6f
fix: chain rlp_decode_signed
prestwich Oct 16, 2024
b5906de
fix: restore remaining check
prestwich Oct 16, 2024
055e26b
refactor: helper trait
prestwich Oct 16, 2024
412ad48
feat: add API to Signed for convenience
prestwich Oct 16, 2024
471ab5e
refactor: add header shortcut functions
prestwich Oct 16, 2024
4c7e1b5
feat: impl for some other tx types
prestwich Oct 17, 2024
0c0795d
refctor: impl RlpEcdsaTx for TxLegacy
prestwich Oct 17, 2024
f59f2d4
lint: clippy
prestwich Oct 17, 2024
36ea0bb
fix: import vec
prestwich Oct 17, 2024
3f3a4a2
fix: import Vec
prestwich Oct 17, 2024
0fa47c0
fix: rebase artifacts
prestwich Oct 19, 2024
4dec193
fix: correct handling of legacy signatures when encoding (#1510)
prestwich Oct 19, 2024
cc7f064
lint: clippy
prestwich Oct 19, 2024
1216123
fix: use corrected impls
prestwich Oct 21, 2024
cf6fb4f
nit: remove bug comments
prestwich Oct 21, 2024
1257307
refactor: use length_with_payload
prestwich Oct 23, 2024
a977339
fix: legacy network headers are transparent
prestwich Oct 24, 2024
7b03e1a
fix: restore signature v check
prestwich Oct 24, 2024
a7aba88
fix: check for leftoverbytes
prestwich Oct 24, 2024
72169eb
fix: corrected RLP decoding of eip4844 variant
prestwich Oct 24, 2024
2a024ee
fix: list check in 4844 decoding
prestwich Oct 24, 2024
fe75cf0
fix: add map or
prestwich Oct 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion crates/consensus/src/signed.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::transaction::SignableTransaction;
use crate::transaction::{RlpEcdsaTx, SignableTransaction};
use alloy_eips::eip2718::Eip2718Result;
use alloy_primitives::{Signature, B256};
use alloy_rlp::BufMut;

/// A transaction with a signature and hash seal.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -55,6 +57,76 @@ impl<T: SignableTransaction<Sig>, Sig> Signed<T, Sig> {
}
}

impl<T> Signed<T>
where
T: RlpEcdsaTx,
{
/// Get the length of the transaction when RLP encoded.
pub fn rlp_encoded_length(&self) -> usize {
self.tx.rlp_encoded_length_with_signature(&self.signature)
}

/// RLP encode the signed transaction.
pub fn rlp_encode(&self, out: &mut dyn BufMut) {
self.tx.rlp_encode_signed(&self.signature, out);
}

/// Get the length of the transaction when EIP-2718 encoded.
pub fn eip2718_encoded_length(&self) -> usize {
self.tx.eip2718_encoded_length(&self.signature)
}

/// EIP-2718 encode the signed transaction with a specified type flag.
pub fn eip2718_encode_with_type(&self, ty: u8, out: &mut dyn BufMut) {
self.tx.eip2718_encode_with_type(&self.signature, ty, out);
}

/// EIP-2718 encode the signed transaction.
pub fn eip2718_encode(&self, out: &mut dyn BufMut) {
self.tx.eip2718_encode(&self.signature, out);
}

/// Get the length of the transaction when network encoded.
pub fn network_encoded_length(&self) -> usize {
self.tx.network_encoded_length(&self.signature)
}

/// Network encode the signed transaction with a specified type flag.
pub fn network_encode_with_type(&self, ty: u8, out: &mut dyn BufMut) {
self.tx.network_encode_with_type(&self.signature, ty, out);
}

/// Network encode the signed transaction.
pub fn network_encode(&self, out: &mut dyn BufMut) {
self.tx.network_encode(&self.signature, out);
}

/// RLP decode the signed transaction.
pub fn rlp_decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
T::rlp_decode_signed(buf)
}

/// EIP-2718 decode the signed transaction with a specified type flag.
pub fn eip2718_decode_with_type(buf: &mut &[u8], ty: u8) -> Eip2718Result<Self> {
T::eip2718_decode_with_type(buf, ty)
}

/// EIP-2718 decode the signed transaction.
pub fn eip2718_decode(buf: &mut &[u8]) -> Eip2718Result<Self> {
T::eip2718_decode(buf)
}

/// Network decode the signed transaction with a specified type flag.
pub fn network_decode_with_type(buf: &mut &[u8], ty: u8) -> Eip2718Result<Self> {
T::network_decode_with_type(buf, ty)
}

/// Network decode the signed transaction.
pub fn network_decode(buf: &mut &[u8]) -> Eip2718Result<Self> {
T::network_decode(buf)
}
}

#[cfg(feature = "k256")]
impl<T: SignableTransaction<Signature>> Signed<T, Signature> {
/// Recover the signer of the transaction
Expand Down
Loading