Browse Source

feat: skip errors on keyset response

This is needed in the case that a non v1 keyset id is returned
in a response. They are simply ignored.
thesimplekid 1 year ago
parent
commit
d0d3a6732f
3 changed files with 22 additions and 1 deletions
  1. 1 0
      crates/cashu/Cargo.toml
  2. 3 0
      crates/cashu/src/nuts/nut01.rs
  3. 18 1
      crates/cashu/src/nuts/nut02.rs

+ 1 - 0
crates/cashu/Cargo.toml

@@ -34,6 +34,7 @@ rand = "0.8.5"
 getrandom = { version = "0.2", features = ["js"] }
 serde = { workspace = true }
 serde_json = { workspace = true }
+serde_with = "3.4.0"
 url = { workspace = true }
 regex = "1.8.4"
 itertools = "0.11.0"

+ 3 - 0
crates/cashu/src/nuts/nut01.rs

@@ -7,6 +7,7 @@ use std::str::FromStr;
 use bip32::{DerivationPath, XPrv};
 use bip39::Mnemonic;
 use serde::{Deserialize, Serialize};
+use serde_with::{serde_as, VecSkipError};
 
 use super::{Id, KeySet};
 use crate::error::Error;
@@ -143,8 +144,10 @@ impl Keys {
 }
 
 /// Mint Public Keys [NUT-01]
+#[serde_as]
 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 pub struct KeysResponse {
+    #[serde_as(as = "VecSkipError<_>")]
     pub keysets: Vec<KeySet>,
 }
 

+ 18 - 1
crates/cashu/src/nuts/nut02.rs

@@ -7,6 +7,7 @@ use std::str::FromStr;
 use bitcoin::hashes::{sha256, Hash};
 use itertools::Itertools;
 use serde::{Deserialize, Serialize};
+use serde_with::{serde_as, VecSkipError};
 use thiserror::Error;
 
 use super::nut01::Keys;
@@ -157,9 +158,11 @@ impl From<&Keys> for Id {
 
 /// Mint Keysets [NUT-02]
 /// Ids of mints keyset ids
+#[serde_as]
 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 pub struct KeysetResponse {
     /// set of public key ids that the mint generates
+    #[serde_as(as = "VecSkipError<_>")]
     pub keysets: Vec<KeySetInfo>,
 }
 
@@ -290,7 +293,7 @@ mod test {
 
     use std::str::FromStr;
 
-    use super::Keys;
+    use super::{KeySetInfo, Keys, KeysetResponse};
     use crate::nuts::nut02::Id;
 
     const SHORT_KEYSET_ID: &str = "00456a94ab4e1c46";
@@ -389,4 +392,18 @@ mod test {
 
         assert_eq!(id, Id::from_str(KEYSET_ID).unwrap());
     }
+
+    #[test]
+    fn de_keyset_info() {
+        let h = r#"{"id":"009a1f293253e41e","unit":"sat","active":true}"#;
+
+        let _keyset_response: KeySetInfo = serde_json::from_str(h).unwrap();
+    }
+
+    #[test]
+    fn test_deserialization_of_keyset_response() {
+        let h = r#"{"keysets":[{"id":"009a1f293253e41e","unit":"sat","active":true},{"id":"eGnEWtdJ0PIM","unit":"sat","active":true},{"id":"003dfdf4e5e35487","unit":"sat","active":true},{"id":"0066ad1a4b6fc57c","unit":"sat","active":true},{"id":"00f7ca24d44c3e5e","unit":"sat","active":true},{"id":"001fcea2931f2d85","unit":"sat","active":true},{"id":"00d095959d940edb","unit":"sat","active":true},{"id":"000d7f730d657125","unit":"sat","active":true},{"id":"0007208d861d7295","unit":"sat","active":true},{"id":"00bfdf8889b719dd","unit":"sat","active":true},{"id":"00ca9b17da045f21","unit":"sat","active":true}]}"#;
+
+        let _keyset_response: KeysetResponse = serde_json::from_str(h).unwrap();
+    }
 }