fix: cdk-cli create wallets for units mint supports

This commit is contained in:
thesimplekid
2025-06-25 14:39:25 +01:00
parent 738202b957
commit ae84c61a03
2 changed files with 77 additions and 21 deletions

View File

@@ -4,6 +4,7 @@
#[cfg(feature = "auth")] #[cfg(feature = "auth")]
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::HashSet;
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
@@ -13,6 +14,7 @@ use super::nut19::CachedEndpoint;
use super::{nut04, nut05, nut15, nut19, MppMethodSettings}; use super::{nut04, nut05, nut15, nut19, MppMethodSettings};
#[cfg(feature = "auth")] #[cfg(feature = "auth")]
use super::{AuthRequired, BlindAuthSettings, ClearAuthSettings, ProtectedEndpoint}; use super::{AuthRequired, BlindAuthSettings, ClearAuthSettings, ProtectedEndpoint};
use crate::CurrencyUnit;
/// Mint Version /// Mint Version
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -256,6 +258,16 @@ impl MintInfo {
pub fn bat_max_mint(&self) -> Option<u64> { pub fn bat_max_mint(&self) -> Option<u64> {
self.nuts.nut22.as_ref().map(|s| s.bat_max_mint) self.nuts.nut22.as_ref().map(|s| s.bat_max_mint)
} }
/// Get all supported currency units for this mint (both mint and melt)
pub fn supported_units(&self) -> Vec<&CurrencyUnit> {
let mut units = HashSet::new();
units.extend(self.nuts.supported_mint_units());
units.extend(self.nuts.supported_melt_units());
units.into_iter().collect()
}
} }
/// Supported nuts and settings /// Supported nuts and settings
@@ -440,6 +452,28 @@ impl Nuts {
..self ..self
} }
} }
/// Units where minting is supported
pub fn supported_mint_units(&self) -> Vec<&CurrencyUnit> {
self.nut04
.methods
.iter()
.map(|s| &s.unit)
.collect::<HashSet<_>>()
.into_iter()
.collect()
}
/// Units where melting is supported
pub fn supported_melt_units(&self) -> Vec<&CurrencyUnit> {
self.nut05
.methods
.iter()
.map(|s| &s.unit)
.collect::<HashSet<_>>()
.into_iter()
.collect()
}
} }
/// Check state Settings /// Check state Settings

View File

@@ -8,6 +8,7 @@ use bip39::rand::{thread_rng, Rng};
use bip39::Mnemonic; use bip39::Mnemonic;
use cdk::cdk_database; use cdk::cdk_database;
use cdk::cdk_database::WalletDatabase; use cdk::cdk_database::WalletDatabase;
use cdk::nuts::CurrencyUnit;
use cdk::wallet::{HttpClient, MultiMintWallet, Wallet, WalletBuilder}; use cdk::wallet::{HttpClient, MultiMintWallet, Wallet, WalletBuilder};
#[cfg(feature = "redb")] #[cfg(feature = "redb")]
use cdk_redb::WalletRedbDatabase; use cdk_redb::WalletRedbDatabase;
@@ -171,16 +172,36 @@ async fn main() -> Result<()> {
let mints = localstore.get_mints().await?; let mints = localstore.get_mints().await?;
for (mint_url, _) in mints { for (mint_url, mint_info) in mints {
let mut builder = WalletBuilder::new() let units = if let Some(mint_info) = mint_info {
.mint_url(mint_url.clone()) mint_info.supported_units().into_iter().cloned().collect()
.unit(cdk::nuts::CurrencyUnit::Sat) } else {
.localstore(localstore.clone()) vec![CurrencyUnit::Sat]
.seed(&mnemonic.to_seed_normalized("")); };
if let Some(proxy_url) = args.proxy.as_ref() { let proxy_client = if let Some(proxy_url) = args.proxy.as_ref() {
let http_client = HttpClient::with_proxy(mint_url, proxy_url.clone(), None, true)?; Some(HttpClient::with_proxy(
builder = builder.client(http_client); mint_url.clone(),
proxy_url.clone(),
None,
true,
)?)
} else {
None
};
let seed = mnemonic.to_seed_normalized("");
for unit in units {
let mint_url_clone = mint_url.clone();
let mut builder = WalletBuilder::new()
.mint_url(mint_url_clone.clone())
.unit(unit)
.localstore(localstore.clone())
.seed(&seed);
if let Some(http_client) = &proxy_client {
builder = builder.client(http_client.clone());
} }
let wallet = builder.build()?; let wallet = builder.build()?;
@@ -199,6 +220,7 @@ async fn main() -> Result<()> {
wallets.push(wallet); wallets.push(wallet);
} }
}
let multi_mint_wallet = MultiMintWallet::new(localstore, Arc::new(seed), wallets); let multi_mint_wallet = MultiMintWallet::new(localstore, Arc::new(seed), wallets);