Skip to content

Commit

Permalink
fixed unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
andreivasilescu24 committed Aug 21, 2024
1 parent 1f16dd3 commit 8654092
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 40 deletions.
2 changes: 1 addition & 1 deletion framework/meta/src/cmd/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ fn new(new_args: &WalletNewArgs) {
}
}

fn generate_pem_content(address: &Address, private_key: &str, public_key: &str) -> String {
pub fn generate_pem_content(address: &Address, private_key: &str, public_key: &str) -> String {
let concat_keys = format!("{}{}", private_key, public_key);
let concat_keys_b64 = base64_encode(concat_keys);

Expand Down
110 changes: 71 additions & 39 deletions framework/meta/tests/wallet_test.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,91 @@
use std::fs;
use std::fs::{self, File};
use std::io::Write;

use multiversx_sc_meta::cli::{WalletAction, WalletArgs, WalletConvertArgs};
use multiversx_sc_meta::cmd::wallet::wallet;
use multiversx_sc_snippets::imports::Wallet;
use multiversx_sc_meta::cmd::wallet::generate_pem_content;
use multiversx_sc_snippets::sdk::{crypto::public_key::PublicKey, data::address::Address};
use multiversx_sc_snippets::{hex, imports::Wallet};

const ALICE_PEM_PATH: &str = "../snippets/src/test_wallets/alice.pem";
const ALICE_KEYSTORE_PATH: &str = "alice.json";

// Insert a password for your keystore, followed by (CTRL+D) for Linux/Mac or (CTRL+Z) for Windows
fn create_keystore_from_pem() {
let wallet_convert_args = WalletConvertArgs {
infile: Some(ALICE_PEM_PATH.to_string()),
outfile: Some(ALICE_KEYSTORE_PATH.to_string()),
from: "pem".to_string(),
to: "keystore-secret".to_string(),
};
let wallet_args = WalletArgs {
command: WalletAction::Convert(wallet_convert_args),
};
wallet(&wallet_args);
const ALICE_KEYSTORE_PATH_TEST: &str = "alice.json";
const ALICE_PEM_PATH_TEST: &str = "alice_test.pem";
const KEYSTORE_PASSWORD: &str = "abcd";

fn create_keystore_from_pem(file: &str) {
let pem_decoded_keys = Wallet::get_pem_decoded_content(file);
let (private_key_str, public_key_str) = Wallet::get_wallet_keys_pem(file);

let address = Wallet::from_private_key(&private_key_str)
.unwrap()
.address();
let hex_decoded_keys = hex::decode(pem_decoded_keys).unwrap();

let json_result = Wallet::encrypt_keystore(
hex_decoded_keys.as_slice(),
&address,
&public_key_str,
KEYSTORE_PASSWORD,
);

write_to_file(&json_result, ALICE_KEYSTORE_PATH_TEST);
}

fn write_to_file(content: &str, file: &str) {
let mut file = File::create(file).unwrap();
file.write_all(content.as_bytes()).unwrap();
}

fn create_keystore_file_from_scratch() -> Address {
let mnemonic = Wallet::generate_mnemonic();
let (private_key_str, public_key_str) = Wallet::get_wallet_keys_mnemonic(mnemonic.to_string());
let wallet = Wallet::from_private_key(&private_key_str).unwrap();
let address = wallet.address();

let concatenated_keys = format!("{}{}", private_key_str, public_key_str);
let hex_decoded_keys = hex::decode(concatenated_keys).unwrap();
let json_result = Wallet::encrypt_keystore(
hex_decoded_keys.as_slice(),
&address,
&public_key_str,
KEYSTORE_PASSWORD,
);
write_to_file(&json_result, ALICE_KEYSTORE_PATH_TEST);
address
}

// Insert "1234" as password for your keystore, followed by (CTRL+D) for Linux/Mac or (CTRL+Z) for Windows
#[test]
fn test_wallet_convert_pem_to_keystore() {
let keystore_password = "1234";
create_keystore_from_pem();
create_keystore_from_pem(ALICE_PEM_PATH);
let (private_key_pem, _public_key_pem) = Wallet::get_wallet_keys_pem(ALICE_PEM_PATH);
assert_eq!(
Wallet::get_private_key_from_keystore_secret(ALICE_KEYSTORE_PATH, keystore_password)
Wallet::get_private_key_from_keystore_secret(ALICE_KEYSTORE_PATH_TEST, KEYSTORE_PASSWORD)
.unwrap()
.to_string(),
private_key_pem
);
fs::remove_file(ALICE_KEYSTORE_PATH).unwrap();
fs::remove_file(ALICE_KEYSTORE_PATH_TEST).unwrap();
}

// Insert the same password twice, each followed by (CTRL+D) for Linux/Mac or (CTRL+Z) for Windows
#[test]
fn test_wallet_convert_keystore_to_pem() {
create_keystore_from_pem();
let wallet_convert_args = WalletConvertArgs {
infile: Some(ALICE_KEYSTORE_PATH.to_string()),
outfile: Some("alice_test.pem".to_string()),
from: "keystore-secret".to_string(),
to: "pem".to_string(),
};
let wallet_args = WalletArgs {
command: WalletAction::Convert(wallet_convert_args),
};
wallet(&wallet_args);
let address = create_keystore_file_from_scratch();

let private_key =
Wallet::get_private_key_from_keystore_secret(ALICE_KEYSTORE_PATH_TEST, KEYSTORE_PASSWORD)
.unwrap();
let private_key_str = private_key.to_string();
let public_key = PublicKey::from(&private_key);
let public_key_str = public_key.to_string();

let pem_content = generate_pem_content(&address, &private_key_str, &public_key_str);
write_to_file(&pem_content, ALICE_PEM_PATH_TEST);

assert_eq!(
Wallet::get_pem_decoded_content(ALICE_PEM_PATH),
Wallet::get_pem_decoded_content("alice_test.pem")
Wallet::get_private_key_from_keystore_secret(ALICE_KEYSTORE_PATH_TEST, KEYSTORE_PASSWORD)
.unwrap()
.to_string(),
Wallet::get_wallet_keys_pem(ALICE_PEM_PATH_TEST).0
);
fs::remove_file("alice_test.pem").unwrap();
fs::remove_file(ALICE_KEYSTORE_PATH).unwrap();

fs::remove_file(ALICE_PEM_PATH_TEST).unwrap();
fs::remove_file(ALICE_KEYSTORE_PATH_TEST).unwrap();
}

0 comments on commit 8654092

Please sign in to comment.