Skip to content

Commit

Permalink
feat: add tnc to token
Browse files Browse the repository at this point in the history
  • Loading branch information
DhananjayPurohit committed Aug 28, 2024
1 parent d858cc7 commit 8e6138b
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 2 deletions.
3 changes: 2 additions & 1 deletion server/migrations/0001_key_data_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ CREATE TABLE public.tokens (
onchain_address varchar NULL,
processor_id varchar NULL,
confirmed boolean DEFAULT false,
spent boolean DEFAULT false
spent boolean DEFAULT false,
accepted boolean DEFAULT false
);
27 changes: 27 additions & 0 deletions token-server/src/endpoints/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,33 @@ pub async fn token_init(token_server: &State<TokenServer>) -> status::Custom<Jso
return status::Custom(Status::Ok, Json(response_body));
}

#[get("/token/token_gen")]
pub async fn token_gen(token_server: &State<TokenServer>) -> status::Custom<Json<Value>> {

let token_id = uuid::Uuid::new_v4().to_string();

let invoice: Invoice = get_lightning_invoice(token_server, token_id.clone()).await;

let pod_info = PODInfo {
token_id: token_id.clone(),
fee: token_server.config.fee.clone(),
lightning_invoice: invoice.pr.clone(),
btc_payment_address: invoice.onChainAddr.clone(),
processor_id: invoice.id.clone(),
};

insert_new_token(&token_server.pool, &token_id, &invoice.pr.clone(), &invoice.onChainAddr, &invoice.id).await;

let config = crate::server_config::ServerConfig::load();

let response_body = json!({
"pod_info": pod_info,
"tnc": config.tnc.clone(),
});

return status::Custom(Status::Ok, Json(response_body));
}

#[get("/token/token_verify/<token_id>")]
pub async fn token_verify(token_server: &State<TokenServer>, token_id: String) -> status::Custom<Json<Value>> {

Expand Down
1 change: 1 addition & 0 deletions token-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ async fn main() {
.mount("/", routes![
endpoints::token::token_init,
endpoints::token::token_verify,
endpoints::token::token_gen,
])
.register("/", catchers![
not_found,
Expand Down
7 changes: 6 additions & 1 deletion token-server/src/server_config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use config::{Config as ConfigRs, Environment, File};
use serde::{Serialize, Deserialize};
use std::env;
use std::{env, fs};

/// Config struct storing all StataChain Entity config
#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -17,6 +17,8 @@ pub struct ServerConfig {
pub delay: u64,
/// Postgres connection string
pub connection_string: String,
/// Tnc string
pub tnc: String,
}

impl Default for ServerConfig {
Expand All @@ -28,6 +30,7 @@ impl Default for ServerConfig {
fee: String::from("10000"),
delay: 3600,
connection_string: String::from("postgresql://postgres:postgres@localhost/mercury"),
tnc: fs::read_to_string("../tnc.html").unwrap_or_else(|_| String::from("")),
}
}
}
Expand All @@ -41,6 +44,7 @@ impl From<ConfigRs> for ServerConfig {
fee: config.get::<String>("fee").unwrap_or_else(|_| String::new()),
delay: config.get::<u64>("delay").unwrap_or(0),
connection_string: config.get::<String>("connection_string").unwrap_or_else(|_| String::new()),
tnc: fs::read_to_string("../tnc.html").unwrap_or_else(|_| String::from("")),
}
}
}
Expand Down Expand Up @@ -73,6 +77,7 @@ impl ServerConfig {
fee: get_env_or_config("fee", "FEE"),
delay: get_env_or_config("delay", "DELAY").parse::<u64>().unwrap(),
connection_string: get_env_or_config("connection_string", "CONNECTION_STRING"),
tnc: fs::read_to_string("../tnc.html").unwrap_or_else(|_| String::from("")),
}
}
}
Expand Down
Loading

0 comments on commit 8e6138b

Please sign in to comment.