Skip to content

Commit

Permalink
feat: integrate signature with boolean parity (#1540)
Browse files Browse the repository at this point in the history
* wip

* make signed_legacy_serde generic

* signed_legacy_serde

* fmt

* patch core

* expose EIP-155 helpers

* remove patches

* fix tests

* patch

* update patch

* update patch

* update patch

* update json

* update chain id handling

* fix

* feat: add mutable tx getter to Signed

* fix

* remove patches
  • Loading branch information
klkvr authored Nov 5, 2024
1 parent 621de26 commit 2323ddb
Show file tree
Hide file tree
Showing 22 changed files with 251 additions and 282 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ alloy-transport-ipc = { version = "0.5", path = "crates/transport-ipc", default-
alloy-transport-ws = { version = "0.5", path = "crates/transport-ws", default-features = false }
alloy-eip5792 = { version = "0.5", path = "crates/eip5792", default-features = false }

alloy-core = { version = "0.8.5", default-features = false }
alloy-dyn-abi = { version = "0.8.5", default-features = false }
alloy-json-abi = { version = "0.8.5", default-features = false }
alloy-primitives = { version = "0.8.5", default-features = false }
alloy-sol-types = { version = "0.8.5", default-features = false }
alloy-core = { version = "0.8.11", default-features = false }
alloy-dyn-abi = { version = "0.8.11", default-features = false }
alloy-json-abi = { version = "0.8.11", default-features = false }
alloy-primitives = { version = "0.8.11", default-features = false }
alloy-sol-types = { version = "0.8.11", default-features = false }

alloy-rlp = { version = "0.3.9", default-features = false }

alloy-chains = { version = "0.1.18", default-features = false }

# eips
alloy-eip2930 = { version = "0.1.0", default-features = false }
alloy-eip7702 = { version = "0.3.2", default-features = false }
alloy-eip7702 = { version = "0.4", default-features = false }

# ethereum
ethereum_ssz_derive = "0.8"
Expand Down
108 changes: 0 additions & 108 deletions crates/consensus/src/encodable_signature.rs

This file was deleted.

3 changes: 0 additions & 3 deletions crates/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ pub use block::{AnyHeader, Block, BlockBody, BlockHeader, Header};
pub mod constants;
pub use constants::{EMPTY_OMMER_ROOT_HASH, EMPTY_ROOT_HASH};

mod encodable_signature;
pub use encodable_signature::EncodableSignature;

mod receipt;
pub use receipt::{
AnyReceiptEnvelope, Eip658Value, Receipt, ReceiptEnvelope, ReceiptWithBloom, Receipts,
Expand Down
17 changes: 11 additions & 6 deletions crates/consensus/src/signed.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::transaction::{RlpEcdsaTx, SignableTransaction};
use alloy_eips::eip2718::Eip2718Result;
use alloy_primitives::{Signature, B256};
use alloy_primitives::{PrimitiveSignature as Signature, B256};
use alloy_rlp::BufMut;

/// A transaction with a signature and hash seal.
Expand All @@ -17,12 +17,22 @@ pub struct Signed<T, Sig = Signature> {
}

impl<T, Sig> Signed<T, Sig> {
/// Instantiate from a transaction and signature. Does not verify the signature.
pub const fn new_unchecked(tx: T, signature: Sig, hash: B256) -> Self {
Self { tx, signature, hash }
}

/// Returns a reference to the transaction.
#[doc(alias = "transaction")]
pub const fn tx(&self) -> &T {
&self.tx
}

/// Returns a mutable reference to the transaction.
pub fn tx_mut(&mut self) -> &mut T {
&mut self.tx
}

/// Returns a reference to the signature.
pub const fn signature(&self) -> &Sig {
&self.signature
Expand All @@ -46,11 +56,6 @@ impl<T, Sig> Signed<T, Sig> {
}

impl<T: SignableTransaction<Sig>, Sig> Signed<T, Sig> {
/// Instantiate from a transaction and signature. Does not verify the signature.
pub const fn new_unchecked(tx: T, signature: Sig, hash: B256) -> Self {
Self { tx, signature, hash }
}

/// Calculate the signing hash for the transaction.
pub fn signature_hash(&self) -> B256 {
self.tx.signature_hash()
Expand Down
16 changes: 6 additions & 10 deletions crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{transaction::RlpEcdsaTx, SignableTransaction, Signed, Transaction, TxType};
use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization};
use alloy_primitives::{Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_primitives::{Bytes, ChainId, PrimitiveSignature as Signature, TxKind, B256, U256};
use alloy_rlp::{BufMut, Decodable, Encodable};
use core::mem;

Expand Down Expand Up @@ -248,10 +248,6 @@ impl SignableTransaction<Signature> for TxEip1559 {
}

fn into_signed(self, signature: Signature) -> Signed<Self> {
// Drop any v chain id value to ensure the signature format is correct at the time of
// combination for an EIP-1559 transaction. V should indicate the y-parity of the
// signature.
let signature = signature.with_parity_bool();
let tx_hash = self.tx_hash(&signature);
Signed::new_unchecked(self, signature, tx_hash)
}
Expand Down Expand Up @@ -398,7 +394,9 @@ mod tests {
use super::TxEip1559;
use crate::{transaction::RlpEcdsaTx, SignableTransaction};
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{address, b256, hex, Address, Signature, B256, U256};
use alloy_primitives::{
address, b256, hex, Address, PrimitiveSignature as Signature, B256, U256,
};

#[test]
fn recover_signer_eip1559() {
Expand All @@ -421,8 +419,7 @@ mod tests {
b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"),
b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"),
false,
)
.unwrap();
);

assert_eq!(
tx.signature_hash(),
Expand Down Expand Up @@ -454,8 +451,7 @@ mod tests {
b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"),
b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"),
false,
)
.unwrap();
);

let mut buf = vec![];
tx.rlp_encode_signed(&sig, &mut buf);
Expand Down
8 changes: 2 additions & 6 deletions crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{SignableTransaction, Signed, Transaction, TxType};
use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization};
use alloy_primitives::{Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_primitives::{Bytes, ChainId, PrimitiveSignature as Signature, TxKind, B256, U256};
use alloy_rlp::{BufMut, Decodable, Encodable};
use core::mem;

Expand Down Expand Up @@ -198,10 +198,6 @@ impl SignableTransaction<Signature> for TxEip2930 {
}

fn into_signed(self, signature: Signature) -> Signed<Self> {
// Drop any v chain id value to ensure the signature format is correct
// at the time of combination for an EIP-2930 transaction. V should
// indicate the y-parity of the signature.
let signature = signature.with_parity_bool();
let tx_hash = self.tx_hash(&signature);
Signed::new_unchecked(self, signature, tx_hash)
}
Expand All @@ -227,7 +223,7 @@ impl Decodable for TxEip2930 {
mod tests {
use super::TxEip2930;
use crate::{transaction::RlpEcdsaTx, SignableTransaction, TxEnvelope};
use alloy_primitives::{Address, Signature, TxKind, U256};
use alloy_primitives::{Address, PrimitiveSignature as Signature, TxKind, U256};
use alloy_rlp::{Decodable, Encodable};

#[test]
Expand Down
25 changes: 6 additions & 19 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use crate::{SignableTransaction, Signed, Transaction, TxType};

use alloc::vec::Vec;
use alloy_eips::{eip2930::AccessList, eip4844::DATA_GAS_PER_BLOB, eip7702::SignedAuthorization};
use alloy_primitives::{Address, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_primitives::{
Address, Bytes, ChainId, PrimitiveSignature as Signature, TxKind, B256, U256,
};
use alloy_rlp::{BufMut, Decodable, Encodable, Header};
use core::mem;

Expand Down Expand Up @@ -345,11 +347,6 @@ impl SignableTransaction<Signature> for TxEip4844Variant {
}

fn into_signed(self, signature: Signature) -> Signed<Self> {
// Drop any v chain id value to ensure the signature format is correct at the time of
// combination for an EIP-4844 transaction. V should indicate the y-parity of the
// signature.
let signature = signature.with_parity_bool();

let hash = self.tx_hash(&signature);

Signed::new_unchecked(self, signature, hash)
Expand Down Expand Up @@ -567,10 +564,6 @@ impl SignableTransaction<Signature> for TxEip4844 {
}

fn into_signed(self, signature: Signature) -> Signed<Self> {
// Drop any v chain id value to ensure the signature format is correct at the time of
// combination for an EIP-4844 transaction. V should indicate the y-parity of the
// signature.
let signature = signature.with_parity_bool();
let hash = self.tx_hash(&signature);
Signed::new_unchecked(self, signature, hash)
}
Expand Down Expand Up @@ -753,11 +746,6 @@ impl SignableTransaction<Signature> for TxEip4844WithSidecar {
}

fn into_signed(self, signature: Signature) -> Signed<Self, Signature> {
// Drop any v chain id value to ensure the signature format is correct at the time of
// combination for an EIP-4844 transaction. V should indicate the y-parity of the
// signature.
let signature = signature.with_parity_bool();

// important: must hash the tx WITHOUT the sidecar
let hash = self.tx_hash(&signature);

Expand Down Expand Up @@ -885,7 +873,7 @@ mod tests {
use super::{BlobTransactionSidecar, TxEip4844, TxEip4844WithSidecar};
use crate::{transaction::eip4844::TxEip4844Variant, SignableTransaction, TxEnvelope};
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{address, b256, bytes, Signature, U256};
use alloy_primitives::{address, b256, bytes, PrimitiveSignature as Signature, U256};
use alloy_rlp::{Decodable, Encodable};

#[test]
Expand Down Expand Up @@ -969,12 +957,11 @@ mod tests {
input: bytes!("701f58c50000000000000000000000000000000000000000000000000000000000073fb1ed12e288def5b439ea074b398dbb4c967f2852baac3238c5fe4b62b871a59a6d00000000000000000000000000000000000000000000000000000000123971da000000000000000000000000000000000000000000000000000000000000000ac39b2a24e1dbdd11a1e7bd7c0f4dfd7d9b9cfa0997d033ad05f961ba3b82c6c83312c967f10daf5ed2bffe309249416e03ee0b101f2b84d2102b9e38b0e4dfdf0000000000000000000000000000000000000000000000000000000066254c8b538dcc33ecf5334bbd294469f9d4fd084a3090693599a46d6c62567747cbc8660000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000073fb20000000000000000000000000000000000000000000000000000000066254da10000000000000000000000000000000000000000000000000000000012397d5e20b09b263779fda4171c341e720af8fa469621ff548651f8dbbc06c2d320400c000000000000000000000000000000000000000000000000000000000000000b50a833bb11af92814e99c6ff7cf7ba7042827549d6f306a04270753702d897d8fc3c411b99159939ac1c16d21d3057ddc8b2333d1331ab34c938cff0eb29ce2e43241c170344db6819f76b1f1e0ab8206f3ec34120312d275c4f5bbea7f5c55700000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000480000000000000000000000000000000000000000000000000000000000000031800000000000000000000000000000000000000000000800b0000000000000000000000000000000000000000000000000000000000000004ed12e288def5b439ea074b398dbb4c967f2852baac3238c5fe4b62b871a59a6d00000ca8000000000000000000000000000000000000800b000000000000000000000000000000000000000000000000000000000000000300000000000000000000000066254da100000000000000000000000066254e9d00010ca80000000000000000000000000000000000008001000000000000000000000000000000000000000000000000000000000000000550a833bb11af92814e99c6ff7cf7ba7042827549d6f306a04270753702d897d800010ca800000000000000000000000000000000000080010000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000b00010ca8000000000000000000000000000000000000801100000000000000000000000000000000000000000000000000000000000000075c1cd5bd0fd333ce9d7c8edfc79f43b8f345b4a394f6aba12a2cc78ce4012ed700010ca80000000000000000000000000000000000008011000000000000000000000000000000000000000000000000000000000000000845392775318aa47beaafbdc827da38c9f1e88c3bdcabba2cb493062e17cbf21e00010ca800000000000000000000000000000000000080080000000000000000000000000000000000000000000000000000000000000000c094e20e7ac9b433f44a5885e3bdc07e51b309aeb993caa24ba84a661ac010c100010ca800000000000000000000000000000000000080080000000000000000000000000000000000000000000000000000000000000001ab42db8f4ed810bdb143368a2b641edf242af6e3d0de8b1486e2b0e7880d431100010ca8000000000000000000000000000000000000800800000000000000000000000000000000000000000000000000000000000000022d94e4cc4525e4e2d81e8227b6172e97076431a2cf98792d978035edd6e6f3100000000000000000000000000000000000000000000000000000000000000000000000000000012101c74dfb80a80fccb9a4022b2406f79f56305e6a7c931d30140f5d372fe793837e93f9ec6b8d89a9d0ab222eeb27547f66b90ec40fbbdd2a4936b0b0c19ca684ff78888fbf5840d7c8dc3c493b139471750938d7d2c443e2d283e6c5ee9fde3765a756542c42f002af45c362b4b5b1687a8fc24cbf16532b903f7bb289728170dcf597f5255508c623ba247735538376f494cdcdd5bd0c4cb067526eeda0f4745a28d8baf8893ecc1b8cee80690538d66455294a028da03ff2add9d8a88e6ee03ba9ffe3ad7d91d6ac9c69a1f28c468f00fe55eba5651a2b32dc2458e0d14b4dd6d0173df255cd56aa01e8e38edec17ea8933f68543cbdc713279d195551d4211bed5c91f77259a695e6768f6c4b110b2158fcc42423a96dcc4e7f6fddb3e2369d00000000000000000000000000000000000000000000000000000000000000") };
let variant = TxEip4844Variant::TxEip4844(tx);

let signature = Signature::from_rs_and_parity(
let signature = Signature::new(
b256!("6c173c3c8db3e3299f2f728d293b912c12e75243e3aa66911c2329b58434e2a4").into(),
b256!("7dd4d1c228cedc5a414a668ab165d9e888e61e4c3b44cd7daf9cdcc4cec5d6b2").into(),
false,
)
.unwrap();
);

let signed = variant.into_signed(signature);
assert_eq!(
Expand Down
19 changes: 7 additions & 12 deletions crates/consensus/src/transaction/eip7702.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use alloy_eips::{
eip2930::AccessList,
eip7702::{constants::EIP7702_TX_TYPE_ID, SignedAuthorization},
};
use alloy_primitives::{Address, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_primitives::{
Address, Bytes, ChainId, PrimitiveSignature as Signature, TxKind, B256, U256,
};
use alloy_rlp::{BufMut, Decodable, Encodable};
use core::mem;

Expand Down Expand Up @@ -244,10 +246,6 @@ impl SignableTransaction<Signature> for TxEip7702 {
}

fn into_signed(self, signature: Signature) -> Signed<Self> {
// Drop any v chain id value to ensure the signature format is correct at the time of
// combination for an EIP-7702 transaction. V should indicate the y-parity of the
// signature.
let signature = signature.with_parity_bool();
let tx_hash = self.tx_hash(&signature);

Signed::new_unchecked(self, signature, tx_hash)
Expand Down Expand Up @@ -397,7 +395,7 @@ mod tests {
use super::*;
use crate::SignableTransaction;
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{address, b256, hex, Address, Signature, U256};
use alloy_primitives::{address, b256, hex, Address, PrimitiveSignature as Signature, U256};

#[test]
fn encode_decode_eip7702() {
Expand All @@ -418,8 +416,7 @@ mod tests {
b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"),
b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"),
false,
)
.unwrap();
);

let mut buf = vec![];
tx.rlp_encode_signed(&sig, &mut buf);
Expand All @@ -446,8 +443,7 @@ mod tests {
b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"),
b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"),
false,
)
.unwrap();
);
let mut buf = vec![];
tx.rlp_encode_signed(&sig, &mut buf);
let decoded = TxEip7702::rlp_decode_signed(&mut &buf[..]).unwrap();
Expand All @@ -473,8 +469,7 @@ mod tests {
b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"),
b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"),
false,
)
.unwrap();
);

let mut buf = vec![];
tx.rlp_encode_signed(&sig, &mut buf);
Expand Down
Loading

0 comments on commit 2323ddb

Please sign in to comment.