Skip to content

Commit 8206163

Browse files
committed
Fix onchain tags on receive
1 parent 9ee1001 commit 8206163

File tree

5 files changed

+55
-43
lines changed

5 files changed

+55
-43
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ class LightningRepo @Inject constructor(
669669
isTransfer = isTransfer,
670670
channelId = channelId ?: "",
671671
)
672-
preActivityMetadataRepo.upsertPreActivityMetadata(listOf(preActivityMetadata))
672+
preActivityMetadataRepo.addPreActivityMetadata(preActivityMetadata)
673673

674674
syncState()
675675
Result.success(txId)

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ class PreActivityMetadataRepo @Inject constructor(
4444
}
4545
}
4646

47+
suspend fun addPreActivityMetadata(metadata: PreActivityMetadata): Result<Unit> = withContext(bgDispatcher) {
48+
return@withContext runCatching {
49+
coreService.activity.addPreActivityMetadata(metadata)
50+
notifyChanged()
51+
}.onFailure { e ->
52+
Logger.error("addPreActivityMetadata error", e, context = TAG)
53+
}
54+
}
55+
4756
suspend fun addPreActivityMetadataTags(
4857
paymentId: String,
4958
tags: List<String>,

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

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,16 @@ class WalletRepo @Inject constructor(
9595
suspend fun refreshBip21(): Result<Unit> = withContext(bgDispatcher) {
9696
Logger.debug("Refreshing bip21", context = TAG)
9797

98+
// Get old payment ID and tags before refreshing (which may change payment ID)
9899
val oldPaymentId = paymentId()
99-
val currentTags = _walletState.value.selectedTags
100+
val tagsToMigrate = if (oldPaymentId != null && oldPaymentId.isNotEmpty()) {
101+
preActivityMetadataRepo
102+
.getPreActivityMetadata(oldPaymentId, searchByAddress = false)
103+
.getOrNull()
104+
?.tags ?: emptyList()
105+
} else {
106+
emptyList()
107+
}
100108

101109
val (_, shouldBlockLightningReceive) = coreService.checkGeoBlock()
102110
_walletState.update {
@@ -109,56 +117,40 @@ class WalletRepo @Inject constructor(
109117
val newPaymentId = paymentId()
110118
val newBip21Url = _walletState.value.bip21
111119
if (newPaymentId != null && newPaymentId.isNotEmpty() && newBip21Url.isNotEmpty()) {
112-
handlePreActivityMetadataForNewPaymentId(newPaymentId, oldPaymentId, currentTags, newBip21Url)
120+
persistPreActivityMetadata(newPaymentId, tagsToMigrate, newBip21Url)
113121
}
114122

115123
return@withContext Result.success(Unit)
116124
}
117125

118-
private suspend fun handlePreActivityMetadataForNewPaymentId(
119-
newPaymentId: String,
120-
oldPaymentId: String?,
121-
currentTags: List<String>,
122-
newBip21Url: String,
126+
private suspend fun persistPreActivityMetadata(
127+
paymentId: String,
128+
tags: List<String>,
129+
bip21Url: String,
123130
) {
124131
val onChainAddress = getOnchainAddress()
125132
val paymentHash = runCatching {
126-
when (val decoded = decode(newBip21Url)) {
133+
when (val decoded = decode(bip21Url)) {
127134
is Scanner.Lightning -> decoded.invoice.paymentHash.toHex()
128135
is Scanner.OnChain -> decoded.extractLightningHash()
129136
else -> null
130137
}
131138
}.getOrNull()
132139

133-
val existingMetadata = preActivityMetadataRepo
134-
.getPreActivityMetadata(newPaymentId, searchByAddress = false)
135-
.getOrNull()
136-
137-
if (existingMetadata == null) {
138-
// Create new metadata with current tags
139-
val preActivityMetadata = PreActivityMetadata(
140-
paymentId = newPaymentId,
141-
createdAt = nowTimestamp().toEpochMilli().toULong(),
142-
tags = currentTags,
143-
paymentHash = paymentHash,
144-
txId = null,
145-
address = onChainAddress,
146-
isReceive = true,
147-
feeRate = 0u,
148-
isTransfer = false,
149-
channelId = "",
150-
)
151-
preActivityMetadataRepo.upsertPreActivityMetadata(listOf(preActivityMetadata))
152-
Logger.debug("Created pre-activity metadata: paymentId=$newPaymentId, tags=$currentTags", context = TAG)
153-
} else if (newPaymentId != oldPaymentId) {
154-
if (currentTags.toSet() != existingMetadata.tags.toSet()) {
155-
preActivityMetadataRepo.resetPreActivityMetadataTags(newPaymentId)
156-
if (currentTags.isNotEmpty()) {
157-
preActivityMetadataRepo.addPreActivityMetadataTags(newPaymentId, currentTags)
158-
}
159-
Logger.debug("Updated tags for new payment ID: $newPaymentId, tags=$currentTags", context = TAG)
160-
}
161-
}
140+
val preActivityMetadata = PreActivityMetadata(
141+
paymentId = paymentId,
142+
createdAt = nowTimestamp().toEpochMilli().toULong(),
143+
tags = tags,
144+
paymentHash = paymentHash,
145+
txId = null,
146+
address = onChainAddress,
147+
isReceive = true,
148+
feeRate = 0u,
149+
isTransfer = false,
150+
channelId = "",
151+
)
152+
153+
preActivityMetadataRepo.addPreActivityMetadata(preActivityMetadata)
162154
}
163155

164156
suspend fun observeLdkWallet() = withContext(bgDispatcher) {
@@ -511,7 +503,14 @@ class WalletRepo @Inject constructor(
511503
): Result<Unit> = withContext(bgDispatcher) {
512504
return@withContext runCatching {
513505
val oldPaymentId = paymentId()
514-
val currentTags = _walletState.value.selectedTags
506+
val tagsToMigrate = if (oldPaymentId != null && oldPaymentId.isNotEmpty()) {
507+
preActivityMetadataRepo
508+
.getPreActivityMetadata(oldPaymentId, searchByAddress = false)
509+
.getOrNull()
510+
?.tags ?: emptyList()
511+
} else {
512+
emptyList()
513+
}
515514

516515
setBip21AmountSats(amountSats)
517516
setBip21Description(description)
@@ -527,10 +526,10 @@ class WalletRepo @Inject constructor(
527526
val newBip21Url = updateBip21Url(amountSats, description)
528527
setBip21(newBip21Url)
529528

530-
// Update metadata with current tags if payment ID changed (e.g., new lightning invoice created)
529+
// Persist metadata with migrated tags
531530
val newPaymentId = paymentId()
532531
if (newPaymentId != null && newPaymentId.isNotEmpty() && newBip21Url.isNotEmpty()) {
533-
handlePreActivityMetadataForNewPaymentId(newPaymentId, oldPaymentId, currentTags, newBip21Url)
532+
persistPreActivityMetadata(newPaymentId, tagsToMigrate, newBip21Url)
534533
}
535534
}.onFailure { e ->
536535
Logger.error("Update BIP21 invoice error", e, context = TAG)

app/src/test/java/to/bitkit/repositories/LightningRepoTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ class LightningRepoTest : BaseUnitTest() {
375375
whenever(settingsStore.data).thenReturn(flowOf(mockSettingsData))
376376

377377
wheneverBlocking {
378-
preActivityMetadataRepo.upsertPreActivityMetadata(any())
378+
preActivityMetadataRepo.addPreActivityMetadata(any())
379379
}.thenReturn(Result.success(Unit))
380380

381381
whenever(
@@ -410,7 +410,7 @@ class LightningRepoTest : BaseUnitTest() {
410410

411411
// Verify pre-activity metadata was saved
412412
verifyBlocking(preActivityMetadataRepo) {
413-
upsertPreActivityMetadata(any())
413+
addPreActivityMetadata(any())
414414
}
415415
}
416416

app/src/test/java/to/bitkit/repositories/WalletRepoTest.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ class WalletRepoTest : BaseUnitTest() {
7373
.thenReturn(Result.success(null))
7474
wheneverBlocking { preActivityMetadataRepo.upsertPreActivityMetadata(any()) }
7575
.thenReturn(Result.success(Unit))
76+
wheneverBlocking { preActivityMetadataRepo.addPreActivityMetadata(any()) }
77+
.thenReturn(Result.success(Unit))
7678
wheneverBlocking { preActivityMetadataRepo.resetPreActivityMetadataTags(any()) }
7779
.thenReturn(Result.success(Unit))
80+
wheneverBlocking { preActivityMetadataRepo.deletePreActivityMetadata(any()) }
81+
.thenReturn(Result.success(Unit))
7882
sut = createSut()
7983
}
8084

0 commit comments

Comments
 (0)