Skip to content

Commit

Permalink
fix: swc-03 unsafe std::mem::transmute, publish anychain-tron 0.2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
loki-cmu committed Sep 7, 2024
1 parent 047d080 commit a077e31
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
4 changes: 2 additions & 2 deletions 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.6"
version = "0.2.7"
keywords = ["tron", "blockchain", "cryptocurrencies", "wallet", "transactions"]

# Workspace inherited keys
Expand Down
31 changes: 28 additions & 3 deletions anychain-tron/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,17 @@ impl TronAddress {
&self.0
}

pub fn from_bytes(raw: &[u8]) -> &Self {
assert!(raw.len() == 21);
unsafe { std::mem::transmute(&raw[0]) }
pub fn from_bytes(raw: &[u8]) -> Result<Self, AddressError> {
if raw.len() != 21 {
return Err(AddressError::InvalidAddress("Invalid length".to_string()));
}

let mut address = [0u8; 21];
address.copy_from_slice(raw);
Ok(TronAddress(address))

// assert!(raw.len() == 21);
// unsafe { std::mem::transmute(&raw[0]) }
}

pub fn to_base58(&self) -> String {
Expand Down Expand Up @@ -247,4 +255,21 @@ mod tests {
let addr = TronAddress::from_public_key(&public, &TronFormat::Standard).unwrap();
assert_eq!(addr.to_string(), "TQHAvs2ZFTbsd93ycTfw1Wuf1e4WsPZWCp");
}

#[test]
fn test_address_from_bytes() {
let bytes = [
65, 150, 163, 186, 206, 90, 218, 207, 99, 126, 183, 204, 121, 213, 120, 127, 66, 71,
218, 75, 190,
];
let addr = TronAddress::from_bytes(&bytes);
assert!(addr.is_ok());

let malicious_bytes: [u8; 22] = [
0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
let addr = TronAddress::from_bytes(&malicious_bytes);
assert!(addr.is_err());
}
}
18 changes: 11 additions & 7 deletions anychain-tron/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,12 @@ mod tests {
#[test]
pub fn test_txid() {
let transaction = build_trx_transaction();
println!("{}", transaction.to_transaction_id().unwrap());
dbg!("{}", transaction.to_transaction_id().unwrap());
let raw = transaction.data.to_transaction_raw().unwrap();
let raw_bytes = crypto::sha256(&raw.write_to_bytes().unwrap());
println!("{}", hex::encode(raw_bytes));
dbg!("{}", hex::encode(raw_bytes));

assert_eq!(transaction.to_transaction_id().unwrap().txid, raw_bytes);
}

#[test]
Expand All @@ -248,9 +250,10 @@ mod tests {
let transaction = TronTransaction::new(&param).unwrap();

let bytes = transaction.to_bytes().unwrap();
println!("{}", hex::encode(bytes));
println!("{}", transaction.to_transaction_id().unwrap());
println!("{:?}", transaction.data);

dbg!("{}", hex::encode(bytes));
dbg!("{}", transaction.to_transaction_id().unwrap());
dbg!("{:?}", transaction.data);
}

#[test]
Expand All @@ -269,7 +272,8 @@ mod tests {
#[test]
pub fn test_raw() {
let raw = "0a025aa722088cb23bfcb18ea03c40facee394ad305a67080112630a2d747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e747261637412320a1541fa3146ab779ce02392d11209f524ee75d4088a45121541436d74fc1577266b7290b85801145d9c5287e19418c0843d709afadf94ad30900180ade204";
let transaction = TronTransaction::from_bytes(&hex::decode(raw).unwrap()).unwrap();
println!("{:?}", transaction.data);
let transaction = TronTransaction::from_bytes(&hex::decode(raw).unwrap());
assert!(transaction.is_ok());
dbg!("{:?}", transaction.unwrap().data);
}
}

0 comments on commit a077e31

Please sign in to comment.