From 58bf1d05f1ae31e123a485b51ac1052b1ec1b9b4 Mon Sep 17 00:00:00 2001 From: aya015757881 <2581015450@qq.com> Date: Fri, 5 Jul 2024 11:12:58 +0800 Subject: [PATCH 1/3] fix: inability to decode signed tron transaction stream --- anychain-tron/src/transaction.rs | 40 ++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/anychain-tron/src/transaction.rs b/anychain-tron/src/transaction.rs index 6013b7c..c67b56e 100644 --- a/anychain-tron/src/transaction.rs +++ b/anychain-tron/src/transaction.rs @@ -134,9 +134,9 @@ impl Transaction for TronTransaction { type TransactionId = TronTransactionId; type TransactionParameters = TronTransactionParameters; - fn new(parameters: &Self::TransactionParameters) -> Result { + fn new(params: &Self::TransactionParameters) -> Result { Ok(Self { - data: parameters.clone(), + data: params.clone(), signature: None, }) } @@ -146,9 +146,18 @@ impl Transaction for TronTransaction { self.to_bytes() } - fn from_bytes(transaction: &[u8]) -> Result { - let raw = TransactionRaw::parse_from_bytes(transaction) - .map_err(|e| TransactionError::Crate("protobuf", e.to_string()))?; + fn from_bytes(tx: &[u8]) -> Result { + let (raw, sig) = + if let Ok(tx) = TransactionProto::parse_from_bytes(tx) { + let raw = tx.raw_data.unwrap(); + let sig = tx.signature[0].clone(); + (raw, Some(sig)) + } else if let Ok(raw) = TransactionRaw::parse_from_bytes(tx) { + (raw, None) + } else { + return Err(TransactionError::Message("illegal tron transaction stream".to_string())); + }; + let param = TronTransactionParameters { timestamp: raw.timestamp, expiration: raw.expiration - raw.timestamp, @@ -160,15 +169,17 @@ impl Transaction for TronTransaction { contract: raw.contract[0].clone(), }; - Ok(Self { - data: param, - signature: None, - }) + let sig = match sig { + Some(sig) => if sig.len() == 65 { Some(TronTransactionSignature(sig)) } else { None }, + None => None, + }; + + Ok(Self {data: param, signature: sig}) } fn to_bytes(&self) -> Result, TransactionError> { let raw = self.data.to_transaction_raw()?; - match self.signature.clone() { + match &self.signature { Some(sign) => { let mut signed_tx = TransactionProto::new(); signed_tx.raw_data = ::protobuf::MessageField::some(raw); @@ -184,14 +195,7 @@ impl Transaction for TronTransaction { } fn to_transaction_id(&self) -> Result { - let bytes = self - .data - .to_transaction_raw()? - .write_to_bytes() - .map_err(|e| TransactionError::Crate("protobuf", e.to_string()))?; - Ok(Self::TransactionId { - txid: crypto::sha256(&bytes).to_vec(), - }) + Ok(Self::TransactionId { txid: crypto::sha256(&self.to_bytes()?).to_vec() }) } } From 79ed253e2609445492482d426a9d5a8955319548 Mon Sep 17 00:00:00 2001 From: aya015757881 <2581015450@qq.com> Date: Fri, 5 Jul 2024 11:14:01 +0800 Subject: [PATCH 2/3] refactor: cargo fmt & version update --- Cargo.lock | 2 +- anychain-tron/Cargo.toml | 2 +- anychain-tron/src/transaction.rs | 26 +++++++++++++++++++------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0c0c366..de8dba8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -309,7 +309,7 @@ dependencies = [ [[package]] name = "anychain-tron" -version = "0.2.3" +version = "0.2.4" dependencies = [ "anychain-core", "base58", diff --git a/anychain-tron/Cargo.toml b/anychain-tron/Cargo.toml index ede8007..a29d196 100644 --- a/anychain-tron/Cargo.toml +++ b/anychain-tron/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "anychain-tron" description = "A Rust library for Tron-focused cryptocurrency wallets, enabling seamless transactions on the Tron blockchain" -version = "0.2.3" +version = "0.2.4" keywords = ["tron", "blockchain", "cryptocurrencies", "wallet", "transactions"] # Workspace inherited keys diff --git a/anychain-tron/src/transaction.rs b/anychain-tron/src/transaction.rs index c67b56e..89ee165 100644 --- a/anychain-tron/src/transaction.rs +++ b/anychain-tron/src/transaction.rs @@ -147,15 +147,16 @@ impl Transaction for TronTransaction { } fn from_bytes(tx: &[u8]) -> Result { - let (raw, sig) = - if let Ok(tx) = TransactionProto::parse_from_bytes(tx) { + let (raw, sig) = if let Ok(tx) = TransactionProto::parse_from_bytes(tx) { let raw = tx.raw_data.unwrap(); let sig = tx.signature[0].clone(); (raw, Some(sig)) } else if let Ok(raw) = TransactionRaw::parse_from_bytes(tx) { (raw, None) } else { - return Err(TransactionError::Message("illegal tron transaction stream".to_string())); + return Err(TransactionError::Message( + "illegal tron transaction stream".to_string(), + )); }; let param = TronTransactionParameters { @@ -170,11 +171,20 @@ impl Transaction for TronTransaction { }; let sig = match sig { - Some(sig) => if sig.len() == 65 { Some(TronTransactionSignature(sig)) } else { None }, + Some(sig) => { + if sig.len() == 65 { + Some(TronTransactionSignature(sig)) + } else { + None + } + } None => None, }; - - Ok(Self {data: param, signature: sig}) + + Ok(Self { + data: param, + signature: sig, + }) } fn to_bytes(&self) -> Result, TransactionError> { @@ -195,7 +205,9 @@ impl Transaction for TronTransaction { } fn to_transaction_id(&self) -> Result { - Ok(Self::TransactionId { txid: crypto::sha256(&self.to_bytes()?).to_vec() }) + Ok(Self::TransactionId { + txid: crypto::sha256(&self.to_bytes()?).to_vec(), + }) } } From ab96e04e588daa1189b8fb502e192f9a65f742e5 Mon Sep 17 00:00:00 2001 From: aya015757881 <2581015450@qq.com> Date: Fri, 5 Jul 2024 11:24:47 +0800 Subject: [PATCH 3/3] refactor: logic simplified --- anychain-tron/src/transaction.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/anychain-tron/src/transaction.rs b/anychain-tron/src/transaction.rs index 89ee165..bd21b3b 100644 --- a/anychain-tron/src/transaction.rs +++ b/anychain-tron/src/transaction.rs @@ -150,7 +150,10 @@ impl Transaction for TronTransaction { let (raw, sig) = if let Ok(tx) = TransactionProto::parse_from_bytes(tx) { let raw = tx.raw_data.unwrap(); let sig = tx.signature[0].clone(); - (raw, Some(sig)) + match sig.len() == 65 { + true => (raw, Some(TronTransactionSignature(sig))), + false => (raw, None), + } } else if let Ok(raw) = TransactionRaw::parse_from_bytes(tx) { (raw, None) } else { @@ -170,17 +173,6 @@ impl Transaction for TronTransaction { contract: raw.contract[0].clone(), }; - let sig = match sig { - Some(sig) => { - if sig.len() == 65 { - Some(TronTransactionSignature(sig)) - } else { - None - } - } - None => None, - }; - Ok(Self { data: param, signature: sig,