Skip to content

Commit

Permalink
feat: support custom unit
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Nov 5, 2024
1 parent 5e26332 commit 767bb17
Show file tree
Hide file tree
Showing 35 changed files with 176 additions and 125 deletions.
2 changes: 1 addition & 1 deletion bindings/cdk-js/src/types/melt_quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl JsMeltQuote {

#[wasm_bindgen(getter)]
pub fn unit(&self) -> JsCurrencyUnit {
self.inner.unit.into()
self.inner.unit.clone().into()
}

#[wasm_bindgen(getter)]
Expand Down
2 changes: 1 addition & 1 deletion bindings/cdk-js/src/types/mint_quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl JsMintQuote {

#[wasm_bindgen(getter)]
pub fn unit(&self) -> JsCurrencyUnit {
self.inner.unit.into()
self.inner.unit.clone().into()
}

#[wasm_bindgen(getter)]
Expand Down
6 changes: 3 additions & 3 deletions crates/cdk-cli/src/sub_commands/pay_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub async fn pay_request(
) -> Result<()> {
let payment_request = &sub_command_args.payment_request;

let unit = payment_request.unit;
let unit = &payment_request.unit;

let amount = match payment_request.amount {
Some(amount) => amount,
Expand Down Expand Up @@ -56,7 +56,7 @@ pub async fn pay_request(
}

if let Some(unit) = unit {
if wallet.unit != unit {
if &wallet.unit != unit {
continue;
}
}
Expand Down Expand Up @@ -97,7 +97,7 @@ pub async fn pay_request(
id: payment_request.payment_id.clone(),
memo: None,
mint: matching_wallet.mint_url.clone(),
unit: matching_wallet.unit,
unit: matching_wallet.unit.clone(),
proofs,
};

Expand Down
4 changes: 2 additions & 2 deletions crates/cdk-cln/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ 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
4 changes: 2 additions & 2 deletions crates/cdk-fake-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ impl MintLightning for FakeWallet {
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
1 change: 1 addition & 0 deletions crates/cdk-integration-tests/src/init_regtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ where
Arc::new(database),
ln_backends,
supported_units,
HashMap::new(),
)
.await?;

Expand Down
1 change: 1 addition & 0 deletions crates/cdk-integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub async fn start_mint(
Arc::new(MintMemoryDatabase::default()),
ln_backends.clone(),
supported_units,
HashMap::new(),
)
.await?;
let cache_time_to_live = 3600;
Expand Down
16 changes: 6 additions & 10 deletions crates/cdk-integration-tests/tests/fake_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,16 +368,12 @@ async fn test_fake_melt_change_in_quote() -> Result<()> {
assert!(melt_response.change.is_some());

let check = wallet.melt_quote_status(&melt_quote.id).await?;
let mut melt_change = melt_response.change.unwrap();
melt_change.sort_by(|a, b| a.amount.cmp(&b.amount));

assert_eq!(
melt_response
.change
.unwrap()
.sort_by(|a, b| a.amount.cmp(&b.amount)),
check
.change
.unwrap()
.sort_by(|a, b| a.amount.cmp(&b.amount))
);
let mut check = check.change.unwrap();
check.sort_by(|a, b| a.amount.cmp(&b.amount));

assert_eq!(melt_change, check);
Ok(())
}
4 changes: 3 additions & 1 deletion crates/cdk-integration-tests/tests/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ async fn new_mint(fee: u64) -> Mint {
Arc::new(MintMemoryDatabase::default()),
HashMap::new(),
supported_units,
HashMap::new(),
)
.await
.unwrap()
Expand Down Expand Up @@ -270,7 +271,8 @@ async fn test_swap_unbalanced() -> Result<()> {
async fn test_swap_overpay_underpay_fee() -> Result<()> {
let mint = new_mint(1).await;

mint.rotate_keyset(CurrencyUnit::Sat, 1, 32, 1).await?;
mint.rotate_keyset(CurrencyUnit::Sat, 1, 32, 1, HashMap::new())
.await?;

let keys = mint.pubkeys().await?.keysets.first().unwrap().clone().keys;
let keyset_id = Id::from(&keys);
Expand Down
4 changes: 2 additions & 2 deletions crates/cdk-lnbits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ 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
4 changes: 2 additions & 2 deletions crates/cdk-lnd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ 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
15 changes: 8 additions & 7 deletions crates/cdk-mintd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ async fn main() -> anyhow::Result<()> {
api_key.clone(),
MintMethodSettings::default(),
MeltMethodSettings::default(),
unit,
unit.clone(),
Arc::new(Mutex::new(Some(receiver))),
webhook_url.to_string(),
)
Expand All @@ -199,7 +199,7 @@ async fn main() -> anyhow::Result<()> {
.await?;
routers.push(router);

let ln_key = LnKey::new(unit, PaymentMethod::Bolt11);
let ln_key = LnKey::new(unit.clone(), PaymentMethod::Bolt11);

ln_backends.insert(ln_key, Arc::new(strike));

Expand Down Expand Up @@ -237,7 +237,7 @@ async fn main() -> anyhow::Result<()> {

let unit = CurrencyUnit::Sat;

let ln_key = LnKey::new(unit, PaymentMethod::Bolt11);
let ln_key = LnKey::new(unit.clone(), PaymentMethod::Bolt11);

ln_backends.insert(ln_key, Arc::new(lnbits));

Expand Down Expand Up @@ -326,7 +326,7 @@ async fn main() -> anyhow::Result<()> {
let units = settings.fake_wallet.unwrap_or_default().supported_units;

for unit in units {
let ln_key = LnKey::new(unit, PaymentMethod::Bolt11);
let ln_key = LnKey::new(unit.clone(), PaymentMethod::Bolt11);

let wallet = Arc::new(FakeWallet::new(
fee_reserve.clone(),
Expand Down Expand Up @@ -361,21 +361,21 @@ async fn main() -> anyhow::Result<()> {

let m = MppMethodSettings {
method: key.method,
unit: key.unit,
unit: key.unit.clone(),
mpp: settings.mpp,
};

let n4 = MintMethodSettings {
method: key.method,
unit: key.unit,
unit: key.unit.clone(),
min_amount: settings.mint_settings.min_amount,
max_amount: settings.mint_settings.max_amount,
description: settings.invoice_description,
};

let n5 = MeltMethodSettings {
method: key.method,
unit: key.unit,
unit: key.unit.clone(),
min_amount: settings.melt_settings.min_amount,
max_amount: settings.melt_settings.max_amount,
};
Expand Down Expand Up @@ -438,6 +438,7 @@ async fn main() -> anyhow::Result<()> {
localstore,
ln_backends.clone(),
supported_units,
HashMap::new(),
)
.await?;

Expand Down
4 changes: 2 additions & 2 deletions crates/cdk-phoenixd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ impl MintLightning for Phoenixd {
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
10 changes: 5 additions & 5 deletions crates/cdk-strike/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ impl MintLightning for Strike {
fn get_settings(&self) -> Settings {
Settings {
mpp: false,
unit: self.unit,
mint_settings: self.mint_settings,
melt_settings: self.melt_settings,
unit: self.unit.clone(),
mint_settings: self.mint_settings.clone(),
melt_settings: self.melt_settings.clone(),
invoice_description: true,
}
}
Expand Down Expand Up @@ -288,7 +288,7 @@ impl MintLightning for Strike {
payment_preimage: None,
status: state,
total_spent: from_strike_amount(invoice.total_amount, &self.unit)?.into(),
unit: self.unit,
unit: self.unit.clone(),
}
}
Err(err) => match err {
Expand All @@ -297,7 +297,7 @@ impl MintLightning for Strike {
payment_preimage: None,
status: MeltQuoteState::Unknown,
total_spent: Amount::ZERO,
unit: self.unit,
unit: self.unit.clone(),
},
_ => {
return Err(Error::from(err).into());
Expand Down
13 changes: 10 additions & 3 deletions crates/cdk/src/mint/keysets.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashSet;
use std::collections::{HashMap, HashSet};

use bitcoin::bip32::DerivationPath;
use tracing::instrument;

use crate::Error;
Expand Down Expand Up @@ -89,14 +90,20 @@ impl Mint {
derivation_path_index: u32,
max_order: u8,
input_fee_ppk: u64,
custom_paths: HashMap<CurrencyUnit, DerivationPath>,
) -> Result<(), Error> {
let derivation_path = derivation_path_from_unit(unit, derivation_path_index);
let derivation_path = match custom_paths.get(&unit) {
Some(path) => path.clone(),
None => derivation_path_from_unit(unit.clone(), derivation_path_index)
.ok_or(Error::UnsupportedUnit)?,
};

let (keyset, keyset_info) = create_new_keyset(
&self.secp_ctx,
self.xpriv,
derivation_path,
Some(derivation_path_index),
unit,
unit.clone(),
max_order,
input_fee_ppk,
);
Expand Down
11 changes: 7 additions & 4 deletions crates/cdk/src/mint/melt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ impl Mint {
}
};

self.check_melt_request_acceptable(amount, *unit, PaymentMethod::Bolt11)?;
self.check_melt_request_acceptable(amount, unit.clone(), PaymentMethod::Bolt11)?;

let ln = self
.ln
.get(&LnKey::new(*unit, PaymentMethod::Bolt11))
.get(&LnKey::new(unit.clone(), PaymentMethod::Bolt11))
.ok_or_else(|| {
tracing::info!("Could not get ln backend for {}, bolt11 ", unit);

Expand All @@ -97,7 +97,7 @@ impl Mint {

let quote = MeltQuote::new(
request.to_string(),
*unit,
unit.clone(),
payment_quote.amount,
payment_quote.fee,
unix_time() + self.quote_ttl.melt_ttl,
Expand Down Expand Up @@ -447,7 +447,10 @@ impl Mint {
}
_ => None,
};
let ln = match self.ln.get(&LnKey::new(quote.unit, PaymentMethod::Bolt11)) {
let ln = match self
.ln
.get(&LnKey::new(quote.unit.clone(), PaymentMethod::Bolt11))
{
Some(ln) => ln,
None => {
tracing::info!("Could not get ln backend for {}, bolt11 ", quote.unit);
Expand Down
10 changes: 5 additions & 5 deletions crates/cdk/src/mint/mint_nut04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ impl Mint {
fn check_mint_request_acceptable(
&self,
amount: Amount,
unit: CurrencyUnit,
unit: &CurrencyUnit,
) -> Result<(), Error> {
let nut04 = &self.mint_info.nuts.nut04;

if nut04.disabled {
return Err(Error::MintingDisabled);
}

match nut04.get_settings(&unit, &PaymentMethod::Bolt11) {
match nut04.get_settings(unit, &PaymentMethod::Bolt11) {
Some(settings) => {
if settings
.max_amount
Expand Down Expand Up @@ -64,11 +64,11 @@ impl Mint {
description,
} = mint_quote_request;

self.check_mint_request_acceptable(amount, unit)?;
self.check_mint_request_acceptable(amount, &unit)?;

let ln = self
.ln
.get(&LnKey::new(unit, PaymentMethod::Bolt11))
.get(&LnKey::new(unit.clone(), PaymentMethod::Bolt11))
.ok_or_else(|| {
tracing::info!("Bolt11 mint request for unsupported unit");

Expand Down Expand Up @@ -98,7 +98,7 @@ impl Mint {
let quote = MintQuote::new(
self.mint_url.clone(),
create_invoice_response.request.to_string(),
unit,
unit.clone(),
amount,
create_invoice_response.expiry.unwrap_or(0),
create_invoice_response.request_lookup_id.clone(),
Expand Down
Loading

0 comments on commit 767bb17

Please sign in to comment.