Skip to content

Commit

Permalink
fix: infer missing type field for transactions where v is 0x0 or 0x1 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
carneiro-cw authored Mar 11, 2025
1 parent 462fa57 commit 6a66101
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/eth/primitives/external_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ impl<'de> serde::Deserialize<'de> for ExternalTransaction {
let mut value = Value::deserialize(deserializer)?;

if let Value::Object(ref mut map) = value {
// If v is 0x0 or 0x1, this is a type 2 (EIP-1559) transaction
if let Some(Value::String(v_value)) = map.get("v") {
if (v_value == "0x0" || v_value == "0x1") && !map.contains_key("type") {
map.insert("type".to_string(), Value::String("0x2".to_string()));
}
}

// Check if this is a type 2 transaction
if let Some(Value::String(type_value)) = map.get("type") {
if type_value == "0x2" {
Expand Down Expand Up @@ -205,4 +212,48 @@ mod tests {

assert!(matches!(tx.0.inner, TxEnvelope::Eip1559(_)));
}

#[test]
fn test_deserialize_type2_inferred_from_v_value() {
let json = json!({
"hash": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
"from": "0x1234567890123456789012345678901234567890",
"to": "0x0987654321098765432109876543210987654321",
"gas": "0x76c0",
"gasPrice": "0x9184e72a000",
"nonce": "0x1",
"value": "0x9184e72a",
"input": "0x",
"chainId": "0x1",
"r": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
"s": "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
"v": "0x0"
// type field is missing, but v is 0x0 so it should be inferred as type 2
});

let tx: ExternalTransaction = serde_json::from_value(json).unwrap();

assert!(matches!(tx.0.inner, TxEnvelope::Eip1559(_)));

// Test with v = 0x1 as well
let json = json!({
"hash": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
"from": "0x1234567890123456789012345678901234567890",
"to": "0x0987654321098765432109876543210987654321",
"gas": "0x76c0",
"gasPrice": "0x9184e72a000",
"nonce": "0x1",
"value": "0x9184e72a",
"input": "0x",
"chainId": "0x1",
"r": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
"s": "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
"v": "0x1"
// type field is missing, but v is 0x1 so it should be inferred as type 2
});

let tx: ExternalTransaction = serde_json::from_value(json).unwrap();

assert!(matches!(tx.0.inner, TxEnvelope::Eip1559(_)));
}
}

0 comments on commit 6a66101

Please # to comment.