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

Allow trailing zero values in RLP decoding #74

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 8 additions & 6 deletions translator/src/rlp/telos_rlp_decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TxLegacy, Error> {
let nonce = u64::decode(data).map_err(|_| Error::Custom("Failed to decode nonce"))?;
Expand Down Expand Up @@ -87,14 +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 {
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(),
})
}
}

Expand Down
29 changes: 28 additions & 1 deletion translator/tests/rlp.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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!");
}
Loading