Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Nut-17 #394

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Renamed Event to NotificationPayload
  • Loading branch information
crodas committed Oct 10, 2024
commit c25266cad560ee9832ff4624b1d2e1389bbe4b57
4 changes: 2 additions & 2 deletions crates/cdk-axum/src/ws/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::MintState;
use axum::extract::ws::{Message, WebSocket};
use cdk::nuts::nut17::{Event, SubId};
use cdk::nuts::nut17::{NotificationPayload, SubId};
use futures::{
future::{self, Either},
StreamExt,
Expand Down Expand Up @@ -53,7 +53,7 @@ pub use error::WsError;
pub struct WsContext {
state: MintState,
subscriptions: HashMap<SubId, tokio::task::JoinHandle<()>>,
publisher: mpsc::Sender<(SubId, Event)>,
publisher: mpsc::Sender<(SubId, NotificationPayload)>,
}

/// Main function for websocket connections
Expand Down
8 changes: 4 additions & 4 deletions crates/cdk-axum/src/ws/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{
WsContext, WsError, JSON_RPC_VERSION,
};
use cdk::{
nuts::nut17::{Event, Params},
nuts::nut17::{NotificationPayload, Params},
subscription::SubId,
};

Expand All @@ -21,11 +21,11 @@ pub struct Notification {
#[serde(rename = "subId")]
pub sub_id: SubId,

pub payload: Event,
pub payload: NotificationPayload,
}

impl From<(SubId, Event)> for WsNotification<Notification> {
fn from((sub_id, payload): (SubId, Event)) -> Self {
impl From<(SubId, NotificationPayload)> for WsNotification<Notification> {
fn from((sub_id, payload): (SubId, NotificationPayload)) -> Self {
WsNotification {
jsonrpc: JSON_RPC_VERSION.to_owned(),
method: "subscribe".to_string(),
Expand Down
6 changes: 3 additions & 3 deletions crates/cdk/src/mint/melt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use crate::{
};

use super::{
CurrencyUnit, Event, MeltBolt11Request, MeltQuote, MeltQuoteBolt11Request,
MeltQuoteBolt11Response, Mint, PaymentMethod, PublicKey, State,
CurrencyUnit, MeltBolt11Request, MeltQuote, MeltQuoteBolt11Request, MeltQuoteBolt11Response,
Mint, NotificationPayload, PaymentMethod, PublicKey, State,
};

impl Mint {
Expand Down Expand Up @@ -133,7 +133,7 @@ impl Mint {
let quote: MeltQuoteBolt11Response = quote.into();

self.subscription_manager
.broadcast(Event::MeltQuoteBolt11Response(quote.clone()));
.broadcast(NotificationPayload::MeltQuoteBolt11Response(quote.clone()));

Ok(quote)
}
Expand Down
6 changes: 3 additions & 3 deletions crates/cdk/src/mint/mint_nut04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use tracing::instrument;
use crate::{nuts::MintQuoteState, types::LnKey, util::unix_time, Amount, Error};

use super::{
nut04, CurrencyUnit, Event, Mint, MintQuote, MintQuoteBolt11Request, MintQuoteBolt11Response,
PaymentMethod, PublicKey,
nut04, CurrencyUnit, Mint, MintQuote, MintQuoteBolt11Request, MintQuoteBolt11Response,
NotificationPayload, PaymentMethod, PublicKey,
};

impl Mint {
Expand Down Expand Up @@ -117,7 +117,7 @@ impl Mint {
let quote: MintQuoteBolt11Response = quote.into();

self.subscription_manager
.broadcast(Event::MintQuoteBolt11Response(quote.clone()));
.broadcast(NotificationPayload::MintQuoteBolt11Response(quote.clone()));

Ok(quote)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/cdk/src/nuts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ pub use nut12::{BlindSignatureDleq, ProofDleq};
pub use nut14::HTLCWitness;
pub use nut15::{Mpp, MppMethodSettings, Settings as NUT15Settings};

pub use nut17::{Event, Manager as SubscriptionManager};
pub use nut17::{NotificationPayload, Manager as SubscriptionManager};
32 changes: 16 additions & 16 deletions crates/cdk/src/nuts/nut17.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use super::BlindSignature;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
/// Subscription response
pub enum Event {
pub enum NotificationPayload {
/// Proof State
ProofState(ProofState),
/// Melt Quote Bolt11 Response
Expand All @@ -38,39 +38,39 @@ pub enum Event {
MintQuoteBolt11Response(MintQuoteBolt11Response),
}

impl From<ProofState> for Event {
fn from(proof_state: ProofState) -> Event {
Event::ProofState(proof_state)
impl From<ProofState> for NotificationPayload {
fn from(proof_state: ProofState) -> NotificationPayload {
NotificationPayload::ProofState(proof_state)
}
}

impl From<MeltQuoteBolt11Response> for Event {
fn from(melt_quote: MeltQuoteBolt11Response) -> Event {
Event::MeltQuoteBolt11Response(melt_quote)
impl From<MeltQuoteBolt11Response> for NotificationPayload {
fn from(melt_quote: MeltQuoteBolt11Response) -> NotificationPayload {
NotificationPayload::MeltQuoteBolt11Response(melt_quote)
}
}

impl From<MintQuoteBolt11Response> for Event {
fn from(mint_quote: MintQuoteBolt11Response) -> Event {
Event::MintQuoteBolt11Response(mint_quote)
impl From<MintQuoteBolt11Response> for NotificationPayload {
fn from(mint_quote: MintQuoteBolt11Response) -> NotificationPayload {
NotificationPayload::MintQuoteBolt11Response(mint_quote)
}
}

impl Indexable for Event {
impl Indexable for NotificationPayload {
type Type = (String, Kind);

fn to_indexes(&self) -> Vec<Index<Self::Type>> {
match self {
Event::ProofState(proof_state) => {
NotificationPayload::ProofState(proof_state) => {
vec![Index::from((proof_state.y.to_hex(), Kind::ProofState))]
}
Event::MeltQuoteBolt11Response(melt_quote) => {
NotificationPayload::MeltQuoteBolt11Response(melt_quote) => {
vec![Index::from((
melt_quote.quote.clone(),
Kind::Bolt11MeltQuote,
))]
}
Event::MintQuoteBolt11Response(mint_quote) => {
NotificationPayload::MintQuoteBolt11Response(mint_quote) => {
vec![Index::from((
mint_quote.quote.clone(),
Kind::Bolt11MintQuote,
Expand Down Expand Up @@ -113,10 +113,10 @@ impl From<Params> for Vec<Index<(String, Kind)>> {
/// Subscription Manager
///
/// This is the Subscription Manager for the cdk crate
pub struct Manager(subscription::Manager<Event, (String, Kind)>);
pub struct Manager(subscription::Manager<NotificationPayload, (String, Kind)>);

impl Deref for Manager {
type Target = subscription::Manager<Event, (String, Kind)>;
type Target = subscription::Manager<NotificationPayload, (String, Kind)>;

fn deref(&self) -> &Self::Target {
&self.0
Expand Down