Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/src/main/java/to/bitkit/env/Env.kt
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ internal object Env {

@Suppress("ConstPropertyName")
object Defaults {
/** Default Bolt11 invoice expiry in seconds. */
const val bolt11InvoiceExpirySeconds = 3_600u

/** Recommended transaction base fee in sats */
const val recommendedBaseFee = 256u

Expand Down
7 changes: 1 addition & 6 deletions app/src/main/java/to/bitkit/repositories/ActivityRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,7 @@ class ActivityRepo @Inject constructor(
getActivities(
filter = ActivityFilter.ALL,
sortDirection = SortDirection.DESC,
).getOrThrow().filter { activity ->
when (activity) {
is Activity.Lightning -> PubkyPublicKeyFormat.matches(activity.v1.contact, normalizedKey)
is Activity.Onchain -> PubkyPublicKeyFormat.matches(activity.v1.contact, normalizedKey)
}
}
).getOrThrow().filter { PubkyPublicKeyFormat.matches(it.contact(), normalizedKey) }
}.onFailure {
Logger.error("Failed to load contact activities for '$publicKey'", it, context = TAG)
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/to/bitkit/repositories/BlocktankRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import org.lightningdevkit.ldknode.ChannelDetails
import to.bitkit.async.ServiceQueue
import to.bitkit.data.CacheStore
import to.bitkit.di.BgDispatcher
import to.bitkit.env.Defaults
import to.bitkit.env.Env
import to.bitkit.ext.calculateRemoteBalance
import to.bitkit.ext.nowTimestamp
Expand Down Expand Up @@ -462,7 +463,7 @@ class BlocktankRepo @Inject constructor(
val invoice = lightningRepo.createInvoice(
amountSats = null,
description = "blocktank-gift-code:$code",
expirySeconds = 3600u,
expirySeconds = Defaults.bolt11InvoiceExpirySeconds,
).getOrThrow()

Logger.debug("Created invoice for gift code, requesting payment from LSP", context = TAG)
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/to/bitkit/repositories/LightningRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import to.bitkit.data.SettingsStore
import to.bitkit.data.backup.VssBackupClientLdk
import to.bitkit.data.keychain.Keychain
import to.bitkit.di.BgDispatcher
import to.bitkit.env.Defaults
import to.bitkit.env.Env
import to.bitkit.ext.getSatsPerVByteFor
import to.bitkit.ext.nowTimestamp
Expand Down Expand Up @@ -917,7 +918,7 @@ class LightningRepo @Inject constructor(
suspend fun createInvoice(
amountSats: ULong? = null,
description: String,
expirySeconds: UInt = 86_400u,
expirySeconds: UInt = Defaults.bolt11InvoiceExpirySeconds,
): Result<String> = executeWhenNodeRunning("createInvoice") {
updateGeoBlockState()
runCatching { lightningService.receive(amountSats, description, expirySeconds) }
Expand All @@ -926,7 +927,7 @@ class LightningRepo @Inject constructor(
suspend fun createInvoiceMsats(
amountMsats: ULong,
description: String,
expirySeconds: UInt = 86_400u,
expirySeconds: UInt = Defaults.bolt11InvoiceExpirySeconds,
): Result<String> = executeWhenNodeRunning("createInvoiceMsats") {
updateGeoBlockState()
runCatching { lightningService.receiveMsats(amountMsats, description, expirySeconds) }
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/to/bitkit/repositories/PublicPaykitRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import kotlin.time.Clock
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.minutes
import kotlin.time.ExperimentalTime
import to.bitkit.di.json as appJson

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

private val payablePreferenceOrder = listOf(
MethodId.Bolt11,
Expand All @@ -77,7 +77,7 @@ class PublicPaykitRepo @Inject constructor(

val knownMethodId = MethodId.fromRawValue(methodId) ?: return null
val payload = runCatching {
json.decodeFromString<PaymentEndpointPayload>(endpointData)
appJson.decodeFromString<PaymentEndpointPayload>(endpointData)
}.getOrNull() ?: return null
val value = payload.value.trim()
if (value.isEmpty()) return null
Expand All @@ -94,7 +94,7 @@ class PublicPaykitRepo @Inject constructor(
fun serializePayload(value: String): String {
val trimmedValue = value.trim()
if (trimmedValue.isEmpty()) throw PublicPaykitError.InvalidPayload
return json.encodeToString(PaymentEndpointPayload(value = trimmedValue))
return Json.encodeToString(PaymentEndpointPayload(value = trimmedValue))
}

fun paymentRequest(endpoints: List<Endpoint>): String {
Expand Down
13 changes: 11 additions & 2 deletions app/src/main/java/to/bitkit/services/LightningService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import to.bitkit.data.SettingsStore
import to.bitkit.data.backup.VssStoreIdProvider
import to.bitkit.data.keychain.Keychain
import to.bitkit.di.BgDispatcher
import to.bitkit.env.Defaults
import to.bitkit.env.Env
import to.bitkit.ext.totalNextOutboundHtlcLimitSats
import to.bitkit.ext.uByteList
Expand Down Expand Up @@ -592,11 +593,19 @@ class LightningService @Inject constructor(
return true
}

suspend fun receive(sat: ULong? = null, description: String, expirySecs: UInt = 3600u): String {
suspend fun receive(
sat: ULong? = null,
description: String,
expirySecs: UInt = Defaults.bolt11InvoiceExpirySeconds,
): String {
return receiveMsats(amountMsat = sat?.let { it * 1000u }, description = description, expirySecs = expirySecs)
}

suspend fun receiveMsats(amountMsat: ULong? = null, description: String, expirySecs: UInt = 3600u): String {
suspend fun receiveMsats(
amountMsat: ULong? = null,
description: String,
expirySecs: UInt = Defaults.bolt11InvoiceExpirySeconds,
): String {
val node = this.node ?: throw ServiceError.NodeNotSetup()

val message = description
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1373,8 +1373,10 @@ class AppViewModel @Inject constructor(

fun preserveContactPaymentContext(paymentHash: String) {
synchronized(contactPaymentContextLock) {
activeContactPaymentContext?.let {
pendingContactPaymentContexts[paymentHash] = it
val context = activeContactPaymentContext
if (context != null) {
pendingContactPaymentContexts[paymentHash] = context
activeContactPaymentContext = null
}
}
}
Expand Down Expand Up @@ -2069,7 +2071,7 @@ class AppViewModel @Inject constructor(
lightningRepo.createInvoiceMsats(
amountMsats = lnurl.data.maxWithdrawable,
description = lnurl.data.defaultDescription,
expirySeconds = 3600u,
expirySeconds = Defaults.bolt11InvoiceExpirySeconds,
)
} else {
val withdrawAmountSats = _sendUiState.value.amount.coerceAtLeast(
Expand All @@ -2079,7 +2081,7 @@ class AppViewModel @Inject constructor(
lightningRepo.createInvoice(
amountSats = withdrawAmountSats,
description = lnurl.data.defaultDescription,
expirySeconds = 3600u,
expirySeconds = Defaults.bolt11InvoiceExpirySeconds,
)
}.getOrNull()

Expand Down
9 changes: 7 additions & 2 deletions app/src/test/java/to/bitkit/repositories/LightningRepoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import to.bitkit.data.SettingsData
import to.bitkit.data.SettingsStore
import to.bitkit.data.backup.VssBackupClientLdk
import to.bitkit.data.keychain.Keychain
import to.bitkit.env.Defaults
import to.bitkit.ext.createChannelDetails
import to.bitkit.ext.of
import to.bitkit.models.CoinSelectionPreference
Expand Down Expand Up @@ -200,11 +201,15 @@ class LightningRepoTest : BaseUnitTest() {
lightningService.receive(
sat = 100uL,
description = "test",
expirySecs = 3600u
expirySecs = Defaults.bolt11InvoiceExpirySeconds,
)
).thenReturn(testInvoice)

val result = sut.createInvoice(amountSats = 100uL, description = "test", expirySeconds = 3600u)
val result = sut.createInvoice(
amountSats = 100uL,
description = "test",
expirySeconds = Defaults.bolt11InvoiceExpirySeconds,
)
assertTrue(result.isSuccess)
assertEquals(testInvoice, result.getOrNull())
}
Expand Down
Loading
Loading