|
| 1 | +package to.bitkit.data.backup |
| 2 | + |
| 3 | +import com.synonym.vssclient.KeyVersion |
| 4 | +import com.synonym.vssclient.LdkNamespace |
| 5 | +import com.synonym.vssclient.VssItem |
| 6 | +import com.synonym.vssclient.vssLdkDelete |
| 7 | +import com.synonym.vssclient.vssLdkGet |
| 8 | +import com.synonym.vssclient.vssLdkListKeys |
| 9 | +import com.synonym.vssclient.vssNewLdkClientWithLnurlAuth |
| 10 | +import kotlinx.coroutines.CompletableDeferred |
| 11 | +import kotlinx.coroutines.CoroutineDispatcher |
| 12 | +import kotlinx.coroutines.sync.Mutex |
| 13 | +import kotlinx.coroutines.sync.withLock |
| 14 | +import kotlinx.coroutines.withContext |
| 15 | +import kotlinx.coroutines.withTimeout |
| 16 | +import to.bitkit.data.keychain.Keychain |
| 17 | +import to.bitkit.di.IoDispatcher |
| 18 | +import to.bitkit.env.Env |
| 19 | +import to.bitkit.utils.Logger |
| 20 | +import javax.inject.Inject |
| 21 | +import javax.inject.Singleton |
| 22 | +import kotlin.time.Duration.Companion.seconds |
| 23 | + |
| 24 | +@Suppress("TooManyFunctions") |
| 25 | +@Singleton |
| 26 | +class VssBackupClientLdk @Inject constructor( |
| 27 | + @IoDispatcher private val ioDispatcher: CoroutineDispatcher, |
| 28 | + private val vssStoreIdProvider: VssStoreIdProvider, |
| 29 | + private val keychain: Keychain, |
| 30 | +) { |
| 31 | + companion object { |
| 32 | + private const val TAG = "VssBackupClientLdk" |
| 33 | + |
| 34 | + private val NAMESPACES = listOf( |
| 35 | + LdkNamespace.Default, |
| 36 | + LdkNamespace.Monitors, |
| 37 | + LdkNamespace.ArchivedMonitors, |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + private var isSetup = CompletableDeferred<Unit>() |
| 42 | + private val setupMutex = Mutex() |
| 43 | + |
| 44 | + suspend fun setup(walletIndex: Int = 0): Result<Unit> = withContext(ioDispatcher) { |
| 45 | + setupMutex.withLock { |
| 46 | + runCatching { |
| 47 | + if (isSetup.isCompleted && !isSetup.isCancelled) { |
| 48 | + runCatching { isSetup.await() }.onSuccess { return@runCatching } |
| 49 | + } |
| 50 | + |
| 51 | + val mnemonic = keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name) |
| 52 | + ?: throw MnemonicNotAvailableException() |
| 53 | + |
| 54 | + withTimeout(30.seconds) { |
| 55 | + val passphrase = keychain.loadString(Keychain.Key.BIP39_PASSPHRASE.name) |
| 56 | + vssNewLdkClientWithLnurlAuth( |
| 57 | + baseUrl = Env.vssServerUrl, |
| 58 | + storeId = vssStoreIdProvider.getVssStoreId(walletIndex), |
| 59 | + mnemonic = mnemonic, |
| 60 | + passphrase = passphrase, |
| 61 | + lnurlAuthServerUrl = Env.lnurlAuthServerUrl, |
| 62 | + ) |
| 63 | + isSetup.complete(Unit) |
| 64 | + Logger.info("VSS LDK client setup", context = TAG) |
| 65 | + } |
| 66 | + }.onFailure { |
| 67 | + isSetup.completeExceptionally(it) |
| 68 | + Logger.error("VSS LDK client setup error", it, context = TAG) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + fun reset() { |
| 74 | + synchronized(this) { |
| 75 | + isSetup.cancel() |
| 76 | + isSetup = CompletableDeferred() |
| 77 | + } |
| 78 | + Logger.debug("VSS LDK client reset", context = TAG) |
| 79 | + } |
| 80 | + |
| 81 | + suspend fun getObject( |
| 82 | + key: String, |
| 83 | + namespace: LdkNamespace = LdkNamespace.Default, |
| 84 | + ): Result<VssItem?> = withContext(ioDispatcher) { |
| 85 | + isSetup.await() |
| 86 | + Logger.verbose("VSS LDK 'getObject' call for '$key'", context = TAG) |
| 87 | + runCatching { |
| 88 | + vssLdkGet(key = key, namespace = namespace) |
| 89 | + }.onSuccess { |
| 90 | + if (it == null) { |
| 91 | + Logger.verbose("VSS LDK 'getObject' success null for '$key'", context = TAG) |
| 92 | + } else { |
| 93 | + Logger.verbose("VSS LDK 'getObject' success for '$key'", context = TAG) |
| 94 | + } |
| 95 | + }.onFailure { |
| 96 | + Logger.verbose("VSS LDK 'getObject' error for '$key'", it, context = TAG) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + suspend fun deleteObject( |
| 101 | + key: String, |
| 102 | + namespace: LdkNamespace = LdkNamespace.Default, |
| 103 | + ): Result<Boolean> = withContext(ioDispatcher) { |
| 104 | + isSetup.await() |
| 105 | + Logger.verbose("VSS LDK 'deleteObject' call for '$key'", context = TAG) |
| 106 | + runCatching { |
| 107 | + vssLdkDelete(key = key, namespace = namespace) |
| 108 | + }.onSuccess { wasDeleted -> |
| 109 | + if (wasDeleted) { |
| 110 | + Logger.verbose("VSS LDK 'deleteObject' success for '$key' - key was deleted", context = TAG) |
| 111 | + } else { |
| 112 | + Logger.verbose("VSS LDK 'deleteObject' success for '$key' - key did not exist", context = TAG) |
| 113 | + } |
| 114 | + }.onFailure { |
| 115 | + Logger.verbose("VSS LDK 'deleteObject' error for '$key'", it, context = TAG) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + suspend fun listAllKeysTagged(): Result<List<Pair<LdkNamespace, KeyVersion>>> = withContext(ioDispatcher) { |
| 120 | + isSetup.await() |
| 121 | + Logger.verbose("VSS LDK 'listAllKeysTagged' call", context = TAG) |
| 122 | + runCatching { |
| 123 | + NAMESPACES.flatMap { ns -> vssLdkListKeys(namespace = ns).map { ns to it } } |
| 124 | + }.onSuccess { |
| 125 | + Logger.verbose("VSS LDK 'listAllKeysTagged' success - found ${it.size} key(s)", context = TAG) |
| 126 | + }.onFailure { |
| 127 | + Logger.verbose("VSS LDK 'listAllKeysTagged' error", it, context = TAG) |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments