diff --git a/token-server/src/endpoints/token.rs b/token-server/src/endpoints/token.rs index 03d6c0de..89fcc99f 100644 --- a/token-server/src/endpoints/token.rs +++ b/token-server/src/endpoints/token.rs @@ -7,6 +7,46 @@ use sqlx::Row; use crate::server::TokenServer; +#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)] +pub struct InvoiceNode{ + pub payment_hash: String, + pub expires_at: u64, + pub bolt11: String +} + +#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)] +pub struct ReqInvoiceNode{ + pub amount: u64, + pub label: String, + pub description: String +} + +#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)] +pub struct RTLInvoiceNode{ + pub payment_hash: String, + pub expires_at: u64, + pub bolt11: String, + pub payment_secret: String, + pub warning_capacity: String +} + +#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)] +pub struct RTLDataNode{ + pub label: String, + pub bolt11: String, + pub payment_hash: String, + pub msatoshi: u64, + pub amount_msat: String, + pub status: String, + pub description: String, + pub expires_at: u64 +} + +#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)] +pub struct RTLQueryNode{ + pub invoices: Vec, +} + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub struct Invoice{ pub id: String, @@ -136,7 +176,14 @@ pub async fn token_gen(token_server: &State) -> status::Custom) -> status::Custom, token_id: return invoice; } -pub async fn query_lightning_payment(token_server: &State, processor_id: &String) -> u64 { +pub async fn get_lightning_invoice_node(token_server: &State, token_id: String) -> Invoice { + + let config = crate::server_config::ServerConfig::load(); + + let id_str = &token_id.to_string(); + + let cln_url: Url = config.lightningd.clone(); + let macaroon = config.lightningd.macaroon; + let path: &str = "v1/invoice/genInvoice"; + let inv_request = ReqInvoiceNode { + amount: *value, + label: token_id.to_string(), + description: "POD".to_string(), + }; + + let client: reqwest::Client = reqwest::Client::new(); + let request = client.post(&format!("{}/{}", cln_url, path)); + + let value = request.header("macaroon", macaroon).header("encodingtype","hex").json(&inv_request).send().await.unwrap().text().await.unwrap(); + + let ret_invoice: RTLInvoiceNode = serde_json::from_str(value.as_str()).expect(&format!("failed to parse: {}", value.as_str())); + + let invoice = Invoice { + id: "null".to_string(), + pr: ret_invoice.bolt11, + checkoutUrl: "null".to_string(), + onChainAddr: "".to_string(), + }; + return invoice; +} + + + +pub async fn query_lightning_payment(token_server: &State, token_id: &String) -> u64 { + + let processor_url = token_server.config.processor_url.clone(); + let api_key = token_server.config.api_key.clone(); + let path: String = "checkout/".to_string() + processor_id; + + let client: reqwest::Client = reqwest::Client::new(); + let request = client.get(&format!("{}/{}", processor_url, path)); + + let value = request.header("Api-Key", api_key).header("encodingtype","hex").send().await.unwrap().text().await.unwrap(); + + let ret_invoice: RTLQuery = serde_json::from_str(value.as_str()).expect(&format!("failed to parse: {}", value.as_str())); + + if ret_invoice.isPaid { + return 0; + } else { + return ret_invoice.createdAt + ret_invoice.delay; + } +} + +pub async fn query_lightning_payment_node(token_server: &State, processor_id: &String) -> u64 { let processor_url = token_server.config.processor_url.clone(); let api_key = token_server.config.api_key.clone(); diff --git a/token-server/src/server_config.rs b/token-server/src/server_config.rs index 6f118dfa..87c23a19 100644 --- a/token-server/src/server_config.rs +++ b/token-server/src/server_config.rs @@ -28,6 +28,12 @@ pub struct ServerConfig { pub db_port: u16, /// Database name pub db_name: String, + /// bitcoind node URL + pub bitcoind: String, + /// lighting node URL + pub lightningd: String, + /// ln_macaroon + pub lnmacaroon: String, } impl Default for ServerConfig { @@ -44,6 +50,9 @@ impl Default for ServerConfig { db_host: String::from("db_server"), db_port: 5432, db_name: String::from("mercury"), + bitcoind: String::from(""), + lightningd: String::from(""), + lnmacaroon: String::from(""), } } } @@ -62,6 +71,9 @@ impl From for ServerConfig { db_host: config.get::("db_host").unwrap_or_else(|_| String::new()), db_port: config.get::("db_port").unwrap_or(0), db_name: config.get::("db_name").unwrap_or_else(|_| String::new()), + bitcoind: config.get::("bitcoind").unwrap_or_else(|_| String::new()), + lightningd: config.get::("lightningd").unwrap_or_else(|_| String::new()), + lnmacaroon: config.get::("lnmacaroon").unwrap_or_else(|_| String::new()), } } } @@ -99,6 +111,9 @@ impl ServerConfig { db_host: get_env_or_config("db_host", "DB_HOST"), db_port: get_env_or_config("db_port", "DB_PORT").parse::().unwrap(), db_name: get_env_or_config("db_name", "DB_NAME"), + bitcoind: get_env_or_config("bitcoind", "BITCOIND"), + lightningd: get_env_or_config("lightningd", "LIGHTNINGD"), + lnmacaroon: get_env_or_config("lnmacaroon", "LNMACAROON"), } }