Skip to content

Commit 1442040

Browse files
committed
feat: load config from env
1 parent a97d885 commit 1442040

File tree

6 files changed

+92
-29
lines changed

6 files changed

+92
-29
lines changed

examples/circom/.env.devnet

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ETH_RPC_URL=http://localhost:8545
2+
PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
3+
# Deploy the contract with make deploy_contract_devnet
4+
FIBONACCI_CONTRACT_ADDRESS=<FIBONACCI_CONTRACT_ADDRESS>
5+
NETWORK=devnet

examples/circom/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ serde_json = "1.0.117"
99
aligned-sdk = { path = "../../crates/sdk" }
1010
tokio = "1.44"
1111
alloy = { version = "0.14", features = ["full", "signer-keystore", "providers"] }
12+
dotenv = "0.15.0"
13+
14+
[[bin]]
15+
name = "main"
16+
path = "./src/main.rs"
17+
18+
[[bin]]
19+
name = "get_vk_commitment"
20+
path = "./src/get_vk_commitment.rs"

examples/circom/src/aligned.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::str::FromStr;
2+
13
use aligned_sdk::{
24
common::types::{AlignedVerificationData, Signer, VerificationData, Wallet},
35
verification_layer::{estimate_fee, get_chain_id},
@@ -14,12 +16,9 @@ pub async fn submit_proof_to_aligned(
1416
let chain_id = get_chain_id(&config.eth_rpc_url)
1517
.await
1618
.expect("To query chain id from rpc");
17-
let wallet = Wallet::decrypt_keystore(
18-
&config.private_key_store_path,
19-
&config.private_key_store_password,
20-
)
21-
.expect("Keystore to be `cast wallet` compliant")
22-
.with_chain_id(chain_id);
19+
let wallet = Wallet::from_str(&config.private_key)
20+
.expect("Keystore to be `cast wallet` compliant")
21+
.with_chain_id(chain_id);
2322

2423
let verification_data = VerificationData {
2524
proof_generator_addr: wallet.address(),

examples/circom/src/config.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,45 @@
1+
use std::env;
2+
13
use aligned_sdk::common::types::Network;
24

35
#[derive(Clone)]
46
pub struct EnvConfig {
57
pub eth_rpc_url: String,
6-
pub private_key_store_path: String,
7-
pub private_key_store_password: String,
8+
pub private_key: String,
89
pub fibonacci_contract_address: String,
910
pub network: Network,
1011
}
12+
13+
impl EnvConfig {
14+
pub fn new(env_file: Option<String>) -> Self {
15+
if let Some(env_file) = env_file {
16+
dotenv::from_filename(env_file).expect("env file to exist");
17+
} else {
18+
dotenv::dotenv().ok();
19+
}
20+
21+
let eth_rpc_url = env::var("ETH_RPC_URL").expect("ETH_RPC_URL not set");
22+
let private_key = env::var("PRIVATE_KEY").expect("PRIVATE_KEY not set");
23+
let fibonacci_contract_address =
24+
env::var("FIBONACCI_CONTRACT_ADDRESS").expect("FIBONACCI_CONTRACT_ADDRESS not set");
25+
26+
let network = match env::var("NETWORK")
27+
.expect("NETWORK not set")
28+
.to_lowercase()
29+
.as_str()
30+
{
31+
"devnet" => Network::Devnet,
32+
"holesky-stage" => Network::HoleskyStage,
33+
"holesky" => Network::Holesky,
34+
"mainnet" => Network::Mainnet,
35+
_ => panic!("Unsupported NETWORK value"),
36+
};
37+
38+
EnvConfig {
39+
eth_rpc_url,
40+
private_key,
41+
fibonacci_contract_address,
42+
network,
43+
}
44+
}
45+
}

examples/circom/src/eth.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ pub async fn update_number_on_contract(
2323
aligned_verification_data: AlignedVerificationData,
2424
) -> String {
2525
let rpc_url = config.eth_rpc_url.parse().expect("RPC URL should be valid");
26-
let signer = LocalSigner::decrypt_keystore(
27-
&config.private_key_store_path,
28-
&config.private_key_store_password,
29-
)
30-
.expect("Keystore signer should be `cast wallet` compliant");
26+
let signer = LocalSigner::from_str(&config.private_key)
27+
.expect("Keystore signer should be `cast wallet` compliant");
3128
let wallet = EthereumWallet::from(signer);
3229

3330
let rpc_provider = ProviderBuilder::new()

examples/circom/src/main.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,61 @@
1-
use std::process::Command;
1+
use std::{env, process::Command};
22

33
use circom_example::{aligned, config::EnvConfig, eth};
44

55
#[tokio::main]
66
async fn main() {
7-
// Adjust the path to your script if it's not in the same directory
7+
println!("===============================");
8+
println!("Starting proof generation...");
9+
println!("===============================");
10+
11+
// Run proof generation script
812
let status = Command::new("bash")
913
.arg("./circuits/generate_proof.sh")
1014
.status()
1115
.expect("failed to execute process");
1216

1317
if status.success() {
14-
println!("Script executed successfully!");
18+
println!("Proof generation script executed successfully");
1519
} else {
1620
println!("Script failed with status: {:?}", status.code());
21+
return;
1722
}
1823

19-
let config: EnvConfig = EnvConfig {
20-
eth_rpc_url: "http://localhost:8545".to_string(),
21-
private_key_store_path: "devnet_keystore.json".to_string(),
22-
private_key_store_password: "".to_string(),
23-
fibonacci_contract_address: "".to_string(),
24-
network: aligned_sdk::common::types::Network::Devnet,
24+
// Handle custom .env file
25+
let args: Vec<String> = env::args().collect();
26+
let custom_env = if args.len() > 1 {
27+
let env_file = args[1].to_string();
28+
println!("Using custom .env file: {env_file}");
29+
Some(env_file)
30+
} else {
31+
println!("Using default .env file (if present)");
32+
None
2533
};
26-
let proof = std::fs::read("circuits/proof.json").expect("proof to be created");
27-
let vk =
28-
std::fs::read("circuits/verification_key.json").expect("verification key to be created");
34+
35+
let config: EnvConfig = EnvConfig::new(custom_env);
36+
37+
// Load circuit artifacts
38+
println!("-------------------------------");
39+
println!("Loading circuit artifacts...");
40+
println!("-------------------------------");
41+
42+
let proof = std::fs::read("circuits/proof.json").expect("proof.json should exist");
43+
let vk = std::fs::read("circuits/verification_key.json")
44+
.expect("verification_key.json should exist");
2945
let public_inputs_file =
30-
std::fs::read("circuits/public.json").expect("public inputs to be created");
46+
std::fs::read("circuits/public.json").expect("public.json should exist");
3147
let pub_inputs: Vec<String> =
32-
serde_json::from_slice(&public_inputs_file).expect("parse inputs json");
48+
serde_json::from_slice(&public_inputs_file).expect("could not parse inputs json");
3349
let decoded_inputs = aligned_sdk::common::utils::encode_circom_pub_inputs(&pub_inputs)
34-
.expect("Inputs to be decoded");
50+
.expect("inputs should be decoded");
3551

52+
println!("Submitting proof to Aligned...");
3653
let aligned_verification_data =
3754
aligned::submit_proof_to_aligned(config.clone(), proof, vk, decoded_inputs.clone()).await;
3855

56+
println!("Updating on-chain contract with proof result...");
3957
let receipt =
4058
eth::update_number_on_contract(config, decoded_inputs, aligned_verification_data).await;
4159

42-
println!("RECEIPT HASH {}", receipt);
60+
println!("Done. Transaction receipt hash: {}", receipt);
4361
}

0 commit comments

Comments
 (0)