@@ -21,6 +21,8 @@ import javax.inject.Inject
2121import javax.inject.Singleton
2222import kotlin.time.Duration.Companion.seconds
2323
24+ class MnemonicNotAvailableException : Exception (" Mnemonic not available" )
25+
2426@Singleton
2527class VssBackupClient @Inject constructor(
2628 @IoDispatcher private val ioDispatcher : CoroutineDispatcher ,
@@ -29,14 +31,14 @@ class VssBackupClient @Inject constructor(
2931) {
3032 private var isSetup = CompletableDeferred <Unit >()
3133
32- suspend fun setup (walletIndex : Int = 0): Result <Boolean > = withContext(ioDispatcher) {
34+ suspend fun setup (walletIndex : Int = 0): Result <Unit > = withContext(ioDispatcher) {
3335 runCatching {
3436 if (isSetup.isCompleted && ! isSetup.isCancelled) {
35- runCatching { isSetup.await() }.onSuccess { return @runCatching true }
37+ runCatching { isSetup.await() }.onSuccess { return @runCatching }
3638 }
3739
3840 val mnemonic = keychain.loadString(Keychain .Key .BIP39_MNEMONIC .name)
39- ? : return @runCatching false
41+ ? : throw MnemonicNotAvailableException ()
4042
4143 withTimeout(30 .seconds) {
4244 Logger .debug(" VSS client setting up…" , context = TAG )
@@ -64,7 +66,6 @@ class VssBackupClient @Inject constructor(
6466 isSetup.complete(Unit )
6567 Logger .info(" VSS client setup with server: '$vssUrl '" , context = TAG )
6668 }
67- true
6869 }.onFailure {
6970 isSetup.completeExceptionally(it)
7071 Logger .error(" VSS client setup error" , it, context = TAG )
@@ -81,16 +82,17 @@ class VssBackupClient @Inject constructor(
8182 maxAttempts : Int = 10,
8283 baseDelayMs : Long = 1000L,
8384 logger : SetupRetryLogger .() -> Unit ,
84- ): Result <Boolean > = withContext(ioDispatcher) {
85+ ): Result <Unit > = withContext(ioDispatcher) {
8586 val log = SetupRetryLogger ().apply (logger)
8687 var attempt = 0
8788 while (attempt < maxAttempts) {
8889 val result = setup()
89- if (result.getOrDefault( false ) ) {
90+ if (result.isSuccess ) {
9091 log.onSuccess(attempt + 1 )
91- return @withContext Result .success(true )
92+ return @withContext Result .success(Unit )
9293 }
93- if (result.isFailure) {
94+ val exception = result.exceptionOrNull()
95+ if (exception != null && exception !is MnemonicNotAvailableException ) {
9496 return @withContext result
9597 }
9698 attempt++
@@ -101,7 +103,7 @@ class VssBackupClient @Inject constructor(
101103 }
102104 }
103105 log.onExhausted(maxAttempts)
104- Result .success( false )
106+ Result .failure( MnemonicNotAvailableException () )
105107 }
106108
107109 fun reset () {
0 commit comments