//! Websocket types and functions for the CDK. //! //! This module extends the `cashu` crate with types and functions for the CDK, using the correct //! expected ID types. #[cfg(feature = "mint")] use cashu::nut17::ws::JSON_RPC_VERSION; use cashu::nut17::{self}; #[cfg(feature = "mint")] use cashu::NotificationPayload; #[cfg(feature = "mint")] use uuid::Uuid; use crate::pub_sub::SubId; /// Request to unsubscribe from a websocket subscription pub type WsUnsubscribeRequest = nut17::ws::WsUnsubscribeRequest; /// Notification message sent over websocket pub type WsNotification = nut17::ws::WsNotification; /// Response to a subscription request pub type WsSubscribeResponse = nut17::ws::WsSubscribeResponse; /// Result part of a websocket response pub type WsResponseResult = nut17::ws::WsResponseResult; /// Response to an unsubscribe request pub type WsUnsubscribeResponse = nut17::ws::WsUnsubscribeResponse; /// Generic websocket request pub type WsRequest = nut17::ws::WsRequest; /// Generic websocket response pub type WsResponse = nut17::ws::WsResponse; /// Method-specific websocket request pub type WsMethodRequest = nut17::ws::WsMethodRequest; /// Error body for websocket responses pub type WsErrorBody = nut17::ws::WsErrorBody; /// Either a websocket message or a response pub type WsMessageOrResponse = nut17::ws::WsMessageOrResponse; /// Inner content of a notification with generic payload type pub type NotificationInner = nut17::ws::NotificationInner; #[cfg(feature = "mint")] /// Converts a notification with UUID identifiers to a notification with string identifiers pub fn notification_uuid_to_notification_string( notification: NotificationInner, ) -> NotificationInner { nut17::ws::NotificationInner { sub_id: notification.sub_id, payload: match notification.payload { NotificationPayload::ProofState(pk) => NotificationPayload::ProofState(pk), NotificationPayload::MeltQuoteBolt11Response(quote) => { NotificationPayload::MeltQuoteBolt11Response(quote.to_string_id()) } NotificationPayload::MintQuoteBolt11Response(quote) => { NotificationPayload::MintQuoteBolt11Response(quote.to_string_id()) } }, } } #[cfg(feature = "mint")] /// Converts a notification to a websocket message that can be sent to clients pub fn notification_to_ws_message(notification: NotificationInner) -> WsMessageOrResponse { nut17::ws::WsMessageOrResponse::Notification(nut17::ws::WsNotification { jsonrpc: JSON_RPC_VERSION.to_owned(), method: "subscribe".to_string(), params: notification_uuid_to_notification_string(notification), }) }