diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/database/ApplicationEntity.kt b/app/src/main/java/com/greenart7c3/nostrsigner/database/ApplicationEntity.kt index fbc30038..225473f8 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/database/ApplicationEntity.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/database/ApplicationEntity.kt @@ -16,7 +16,11 @@ import kotlin.collections.joinToString fun generateBunkerPrivKey(): String = Nip01Crypto.privKeyCreate().toHexKey() -fun localPubKeyFromPrivKey(privKeyHex: String): String = KeyPair(privKey = privKeyHex.hexToByteArray()).pubKey.toHexKey() +fun localPubKeyFromPrivKey(privKeyHex: String): String = try { + KeyPair(privKey = privKeyHex.hexToByteArray()).pubKey.toHexKey() +} catch (_: Exception) { + "" +} @Entity( tableName = "application", diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/service/BunkerRequestUtils.kt b/app/src/main/java/com/greenart7c3/nostrsigner/service/BunkerRequestUtils.kt index c0db03e3..15eaefb3 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/service/BunkerRequestUtils.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/service/BunkerRequestUtils.kt @@ -143,7 +143,11 @@ object BunkerRequestUtils { onDone: (Boolean) -> Unit, ) { val connSigner = if (bunkerRequest.signerPrivKey.isNotEmpty()) { - NostrSignerInternal(KeyPair(privKey = bunkerRequest.signerPrivKey.hexToByteArray())) + try { + NostrSignerInternal(KeyPair(privKey = bunkerRequest.signerPrivKey.hexToByteArray())) + } catch (_: Exception) { + null + } } else { null } diff --git a/app/src/test/java/com/greenart7c3/nostrsigner/database/ApplicationEntityTest.kt b/app/src/test/java/com/greenart7c3/nostrsigner/database/ApplicationEntityTest.kt new file mode 100644 index 00000000..21ae0389 --- /dev/null +++ b/app/src/test/java/com/greenart7c3/nostrsigner/database/ApplicationEntityTest.kt @@ -0,0 +1,40 @@ +package com.greenart7c3.nostrsigner.database + +import com.vitorpamplona.quartz.nip01Core.core.toHexKey +import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair +import com.vitorpamplona.quartz.nip01Core.crypto.Nip01Crypto +import org.junit.Assert.assertEquals +import org.junit.Test + +class ApplicationEntityTest { + + @Test + fun `localPubKey returns empty string for empty localKey`() { + val app = ApplicationEntity.empty().copy(localKey = "") + assertEquals("", app.localPubKey) + } + + @Test + fun `localPubKey returns empty string for invalid localKey`() { + // "invalid-key" is not valid hex and not 32 bytes + val app = ApplicationEntity.empty().copy(localKey = "invalid-key") + assertEquals("", app.localPubKey) + } + + @Test + fun `localPubKey returns empty string for wrong sized hex localKey`() { + // 31 bytes instead of 32 + val app = ApplicationEntity.empty().copy(localKey = "00".repeat(31)) + assertEquals("", app.localPubKey) + } + + @Test + fun `localPubKey returns valid pubkey for valid 32 byte hex localKey`() { + val privKey = Nip01Crypto.privKeyCreate() + val privKeyHex = privKey.toHexKey() + val app = ApplicationEntity.empty().copy(localKey = privKeyHex) + + val expectedPubKey = KeyPair(privKey = privKey).pubKey.toHexKey() + assertEquals(expectedPubKey, app.localPubKey) + } +}