From c630f9d362c373c2aaebb9736e9e5ec2de8f7a6c Mon Sep 17 00:00:00 2001 From: greenart7c3 Date: Wed, 11 Dec 2024 07:53:46 -0300 Subject: [PATCH] show error message instead of toast message when signup --- .../nostrsigner/LocalPreferences.kt | 15 +++++++ .../greenart7c3/nostrsigner/ui/LoginScreen.kt | 43 ++++++++++++++++--- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/LocalPreferences.kt b/app/src/main/java/com/greenart7c3/nostrsigner/LocalPreferences.kt index d1aea92..703ce6b 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/LocalPreferences.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/LocalPreferences.kt @@ -14,7 +14,9 @@ import com.greenart7c3.nostrsigner.ui.parseBiometricsTimeType import com.vitorpamplona.ammolite.relays.COMMON_FEED_TYPES import com.vitorpamplona.ammolite.relays.RelaySetupInfo import com.vitorpamplona.ammolite.service.HttpClientManager +import com.vitorpamplona.quartz.crypto.CryptoUtils import com.vitorpamplona.quartz.crypto.KeyPair +import com.vitorpamplona.quartz.encoders.HexKey import com.vitorpamplona.quartz.encoders.hexToByteArray import com.vitorpamplona.quartz.encoders.toHexKey import com.vitorpamplona.quartz.encoders.toNpub @@ -58,6 +60,7 @@ private object PrefKeys { const val PROFILE_URL = "profile_url" const val LAST_METADATA_UPDATE = "last_metadata_update" const val LAST_CHECK = "last_check" + const val NCRYPT_SEC = "ncrypt_sec" } @Immutable @@ -350,6 +353,18 @@ object LocalPreferences { } } + suspend fun saveNcryptsec( + npub: String, + privateKeyHex: HexKey, + password: String, + ) { + val context = NostrSigner.getInstance() + val ncryptsec = CryptoUtils.nip49.encrypt(privateKeyHex, password) + encryptedPreferences(context, npub).edit().apply { + putString(PrefKeys.NCRYPT_SEC, ncryptsec) + }.apply() + } + fun updateProxy( context: Context, useProxy: Boolean, diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/ui/LoginScreen.kt b/app/src/main/java/com/greenart7c3/nostrsigner/ui/LoginScreen.kt index 92df9d7..b02213b 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/ui/LoginScreen.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/ui/LoginScreen.kt @@ -94,6 +94,8 @@ import com.anggrayudi.storage.SimpleStorageHelper import com.anggrayudi.storage.file.CreateMode import com.anggrayudi.storage.file.makeFile import com.anggrayudi.storage.file.openOutputStream +import com.greenart7c3.nostrsigner.LocalPreferences +import com.greenart7c3.nostrsigner.NostrSigner import com.greenart7c3.nostrsigner.R import com.greenart7c3.nostrsigner.ui.components.AmberButton import com.greenart7c3.nostrsigner.ui.components.AmberElevatedButton @@ -454,12 +456,13 @@ fun SignUpPage( capitalization = KeyboardCapitalization.None, keyboardType = KeyboardType.Password, autoCorrectEnabled = false, - imeAction = ImeAction.Next, + imeAction = ImeAction.Done, ), modifier = Modifier.fillMaxWidth(), ) AmberButton( + enabled = nickname.text.isNotBlank() && password.text.isNotBlank(), modifier = Modifier.padding(vertical = 40.dp), onClick = { if (nickname.text.isBlank()) { @@ -489,6 +492,7 @@ fun SignUpPage( 1 -> { var showPassword by remember { mutableStateOf(false) } + var errorMessage by remember { mutableStateOf("") } Column( Modifier .fillMaxSize() @@ -505,6 +509,9 @@ fun SignUpPage( value = password2, onValueChange = { value -> password2 = value + if (errorMessage.isNotBlank()) { + errorMessage = "" + } }, trailingIcon = { Row { @@ -539,11 +546,7 @@ fun SignUpPage( AmberButton( onClick = { if (password2.text != password.text) { - Toast.makeText( - context, - "Passwords do not match", - Toast.LENGTH_SHORT, - ).show() + errorMessage = "Passwords do not match" return@AmberButton } storageHelper.openFolderPicker() @@ -615,9 +618,27 @@ fun SignUpPage( } } } + + if (errorMessage.isNotBlank()) { + Text( + text = errorMessage, + color = Color.Red, + ) + } + AmberButton( - enabled = enabled, + enabled = password2.text.isNotBlank(), onClick = { + if (password2.text.isBlank()) { + errorMessage = "Password is required" + return@AmberButton + } + + if (password2.text != password.text) { + errorMessage = "Passwords do not match" + return@AmberButton + } + scope.launch { state.animateScrollToPage(2) } @@ -800,6 +821,14 @@ fun SignUpPage( name = nickname.text, ) + NostrSigner.getInstance().applicationIOScope.launch(Dispatchers.IO) { + LocalPreferences.saveNcryptsec( + keyPair.pubKey.toNpub(), + keyPair.privKey!!.toHexKey(), + password.text, + ) + } + onFinish() }, text = stringResource(R.string.finish),