Skip to content

Commit

Permalink
Merge pull request #248 from 0xcregis/247-fix-anychain-tron-cannot-de…
Browse files Browse the repository at this point in the history
…serialize-signed-tx-stream

fix: anychain tron cannot deserialize signed tx stream
  • Loading branch information
shuimuliang authored Jul 5, 2024
2 parents 346b64c + ab96e04 commit 47af9e6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion anychain-tron/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
34 changes: 21 additions & 13 deletions anychain-tron/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ impl Transaction for TronTransaction {
type TransactionId = TronTransactionId;
type TransactionParameters = TronTransactionParameters;

fn new(parameters: &Self::TransactionParameters) -> Result<Self, TransactionError> {
fn new(params: &Self::TransactionParameters) -> Result<Self, TransactionError> {
Ok(Self {
data: parameters.clone(),
data: params.clone(),
signature: None,
})
}
Expand All @@ -146,9 +146,22 @@ impl Transaction for TronTransaction {
self.to_bytes()
}

fn from_bytes(transaction: &[u8]) -> Result<Self, TransactionError> {
let raw = TransactionRaw::parse_from_bytes(transaction)
.map_err(|e| TransactionError::Crate("protobuf", e.to_string()))?;
fn from_bytes(tx: &[u8]) -> Result<Self, TransactionError> {
let (raw, sig) = if let Ok(tx) = TransactionProto::parse_from_bytes(tx) {
let raw = tx.raw_data.unwrap();
let sig = tx.signature[0].clone();
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 {
return Err(TransactionError::Message(
"illegal tron transaction stream".to_string(),
));
};

let param = TronTransactionParameters {
timestamp: raw.timestamp,
expiration: raw.expiration - raw.timestamp,
Expand All @@ -162,13 +175,13 @@ impl Transaction for TronTransaction {

Ok(Self {
data: param,
signature: None,
signature: sig,
})
}

fn to_bytes(&self) -> Result<Vec<u8>, 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);
Expand All @@ -184,13 +197,8 @@ impl Transaction for TronTransaction {
}

fn to_transaction_id(&self) -> Result<Self::TransactionId, TransactionError> {
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(),
txid: crypto::sha256(&self.to_bytes()?).to_vec(),
})
}
}
Expand Down

0 comments on commit 47af9e6

Please sign in to comment.