Skip to content

Commit eabf0ad

Browse files
authored
Merge pull request #317 from synonymdev/fix/vss-store-id
feat: use vss client to derive store id
2 parents f6265d3 + 77b0ffb commit eabf0ad

File tree

6 files changed

+37
-38
lines changed

6 files changed

+37
-38
lines changed

app/src/main/java/to/bitkit/App.kt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,4 @@ class CurrentActivity : ActivityLifecycleCallbacks {
5555
override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle) = Unit
5656
override fun onActivityDestroyed(activity: Activity) = Unit
5757
}
58-
59-
/**
60-
* Returns the current activity of the application.
61-
*
62-
* **NEVER** store the result to a variable, further calls to such reference can lead to memory leaks.
63-
*
64-
* **ALWAYS** retrieve the current activity functionally, processing on the result of this function.
65-
* */
66-
internal inline fun <reified T> currentActivity(): T? {
67-
return when (val activity = App.currentActivity?.value) {
68-
is T -> activity
69-
else -> null
70-
}
71-
}
7258
// endregion

app/src/main/java/to/bitkit/androidServices/LightningNodeService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class LightningNodeService : Service() {
101101
ACTION_STOP_SERVICE_AND_APP -> {
102102
Logger.debug("ACTION_STOP_SERVICE_AND_APP detected", context = TAG)
103103
// Close all activities
104-
App.currentActivity?.value?.finishAffinity()
104+
App.currentActivity?.value?.finishAndRemoveTask()
105105
// Stop the service
106106
stopSelf()
107107
return START_NOT_STICKY
Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package to.bitkit.data.backup
22

3-
import org.lightningdevkit.ldknode.Network
3+
import com.synonym.vssclient.vssDeriveStoreId
44
import to.bitkit.data.keychain.Keychain
55
import to.bitkit.env.Env
6-
import to.bitkit.ext.toHex
7-
import to.bitkit.ext.toSha256
86
import to.bitkit.utils.Logger
97
import to.bitkit.utils.ServiceError
108
import javax.inject.Inject
@@ -14,21 +12,31 @@ import javax.inject.Singleton
1412
class VssStoreIdProvider @Inject constructor(
1513
private val keychain: Keychain,
1614
) {
15+
@Volatile
16+
private var cachedStoreId: String? = null
17+
1718
fun getVssStoreId(): String {
18-
// TODO Temp fix as we don't have VSS auth yet
19-
if (Env.network == Network.BITCOIN) {
20-
error(
21-
"Do not run this on mainnet until VSS auth is implemented. Below hack is a temporary fix and not safe for mainnet."
22-
)
23-
}
19+
cachedStoreId?.let { return it }
20+
21+
return synchronized(this) {
22+
cachedStoreId?.let { return it }
2423

25-
val mnemonic = keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name) ?: throw ServiceError.MnemonicNotFound
26-
val mnemonicData = mnemonic.encodeToByteArray()
27-
val hashedMnemonic = mnemonicData.toSha256()
24+
val mnemonic = keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name) ?: throw ServiceError.MnemonicNotFound
25+
val passphrase = keychain.loadString(Keychain.Key.BIP39_PASSPHRASE.name)
2826

29-
val storeIdHack = Env.vssStoreId + hashedMnemonic.toHex()
30-
Logger.info("storeIdHack: $storeIdHack")
27+
val storeId = vssDeriveStoreId(
28+
prefix = Env.vssStoreIdPrefix,
29+
mnemonic = mnemonic,
30+
passphrase = passphrase,
31+
)
32+
33+
Logger.info("VSS store id: '$storeId'", context = TAG)
34+
cachedStoreId = storeId
35+
storeId
36+
}
37+
}
3138

32-
return storeIdHack
39+
companion object {
40+
private const val TAG = "VssStoreIdProvider"
3341
}
3442
}

app/src/main/java/to/bitkit/env/Env.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,11 @@ internal object Env {
4646
}
4747

4848
val lnurlAuthSeverUrl = when (network) {
49-
// Network.REGTEST -> "http://localhost:3000/auth"
49+
// Network.REGTEST -> "http://localhost:5005/auth"
5050
else -> "" // TODO implement LNURL-auth Server for other networks
5151
}
5252

53-
val vssStoreId
54-
get() = when (network) {
55-
Network.REGTEST -> "bitkit_regtest"
56-
Network.TESTNET -> "bitkit_testnet"
57-
else -> TODO("${network.name} network not implemented")
58-
}
53+
val vssStoreIdPrefix get() = "bitkit_v1_${network.name.lowercase()}"
5954

6055
val esploraServerUrl
6156
get() = when (network) {

app/src/main/java/to/bitkit/ui/components/IsOnlineTracker.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package to.bitkit.ui.components
33
import androidx.compose.runtime.Composable
44
import androidx.compose.runtime.LaunchedEffect
55
import androidx.compose.runtime.getValue
6+
import androidx.compose.runtime.mutableStateOf
7+
import androidx.compose.runtime.remember
68
import androidx.compose.ui.platform.LocalContext
79
import androidx.lifecycle.compose.collectAsStateWithLifecycle
810
import to.bitkit.R
@@ -17,7 +19,15 @@ fun IsOnlineTracker(
1719
val context = LocalContext.current
1820
val connectivityState by app.isOnline.collectAsStateWithLifecycle(initialValue = ConnectivityState.CONNECTED)
1921

22+
val (isFirstEmission, setIsFirstEmission) = remember { mutableStateOf(true) }
23+
2024
LaunchedEffect(connectivityState) {
25+
// Skip the first emission to prevent toast on startup
26+
if (isFirstEmission) {
27+
setIsFirstEmission(true)
28+
return@LaunchedEffect
29+
}
30+
2131
when (connectivityState) {
2232
ConnectivityState.CONNECTED -> {
2333
app.toast(

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ test-junit-ext = { module = "androidx.test.ext:junit", version.ref = "junitExt"
109109
test-mockito-kotlin = { module = "org.mockito.kotlin:mockito-kotlin", version.ref = "mockitoKotlin" }
110110
test-robolectric = { module = "org.robolectric:robolectric", version.ref = "robolectric" }
111111
test-turbine = { group = "app.cash.turbine", name = "turbine", version.ref = "turbine" }
112-
vss = { module = "com.synonym:vss-client-android", version = "0.2.0" }
112+
vss = { module = "com.synonym:vss-client-android", version = "0.3.0" }
113113
work-runtime-ktx = { module = "androidx.work:work-runtime-ktx", version.ref = "workRuntimeKtx" }
114114
zxing = { module = "com.google.zxing:core", version.ref = "zxing" }
115115
lottie = { module = "com.airbnb.android:lottie-compose", version.ref = "lottieVersion" }

0 commit comments

Comments
 (0)