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