diff --git a/Cargo.lock b/Cargo.lock index cedf2d8..d71c242 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -188,7 +188,7 @@ dependencies = [ [[package]] name = "anychain-ethereum" -version = "0.1.17" +version = "0.1.18" dependencies = [ "anychain-core", "ethabi", @@ -310,7 +310,7 @@ dependencies = [ [[package]] name = "anychain-tron" -version = "0.2.6" +version = "0.2.7" dependencies = [ "anychain-core", "base58", diff --git a/anychain-tron/Cargo.toml b/anychain-tron/Cargo.toml index 41993b1..428fbf8 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.6" +version = "0.2.7" keywords = ["tron", "blockchain", "cryptocurrencies", "wallet", "transactions"] # Workspace inherited keys diff --git a/anychain-tron/src/address.rs b/anychain-tron/src/address.rs index 596376a..8440901 100644 --- a/anychain-tron/src/address.rs +++ b/anychain-tron/src/address.rs @@ -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 { + 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 { @@ -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()); + } } diff --git a/anychain-tron/src/transaction.rs b/anychain-tron/src/transaction.rs index bd21b3b..e4fbab9 100644 --- a/anychain-tron/src/transaction.rs +++ b/anychain-tron/src/transaction.rs @@ -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] @@ -248,9 +250,10 @@ mod tests { let transaction = TronTransaction::new(¶m).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] @@ -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); } }