Skip to content

Commit 8a5c573

Browse files
authored
Merge pull request #930 from synonymdev/codex/pr924-review-fixes
fix: refine public Paykit contact handling
2 parents afeb1f4 + 1e4dd83 commit 8a5c573

11 files changed

Lines changed: 132 additions & 131 deletions

File tree

app/src/main/java/to/bitkit/env/Env.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ internal object Env {
245245

246246
@Suppress("ConstPropertyName")
247247
object Defaults {
248+
/** Default Bolt11 invoice expiry in seconds. */
249+
const val bolt11InvoiceExpirySeconds = 3_600u
250+
248251
/** Recommended transaction base fee in sats */
249252
const val recommendedBaseFee = 256u
250253

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,7 @@ class ActivityRepo @Inject constructor(
348348
getActivities(
349349
filter = ActivityFilter.ALL,
350350
sortDirection = SortDirection.DESC,
351-
).getOrThrow().filter { activity ->
352-
when (activity) {
353-
is Activity.Lightning -> PubkyPublicKeyFormat.matches(activity.v1.contact, normalizedKey)
354-
is Activity.Onchain -> PubkyPublicKeyFormat.matches(activity.v1.contact, normalizedKey)
355-
}
356-
}
351+
).getOrThrow().filter { PubkyPublicKeyFormat.matches(it.contact(), normalizedKey) }
357352
}.onFailure {
358353
Logger.error("Failed to load contact activities for '$publicKey'", it, context = TAG)
359354
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import org.lightningdevkit.ldknode.ChannelDetails
4141
import to.bitkit.async.ServiceQueue
4242
import to.bitkit.data.CacheStore
4343
import to.bitkit.di.BgDispatcher
44+
import to.bitkit.env.Defaults
4445
import to.bitkit.env.Env
4546
import to.bitkit.ext.calculateRemoteBalance
4647
import to.bitkit.ext.nowTimestamp
@@ -462,7 +463,7 @@ class BlocktankRepo @Inject constructor(
462463
val invoice = lightningRepo.createInvoice(
463464
amountSats = null,
464465
description = "blocktank-gift-code:$code",
465-
expirySeconds = 3600u,
466+
expirySeconds = Defaults.bolt11InvoiceExpirySeconds,
466467
).getOrThrow()
467468

468469
Logger.debug("Created invoice for gift code, requesting payment from LSP", context = TAG)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import to.bitkit.data.SettingsStore
5858
import to.bitkit.data.backup.VssBackupClientLdk
5959
import to.bitkit.data.keychain.Keychain
6060
import to.bitkit.di.BgDispatcher
61+
import to.bitkit.env.Defaults
6162
import to.bitkit.env.Env
6263
import to.bitkit.ext.getSatsPerVByteFor
6364
import to.bitkit.ext.nowTimestamp
@@ -917,7 +918,7 @@ class LightningRepo @Inject constructor(
917918
suspend fun createInvoice(
918919
amountSats: ULong? = null,
919920
description: String,
920-
expirySeconds: UInt = 86_400u,
921+
expirySeconds: UInt = Defaults.bolt11InvoiceExpirySeconds,
921922
): Result<String> = executeWhenNodeRunning("createInvoice") {
922923
updateGeoBlockState()
923924
runCatching { lightningService.receive(amountSats, description, expirySeconds) }
@@ -926,7 +927,7 @@ class LightningRepo @Inject constructor(
926927
suspend fun createInvoiceMsats(
927928
amountMsats: ULong,
928929
description: String,
929-
expirySeconds: UInt = 86_400u,
930+
expirySeconds: UInt = Defaults.bolt11InvoiceExpirySeconds,
930931
): Result<String> = executeWhenNodeRunning("createInvoiceMsats") {
931932
updateGeoBlockState()
932933
runCatching { lightningService.receiveMsats(amountMsats, description, expirySeconds) }

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import kotlin.time.Clock
2929
import kotlin.time.Duration.Companion.hours
3030
import kotlin.time.Duration.Companion.minutes
3131
import kotlin.time.ExperimentalTime
32+
import to.bitkit.di.json as appJson
3233

3334
sealed class PublicPaykitError(message: String) : AppError(message) {
3435
data object InvalidPayload : PublicPaykitError("Invalid Paykit payment endpoint payload")
@@ -57,7 +58,6 @@ class PublicPaykitRepo @Inject constructor(
5758
) {
5859
companion object {
5960
private val methodIdPattern = Regex("^[a-z0-9]+-[a-z0-9]+-[a-z0-9]+$")
60-
private val json = Json { ignoreUnknownKeys = true }
6161

6262
private val payablePreferenceOrder = listOf(
6363
MethodId.Bolt11,
@@ -77,7 +77,7 @@ class PublicPaykitRepo @Inject constructor(
7777

7878
val knownMethodId = MethodId.fromRawValue(methodId) ?: return null
7979
val payload = runCatching {
80-
json.decodeFromString<PaymentEndpointPayload>(endpointData)
80+
appJson.decodeFromString<PaymentEndpointPayload>(endpointData)
8181
}.getOrNull() ?: return null
8282
val value = payload.value.trim()
8383
if (value.isEmpty()) return null
@@ -94,7 +94,7 @@ class PublicPaykitRepo @Inject constructor(
9494
fun serializePayload(value: String): String {
9595
val trimmedValue = value.trim()
9696
if (trimmedValue.isEmpty()) throw PublicPaykitError.InvalidPayload
97-
return json.encodeToString(PaymentEndpointPayload(value = trimmedValue))
97+
return Json.encodeToString(PaymentEndpointPayload(value = trimmedValue))
9898
}
9999

100100
fun paymentRequest(endpoints: List<Endpoint>): String {

app/src/main/java/to/bitkit/services/LightningService.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import to.bitkit.data.SettingsStore
4646
import to.bitkit.data.backup.VssStoreIdProvider
4747
import to.bitkit.data.keychain.Keychain
4848
import to.bitkit.di.BgDispatcher
49+
import to.bitkit.env.Defaults
4950
import to.bitkit.env.Env
5051
import to.bitkit.ext.totalNextOutboundHtlcLimitSats
5152
import to.bitkit.ext.uByteList
@@ -592,11 +593,19 @@ class LightningService @Inject constructor(
592593
return true
593594
}
594595

595-
suspend fun receive(sat: ULong? = null, description: String, expirySecs: UInt = 3600u): String {
596+
suspend fun receive(
597+
sat: ULong? = null,
598+
description: String,
599+
expirySecs: UInt = Defaults.bolt11InvoiceExpirySeconds,
600+
): String {
596601
return receiveMsats(amountMsat = sat?.let { it * 1000u }, description = description, expirySecs = expirySecs)
597602
}
598603

599-
suspend fun receiveMsats(amountMsat: ULong? = null, description: String, expirySecs: UInt = 3600u): String {
604+
suspend fun receiveMsats(
605+
amountMsat: ULong? = null,
606+
description: String,
607+
expirySecs: UInt = Defaults.bolt11InvoiceExpirySeconds,
608+
): String {
600609
val node = this.node ?: throw ServiceError.NodeNotSetup()
601610

602611
val message = description

app/src/main/java/to/bitkit/ui/screens/wallets/activity/components/ActivityContactTitle.kt renamed to app/src/main/java/to/bitkit/ui/screens/wallets/activity/components/ContactActivityTitle.kt

File renamed without changes.

app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,8 +1373,10 @@ class AppViewModel @Inject constructor(
13731373

13741374
fun preserveContactPaymentContext(paymentHash: String) {
13751375
synchronized(contactPaymentContextLock) {
1376-
activeContactPaymentContext?.let {
1377-
pendingContactPaymentContexts[paymentHash] = it
1376+
val context = activeContactPaymentContext
1377+
if (context != null) {
1378+
pendingContactPaymentContexts[paymentHash] = context
1379+
activeContactPaymentContext = null
13781380
}
13791381
}
13801382
}
@@ -2069,7 +2071,7 @@ class AppViewModel @Inject constructor(
20692071
lightningRepo.createInvoiceMsats(
20702072
amountMsats = lnurl.data.maxWithdrawable,
20712073
description = lnurl.data.defaultDescription,
2072-
expirySeconds = 3600u,
2074+
expirySeconds = Defaults.bolt11InvoiceExpirySeconds,
20732075
)
20742076
} else {
20752077
val withdrawAmountSats = _sendUiState.value.amount.coerceAtLeast(
@@ -2079,7 +2081,7 @@ class AppViewModel @Inject constructor(
20792081
lightningRepo.createInvoice(
20802082
amountSats = withdrawAmountSats,
20812083
description = lnurl.data.defaultDescription,
2082-
expirySeconds = 3600u,
2084+
expirySeconds = Defaults.bolt11InvoiceExpirySeconds,
20832085
)
20842086
}.getOrNull()
20852087

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import to.bitkit.data.SettingsData
4040
import to.bitkit.data.SettingsStore
4141
import to.bitkit.data.backup.VssBackupClientLdk
4242
import to.bitkit.data.keychain.Keychain
43+
import to.bitkit.env.Defaults
4344
import to.bitkit.ext.createChannelDetails
4445
import to.bitkit.ext.of
4546
import to.bitkit.models.CoinSelectionPreference
@@ -200,11 +201,15 @@ class LightningRepoTest : BaseUnitTest() {
200201
lightningService.receive(
201202
sat = 100uL,
202203
description = "test",
203-
expirySecs = 3600u
204+
expirySecs = Defaults.bolt11InvoiceExpirySeconds,
204205
)
205206
).thenReturn(testInvoice)
206207

207-
val result = sut.createInvoice(amountSats = 100uL, description = "test", expirySeconds = 3600u)
208+
val result = sut.createInvoice(
209+
amountSats = 100uL,
210+
description = "test",
211+
expirySeconds = Defaults.bolt11InvoiceExpirySeconds,
212+
)
208213
assertTrue(result.isSuccess)
209214
assertEquals(testInvoice, result.getOrNull())
210215
}

0 commit comments

Comments
 (0)