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(consensus): populate chain id when decoding signed legacy txs #137

Merged
merged 7 commits into from
Jan 19, 2024
Merged
Changes from 3 commits
Commits
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
34 changes: 32 additions & 2 deletions crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::TxKind;
use alloy_network::{Signed, Transaction};
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, B256, U256};
use alloy_primitives::{keccak256, Bytes, ChainId, Parity, Signature, B256, U256};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header, Result};
use std::mem;

Expand Down Expand Up @@ -226,9 +226,17 @@ impl Transaction for TxLegacy {
if !header.list {
return Err(alloy_rlp::Error::UnexpectedString);
}
let tx = Self::decode_fields(buf)?;
let mut tx = Self::decode_fields(buf)?;

let signature = Signature::decode_rlp_vrs(buf)?;
Evalir marked this conversation as resolved.
Show resolved Hide resolved

let v = signature.v();

match v {
Parity::Eip155(_) => tx.chain_id = v.chain_id(),
_ => {}
};

Ok(tx.into_signed(signature))
}

Expand Down Expand Up @@ -336,4 +344,26 @@ mod tests {
assert_eq!(*signed_tx.hash(), hash, "Expected same hash");
assert_eq!(signed_tx.recover_signer().unwrap(), signer, "Recovering signer should pass.");
}

#[test]
#[cfg(feature = "k256")]
fn decode_legacy_and_recover_signer() {
use crate::TxLegacy;
Evalir marked this conversation as resolved.
Show resolved Hide resolved
use alloy_network::Signed;
use alloy_primitives::address;
use alloy_rlp::Decodable;

let raw_tx = "f9015482078b8505d21dba0083022ef1947a250d5630b4cf539739df2c5dacb4c659f2488d880c46549a521b13d8b8e47ff36ab50000000000000000000000000000000000000000000066ab5a608bd00a23f2fe000000000000000000000000000000000000000000000000000000000000008000000000000000000000000048c04ed5691981c42154c6167398f95e8f38a7ff00000000000000000000000000000000000000000000000000000000632ceac70000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006c6ee5e31d828de241282b9606c8e98ea48526e225a0c9077369501641a92ef7399ff81c21639ed4fd8fc69cb793cfa1dbfab342e10aa0615facb2f1bcf3274a354cfe384a38d0cc008a11c2dd23a69111bc6930ba27a8";

let tx = <Signed<TxLegacy> as Decodable>::decode(
&mut alloy_primitives::hex::decode(raw_tx).unwrap().as_slice(),
)
.unwrap();

let recovered = tx.recover_signer().unwrap();
let expected = address!("a12e1462d0ceD572f396F58B6E2D03894cD7C8a4");

assert_eq!(tx.chain_id, Some(1), "Expected same chain id");
assert_eq!(expected, recovered, "Expected same signer");
}
}
Loading