Skip to content

Commit

Permalink
test: add tcx-filecoin test case
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoguang1010 committed Dec 15, 2023
1 parent e5b57a3 commit 26133d2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
23 changes: 23 additions & 0 deletions token-core/tcx-filecoin/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"
);
}
}
25 changes: 24 additions & 1 deletion token-core/tcx-filecoin/src/key_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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();
}
}
33 changes: 33 additions & 0 deletions token-core/tcx-filecoin/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 26133d2

Please sign in to comment.