Skip to content

Commit

Permalink
feat(mintd): phoenixd
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Aug 12, 2024
1 parent 545ad1e commit 95e1f2e
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 23 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cdk-rexie = { version = "0.3", path = "./crates/cdk-rexie", default-features = f
cdk-sqlite = { version = "0.3", path = "./crates/cdk-sqlite", default-features = false }
cdk-redb = { version = "0.3", path = "./crates/cdk-redb", default-features = false }
cdk-cln = { version = "0.3", path = "./crates/cdk-cln", default-features = false }
cdk-phoenixd = { version = "0.3", path = "./crates/cdk-phoenixd", default-features = false }
cdk-axum = { version = "0.3", path = "./crates/cdk-axum", default-features = false }
cdk-fake-wallet = { version = "0.3", path = "./crates/cdk-fake-wallet", default-features = false }
cdk-strike = { version = "0.3", path = "./crates/cdk-strike", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions crates/cdk-mintd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cdk = { workspace = true, default-features = false, features = ["mint"] }
cdk-redb = { workspace = true, default-features = false, features = ["mint"] }
cdk-sqlite = { workspace = true, default-features = false, features = ["mint"] }
cdk-cln = { workspace = true, default-features = false }
cdk-phoenixd = { workspace = true, default-features = false }
cdk-fake-wallet = { workspace = true, default-features = false }
cdk-strike.workspace = true
cdk-axum = { workspace = true, default-features = false }
Expand Down
4 changes: 4 additions & 0 deletions crates/cdk-mintd/example.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ ln_backend = "cln"
# api_key=""
# Optional default sats
# supported_units=[""]

# [phoenixd]
# api_password = ""
# api_url = ""
11 changes: 9 additions & 2 deletions crates/cdk-mintd/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ pub enum LnBackend {
Cln,
Strike,
FakeWallet,
// Greenlight,
// Ldk,
Phoenixd,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
Expand All @@ -45,6 +44,12 @@ pub struct Cln {
pub rpc_path: PathBuf,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Phoenixd {
pub api_password: String,
pub api_url: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FakeWallet {
pub supported_units: Vec<CurrencyUnit>,
Expand Down Expand Up @@ -78,6 +83,7 @@ pub struct Settings {
pub ln: Ln,
pub cln: Option<Cln>,
pub strike: Option<Strike>,
pub phoenixd: Option<Phoenixd>,
pub fake_wallet: Option<FakeWallet>,
pub database: Database,
}
Expand Down Expand Up @@ -143,6 +149,7 @@ impl Settings {
LnBackend::Cln => assert!(settings.cln.is_some()),
LnBackend::FakeWallet => (),
LnBackend::Strike => assert!(settings.strike.is_some()),
LnBackend::Phoenixd => assert!(settings.phoenixd.is_some()),
}

Ok(settings)
Expand Down
58 changes: 53 additions & 5 deletions crates/cdk-mintd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;

use anyhow::{anyhow, Result};
use anyhow::{anyhow, bail, Result};
use axum::Router;
use bip39::Mnemonic;
use cdk::cdk_database::{self, MintDatabase};
Expand All @@ -22,6 +22,7 @@ use cdk::nuts::{
use cdk_axum::LnKey;
use cdk_cln::Cln;
use cdk_fake_wallet::FakeWallet;
use cdk_phoenixd::Phoenixd;
use cdk_redb::MintRedbDatabase;
use cdk_sqlite::MintSqliteDatabase;
use cdk_strike::Strike;
Expand Down Expand Up @@ -82,8 +83,8 @@ async fn main() -> anyhow::Result<()> {

let mut contact_info: Option<Vec<ContactInfo>> = None;

if let Some(nostr_contact) = settings.mint_info.contact_nostr_public_key {
let nostr_contact = ContactInfo::new("nostr".to_string(), nostr_contact);
if let Some(nostr_contact) = &settings.mint_info.contact_nostr_public_key {
let nostr_contact = ContactInfo::new("nostr".to_string(), nostr_contact.to_string());

contact_info = match contact_info {
Some(mut vec) => {
Expand All @@ -94,8 +95,8 @@ async fn main() -> anyhow::Result<()> {
};
}

if let Some(email_contact) = settings.mint_info.contact_email {
let email_contact = ContactInfo::new("email".to_string(), email_contact);
if let Some(email_contact) = &settings.mint_info.contact_email {
let email_contact = ContactInfo::new("email".to_string(), email_contact.to_string());

contact_info = match contact_info {
Some(mut vec) => {
Expand Down Expand Up @@ -194,6 +195,53 @@ async fn main() -> anyhow::Result<()> {

routers
}
LnBackend::Phoenixd => {
let api_password = settings
.clone()
.phoenixd
.expect("Checked at config load")
.api_password;

let api_url = settings
.clone()
.phoenixd
.expect("Checked at config load")
.api_url;
if fee_reserve.percent_fee_reserve < 2.0 {
bail!("Fee reserve is too low");
}

let webhook_endpoint = "/webhook/phoenixd";

let webhook_url = format!("{}{}", settings.info.url, webhook_endpoint);

let (sender, receiver) = tokio::sync::mpsc::channel(8);

let phoenixd = Phoenixd::new(
api_password.to_string(),
api_url.to_string(),
MintMeltSettings::default(),
MintMeltSettings::default(),
fee_reserve,
Arc::new(Mutex::new(Some(receiver))),
webhook_url,
)?;

let router = phoenixd
.create_invoice_webhook(webhook_endpoint, sender)
.await?;

supported_units.insert(CurrencyUnit::Sat, (input_fee_ppk, 64));
ln_backends.insert(
LnKey {
unit: CurrencyUnit::Sat,
method: PaymentMethod::Bolt11,
},
Arc::new(phoenixd),
);

vec![router]
}
LnBackend::FakeWallet => {
let units = settings.fake_wallet.unwrap_or_default().supported_units;

Expand Down
2 changes: 1 addition & 1 deletion crates/cdk-phoenixd/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cdk-phoenixd"
version = "0.1.0"
version = { workspace = true }
edition = "2021"
authors = ["CDK Developers"]
homepage.workspace = true
Expand Down
28 changes: 13 additions & 15 deletions crates/cdk-phoenixd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ pub struct Phoenixd {
}

impl Phoenixd {
/// Create new [`Strike`] wallet
pub async fn new(
/// Create new [`Phoenixd`] wallet
pub fn new(
api_password: String,
api_url: String,
mint_settings: MintMeltSettings,
Expand All @@ -56,6 +56,17 @@ impl Phoenixd {
webhook_url,
})
}

/// Create invoice webhook
pub async fn create_invoice_webhook(
&self,
webhook_endpoint: &str,
sender: tokio::sync::mpsc::Sender<WebhookResponse>,
) -> anyhow::Result<Router> {
self.phoenixd_api
.create_invoice_webhook_router(webhook_endpoint, sender)
.await
}
}

#[async_trait]
Expand Down Expand Up @@ -195,16 +206,3 @@ impl MintLightning for Phoenixd {
Ok(state)
}
}

impl Phoenixd {
/// Create invoice webhook
pub async fn create_invoice_webhook(
&self,
webhook_endpoint: &str,
sender: tokio::sync::mpsc::Sender<WebhookResponse>,
) -> anyhow::Result<Router> {
self.phoenixd_api
.create_invoice_webhook_router(webhook_endpoint, sender)
.await
}
}

0 comments on commit 95e1f2e

Please sign in to comment.