Skip to content

Commit

Permalink
fix: make a sensible encoding api (#1496)
Browse files Browse the repository at this point in the history
* fix: make a sensible encoding api

* fix: k256 test

* fix: single doc hidden

* fix: missing doc

* nit: undo changed list

* feat: optimize hash calculation

* fix: chain rlp_decode_signed

* fix: restore remaining check

* refactor: helper trait

* feat: add API to Signed for convenience

* refactor: add header shortcut functions

* feat: impl for some other tx types

* refctor: impl RlpEcdsaTx for TxLegacy

* lint: clippy

* fix: import vec

* fix: import Vec

* fix: rebase artifacts

* fix: correct handling of legacy signatures when encoding (#1510)

* lint: clippy

* fix: use corrected impls

* nit: remove bug comments

* refactor: use length_with_payload

* fix: legacy network headers are transparent

* fix: restore signature v check

* fix: check for leftoverbytes

* fix: corrected RLP decoding of eip4844 variant

* fix: list check in 4844 decoding

* fix: add map or
  • Loading branch information
prestwich authored Oct 28, 2024
1 parent bf1f31a commit f34a2dd
Show file tree
Hide file tree
Showing 11 changed files with 886 additions and 1,040 deletions.
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

0 comments on commit f34a2dd

Please sign in to comment.