Преглед изворни кода

refactor: Keyset_id in proof is required

thesimplekid пре 1 година
родитељ
комит
ff0fead2c0

+ 2 - 2
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 {

+ 6 - 11
bindings/cashu-ffi/src/nuts/nut00/proof.rs

@@ -18,18 +18,13 @@ impl Deref for Proof {
 }
 
 impl Proof {
-    pub fn new(
-        amount: Arc<Amount>,
-        secret: Arc<Secret>,
-        c: Arc<PublicKey>,
-        id: Option<Arc<Id>>,
-    ) -> Self {
+    pub fn new(amount: Arc<Amount>, secret: Arc<Secret>, c: Arc<PublicKey>, id: Arc<Id>) -> 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<Arc<Id>> {
-        self.inner.id.map(|id| Arc::new(id.into()))
+    pub fn id(&self) -> Arc<Id> {
+        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<Arc<Id>> {
-            self.inner.id.map(|id| Arc::new(id.into()))
+            self.inner.id.clone().map(|id| Arc::new(id.into()))
         }
     }
 

+ 4 - 4
bindings/cashu-js/src/nuts/nut00/proof.rs

@@ -28,13 +28,13 @@ impl From<Proof> for JsProof {
 #[wasm_bindgen(js_class = Proof)]
 impl JsProof {
     #[wasm_bindgen(constructor)]
-    pub fn new(amount: JsAmount, secret: JsSecret, c: JsPublicKey, id: Option<JsId>) -> 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<JsId> {
-        self.inner.id.map(|id| id.into())
+    pub fn id(&self) -> JsId {
+        self.inner.id.into()
     }
 }

+ 3 - 3
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);

+ 10 - 15
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);

+ 1 - 1
crates/cashu-sdk/src/wallet.rs

@@ -228,7 +228,7 @@ impl<C: Client> Wallet<C> {
 
             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,

+ 1 - 1
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,

+ 5 - 8
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<Id>,
 }
 
 impl Ord for Proof {
@@ -236,7 +236,7 @@ impl From<Proof> 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()
         );