Skip to content

Commit

Permalink
make port configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
notV4l committed Sep 23, 2024
1 parent ae732d2 commit 6e440d8
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use axum::{
Json, Router,
};
use clap::Parser;
use num::{BigInt, Num};
use num::{BigInt, BigUint, Num};
use oracle::*;
use serde::{Deserialize, Serialize};
use stark_vrf::{generate_public_key, BaseField, StarkVRF};
Expand All @@ -20,7 +20,11 @@ use tracing::debug;
struct Args {
/// Secret key
#[arg(short, long, required = true)]
secret_key: u64,
secret_key: String,

/// Port
#[arg(short, long, default_value = "3003")]
port: u16,
}

fn format<T: std::fmt::Display>(v: T) -> String {
Expand All @@ -34,14 +38,21 @@ struct InfoResult {
public_key_y: String,
}

fn get_secret_key() -> String {
fn get_secret_key() -> BigUint {
let args = Args::parse();
args.secret_key.to_string()
BigUint::from_str_radix(
&args
.secret_key
.trim_start_matches("0x")
.trim_start_matches("0X"),
16,
)
.expect("unable to parse secret_key")
}

async fn vrf_info() -> Json<InfoResult> {
let secret_key = get_secret_key();
let public_key = generate_public_key(secret_key.parse().unwrap());
let public_key = generate_public_key(secret_key.into());

Json(InfoResult {
public_key_x: format(public_key.x),
Expand All @@ -57,7 +68,7 @@ struct JsonResult {
async fn stark_vrf(extract::Json(payload): extract::Json<StarkVrfRequest>) -> Json<JsonResult> {
debug!("received payload {payload:?}");
let secret_key = get_secret_key();
let public_key = generate_public_key(secret_key.parse().unwrap());
let public_key = generate_public_key(secret_key.clone().into());

println!("public key {public_key}");

Expand All @@ -72,9 +83,7 @@ async fn stark_vrf(extract::Json(payload): extract::Json<StarkVrfRequest>) -> Js
.collect();

let ecvrf = StarkVRF::new(public_key).unwrap();
let proof = ecvrf
.prove(&secret_key.parse().unwrap(), seed.as_slice())
.unwrap();
let proof = ecvrf.prove(&secret_key.into(), seed.as_slice()).unwrap();
let sqrt_ratio_hint = ecvrf.hash_to_sqrt_ratio_hint(seed.as_slice());
let rnd = ecvrf.proof_to_hash(&proof).unwrap();

Expand All @@ -100,7 +109,7 @@ async fn stark_vrf(extract::Json(payload): extract::Json<StarkVrfRequest>) -> Js

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

tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
Expand All @@ -116,11 +125,11 @@ async fn main() {
.route("/stark_vrf", post(stark_vrf))
.layer(TraceLayer::new_for_http());

let listener = tokio::net::TcpListener::bind("0.0.0.0:3000")
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", &args.port))
.await
.expect("Failed to bind to port 3000, port already in use by another process. Change the port or terminate the other process.");
.expect(&format!("Failed to bind to port {}, port already in use by another process. Change the port or terminate the other process.", &args.port));

debug!("Server started on http://0.0.0.0:3000");
debug!("Server started on http://0.0.0.0:{}", &args.port);

axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal())
Expand Down

0 comments on commit 6e440d8

Please sign in to comment.