Fixes all or nothing decryption of Zaps failing in a few cases. Now it has a fallback when decryption fails.

This commit is contained in:
Vitor Pamplona
2023-11-30 10:15:36 -05:00
parent 21a797be9d
commit 07a92cd70d
6 changed files with 108 additions and 15 deletions
@@ -28,7 +28,7 @@ class LnZapRequestEvent(
) : Event(id, pubKey, createdAt, kind, tags, content, sig) {
@Transient
private var privateZapEvent: Event? = null
private var privateZapEvent: LnZapPrivateEvent? = null
fun zappedPost() = tags.filter { it.size > 1 && it[0] == "e" }.map { it[1] }
@@ -36,7 +36,7 @@ class LnZapRequestEvent(
fun isPrivateZap() = tags.any { t -> t.size >= 2 && t[0] == "anon" && t[1].isNotBlank() }
fun getPrivateZapEvent(loggedInUserPrivKey: ByteArray, pubKey: HexKey): Event? {
fun getPrivateZapEvent(loggedInUserPrivKey: ByteArray, pubKey: HexKey): LnZapPrivateEvent? {
val anonTag = tags.firstOrNull { t -> t.size >= 2 && t[0] == "anon" }
if (anonTag != null) {
val encnote = anonTag[1]
@@ -45,7 +45,7 @@ class LnZapRequestEvent(
val note = decryptPrivateZapMessage(encnote, loggedInUserPrivKey, pubKey.hexToByteArray())
val decryptedEvent = fromJson(note)
if (decryptedEvent.kind == 9733) {
return decryptedEvent
return decryptedEvent as LnZapPrivateEvent
}
} catch (e: Exception) {
e.printStackTrace()
@@ -55,7 +55,7 @@ class LnZapRequestEvent(
return null
}
fun cachedPrivateZap(): Event? {
fun cachedPrivateZap(): LnZapPrivateEvent? {
return privateZapEvent
}
@@ -3,6 +3,7 @@ package com.vitorpamplona.quartz.signers
import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.events.EventFactory
import com.vitorpamplona.quartz.events.LnZapPrivateEvent
import com.vitorpamplona.quartz.events.LnZapRequestEvent
import com.vitorpamplona.quartz.events.PeopleListEvent
import kotlinx.collections.immutable.ImmutableSet
@@ -19,5 +20,5 @@ abstract class NostrSigner(val pubKey: HexKey) {
abstract fun nip44Encrypt(decryptedContent: String, toPublicKey: HexKey, onReady: (String)-> Unit)
abstract fun nip44Decrypt(encryptedContent: String, fromPublicKey: HexKey, onReady: (String)-> Unit)
abstract fun decryptZapEvent(event: LnZapRequestEvent, onReady: (Event)-> Unit)
abstract fun decryptZapEvent(event: LnZapRequestEvent, onReady: (LnZapPrivateEvent)-> Unit)
}
@@ -7,6 +7,7 @@ import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.encoders.toNpub
import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.events.EventFactory
import com.vitorpamplona.quartz.events.LnZapPrivateEvent
import com.vitorpamplona.quartz.events.LnZapRequestEvent
class NostrSignerExternal(
@@ -108,7 +109,7 @@ class NostrSignerExternal(
)
}
override fun decryptZapEvent(event: LnZapRequestEvent, onReady: (Event)-> Unit) {
override fun decryptZapEvent(event: LnZapRequestEvent, onReady: (LnZapPrivateEvent)-> Unit) {
return launcher.decryptZapEvent(event) {
val event = try {
Event.fromJson(it)
@@ -116,7 +117,7 @@ class NostrSignerExternal(
Log.e("NostrExternalSigner", "Unable to parse returned decrypted Zap: ${it}")
null
}
event?.let {
(event as? LnZapPrivateEvent)?.let {
onReady(event)
}
}
@@ -155,7 +155,7 @@ class NostrSignerInternal(val keyPair: KeyPair): NostrSigner(keyPair.pubKey.toHe
}
}
override fun decryptZapEvent(event: LnZapRequestEvent, onReady: (Event)-> Unit) {
override fun decryptZapEvent(event: LnZapRequestEvent, onReady: (LnZapPrivateEvent)-> Unit) {
if (keyPair.privKey == null) return
val recipientPK = event.zappedAuthor().firstOrNull()