Skip to content

Commit

Permalink
Add generate batch
Browse files Browse the repository at this point in the history
  • Loading branch information
octavonce committed Oct 15, 2022
1 parent ae9b6f4 commit 72bc056
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "purplecoincli"
version = "0.0.1"
version = "0.0.2"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "Purplecoin CLI tools"
Expand Down
30 changes: 25 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ struct Cli {
enum Commands {
/// Generate a simple encrypted wallet and print it to the screen
GenSimpleWallet,
GenSimpleWalletBatch { batch_size: u64 },
}


fn main() {
let cli = Cli::parse();

// You can check for the existence of subcommands, and if found use their
// matches just as you would the top level cmd
match &cli.command {
Some(Commands::GenSimpleWallet) => {
let mut password = rpassword::prompt_password("Your password: ").unwrap();
Expand All @@ -38,11 +37,32 @@ fn main() {
return;
}

let (address, encrypted_key) = gen_encrypted_simple_wallet(&password);
let addresses = gen_encrypted_simple_wallet(&password, 1);
password.zeroize();
confirm_password.zeroize();
println!("Your address is: {}", address);
println!("Your encrypted key is: {}", encrypted_key);
println!("Your address is: {}", addresses[0].0);
println!("Your encrypted key is: {}", addresses[0].1);
}
Some(Commands::GenSimpleWalletBatch { batch_size }) => {
if batch_size > &100_000 {
println!("Max batch size is 100,000!");
return;
}

let mut password = rpassword::prompt_password("Your password: ").unwrap();
let mut confirm_password = rpassword::prompt_password("Your password confirmation: ").unwrap();

if password != confirm_password {
println!("Password does not match password confirmation!");
return;
}

let batch = gen_encrypted_simple_wallet(&password, *batch_size);
password.zeroize();
confirm_password.zeroize();
for (address, encrypted_key) in batch.iter() {
println!("{} {}", address, encrypted_key);
}
}
None => {
println!("No command given. Type \"purplecoincli help\" for a list of commands.")
Expand Down
25 changes: 17 additions & 8 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ use zeroize::Zeroize;

const SEED_BYTES: usize = 64;

/// Generates a simple wallet consisting of an Address and encrypted private key.
/// Generates simple wallets consisting of an Address and encrypted private key.
///
/// Returns them as a tuple of two strings.
pub fn gen_encrypted_simple_wallet(passphrase: &str) -> (String, String) {
pub fn gen_encrypted_simple_wallet(passphrase: &str, batch_size: u64) -> Vec<(String, String)> {
// Generate random 64 byte seed
let mut rng = rand::thread_rng();
let mut seed: [u8; SEED_BYTES] = [0; SEED_BYTES];
Expand Down Expand Up @@ -84,9 +84,19 @@ pub fn gen_encrypted_simple_wallet(passphrase: &str) -> (String, String) {
0x000000,
);

let mut next_internal = master_keypair_internal.derive_next();
let address = next_internal.pub_key().to_address().to_bech32("pu");
let encrypted_key = hex::encode(next_internal.secret_key.secret_key);
let batch = (0..batch_size)
.into_iter()
.map(|_| {
let mut next_internal = master_keypair_internal.derive_next();
master_keypair_internal = next_internal.clone();
let address = next_internal.pub_key().to_address().to_bech32("pu");
let encrypted_key = hex::encode(next_internal.secret_key.secret_key);

next_internal.zeroize();
(address, encrypted_key)
}).collect();



seed.zeroize();
encryption_key.zeroize();
Expand All @@ -96,9 +106,8 @@ pub fn gen_encrypted_simple_wallet(passphrase: &str) -> (String, String) {
passphrase_hash256.zeroize();
master_priv_hash512.zeroize();
master_keypair_internal.zeroize();
next_internal.zeroize();

(address, encrypted_key)
batch

}

Expand Down Expand Up @@ -195,7 +204,7 @@ pub struct XPriv {
secret_key: [u8; 64],
}

#[derive(Encode, Decode, Zeroize, Debug)]
#[derive(Encode, Decode, Zeroize, Debug, Clone)]
pub struct XKeypair {
pub pub_key: XPub,
pub secret_key: XPriv,
Expand Down

0 comments on commit 72bc056

Please sign in to comment.