-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f16dd3
commit 8654092
Showing
2 changed files
with
72 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |