support for public and anon zaps

This commit is contained in:
greenart7c3
2023-09-01 12:25:42 -03:00
parent d9cf090cc9
commit bbba27752d
3 changed files with 125 additions and 16 deletions
@@ -337,9 +337,44 @@ class Account(
} }
fun createZapRequestFor(note: Note, pollOption: Int?, message: String = "", zapType: LnZapEvent.ZapType): LnZapRequestEvent? { 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 -> note.event?.let { event ->
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( return LnZapRequestEvent.create(
event, event,
userProfile().latestContactList?.relays()?.keys?.ifEmpty { null } userProfile().latestContactList?.relays()?.keys?.ifEmpty { null }
@@ -350,6 +385,7 @@ class Account(
zapType zapType
) )
} }
}
return null return null
} }
@@ -1118,6 +1118,26 @@ private fun zapClick(
.show() .show()
} }
} else if (!accountViewModel.isWriteable()) { } else if (!accountViewModel.isWriteable()) {
if (accountViewModel.loggedInWithAmber()) {
if (accountViewModel.account.zapAmountChoices.size == 1) {
accountViewModel.zap(
baseNote,
accountViewModel.account.zapAmountChoices.first() * 1000,
null,
"",
context,
onError = onError,
onProgress = {
scope.launch(Dispatchers.Main) {
onZappingProgress(it)
}
},
zapType = accountViewModel.account.defaultZapType
)
} else if (accountViewModel.account.zapAmountChoices.size > 1) {
onMultipleChoices()
}
} else {
scope.launch { scope.launch {
Toast Toast
.makeText( .makeText(
@@ -1127,6 +1147,7 @@ private fun zapClick(
) )
.show() .show()
} }
}
} else if (accountViewModel.account.zapAmountChoices.size == 1) { } else if (accountViewModel.account.zapAmountChoices.size == 1) {
accountViewModel.zap( accountViewModel.zap(
baseNote, baseNote,
@@ -99,6 +99,58 @@ class LnZapRequestEvent(
return LnZapRequestEvent(id.toHexKey(), pubKey, createdAt, tags, content, sig.toHexKey()) return LnZapRequestEvent(id.toHexKey(), pubKey, createdAt, tags, content, sig.toHexKey())
} }
fun createPublic(
originalNote: EventInterface,
relays: Set<String>,
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<String>,
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( fun create(
userHex: String, userHex: String,
relays: Set<String>, relays: Set<String>,