subscription.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //! Subscription types and traits
  2. use std::ops::Deref;
  3. use std::str::FromStr;
  4. use std::sync::Arc;
  5. use cashu::nut17::{self, Kind, NotificationId};
  6. use cashu::quote_id::QuoteId;
  7. use cashu::PublicKey;
  8. use serde::{Deserialize, Serialize};
  9. use crate::pub_sub::{Error, SubscriptionRequest};
  10. /// CDK/Mint Subscription parameters.
  11. ///
  12. /// This is a concrete type alias for `nut17::Params<SubId>`.
  13. pub type Params = nut17::Params<Arc<SubId>>;
  14. impl SubscriptionRequest for Params {
  15. type Topic = NotificationId<QuoteId>;
  16. type SubscriptionId = SubId;
  17. fn subscription_name(&self) -> Arc<Self::SubscriptionId> {
  18. self.id.clone()
  19. }
  20. fn try_get_topics(&self) -> Result<Vec<Self::Topic>, Error> {
  21. self.filters
  22. .iter()
  23. .map(|filter| match self.kind {
  24. Kind::Bolt11MeltQuote => QuoteId::from_str(filter)
  25. .map(NotificationId::MeltQuoteBolt11)
  26. .map_err(|_| Error::ParsingError(filter.to_owned())),
  27. Kind::Bolt11MintQuote => QuoteId::from_str(filter)
  28. .map(NotificationId::MintQuoteBolt11)
  29. .map_err(|_| Error::ParsingError(filter.to_owned())),
  30. Kind::ProofState => PublicKey::from_str(filter)
  31. .map(NotificationId::ProofState)
  32. .map_err(|_| Error::ParsingError(filter.to_owned())),
  33. Kind::Bolt12MintQuote => QuoteId::from_str(filter)
  34. .map(NotificationId::MintQuoteBolt12)
  35. .map_err(|_| Error::ParsingError(filter.to_owned())),
  36. })
  37. .collect::<Result<Vec<_>, _>>()
  38. }
  39. }
  40. /// Subscriptions parameters for the wallet
  41. ///
  42. /// This is because the Wallet can subscribe to non CDK quotes, where IDs are not constraint to
  43. /// QuoteId
  44. pub type WalletParams = nut17::Params<Arc<String>>;
  45. impl SubscriptionRequest for WalletParams {
  46. type Topic = NotificationId<String>;
  47. type SubscriptionId = String;
  48. fn subscription_name(&self) -> Arc<Self::SubscriptionId> {
  49. self.id.clone()
  50. }
  51. fn try_get_topics(&self) -> Result<Vec<Self::Topic>, Error> {
  52. self.filters
  53. .iter()
  54. .map(|filter| {
  55. Ok(match self.kind {
  56. Kind::Bolt11MeltQuote => NotificationId::MeltQuoteBolt11(filter.to_owned()),
  57. Kind::Bolt11MintQuote => NotificationId::MintQuoteBolt11(filter.to_owned()),
  58. Kind::ProofState => PublicKey::from_str(filter)
  59. .map(NotificationId::ProofState)
  60. .map_err(|_| Error::ParsingError(filter.to_owned()))?,
  61. Kind::Bolt12MintQuote => NotificationId::MintQuoteBolt12(filter.to_owned()),
  62. })
  63. })
  64. .collect::<Result<Vec<_>, _>>()
  65. }
  66. }
  67. /// Subscription Id wrapper
  68. ///
  69. /// This is the place to add some sane default (like a max length) to the
  70. /// subscription ID
  71. #[derive(Debug, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
  72. pub struct SubId(String);
  73. impl From<&str> for SubId {
  74. fn from(s: &str) -> Self {
  75. Self(s.to_string())
  76. }
  77. }
  78. impl From<String> for SubId {
  79. fn from(s: String) -> Self {
  80. Self(s)
  81. }
  82. }
  83. impl FromStr for SubId {
  84. type Err = ();
  85. fn from_str(s: &str) -> Result<Self, Self::Err> {
  86. Ok(Self(s.to_string()))
  87. }
  88. }
  89. impl Deref for SubId {
  90. type Target = String;
  91. fn deref(&self) -> &Self::Target {
  92. &self.0
  93. }
  94. }