ws.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //! Websocket types and functions for the CDK.
  2. //!
  3. //! This module extends the `cashu` crate with types and functions for the CDK, using the correct
  4. //! expected ID types.
  5. #[cfg(feature = "mint")]
  6. use cashu::nut17::ws::JSON_RPC_VERSION;
  7. use cashu::nut17::{self};
  8. #[cfg(feature = "mint")]
  9. use cashu::NotificationPayload;
  10. #[cfg(feature = "mint")]
  11. use uuid::Uuid;
  12. use crate::pub_sub::SubId;
  13. /// Request to unsubscribe from a websocket subscription
  14. pub type WsUnsubscribeRequest = nut17::ws::WsUnsubscribeRequest<SubId>;
  15. /// Notification message sent over websocket
  16. pub type WsNotification = nut17::ws::WsNotification<SubId>;
  17. /// Response to a subscription request
  18. pub type WsSubscribeResponse = nut17::ws::WsSubscribeResponse<SubId>;
  19. /// Result part of a websocket response
  20. pub type WsResponseResult = nut17::ws::WsResponseResult<SubId>;
  21. /// Response to an unsubscribe request
  22. pub type WsUnsubscribeResponse = nut17::ws::WsUnsubscribeResponse<SubId>;
  23. /// Generic websocket request
  24. pub type WsRequest = nut17::ws::WsRequest<SubId>;
  25. /// Generic websocket response
  26. pub type WsResponse = nut17::ws::WsResponse<SubId>;
  27. /// Method-specific websocket request
  28. pub type WsMethodRequest = nut17::ws::WsMethodRequest<SubId>;
  29. /// Error body for websocket responses
  30. pub type WsErrorBody = nut17::ws::WsErrorBody;
  31. /// Either a websocket message or a response
  32. pub type WsMessageOrResponse = nut17::ws::WsMessageOrResponse<SubId>;
  33. /// Inner content of a notification with generic payload type
  34. pub type NotificationInner<T> = nut17::ws::NotificationInner<T, SubId>;
  35. #[cfg(feature = "mint")]
  36. /// Converts a notification with UUID identifiers to a notification with string identifiers
  37. pub fn notification_uuid_to_notification_string(
  38. notification: NotificationInner<Uuid>,
  39. ) -> NotificationInner<String> {
  40. nut17::ws::NotificationInner {
  41. sub_id: notification.sub_id,
  42. payload: match notification.payload {
  43. NotificationPayload::ProofState(pk) => NotificationPayload::ProofState(pk),
  44. NotificationPayload::MeltQuoteBolt11Response(quote) => {
  45. NotificationPayload::MeltQuoteBolt11Response(quote.to_string_id())
  46. }
  47. NotificationPayload::MintQuoteBolt11Response(quote) => {
  48. NotificationPayload::MintQuoteBolt11Response(quote.to_string_id())
  49. }
  50. },
  51. }
  52. }
  53. #[cfg(feature = "mint")]
  54. /// Converts a notification to a websocket message that can be sent to clients
  55. pub fn notification_to_ws_message(notification: NotificationInner<Uuid>) -> WsMessageOrResponse {
  56. nut17::ws::WsMessageOrResponse::Notification(nut17::ws::WsNotification {
  57. jsonrpc: JSON_RPC_VERSION.to_owned(),
  58. method: "subscribe".to_string(),
  59. params: notification_uuid_to_notification_string(notification),
  60. })
  61. }