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
7 changes: 4 additions & 3 deletions app/src/main/java/to/bitkit/data/backup/VssBackupClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ class VssBackupClient @Inject constructor(
) {
private val isSetup = CompletableDeferred<Unit>()

suspend fun setup() = withContext(bgDispatcher) {
suspend fun setup(walletIndex: Int = 0) = withContext(bgDispatcher) {
try {
withTimeout(30.seconds) {
Logger.debug("VSS client setting up…", context = TAG)
val vssUrl = Env.vssServerUrl
val lnurlAuthServerUrl = Env.lnurlAuthServerUrl
val vssStoreId = vssStoreIdProvider.getVssStoreId(walletIndex)
Logger.verbose("Building VSS client with vssUrl: '$vssUrl'")
Logger.verbose("Building VSS client with lnurlAuthServerUrl: '$lnurlAuthServerUrl'")
if (lnurlAuthServerUrl.isNotEmpty()) {
Expand All @@ -41,15 +42,15 @@ class VssBackupClient @Inject constructor(

vssNewClientWithLnurlAuth(
baseUrl = vssUrl,
storeId = vssStoreIdProvider.getVssStoreId(),
storeId = vssStoreId,
mnemonic = mnemonic,
passphrase = passphrase,
lnurlAuthServerUrl = lnurlAuthServerUrl,
)
} else {
vssNewClient(
baseUrl = vssUrl,
storeId = vssStoreIdProvider.getVssStoreId(),
storeId = vssStoreId,
)
}
isSetup.complete(Unit)
Expand Down
22 changes: 12 additions & 10 deletions app/src/main/java/to/bitkit/data/backup/VssStoreIdProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ import to.bitkit.data.keychain.Keychain
import to.bitkit.env.Env
import to.bitkit.utils.Logger
import to.bitkit.utils.ServiceError
import java.util.concurrent.ConcurrentHashMap
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class VssStoreIdProvider @Inject constructor(
private val keychain: Keychain,
) {
@Volatile
private var cachedStoreId: String? = null
private val cacheMap: MutableMap<Int, String> = ConcurrentHashMap()

fun getVssStoreId(): String {
cachedStoreId?.let { return it }

return synchronized(this) {
cachedStoreId?.let { return it }
fun getVssStoreId(walletIndex: Int = 0): String {
synchronized(this) {
cacheMap[walletIndex]?.let { return it }

val mnemonic = keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name) ?: throw ServiceError.MnemonicNotFound
val passphrase = keychain.loadString(Keychain.Key.BIP39_PASSPHRASE.name)
Expand All @@ -30,12 +28,16 @@ class VssStoreIdProvider @Inject constructor(
passphrase = passphrase,
)

Logger.info("VSS store id: '$storeId'", context = TAG)
cachedStoreId = storeId
storeId
cacheMap[walletIndex] = storeId
Logger.info("VSS store id setup for wallet[$walletIndex]: '$storeId'", context = TAG)
return storeId
}
}

fun clearCache(walletIndex: Int = 0) {
cacheMap.remove(walletIndex)
}

companion object {
private const val TAG = "VssStoreIdProvider"
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/to/bitkit/repositories/WalletRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.lightningdevkit.ldknode.Event
import to.bitkit.data.AppDb
import to.bitkit.data.CacheStore
import to.bitkit.data.SettingsStore
import to.bitkit.data.backup.VssStoreIdProvider
import to.bitkit.data.entities.TagMetadataEntity
import to.bitkit.data.keychain.Keychain
import to.bitkit.di.BgDispatcher
Expand Down Expand Up @@ -50,6 +51,7 @@ class WalletRepo @Inject constructor(
private val lightningRepo: LightningRepo,
private val cacheStore: CacheStore,
private val deriveBalanceStateUseCase: DeriveBalanceStateUseCase,
private val vssStoreIdProvider: VssStoreIdProvider,
) {
private val repoScope = CoroutineScope(bgDispatcher + SupervisorJob())

Expand Down Expand Up @@ -210,6 +212,7 @@ class WalletRepo @Inject constructor(
suspend fun wipeWallet(walletIndex: Int = 0): Result<Unit> = withContext(bgDispatcher) {
try {
keychain.wipe()
vssStoreIdProvider.clearCache(walletIndex)
db.clearAllTables()
settingsStore.reset()
cacheStore.reset()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/to/bitkit/services/LightningService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class LightningService @Inject constructor(

Logger.debug("Building node…")

val vssStoreId = vssStoreIdProvider.getVssStoreId()
val vssStoreId = vssStoreIdProvider.getVssStoreId(walletIndex)

ServiceQueue.LDK.background {
node = try {
Expand Down
3 changes: 3 additions & 0 deletions app/src/test/java/to/bitkit/repositories/WalletRepoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import to.bitkit.data.AppDb
import to.bitkit.data.CacheStore
import to.bitkit.data.SettingsData
import to.bitkit.data.SettingsStore
import to.bitkit.data.backup.VssStoreIdProvider
import to.bitkit.data.keychain.Keychain
import to.bitkit.models.BalanceState
import to.bitkit.services.CoreService
Expand All @@ -46,6 +47,7 @@ class WalletRepoTest : BaseUnitTest() {
private val lightningRepo: LightningRepo = mock()
private val cacheStore: CacheStore = mock()
private val deriveBalanceStateUseCase: DeriveBalanceStateUseCase = mock()
private val vssStoreIdProvider = mock<VssStoreIdProvider>()

@Before
fun setUp() {
Expand Down Expand Up @@ -75,6 +77,7 @@ class WalletRepoTest : BaseUnitTest() {
lightningRepo = lightningRepo,
cacheStore = cacheStore,
deriveBalanceStateUseCase = deriveBalanceStateUseCase,
vssStoreIdProvider = vssStoreIdProvider,
)

@Test
Expand Down
Loading