From 2f266a34247999d243ab01d43faad3aa21acb7c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Granh=C3=A3o?= Date: Wed, 22 May 2024 11:32:36 +0100 Subject: [PATCH] Implement toggle for specific notification handling --- examples/notification_handler/main.rs | 35 ++++++++++++++++++++++++--- src/errors.rs | 3 +++ src/lib.rs | 2 +- src/lipalightninglib.udl | 9 ++++++- src/notification_handling.rs | 32 ++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 6 deletions(-) diff --git a/examples/notification_handler/main.rs b/examples/notification_handler/main.rs index 8342eda4..e6b23c1f 100644 --- a/examples/notification_handler/main.rs +++ b/examples/notification_handler/main.rs @@ -12,7 +12,7 @@ use rustyline::{CompletionType, Editor}; use std::collections::HashSet; use std::env; use uniffi_lipalightninglib::{ - handle_notification, mnemonic_to_secret, Config, EnvironmentCode, TzConfig, + handle_notification, mnemonic_to_secret, Config, EnvironmentCode, NotificationToggles, TzConfig, }; static BASE_DIR: &str = ".3l_node"; @@ -160,7 +160,16 @@ fn start_payment_received(words: &mut dyn Iterator) -> Result<()> { }}" ); - let notification = handle_notification(config, notification_payload).unwrap(); + let notification = handle_notification( + config, + notification_payload, + NotificationToggles { + payment_received_is_enabled: true, + address_txs_confirmed_is_enabled: true, + lnurl_pay_request_is_enabled: true, + }, + ) + .unwrap(); println!("The returned notification is {notification:?}"); @@ -186,7 +195,16 @@ fn start_address_txs_confirmed(words: &mut dyn Iterator) -> Result< }}" ); - let notification = handle_notification(config, notification_payload).unwrap(); + let notification = handle_notification( + config, + notification_payload, + NotificationToggles { + payment_received_is_enabled: true, + address_txs_confirmed_is_enabled: true, + lnurl_pay_request_is_enabled: true, + }, + ) + .unwrap(); println!("The returned notification is {notification:?}"); @@ -224,7 +242,16 @@ fn start_lnurl_pay_request(words: &mut dyn Iterator) -> Result<()> }}" ); - let notification = handle_notification(config, notification_payload).unwrap(); + let notification = handle_notification( + config, + notification_payload, + NotificationToggles { + payment_received_is_enabled: true, + address_txs_confirmed_is_enabled: true, + lnurl_pay_request_is_enabled: true, + }, + ) + .unwrap(); println!("The returned notification is {notification:?}"); diff --git a/src/errors.rs b/src/errors.rs index eb1a7132..ed68f8dc 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -313,6 +313,9 @@ pub enum NotificationHandlingErrorCode { InsufficientInboundLiquidity, /// A request to one of lipa's services failed. LipaServiceUnavailable, + /// The notification payload is disabled in the provided + /// [`NotificationToggles`](crate::notification_handling::NotificationToggles). + NotificationDisabledInNotificationToggles, } impl Display for NotificationHandlingErrorCode { diff --git a/src/lib.rs b/src/lib.rs index d5f33daf..1e6b2124 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,7 +73,7 @@ use crate::key_derivation::derive_persistence_encryption_key; pub use crate::limits::{LiquidityLimit, PaymentAmountLimits}; pub use crate::lnurl::{LnUrlPayDetails, LnUrlWithdrawDetails}; use crate::locker::Locker; -pub use crate::notification_handling::{handle_notification, Notification}; +pub use crate::notification_handling::{handle_notification, Notification, NotificationToggles}; pub use crate::offer::{OfferInfo, OfferKind, OfferStatus}; pub use crate::payment::{ IncomingPaymentInfo, OutgoingPaymentInfo, PaymentInfo, PaymentState, Recipient, diff --git a/src/lipalightninglib.udl b/src/lipalightninglib.udl index 095fce0c..87ab41cc 100644 --- a/src/lipalightninglib.udl +++ b/src/lipalightninglib.udl @@ -640,7 +640,7 @@ namespace lipalightninglib { void parse_lightning_address([ByRef] string address); [Throws=NotificationHandlingError] - Notification handle_notification(Config config, string notification_payload); + Notification handle_notification(Config config, string notification_payload, NotificationToggles notification_toggles); }; dictionary Secret { @@ -656,6 +656,12 @@ interface Notification { LnurlInvoiceCreated(u64 amount_sat); }; +dictionary NotificationToggles { + boolean payment_received_is_enabled; + boolean address_txs_confirmed_is_enabled; + boolean lnurl_pay_request_is_enabled; +}; + // // ----------------------------- ERROR RELATED DEFINITIONS ----------------------------- // @@ -793,4 +799,5 @@ enum NotificationHandlingErrorCode { "ExpectedPaymentNotReceived", "InsufficientInboundLiquidity", "LipaServiceUnavailable", + "NotificationDisabledInNotificationToggles", }; diff --git a/src/notification_handling.rs b/src/notification_handling.rs index b69c41dd..980bd8e1 100644 --- a/src/notification_handling.rs +++ b/src/notification_handling.rs @@ -61,6 +61,13 @@ pub enum Notification { LnurlInvoiceCreated { amount_sat: u64 }, } +/// A configuration struct used to enable/disable processing of different payloads in [`handle_notification`]. +pub struct NotificationToggles { + pub payment_received_is_enabled: bool, + pub address_txs_confirmed_is_enabled: bool, + pub lnurl_pay_request_is_enabled: bool, +} + /// Handles a notification. /// /// Notifications are used to wake up the node in order to process some request. Currently supported @@ -72,6 +79,7 @@ pub enum Notification { pub fn handle_notification( config: Config, notification_payload: String, + notification_toggles: NotificationToggles, ) -> NotificationHandlingResult { enable_backtrace(); if let Some(level) = config.file_logging_level { @@ -90,6 +98,30 @@ pub fn handle_notification( } }; + match payload { + Payload::PaymentReceived { .. } => ensure!( + notification_toggles.payment_received_is_enabled, + runtime_error( + NotificationHandlingErrorCode::NotificationDisabledInNotificationToggles, + "PaymentReceived notification dismissed due to disabled setting in NotificationToggles" + ) + ), + Payload::AddressTxsConfirmed { .. } => ensure!( + notification_toggles.address_txs_confirmed_is_enabled, + runtime_error( + NotificationHandlingErrorCode::NotificationDisabledInNotificationToggles, + "AddressTxsConfirmed notification dismissed due to disabled setting in NotificationToggles" + ) + ), + Payload::LnurlPayRequest { .. } => ensure!( + notification_toggles.lnurl_pay_request_is_enabled, + runtime_error( + NotificationHandlingErrorCode::NotificationDisabledInNotificationToggles, + "LnurlPayRequest notification dismissed due to disabled setting in NotificationToggles" + ) + ), + } + let rt = AsyncRuntime::new() .map_runtime_error_using(NotificationHandlingErrorCode::from_runtime_error)?;