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 0aada3b90..42cfd56e0 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -337,18 +337,54 @@ class Account( } fun createZapRequestFor(note: Note, pollOption: Int?, message: String = "", zapType: LnZapEvent.ZapType): LnZapRequestEvent? { - if (!isWriteable()) return null + if (!isWriteable() && !loginWithAmber) return null note.event?.let { event -> - return LnZapRequestEvent.create( - event, - userProfile().latestContactList?.relays()?.keys?.ifEmpty { null } - ?: localRelays.map { it.url }.toSet(), - keyPair.privKey!!, - pollOption, - message, - zapType - ) + if (loginWithAmber) { + when (zapType) { + LnZapEvent.ZapType.ANONYMOUS -> { + return LnZapRequestEvent.createAnonymous( + event, + userProfile().latestContactList?.relays()?.keys?.ifEmpty { null } + ?: localRelays.map { it.url }.toSet(), + pollOption, + message + ) + } + LnZapEvent.ZapType.PUBLIC -> { + val unsignedEvent = LnZapRequestEvent.createPublic( + event, + userProfile().latestContactList?.relays()?.keys?.ifEmpty { null } + ?: localRelays.map { it.url }.toSet(), + keyPair.pubKey.toHexKey(), + pollOption, + message + ) + AmberUtils.content = "" + AmberUtils.openAmber(unsignedEvent) + if (AmberUtils.content.isBlank()) return null + return LnZapRequestEvent( + unsignedEvent.id, + unsignedEvent.pubKey, + unsignedEvent.createdAt, + unsignedEvent.tags, + unsignedEvent.content, + AmberUtils.content + ) + } + else -> null + } + } else { + return LnZapRequestEvent.create( + event, + userProfile().latestContactList?.relays()?.keys?.ifEmpty { null } + ?: localRelays.map { it.url }.toSet(), + keyPair.privKey!!, + pollOption, + message, + zapType + ) + } } return null } 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 8bd56cba3..fbe4e21d2 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 @@ -1118,14 +1118,35 @@ private fun zapClick( .show() } } else if (!accountViewModel.isWriteable()) { - scope.launch { - Toast - .makeText( + if (accountViewModel.loggedInWithAmber()) { + if (accountViewModel.account.zapAmountChoices.size == 1) { + accountViewModel.zap( + baseNote, + accountViewModel.account.zapAmountChoices.first() * 1000, + null, + "", context, - context.getString(R.string.login_with_a_private_key_to_be_able_to_send_zaps), - Toast.LENGTH_SHORT + onError = onError, + onProgress = { + scope.launch(Dispatchers.Main) { + onZappingProgress(it) + } + }, + zapType = accountViewModel.account.defaultZapType ) - .show() + } else if (accountViewModel.account.zapAmountChoices.size > 1) { + onMultipleChoices() + } + } else { + scope.launch { + Toast + .makeText( + context, + context.getString(R.string.login_with_a_private_key_to_be_able_to_send_zaps), + Toast.LENGTH_SHORT + ) + .show() + } } } else if (accountViewModel.account.zapAmountChoices.size == 1) { accountViewModel.zap( diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/events/LnZapRequestEvent.kt b/quartz/src/main/java/com/vitorpamplona/quartz/events/LnZapRequestEvent.kt index 50cbe743d..58696cdd4 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/events/LnZapRequestEvent.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/events/LnZapRequestEvent.kt @@ -99,6 +99,58 @@ class LnZapRequestEvent( return LnZapRequestEvent(id.toHexKey(), pubKey, createdAt, tags, content, sig.toHexKey()) } + fun createPublic( + originalNote: EventInterface, + relays: Set, + pubKey: HexKey, + pollOption: Int?, + message: String, + createdAt: Long = TimeUtils.now() + ): LnZapRequestEvent { + var tags = listOf( + listOf("e", originalNote.id()), + listOf("p", originalNote.pubKey()), + listOf("relays") + relays + ) + if (originalNote is AddressableEvent) { + tags = tags + listOf(listOf("a", originalNote.address().toTag())) + } + if (pollOption != null && pollOption >= 0) { + tags = tags + listOf(listOf(POLL_OPTION, pollOption.toString())) + } + + val id = generateId(pubKey, createdAt, kind, tags, message) + return LnZapRequestEvent(id.toHexKey(), pubKey, createdAt, tags, message, "") + } + + fun createAnonymous( + originalNote: EventInterface, + relays: Set, + pollOption: Int?, + message: String, + createdAt: Long = TimeUtils.now() + ): LnZapRequestEvent { + var tags = listOf( + listOf("e", originalNote.id()), + listOf("p", originalNote.pubKey()), + listOf("relays") + relays + ) + if (originalNote is AddressableEvent) { + tags = tags + listOf(listOf("a", originalNote.address().toTag())) + } + if (pollOption != null && pollOption >= 0) { + tags = tags + listOf(listOf(POLL_OPTION, pollOption.toString())) + } + + tags = tags + listOf(listOf("anon", "")) + val privkey = CryptoUtils.privkeyCreate() + val pubKey = CryptoUtils.pubkeyCreate(privkey).toHexKey() + + val id = generateId(pubKey, createdAt, kind, tags, message) + val sig = CryptoUtils.sign(id, privkey) + return LnZapRequestEvent(id.toHexKey(), pubKey, createdAt, tags, message, sig.toHexKey()) + } + fun create( userHex: String, relays: Set,