From 5b63c7b7090c11f07f8bd0d0bca4f65997f13b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Granh=C3=A3o?= Date: Mon, 30 Dec 2024 11:17:06 +0000 Subject: [PATCH 1/2] Show notification if payment requires fee acceptance --- .../Constants.kt | 8 +++ .../job/SwapUpdated.kt | 54 +++++++++++++++++-- .../Sources/BreezSDKLiquid/Constants.swift | 4 ++ .../BreezSDKLiquid/Task/SwapUpdated.swift | 21 ++++++++ 4 files changed, 83 insertions(+), 4 deletions(-) diff --git a/lib/bindings/langs/android/lib/src/main/kotlin/breez_sdk_liquid_notification/Constants.kt b/lib/bindings/langs/android/lib/src/main/kotlin/breez_sdk_liquid_notification/Constants.kt index 1f4e92421..b125dc3a4 100644 --- a/lib/bindings/langs/android/lib/src/main/kotlin/breez_sdk_liquid_notification/Constants.kt +++ b/lib/bindings/langs/android/lib/src/main/kotlin/breez_sdk_liquid_notification/Constants.kt @@ -57,6 +57,10 @@ object Constants { "payment_sent_notification_text" const val PAYMENT_SENT_NOTIFICATION_TITLE = "payment_sent_notification_title" + const val PAYMENT_WAITING_FEE_ACCEPTANCE_TITLE = + "payment_waiting_fee_acceptance_notification_title" + const val PAYMENT_WAITING_FEE_ACCEPTANCE_TEXT = + "payment_waiting_fee_acceptance_text" const val SWAP_CONFIRMED_NOTIFICATION_FAILURE_TEXT = "swap_confirmed_notification_failure_text" const val SWAP_CONFIRMED_NOTIFICATION_FAILURE_TITLE = @@ -102,6 +106,10 @@ object Constants { "Sent %d sats" const val DEFAULT_PAYMENT_SENT_NOTIFICATION_TITLE = "Payment Sent" + const val DEFAULT_PAYMENT_WAITING_FEE_ACCEPTANCE_TITLE = + "Payment requires fee acceptance" + const val DEFAULT_PAYMENT_WAITING_FEE_ACCEPTANCE_TEXT = + "Tap to review updated fees" const val DEFAULT_SWAP_CONFIRMED_NOTIFICATION_FAILURE_TEXT = "Tap to complete payment" const val DEFAULT_SWAP_CONFIRMED_NOTIFICATION_FAILURE_TITLE = diff --git a/lib/bindings/langs/android/lib/src/main/kotlin/breez_sdk_liquid_notification/job/SwapUpdated.kt b/lib/bindings/langs/android/lib/src/main/kotlin/breez_sdk_liquid_notification/job/SwapUpdated.kt index f96300be4..e12ffd2a2 100644 --- a/lib/bindings/langs/android/lib/src/main/kotlin/breez_sdk_liquid_notification/job/SwapUpdated.kt +++ b/lib/bindings/langs/android/lib/src/main/kotlin/breez_sdk_liquid_notification/job/SwapUpdated.kt @@ -10,6 +10,8 @@ import breez_sdk_liquid_notification.Constants.DEFAULT_PAYMENT_RECEIVED_NOTIFICA import breez_sdk_liquid_notification.Constants.DEFAULT_PAYMENT_RECEIVED_NOTIFICATION_TITLE import breez_sdk_liquid_notification.Constants.DEFAULT_PAYMENT_SENT_NOTIFICATION_TEXT import breez_sdk_liquid_notification.Constants.DEFAULT_PAYMENT_SENT_NOTIFICATION_TITLE +import breez_sdk_liquid_notification.Constants.DEFAULT_PAYMENT_WAITING_FEE_ACCEPTANCE_TEXT +import breez_sdk_liquid_notification.Constants.DEFAULT_PAYMENT_WAITING_FEE_ACCEPTANCE_TITLE import breez_sdk_liquid_notification.Constants.DEFAULT_SWAP_CONFIRMED_NOTIFICATION_FAILURE_TEXT import breez_sdk_liquid_notification.Constants.DEFAULT_SWAP_CONFIRMED_NOTIFICATION_FAILURE_TITLE import breez_sdk_liquid_notification.Constants.NOTIFICATION_CHANNEL_SWAP_UPDATED @@ -17,6 +19,8 @@ import breez_sdk_liquid_notification.Constants.PAYMENT_RECEIVED_NOTIFICATION_TEX import breez_sdk_liquid_notification.Constants.PAYMENT_RECEIVED_NOTIFICATION_TITLE import breez_sdk_liquid_notification.Constants.PAYMENT_SENT_NOTIFICATION_TEXT import breez_sdk_liquid_notification.Constants.PAYMENT_SENT_NOTIFICATION_TITLE +import breez_sdk_liquid_notification.Constants.PAYMENT_WAITING_FEE_ACCEPTANCE_TEXT +import breez_sdk_liquid_notification.Constants.PAYMENT_WAITING_FEE_ACCEPTANCE_TITLE import breez_sdk_liquid_notification.Constants.SWAP_CONFIRMED_NOTIFICATION_FAILURE_TEXT import breez_sdk_liquid_notification.Constants.SWAP_CONFIRMED_NOTIFICATION_FAILURE_TITLE import breez_sdk_liquid_notification.NotificationHelper.Companion.notifyChannel @@ -57,8 +61,9 @@ class SwapUpdatedJob( override fun onEvent(e: SdkEvent) { when (e) { - is SdkEvent.PaymentWaitingConfirmation -> handlePaymentEvent(e.details) - is SdkEvent.PaymentSucceeded -> handlePaymentEvent(e.details) + is SdkEvent.PaymentWaitingConfirmation -> handlePaymentSuccess(e.details) + is SdkEvent.PaymentSucceeded -> handlePaymentSuccess(e.details) + is SdkEvent.PaymentWaitingFeeAcceptance -> handlePaymentWaitingFeeAcceptance(e.details) else -> { logger.log(TAG, "Received event: ${e}", "TRACE") @@ -76,12 +81,16 @@ class SwapUpdatedJob( .fold(StringBuilder()) { sb, it -> sb.append("%02x".format(it)) } .toString() - private fun handlePaymentEvent(payment: Payment) { - val swapId = when (val details = payment.details) { + private fun getSwapId(details: PaymentDetails?): String? { + return when (details) { is PaymentDetails.Bitcoin -> details.swapId is PaymentDetails.Lightning -> details.swapId else -> null } + } + + private fun handlePaymentSuccess(payment: Payment) { + val swapId = getSwapId(payment.details) swapId?.let { if (this.swapIdHash == hashId(it)) { @@ -95,6 +104,21 @@ class SwapUpdatedJob( } } + private fun handlePaymentWaitingFeeAcceptance(payment: Payment) { + val swapId = getSwapId(payment.details) + + swapId?.let { + if (this.swapIdHash == hashId(it)) { + logger.log( + TAG, + "Payment waiting fee acceptance: ${this.swapIdHash}", + "TRACE" + ) + notifyPaymentWaitingFeeAcceptance(payment) + } + } + } + private fun notifySuccess(payment: Payment) { if (!this.notified) { logger.log(TAG, "Payment ${payment.txId} processing successful", "INFO") @@ -121,6 +145,28 @@ class SwapUpdatedJob( } } + private fun notifyPaymentWaitingFeeAcceptance(payment: Payment) { + if (!this.notified) { + logger.log(TAG, "Payment ${payment.txId} requires fee acceptance", "INFO") + notifyChannel( + context, + NOTIFICATION_CHANNEL_SWAP_UPDATED, + getString( + context, + PAYMENT_WAITING_FEE_ACCEPTANCE_TITLE, + DEFAULT_PAYMENT_WAITING_FEE_ACCEPTANCE_TITLE + ), + getString( + context, + PAYMENT_WAITING_FEE_ACCEPTANCE_TEXT, + DEFAULT_PAYMENT_WAITING_FEE_ACCEPTANCE_TEXT + ) + ) + this.notified = true + fgService.onFinished(this) + } + } + private fun notifyFailure() { this.swapIdHash?.let { swapIdHash -> logger.log(TAG, "Swap $swapIdHash processing failed", "INFO") diff --git a/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Constants.swift b/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Constants.swift index aaef8dc11..5e26b545a 100644 --- a/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Constants.swift +++ b/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Constants.swift @@ -20,6 +20,8 @@ struct Constants { static let LNURL_PAY_NOTIFICATION_FAILURE_TITLE = "lnurl_pay_notification_failure_title" static let PAYMENT_RECEIVED_NOTIFICATION_TITLE = "payment_received_notification_title" static let PAYMENT_SENT_NOTIFICATION_TITLE = "payment_sent_notification_title" + static let PAYMENT_WAITING_FEE_ACCEPTANCE_TITLE = "payment_waiting_fee_acceptance_notification_title" + static let PAYMENT_WAITING_FEE_ACCEPTANCE_TEXT = "payment_waiting_fee_acceptance_text" static let SWAP_CONFIRMED_NOTIFICATION_FAILURE_TEXT = "swap_confirmed_notification_failure_text" static let SWAP_CONFIRMED_NOTIFICATION_FAILURE_TITLE = "swap_confirmed_notification_failure_title" @@ -30,6 +32,8 @@ struct Constants { static let DEFAULT_LNURL_PAY_NOTIFICATION_FAILURE_TITLE = "Receive Payment Failed" static let DEFAULT_PAYMENT_RECEIVED_NOTIFICATION_TITLE = "Received %d sats" static let DEFAULT_PAYMENT_SENT_NOTIFICATION_TITLE = "Sent %d sats" + static let DEFAULT_PAYMENT_WAITING_FEE_ACCEPTANCE_TITLE = "Payment requires fee acceptance" + static let DEFAULT_PAYMENT_WAITING_FEE_ACCEPTANCE_TEXT = "Tap to review updated fees" static let DEFAULT_SWAP_CONFIRMED_NOTIFICATION_FAILURE_TEXT = "Tap to complete payment" static let DEFAULT_SWAP_CONFIRMED_NOTIFICATION_FAILURE_TITLE = "Payment Pending" } diff --git a/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Task/SwapUpdated.swift b/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Task/SwapUpdated.swift index fd70f715b..d7b0d0867 100644 --- a/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Task/SwapUpdated.swift +++ b/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Task/SwapUpdated.swift @@ -43,6 +43,13 @@ class SwapUpdatedTask : TaskProtocol { self.notifySuccess(payment: payment) } break + case .paymentWaitingFeeAcceptance(details: let payment): + let swapId = self.getSwapId(details: payment.details) + if swapIdHash == swapId?.sha256() { + self.logger.log(tag: TAG, line: "Received payment event: \(swapIdHash) \(payment.status)", level: "INFO") + self.notifyPaymentWaitingFeeAcceptance(payment: payment) + } + break default: break } @@ -81,4 +88,18 @@ class SwapUpdatedTask : TaskProtocol { self.displayPushNotification(title: String(format: notificationTitle, payment.amountSat), logger: self.logger, threadIdentifier: Constants.NOTIFICATION_THREAD_SWAP_UPDATED) } } + + func notifyPaymentWaitingFeeAcceptance(payment: Payment) { + if !self.notified { + self.logger.log(tag: TAG, line: "Payment \(payment.txId ?? "") requires fee acceptance", level: "INFO") + let notificationTitle = ResourceHelper.shared.getString( + key: Constants.PAYMENT_WAITING_FEE_ACCEPTANCE_TITLE, + fallback: Constants.DEFAULT_PAYMENT_WAITING_FEE_ACCEPTANCE_TITLE) + let notificationBody = ResourceHelper.shared.getString( + key: Constants.PAYMENT_WAITING_FEE_ACCEPTANCE_TEXT, + fallback: Constants.DEFAULT_PAYMENT_WAITING_FEE_ACCEPTANCE_TEXT) + self.notified = true + self.displayPushNotification(title: notificationTitle, body: notificationBody, logger: self.logger, threadIdentifier: Constants.NOTIFICATION_THREAD_SWAP_UPDATED) + } + } } From e80e9076c7f1f3e467f22f5d96e39c82fa8785fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Granh=C3=A3o?= Date: Mon, 30 Dec 2024 14:55:40 +0000 Subject: [PATCH 2/2] Fix logs using non-existent txid --- .../kotlin/breez_sdk_liquid_notification/job/SwapUpdated.kt | 2 +- .../langs/swift/Sources/BreezSDKLiquid/Task/SwapUpdated.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/bindings/langs/android/lib/src/main/kotlin/breez_sdk_liquid_notification/job/SwapUpdated.kt b/lib/bindings/langs/android/lib/src/main/kotlin/breez_sdk_liquid_notification/job/SwapUpdated.kt index e12ffd2a2..532a298c6 100644 --- a/lib/bindings/langs/android/lib/src/main/kotlin/breez_sdk_liquid_notification/job/SwapUpdated.kt +++ b/lib/bindings/langs/android/lib/src/main/kotlin/breez_sdk_liquid_notification/job/SwapUpdated.kt @@ -147,7 +147,7 @@ class SwapUpdatedJob( private fun notifyPaymentWaitingFeeAcceptance(payment: Payment) { if (!this.notified) { - logger.log(TAG, "Payment ${payment.txId} requires fee acceptance", "INFO") + logger.log(TAG, "Payment with swap ID ${getSwapId(payment.details)} requires fee acceptance", "INFO") notifyChannel( context, NOTIFICATION_CHANNEL_SWAP_UPDATED, diff --git a/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Task/SwapUpdated.swift b/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Task/SwapUpdated.swift index d7b0d0867..26a74d2e2 100644 --- a/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Task/SwapUpdated.swift +++ b/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Task/SwapUpdated.swift @@ -91,7 +91,7 @@ class SwapUpdatedTask : TaskProtocol { func notifyPaymentWaitingFeeAcceptance(payment: Payment) { if !self.notified { - self.logger.log(tag: TAG, line: "Payment \(payment.txId ?? "") requires fee acceptance", level: "INFO") + self.logger.log(tag: TAG, line: "Payment \(self.getSwapId(details: payment.details) ?? "") requires fee acceptance", level: "INFO") let notificationTitle = ResourceHelper.shared.getString( key: Constants.PAYMENT_WAITING_FEE_ACCEPTANCE_TITLE, fallback: Constants.DEFAULT_PAYMENT_WAITING_FEE_ACCEPTANCE_TITLE)