From 910f8874ac377a118796a23811bf98a929a6e0c9 Mon Sep 17 00:00:00 2001 From: Jesse Schulman Date: Sat, 28 Sep 2024 16:24:51 -0700 Subject: [PATCH 1/2] Allow trailing zero values in RLP decoding --- translator/src/rlp/telos_rlp_decode.rs | 14 +++++++++---- translator/tests/rlp.rs | 29 +++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/translator/src/rlp/telos_rlp_decode.rs b/translator/src/rlp/telos_rlp_decode.rs index f9f4881..ad52456 100644 --- a/translator/src/rlp/telos_rlp_decode.rs +++ b/translator/src/rlp/telos_rlp_decode.rs @@ -4,6 +4,7 @@ use alloy_consensus::{SignableTransaction, Signed, TxLegacy}; use alloy_rlp::Result; use bytes::Buf; +use tracing::error; fn decode_fields(data: &mut &[u8]) -> Result { let nonce = u64::decode(data).map_err(|_| Error::Custom("Failed to decode nonce"))?; @@ -88,10 +89,15 @@ impl TelosTxDecodable for TxLegacy { let signed = tx.into_signed(sig); if buf.len() + header.payload_length != original_len { - return Err(Error::ListLengthMismatch { - expected: header.payload_length, - got: original_len - buf.len(), - }); + for b in buf.iter() { + if *b != 128 { + error!("Transaction has trailing non-zero RLP values: {:?}", signed); + return Err(Error::ListLengthMismatch { + expected: header.payload_length, + got: original_len - buf.len(), + }); + } + } } Ok(signed) diff --git a/translator/tests/rlp.rs b/translator/tests/rlp.rs index e4b1d9a..81eab35 100644 --- a/translator/tests/rlp.rs +++ b/translator/tests/rlp.rs @@ -1,10 +1,12 @@ use alloy::hex; use alloy::hex::FromHex; -use alloy::primitives::{Address, B256}; +use alloy::primitives::{Address, Signature, B256, U256}; use alloy_consensus::TxLegacy; use antelope::chain::checksum::Checksum256; +use std::str::FromStr; use telos_translator_rs::rlp::telos_rlp_decode::TelosTxDecodable; use telos_translator_rs::transaction::make_unique_vrs; +use tracing::info; #[test] fn test_unsigned_trx() { @@ -80,3 +82,28 @@ fn test_signed_trx() { assert!(signed_legacy.is_ok()); } + +#[tokio::test] +async fn test_trailing_empty_values() { + // this buffer has trailing empty RLP values, which have been accepted on chain + // and so we need to handle them + let byte_array: [u8; 43] = [ + 234, 21, 133, 117, 98, 209, 251, 63, 131, 30, 132, 128, 148, 221, 124, 155, 23, 110, 221, + 57, 225, 22, 88, 115, 0, 111, 245, 56, 10, 44, 0, 51, 174, 130, 39, 16, 130, 0, 0, 128, + 128, 128, 128, + ]; + let mut slice: &[u8] = &byte_array; + let buf = &mut slice; + let r = U256::from_str( + "7478307613393818857995123362551696556625819847066981460737539381080402549198", + ) + .unwrap(); + let s = U256::from_str( + "93208746529385687702128536437164864077231874732405909428462768306792425324544", + ) + .unwrap(); + let v = 42u64; + let sig = Signature::from_rs_and_parity(r, s, v); + TxLegacy::decode_telos_signed_fields(buf, Some(sig.unwrap())).unwrap(); + info!("Passed!"); +} From 824118f9a3fc1654acf73bf45ba3ff0566b2c445 Mon Sep 17 00:00:00 2001 From: Jesse Schulman Date: Sun, 29 Sep 2024 23:10:25 -0700 Subject: [PATCH 2/2] Better logic for zero values check --- translator/src/rlp/telos_rlp_decode.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/translator/src/rlp/telos_rlp_decode.rs b/translator/src/rlp/telos_rlp_decode.rs index ad52456..21d5dde 100644 --- a/translator/src/rlp/telos_rlp_decode.rs +++ b/translator/src/rlp/telos_rlp_decode.rs @@ -88,19 +88,15 @@ impl TelosTxDecodable for TxLegacy { tx.chain_id = sig.v().chain_id(); let signed = tx.into_signed(sig); - if buf.len() + header.payload_length != original_len { - for b in buf.iter() { - if *b != 128 { - error!("Transaction has trailing non-zero RLP values: {:?}", signed); - return Err(Error::ListLengthMismatch { - expected: header.payload_length, - got: original_len - buf.len(), - }); - } - } + if buf.len() + header.payload_length == original_len || buf.iter().all(|&b| b == 128) { + return Ok(signed); } - Ok(signed) + error!("Transaction has trailing non-zero RLP values: {:?}", signed); + Err(Error::ListLengthMismatch { + expected: header.payload_length, + got: original_len - buf.len(), + }) } }