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

View File

@ -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
}

View File

@ -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(

View File

@ -99,6 +99,58 @@ class LnZapRequestEvent(
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(
userHex: String,
relays: Set<String>,