mirror of
https://github.com/aljazceru/cdk.git
synced 2026-02-07 22:26:21 +01:00
feat: mint with spending conditions
This commit is contained in:
@@ -170,16 +170,28 @@ impl JsWallet {
|
||||
&mut self,
|
||||
mint_url: String,
|
||||
quote_id: String,
|
||||
p2pk_condition: Option<JsP2PKSpendingConditions>,
|
||||
htlc_condition: Option<JsHTLCSpendingConditions>,
|
||||
split_target_amount: Option<JsAmount>,
|
||||
) -> Result<JsAmount> {
|
||||
let target = split_target_amount
|
||||
.map(|a| SplitTarget::Value(*a.deref()))
|
||||
.unwrap_or_default();
|
||||
let mint_url = UncheckedUrl::from_str(&mint_url).map_err(into_err)?;
|
||||
let conditions = match (p2pk_condition, htlc_condition) {
|
||||
(Some(_), Some(_)) => {
|
||||
return Err(JsValue::from_str(
|
||||
"Cannot define both p2pk and htlc conditions",
|
||||
));
|
||||
}
|
||||
(None, Some(htlc_condition)) => Some(htlc_condition.deref().clone()),
|
||||
(Some(p2pk_condition), None) => Some(p2pk_condition.deref().clone()),
|
||||
(None, None) => None,
|
||||
};
|
||||
|
||||
Ok(self
|
||||
.inner
|
||||
.mint(mint_url, "e_id, target)
|
||||
.mint(mint_url, "e_id, target, conditions)
|
||||
.await
|
||||
.map_err(into_err)?
|
||||
.into())
|
||||
|
||||
@@ -432,7 +432,7 @@ impl PreMintSecrets {
|
||||
keyset_id: Id,
|
||||
amount: Amount,
|
||||
amount_split_target: &SplitTarget,
|
||||
conditions: SpendingConditions,
|
||||
conditions: &SpendingConditions,
|
||||
) -> Result<Self, Error> {
|
||||
let amount_split = amount.split_targeted(amount_split_target);
|
||||
|
||||
|
||||
@@ -522,7 +522,12 @@ impl Wallet {
|
||||
|
||||
if mint_quote_response.paid {
|
||||
let amount = self
|
||||
.mint(mint_quote.mint_url, &mint_quote.id, SplitTarget::default())
|
||||
.mint(
|
||||
mint_quote.mint_url,
|
||||
&mint_quote.id,
|
||||
SplitTarget::default(),
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
total_amount += amount;
|
||||
} else if mint_quote.expiry.le(&unix_time()) {
|
||||
@@ -595,6 +600,7 @@ impl Wallet {
|
||||
mint_url: UncheckedUrl,
|
||||
quote_id: &str,
|
||||
amount_split_target: SplitTarget,
|
||||
spending_conditions: Option<SpendingConditions>,
|
||||
) -> Result<Amount, Error> {
|
||||
// Check that mint is in store of mints
|
||||
if self.localstore.get_mint(mint_url.clone()).await?.is_none() {
|
||||
@@ -622,14 +628,22 @@ impl Wallet {
|
||||
|
||||
let count = count.map_or(0, |c| c + 1);
|
||||
|
||||
let premint_secrets = PreMintSecrets::from_xpriv(
|
||||
active_keyset_id,
|
||||
count,
|
||||
self.xpriv,
|
||||
quote_info.amount,
|
||||
false,
|
||||
&amount_split_target,
|
||||
)?;
|
||||
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,
|
||||
false,
|
||||
&amount_split_target,
|
||||
)?,
|
||||
};
|
||||
|
||||
let mint_res = self
|
||||
.client
|
||||
@@ -666,10 +680,12 @@ impl Wallet {
|
||||
// Remove filled quote from store
|
||||
self.localstore.remove_mint_quote("e_info.id).await?;
|
||||
|
||||
// Update counter for keyset
|
||||
self.localstore
|
||||
.increment_keyset_counter(&active_keyset_id, proofs.len() as u32)
|
||||
.await?;
|
||||
if spending_conditions.is_none() {
|
||||
// Update counter for keyset
|
||||
self.localstore
|
||||
.increment_keyset_counter(&active_keyset_id, proofs.len() as u32)
|
||||
.await?;
|
||||
}
|
||||
|
||||
let proofs = proofs
|
||||
.into_iter()
|
||||
@@ -846,7 +862,7 @@ impl Wallet {
|
||||
active_keyset_id,
|
||||
desired_amount,
|
||||
amount_split_target,
|
||||
conditions,
|
||||
&conditions,
|
||||
)?,
|
||||
change_premint_secrets,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user