From a773be12c45bdc94ab86281ee96c318cce1a52a4 Mon Sep 17 00:00:00 2001 From: greenart7c3 Date: Mon, 11 Sep 2023 11:52:53 -0300 Subject: [PATCH] decrypt LnZapRequestEvent --- .../vitorpamplona/amethyst/model/Account.kt | 28 ++++++++++++++++++- .../amethyst/service/AmberUtils.kt | 18 ++++++++++++ .../amethyst/ui/actions/SignerDialog.kt | 3 +- .../ui/screen/loggedIn/AccountViewModel.kt | 3 ++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt index 0043d4054..42cf78580 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -2523,14 +2523,40 @@ class Account( } } + fun decryptContentWithAmber(note: Note): String? = with(Dispatchers.IO) { + val event = note.event + return when (event) { + is PrivateDmEvent -> { + AmberUtils.decrypt(event.content, event.talkingWith(userProfile().pubkeyHex), event.id) + AmberUtils.cachedDecryptedContent[event.id] + } + is LnZapRequestEvent -> { + decryptZapContentAuthor(note)?.content() + } + else -> { + event?.content() + } + } + } + fun decryptZapContentAuthor(note: Note): Event? { val event = note.event val loggedInPrivateKey = keyPair.privKey + if (loginWithAmber && event is LnZapRequestEvent && event.isPrivateZap()) { + val decryptedContent = AmberUtils.cachedDecryptedContent[event.id] + if (decryptedContent != null) { + return Event.fromJson(decryptedContent) + } + AmberUtils.decryptZapEvent(event) + if (AmberUtils.content.isBlank()) return null + AmberUtils.cachedDecryptedContent[event.id] = AmberUtils.content + return Event.fromJson(AmberUtils.content) + } + return if (event is LnZapRequestEvent && loggedInPrivateKey != null && event.isPrivateZap()) { val recipientPK = event.zappedAuthor().firstOrNull() val recipientPost = event.zappedPost().firstOrNull() - if (recipientPK == userProfile().pubkeyHex) { // if the receiver is logged in, these are the params. val privateKeyToUse = loggedInPrivateKey diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/AmberUtils.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/AmberUtils.kt index efb793795..b2205e46e 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/AmberUtils.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/AmberUtils.kt @@ -7,6 +7,7 @@ import com.vitorpamplona.amethyst.ServiceManager import com.vitorpamplona.amethyst.ui.actions.SignerType import com.vitorpamplona.quartz.encoders.HexKey import com.vitorpamplona.quartz.events.EventInterface +import com.vitorpamplona.quartz.events.LnZapRequestEvent object AmberUtils { var content: String = "" @@ -29,6 +30,7 @@ object AmberUtils { SignerType.NIP44_ENCRYPT -> "nip44_encrypt" SignerType.NIP44_DECRYPT -> "nip44_decrypt" SignerType.GET_PUBLIC_KEY -> "get_public_key" + SignerType.DECRYPT_ZAP_EVENT -> "decrypt_zap_event" } intent.putExtra("type", signerType) intent.putExtra("pubKey", pubKey) @@ -101,4 +103,20 @@ object AmberUtils { } } } + + fun decryptZapEvent(event: LnZapRequestEvent) { + if (content.isBlank()) { + isActivityRunning = true + openAmber( + event.toJson(), + SignerType.DECRYPT_ZAP_EVENT, + IntentUtils.activityResultLauncher, + event.pubKey, + event.id + ) + while (isActivityRunning) { + // do nothing + } + } + } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/SignerDialog.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/SignerDialog.kt index 93bb68109..579ffe2cd 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/SignerDialog.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/SignerDialog.kt @@ -48,7 +48,8 @@ enum class SignerType { NIP04_DECRYPT, NIP44_ENCRYPT, NIP44_DECRYPT, - GET_PUBLIC_KEY + GET_PUBLIC_KEY, + DECRYPT_ZAP_EVENT } @Composable diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt index 53b339c8c..def0e15de 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt @@ -361,6 +361,9 @@ class AccountViewModel(val account: Account) : ViewModel() { } fun decrypt(note: Note): String? { + if (loggedInWithAmber()) { + return account.decryptContentWithAmber(note) + } return account.decryptContent(note) }