mirror of
https://github.com/aljazceru/cdk.git
synced 2026-01-08 15:35:35 +01:00
feat(bindings): HTLC conditions
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
use std::ops::Deref;
|
||||
|
||||
use cdk::nuts::HTLCWitness;
|
||||
use cdk::nuts::{HTLCWitness, SpendingConditions};
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
use super::JsConditions;
|
||||
use crate::error::{into_err, Result};
|
||||
|
||||
#[wasm_bindgen(js_name = HTLCWitness)]
|
||||
pub struct JsHTLCWitness {
|
||||
inner: HTLCWitness,
|
||||
@@ -20,3 +23,26 @@ impl From<HTLCWitness> for JsHTLCWitness {
|
||||
JsHTLCWitness { inner }
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = HTLCSpendingConditions)]
|
||||
pub struct JsHTLCSpendingConditions {
|
||||
inner: SpendingConditions,
|
||||
}
|
||||
|
||||
impl Deref for JsHTLCSpendingConditions {
|
||||
type Target = SpendingConditions;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_class = HTLCSpendingConditions)]
|
||||
impl JsHTLCSpendingConditions {
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new(preimage: String, conditions: JsConditions) -> Result<JsHTLCSpendingConditions> {
|
||||
Ok(Self {
|
||||
inner: SpendingConditions::new_htlc(preimage, conditions.deref().clone())
|
||||
.map_err(into_err)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ use wasm_bindgen::prelude::*;
|
||||
|
||||
use crate::error::{into_err, Result};
|
||||
use crate::nuts::nut11::JsP2PKSpendingConditions;
|
||||
use crate::nuts::nut14::JsHTLCSpendingConditions;
|
||||
use crate::nuts::{JsCurrencyUnit, JsMintInfo};
|
||||
use crate::types::melt_quote::JsMeltQuote;
|
||||
use crate::types::{JsAmount, JsMelted, JsMintQuote};
|
||||
@@ -157,7 +158,19 @@ impl JsWallet {
|
||||
memo: Option<String>,
|
||||
amount: u64,
|
||||
p2pk_condition: Option<JsP2PKSpendingConditions>,
|
||||
htlc_condition: Option<JsHTLCSpendingConditions>,
|
||||
) -> Result<String> {
|
||||
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,
|
||||
};
|
||||
|
||||
let mint_url = UncheckedUrl::from_str(&mint_url).map_err(into_err)?;
|
||||
|
||||
self.inner
|
||||
@@ -166,7 +179,7 @@ impl JsWallet {
|
||||
&unit.into(),
|
||||
memo,
|
||||
Amount::from(amount),
|
||||
p2pk_condition.map(|c| c.deref().clone()),
|
||||
conditions,
|
||||
)
|
||||
.await
|
||||
.map_err(into_err)
|
||||
|
||||
Reference in New Issue
Block a user