Skip to content

Commit

Permalink
Zeroize the cli structs
Browse files Browse the repository at this point in the history
  • Loading branch information
raychu86 committed Jan 9, 2024
1 parent 052457b commit 21a393f
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,9 @@ features = [ "env-filter" ]
[dependencies.ureq]
version = "2.9"

[dependencies.zeroize]
version = "1"
features = [ "derive" ]

[target."cfg(target_family = \"unix\")".dependencies.nix]
version = "0.26"
3 changes: 2 additions & 1 deletion cli/src/commands/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ use rand::SeedableRng;
use rand_chacha::ChaChaRng;
use rayon::prelude::*;
use std::io::{Read, Write};
use zeroize::Zeroize;

type Network = snarkvm::prelude::Testnet3;

/// Commands to manage Aleo accounts.
#[derive(Debug, Parser)]
#[derive(Debug, Parser, Zeroize)]
pub enum Account {
/// Generates a new Aleo account
New {
Expand Down
3 changes: 2 additions & 1 deletion cli/src/commands/developer/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ use snarkvm::{
use anyhow::{bail, Result};
use clap::Parser;
use std::str::FromStr;
use zeroize::Zeroize;

/// Decrypts a record ciphertext.
#[derive(Debug, Parser)]
#[derive(Debug, Parser, Zeroize)]
pub struct Decrypt {
/// The record ciphertext to decrypt.
#[clap(short, long)]
Expand Down
14 changes: 11 additions & 3 deletions cli/src/commands/developer/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use anyhow::{bail, Result};
use clap::Parser;
use colored::Colorize;
use std::str::FromStr;
use zeroize::Zeroize;

/// Deploys an Aleo program.
#[derive(Debug, Parser)]
Expand Down Expand Up @@ -63,6 +64,13 @@ pub struct Deploy {
store: Option<String>,
}

impl Drop for Deploy {
/// Zeroize the private key when the `Deploy` struct goes out of scope.
fn drop(&mut self) {
self.private_key.zeroize();
}
}

impl Deploy {
/// Deploys an Aleo program.
pub fn parse(self) -> Result<String> {
Expand All @@ -78,7 +86,7 @@ impl Deploy {
let private_key = PrivateKey::from_str(&self.private_key)?;

// Fetch the package from the directory.
let package = Developer::parse_package(self.program_id, self.path)?;
let package = Developer::parse_package(self.program_id, &self.path)?;

println!("📦 Creating deployment transaction for '{}'...\n", &self.program_id.to_string().bold());

Expand Down Expand Up @@ -133,9 +141,9 @@ impl Deploy {

// Determine if the transaction should be broadcast, stored, or displayed to the user.
Developer::handle_transaction(
self.broadcast,
&self.broadcast,
self.dry_run,
self.store,
&self.store,
transaction,
self.program_id.to_string(),
)
Expand Down
10 changes: 9 additions & 1 deletion cli/src/commands/developer/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use anyhow::{anyhow, bail, Result};
use clap::Parser;
use colored::Colorize;
use std::str::FromStr;
use zeroize::Zeroize;

/// Executes an Aleo program function.
#[derive(Debug, Parser)]
Expand Down Expand Up @@ -64,6 +65,13 @@ pub struct Execute {
store: Option<String>,
}

impl Drop for Execute {
/// Zeroize the private key when the `Execute` struct goes out of scope.
fn drop(&mut self) {
self.private_key.zeroize();
}
}

impl Execute {
/// Executes an Aleo program function with the provided inputs.
#[allow(clippy::format_in_format_args)]
Expand Down Expand Up @@ -143,7 +151,7 @@ impl Execute {
println!("✅ Created execution transaction for '{}'", locator.to_string().bold());

// Determine if the transaction should be broadcast, stored, or displayed to the user.
Developer::handle_transaction(self.broadcast, self.dry_run, self.store, transaction, locator.to_string())
Developer::handle_transaction(&self.broadcast, self.dry_run, &self.store, transaction, locator.to_string())
}
}

Expand Down
6 changes: 3 additions & 3 deletions cli/src/commands/developer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Developer {
}

/// Parse the package from the directory.
fn parse_package(program_id: ProgramID<CurrentNetwork>, path: Option<String>) -> Result<Package<CurrentNetwork>> {
fn parse_package(program_id: ProgramID<CurrentNetwork>, path: &Option<String>) -> Result<Package<CurrentNetwork>> {
// Instantiate a path to the directory containing the manifest file.
let directory = match path {
Some(path) => PathBuf::from_str(&path)?,
Expand Down Expand Up @@ -167,9 +167,9 @@ impl Developer {

/// Determine if the transaction should be broadcast or displayed to user.
fn handle_transaction(
broadcast: Option<String>,
broadcast: &Option<String>,
dry_run: bool,
store: Option<String>,
store: &Option<String>,
transaction: Transaction<CurrentNetwork>,
operation: String,
) -> Result<String> {
Expand Down
3 changes: 2 additions & 1 deletion cli/src/commands/developer/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ use std::{
str::FromStr,
sync::Arc,
};
use zeroize::Zeroize;

const MAX_BLOCK_RANGE: u32 = 50;
const CDN_ENDPOINT: &str = "https://s3.us-west-1.amazonaws.com/testnet3.blocks/phase3";

/// Scan the snarkOS node for records.
#[derive(Debug, Parser)]
#[derive(Debug, Parser, Zeroize)]
pub struct Scan {
/// An optional private key scan for unspent records.
#[clap(short, long)]
Expand Down
10 changes: 9 additions & 1 deletion cli/src/commands/developer/transfer_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use snarkvm::prelude::{
use anyhow::{bail, Result};
use clap::Parser;
use std::str::FromStr;
use zeroize::Zeroize;

/// Executes the `transfer_private` function in the `credits.aleo` program.
#[derive(Debug, Parser)]
Expand Down Expand Up @@ -63,6 +64,13 @@ pub struct TransferPrivate {
store: Option<String>,
}

impl Drop for TransferPrivate {
/// Zeroize the private key when the `TransferPrivate` struct goes out of scope.
fn drop(&mut self) {
self.private_key.zeroize();
}
}

impl TransferPrivate {
/// Creates an Aleo transfer with the provided inputs.
#[allow(clippy::format_in_format_args)]
Expand Down Expand Up @@ -116,6 +124,6 @@ impl TransferPrivate {
println!("✅ Created private transfer of {} microcredits to {}\n", &self.amount, self.recipient);

// Determine if the transaction should be broadcast, stored, or displayed to the user.
Developer::handle_transaction(self.broadcast, self.dry_run, self.store, transaction, locator.to_string())
Developer::handle_transaction(&self.broadcast, self.dry_run, &self.store, transaction, locator.to_string())
}
}

0 comments on commit 21a393f

Please sign in to comment.