Ver Fonte

fix: handle keys response with old keyset ids

This adds a new deserializer that will skip invalid keyset ids.
This prevents an the response from erroring
if a mint returns old keysetids
thesimplekid há 11 meses atrás
pai
commit
080253aefc
2 ficheiros alterados com 29 adições e 2 exclusões
  1. 25 2
      crates/cdk/src/nuts/nut01/mod.rs
  2. 4 0
      crates/cdk/src/nuts/nut02.rs

+ 25 - 2
crates/cdk/src/nuts/nut01/mod.rs

@@ -6,7 +6,8 @@ use std::collections::BTreeMap;
 use std::ops::{Deref, DerefMut};
 
 use bitcoin::secp256k1;
-use serde::{Deserialize, Serialize};
+use serde::{de, Deserialize, Deserializer, Serialize};
+use serde_json::Value;
 use thiserror::Error;
 
 mod public_key;
@@ -66,11 +67,33 @@ impl Keys {
 }
 
 /// Mint Public Keys [NUT-01]
-#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
 pub struct KeysResponse {
     pub keysets: Vec<KeySet>,
 }
 
+impl<'de> Deserialize<'de> for KeysResponse {
+    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+    where
+        D: Deserializer<'de>,
+    {
+        let keys_response: Value = Value::deserialize(deserializer)?;
+
+        let keysets = keys_response
+            .get("keysets")
+            .ok_or(de::Error::custom("Keysets not found"))?
+            .as_array()
+            .ok_or(de::Error::custom("Keysets not found"))?;
+
+        let keysets = keysets
+            .iter()
+            .flat_map(|keyset| serde_json::from_value(keyset.clone()))
+            .collect();
+
+        Ok(KeysResponse { keysets })
+    }
+}
+
 /// Mint keys
 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 pub struct MintKeys(BTreeMap<Amount, MintKeyPair>);

Diff do ficheiro suprimidas por serem muito extensas
+ 4 - 0
crates/cdk/src/nuts/nut02.rs


Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff