This commit is contained in:
Vitor Pamplona
2023-11-22 08:44:53 -05:00
7 changed files with 116 additions and 51 deletions
@@ -26,9 +26,11 @@ import com.vitorpamplona.amethyst.service.checkNotInMainThread
import com.vitorpamplona.quartz.crypto.KeyPair
import com.vitorpamplona.quartz.encoders.hexToByteArray
import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.encoders.toNpub
import com.vitorpamplona.quartz.events.ContactListEvent
import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.events.LnZapEvent
import com.vitorpamplona.quartz.signers.ExternalSignerLauncher
import com.vitorpamplona.quartz.signers.NostrSignerExternal
import com.vitorpamplona.quartz.signers.NostrSignerInternal
import kotlinx.coroutines.Dispatchers
@@ -89,6 +91,7 @@ private object PrefKeys {
const val AUTOMATICALLY_HIDE_NAV_BARS = "automatically_hide_nav_bars"
const val LOGIN_WITH_EXTERNAL_SIGNER = "login_with_external_signer"
const val AUTOMATICALLY_SHOW_PROFILE_PICTURE = "automatically_show_profile_picture"
const val SIGNER_PACKAGE_NAME = "signer_package_name"
const val ALL_ACCOUNT_INFO = "all_saved_accounts_info"
const val SHARED_SETTINGS = "shared_settings"
@@ -249,6 +252,7 @@ object LocalPreferences {
putBoolean(PrefKeys.LOGIN_WITH_EXTERNAL_SIGNER, account.signer is NostrSignerExternal)
if (account.signer is NostrSignerExternal) {
remove(PrefKeys.NOSTR_PRIVKEY)
putString(PrefKeys.SIGNER_PACKAGE_NAME, account.signer.launcher.signerPackageName)
} else {
account.keyPair.privKey?.let { putString(PrefKeys.NOSTR_PRIVKEY, it.toHexKey()) }
}
@@ -475,7 +479,8 @@ object LocalPreferences {
val keyPair = KeyPair(privKey = privKey?.hexToByteArray(), pubKey = pubKey.hexToByteArray())
val signer = if (loginWithExternalSigner) {
NostrSignerExternal(pubKey)
val packageName = getString(PrefKeys.SIGNER_PACKAGE_NAME, null) ?: "com.greenart7c3.nostrsigner"
NostrSignerExternal(pubKey, ExternalSignerLauncher(pubKey.hexToByteArray().toNpub(), packageName))
} else {
NostrSignerInternal(keyPair)
}
@@ -49,6 +49,8 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.net.URLEncoder
import java.nio.charset.StandardCharsets
import java.util.Timer
import kotlin.concurrent.schedule
class MainActivity : AppCompatActivity() {
private val isOnMobileDataState = mutableStateOf(false)
@@ -108,6 +110,7 @@ class MainActivity : AppCompatActivity() {
DefaultMutedSetting.value = true
// Keep connection alive if it's calling the signer app
Log.d("shouldPauseService", "shouldPauseService onResume: $shouldPauseService")
if (shouldPauseService) {
GlobalScope.launch(Dispatchers.IO) {
serviceManager.justStart()
@@ -121,7 +124,9 @@ class MainActivity : AppCompatActivity() {
(getSystemService(ConnectivityManager::class.java) as ConnectivityManager).registerDefaultNetworkCallback(networkCallback)
// resets state until next External Signer Call
shouldPauseService = true
Timer().schedule(350) {
shouldPauseService = true
}
}
override fun onPause() {
@@ -134,6 +139,7 @@ class MainActivity : AppCompatActivity() {
}
// }
Log.d("shouldPauseService", "shouldPauseService onPause: $shouldPauseService")
if (shouldPauseService) {
GlobalScope.launch(Dispatchers.IO) {
serviceManager.pauseForGood()
@@ -171,8 +177,11 @@ class MainActivity : AppCompatActivity() {
override fun onAvailable(network: Network) {
super.onAvailable(network)
GlobalScope.launch(Dispatchers.IO) {
serviceManager.forceRestart()
Log.d("shouldPauseService", "shouldPauseService onAvailable: $shouldPauseService")
if (shouldPauseService) {
GlobalScope.launch(Dispatchers.IO) {
serviceManager.forceRestart()
}
}
}
@@ -202,7 +211,8 @@ class MainActivity : AppCompatActivity() {
changedNetwork = true
}
if (changedNetwork) {
Log.d("shouldPauseService", "shouldPauseService onCapabilitiesChanged: $shouldPauseService")
if (changedNetwork && shouldPauseService) {
GlobalScope.launch(Dispatchers.IO) {
serviceManager.forceRestart()
}
@@ -34,6 +34,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.service.lnurl.LightningAddressResolver
import com.vitorpamplona.amethyst.ui.theme.QuoteBorder
import com.vitorpamplona.amethyst.ui.theme.placeholderText
@@ -170,6 +171,7 @@ fun InvoiceRequest(
)
} else {
account.createZapRequestFor(toUserPubKeyHex, message, account.defaultZapType) { zapRequest ->
LocalCache.justConsume(zapRequest, null)
LightningAddressResolver().lnAddressInvoice(
lud16,
amount * 1000,
@@ -14,6 +14,8 @@ import com.vitorpamplona.quartz.encoders.Nip19
import com.vitorpamplona.quartz.encoders.bechToBytes
import com.vitorpamplona.quartz.encoders.hexToByteArray
import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.encoders.toNpub
import com.vitorpamplona.quartz.signers.ExternalSignerLauncher
import com.vitorpamplona.quartz.signers.NostrSignerExternal
import com.vitorpamplona.quartz.signers.NostrSignerInternal
import kotlinx.coroutines.DelicateCoroutinesApi
@@ -56,7 +58,8 @@ class AccountStateViewModel() : ViewModel() {
key: String,
useProxy: Boolean,
proxyPort: Int,
loginWithExternalSigner: Boolean = false
loginWithExternalSigner: Boolean = false,
packageName: String = ""
) = withContext(Dispatchers.IO) {
val parsed = Nip19.uriToRoute(key)
val pubKeyParsed = parsed?.hex?.hexToByteArray()
@@ -69,7 +72,8 @@ class AccountStateViewModel() : ViewModel() {
val account =
if (loginWithExternalSigner) {
val keyPair = KeyPair(pubKey = pubKeyParsed)
Account(keyPair, proxy = proxy, proxyPort = proxyPort, signer = NostrSignerExternal(keyPair.pubKey.toHexKey()))
val localPackageName = packageName.ifBlank { "com.greenart7c3.nostrsigner" }
Account(keyPair, proxy = proxy, proxyPort = proxyPort, signer = NostrSignerExternal(keyPair.pubKey.toHexKey(), ExternalSignerLauncher(keyPair.pubKey.toNpub(), localPackageName)))
} else if (key.startsWith("nsec")) {
val keyPair = KeyPair(privKey = key.bechToBytes())
Account(keyPair, proxy = proxy, proxyPort = proxyPort, signer = NostrSignerInternal(keyPair))
@@ -130,11 +134,12 @@ class AccountStateViewModel() : ViewModel() {
useProxy: Boolean,
proxyPort: Int,
loginWithExternalSigner: Boolean = false,
packageName: String = "",
onError: () -> Unit
) {
viewModelScope.launch(Dispatchers.IO) {
try {
loginAndStartUI(key, useProxy, proxyPort, loginWithExternalSigner)
loginAndStartUI(key, useProxy, proxyPort, loginWithExternalSigner, packageName)
} catch (e: Exception) {
Log.e("Login", "Could not sign in", e)
onError()
@@ -108,7 +108,7 @@ fun LoginPage(
var loginWithExternalSigner by remember { mutableStateOf(false) }
if (loginWithExternalSigner) {
val externalSignerLauncher = remember { ExternalSignerLauncher("") }
val externalSignerLauncher = remember { ExternalSignerLauncher("", signerPackageName = "") }
val id = remember { UUID.randomUUID().toString() }
val launcher = rememberLauncherForActivityResult(
@@ -162,8 +162,11 @@ fun LoginPage(
SignerType.GET_PUBLIC_KEY,
"",
id
) { pubkey ->
) { result ->
println("AAAA- COME BACK")
val split = result.split("-")
val pubkey = split.first()
val packageName = if (split.size > 1) split[1] else ""
key.value = TextFieldValue(pubkey)
if (!acceptedTerms.value) {
termsAcceptanceIsRequired =
@@ -175,7 +178,7 @@ fun LoginPage(
}
if (acceptedTerms.value && key.value.text.isNotBlank()) {
accountViewModel.login(key.value.text, useProxy.value, proxyPort.value.toInt(), true) {
accountViewModel.login(key.value.text, useProxy.value, proxyPort.value.toInt(), true, packageName) {
errorMessage = context.getString(R.string.invalid_key)
}
}
@@ -432,33 +435,8 @@ fun LoginPage(
return@Button
}
val result = ExternalSignerLauncher("").getDataFromResolver(
SignerType.GET_PUBLIC_KEY,
arrayOf("login"),
contentResolver = Amethyst.instance.contentResolver
)
if (result == null) {
loginWithExternalSigner = true
return@Button
} else {
key.value = TextFieldValue(result)
if (key.value.text.isBlank()) {
errorMessage = context.getString(R.string.key_is_required)
return@Button
}
if (acceptedTerms.value && key.value.text.isNotBlank()) {
accountViewModel.login(
key.value.text,
useProxy.value,
proxyPort.value.toInt(),
true
) {
errorMessage = context.getString(R.string.invalid_key)
}
}
}
loginWithExternalSigner = true
return@Button
},
shape = RoundedCornerShape(Size35dp),
modifier = Modifier