Skip to content

Commit

Permalink
feat: bolt12 mint builder
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Nov 17, 2024
1 parent a4789ea commit e90dc6d
Show file tree
Hide file tree
Showing 18 changed files with 145 additions and 213 deletions.
13 changes: 1 addition & 12 deletions crates/cdk-cln/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,20 @@ pub struct Cln {
rpc_socket: PathBuf,
cln_client: Arc<Mutex<cln_rpc::ClnRpc>>,
fee_reserve: FeeReserve,
bolt12_mint: bool,
bolt12_melt: bool,
wait_invoice_cancel_token: CancellationToken,
wait_invoice_is_active: Arc<AtomicBool>,
bolt12_wait_invoice_is_active: Arc<AtomicBool>,
}

impl Cln {
/// Create new [`Cln`]
pub async fn new(
rpc_socket: PathBuf,
fee_reserve: FeeReserve,
bolt12_mint: bool,
bolt12_melt: bool,
) -> Result<Self, Error> {
pub async fn new(rpc_socket: PathBuf, fee_reserve: FeeReserve) -> Result<Self, Error> {
let cln_client = cln_rpc::ClnRpc::new(&rpc_socket).await?;

Ok(Self {
rpc_socket,
cln_client: Arc::new(Mutex::new(cln_client)),
fee_reserve,
bolt12_mint,
bolt12_melt,
wait_invoice_cancel_token: CancellationToken::new(),
wait_invoice_is_active: Arc::new(AtomicBool::new(false)),
bolt12_wait_invoice_is_active: Arc::new(AtomicBool::new(false)),
Expand All @@ -83,8 +74,6 @@ impl MintLightning for Cln {
Settings {
mpp: true,
unit: CurrencyUnit::Msat,
bolt12_mint: self.bolt12_mint,
bolt12_melt: self.bolt12_melt,
invoice_description: true,
}
}
Expand Down
2 changes: 0 additions & 2 deletions crates/cdk-fake-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ impl MintLightning for FakeWallet {
Settings {
mpp: true,
unit: CurrencyUnit::Msat,
bolt12_mint: false,
bolt12_melt: false,
invoice_description: true,
}
}
Expand Down
8 changes: 2 additions & 6 deletions crates/cdk-integration-tests/src/init_fake_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use cdk::cdk_database::{self, MintDatabase};
use cdk::cdk_lightning::MintLightning;
use cdk::mint::FeeReserve;
use cdk::nuts::CurrencyUnit;
use cdk::types::LnKey;
use cdk_fake_wallet::FakeWallet;
use tokio::sync::Notify;
use tower_http::cors::CorsLayer;
Expand All @@ -33,7 +32,7 @@ where
tracing_subscriber::fmt().with_env_filter(env_filter).init();

let mut ln_backends: HashMap<
LnKey,
CurrencyUnit,
Arc<dyn MintLightning<Err = cdk::cdk_lightning::Error> + Sync + Send>,
> = HashMap::new();

Expand All @@ -44,10 +43,7 @@ where

let fake_wallet = FakeWallet::new(fee_reserve, HashMap::default(), HashSet::default(), 0);

ln_backends.insert(
LnKey::new(CurrencyUnit::Sat, cdk::nuts::PaymentMethod::Bolt11),
Arc::new(fake_wallet),
);
ln_backends.insert(CurrencyUnit::Sat, Arc::new(fake_wallet));

let mint = create_mint(database, ln_backends.clone()).await?;
let cache_ttl = 3600;
Expand Down
13 changes: 5 additions & 8 deletions crates/cdk-integration-tests/src/init_regtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cdk::cdk_database::{self, MintDatabase};
use cdk::cdk_lightning::MintLightning;
use cdk::mint::{FeeReserve, Mint};
use cdk::nuts::{CurrencyUnit, MintInfo};
use cdk::types::{LnKey, QuoteTTL};
use cdk::types::QuoteTTL;
use cdk_cln::Cln as CdkCln;
use ln_regtest_rs::bitcoin_client::BitcoinClient;
use ln_regtest_rs::bitcoind::Bitcoind;
Expand Down Expand Up @@ -138,13 +138,13 @@ pub async fn create_cln_backend(cln_client: &ClnClient) -> Result<CdkCln> {
percent_fee_reserve: 1.0,
};

Ok(CdkCln::new(rpc_path, fee_reserve, true, true).await?)
Ok(CdkCln::new(rpc_path, fee_reserve).await?)
}

pub async fn create_mint<D>(
database: D,
ln_backends: HashMap<
LnKey,
CurrencyUnit,
Arc<dyn MintLightning<Err = cdk::cdk_lightning::Error> + Sync + Send>,
>,
) -> Result<Mint>
Expand Down Expand Up @@ -207,14 +207,11 @@ where
let cln_backend = create_cln_backend(&cln_client).await?;

let mut ln_backends: HashMap<
LnKey,
CurrencyUnit,
Arc<dyn MintLightning<Err = cdk::cdk_lightning::Error> + Sync + Send>,
> = HashMap::new();

ln_backends.insert(
LnKey::new(CurrencyUnit::Sat, cdk::nuts::PaymentMethod::Bolt11),
Arc::new(cln_backend),
);
ln_backends.insert(CurrencyUnit::Sat, Arc::new(cln_backend));

let mint = create_mint(database, ln_backends.clone()).await?;
let cache_time_to_live = 3600;
Expand Down
2 changes: 1 addition & 1 deletion crates/cdk-integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn create_backends_fake_wallet(

pub async fn start_mint(
ln_backends: HashMap<
LnKey,
CurrencyUnit,
Arc<dyn MintLightning<Err = cdk::cdk_lightning::Error> + Sync + Send>,
>,
supported_units: HashMap<CurrencyUnit, (u64, u8)>,
Expand Down
2 changes: 0 additions & 2 deletions crates/cdk-lnbits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ impl MintLightning for LNbits {
Settings {
mpp: false,
unit: CurrencyUnit::Sat,
bolt12_mint: false,
bolt12_melt: false,
invoice_description: true,
}
}
Expand Down
2 changes: 0 additions & 2 deletions crates/cdk-lnd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ impl MintLightning for Lnd {
Settings {
mpp: true,
unit: CurrencyUnit::Msat,
bolt12_mint: false,
bolt12_melt: false,
invoice_description: true,
}
}
Expand Down
77 changes: 18 additions & 59 deletions crates/cdk-mintd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,8 @@ async fn main() -> anyhow::Result<()> {
};
ln_backends.insert(ln_key, cln.clone());

mint_builder = mint_builder.add_ln_backend(
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
mint_melt_limits,
cln.clone(),
);
mint_builder =
mint_builder.add_ln_backend(CurrencyUnit::Sat, mint_melt_limits, cln.clone());

if cln_settings.bolt12 {
let ln_key = LnKey {
Expand All @@ -158,12 +154,8 @@ async fn main() -> anyhow::Result<()> {
};
ln_backends.insert(ln_key, cln.clone());

mint_builder = mint_builder.add_ln_backend(
CurrencyUnit::Sat,
PaymentMethod::Bolt12,
mint_melt_limits,
cln,
)
mint_builder =
mint_builder.add_bolt12_ln_backend(CurrencyUnit::Sat, mint_melt_limits, cln)
}
}
LnBackend::Strike => {
Expand All @@ -178,12 +170,8 @@ async fn main() -> anyhow::Result<()> {
.setup(&mut ln_routers, &settings, unit.clone())
.await?;

mint_builder = mint_builder.add_ln_backend(
unit,
PaymentMethod::Bolt11,
mint_melt_limits,
Arc::new(strike),
);
mint_builder =
mint_builder.add_ln_backend(unit, mint_melt_limits, Arc::new(strike));
}
}
LnBackend::LNbits => {
Expand All @@ -192,38 +180,26 @@ async fn main() -> anyhow::Result<()> {
.setup(&mut ln_routers, &settings, CurrencyUnit::Sat)
.await?;

mint_builder = mint_builder.add_ln_backend(
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
mint_melt_limits,
Arc::new(lnbits),
);
mint_builder =
mint_builder.add_ln_backend(CurrencyUnit::Sat, mint_melt_limits, Arc::new(lnbits));
}
LnBackend::Phoenixd => {
let phd_settings = settings.clone().phoenixd.expect("Checked at config load");
let phd = phd_settings
.setup(&mut ln_routers, &settings, CurrencyUnit::Sat)
.await?;

mint_builder = mint_builder.add_ln_backend(
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
mint_melt_limits,
Arc::new(phd),
);
mint_builder =
mint_builder.add_ln_backend(CurrencyUnit::Sat, mint_melt_limits, Arc::new(phd));
}
LnBackend::Lnd => {
let lnd_settings = settings.clone().lnd.expect("Checked at config load");
let lnd = lnd_settings
.setup(&mut ln_routers, &settings, CurrencyUnit::Msat)
.await?;

mint_builder = mint_builder.add_ln_backend(
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
mint_melt_limits,
Arc::new(lnd),
);
mint_builder =
mint_builder.add_ln_backend(CurrencyUnit::Sat, mint_melt_limits, Arc::new(lnd));
}
LnBackend::FakeWallet => {
let fake_wallet = settings.clone().fake_wallet.expect("Fake wallet defined");
Expand All @@ -235,33 +211,16 @@ async fn main() -> anyhow::Result<()> {

let fake = Arc::new(fake);

mint_builder = mint_builder.add_ln_backend(
unit.clone(),
PaymentMethod::Bolt11,
mint_melt_limits,
fake.clone(),
);
mint_builder =
mint_builder.add_ln_backend(unit.clone(), mint_melt_limits, fake.clone());

mint_builder = mint_builder.add_ln_backend(
unit,
PaymentMethod::Bolt12,
mint_melt_limits,
fake.clone(),
);
// TODO: Bolt12 for fake
// mint_builder =
// mint_builder.add_bolt12_ln_backend(unit, mint_melt_limits, fake.clone());
}
}
};

let support_bolt12_mint = ln_backends.iter().any(|(_k, ln)| {
let settings = ln.get_settings();
settings.bolt12_mint
});

let support_bolt12_melt = ln_backends.iter().any(|(_k, ln)| {
let settings = ln.get_settings();
settings.bolt12_melt
});

if let Some(long_description) = &settings.mint_info.description_long {
mint_builder = mint_builder.with_long_description(long_description.to_string());
}
Expand Down Expand Up @@ -326,7 +285,7 @@ async fn main() -> anyhow::Result<()> {
.seconds_to_extend_cache_by
.unwrap_or(DEFAULT_CACHE_TTI_SECS);

let include_bolt12 = support_bolt12_mint || support_bolt12_melt;
let include_bolt12 = !mint.bolt12_backends.is_empty();

let v1_service =
cdk_axum::create_mint_router(Arc::clone(&mint), cache_ttl, cache_tti, include_bolt12)
Expand Down
2 changes: 1 addition & 1 deletion crates/cdk-mintd/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl LnBackendSetup for config::Cln {
percent_fee_reserve: self.fee_percent,
};

let cln = cdk_cln::Cln::new(cln_socket, fee_reserve, true, true).await?;
let cln = cdk_cln::Cln::new(cln_socket, fee_reserve).await?;

Ok(cln)
}
Expand Down
2 changes: 0 additions & 2 deletions crates/cdk-phoenixd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ impl MintLightning for Phoenixd {
Settings {
mpp: false,
unit: CurrencyUnit::Sat,
bolt12_mint: false,
bolt12_melt: true,
invoice_description: true,
}
}
Expand Down
2 changes: 0 additions & 2 deletions crates/cdk-strike/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ impl MintLightning for Strike {
Settings {
mpp: false,
unit: self.unit.clone(),
bolt12_mint: false,
bolt12_melt: false,
invoice_description: true,
}
}
Expand Down
4 changes: 0 additions & 4 deletions crates/cdk/src/cdk_lightning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,6 @@ pub struct Bolt12PaymentQuoteResponse {
pub struct Settings {
/// MPP supported
pub mpp: bool,
/// Supports bolt12 mint
pub bolt12_mint: bool,
/// Supports bolt12 melt
pub bolt12_melt: bool,
/// Base unit of backend
pub unit: CurrencyUnit,
/// Invoice Description supported
Expand Down
Loading

0 comments on commit e90dc6d

Please sign in to comment.