From 26133d226f5e4a4f45585de460f1a3f07ae49ecd Mon Sep 17 00:00:00 2001 From: xiaoguang Date: Fri, 15 Dec 2023 10:47:38 +0800 Subject: [PATCH] test: add tcx-filecoin test case --- token-core/tcx-filecoin/src/address.rs | 23 +++++++++++++++++ token-core/tcx-filecoin/src/key_info.rs | 25 ++++++++++++++++++- token-core/tcx-filecoin/src/signer.rs | 33 +++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/token-core/tcx-filecoin/src/address.rs b/token-core/tcx-filecoin/src/address.rs index b4c57858..39e88db9 100644 --- a/token-core/tcx-filecoin/src/address.rs +++ b/token-core/tcx-filecoin/src/address.rs @@ -103,7 +103,10 @@ impl ToString for FilecoinAddress { #[cfg(test)] mod tests { + use std::str::FromStr; + use crate::address::FilecoinAddress; + use tcx_common::FromHex; use tcx_constants::{coin_info_from_param, CoinInfo, CurveType}; use tcx_keystore::{Address, Keystore, Metadata}; use tcx_primitive::TypedPublicKey; @@ -267,4 +270,24 @@ mod tests { assert_eq!(address.to_string(), expected); } } + + #[test] + #[should_panic(expected = "InvalidCurveType")] + fn test_invalid_curve_type() { + let input = + Vec::from_hex("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc") + .unwrap(); + let pk = TypedPublicKey::from_slice(CurveType::ED25519, &input).unwrap(); + FilecoinAddress::from_public_key(&pk, &CoinInfo::default()).unwrap(); + } + + #[test] + fn test_from_str() { + let address = + FilecoinAddress::from_str("t1xtwapqc6nh4si2hcwpr3656iotzmlwumogqbuaa").unwrap(); + assert_eq!( + address.to_string(), + "t1xtwapqc6nh4si2hcwpr3656iotzmlwumogqbuaa" + ); + } } diff --git a/token-core/tcx-filecoin/src/key_info.rs b/token-core/tcx-filecoin/src/key_info.rs index b6a96004..4a2dafd7 100644 --- a/token-core/tcx-filecoin/src/key_info.rs +++ b/token-core/tcx-filecoin/src/key_info.rs @@ -61,7 +61,7 @@ mod tests { } #[test] - fn test_from_private_key() { + fn test_from_private_key_secp256k1() { let key_info = KeyInfo::from_private_key( CurveType::SECP256k1, &Vec::from_hex("0ae0d7924285c3921e9948594d0b100a248d9a3da96d33d1213f503ac957c45b") @@ -71,4 +71,27 @@ mod tests { assert_eq!(key_info.to_json().unwrap().to_hex(), "7b2254797065223a22736563703235366b31222c22507269766174654b6579223a22437544586b6b4b46773549656d55685a545173514369534e6d6a327062545052495439514f736c587846733d227d"); } + + #[test] + fn test_from_private_key_bls() { + let key_info = KeyInfo::from_private_key( + CurveType::BLS, + &Vec::from_hex("0ae0d7924285c3921e9948594d0b100a248d9a3da96d33d1213f503ac957c45b") + .unwrap(), + ) + .unwrap(); + + assert_eq!(key_info.to_json().unwrap().to_hex(), "7b2254797065223a22626c73222c22507269766174654b6579223a22437544586b6b4b46773549656d55685a545173514369534e6d6a327062545052495439514f736c587846733d227d"); + } + + #[test] + #[should_panic(expected = "InvalidCurveType")] + fn test_from_private_key_invalid_curve_type() { + KeyInfo::from_private_key( + CurveType::ED25519Blake2bNano, + &Vec::from_hex("0ae0d7924285c3921e9948594d0b100a248d9a3da96d33d1213f503ac957c45b") + .unwrap(), + ) + .unwrap(); + } } diff --git a/token-core/tcx-filecoin/src/signer.rs b/token-core/tcx-filecoin/src/signer.rs index 4d34e32c..9fc55c9b 100644 --- a/token-core/tcx-filecoin/src/signer.rs +++ b/token-core/tcx-filecoin/src/signer.rs @@ -184,4 +184,37 @@ mod tests { ); assert_eq!(signature.data, "tNRsgNdWO6UdY9IOh5tvzcL1Dwi7gljLt22aITKUgtF363lrP2gHxOX9oNGhnFD6BoM4/Y/HMzETlYF0r4+1aHZo1F8fV3XDwxwwz1HKxoDIreXBtPAjTiqBGlTiMwPX"); } + + #[test] + #[should_panic(expected = "InvalidCurveType")] + fn test_sign_invalid_curve_type() { + let unsigned_message = UnsignedMessage { + to: "f1zlkjwo5pnm6petm4u4luj6gb6e64eecrw4t4stq".to_string(), + from: "f3qdyntx5snnwgmjkp2ztd6tf6hhcmurxfj53zylrqyympwvzvbznx6vnvdqloate5eviphnzrkupno4wheesa".to_string(), + nonce: 1, + value: "10000000000000000".to_string(), + gas_limit: 491585, + gas_fee_cap: "151367".to_string(), + gas_premium: "150313".to_string(), + method: 0, + params: "".to_string() + }; + + let key_info = + KeyInfo::from_lotus( + &Vec::from_hex("7b2254797065223a22626c73222c22507269766174654b6579223a2269376b4f2b7a78633651532b7637597967636d555968374d55595352657336616e6967694c684b463830383d227d").unwrap()).unwrap(); + let private_key = key_info.decode_private_key().unwrap(); + let mut ks = + Keystore::from_private_key(&private_key.to_hex(), "Password", Metadata::default()); + ks.unlock_by_password("Password").unwrap(); + + let sign_context = SignatureParameters { + curve: CurveType::SubSr25519, + derivation_path: "".to_string(), + chain_type: "FILECOIN".to_string(), + ..Default::default() + }; + ks.sign_transaction(&sign_context, &unsigned_message) + .unwrap(); + } }