Explorar el Código

Fix feature support

Add a no-op code handle for subscription creation when the crate is compiled
without the mint feature
Cesar Rodas hace 4 meses
padre
commit
2c7cb80ef8

+ 1 - 1
crates/cdk/Cargo.toml

@@ -39,7 +39,7 @@ serde_json = "1"
 serde_with = "3"
 tracing = { version = "0.1", default-features = false, features = ["attributes", "log"] }
 thiserror = "1"
-futures = { version = "0.3.28", default-features = false, optional = true }
+futures = { version = "0.3.28", default-features = false, optional = true, features = ["alloc"] }
 url = "2.3"
 utoipa = { version = "4", optional = true }
 uuid = { version = "1", features = ["v4"] }

+ 2 - 0
crates/cdk/src/nuts/mod.rs

@@ -18,6 +18,7 @@ pub mod nut12;
 pub mod nut13;
 pub mod nut14;
 pub mod nut15;
+#[cfg(feature = "mint")]
 pub mod nut17;
 pub mod nut18;
 
@@ -48,5 +49,6 @@ pub use nut11::{Conditions, P2PKWitness, SigFlag, SpendingConditions};
 pub use nut12::{BlindSignatureDleq, ProofDleq};
 pub use nut14::HTLCWitness;
 pub use nut15::{Mpp, MppMethodSettings, Settings as NUT15Settings};
+#[cfg(feature = "mint")]
 pub use nut17::{NotificationPayload, PubSubManager};
 pub use nut18::{PaymentRequest, PaymentRequestPayload, Transport};

+ 3 - 2
crates/cdk/src/nuts/nut06.rs

@@ -5,7 +5,7 @@
 use serde::{Deserialize, Deserializer, Serialize, Serializer};
 
 use super::nut01::PublicKey;
-use super::{nut04, nut05, nut15, nut17, MppMethodSettings};
+use super::{nut04, nut05, nut15, MppMethodSettings};
 
 /// Mint Version
 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -238,7 +238,8 @@ pub struct Nuts {
     /// NUT17 Settings
     #[serde(default)]
     #[serde(rename = "17")]
-    pub nut17: nut17::SupportedSettings,
+    #[cfg(feature = "mint")]
+    pub nut17: super::nut17::SupportedSettings,
 }
 
 impl Nuts {

+ 14 - 109
crates/cdk/src/nuts/nut17.rs → crates/cdk/src/nuts/nut17/mod.rs

@@ -1,15 +1,21 @@
 //! Specific Subscription for the cdk crate
 
+use super::{BlindSignature, CurrencyUnit, PaymentMethod};
+use crate::cdk_database::{self, MintDatabase};
+pub use crate::pub_sub::SubId;
 use crate::{
-    cdk_database::{self, MintDatabase},
     nuts::{
         MeltQuoteBolt11Response, MeltQuoteState, MintQuoteBolt11Response, MintQuoteState,
         ProofState,
     },
-    pub_sub::{self, Index, Indexable, OnNewSubscription, SubscriptionGlobalId},
+    pub_sub::{self, Index, Indexable, SubscriptionGlobalId},
 };
 use serde::{Deserialize, Serialize};
-use std::{collections::HashMap, ops::Deref, sync::Arc};
+use std::{ops::Deref, sync::Arc};
+
+mod on_subscription;
+
+pub use on_subscription::OnSubscription;
 
 /// Subscription Parameter according to the standard
 #[derive(Debug, Clone, Serialize, Deserialize)]
@@ -58,10 +64,6 @@ impl Default for SupportedMethods {
     }
 }
 
-pub use crate::pub_sub::SubId;
-
-use super::{BlindSignature, CurrencyUnit, PaymentMethod, PublicKey};
-
 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 #[serde(untagged)]
 /// Subscription response
@@ -145,125 +147,28 @@ impl From<Params> for Vec<Index<(String, Kind)>> {
     }
 }
 
-#[derive(Default)]
-/// Subscription Init
-///
-/// This struct triggers code when a new subscription is created.
-///
-/// It is used to send the initial state of the subscription to the client.
-pub struct SubscriptionInit(Option<Arc<dyn MintDatabase<Err = cdk_database::Error> + Send + Sync>>);
-
-#[async_trait::async_trait]
-impl OnNewSubscription for SubscriptionInit {
-    type Event = NotificationPayload;
-    type Index = (String, Kind);
-
-    async fn on_new_subscription(
-        &self,
-        request: &[&Self::Index],
-    ) -> Result<Vec<Self::Event>, String> {
-        let datastore = if let Some(localstore) = self.0.as_ref() {
-            localstore
-        } else {
-            return Ok(vec![]);
-        };
-
-        let mut to_return = vec![];
-
-        for (kind, values) in request.iter().fold(
-            HashMap::new(),
-            |mut acc: HashMap<&Kind, Vec<&String>>, (data, kind)| {
-                acc.entry(kind).or_default().push(data);
-                acc
-            },
-        ) {
-            match kind {
-                Kind::Bolt11MeltQuote => {
-                    let queries = values
-                        .iter()
-                        .map(|id| datastore.get_melt_quote(id))
-                        .collect::<Vec<_>>();
-
-                    to_return.extend(
-                        futures::future::try_join_all(queries)
-                            .await
-                            .map(|quotes| {
-                                quotes
-                                    .into_iter()
-                                    .filter_map(|quote| quote.map(|x| x.into()))
-                                    .map(|x: MeltQuoteBolt11Response| x.into())
-                                    .collect::<Vec<_>>()
-                            })
-                            .map_err(|e| e.to_string())?,
-                    );
-                }
-                Kind::Bolt11MintQuote => {
-                    let queries = values
-                        .iter()
-                        .map(|id| datastore.get_mint_quote(id))
-                        .collect::<Vec<_>>();
-
-                    to_return.extend(
-                        futures::future::try_join_all(queries)
-                            .await
-                            .map(|quotes| {
-                                quotes
-                                    .into_iter()
-                                    .filter_map(|quote| quote.map(|x| x.into()))
-                                    .map(|x: MintQuoteBolt11Response| x.into())
-                                    .collect::<Vec<_>>()
-                            })
-                            .map_err(|e| e.to_string())?,
-                    );
-                }
-                Kind::ProofState => {
-                    let public_keys = values
-                        .iter()
-                        .map(PublicKey::from_hex)
-                        .collect::<Result<Vec<PublicKey>, _>>()
-                        .map_err(|e| e.to_string())?;
-
-                    to_return.extend(
-                        datastore
-                            .get_proofs_states(&public_keys)
-                            .await
-                            .map_err(|e| e.to_string())?
-                            .into_iter()
-                            .enumerate()
-                            .filter_map(|(idx, state)| {
-                                state.map(|state| (public_keys[idx], state).into())
-                            })
-                            .map(|state: ProofState| state.into()),
-                    );
-                }
-            }
-        }
-
-        Ok(to_return)
-    }
-}
-
 /// Manager
 /// Publish–subscribe manager
 ///
 /// Nut-17 implementation is system-wide and not only through the WebSocket, so
 /// it is possible for another part of the system to subscribe to events.
-pub struct PubSubManager(pub_sub::Manager<NotificationPayload, (String, Kind), SubscriptionInit>);
+pub struct PubSubManager(pub_sub::Manager<NotificationPayload, (String, Kind), OnSubscription>);
 
+#[allow(clippy::default_constructed_unit_structs)]
 impl Default for PubSubManager {
     fn default() -> Self {
-        PubSubManager(SubscriptionInit::default().into())
+        PubSubManager(OnSubscription::default().into())
     }
 }
 
 impl From<Arc<dyn MintDatabase<Err = cdk_database::Error> + Send + Sync>> for PubSubManager {
     fn from(val: Arc<dyn MintDatabase<Err = cdk_database::Error> + Send + Sync>) -> Self {
-        PubSubManager(SubscriptionInit(Some(val)).into())
+        PubSubManager(OnSubscription(Some(val)).into())
     }
 }
 
 impl Deref for PubSubManager {
-    type Target = pub_sub::Manager<NotificationPayload, (String, Kind), SubscriptionInit>;
+    type Target = pub_sub::Manager<NotificationPayload, (String, Kind), OnSubscription>;
 
     fn deref(&self) -> &Self::Target {
         &self.0

+ 110 - 0
crates/cdk/src/nuts/nut17/on_subscription.rs

@@ -0,0 +1,110 @@
+//! On Subscription
+//!
+//! This module contains the code that is triggered when a new subscription is created.
+use super::{Kind, NotificationPayload};
+use crate::{
+    cdk_database::{self, MintDatabase},
+    nuts::{MeltQuoteBolt11Response, MintQuoteBolt11Response, ProofState, PublicKey},
+    pub_sub::OnNewSubscription,
+};
+use std::{collections::HashMap, sync::Arc};
+
+#[derive(Default)]
+/// Subscription Init
+///
+/// This struct triggers code when a new subscription is created.
+///
+/// It is used to send the initial state of the subscription to the client.
+pub struct OnSubscription(
+    pub(crate) Option<Arc<dyn MintDatabase<Err = cdk_database::Error> + Send + Sync>>,
+);
+
+#[async_trait::async_trait]
+impl OnNewSubscription for OnSubscription {
+    type Event = NotificationPayload;
+    type Index = (String, Kind);
+
+    async fn on_new_subscription(
+        &self,
+        request: &[&Self::Index],
+    ) -> Result<Vec<Self::Event>, String> {
+        let datastore = if let Some(localstore) = self.0.as_ref() {
+            localstore
+        } else {
+            return Ok(vec![]);
+        };
+
+        let mut to_return = vec![];
+
+        for (kind, values) in request.iter().fold(
+            HashMap::new(),
+            |mut acc: HashMap<&Kind, Vec<&String>>, (data, kind)| {
+                acc.entry(kind).or_default().push(data);
+                acc
+            },
+        ) {
+            match kind {
+                Kind::Bolt11MeltQuote => {
+                    let queries = values
+                        .iter()
+                        .map(|id| datastore.get_melt_quote(id))
+                        .collect::<Vec<_>>();
+
+                    to_return.extend(
+                        futures::future::try_join_all(queries)
+                            .await
+                            .map(|quotes| {
+                                quotes
+                                    .into_iter()
+                                    .filter_map(|quote| quote.map(|x| x.into()))
+                                    .map(|x: MeltQuoteBolt11Response| x.into())
+                                    .collect::<Vec<_>>()
+                            })
+                            .map_err(|e| e.to_string())?,
+                    );
+                }
+                Kind::Bolt11MintQuote => {
+                    let queries = values
+                        .iter()
+                        .map(|id| datastore.get_mint_quote(id))
+                        .collect::<Vec<_>>();
+
+                    to_return.extend(
+                        futures::future::try_join_all(queries)
+                            .await
+                            .map(|quotes| {
+                                quotes
+                                    .into_iter()
+                                    .filter_map(|quote| quote.map(|x| x.into()))
+                                    .map(|x: MintQuoteBolt11Response| x.into())
+                                    .collect::<Vec<_>>()
+                            })
+                            .map_err(|e| e.to_string())?,
+                    );
+                }
+                Kind::ProofState => {
+                    let public_keys = values
+                        .iter()
+                        .map(PublicKey::from_hex)
+                        .collect::<Result<Vec<PublicKey>, _>>()
+                        .map_err(|e| e.to_string())?;
+
+                    to_return.extend(
+                        datastore
+                            .get_proofs_states(&public_keys)
+                            .await
+                            .map_err(|e| e.to_string())?
+                            .into_iter()
+                            .enumerate()
+                            .filter_map(|(idx, state)| {
+                                state.map(|state| (public_keys[idx], state).into())
+                            })
+                            .map(|state: ProofState| state.into()),
+                    );
+                }
+            }
+        }
+
+        Ok(to_return)
+    }
+}