Skip to content

Commit

Permalink
Add CLI argument parsing with clap for secret key (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen authored Sep 11, 2024
1 parent 6bbe64d commit 1c8df31
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 3 deletions.
114 changes: 114 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ tracing-subscriber = "0.3.18"
prost = "0.12.3"
stark-vrf = { git = "https://github.com/dojoengine/stark-vrf.git" }
num = "0.4.3"
clap = { version = "4.5.17", features = ["derive"] }

[build-dependencies]
prost-build = "0.12.3"
20 changes: 17 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ use axum::{
routing::{get, post},
Json, Router,
};
use clap::Parser;
use num::{BigInt, Num};
use oracle::*;
use serde::{Deserialize, Serialize};
use stark_vrf::{generate_public_key, BaseField, ScalarValue, StarkVRF};
use stark_vrf::{generate_public_key, BaseField, StarkVRF};
use std::str::FromStr;
use tokio::signal;
use tower_http::trace::TraceLayer;
use tracing::debug;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Secret key as a u64
#[arg(short, long, required = true)]
secret_key: u64,
}

fn format<T: std::fmt::Display>(v: T) -> String {
let int = BigInt::from_str(&format!("{v}")).unwrap();
format!("0x{}", int.to_str_radix(16))
Expand All @@ -24,8 +33,10 @@ struct InfoResult {
public_key_x: String,
public_key_y: String,
}

async fn vrf_info() -> Json<InfoResult> {
let secret_key = ScalarValue!("190");
let args = Args::parse();
let secret_key = args.secret_key.to_string().parse().unwrap();
let public_key = generate_public_key(secret_key);

Json(InfoResult {
Expand All @@ -41,7 +52,8 @@ struct JsonResult {

async fn stark_vrf(extract::Json(payload): extract::Json<StarkVrfRequest>) -> Json<JsonResult> {
debug!("received payload {payload:?}");
let secret_key = ScalarValue!("190");
let args = Args::parse();
let secret_key = args.secret_key.to_string().parse().unwrap();
let public_key = generate_public_key(secret_key);

println!("public key {public_key}");
Expand Down Expand Up @@ -83,6 +95,8 @@ async fn stark_vrf(extract::Json(payload): extract::Json<StarkVrfRequest>) -> Js

#[tokio::main(flavor = "current_thread")]
async fn main() {
Args::parse();

tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.init();
Expand Down

0 comments on commit 1c8df31

Please sign in to comment.