Skip to content

Commit d6fb74b

Browse files
committed
fix: persist lightning node id to database
1 parent fea384e commit d6fb74b

File tree

8 files changed

+60
-10
lines changed

8 files changed

+60
-10
lines changed

common/src/commonMain/kotlin/com/blockstream/common/data/WalletExtras.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import kotlinx.serialization.Serializable
55

66
@Serializable
77
data class WalletExtras(
8-
val totalBalanceInFiat: Boolean = false
8+
val totalBalanceInFiat: Boolean = false,
9+
val lightningNodeId: String? = null
910
) : GreenJson<WalletExtras>() {
1011
override fun kSerializer() = serializer()
1112

common/src/commonMain/kotlin/com/blockstream/common/di/CommonModule.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.blockstream.common.usecases.SetPinUseCase
1010
import com.blockstream.domain.banner.GetBannerUseCase
1111
import com.blockstream.domain.bitcoinpricehistory.ObserveBitcoinPriceHistory
1212
import com.blockstream.domain.hardware.VerifyAddressUseCase
13+
import com.blockstream.domain.lightning.LightningNodeIdUseCase
1314
import com.blockstream.domain.meld.CreateCryptoQuoteUseCase
1415
import com.blockstream.domain.meld.CreateCryptoWidgetUseCase
1516
import com.blockstream.domain.meld.DefaultValuesUseCase
@@ -19,12 +20,17 @@ import com.blockstream.domain.navigation.NavigateToWallet
1920
import com.blockstream.domain.promo.GetPromoUseCase
2021
import com.blockstream.green.data.dataModule
2122
import com.blockstream.green.domain.domainModule
23+
import org.koin.core.module.dsl.singleOf
2224
import org.koin.dsl.module
2325

2426
//At some point we'll move this to domain module.
2527
val commonModule = module {
2628
includes(dataModule)
2729
includes(domainModule)
30+
singleOf(::RestoreWalletUseCase)
31+
single {
32+
LightningNodeIdUseCase(get())
33+
}
2834
single {
2935
NavigateToWallet(get(), get())
3036
}
@@ -46,9 +52,6 @@ val commonModule = module {
4652
single {
4753
NewWalletUseCase(get(), get(), get(), get(), get(), get(), get())
4854
}
49-
single {
50-
RestoreWalletUseCase(get(), get(), get(), get(), get(), get(), get())
51-
}
5255
single {
5356
CheckRecoveryPhraseUseCase(get())
5457
}
@@ -62,7 +65,7 @@ val commonModule = module {
6265
EnableHardwareWatchOnlyUseCase(get(), get())
6366
}
6467
single {
65-
CreateAccountUseCase(get(), get(), get())
68+
CreateAccountUseCase(get(), get(), get(), get())
6669
}
6770

6871
factory {

common/src/commonMain/kotlin/com/blockstream/common/gdk/GdkSession.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ class GdkSession constructor(
463463
var xPubHashId: String? = null
464464
private set
465465

466+
var lightningNodeId: String? = null
467+
get() = lightningSdkOrNull?.nodeInfoStateFlow?.value?.id.takeIf { it.isNotBlank() } ?: field
468+
private set
469+
466470
var pendingTransactionParams: CreateTransactionParams? = null
467471
var pendingTransaction: PendingTransaction? = null
468472

@@ -766,6 +770,7 @@ class GdkSession constructor(
766770

767771
_isConnectedState.value = false
768772
xPubHashId = null
773+
lightningNodeId = null
769774

770775
authenticationRequired.clear()
771776

@@ -1730,6 +1735,8 @@ class GdkSession constructor(
17301735
_isConnectedState.value = true
17311736
xPubHashId = if (isNoBlobWatchOnly && !isHwWatchOnly) loginData.networkHashId else loginData.xpubHashId
17321737

1738+
lightningNodeId = wallet?.extras?.lightningNodeId
1739+
17331740
if (initializeSession) {
17341741
countly.activeWalletStart()
17351742
initializeSessionData(wallet, initNetwork, initAccount)
@@ -3306,9 +3313,14 @@ class GdkSession constructor(
33063313
}
33073314
}
33083315

3309-
fun supportId() = allAccounts.value.filter {
3310-
it.isMultisig && it.pointer == 0L || it.isLightning
3311-
}.joinToString(",") { "${it.network.bip21Prefix}:${if (it.isLightning) lightningSdk.nodeInfoStateFlow.value.id else it.receivingId}" }
3316+
fun supportId(): String = (allAccounts.value.filter {
3317+
it.isMultisig && it.pointer == 0L
3318+
}.map { "${it.network.bip21Prefix}:${it.receivingId}" } + listOfNotNull(
3319+
lightning?.bip21Prefix?.let { bip21Prefix ->
3320+
lightningNodeId?.let { "$bip21Prefix:$it" }
3321+
}
3322+
3323+
)).joinToString(",")
33123324

33133325
internal fun destroy(disconnect: Boolean = true) {
33143326
if (disconnect) {

common/src/commonMain/kotlin/com/blockstream/common/models/login/LoginViewModel.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import com.blockstream.common.sideeffects.SideEffects
6060
import com.blockstream.common.usecases.EnableHardwareWatchOnlyUseCase
6161
import com.blockstream.common.utils.SetupDevelopmentEnv
6262
import com.blockstream.common.utils.StringHolder
63+
import com.blockstream.domain.lightning.LightningNodeIdUseCase
6364
import com.blockstream.green.data.banner.Banner
6465
import com.blockstream.green.utils.Loggable
6566
import com.blockstream.ui.events.Event
@@ -158,6 +159,7 @@ class LoginViewModel constructor(
158159

159160
override val isLoginRequired: Boolean = false
160161

162+
private val lightningNodeIdUseCase: LightningNodeIdUseCase by inject()
161163
private val enableHardwareWatchOnlyUseCase: EnableHardwareWatchOnlyUseCase by inject()
162164
private val setupDevelopmentEnv = SetupDevelopmentEnv() // Only for dev env
163165

@@ -978,6 +980,8 @@ class LoginViewModel constructor(
978980
loginCredentials = loginCredentials
979981
)
980982

983+
lightningNodeIdUseCase.invoke(wallet = pair.first, session = session)
984+
981985
if (isWatchOnlyUpgrade) {
982986
sessionManager.getWalletSessionOrNull(greenWallet)?.also { woSession ->
983987
logger.d { "Upgrade wo session to full" }

common/src/commonMain/kotlin/com/blockstream/common/usecases/CreateAccountUseCase.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.blockstream.common.gdk.data.Network
1818
import com.blockstream.common.gdk.device.DeviceResolver
1919
import com.blockstream.common.gdk.device.HardwareWalletInteraction
2020
import com.blockstream.common.gdk.params.SubAccountParams
21+
import com.blockstream.domain.lightning.LightningNodeIdUseCase
2122
import com.blockstream.green.utils.Loggable
2223
import kotlinx.coroutines.Dispatchers
2324
import kotlinx.coroutines.IO
@@ -26,7 +27,8 @@ import kotlinx.coroutines.withContext
2627
class CreateAccountUseCase(
2728
val database: Database,
2829
val greenKeystore: GreenKeystore,
29-
val countly: CountlyBase
30+
val countly: CountlyBase,
31+
val lightningNodeIdUseCase: LightningNodeIdUseCase,
3032
) : Loggable() {
3133

3234
suspend operator fun invoke(
@@ -96,6 +98,9 @@ class CreateAccountUseCase(
9698
}
9799
}
98100

101+
// Save Lightning Node Id
102+
lightningNodeIdUseCase.invoke(wallet = greenWallet, session = session)
103+
99104
return session.lightningAccount
100105
} else {
101106

common/src/commonMain/kotlin/com/blockstream/common/usecases/RestoreWalletUseCase.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.blockstream.common.gdk.params.LoginCredentialsParams
1313
import com.blockstream.common.managers.SessionManager
1414
import com.blockstream.common.managers.SettingsManager
1515
import com.blockstream.common.utils.generateWalletName
16+
import com.blockstream.domain.lightning.LightningNodeIdUseCase
1617
import kotlinx.coroutines.Dispatchers
1718
import kotlinx.coroutines.IO
1819
import kotlinx.coroutines.withContext
@@ -25,6 +26,7 @@ class RestoreWalletUseCase(
2526
private val settingsManager: SettingsManager,
2627
private val setPinUseCase: SetPinUseCase,
2728
private val setBiometricsUseCase: SetBiometricsUseCase,
29+
private val lightningNodeIdUseCase: LightningNodeIdUseCase
2830
) {
2931

3032
suspend operator fun invoke(
@@ -90,6 +92,8 @@ class RestoreWalletUseCase(
9092
encryptedData = encryptedData
9193
)
9294
)
95+
96+
lightningNodeIdUseCase.invoke(wallet = wallet, session = session)
9397
}
9498

9599
sessionManager.upgradeOnBoardingSessionToWallet(wallet)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.blockstream.domain.lightning
2+
3+
import com.blockstream.common.data.GreenWallet
4+
import com.blockstream.common.data.WalletExtras
5+
import com.blockstream.common.database.Database
6+
import com.blockstream.common.extensions.isNotBlank
7+
import com.blockstream.common.gdk.GdkSession
8+
import com.blockstream.jade.Loggable
9+
10+
class LightningNodeIdUseCase(private val database: Database) : Loggable() {
11+
suspend operator fun invoke(wallet: GreenWallet, session: GdkSession) {
12+
val lightningNodeId = session.lightningNodeId
13+
14+
logger.d { "Lightning NodeId: $lightningNodeId" }
15+
16+
if (!wallet.isEphemeral && lightningNodeId.isNotBlank() && wallet.extras?.lightningNodeId != lightningNodeId) {
17+
wallet.extras = wallet.extras?.copy(lightningNodeId = lightningNodeId) ?: WalletExtras(lightningNodeId = lightningNodeId)
18+
database.updateWallet(wallet)
19+
}
20+
}
21+
}

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ktor = "3.2.3"
1414
phosphorIcon = "1.0.0"
1515
compose-rebugger = "1.0.1"
1616
tuulbox-coroutines = "8.0.3"
17-
android-gradle-plugin = "8.12.1"
17+
android-gradle-plugin = "8.12.2"
1818
androidCompileSdk = "36"
1919
androidTargetSdk = "36"
2020
androidMinSdk = "24"

0 commit comments

Comments
 (0)