Exposes decryption crashing because it is used by Notification Services to check which account can decrypt a GiftWrap.

This commit is contained in:
Vitor Pamplona
2024-08-27 17:43:12 -04:00
parent f7237b5666
commit 931081c5e7
2 changed files with 61 additions and 61 deletions
@@ -20,7 +20,6 @@
*/ */
package com.vitorpamplona.quartz.crypto.nip44 package com.vitorpamplona.quartz.crypto.nip44
import android.util.Log
import com.vitorpamplona.quartz.crypto.nip04.Nip04 import com.vitorpamplona.quartz.crypto.nip04.Nip04
import com.vitorpamplona.quartz.events.Event import com.vitorpamplona.quartz.events.Event
import fr.acinq.secp256k1.Secp256k1 import fr.acinq.secp256k1.Secp256k1
@@ -85,11 +84,10 @@ class Nip44(
json: String, json: String,
privateKey: ByteArray, privateKey: ByteArray,
pubKey: ByteArray, pubKey: ByteArray,
): String? = ): String? {
try {
val info = Event.mapper.readValue(json, EncryptedInfoString::class.java) val info = Event.mapper.readValue(json, EncryptedInfoString::class.java)
when (info.v) { return when (info.v) {
Nip04.EncryptedInfo.V -> { Nip04.EncryptedInfo.V -> {
val encryptedInfo = val encryptedInfo =
Nip04.EncryptedInfo( Nip04.EncryptedInfo(
@@ -98,6 +96,7 @@ class Nip44(
) )
nip04.decrypt(encryptedInfo, privateKey, pubKey) nip04.decrypt(encryptedInfo, privateKey, pubKey)
} }
Nip44v1.EncryptedInfo.V -> { Nip44v1.EncryptedInfo.V -> {
val encryptedInfo = val encryptedInfo =
Nip44v1.EncryptedInfo( Nip44v1.EncryptedInfo(
@@ -106,6 +105,7 @@ class Nip44(
) )
v1.decrypt(encryptedInfo, privateKey, pubKey) v1.decrypt(encryptedInfo, privateKey, pubKey)
} }
Nip44v2.EncryptedInfo.V -> { Nip44v2.EncryptedInfo.V -> {
val encryptedInfo = val encryptedInfo =
Nip44v2.EncryptedInfo( Nip44v2.EncryptedInfo(
@@ -115,11 +115,9 @@ class Nip44(
) )
v2.decrypt(encryptedInfo, privateKey, pubKey) v2.decrypt(encryptedInfo, privateKey, pubKey)
} }
else -> null else -> null
} }
} catch (e: Exception) {
Log.e("CryptoUtils", "NIP44: Unable to find version and decrypt $json", e)
null
} }
fun decryptNIP44FromBase64( fun decryptNIP44FromBase64(
@@ -129,18 +127,13 @@ class Nip44(
): String? { ): String? {
if (payload.isEmpty()) return null if (payload.isEmpty()) return null
return try {
val byteArray = Base64.getDecoder().decode(payload) val byteArray = Base64.getDecoder().decode(payload)
when (byteArray[0].toInt()) { return when (byteArray[0].toInt()) {
Nip04.EncryptedInfo.V -> nip04.decrypt(payload, privateKey, pubKey) Nip04.EncryptedInfo.V -> nip04.decrypt(payload, privateKey, pubKey)
Nip44v1.EncryptedInfo.V -> v1.decrypt(payload, privateKey, pubKey) Nip44v1.EncryptedInfo.V -> v1.decrypt(payload, privateKey, pubKey)
Nip44v2.EncryptedInfo.V -> v2.decrypt(payload, privateKey, pubKey) Nip44v2.EncryptedInfo.V -> v2.decrypt(payload, privateKey, pubKey)
else -> null else -> null
} }
} catch (e: Exception) {
Log.e("CryptoUtils", "NIP44: Unable to find version and decrypt $payload", e)
null
}
} }
} }
@@ -72,11 +72,10 @@ class GiftWrapEvent(
onReady: (Event) -> Unit, onReady: (Event) -> Unit,
) = unwrap(signer, onReady) ) = unwrap(signer, onReady)
fun unwrap( fun unwrapThrowing(
signer: NostrSigner, signer: NostrSigner,
onReady: (Event) -> Unit, onReady: (Event) -> Unit,
) { ) {
try {
plainContent(signer) { giftStr -> plainContent(signer) { giftStr ->
val gift = val gift =
try { try {
@@ -93,6 +92,14 @@ class GiftWrapEvent(
onReady(gift) onReady(gift)
} }
}
fun unwrap(
signer: NostrSigner,
onReady: (Event) -> Unit,
) {
try {
unwrapThrowing(signer, onReady)
} catch (e: Exception) { } catch (e: Exception) {
Log.w("GiftWrapEvent", "Couldn't Decrypt the content " + this.toNostrUri()) Log.w("GiftWrapEvent", "Couldn't Decrypt the content " + this.toNostrUri())
} }