Skip to content

Commit

Permalink
NUT-04 Mint Quote Description (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
lollerfirst authored Sep 12, 2024
1 parent ce77295 commit 7e860c7
Show file tree
Hide file tree
Showing 22 changed files with 193 additions and 126 deletions.
8 changes: 6 additions & 2 deletions bindings/cdk-js/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,14 @@ impl JsWallet {
}

#[wasm_bindgen(js_name = mintQuote)]
pub async fn mint_quote(&mut self, amount: u64) -> Result<JsMintQuote> {
pub async fn mint_quote(
&mut self,
amount: u64,
description: Option<String>,
) -> Result<JsMintQuote> {
let quote = self
.inner
.mint_quote(amount.into())
.mint_quote(amount.into(), description)
.await
.map_err(into_err)?;

Expand Down
14 changes: 11 additions & 3 deletions crates/cdk-axum/src/router_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,22 @@ pub async fn get_mint_bolt11_quote(

let amount =
to_unit(payload.amount, &payload.unit, &ln.get_settings().unit).map_err(|err| {
tracing::error!("Backed does not support unit: {}", err);
tracing::error!("Backend does not support unit: {}", err);
into_response(Error::UnitUnsupported)
})?;

let quote_expiry = unix_time() + state.quote_ttl;

if payload.description.is_some() && !ln.get_settings().invoice_description {
tracing::error!("Backend does not support invoice description");
return Err(into_response(Error::InvoiceDescriptionUnsupported));
}
let create_invoice_response = ln
.create_invoice(amount, &payload.unit, "".to_string(), quote_expiry)
.create_invoice(
amount,
&payload.unit,
payload.description.unwrap_or("".to_string()),
quote_expiry,
)
.await
.map_err(|err| {
tracing::error!("Could not create invoice: {}", err);
Expand Down
9 changes: 7 additions & 2 deletions crates/cdk-cli/src/sub_commands/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use cdk::wallet::multi_mint_wallet::WalletKey;
use cdk::wallet::{MultiMintWallet, Wallet};
use cdk::Amount;
use clap::Args;
use serde::{Deserialize, Serialize};
use tokio::time::sleep;

#[derive(Args)]
#[derive(Args, Serialize, Deserialize)]
pub struct MintSubCommand {
/// Mint url
mint_url: MintUrl,
Expand All @@ -22,6 +23,9 @@ pub struct MintSubCommand {
/// Currency unit e.g. sat
#[arg(default_value = "sat")]
unit: String,
/// Quote description
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
}

pub async fn mint(
Expand All @@ -32,6 +36,7 @@ pub async fn mint(
) -> Result<()> {
let mint_url = sub_command_args.mint_url.clone();
let unit = CurrencyUnit::from_str(&sub_command_args.unit)?;
let description: Option<String> = sub_command_args.description.clone();

let wallet = match multi_mint_wallet
.get_wallet(&WalletKey::new(mint_url.clone(), CurrencyUnit::Sat))
Expand All @@ -47,7 +52,7 @@ pub async fn mint(
};

let quote = wallet
.mint_quote(Amount::from(sub_command_args.amount))
.mint_quote(Amount::from(sub_command_args.amount), description)
.await?;

println!("Quote: {:#?}", quote);
Expand Down
22 changes: 13 additions & 9 deletions crates/cdk-cln/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ use std::time::Duration;
use async_trait::async_trait;
use cdk::amount::Amount;
use cdk::cdk_lightning::{
self, to_unit, CreateInvoiceResponse, MintLightning, MintMeltSettings, PayInvoiceResponse,
PaymentQuoteResponse, Settings,
self, to_unit, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, PaymentQuoteResponse,
Settings,
};
use cdk::mint::FeeReserve;
use cdk::nuts::{CurrencyUnit, MeltQuoteBolt11Request, MeltQuoteState, MintQuoteState};
use cdk::nuts::{
CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteState, MintMethodSettings,
MintQuoteState,
};
use cdk::util::{hex, unix_time};
use cdk::{mint, Bolt11Invoice};
use cln_rpc::model::requests::{
Expand All @@ -40,17 +43,17 @@ pub struct Cln {
rpc_socket: PathBuf,
cln_client: Arc<Mutex<cln_rpc::ClnRpc>>,
fee_reserve: FeeReserve,
mint_settings: MintMeltSettings,
melt_settings: MintMeltSettings,
mint_settings: MintMethodSettings,
melt_settings: MeltMethodSettings,
}

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

Expand All @@ -72,8 +75,9 @@ impl MintLightning for Cln {
Settings {
mpp: true,
unit: CurrencyUnit::Msat,
mint_settings: self.mint_settings,
melt_settings: self.melt_settings,
mint_settings: self.mint_settings.clone(),
melt_settings: self.melt_settings.clone(),
invoice_description: true,
}
}

Expand Down
22 changes: 13 additions & 9 deletions crates/cdk-fake-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ use bitcoin::hashes::{sha256, Hash};
use bitcoin::secp256k1::{Secp256k1, SecretKey};
use cdk::amount::Amount;
use cdk::cdk_lightning::{
self, to_unit, CreateInvoiceResponse, MintLightning, MintMeltSettings, PayInvoiceResponse,
PaymentQuoteResponse, Settings,
self, to_unit, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, PaymentQuoteResponse,
Settings,
};
use cdk::mint;
use cdk::mint::FeeReserve;
use cdk::nuts::{CurrencyUnit, MeltQuoteBolt11Request, MeltQuoteState, MintQuoteState};
use cdk::nuts::{
CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteState, MintMethodSettings,
MintQuoteState,
};
use cdk::util::unix_time;
use error::Error;
use futures::stream::StreamExt;
Expand All @@ -37,16 +40,16 @@ pub struct FakeWallet {
fee_reserve: FeeReserve,
sender: tokio::sync::mpsc::Sender<String>,
receiver: Arc<Mutex<Option<tokio::sync::mpsc::Receiver<String>>>>,
mint_settings: MintMeltSettings,
melt_settings: MintMeltSettings,
mint_settings: MintMethodSettings,
melt_settings: MeltMethodSettings,
}

impl FakeWallet {
/// Creat new [`FakeWallet`]
pub fn new(
fee_reserve: FeeReserve,
mint_settings: MintMeltSettings,
melt_settings: MintMeltSettings,
mint_settings: MintMethodSettings,
melt_settings: MeltMethodSettings,
) -> Self {
let (sender, receiver) = tokio::sync::mpsc::channel(8);

Expand All @@ -68,8 +71,9 @@ impl MintLightning for FakeWallet {
Settings {
mpp: true,
unit: CurrencyUnit::Msat,
melt_settings: self.melt_settings,
mint_settings: self.mint_settings,
melt_settings: self.melt_settings.clone(),
mint_settings: self.mint_settings.clone(),
invoice_description: true,
}
}

Expand Down
15 changes: 9 additions & 6 deletions crates/cdk-integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use axum::Router;
use bip39::Mnemonic;
use cdk::amount::{Amount, SplitTarget};
use cdk::cdk_database::mint_memory::MintMemoryDatabase;
use cdk::cdk_lightning::{MintLightning, MintMeltSettings};
use cdk::cdk_lightning::MintLightning;
use cdk::dhke::construct_proofs;
use cdk::mint::FeeReserve;
use cdk::nuts::{
CurrencyUnit, Id, KeySet, MintInfo, MintQuoteState, Nuts, PaymentMethod, PreMintSecrets, Proofs,
CurrencyUnit, Id, KeySet, MeltMethodSettings, MintInfo, MintMethodSettings, MintQuoteState,
Nuts, PaymentMethod, PreMintSecrets, Proofs,
};
use cdk::wallet::client::HttpClient;
use cdk::{Mint, Wallet};
Expand Down Expand Up @@ -39,8 +40,8 @@ pub fn create_backends_fake_wallet(

let wallet = Arc::new(FakeWallet::new(
fee_reserve.clone(),
MintMeltSettings::default(),
MintMeltSettings::default(),
MintMethodSettings::default(),
MeltMethodSettings::default(),
));

ln_backends.insert(ln_key, wallet.clone());
Expand Down Expand Up @@ -152,8 +153,9 @@ pub async fn wallet_mint(
wallet: Arc<Wallet>,
amount: Amount,
split_target: SplitTarget,
description: Option<String>,
) -> Result<()> {
let quote = wallet.mint_quote(amount).await?;
let quote = wallet.mint_quote(amount, description).await?;

loop {
let status = wallet.mint_quote_state(&quote.id).await?;
Expand All @@ -178,14 +180,15 @@ pub async fn mint_proofs(
amount: Amount,
keyset_id: Id,
mint_keys: &KeySet,
description: Option<String>,
) -> anyhow::Result<Proofs> {
println!("Minting for ecash");
println!();

let wallet_client = HttpClient::new();

let mint_quote = wallet_client
.post_mint_quote(mint_url.parse()?, 1.into(), CurrencyUnit::Sat)
.post_mint_quote(mint_url.parse()?, 1.into(), CurrencyUnit::Sat, description)
.await?;

println!("Please pay: {}", mint_quote.request);
Expand Down
22 changes: 13 additions & 9 deletions crates/cdk-lnbits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ use async_trait::async_trait;
use axum::Router;
use cdk::amount::Amount;
use cdk::cdk_lightning::{
self, to_unit, CreateInvoiceResponse, MintLightning, MintMeltSettings, PayInvoiceResponse,
PaymentQuoteResponse, Settings,
self, to_unit, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, PaymentQuoteResponse,
Settings,
};
use cdk::mint::FeeReserve;
use cdk::nuts::{CurrencyUnit, MeltQuoteBolt11Request, MeltQuoteState, MintQuoteState};
use cdk::nuts::{
CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteState, MintMethodSettings,
MintQuoteState,
};
use cdk::util::unix_time;
use cdk::{mint, Bolt11Invoice};
use error::Error;
Expand All @@ -31,8 +34,8 @@ pub mod error;
#[derive(Clone)]
pub struct LNbits {
lnbits_api: LNBitsClient,
mint_settings: MintMeltSettings,
melt_settings: MintMeltSettings,
mint_settings: MintMethodSettings,
melt_settings: MeltMethodSettings,
fee_reserve: FeeReserve,
receiver: Arc<Mutex<Option<tokio::sync::mpsc::Receiver<String>>>>,
webhook_url: String,
Expand All @@ -45,8 +48,8 @@ impl LNbits {
admin_api_key: String,
invoice_api_key: String,
api_url: String,
mint_settings: MintMeltSettings,
melt_settings: MintMeltSettings,
mint_settings: MintMethodSettings,
melt_settings: MeltMethodSettings,
fee_reserve: FeeReserve,
receiver: Arc<Mutex<Option<tokio::sync::mpsc::Receiver<String>>>>,
webhook_url: String,
Expand All @@ -72,8 +75,9 @@ impl MintLightning for LNbits {
Settings {
mpp: false,
unit: CurrencyUnit::Sat,
mint_settings: self.mint_settings,
melt_settings: self.melt_settings,
mint_settings: self.mint_settings.clone(),
melt_settings: self.melt_settings.clone(),
invoice_description: true,
}
}

Expand Down
22 changes: 13 additions & 9 deletions crates/cdk-lnd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ use anyhow::anyhow;
use async_trait::async_trait;
use cdk::amount::Amount;
use cdk::cdk_lightning::{
self, to_unit, CreateInvoiceResponse, MintLightning, MintMeltSettings, PayInvoiceResponse,
PaymentQuoteResponse, Settings, MSAT_IN_SAT,
self, to_unit, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, PaymentQuoteResponse,
Settings, MSAT_IN_SAT,
};
use cdk::mint::FeeReserve;
use cdk::nuts::{CurrencyUnit, MeltQuoteBolt11Request, MeltQuoteState, MintQuoteState};
use cdk::nuts::{
CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteState, MintMethodSettings,
MintQuoteState,
};
use cdk::util::{hex, unix_time};
use cdk::{mint, Bolt11Invoice};
use error::Error;
Expand All @@ -38,8 +41,8 @@ pub struct Lnd {
macaroon_file: PathBuf,
client: Arc<Mutex<Client>>,
fee_reserve: FeeReserve,
mint_settings: MintMeltSettings,
melt_settings: MintMeltSettings,
mint_settings: MintMethodSettings,
melt_settings: MeltMethodSettings,
}

impl Lnd {
Expand All @@ -49,8 +52,8 @@ impl Lnd {
cert_file: PathBuf,
macaroon_file: PathBuf,
fee_reserve: FeeReserve,
mint_settings: MintMeltSettings,
melt_settings: MintMeltSettings,
mint_settings: MintMethodSettings,
melt_settings: MeltMethodSettings,
) -> Result<Self, Error> {
let client = fedimint_tonic_lnd::connect(address.to_string(), &cert_file, &macaroon_file)
.await
Expand Down Expand Up @@ -79,8 +82,9 @@ impl MintLightning for Lnd {
Settings {
mpp: true,
unit: CurrencyUnit::Msat,
mint_settings: self.mint_settings,
melt_settings: self.melt_settings,
mint_settings: self.mint_settings.clone(),
melt_settings: self.melt_settings.clone(),
invoice_description: true,
}
}

Expand Down
Loading

0 comments on commit 7e860c7

Please sign in to comment.