Skip to content

Commit daf6515

Browse files
committed
fix: wrap pubky init in io context
1 parent 9bdfc4f commit daf6515

1 file changed

Lines changed: 23 additions & 25 deletions

File tree

app/src/main/java/to/bitkit/repositories/PubkyRepo.kt

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -125,28 +125,26 @@ class PubkyRepo @Inject constructor(
125125

126126
// region Initialization
127127

128-
suspend fun initialize() {
128+
suspend fun initialize() = withContext(ioDispatcher) {
129129
initializeMutex.withLock {
130130
_sessionRestorationFailed.update { false }
131131
val result = runCatching {
132-
withContext(ioDispatcher) {
133-
pubkyService.initialize()
134-
135-
val savedSessionSecret = runCatching {
136-
keychain.loadString(Keychain.Key.PAYKIT_SESSION.name)
137-
}.getOrNull()
138-
val storedSecretKeyHex = runCatching {
139-
keychain.loadString(Keychain.Key.PUBKY_SECRET_KEY.name)
140-
}.getOrNull()
141-
142-
resolveSessionInitialization(
143-
savedSessionSecret = savedSessionSecret,
144-
storedSecretKeyHex = storedSecretKeyHex,
145-
)
146-
}
132+
pubkyService.initialize()
133+
134+
val savedSessionSecret = runCatching {
135+
keychain.loadString(Keychain.Key.PAYKIT_SESSION.name)
136+
}.getOrNull()
137+
val storedSecretKeyHex = runCatching {
138+
keychain.loadString(Keychain.Key.PUBKY_SECRET_KEY.name)
139+
}.getOrNull()
140+
141+
resolveSessionInitialization(
142+
savedSessionSecret = savedSessionSecret,
143+
storedSecretKeyHex = storedSecretKeyHex,
144+
)
147145
}.onFailure {
148146
Logger.error("Failed to initialize paykit", it, context = TAG)
149-
}.getOrNull() ?: return
147+
}.getOrNull() ?: return@withLock
150148

151149
when (result) {
152150
is InitResult.NoSession -> {
@@ -708,7 +706,7 @@ class PubkyRepo @Inject constructor(
708706
}
709707
}
710708

711-
suspend fun clearPendingImport() {
709+
suspend fun clearPendingImport() = withContext(ioDispatcher) {
712710
_pendingImportProfile.update { null }
713711
_pendingImportContacts.update { emptyList() }
714712
}
@@ -862,22 +860,22 @@ class PubkyRepo @Inject constructor(
862860
null
863861
}
864862

865-
private suspend fun deriveLocalSecretKeyFromWalletSeed(): String {
863+
private suspend fun deriveLocalSecretKeyFromWalletSeed(): String = withContext(ioDispatcher) {
866864
val mnemonic = requireNotNull(keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name)) {
867865
"BIP39 mnemonic not found in keychain"
868866
}
869867
val passphrase = keychain.loadString(Keychain.Key.BIP39_PASSPHRASE.name)
870868
val seed = pubkyService.mnemonicToSeed(mnemonic, passphrase)
871-
return pubkyService.deriveSecretKey(seed)
869+
pubkyService.deriveSecretKey(seed)
872870
}
873871

874872
private fun notifyBackupStateChanged() {
875873
_backupStateVersion.update { it + 1 }
876874
}
877875

878-
private suspend fun clearAuthenticatedState() {
876+
private suspend fun clearAuthenticatedState() = withContext(ioDispatcher) {
879877
evictPubkyImages()
880-
runCatching { withContext(ioDispatcher) { pubkyStore.reset() } }
878+
runCatching { pubkyStore.reset() }
881879
_publicKey.update { null }
882880
_profile.update { null }
883881
_contacts.update { emptyList() }
@@ -886,9 +884,9 @@ class PubkyRepo @Inject constructor(
886884
_authState.update { PubkyAuthState.Idle }
887885
}
888886

889-
private suspend fun clearLocalState() {
890-
runCatching { withContext(ioDispatcher) { keychain.delete(Keychain.Key.PAYKIT_SESSION.name) } }
891-
runCatching { withContext(ioDispatcher) { keychain.delete(Keychain.Key.PUBKY_SECRET_KEY.name) } }
887+
private suspend fun clearLocalState() = withContext(ioDispatcher) {
888+
runCatching { keychain.delete(Keychain.Key.PAYKIT_SESSION.name) }
889+
runCatching { keychain.delete(Keychain.Key.PUBKY_SECRET_KEY.name) }
892890
notifyBackupStateChanged()
893891
clearAuthenticatedState()
894892
}

0 commit comments

Comments
 (0)