Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
363 changes: 197 additions & 166 deletions app/src/main/kotlin/io/privkey/keep/ExportShareScreen.kt

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions app/src/main/kotlin/io/privkey/keep/ImportNsecScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ fun ImportNsecScreen(

Spacer(modifier = Modifier.height(24.dp))

if (scanError != null) {
val currentScanError = scanError
if (currentScanError != null) {
StatusCard(
text = scanError!!,
text = currentScanError,
containerColor = MaterialTheme.colorScheme.errorContainer,
contentColor = MaterialTheme.colorScheme.onErrorContainer
)
Expand Down
224 changes: 131 additions & 93 deletions app/src/main/kotlin/io/privkey/keep/ImportShareScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,112 +123,150 @@ fun ImportShareScreen(

Spacer(modifier = Modifier.height(24.dp))

OutlinedTextField(
value = shareDataDisplay,
onValueChange = {
if (it.length <= MAX_SHARE_LENGTH) {
shareData.update(it)
shareDataDisplay = it
ImportShareInputFields(
shareDataDisplay = shareDataDisplay,
onShareDataChange = { value ->
if (value.length <= MAX_SHARE_LENGTH) {
shareData.update(value)
shareDataDisplay = value
}
},
label = { Text("Share Data") },
placeholder = { Text("kshare1q...") },
modifier = Modifier.fillMaxWidth(),
minLines = 3,
maxLines = 5,
enabled = isInputEnabled
passphraseDisplay = passphraseDisplay,
onPassphraseChange = { value ->
passphrase.update(value)
passphraseDisplay = value
},
shareName = shareName,
onShareNameChange = { if (it.length <= 64) shareName = it },
onScanClick = { showScanner = true },
isInputEnabled = isInputEnabled
)

Spacer(modifier = Modifier.height(8.dp))
Spacer(modifier = Modifier.height(24.dp))

OutlinedButton(
onClick = { showScanner = true },
modifier = Modifier.fillMaxWidth(),
enabled = isInputEnabled
) {
Text("Scan QR Code")
}
ImportStatusAndActions(
importState = importState,
canImport = shareData.isNotBlank() && passphrase.length > 0 && isInputEnabled,
onDismiss = onDismiss,
onImportClick = { onError ->
val passphraseChars = passphrase.toCharArray()
fun clearChars() = Arrays.fill(passphraseChars, '\u0000')
try {
val cipher = onGetCipher()
onBiometricAuth(cipher) { authedCipher ->
try {
if (authedCipher != null) {
onImport(shareData.valueUnsafe(), String(passphraseChars), shareName, authedCipher)
}
} finally {
clearChars()
}
}
} catch (e: KeyPermanentlyInvalidatedException) {
clearChars()
if (BuildConfig.DEBUG) Log.e("ImportShare", "Biometric key invalidated during cipher init: ${e::class.simpleName}")
onError("Biometric key invalidated. Please re-enroll biometrics.")
} catch (e: Exception) {
clearChars()
if (BuildConfig.DEBUG) Log.e("ImportShare", "Failed to initialize cipher for biometric auth: ${e::class.simpleName}")
onError("Failed to initialize encryption")
}
}
)
}
}

Spacer(modifier = Modifier.height(16.dp))
@Composable
private fun ImportShareInputFields(
shareDataDisplay: String,
onShareDataChange: (String) -> Unit,
passphraseDisplay: String,
onPassphraseChange: (String) -> Unit,
shareName: String,
onShareNameChange: (String) -> Unit,
onScanClick: () -> Unit,
isInputEnabled: Boolean
) {
OutlinedTextField(
value = shareDataDisplay,
onValueChange = onShareDataChange,
label = { Text("Share Data") },
placeholder = { Text("kshare1q...") },
modifier = Modifier.fillMaxWidth(),
minLines = 3,
maxLines = 5,
enabled = isInputEnabled
)

OutlinedTextField(
value = passphraseDisplay,
onValueChange = {
passphrase.update(it)
passphraseDisplay = it
},
label = { Text("Passphrase") },
modifier = Modifier.fillMaxWidth(),
visualTransformation = PasswordVisualTransformation(),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
singleLine = true,
enabled = isInputEnabled
)
Spacer(modifier = Modifier.height(8.dp))

Spacer(modifier = Modifier.height(16.dp))
OutlinedButton(
onClick = onScanClick,
modifier = Modifier.fillMaxWidth(),
enabled = isInputEnabled
) {
Text("Scan QR Code")
}

OutlinedTextField(
value = shareName,
onValueChange = { if (it.length <= 64) shareName = it },
label = { Text("Share Name") },
modifier = Modifier.fillMaxWidth(),
singleLine = true,
enabled = isInputEnabled
)
Spacer(modifier = Modifier.height(16.dp))

Spacer(modifier = Modifier.height(24.dp))
OutlinedTextField(
value = passphraseDisplay,
onValueChange = onPassphraseChange,
label = { Text("Passphrase") },
modifier = Modifier.fillMaxWidth(),
visualTransformation = PasswordVisualTransformation(),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
singleLine = true,
enabled = isInputEnabled
)

Spacer(modifier = Modifier.height(16.dp))

OutlinedTextField(
value = shareName,
onValueChange = onShareNameChange,
label = { Text("Share Name") },
modifier = Modifier.fillMaxWidth(),
singleLine = true,
enabled = isInputEnabled
)
}

if (importState is ImportState.Error) {
StatusCard(
text = importState.message,
containerColor = MaterialTheme.colorScheme.errorContainer,
contentColor = MaterialTheme.colorScheme.onErrorContainer
)
Spacer(modifier = Modifier.height(16.dp))
}
@Composable
private fun ImportStatusAndActions(
importState: ImportState,
canImport: Boolean,
onDismiss: () -> Unit,
onImportClick: (onError: (String) -> Unit) -> Unit
) {
if (importState is ImportState.Error) {
StatusCard(
text = importState.message,
containerColor = MaterialTheme.colorScheme.errorContainer,
contentColor = MaterialTheme.colorScheme.onErrorContainer
)
Spacer(modifier = Modifier.height(16.dp))
}

if (importState is ImportState.Success) {
StatusCard(
text = "Share '${importState.name}' imported successfully",
containerColor = MaterialTheme.colorScheme.primaryContainer,
contentColor = MaterialTheme.colorScheme.onPrimaryContainer
)
Spacer(modifier = Modifier.height(16.dp))
}
if (importState is ImportState.Success) {
StatusCard(
text = "Share '${importState.name}' imported successfully",
containerColor = MaterialTheme.colorScheme.primaryContainer,
contentColor = MaterialTheme.colorScheme.onPrimaryContainer
)
Spacer(modifier = Modifier.height(16.dp))
}

if (importState is ImportState.Importing) {
CircularProgressIndicator()
} else {
ImportButtons(
importState = importState,
canImport = shareData.isNotBlank() && passphrase.length > 0 && isInputEnabled,
onDismiss = onDismiss,
onImportClick = { onError ->
val passphraseChars = passphrase.toCharArray()
fun clearChars() = Arrays.fill(passphraseChars, '\u0000')
try {
val cipher = onGetCipher()
onBiometricAuth(cipher) { authedCipher ->
try {
if (authedCipher != null) {
onImport(shareData.valueUnsafe(), String(passphraseChars), shareName, authedCipher)
}
} finally {
clearChars()
}
}
} catch (e: KeyPermanentlyInvalidatedException) {
clearChars()
if (BuildConfig.DEBUG) Log.e("ImportShare", "Biometric key invalidated during cipher init", e)
onError("Biometric key invalidated. Please re-enroll biometrics.")
} catch (e: Exception) {
clearChars()
if (BuildConfig.DEBUG) Log.e("ImportShare", "Failed to initialize cipher for biometric auth", e)
onError("Failed to initialize encryption")
}
}
)
}
if (importState is ImportState.Importing) {
CircularProgressIndicator()
} else {
ImportButtons(
importState = importState,
canImport = canImport,
onDismiss = onDismiss,
onImportClick = onImportClick
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/io/privkey/keep/KeepMobileApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class KeepMobileApp : Application() {
keepMobile = newKeepMobile
nip55Handler = Nip55Handler(newKeepMobile)
}.onFailure { e ->
initError = "${e::class.simpleName}: ${e.message}"
initError = "Failed to initialize application"
if (BuildConfig.DEBUG) Log.e(TAG, "Failed to initialize KeepMobile: ${e::class.simpleName}", e)
}
}
Expand Down Expand Up @@ -262,7 +262,7 @@ class KeepMobileApp : Application() {
val proxy = proxyConfigStore?.getProxyConfig()
if (BuildConfig.DEBUG) {
val shareInfo = mobile.getShareInfo()
Log.d(TAG, "Share: index=${shareInfo?.shareIndex}, group=${shareInfo?.groupPubkey?.take(16)}")
Log.d(TAG, "Share: index=${shareInfo?.shareIndex}, hasGroup=${shareInfo?.groupPubkey != null}")
Log.d(TAG, "Initializing with ${relays.size} relay(s), proxy=${proxy != null}")
}
initializeWithProxy(mobile, relays, proxy)
Expand Down
Loading