From 9a5d8522fb3f5905d911a54b409162f193f70adb Mon Sep 17 00:00:00 2001 From: Believethehype Date: Mon, 10 Apr 2023 13:25:09 +0200 Subject: [PATCH] added anonymous zaps --- .../vitorpamplona/amethyst/model/Account.kt | 10 +++++--- .../service/model/LnZapRequestEvent.kt | 25 ++++++++++++++----- .../amethyst/ui/components/InvoiceRequest.kt | 3 ++- .../amethyst/ui/note/PollNote.kt | 2 +- .../amethyst/ui/note/ReactionsRow.kt | 2 +- .../amethyst/ui/note/ZapCustomDialog.kt | 4 +-- .../ui/screen/loggedIn/AccountViewModel.kt | 12 +++++---- 7 files changed, 38 insertions(+), 20 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 f287b3011..178e5f264 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -137,7 +137,7 @@ class Account( } } - fun createZapRequestFor(note: Note, pollOption: Int?, message: String = ""): LnZapRequestEvent? { + fun createZapRequestFor(note: Note, pollOption: Int?, message: String = "", zapType: LnZapEvent.ZapType): LnZapRequestEvent? { if (!isWriteable()) return null note.event?.let { event -> @@ -147,7 +147,8 @@ class Account( ?: localRelays.map { it.url }.toSet(), loggedIn.privKey!!, pollOption, - message + message, + zapType ) } return null @@ -171,14 +172,15 @@ class Account( return createZapRequestFor(user) } - fun createZapRequestFor(userPubKeyHex: String, message: String = ""): LnZapRequestEvent? { + fun createZapRequestFor(userPubKeyHex: String, message: String = "", zapType: LnZapEvent.ZapType): LnZapRequestEvent? { if (!isWriteable()) return null return LnZapRequestEvent.create( userPubKeyHex, userProfile().latestContactList?.relays()?.keys?.ifEmpty { null } ?: localRelays.map { it.url }.toSet(), loggedIn.privKey!!, - message + message, + zapType ) } 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 5452aa36a..34fddc02a 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 @@ -25,10 +25,12 @@ class LnZapRequestEvent( privateKey: ByteArray, pollOption: Int?, message: String, + zapType: LnZapEvent.ZapType, createdAt: Long = Date().time / 1000 ): LnZapRequestEvent { val content = message - val pubKey = Utils.pubkeyCreate(privateKey).toHexKey() + var privkey = privateKey + var pubKey = Utils.pubkeyCreate(privateKey).toHexKey() var tags = listOf( listOf("e", originalNote.id()), listOf("p", originalNote.pubKey()), @@ -40,9 +42,13 @@ class LnZapRequestEvent( if (pollOption != null && pollOption >= 0) { tags = tags + listOf(listOf(POLL_OPTION, pollOption.toString())) } - + if (zapType == LnZapEvent.ZapType.ANONYMOUS) { + tags = tags + listOf(listOf("anon", "")) + privkey = Utils.privkeyCreate() + pubKey = Utils.pubkeyCreate(privkey).toHexKey() + } val id = generateId(pubKey, createdAt, kind, tags, content) - val sig = Utils.sign(id, privateKey) + val sig = Utils.sign(id, privkey) return LnZapRequestEvent(id.toHexKey(), pubKey, createdAt, tags, content, sig.toHexKey()) } @@ -51,17 +57,24 @@ class LnZapRequestEvent( relays: Set, privateKey: ByteArray, message: String, + zapType: LnZapEvent.ZapType, createdAt: Long = Date().time / 1000 ): LnZapRequestEvent { val content = message - val pubKey = Utils.pubkeyCreate(privateKey).toHexKey() - val tags = listOf( + var privkey = privateKey + var pubKey = Utils.pubkeyCreate(privateKey).toHexKey() + var tags = listOf( listOf("p", userHex), listOf("relays") + relays ) + if (zapType == LnZapEvent.ZapType.ANONYMOUS) { + tags = tags + listOf(listOf("anon", "")) + privkey = Utils.privkeyCreate() + pubKey = Utils.pubkeyCreate(privkey).toHexKey() + } val id = generateId(pubKey, createdAt, kind, tags, content) - val sig = Utils.sign(id, privateKey) + val sig = Utils.sign(id, privkey) return LnZapRequestEvent(id.toHexKey(), pubKey, createdAt, tags, content, sig.toHexKey()) } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/InvoiceRequest.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/InvoiceRequest.kt index bca274b3c..06e38c773 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/InvoiceRequest.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/InvoiceRequest.kt @@ -37,6 +37,7 @@ import androidx.compose.ui.unit.sp import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.service.lnurl.LightningAddressResolver +import com.vitorpamplona.amethyst.service.model.LnZapEvent import kotlinx.coroutines.launch @Composable @@ -135,7 +136,7 @@ fun InvoiceRequest( Button( modifier = Modifier.fillMaxWidth().padding(vertical = 10.dp), onClick = { - val zapRequest = account.createZapRequestFor(toUserPubKeyHex, message) + val zapRequest = account.createZapRequestFor(toUserPubKeyHex, message, LnZapEvent.ZapType.PUBLIC) LightningAddressResolver().lnAddressInvoice( lud16, diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNote.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNote.kt index 6a56c847f..da0f35936 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNote.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNote.kt @@ -253,7 +253,7 @@ fun ZapVote( zappingProgress = it } }, - type = LnZapEvent.ZapType.PUBLIC + zapType = LnZapEvent.ZapType.PUBLIC ) } } else { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt index 77e7ddfee..5a0d7a3b4 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt @@ -365,7 +365,7 @@ fun ZapReaction( zappingProgress = it } }, - type = LnZapEvent.ZapType.PUBLIC + zapType = LnZapEvent.ZapType.PUBLIC ) } } else if (account.zapAmountChoices.size > 1) { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ZapCustomDialog.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ZapCustomDialog.kt index 2fcde04b3..5fe601a6a 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ZapCustomDialog.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ZapCustomDialog.kt @@ -59,7 +59,6 @@ fun ZapCustomDialog(onClose: () -> Unit, account: Account, accountViewModel: Acc val context = LocalContext.current val scope = rememberCoroutineScope() val postViewModel: ZapOptionstViewModel = viewModel() - postViewModel.load(account) LaunchedEffect(account) { postViewModel.load(account) } @@ -68,6 +67,7 @@ fun ZapCustomDialog(onClose: () -> Unit, account: Account, accountViewModel: Acc val zapTypes = listOf( Pair(LnZapEvent.ZapType.PUBLIC, "Public"), + Pair(LnZapEvent.ZapType.ANONYMOUS, "Anonymous"), Pair(LnZapEvent.ZapType.NONZAP, "Non-Zap") ) @@ -115,7 +115,7 @@ fun ZapCustomDialog(onClose: () -> Unit, account: Account, accountViewModel: Acc zappingProgress = it } }, - type = selectedZapType.first + zapType = selectedZapType.first ) } onClose() 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 513c5b150..c88352b6c 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 @@ -53,7 +53,7 @@ class AccountViewModel(private val account: Account) : ViewModel() { account.delete(account.boostsTo(note)) } - fun zap(note: Note, amount: Long, pollOption: Int?, message: String, context: Context, onError: (String) -> Unit, onProgress: (percent: Float) -> Unit, type: LnZapEvent.ZapType) { + fun zap(note: Note, amount: Long, pollOption: Int?, message: String, context: Context, onError: (String) -> Unit, onProgress: (percent: Float) -> Unit, zapType: LnZapEvent.ZapType) { val lud16 = note.author?.info?.lud16?.trim() ?: note.author?.info?.lud06?.trim() if (lud16.isNullOrBlank()) { @@ -61,11 +61,13 @@ class AccountViewModel(private val account: Account) : ViewModel() { return } - var zapRequest = account.createZapRequestFor(note, pollOption, message) - var zapRequestJson = zapRequest?.toJson() + var zapRequestJson = "" - if (type == LnZapEvent.ZapType.NONZAP) { - zapRequestJson = "" + if (zapType != LnZapEvent.ZapType.NONZAP) { + val zapRequest = account.createZapRequestFor(note, pollOption, message, zapType) + if (zapRequest != null) { + zapRequestJson = zapRequest.toJson() + } } onProgress(0.10f)