Skip to content

Commit

Permalink
[gh-755] fix integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Feliciss committed Oct 3, 2023
1 parent 883cfb0 commit f9ed104
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use crate::binding_test;
use ethers::prelude::*;
use moveos_types::transaction::MoveAction;
use rooch_key::keypair_type::KeyPairType;
use rooch_key::keystore::{AccountKeystore, InMemKeystore};
use rooch_types::address::RoochAddress;
use rooch_types::crypto::RoochKeyPair;
Expand All @@ -18,7 +17,7 @@ fn test_submit_block() {
let mut binding_test = binding_test::RustBindingTest::new().unwrap();

let keystore = InMemKeystore::<RoochAddress, RoochKeyPair>::new_insecure_for_tests(1);
let sender = keystore.addresses()[0];
let sender = keystore.addresses(Some("".to_owned()))[0];
let sequence_number = 0;

let json = serde_json::json!(
Expand Down Expand Up @@ -58,7 +57,12 @@ fn test_submit_block() {
let action = MoveAction::Function(rooch_types::framework::ethereum_light_client::EthereumLightClientModule::create_submit_new_block_call(&block_header));
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number, action);
let tx = keystore
.sign_transaction(&sender, tx_data, KeyPairType::RoochKeyPairType)
.sign_transaction(
&sender,
tx_data,
KeyPairType::RoochKeyPairType,
Some("".to_owned()),
)
.unwrap();
binding_test.execute(tx).unwrap();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use ethers::types::{Bytes, U256};
use fastcrypto::secp256k1::recoverable::Secp256k1RecoverableKeyPair;
use moveos_types::transaction::MoveAction;
use rooch_key::keypair_type::KeyPairType;
use rooch_key::keystore::{AccountKeystore, InMemKeystore};
use rooch_types::address::{EthereumAddress, MultiChainAddress};
use rooch_types::framework::empty::Empty;
Expand All @@ -25,14 +24,19 @@ fn test_validate() {

let keystore =
InMemKeystore::<EthereumAddress, Secp256k1RecoverableKeyPair>::new_insecure_for_tests(1);
let sender = keystore.addresses()[0];
let sender = keystore.addresses(Some("".to_owned()))[0];
let sequence_number = U256::zero();
let action = MoveAction::new_function_call(Empty::empty_function_id(), vec![], vec![]);
let action_bytes =
Bytes::try_from(bcs::to_bytes(&action).unwrap()).expect("Convert action to bytes failed.");
let tx_data = EthereumTransactionData::new_for_test(sender, sequence_number, action_bytes);
keystore
.sign_transaction(&sender, tx_data.clone(), KeyPairType::EthereumKeyPairType)
.sign_transaction(
&sender,
tx_data.clone(),
KeyPairType::EthereumKeyPairType,
Some("".to_owned()),
)
.unwrap();
let auth_info = tx_data.authenticator_info().unwrap();
let multichain_address = MultiChainAddress::from(sender);
Expand Down
10 changes: 7 additions & 3 deletions crates/rooch-framework-tests/src/tests/native_validator_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

use moveos_types::transaction::MoveAction;
use rooch_key::keypair_type::KeyPairType;
use rooch_key::keystore::{AccountKeystore, InMemKeystore};
use rooch_types::address::RoochAddress;
use rooch_types::crypto::RoochKeyPair;
Expand All @@ -20,12 +19,17 @@ fn test_validate() {
);

let keystore = InMemKeystore::<RoochAddress, RoochKeyPair>::new_insecure_for_tests(1);
let sender = keystore.addresses()[0];
let sender = keystore.addresses(Some("".to_owned()))[0];
let sequence_number = 0;
let action = MoveAction::new_function_call(Empty::empty_function_id(), vec![], vec![]);
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number, action);
let tx = keystore
.sign_transaction(&sender, tx_data, KeyPairType::RoochKeyPairType)
.sign_transaction(
&sender,
tx_data,
KeyPairType::RoochKeyPairType,
Some("".to_owned()),
)
.unwrap();
let auth_info = tx.authenticator_info().unwrap();
let move_tx = tx.construct_moveos_transaction(sender.into()).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use move_core_types::value::MoveValue;
use move_core_types::vm_status::{AbortLocation, VMStatus};
use moveos_types::move_types::FunctionId;
use moveos_types::{module_binding::ModuleBinding, transaction::MoveAction};
use rooch_key::keypair_type::KeyPairType;
use rooch_key::keystore::{AccountKeystore, InMemKeystore};
use rooch_types::address::{EthereumAddress, MultiChainAddress, RoochAddress};
use rooch_types::crypto::RoochKeyPair;
Expand All @@ -34,12 +33,17 @@ fn test_validate_rooch() {
);

let keystore = InMemKeystore::<RoochAddress, RoochKeyPair>::new_insecure_for_tests(1);
let sender = keystore.addresses()[0];
let sender = keystore.addresses(Some("".to_owned()))[0];
let sequence_number = 0;
let action = MoveAction::new_function_call(Empty::empty_function_id(), vec![], vec![]);
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number, action);
let tx = keystore
.sign_transaction(&sender, tx_data, KeyPairType::RoochKeyPairType)
.sign_transaction(
&sender,
tx_data,
KeyPairType::RoochKeyPairType,
Some("".to_owned()),
)
.unwrap();
let auth_info = tx.authenticator_info().unwrap();
let move_tx = tx.construct_moveos_transaction(sender.into()).unwrap();
Expand All @@ -62,14 +66,19 @@ fn test_validate_ethereum() {

let keystore =
InMemKeystore::<EthereumAddress, Secp256k1RecoverableKeyPair>::new_insecure_for_tests(1);
let sender = keystore.addresses()[0];
let sender = keystore.addresses(Some("".to_owned()))[0];
let sequence_number = U256::zero();
let action = MoveAction::new_function_call(Empty::empty_function_id(), vec![], vec![]);
let action_bytes =
Bytes::try_from(bcs::to_bytes(&action).unwrap()).expect("Convert action to bytes failed.");
let tx_data = EthereumTransactionData::new_for_test(sender, sequence_number, action_bytes);
let (_, _sig) = keystore
.sign_transaction(&sender, tx_data.clone(), KeyPairType::EthereumKeyPairType)
.sign_transaction(
&sender,
tx_data.clone(),
KeyPairType::EthereumKeyPairType,
Some("".to_owned()),
)
.unwrap();
let auth_info = tx_data.authenticator_info().unwrap();
let multichain_address = MultiChainAddress::from(sender);
Expand All @@ -93,7 +102,7 @@ fn test_session_key_rooch() {
let mut binding_test = binding_test::RustBindingTest::new().unwrap();

let mut keystore = InMemKeystore::<RoochAddress, RoochKeyPair>::new_insecure_for_tests(1);
let sender = keystore.addresses()[0];
let sender = keystore.addresses(Some("".to_owned()))[0];
let sequence_number = 0;

let session_auth_key = keystore.generate_session_key(&sender).unwrap();
Expand All @@ -111,7 +120,12 @@ fn test_session_key_rooch() {
);
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number, action);
let tx = keystore
.sign_transaction(&sender, tx_data, KeyPairType::RoochKeyPairType)
.sign_transaction(
&sender,
tx_data,
KeyPairType::RoochKeyPairType,
Some("".to_owned()),
)
.unwrap();
binding_test.execute(tx).unwrap();

Expand Down Expand Up @@ -184,7 +198,12 @@ fn test_session_key_rooch() {
let tx_data =
RoochTransactionData::new_for_test(sender, sequence_number + 2, update_time_action);
let tx = keystore
.sign_transaction(&sender, tx_data, KeyPairType::RoochKeyPairType)
.sign_transaction(
&sender,
tx_data,
KeyPairType::RoochKeyPairType,
Some("".to_owned()),
)
.unwrap();
binding_test.execute(tx).unwrap();

Expand Down
7 changes: 4 additions & 3 deletions crates/rooch-key/src/key_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ fn parse_word_length(s: Option<String>) -> Result<MnemonicType, anyhow::Error> {

/// Get a rooch keypair from a random encryption data
pub fn get_rooch_key_pair_from_red() -> (RoochAddress, EncryptionData) {
let random_encryption_data = EncryptionData::new_random();
let random_encryption_data = EncryptionData::new_for_test();
let key_pair_type = KeyPairType::RoochKeyPairType;
let kp: RoochKeyPair = key_pair_type
.retrieve_key_pair(&random_encryption_data, Some("".to_owned()))
Expand All @@ -514,12 +514,13 @@ pub fn get_rooch_key_pair_from_red() -> (RoochAddress, EncryptionData) {

/// Get an ethereum keypair from a random encryption data
pub fn get_ethereum_key_pair_from_red() -> (EthereumAddress, EncryptionData) {
let random_encryption_data = EncryptionData::new_random();
let random_encryption_data = EncryptionData::new_for_test();
let key_pair_type = KeyPairType::EthereumKeyPairType;

let kp: Secp256k1RecoverableKeyPair = key_pair_type
.retrieve_key_pair(&random_encryption_data, Some("".to_owned()))
.unwrap();

let address = EthereumAddress::from(kp.public().clone());

(address, random_encryption_data)
}
33 changes: 13 additions & 20 deletions crates/rooch-types/src/key_struct.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use rand::Rng;
use serde::{Deserialize, Serialize};

use crate::keypair_type::KeyPairType;
Expand All @@ -25,11 +24,19 @@ pub struct GeneratedKeyPair<Addr, KeyPair> {
}

impl EncryptionData {
pub fn new_random() -> EncryptionData {
let hashed_password = generate_random_string(32);
let nonce = generate_random_bytes(12);
let ciphertext = generate_random_bytes(32);
let tag = generate_random_bytes(16);
pub fn new_for_test() -> EncryptionData {
// hashed password from "" string
let hashed_password = "$argon2id$v=19$m=19456,t=2,p=1$zc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc0$RysE6tj+Zu0lLhtKJIedVHrKn9FspulS3vLj/UPaVvQ".to_owned();
let nonce = [202, 31, 86, 27, 113, 29, 104, 237, 218, 110, 152, 145].to_vec();
let ciphertext = [
86, 255, 133, 44, 42, 219, 86, 153, 245, 192, 200, 93, 172, 157, 89, 211, 13, 158, 128,
21, 131, 19, 74, 203, 194, 159, 3, 164, 136, 125, 69, 221,
]
.to_vec();
let tag = [
139, 112, 155, 74, 182, 134, 97, 95, 41, 119, 202, 17, 146, 40, 11, 75,
]
.to_vec();

EncryptionData {
hashed_password,
Expand All @@ -39,17 +46,3 @@ impl EncryptionData {
}
}
}

fn generate_random_string(length: usize) -> String {
let mut rng = rand::thread_rng();
let random_string: String = (0..length)
.map(|_| rng.gen_range(b'a'..=b'z') as char)
.collect();
random_string
}

fn generate_random_bytes(length: usize) -> Vec<u8> {
let mut rng = rand::thread_rng();
let random_bytes: Vec<u8> = (0..length).map(|_| rng.gen()).collect();
random_bytes
}

0 comments on commit f9ed104

Please sign in to comment.