Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get blinded secrets #309

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bindings/cdk-js/src/nuts/nut00/currency_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum JsCurrencyUnit {
Msat,
Usd,
Eur,
Hash,
}

impl From<CurrencyUnit> for JsCurrencyUnit {
Expand All @@ -18,6 +19,7 @@ impl From<CurrencyUnit> for JsCurrencyUnit {
CurrencyUnit::Msat => JsCurrencyUnit::Msat,
CurrencyUnit::Usd => JsCurrencyUnit::Usd,
CurrencyUnit::Eur => JsCurrencyUnit::Eur,
CurrencyUnit::Hash => JsCurrencyUnit::Hash,
}
}
}
Expand All @@ -29,6 +31,7 @@ impl From<JsCurrencyUnit> for CurrencyUnit {
JsCurrencyUnit::Msat => CurrencyUnit::Msat,
JsCurrencyUnit::Usd => CurrencyUnit::Usd,
JsCurrencyUnit::Eur => CurrencyUnit::Eur,
JsCurrencyUnit::Hash => CurrencyUnit::Hash,
}
}
}
3 changes: 3 additions & 0 deletions crates/cdk-strike/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ impl MintLightning for Strike {
CurrencyUnit::Msat => StrikeCurrencyUnit::BTC,
CurrencyUnit::Usd => StrikeCurrencyUnit::USD,
CurrencyUnit::Eur => StrikeCurrencyUnit::EUR,
CurrencyUnit::Hash => unimplemented!(),
};

let payment_quote_request = PayInvoiceQuoteRequest {
Expand Down Expand Up @@ -274,6 +275,7 @@ pub(crate) fn from_strike_amount(
bail!("Could not convert to EUR");
}
}
CurrencyUnit::Hash => unimplemented!(),
}
}

Expand Down Expand Up @@ -301,5 +303,6 @@ where
amount: euro.round() / 100.0,
}
}
CurrencyUnit::Hash => unimplemented!(),
}
}
5 changes: 3 additions & 2 deletions crates/cdk/src/cdk_database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#[cfg(any(feature = "wallet", feature = "mint"))]
use std::collections::HashMap;
use std::fmt;
use std::fmt::Debug;

#[cfg(any(feature = "wallet", feature = "mint"))]
Expand Down Expand Up @@ -169,9 +170,9 @@ pub trait WalletDatabase: Debug {
/// Mint Database trait
#[cfg(feature = "mint")]
#[async_trait]
pub trait MintDatabase {
pub trait MintDatabase: fmt::Debug {
/// Mint Database Error
type Err: Into<Error> + From<Error>;
type Err: Into<Error> + From<Error> + Debug;

/// Add Active Keyset
async fn set_active_keyset(&self, unit: CurrencyUnit, id: Id) -> Result<(), Self::Err>;
Expand Down
2 changes: 1 addition & 1 deletion crates/cdk/src/mint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub mod types;
pub use types::{MeltQuote, MintQuote};

/// Cashu Mint
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Mint {
/// Mint Url
pub mint_url: MintUrl,
Expand Down
5 changes: 5 additions & 0 deletions crates/cdk/src/nuts/nut00/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ pub enum CurrencyUnit {
Usd,
/// Euro
Eur,
/// Hash
Hash,
}

#[cfg(feature = "mint")]
Expand All @@ -334,6 +336,7 @@ impl CurrencyUnit {
Self::Msat => 1,
Self::Usd => 2,
Self::Eur => 3,
Self::Hash => 4,
}
}
}
Expand All @@ -346,6 +349,7 @@ impl FromStr for CurrencyUnit {
"msat" => Ok(Self::Msat),
"usd" => Ok(Self::Usd),
"eur" => Ok(Self::Eur),
"hash" => Ok(Self::Hash),
_ => Err(Error::UnsupportedUnit),
}
}
Expand All @@ -358,6 +362,7 @@ impl fmt::Display for CurrencyUnit {
CurrencyUnit::Msat => write!(f, "msat"),
CurrencyUnit::Usd => write!(f, "usd"),
CurrencyUnit::Eur => write!(f, "eur"),
CurrencyUnit::Hash => write!(f, "hash"),
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions crates/cdk/src/nuts/nut02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ impl Id {
id: bytes[1..].try_into()?,
})
}

/// [`Id`] to u64
pub fn to_u64(&self) -> u64 {
let bytes = self.to_bytes();
let mut array = [0u8; 8];
array[..bytes.len()].copy_from_slice(&bytes);
u64::from_be_bytes(array)
}

/// [`Id`] from u64
pub fn from_u64(value: u64) -> Result<Self, Error> {
let bytes = value.to_be_bytes();
Self::from_bytes(&bytes)
}
}

impl TryFrom<Id> for u64 {
Expand Down
76 changes: 61 additions & 15 deletions crates/cdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,60 @@ impl Wallet {
Ok(total_amount)
}

/// Generates blinded secrets to send to the mint for signing. This function
/// is appropriate if the caller is providing their own network
/// transport. Otherwise use `mint`, which makes a network request to
/// the mint.
///
/// # Parameters
///
/// - `&self`: A reference to the current instance
/// - `active_keyset_id`: The ID of the active keyset
/// - `quote_info_amount`: The amount to be minted
/// - `amount_split_target`: Strategy for splitting amount into discrete
/// tokens
/// - `spending_conditions`: Optional spending conditions to apply to the
/// minted tokens
/// - `count`: How many tokens were previously generated from this keyset +
/// 1
///
/// # Returns
///
/// A `Result` containing `PreMintSecrets` if successful, or an `Error`
/// otherwise.
///
/// # Errors
///
/// This function will return an error if the creation of `PreMintSecrets`
/// fails.
///
/// ```
pub fn generate_premint_secrets(
&self,
active_keyset_id: Id,
quote_info_amount: Amount,
amount_split_target: &SplitTarget,
spending_conditions: Option<&SpendingConditions>,
count: u32,
) -> Result<PreMintSecrets, Error> {
// Move the match logic into this function.
match spending_conditions {
Some(spending_conditions) => Ok(PreMintSecrets::with_conditions(
active_keyset_id,
quote_info_amount,
amount_split_target,
spending_conditions,
)?),
None => Ok(PreMintSecrets::from_xpriv(
active_keyset_id,
count,
self.xpriv,
quote_info_amount,
amount_split_target,
)?),
}
}

/// Mint
/// # Synopsis
/// ```rust
Expand Down Expand Up @@ -632,21 +686,13 @@ impl Wallet {

let count = count.map_or(0, |c| c + 1);

let premint_secrets = match &spending_conditions {
Some(spending_conditions) => PreMintSecrets::with_conditions(
active_keyset_id,
quote_info.amount,
&amount_split_target,
spending_conditions,
)?,
None => PreMintSecrets::from_xpriv(
active_keyset_id,
count,
self.xpriv,
quote_info.amount,
&amount_split_target,
)?,
};
let premint_secrets = self.generate_premint_secrets(
active_keyset_id,
quote_info.amount,
&amount_split_target,
spending_conditions.as_ref(),
count,
)?;

let mint_res = self
.client
Expand Down
6 changes: 4 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@
pname = "flexbox-multibuild";
src = rustSrc;
}).overrideArgs commonArgs;
in rec {
in
rec {
workspaceDeps = craneLib.buildWorkspaceDepsOnly { };
workspaceBuild =
craneLib.buildWorkspace { cargoArtifacts = workspaceDeps; };
});
in {
in
{
devShells = flakeboxLib.mkShells {
toolchain = toolchainNative;
packages = [ ];
Expand Down