Add zeroize support for private key (#27)

This commit is contained in:
Sydhds
2025-08-13 15:15:25 +02:00
committed by GitHub
parent dc9d7947b3
commit 11b06a440d
7 changed files with 18 additions and 6 deletions

2
Cargo.lock generated
View File

@@ -3937,6 +3937,7 @@ dependencies = [
"tracing-subscriber 0.3.19",
"tracing-test",
"url",
"zeroize",
"zerokit_utils",
]
@@ -4905,6 +4906,7 @@ dependencies = [
"thiserror 2.0.12",
"tokio",
"url",
"zeroize",
]
[[package]]

View File

@@ -18,6 +18,7 @@ alloy = { version = "1.0", features = ["getrandom", "sol-types", "contract", "pr
async-trait = "0.1"
derive_more = "2.0.1"
thiserror = "2.0"
zeroize = "1.8"
# dev
criterion = { version = "0.6", features = ["async_tokio"] }

View File

@@ -45,6 +45,7 @@ zerokit_utils = { git = "https://github.com/vacp2p/zerokit", package = "zerokit_
rln_proof = { path = "../rln_proof" }
smart_contract = { path = "../smart_contract" }
rayon = "1.7"
zeroize.workspace = true
[build-dependencies]
tonic-build = "*"

View File

@@ -21,6 +21,7 @@ use tonic_web::GrpcWebLayer;
use tower_http::cors::{Any, CorsLayer};
use tracing::{debug, error};
use url::Url;
use zeroize::Zeroizing;
// internal
use crate::error::{AppError, ProofGenerationStringError};
use crate::metrics::{
@@ -313,10 +314,10 @@ impl GrpcProverService {
panic!("Please provide karma_sc_info or use serve_with_mock");
};
let karma_rln_sc = if let Some(rln_sc_info) = self.rln_sc_info.as_ref() {
let private_key = std::env::var("PRIVATE_KEY").map_err(|_| {
let private_key = Zeroizing::new(std::env::var("PRIVATE_KEY").map_err(|_| {
error!("PRIVATE_KEY environment variable is not set");
AppError::RlnScError(RlnScError::EmptyPrivateKey)
})?;
})?);
KarmaRLNSCInstance::try_new_with_signer(
rln_sc_info.0.clone(),
rln_sc_info.1,

View File

@@ -24,6 +24,7 @@ log = "0.4.27"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
clap = { version = "4.0", features = ["derive"] }
rustls = "0.23.31"
zeroize.workspace = true
[dev-dependencies]
claims = "0.8"

View File

@@ -12,6 +12,7 @@ use alloy::{
use async_trait::async_trait;
use std::str::FromStr;
use url::Url;
use zeroize::Zeroizing;
// internal
use crate::common::AlloyWsProvider;
@@ -73,7 +74,7 @@ impl KarmaRLNSC::KarmaRLNSCInstance<AlloyWsProvider> {
pub async fn try_new_with_signer(
rpc_url: Url,
address: Address,
private_key: String,
private_key: Zeroizing<String>,
) -> Result<KarmaRLNSC::KarmaRLNSCInstance<impl alloy::providers::Provider>, RlnScError> {
if private_key.is_empty() {
return Err(RlnScError::EmptyPrivateKey);

View File

@@ -1,12 +1,16 @@
// std
use std::str::FromStr;
// third-party
use alloy::{
hex,
primitives::{Address, U256},
};
use clap::Parser;
use rustls::crypto::aws_lc_rs;
use smart_contract::{KarmaRLNSC, RlnScError};
use std::str::FromStr;
use url::Url;
use zeroize::Zeroizing;
// internal
use smart_contract::{KarmaRLNSC, RlnScError};
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
@@ -20,6 +24,7 @@ struct Args {
contract_address: String,
/// Private key for signing transactions
/// Warning: this is a test key, do not use in production
#[arg(long, default_value = "")]
private_key: String,
@@ -61,7 +66,7 @@ async fn main() -> Result<(), RlnScError> {
// Connect to KarmaRLN contract with signer
let rln_contract =
KarmaRLNSC::KarmaRLNSCInstance::try_new_with_signer(url, contract_addr, args.private_key)
KarmaRLNSC::KarmaRLNSCInstance::try_new_with_signer(url, contract_addr, Zeroizing::new(args.private_key))
.await?;
println!("Successfully connected to RLN contract with signer at {contract_addr}",);