cdk_common/
ws.rs

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