Skip to content

Commit

Permalink
feat: refactor generate premint secrets into a separate function
Browse files Browse the repository at this point in the history
This change is intended to enable devs who want to provide their own
network transport to get blinded secrets without making a network call.

The nix flake changes were implemented by the code formatter.
  • Loading branch information
vnprc committed Aug 31, 2024
1 parent 847eab8 commit 05b8ecf
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 17 deletions.
79 changes: 64 additions & 15 deletions crates/cdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,62 @@ 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
/// - `xpriv`: The extended private key used for generating secrets
///
/// # 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,
xpriv: ExtendedPrivKey,
) -> 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,
xpriv,
quote_info_amount,
amount_split_target,
)?),
}
}

/// Mint
/// # Synopsis
/// ```rust
Expand Down Expand Up @@ -632,21 +688,14 @@ 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,
self.xpriv,
)?;

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

0 comments on commit 05b8ecf

Please sign in to comment.