diff --git a/bindings/cashu-ffi/src/cashu.udl b/bindings/cashu-ffi/src/cashu.udl index 7147d9d7..5281bc07 100644 --- a/bindings/cashu-ffi/src/cashu.udl +++ b/bindings/cashu-ffi/src/cashu.udl @@ -56,11 +56,11 @@ interface BlindedMessage { }; interface Proof { - constructor(Amount amount, Secret secret, PublicKey c, Id? id); + constructor(Amount amount, Secret secret, PublicKey c, Id id); Amount amount(); Secret secret(); PublicKey c(); - Id? id(); + Id id(); }; interface BlindedSignature { diff --git a/bindings/cashu-ffi/src/nuts/nut00/proof.rs b/bindings/cashu-ffi/src/nuts/nut00/proof.rs index 65da1a99..9cd408b5 100644 --- a/bindings/cashu-ffi/src/nuts/nut00/proof.rs +++ b/bindings/cashu-ffi/src/nuts/nut00/proof.rs @@ -18,18 +18,13 @@ impl Deref for Proof { } impl Proof { - pub fn new( - amount: Arc, - secret: Arc, - c: Arc, - id: Option>, - ) -> Self { + pub fn new(amount: Arc, secret: Arc, c: Arc, id: Arc) -> Self { Self { inner: ProofSdk { amount: *amount.as_ref().deref(), secret: secret.as_ref().deref().clone(), c: c.as_ref().deref().clone(), - id: id.map(|id| *id.as_ref().deref()), + id: *id.as_ref().deref(), }, } } @@ -46,8 +41,8 @@ impl Proof { Arc::new(self.inner.c.clone().into()) } - pub fn id(&self) -> Option> { - self.inner.id.map(|id| Arc::new(id.into())) + pub fn id(&self) -> Arc { + Arc::new(self.id.into()) } } @@ -57,7 +52,7 @@ impl From<&Proof> for ProofSdk { amount: *proof.amount().as_ref().deref(), secret: proof.secret().as_ref().deref().clone(), c: proof.c().deref().into(), - id: proof.id().map(|id| *id.as_ref().deref()), + id: proof.id, } } } @@ -118,7 +113,7 @@ pub mod mint { } pub fn id(&self) -> Option> { - self.inner.id.map(|id| Arc::new(id.into())) + self.inner.id.clone().map(|id| Arc::new(id.into())) } } diff --git a/bindings/cashu-js/src/nuts/nut00/proof.rs b/bindings/cashu-js/src/nuts/nut00/proof.rs index 6ed34581..671a2ded 100644 --- a/bindings/cashu-js/src/nuts/nut00/proof.rs +++ b/bindings/cashu-js/src/nuts/nut00/proof.rs @@ -28,13 +28,13 @@ impl From for JsProof { #[wasm_bindgen(js_class = Proof)] impl JsProof { #[wasm_bindgen(constructor)] - pub fn new(amount: JsAmount, secret: JsSecret, c: JsPublicKey, id: Option) -> JsProof { + pub fn new(amount: JsAmount, secret: JsSecret, c: JsPublicKey, id: JsId) -> JsProof { Self { inner: Proof { amount: *amount.deref(), secret: secret.deref().clone(), c: c.deref().clone(), - id: id.map(|i| *i.deref()), + id: *id.deref(), }, } } @@ -59,7 +59,7 @@ impl JsProof { /// Id #[wasm_bindgen(getter)] - pub fn id(&self) -> Option { - self.inner.id.map(|id| id.into()) + pub fn id(&self) -> JsId { + self.inner.id.into() } } diff --git a/bindings/cashu-sdk-ffi/src/cashu_sdk.udl b/bindings/cashu-sdk-ffi/src/cashu_sdk.udl index ec4ac1db..708c2b8e 100644 --- a/bindings/cashu-sdk-ffi/src/cashu_sdk.udl +++ b/bindings/cashu-sdk-ffi/src/cashu_sdk.udl @@ -59,12 +59,12 @@ interface BlindedMessage { }; interface Proof { - constructor(Amount amount, Secret secret, PublicKey c, Id? id); + constructor(Amount amount, Secret secret, PublicKey c, Id id); Amount amount(); Secret secret(); PublicKey c(); - Id? id(); -}; + Id id(); + }; interface BlindedSignature { constructor(Id id, Amount amount, PublicKey c); diff --git a/crates/cashu-sdk/src/mint.rs b/crates/cashu-sdk/src/mint.rs index 1bcd1dce..344075cf 100644 --- a/crates/cashu-sdk/src/mint.rs +++ b/crates/cashu-sdk/src/mint.rs @@ -190,21 +190,16 @@ impl Mint { return Err(Error::TokenSpent); } - let keyset = proof.id.as_ref().map_or_else( - || self.active_keyset.clone(), - |id| { - if let Some(keyset) = self.inactive_keysets.get(id) { - nut02::mint::KeySet::generate( - &self.secret, - &keyset.symbol, - &keyset.derivation_path, - keyset.max_order, - ) - } else { - self.active_keyset.clone() - } - }, - ); + let keyset = if let Some(keyset) = self.inactive_keysets.get(&proof.id) { + nut02::mint::KeySet::generate( + &self.secret, + &keyset.symbol, + &keyset.derivation_path, + keyset.max_order, + ) + } else { + self.active_keyset.clone() + }; let Some(keypair) = keyset.keys.0.get(&proof.amount) else { return Err(Error::AmountKey); diff --git a/crates/cashu-sdk/src/wallet.rs b/crates/cashu-sdk/src/wallet.rs index fedd01f1..daf8bab5 100644 --- a/crates/cashu-sdk/src/wallet.rs +++ b/crates/cashu-sdk/src/wallet.rs @@ -228,7 +228,7 @@ impl Wallet { let unblinded_sig = unblind_message(blinded_c, rs[i].clone().into(), a).unwrap(); let proof = Proof { - id: Some(promise.id), + id: promise.id, amount: promise.amount, secret: secrets[i].clone(), c: unblinded_sig, diff --git a/crates/cashu/src/dhke.rs b/crates/cashu/src/dhke.rs index 0bbc4f67..546c99b5 100644 --- a/crates/cashu/src/dhke.rs +++ b/crates/cashu/src/dhke.rs @@ -89,7 +89,7 @@ mod wallet { let unblinded_signature = unblind_message(blinded_c, rs[i].clone().into(), a)?; let proof = Proof { - id: Some(promise.id), + id: promise.id, amount: promise.amount, secret: secrets[i].clone(), c: unblinded_signature, diff --git a/crates/cashu/src/nuts/nut00.rs b/crates/cashu/src/nuts/nut00.rs index efadca73..4fba8a0b 100644 --- a/crates/cashu/src/nuts/nut00.rs +++ b/crates/cashu/src/nuts/nut00.rs @@ -207,6 +207,8 @@ pub struct BlindedSignature { /// Proofs [NUT-00] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Proof { + /// `Keyset id` + pub id: Id, /// Amount in satoshi pub amount: Amount, /// Secret message @@ -214,8 +216,6 @@ pub struct Proof { /// Unblinded signature #[serde(rename = "C")] pub c: PublicKey, - /// `Keyset id` - pub id: Option, } impl Ord for Proof { @@ -236,7 +236,7 @@ impl From for mint::Proof { amount: Some(proof.amount), secret: proof.secret, c: Some(proof.c), - id: proof.id, + id: Some(proof.id), } } } @@ -283,10 +283,7 @@ mod tests { let proof = "[{\"id\":\"DSAl9nvvyfva\",\"amount\":2,\"secret\":\"EhpennC9qB3iFlW8FZ_pZw\",\"C\":\"02c020067db727d586bc3183aecf97fcb800c3f4cc4759f69c626c9db5d8f5b5d4\"},{\"id\":\"DSAl9nvvyfva\",\"amount\":8,\"secret\":\"TmS6Cv0YT5PU_5ATVKnukw\",\"C\":\"02ac910bef28cbe5d7325415d5c263026f15f9b967a079ca9779ab6e5c2db133a7\"}]"; let proof: Proofs = serde_json::from_str(proof).unwrap(); - assert_eq!( - proof[0].clone().id.unwrap(), - Id::from_str("DSAl9nvvyfva").unwrap() - ); + assert_eq!(proof[0].clone().id, Id::from_str("DSAl9nvvyfva").unwrap()); } #[test] @@ -300,7 +297,7 @@ mod tests { UncheckedUrl::from_str("https://8333.space:3338").unwrap() ); assert_eq!( - token.token[0].proofs[0].clone().id.unwrap(), + token.token[0].proofs[0].clone().id, Id::from_str("DSAl9nvvyfva").unwrap() );