Skip to content

Commit

Permalink
feat: pubkey on bolt12
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Nov 18, 2024
1 parent 0af8b0d commit 1a75215
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
8 changes: 5 additions & 3 deletions crates/cdk-cli/src/sub_commands/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use anyhow::Result;
use cdk::amount::SplitTarget;
use cdk::cdk_database::{Error, WalletDatabase};
use cdk::mint_url::MintUrl;
use cdk::nuts::{CurrencyUnit, MintQuoteState, PaymentMethod};
use cdk::nuts::{CurrencyUnit, MintQuoteState, PaymentMethod, SecretKey};
use cdk::wallet::multi_mint_wallet::WalletKey;
use cdk::wallet::{MultiMintWallet, Wallet};
use clap::Args;
Expand Down Expand Up @@ -57,6 +57,8 @@ pub async fn mint(
}
};

let secret_key = SecretKey::generate();

let method = PaymentMethod::from_str(&sub_command_args.method)?;

let quote = match method {
Expand All @@ -69,8 +71,7 @@ pub async fn mint(
.expect("Amount must be defined")
.into(),
description,
// TODO: Get pubkey
None,
Some(secret_key.public_key()),
)
.await?
}
Expand All @@ -81,6 +82,7 @@ pub async fn mint(
description,
sub_command_args.single_use.unwrap_or(false),
sub_command_args.expiry,
secret_key.public_key(),
)
.await?
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ impl Mint {
description,
single_use,
expiry,
pubkey,
} = mint_quote_request;

let nut18 = &self
Expand Down Expand Up @@ -68,8 +69,7 @@ impl Mint {
Amount::ZERO,
single_use,
vec![],
// TODO: Add pubkey to request
None,
Some(pubkey),
);

tracing::debug!(
Expand All @@ -82,7 +82,7 @@ impl Mint {

self.localstore.add_mint_quote(quote.clone()).await?;

Ok(quote.into())
Ok(quote.try_into()?)
}

/// Check mint quote
Expand All @@ -97,6 +97,6 @@ impl Mint {
.await?
.ok_or(Error::UnknownQuote)?;

Ok(quote.into())
Ok(quote.try_into()?)
}
}
2 changes: 1 addition & 1 deletion crates/cdk/src/mint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod check_spendable;
mod info;
mod keysets;
mod melt;
mod mint_18;
mod mint_20;
mod mint_nut04;
mod start_up_check;
mod swap;
Expand Down
19 changes: 15 additions & 4 deletions crates/cdk/src/nuts/nut20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
use thiserror::Error;

use super::nut00::CurrencyUnit;
use super::PublicKey;
use crate::Amount;

/// NUT04 Error
Expand All @@ -17,6 +18,9 @@ pub enum Error {
/// Amount overflow
#[error("Amount overflow")]
AmountOverflow,
/// Publickey not defined
#[error("Publickey not defined")]
PublickeyUndefined,
}

/// Mint quote request [NUT-19]
Expand All @@ -32,6 +36,8 @@ pub struct MintQuoteBolt12Request {
pub single_use: bool,
/// Expiry
pub expiry: Option<u64>,
/// Pubkey
pub pubkey: PublicKey,
}

/// Mint quote response [NUT-19]
Expand All @@ -49,18 +55,23 @@ pub struct MintQuoteBolt12Response {
pub amount_paid: Amount,
/// Amount that has been issued
pub amount_issued: Amount,
/// Pubkey
pub pubkey: PublicKey,
}

#[cfg(feature = "mint")]
impl From<crate::mint::MintQuote> for MintQuoteBolt12Response {
fn from(mint_quote: crate::mint::MintQuote) -> MintQuoteBolt12Response {
MintQuoteBolt12Response {
impl TryFrom<crate::mint::MintQuote> for MintQuoteBolt12Response {
type Error = Error;

fn try_from(mint_quote: crate::mint::MintQuote) -> Result<MintQuoteBolt12Response, Error> {
Ok(MintQuoteBolt12Response {
quote: mint_quote.id,
request: mint_quote.request,
expiry: Some(mint_quote.expiry),
amount_paid: mint_quote.amount_paid,
amount_issued: mint_quote.amount_issued,
single_use: mint_quote.single_use,
}
pubkey: mint_quote.pubkey.ok_or(Error::PublickeyUndefined)?,
})
}
}
4 changes: 3 additions & 1 deletion crates/cdk/src/wallet/mint_bolt12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::dhke::construct_proofs;
use crate::nuts::nut00::ProofsMethods;
use crate::nuts::{
nut12, MintBolt11Request, MintQuoteBolt12Request, MintQuoteBolt12Response, PaymentMethod,
PreMintSecrets, SpendingConditions, State,
PreMintSecrets, PublicKey, SpendingConditions, State,
};
use crate::types::ProofInfo;
use crate::util::unix_time;
Expand All @@ -21,6 +21,7 @@ impl Wallet {
description: Option<String>,
single_use: bool,
expiry: Option<u64>,
pubkey: PublicKey,
) -> Result<MintQuote, Error> {
let mint_url = self.mint_url.clone();
let unit = &self.unit;
Expand Down Expand Up @@ -48,6 +49,7 @@ impl Wallet {
description,
single_use,
expiry,
pubkey,
};

let quote_res = self
Expand Down

0 comments on commit 1a75215

Please sign in to comment.