diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt index 9385bddba..e0771efb1 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt @@ -4,10 +4,10 @@ import android.util.Log import androidx.lifecycle.LiveData import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.vitorpamplona.amethyst.service.NostrAccountDataSource.account import com.vitorpamplona.amethyst.service.model.* import com.vitorpamplona.amethyst.service.relays.Relay import com.vitorpamplona.amethyst.ui.components.BundledInsert -import com.vitorpamplona.amethyst.ui.dal.NotificationFeedFilter import fr.acinq.secp256k1.Hex import kotlinx.coroutines.* import nostr.postr.toNpub @@ -595,6 +595,16 @@ object LocalCache { fun consume(event: LnZapEvent) { val note = getOrCreateNote(event.id) + var decryptedContent = LnZapRequestEvent.checkForPrivateZap(event.zapRequest!!, account.loggedIn.privKey!!) + if (decryptedContent != null) { + Log.e( + "DC", + "Decrypted Event: Sender: {${decryptedContent.pubKey}}, Message: {${decryptedContent.content}} " + + // TODO Update Notification with this Sender and Message + ) + } + // Already processed this event. if (note.event != null) return @@ -627,27 +637,6 @@ object LocalCache { refreshObservers(note) } - fun checkPrivateZap(zaprequest: Event): Event { - var anonTag = zaprequest.tags.firstOrNull { t -> t.count() >= 2 && t[0] == "anon" } - if (anonTag != null && anonTag.size > 1) { - var encnote = anonTag?.elementAt(1) - if (encnote != null && encnote != "") { - try { - val loggedInUserHex = NotificationFeedFilter.account.loggedIn.privKey!! // Replace without Filter - var note = LnZapRequestEvent.decrypt_privatezap_message(encnote, loggedInUserHex, zaprequest.pubKey.toByteArray()) - var decryptedEvent = Event.fromJson(note) - if (decryptedEvent.kind == 9733) { - zaprequest.pubKey = decryptedEvent.pubKey - zaprequest.content = decryptedEvent.content - // return decryptedEvent - } - } catch (e: Exception) { - e.printStackTrace() - } - } - } - return zaprequest - } fun consume(event: LnZapRequestEvent) { val note = getOrCreateNote(event.id) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/model/Event.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/model/Event.kt index 85a47e1c9..6d857328c 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/model/Event.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/model/Event.kt @@ -15,11 +15,11 @@ import java.util.* open class Event( val id: HexKey, - @SerializedName("pubkey") var pubKey: HexKey, + @SerializedName("pubkey") val pubKey: HexKey, @SerializedName("created_at") val createdAt: Long, val kind: Int, val tags: List>, - var content: String, + val content: String, val sig: HexKey ) : EventInterface { override fun id(): HexKey = id diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/model/LnZapRequestEvent.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/model/LnZapRequestEvent.kt index c08e0b1f6..e541f7382 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/model/LnZapRequestEvent.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/model/LnZapRequestEvent.kt @@ -55,9 +55,9 @@ class LnZapRequestEvent( privkey = Utils.privkeyCreate() pubKey = Utils.pubkeyCreate(privkey).toHexKey() } else if (zapType == LnZapEvent.ZapType.PRIVATE) { - var encryptionprkey = create_private_key(privateKey.toHexKey(), originalNote.id(), createdAt) + var encryptionprkey = createPrivateKey(privateKey.toHexKey(), originalNote.id(), createdAt) var noteJson = (create(privkey, 9733, listOf(tags[0], tags[1]), message)).toJson() - var privreq = encrypt_privatezap_message(noteJson, encryptionprkey, originalNote.pubKey().toByteArray()) + var privreq = encryptPrivateZapMessage(noteJson, encryptionprkey, originalNote.pubKey().toByteArray()) tags = tags + listOf(listOf("anon", privreq)) content = "" privkey = encryptionprkey @@ -68,13 +68,13 @@ class LnZapRequestEvent( return LnZapRequestEvent(id.toHexKey(), pubKey, createdAt, tags, content, sig.toHexKey()) } - fun create_private_key(privkey: String, id: String, createdAt: Long): ByteArray { + fun createPrivateKey(privkey: String, id: String, createdAt: Long): ByteArray { var str = privkey + id + createdAt.toString() var strbyte = str.toByteArray(Charset.forName("utf-8")) return sha256.digest(strbyte) } - fun encrypt_privatezap_message(msg: String, privkey: ByteArray, pubkey: ByteArray): String { + fun encryptPrivateZapMessage(msg: String, privkey: ByteArray, pubkey: ByteArray): String { var sharedSecret = Utils.getSharedSecret(privkey, pubkey) val iv = ByteArray(16) SecureRandom().nextBytes(iv) @@ -93,7 +93,7 @@ class LnZapRequestEvent( return encryptedMsgBech32 + "_" + ivBech32 } - fun decrypt_privatezap_message(msg: String, privkey: ByteArray, pubkey: ByteArray): String { + fun decryptPrivateZapMessage(msg: String, privkey: ByteArray, pubkey: ByteArray): String { var sharedSecret = Utils.getSharedSecret(privkey, pubkey) if (sharedSecret.size != 16 && sharedSecret.size != 32) { throw IllegalArgumentException("Invalid shared secret size") @@ -115,6 +115,25 @@ class LnZapRequestEvent( } } + fun checkForPrivateZap(zaprequest: Event, loggedInUserPrivKey: ByteArray): Event? { + var anonTag = zaprequest.tags.firstOrNull { t -> t.count() >= 2 && t[0] == "anon" } + if (anonTag != null && anonTag.size > 1) { + var encnote = anonTag?.elementAt(1) + if (encnote != null && encnote != "") { + try { + var note = decryptPrivateZapMessage(encnote, loggedInUserPrivKey, zaprequest.pubKey.toByteArray()) + var decryptedEvent = fromJson(note) + if (decryptedEvent.kind == 9733) { + return decryptedEvent + } + } catch (e: Exception) { + e.printStackTrace() + } + } + } + return null + } + fun create( userHex: String, relays: Set, @@ -135,9 +154,9 @@ class LnZapRequestEvent( pubKey = Utils.pubkeyCreate(privkey).toHexKey() tags = tags + listOf(listOf("anon", "")) } else if (zapType == LnZapEvent.ZapType.PRIVATE) { - var enc_prkey = create_private_key(privateKey.toHexKey(), userHex, createdAt) + var enc_prkey = createPrivateKey(privateKey.toHexKey(), userHex, createdAt) var noteJson = (create(privkey, 9733, listOf(tags[0], tags[1]), message)).toJson() - var privreq = encrypt_privatezap_message(noteJson, enc_prkey, userHex.toByteArray()) + var privreq = encryptPrivateZapMessage(noteJson, enc_prkey, userHex.toByteArray()) tags = tags + listOf(listOf("anon", privreq)) content = "" privkey = enc_prkey