Skip to content

Commit

Permalink
Merge branch 'main' into mock-deps
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgranhao committed May 22, 2024
2 parents ae06427 + 808c367 commit e0e7d61
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 deletions.
35 changes: 31 additions & 4 deletions examples/notification_handler/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -160,7 +160,16 @@ fn start_payment_received(words: &mut dyn Iterator<Item = &str>) -> 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:?}");

Expand All @@ -186,7 +195,16 @@ fn start_address_txs_confirmed(words: &mut dyn Iterator<Item = &str>) -> 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:?}");

Expand Down Expand Up @@ -224,7 +242,16 @@ fn start_lnurl_pay_request(words: &mut dyn Iterator<Item = &str>) -> 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:?}");

Expand Down
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion src/lipalightninglib.udl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 -----------------------------
//
Expand Down Expand Up @@ -793,4 +799,5 @@ enum NotificationHandlingErrorCode {
"ExpectedPaymentNotReceived",
"InsufficientInboundLiquidity",
"LipaServiceUnavailable",
"NotificationDisabledInNotificationToggles",
};
32 changes: 32 additions & 0 deletions src/notification_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -72,6 +79,7 @@ pub enum Notification {
pub fn handle_notification(
config: Config,
notification_payload: String,
notification_toggles: NotificationToggles,
) -> NotificationHandlingResult<Notification> {
enable_backtrace();
if let Some(level) = config.file_logging_level {
Expand All @@ -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)?;

Expand Down

0 comments on commit e0e7d61

Please sign in to comment.