Skip to content

Commit

Permalink
feat: new struct 'MintUrl' which trims trailing slashes (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbeery24 authored Aug 12, 2024
1 parent e57c7f1 commit 4055498
Show file tree
Hide file tree
Showing 28 changed files with 385 additions and 229 deletions.
6 changes: 3 additions & 3 deletions crates/cdk-cli/src/sub_commands/balance.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashMap;

use anyhow::Result;
use cdk::mint_url::MintUrl;
use cdk::nuts::CurrencyUnit;
use cdk::url::UncheckedUrl;
use cdk::wallet::multi_mint_wallet::MultiMintWallet;
use cdk::Amount;

Expand All @@ -14,8 +14,8 @@ pub async fn balance(multi_mint_wallet: &MultiMintWallet) -> Result<()> {
pub async fn mint_balances(
multi_mint_wallet: &MultiMintWallet,
unit: &CurrencyUnit,
) -> Result<Vec<(UncheckedUrl, Amount)>> {
let wallets: HashMap<UncheckedUrl, Amount> = multi_mint_wallet.get_balances(unit).await?;
) -> Result<Vec<(MintUrl, Amount)>> {
let wallets: HashMap<MintUrl, Amount> = multi_mint_wallet.get_balances(unit).await?;

let mut wallets_vec = Vec::with_capacity(wallets.capacity());

Expand Down
5 changes: 3 additions & 2 deletions crates/cdk-cli/src/sub_commands/burn.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::str::FromStr;

use anyhow::Result;
use cdk::mint_url::MintUrl;
use cdk::nuts::CurrencyUnit;
use cdk::wallet::multi_mint_wallet::WalletKey;
use cdk::wallet::MultiMintWallet;
use cdk::{Amount, UncheckedUrl};
use cdk::Amount;
use clap::Args;

#[derive(Args)]
pub struct BurnSubCommand {
/// Mint Url
mint_url: Option<UncheckedUrl>,
mint_url: Option<MintUrl>,
/// Currency unit e.g. sat
#[arg(default_value = "sat")]
unit: String,
Expand Down
4 changes: 2 additions & 2 deletions crates/cdk-cli/src/sub_commands/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::time::Duration;
use anyhow::Result;
use cdk::amount::SplitTarget;
use cdk::cdk_database::{Error, WalletDatabase};
use cdk::mint_url::MintUrl;
use cdk::nuts::{CurrencyUnit, MintQuoteState};
use cdk::url::UncheckedUrl;
use cdk::wallet::multi_mint_wallet::WalletKey;
use cdk::wallet::{MultiMintWallet, Wallet};
use cdk::Amount;
Expand All @@ -16,7 +16,7 @@ use tokio::time::sleep;
#[derive(Args)]
pub struct MintSubCommand {
/// Mint url
mint_url: UncheckedUrl,
mint_url: MintUrl,
/// Amount
amount: u64,
/// Currency unit e.g. sat
Expand Down
4 changes: 2 additions & 2 deletions crates/cdk-cli/src/sub_commands/mint_info.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use anyhow::Result;
use cdk::url::UncheckedUrl;
use cdk::mint_url::MintUrl;
use cdk::HttpClient;
use clap::Args;
use url::Url;

#[derive(Args)]
pub struct MintInfoSubcommand {
/// Cashu Token
mint_url: UncheckedUrl,
mint_url: MintUrl,
}

pub async fn mint_info(proxy: Option<Url>, sub_command_args: &MintInfoSubcommand) -> Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions crates/cdk-cli/src/sub_commands/restore.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::str::FromStr;

use anyhow::{anyhow, Result};
use cdk::mint_url::MintUrl;
use cdk::nuts::CurrencyUnit;
use cdk::url::UncheckedUrl;
use cdk::wallet::multi_mint_wallet::WalletKey;
use cdk::wallet::MultiMintWallet;
use clap::Args;

#[derive(Args)]
pub struct RestoreSubCommand {
/// Mint Url
mint_url: UncheckedUrl,
mint_url: MintUrl,
/// Currency unit e.g. sat
#[arg(default_value = "sat")]
unit: String,
Expand Down
6 changes: 3 additions & 3 deletions crates/cdk-cli/src/sub_commands/update_mint_url.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use anyhow::{anyhow, Result};
use cdk::mint_url::MintUrl;
use cdk::nuts::CurrencyUnit;
use cdk::url::UncheckedUrl;
use cdk::wallet::multi_mint_wallet::WalletKey;
use cdk::wallet::MultiMintWallet;
use clap::Args;

#[derive(Args)]
pub struct UpdateMintUrlSubCommand {
/// Old Mint Url
old_mint_url: UncheckedUrl,
old_mint_url: MintUrl,
/// New Mint Url
new_mint_url: UncheckedUrl,
new_mint_url: MintUrl,
}

pub async fn update_mint_url(
Expand Down
3 changes: 3 additions & 0 deletions crates/cdk-redb/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub enum Error {
/// CDK Database Error
#[error(transparent)]
CDKDatabase(#[from] cdk::cdk_database::Error),
/// CDK Mint Url Error
#[error(transparent)]
CDKMintUrl(#[from] cdk::mint_url::Error),
/// CDK Error
#[error(transparent)]
CDK(#[from] cdk::error::Error),
Expand Down
7 changes: 4 additions & 3 deletions crates/cdk-redb/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::collections::HashMap;
use std::sync::Arc;

use cdk::mint_url::MintUrl;
use cdk::nuts::{CurrencyUnit, MeltQuoteState, MintQuoteState};
use cdk::{Amount, UncheckedUrl};
use cdk::Amount;
use redb::{Database, ReadableTable, TableDefinition};
use serde::{Deserialize, Serialize};

Expand All @@ -15,7 +16,7 @@ const MELT_QUOTES_TABLE: TableDefinition<&str, &str> = TableDefinition::new("mel
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct V0MintQuote {
pub id: String,
pub mint_url: UncheckedUrl,
pub mint_url: MintUrl,
pub amount: Amount,
pub unit: CurrencyUnit,
pub request: String,
Expand All @@ -27,7 +28,7 @@ pub struct V0MintQuote {
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct V1MintQuote {
pub id: String,
pub mint_url: UncheckedUrl,
pub mint_url: MintUrl,
pub amount: Amount,
pub unit: CurrencyUnit,
pub request: String,
Expand Down
6 changes: 4 additions & 2 deletions crates/cdk-redb/src/mint/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use std::str::FromStr;
use std::sync::Arc;

use cdk::mint::MintQuote;
use cdk::mint_url::MintUrl;
use cdk::nuts::{CurrencyUnit, MintQuoteState, Proof, State};
use cdk::{Amount, UncheckedUrl};
use cdk::Amount;
use lightning_invoice::Bolt11Invoice;
use redb::{Database, ReadableTable, TableDefinition};
use serde::{Deserialize, Serialize};
Expand All @@ -26,11 +27,12 @@ pub fn migrate_02_to_03(db: Arc<Database>) -> Result<u32, Error> {
migrate_mint_proofs_02_to_03(db)?;
Ok(3)
}

/// Mint Quote Info
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
struct V1MintQuote {
pub id: String,
pub mint_url: UncheckedUrl,
pub mint_url: MintUrl,
pub amount: Amount,
pub unit: CurrencyUnit,
pub request: String,
Expand Down
100 changes: 100 additions & 0 deletions crates/cdk-redb/src/wallet/migrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//! Wallet Migrations
use std::ops::Deref;
use std::str::FromStr;
use std::sync::Arc;

use cdk::mint_url::MintUrl;
use redb::{
Database, MultimapTableDefinition, ReadableMultimapTable, ReadableTable, TableDefinition,
};

use super::Error;

// <Mint_url, Info>
const MINTS_TABLE: TableDefinition<&str, &str> = TableDefinition::new("mints_table");
// <Mint_Url, Keyset_id>
const MINT_KEYSETS_TABLE: MultimapTableDefinition<&str, &[u8]> =
MultimapTableDefinition::new("mint_keysets");

pub fn migrate_01_to_02(db: Arc<Database>) -> Result<u32, Error> {
migrate_trim_mint_urls_01_to_02(db)?;
Ok(2)
}

fn migrate_mints_table_01_to_02(db: Arc<Database>) -> Result<(), Error> {
let mints: Vec<(String, String)>;
{
let read_txn = db.begin_read().map_err(Error::from)?;
let table = read_txn.open_table(MINTS_TABLE).map_err(Error::from)?;

mints = table
.iter()
.map_err(Error::from)?
.flatten()
.map(|(mint_url, mint_info)| {
(mint_url.value().to_string(), mint_info.value().to_string())
})
.collect();
}

let write_txn = db.begin_write()?;
{
let mut table = write_txn.open_table(MINTS_TABLE).map_err(Error::from)?;
for (mint_url_str, info) in mints {
let mint_url = MintUrl::from_str(&mint_url_str).map_err(Error::from)?;

table.remove(mint_url_str.as_str())?;

table.insert(mint_url.to_string().as_str(), info.as_str())?;
}
}
write_txn.commit()?;

Ok(())
}

fn migrate_mint_keyset_table_01_to_02(db: Arc<Database>) -> Result<(), Error> {
let mut mints: Vec<(String, Vec<Vec<u8>>)> = vec![];
{
let read_txn = db.begin_read().map_err(Error::from)?;
let table = read_txn
.open_multimap_table(MINT_KEYSETS_TABLE)
.map_err(Error::from)?;

let mint_keysets_range = table.iter().map_err(Error::from)?;

for (url, keysets) in mint_keysets_range.flatten() {
let keysets: Vec<Vec<u8>> = keysets
.into_iter()
.flatten()
.map(|k| k.value().to_vec())
.collect();

mints.push((url.value().to_string(), keysets));
}
}

let write_txn = db.begin_write()?;
{
let mut table = write_txn
.open_multimap_table(MINT_KEYSETS_TABLE)
.map_err(Error::from)?;
for (mint_url_str, keysets) in mints {
let mint_url = MintUrl::from_str(&mint_url_str).map_err(Error::from)?;

table.remove_all(mint_url_str.as_str())?;
for keyset in keysets {
table.insert(mint_url.to_string().as_str(), keyset.deref())?;
}
}
}
write_txn.commit()?;

Ok(())
}

fn migrate_trim_mint_urls_01_to_02(db: Arc<Database>) -> Result<(), Error> {
migrate_mints_table_01_to_02(Arc::clone(&db))?;
migrate_mint_keyset_table_01_to_02(Arc::clone(&db))?;
Ok(())
}
Loading

0 comments on commit 4055498

Please sign in to comment.