Skip to content

Commit

Permalink
Finish porting C++ test to Rust.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neopallium committed Dec 16, 2024
1 parent 82fe12a commit 3b42dc3
Show file tree
Hide file tree
Showing 3 changed files with 837 additions and 12 deletions.
39 changes: 31 additions & 8 deletions rust/tw_tests/tests/chains/polkadot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ mod polkadot_compile;
mod polkadot_sign;

const GENESIS_HASH: &str = "91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3";
const POLYMESH_GENESIS_HASH: &str =
"6fbd74e5e1d0a61d52ccfe9d4adaed16dd3a7caa37c6bc4d0c2fa12e8b2f4063";
const PRIVATE_KEY: &str = "abf8e5bdbe30c65656c0a3cbd181ff8a56294a69dfedd27982aace4a76909115";
const PRIVATE_KEY_IOS: &str = "37932b086586a6675e66e562fe68bd3eeea4177d066619c602fe3efc290ada62";
const PRIVATE_KEY_2: &str = "70a794d4f1019c3ce002f33062f45029c4f930a56b3d20ec477f7668c6bbc37f";
const PRIVATE_KEY_POLKADOT: &str =
"298fcced2b497ed48367261d8340f647b3fca2d9415d57c2e3c5ef90482a2266";
const ACCOUNT_2: &str = "14Ztd3KJDaB9xyJtRkREtSZDdhLSbm7UUKt8Z7AwSv7q85G2";

pub fn helper_sign(coin: CoinType, input: SigningInput<'_>) -> String {
Expand All @@ -30,16 +34,20 @@ pub fn helper_sign(coin: CoinType, input: SigningInput<'_>) -> String {
signed_output.encoded.to_hex()
}

pub fn helper_encode(coin: CoinType, input: &SigningInput<'_>) -> String {
let mut pre_imager = PreImageHelper::<PreSigningOutput>::default();
let preimage_output = pre_imager.pre_image_hashes(coin, input);

assert_eq!(preimage_output.error, SigningError::OK);
preimage_output.data.to_hex()
}

pub fn helper_encode_and_maybe_sign(
coin: CoinType,
input: SigningInput<'_>,
) -> (String, Option<String>) {
// Step 1: Obtain preimage hash
let mut pre_imager = PreImageHelper::<PreSigningOutput>::default();
let preimage_output = pre_imager.pre_image_hashes(coin, &input);

assert_eq!(preimage_output.error, SigningError::OK);
let preimage = preimage_output.data.to_hex();
let preimage = helper_encode(coin, &input);
if input.private_key.is_empty() {
return (preimage, None);
}
Expand All @@ -55,6 +63,7 @@ pub fn helper_encode_and_compile(
input: Proto::SigningInput,
signature: &str,
public_key: &str,
ed25519: bool,
) -> (String, String) {
// Step 1: Obtain preimage hash
let mut pre_imager = PreImageHelper::<CompilerProto::PreSigningOutput>::default();
Expand All @@ -67,12 +76,14 @@ pub fn helper_encode_and_compile(

// Simulate signature, normally obtained from signature server
let signature_bytes = signature.decode_hex().unwrap();
let signature = Signature::try_from(signature_bytes.as_slice()).unwrap();
let public_key = public_key.decode_hex().unwrap();
let public = PublicKey::try_from(public_key.as_slice()).unwrap();

// Verify signature (pubkey & hash & signature)
assert!(public.verify(signature, preimage_output.data.into()));
if !ed25519 {
let signature = Signature::try_from(signature_bytes.as_slice()).unwrap();
let public = PublicKey::try_from(public_key.as_slice()).unwrap();
assert!(public.verify(signature, preimage_output.data.into()));
}

// Compile transaction info
let mut compiler = CompilerHelper::<Proto::SigningOutput>::default();
Expand All @@ -98,3 +109,15 @@ pub fn staking_call(
message_oneof: call,
})
}

pub fn polymesh_call(
call: Proto::mod_Identity::OneOfmessage_oneof,
) -> Proto::mod_SigningInput::OneOfmessage_oneof {
Proto::mod_SigningInput::OneOfmessage_oneof::polymesh_call(Proto::PolymeshCall {
message_oneof: Proto::mod_PolymeshCall::OneOfmessage_oneof::identity_call(
Proto::Identity {
message_oneof: call,
},
),
})
}
2 changes: 1 addition & 1 deletion rust/tw_tests/tests/chains/polkadot/polkadot_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn test_polkadot_compile_transfer() {
let public_key = "d84accbb64934815506288fafbfc7d275e64aa4e3cd9c5392db6e83b13256bf3";

let (preimage, signed) =
helper_encode_and_compile(CoinType::Polkadot, input, signature, public_key);
helper_encode_and_compile(CoinType::Polkadot, input, signature, public_key, true);

assert_eq!(
preimage,
Expand Down
Loading

0 comments on commit 3b42dc3

Please sign in to comment.