Files
api/lnvps_api/src/lightning/mod.rs
Kieran 9296e571ec
All checks were successful
continuous-integration/drone/push Build is passing
refactor: convert to workspace
2025-04-02 13:18:18 +01:00

64 lines
1.7 KiB
Rust

use crate::settings::{LightningConfig, Settings};
use anyhow::Result;
use futures::Stream;
use lnvps_db::async_trait;
use std::pin::Pin;
use std::sync::Arc;
#[cfg(feature = "bitvora")]
mod bitvora;
#[cfg(feature = "lnd")]
mod lnd;
/// Generic lightning node for creating payments
#[async_trait]
pub trait LightningNode: Send + Sync {
async fn add_invoice(&self, req: AddInvoiceRequest) -> Result<AddInvoiceResult>;
async fn subscribe_invoices(
&self,
from_payment_hash: Option<Vec<u8>>,
) -> Result<Pin<Box<dyn Stream<Item = InvoiceUpdate> + Send>>>;
}
#[derive(Debug, Clone)]
pub struct AddInvoiceRequest {
pub amount: u64,
pub memo: Option<String>,
pub expire: Option<u32>,
}
#[derive(Debug, Clone)]
pub struct AddInvoiceResult {
pub pr: String,
pub payment_hash: String,
pub external_id: Option<String>,
}
#[derive(Debug, Clone)]
pub enum InvoiceUpdate {
/// Internal impl created an update which we don't support or care about
Unknown,
Error(String),
Settled {
payment_hash: Option<String>,
external_id: Option<String>,
},
}
pub async fn get_node(settings: &Settings) -> Result<Arc<dyn LightningNode>> {
match &settings.lightning {
#[cfg(feature = "lnd")]
LightningConfig::LND {
url,
cert,
macaroon,
} => Ok(Arc::new(lnd::LndNode::new(url, cert, macaroon).await?)),
#[cfg(feature = "bitvora")]
LightningConfig::Bitvora {
token,
webhook_secret,
} => Ok(Arc::new(bitvora::BitvoraNode::new(token, webhook_secret))),
_ => anyhow::bail!("Unsupported lightning config!"),
}
}