|
| 1 | +use std::{fs, str::FromStr}; |
| 2 | + |
| 3 | +use async_trait::async_trait; |
| 4 | +use cln_rpc::{ |
| 5 | + model::{ |
| 6 | + requests::InvoiceRequest, |
| 7 | + responses::{InvoiceResponse, ListinvoicesInvoicesStatus, ListinvoicesResponse}, |
| 8 | + }, |
| 9 | + primitives::{Amount, AmountOrAny}, |
| 10 | +}; |
| 11 | +use config::ConfigError; |
| 12 | +use http::{header::CONTENT_TYPE, HeaderValue, Uri}; |
| 13 | +use hyper::{client::HttpConnector, Client}; |
| 14 | +use hyper_rustls::HttpsConnector; |
| 15 | +use nostr::Keys; |
| 16 | +use rand::random; |
| 17 | + |
| 18 | +use crate::{ |
| 19 | + config::Settings, |
| 20 | + error::{Error, Result}, |
| 21 | +}; |
| 22 | + |
| 23 | +use super::{InvoiceInfo, InvoiceStatus, PaymentProcessor}; |
| 24 | + |
| 25 | +#[derive(Clone)] |
| 26 | +pub struct ClnRestPaymentProcessor { |
| 27 | + client: hyper::Client<HttpsConnector<HttpConnector>, hyper::Body>, |
| 28 | + settings: Settings, |
| 29 | + rune_header: HeaderValue, |
| 30 | +} |
| 31 | + |
| 32 | +impl ClnRestPaymentProcessor { |
| 33 | + pub fn new(settings: &Settings) -> Result<Self> { |
| 34 | + let rune_path = settings |
| 35 | + .pay_to_relay |
| 36 | + .rune_path |
| 37 | + .clone() |
| 38 | + .ok_or(ConfigError::NotFound("rune_path".to_string()))?; |
| 39 | + let rune = String::from_utf8(fs::read(rune_path)?) |
| 40 | + .map_err(|_| ConfigError::Message("Rune should be UTF8".to_string()))?; |
| 41 | + let mut rune_header = HeaderValue::from_str(&rune.trim()) |
| 42 | + .map_err(|_| ConfigError::Message("Invalid Rune header".to_string()))?; |
| 43 | + rune_header.set_sensitive(true); |
| 44 | + |
| 45 | + let https = hyper_rustls::HttpsConnectorBuilder::new() |
| 46 | + .with_native_roots() |
| 47 | + .https_only() |
| 48 | + .enable_http1() |
| 49 | + .build(); |
| 50 | + let client = Client::builder().build::<_, hyper::Body>(https); |
| 51 | + |
| 52 | + Ok(Self { |
| 53 | + client, |
| 54 | + settings: settings.clone(), |
| 55 | + rune_header, |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#[async_trait] |
| 61 | +impl PaymentProcessor for ClnRestPaymentProcessor { |
| 62 | + async fn get_invoice(&self, key: &Keys, amount: u64) -> Result<InvoiceInfo, Error> { |
| 63 | + let random_number: u16 = random(); |
| 64 | + let memo = format!("{}: {}", random_number, key.public_key()); |
| 65 | + |
| 66 | + let body = InvoiceRequest { |
| 67 | + cltv: None, |
| 68 | + deschashonly: None, |
| 69 | + expiry: None, |
| 70 | + preimage: None, |
| 71 | + exposeprivatechannels: None, |
| 72 | + fallbacks: None, |
| 73 | + amount_msat: AmountOrAny::Amount(Amount::from_sat(amount)), |
| 74 | + description: memo.clone(), |
| 75 | + label: "Nostr".to_string(), |
| 76 | + }; |
| 77 | + let uri = Uri::from_str(&format!( |
| 78 | + "{}/v1/invoice", |
| 79 | + &self.settings.pay_to_relay.node_url |
| 80 | + )) |
| 81 | + .map_err(|_| ConfigError::Message("Bad node URL".to_string()))?; |
| 82 | + |
| 83 | + let req = hyper::Request::builder() |
| 84 | + .method(hyper::Method::POST) |
| 85 | + .uri(uri) |
| 86 | + .header(CONTENT_TYPE, HeaderValue::from_static("application/json")) |
| 87 | + .header("Rune", self.rune_header.clone()) |
| 88 | + .body(hyper::Body::from(serde_json::to_string(&body)?)) |
| 89 | + .expect("request builder"); |
| 90 | + |
| 91 | + let res = self.client.request(req).await?; |
| 92 | + |
| 93 | + let body = hyper::body::to_bytes(res.into_body()).await?; |
| 94 | + let invoice_response: InvoiceResponse = serde_json::from_slice(&body)?; |
| 95 | + |
| 96 | + Ok(InvoiceInfo { |
| 97 | + pubkey: key.public_key().to_string(), |
| 98 | + payment_hash: invoice_response.payment_hash.to_string(), |
| 99 | + bolt11: invoice_response.bolt11, |
| 100 | + amount, |
| 101 | + memo, |
| 102 | + status: InvoiceStatus::Unpaid, |
| 103 | + confirmed_at: None, |
| 104 | + }) |
| 105 | + } |
| 106 | + |
| 107 | + async fn check_invoice(&self, payment_hash: &str) -> Result<InvoiceStatus, Error> { |
| 108 | + let uri = Uri::from_str(&format!( |
| 109 | + "{}/v1/listinvoices?payment_hash={}", |
| 110 | + &self.settings.pay_to_relay.node_url, payment_hash |
| 111 | + )) |
| 112 | + .map_err(|_| ConfigError::Message("Bad node URL".to_string()))?; |
| 113 | + |
| 114 | + let req = hyper::Request::builder() |
| 115 | + .method(hyper::Method::POST) |
| 116 | + .uri(uri) |
| 117 | + .header(CONTENT_TYPE, HeaderValue::from_static("application/json")) |
| 118 | + .header("Rune", self.rune_header.clone()) |
| 119 | + .body(hyper::Body::empty()) |
| 120 | + .expect("request builder"); |
| 121 | + |
| 122 | + let res = self.client.request(req).await?; |
| 123 | + |
| 124 | + let body = hyper::body::to_bytes(res.into_body()).await?; |
| 125 | + let invoice_response: ListinvoicesResponse = serde_json::from_slice(&body)?; |
| 126 | + let invoice = invoice_response |
| 127 | + .invoices |
| 128 | + .first() |
| 129 | + .ok_or(Error::CustomError("Invoice not found".to_string()))?; |
| 130 | + let status = match invoice.status { |
| 131 | + ListinvoicesInvoicesStatus::PAID => InvoiceStatus::Paid, |
| 132 | + ListinvoicesInvoicesStatus::UNPAID => InvoiceStatus::Unpaid, |
| 133 | + ListinvoicesInvoicesStatus::EXPIRED => InvoiceStatus::Expired, |
| 134 | + }; |
| 135 | + Ok(status) |
| 136 | + } |
| 137 | +} |
0 commit comments