Skip to content

Commit a7ffdb7

Browse files
authored
Merge branch 'feat/onchain-events' into fix/toast-ui
2 parents ff24178 + d048c00 commit a7ffdb7

File tree

11 files changed

+333
-234
lines changed

11 files changed

+333
-234
lines changed

app/src/main/java/to/bitkit/data/CacheStore.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,6 @@ data class AppCacheData(
133133
val lastLightningPaymentId: String? = null,
134134
val pendingBoostActivities: List<PendingBoostActivity> = listOf(),
135135
val backgroundReceive: NewTransactionSheetDetails? = null,
136-
)
136+
) {
137+
fun resetBip21() = copy(bip21 = "", bolt11 = "", onchainAddress = "")
138+
}

app/src/main/java/to/bitkit/domain/commands/NotifyPaymentReceived.kt

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,31 @@ import to.bitkit.models.NotificationDetails
77
sealed interface NotifyPaymentReceived {
88

99
sealed interface Command : NotifyPaymentReceived {
10-
val sats: ULong
11-
val paymentHashOrTxId: String
1210
val includeNotification: Boolean
1311

1412
data class Lightning(
15-
override val sats: ULong,
16-
override val paymentHashOrTxId: String,
13+
val event: Event.PaymentReceived,
1714
override val includeNotification: Boolean = false,
1815
) : Command
1916

2017
data class Onchain(
21-
override val sats: ULong,
22-
override val paymentHashOrTxId: String,
18+
val event: Event.OnchainTransactionReceived,
2319
override val includeNotification: Boolean = false,
2420
) : Command
2521

2622
companion object {
2723
fun from(event: Event, includeNotification: Boolean = false): Command? =
2824
when (event) {
2925
is Event.PaymentReceived -> Lightning(
30-
sats = event.amountMsat / 1000u,
31-
paymentHashOrTxId = event.paymentHash,
26+
event = event,
3227
includeNotification = includeNotification,
3328
)
3429

35-
is Event.OnchainTransactionReceived -> {
36-
val amountSats = event.details.amountSats
37-
Onchain(
38-
sats = amountSats.toULong(),
39-
paymentHashOrTxId = event.txid,
40-
includeNotification = includeNotification,
41-
).takeIf {
42-
amountSats > 0
43-
}
30+
is Event.OnchainTransactionReceived -> Onchain(
31+
event = event,
32+
includeNotification = includeNotification,
33+
).takeIf {
34+
event.details.amountSats > 0
4435
}
4536

4637
else -> null

app/src/main/java/to/bitkit/domain/commands/NotifyPaymentReceivedHandler.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,32 @@ class NotifyPaymentReceivedHandler @Inject constructor(
3838
val shouldShow = when (command) {
3939
is NotifyPaymentReceived.Command.Lightning -> true
4040
is NotifyPaymentReceived.Command.Onchain -> {
41+
activityRepo.handleOnchainTransactionReceived(command.event.txid, command.event.details)
4142
delay(DELAY_FOR_ACTIVITY_SYNC_MS)
42-
activityRepo.shouldShowReceivedSheet(command.paymentHashOrTxId, command.sats)
43+
activityRepo.shouldShowReceivedSheet(command.event.txid, command.event.details.amountSats.toULong())
4344
}
4445
}
4546

4647
if (!shouldShow) return@runCatching NotifyPaymentReceived.Result.Skip
4748

48-
val satsLong = command.sats.toLong()
4949
val details = NewTransactionSheetDetails(
5050
type = when (command) {
5151
is NotifyPaymentReceived.Command.Lightning -> NewTransactionSheetType.LIGHTNING
5252
is NotifyPaymentReceived.Command.Onchain -> NewTransactionSheetType.ONCHAIN
5353
},
5454
direction = NewTransactionSheetDirection.RECEIVED,
55-
paymentHashOrTxId = command.paymentHashOrTxId,
56-
sats = satsLong,
55+
paymentHashOrTxId = when (command) {
56+
is NotifyPaymentReceived.Command.Lightning -> command.event.paymentHash
57+
is NotifyPaymentReceived.Command.Onchain -> command.event.txid
58+
},
59+
sats = when (command) {
60+
is NotifyPaymentReceived.Command.Lightning -> (command.event.amountMsat / 1000u).toLong()
61+
is NotifyPaymentReceived.Command.Onchain -> command.event.details.amountSats
62+
},
5763
)
5864

5965
if (command.includeNotification) {
60-
val notification = buildNotificationContent(satsLong)
66+
val notification = buildNotificationContent(details.sats)
6167
NotifyPaymentReceived.Result.ShowNotification(details, notification)
6268
} else {
6369
NotifyPaymentReceived.Result.ShowSheet(details)
@@ -97,8 +103,8 @@ class NotifyPaymentReceivedHandler @Inject constructor(
97103
const val TAG = "NotifyPaymentReceivedHandler"
98104

99105
/**
100-
* Delay before calling `shouldShowPaymentReceived` for onchain transactions to allow ActivityRepo
101-
* to sync payments before we check for RBF replacement or channel closure.
106+
* Delay after syncing onchain transaction to allow the database to fully process
107+
* the transaction before checking for RBF replacement or channel closure.
102108
*/
103109
private const val DELAY_FOR_ACTIVITY_SYNC_MS = 500L
104110
}

app/src/main/java/to/bitkit/repositories/BackupRepo.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,8 @@ class BackupRepo @Inject constructor(
484484
return@withContext try {
485485
performRestore(BackupCategory.METADATA) { dataBytes ->
486486
val parsed = json.decodeFromString<MetadataBackupV1>(String(dataBytes))
487-
val cleanedUp = parsed.cache.copy(onchainAddress = "") // Force address rotation
488-
cacheStore.update { cleanedUp }
487+
val cleanCache = parsed.cache.resetBip21() // Force address rotation
488+
cacheStore.update { cleanCache }
489489
Logger.debug("Restored caches: ${jsonLogOf(parsed.cache.copy(cachedRates = emptyList()))}", TAG)
490490
onCacheRestored()
491491
preActivityMetadataRepo.upsertPreActivityMetadata(parsed.tagMetadata).getOrNull()

0 commit comments

Comments
 (0)