@@ -16,7 +16,6 @@ import to.bitkit.data.keychain.Keychain
1616import to.bitkit.di.IoDispatcher
1717import to.bitkit.env.Env
1818import to.bitkit.utils.Logger
19- import to.bitkit.utils.ServiceError
2019import javax.inject.Inject
2120import javax.inject.Singleton
2221import kotlin.time.Duration.Companion.seconds
@@ -29,7 +28,23 @@ class VssBackupClient @Inject constructor(
2928) {
3029 private var isSetup = CompletableDeferred <Unit >()
3130
32- suspend fun setup (walletIndex : Int = 0) = withContext(ioDispatcher) {
31+ /* *
32+ * Sets up the VSS client. Returns true if setup succeeded, false if mnemonic not available.
33+ * Throws on other errors.
34+ */
35+ suspend fun setup (walletIndex : Int = 0): Boolean = withContext(ioDispatcher) {
36+ // If already set up successfully, return immediately
37+ if (isSetup.isCompleted && ! isSetup.isCancelled) {
38+ runCatching { isSetup.await() }.onSuccess { return @withContext true }
39+ }
40+
41+ // Check if mnemonic is available before proceeding
42+ val mnemonic = keychain.loadString(Keychain .Key .BIP39_MNEMONIC .name)
43+ if (mnemonic == null ) {
44+ Logger .warn(" VSS client setup deferred: mnemonic not available yet" , context = TAG )
45+ return @withContext false
46+ }
47+
3348 runCatching {
3449 withTimeout(30 .seconds) {
3550 Logger .debug(" VSS client setting up…" , context = TAG )
@@ -39,8 +54,6 @@ class VssBackupClient @Inject constructor(
3954 Logger .verbose(" Building VSS client with vssUrl: '$vssUrl '" , context = TAG )
4055 Logger .verbose(" Building VSS client with lnurlAuthServerUrl: '$lnurlAuthServerUrl '" , context = TAG )
4156 if (lnurlAuthServerUrl.isNotEmpty()) {
42- val mnemonic = keychain.loadString(Keychain .Key .BIP39_MNEMONIC .name)
43- ? : throw ServiceError .MnemonicNotFound ()
4457 val passphrase = keychain.loadString(Keychain .Key .BIP39_PASSPHRASE .name)
4558
4659 vssNewClientWithLnurlAuth(
@@ -63,6 +76,7 @@ class VssBackupClient @Inject constructor(
6376 isSetup.completeExceptionally(it)
6477 Logger .error(" VSS client setup error" , e = it, context = TAG )
6578 }
79+ true
6680 }
6781
6882 fun reset () {
0 commit comments