ws.rs 3.0 KB

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