Adds support for NIP-29 in public messages and new DMs. NIP-54 stays in NIP-54

This commit is contained in:
Vitor Pamplona
2024-02-01 18:31:28 -05:00
parent 54155a3c30
commit e56377f8c3
18 changed files with 277 additions and 202 deletions
@@ -0,0 +1,76 @@
/**
* Copyright (c) 2023 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quartz.encoders
import com.vitorpamplona.quartz.events.FileHeaderEvent
class Nip29 {
companion object {
private const val IMETA = "imeta"
}
fun convertFromFileHeader(header: FileHeaderEvent): Array<String>? {
val myUrl = header.url() ?: return null
return createTag(
myUrl,
header.tags,
)
}
fun createTag(
imageUrl: String,
tags: Array<Array<String>>,
): Array<String> {
return arrayOf(
IMETA,
"url $imageUrl",
) +
tags.mapNotNull {
if (it.isNotEmpty() && it[0] != "url") {
if (it.size > 1) {
"${it[0]} ${it[1]}"
} else {
"${it[0]}}"
}
} else {
null
}
}
}
fun parse(
imageUrl: String,
tags: Array<Array<String>>,
): Map<String, String> {
return tags.firstOrNull {
it.size > 1 && it[0] == IMETA && it[1] == "url $imageUrl"
}?.let { tagList ->
tagList.associate { tag ->
val parts = tag.split(" ", limit = 2)
when (parts.size) {
2 -> parts[0] to parts[1]
1 -> parts[0] to ""
else -> "" to ""
}
}
} ?: emptyMap()
}
}
@@ -0,0 +1,80 @@
/**
* Copyright (c) 2023 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quartz.encoders
import com.vitorpamplona.quartz.events.FileHeaderEvent
import java.net.URI
import java.net.URLDecoder
import java.net.URLEncoder
import kotlin.coroutines.cancellation.CancellationException
class Nip54 {
fun convertFromFileHeader(header: FileHeaderEvent): String? {
val myUrl = header.url() ?: return null
return createUrl(
myUrl,
header.tags,
)
}
fun createUrl(
imageUrl: String,
tags: Array<Array<String>>,
): String {
val extension =
tags.mapNotNull {
if (it.isNotEmpty() && it[0] != "url") {
if (it.size > 1) {
"${it[0]}=${URLEncoder.encode(it[1], "utf-8")}"
} else {
"${it[0]}}="
}
} else {
null
}
}.joinToString("&")
return if (imageUrl.contains("#")) {
"$imageUrl&$extension"
} else {
"$imageUrl#$extension"
}
}
fun parse(url: String): Map<String, String> {
return try {
fragments(URI(url))
} catch (e: Exception) {
if (e is CancellationException) throw e
emptyMap()
}
}
private fun fragments(uri: URI): Map<String, String> {
if (uri.rawFragment == null) return emptyMap()
return uri.rawFragment.split('&').associate { keyValuePair ->
val parts = keyValuePair.split('=')
val name = parts.firstOrNull() ?: ""
val value = parts.getOrNull(1)?.let { URLDecoder.decode(it, "UTF-8") } ?: ""
Pair(name, value)
}
}
}
@@ -22,6 +22,7 @@ package com.vitorpamplona.quartz.events
import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.encoders.Nip29
import com.vitorpamplona.quartz.signers.NostrSigner
import com.vitorpamplona.quartz.utils.TimeUtils
@@ -58,7 +59,7 @@ class ChannelMessageEvent(
markAsSensitive: Boolean,
zapRaiserAmount: Long?,
geohash: String? = null,
nip94attachments: List<Event>? = null,
nip94attachments: List<FileHeaderEvent>? = null,
onReady: (ChannelMessageEvent) -> Unit,
) {
val tags =
@@ -77,7 +78,9 @@ class ChannelMessageEvent(
geohash?.let { tags.addAll(geohashMipMap(it)) }
nip94attachments?.let {
it.forEach {
// tags.add(arrayOf("nip94", it.toJson()))
Nip29().convertFromFileHeader(it)?.let {
tags.add(it)
}
}
}
tags.add(
@@ -23,6 +23,7 @@ package com.vitorpamplona.quartz.events
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.encoders.Nip29
import com.vitorpamplona.quartz.signers.NostrSigner
import com.vitorpamplona.quartz.utils.TimeUtils
import kotlinx.collections.immutable.ImmutableSet
@@ -80,6 +81,7 @@ class ChatMessageEvent(
geohash: String? = null,
signer: NostrSigner,
createdAt: Long = TimeUtils.now(),
nip94attachments: List<FileHeaderEvent>? = null,
onReady: (ChatMessageEvent) -> Unit,
) {
val tags = mutableListOf<Array<String>>()
@@ -95,6 +97,13 @@ class ChatMessageEvent(
zapRaiserAmount?.let { tags.add(arrayOf("zapraiser", "$it")) }
geohash?.let { tags.addAll(geohashMipMap(it)) }
subject?.let { tags.add(arrayOf("subject", it)) }
nip94attachments?.let {
it.forEach {
Nip29().convertFromFileHeader(it)?.let {
tags.add(it)
}
}
}
// tags.add(arrayOf("alt", alt))
signer.sign(createdAt, KIND, tags.toTypedArray(), msg, onReady)
@@ -62,17 +62,17 @@ class FileHeaderEvent(
const val KIND = 1063
const val ALT_DESCRIPTION = "Verifiable file url"
private const val URL = "url"
private const val ENCRYPTION_KEY = "aes-256-gcm"
private const val MIME_TYPE = "m"
private const val FILE_SIZE = "size"
private const val DIMENSION = "dim"
private const val HASH = "x"
private const val MAGNET_URI = "magnet"
private const val TORRENT_INFOHASH = "i"
private const val BLUR_HASH = "blurhash"
private const val ORIGINAL_HASH = "ox"
private const val ALT = "alt"
const val URL = "url"
const val ENCRYPTION_KEY = "aes-256-gcm"
const val MIME_TYPE = "m"
const val FILE_SIZE = "size"
const val DIMENSION = "dim"
const val HASH = "x"
const val MAGNET_URI = "magnet"
const val TORRENT_INFOHASH = "i"
const val BLUR_HASH = "blurhash"
const val ORIGINAL_HASH = "ox"
const val ALT = "alt"
fun create(
url: String,
@@ -23,6 +23,7 @@ package com.vitorpamplona.quartz.events
import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.encoders.ATag
import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.encoders.Nip29
import com.vitorpamplona.quartz.signers.NostrSigner
import com.vitorpamplona.quartz.utils.TimeUtils
@@ -70,7 +71,7 @@ class LiveActivitiesChatMessageEvent(
markAsSensitive: Boolean,
zapRaiserAmount: Long?,
geohash: String? = null,
nip94attachments: List<Event>? = null,
nip94attachments: List<FileHeaderEvent>? = null,
onReady: (LiveActivitiesChatMessageEvent) -> Unit,
) {
val content = message
@@ -90,7 +91,9 @@ class LiveActivitiesChatMessageEvent(
geohash?.let { tags.addAll(geohashMipMap(it)) }
nip94attachments?.let {
it.forEach {
// tags.add(arrayOf("nip94", it.toJson()))
Nip29().convertFromFileHeader(it)?.let {
tags.add(it)
}
}
}
tags.add(arrayOf("alt", ALT))
@@ -77,6 +77,7 @@ class NIP24Factory {
markAsSensitive: Boolean = false,
zapRaiserAmount: Long? = null,
geohash: String? = null,
nip94attachments: List<FileHeaderEvent>? = null,
onReady: (Result) -> Unit,
) {
val senderPublicKey = signer.pubKey
@@ -92,6 +93,7 @@ class NIP24Factory {
markAsSensitive = markAsSensitive,
zapRaiserAmount = zapRaiserAmount,
geohash = geohash,
nip94attachments = nip94attachments,
) { senderMessage ->
createWraps(senderMessage, to.plus(senderPublicKey).toSet(), signer) { wraps ->
onReady(
@@ -23,6 +23,7 @@ package com.vitorpamplona.quartz.events
import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.encoders.ATag
import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.encoders.Nip29
import com.vitorpamplona.quartz.signers.NostrSigner
import com.vitorpamplona.quartz.utils.TimeUtils
@@ -78,7 +79,7 @@ class PollNoteEvent(
markAsSensitive: Boolean,
zapRaiserAmount: Long?,
geohash: String? = null,
nip94attachments: List<Event>? = null,
nip94attachments: List<FileHeaderEvent>? = null,
onReady: (PollNoteEvent) -> Unit,
) {
val tags = mutableListOf<Array<String>>()
@@ -104,7 +105,9 @@ class PollNoteEvent(
geohash?.let { tags.addAll(geohashMipMap(it)) }
nip94attachments?.let {
it.forEach {
// tags.add(arrayOf("nip94", it.toJson()))
Nip29().convertFromFileHeader(it)?.let {
tags.add(it)
}
}
}
tags.add(arrayOf("alt", ALT))
@@ -24,6 +24,7 @@ import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.encoders.Hex
import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.encoders.HexValidator
import com.vitorpamplona.quartz.encoders.Nip54
import com.vitorpamplona.quartz.signers.NostrSigner
import com.vitorpamplona.quartz.utils.TimeUtils
import kotlinx.collections.immutable.persistentSetOf
@@ -122,14 +123,24 @@ class PrivateDmEvent(
markAsSensitive: Boolean,
zapRaiserAmount: Long?,
geohash: String? = null,
nip94attachments: List<FileHeaderEvent>? = null,
onReady: (PrivateDmEvent) -> Unit,
) {
val message =
var message = msg
nip94attachments?.forEach {
val myUrl = it.url()
if (myUrl != null) {
message = message.replace(myUrl, Nip54().createUrl(myUrl, it.tags))
}
}
message =
if (advertiseNip18) {
NIP_18_ADVERTISEMENT
NIP_18_ADVERTISEMENT + message
} else {
""
} + msg
message
}
val tags = mutableListOf<Array<String>>()
publishedRecipientPubKey?.let { tags.add(arrayOf("p", publishedRecipientPubKey)) }
replyTos?.forEach { tags.add(arrayOf("e", it)) }
@@ -142,6 +153,15 @@ class PrivateDmEvent(
}
zapRaiserAmount?.let { tags.add(arrayOf("zapraiser", "$it")) }
geohash?.let { tags.addAll(geohashMipMap(it)) }
/* Privacy issue: DO NOT ADD THESE TO THE TAGS.
nip94attachments?.let {
it.forEach {
Nip29().convertFromFileHeader(it)?.let {
tags.add(it)
}
}
}*/
tags.add(arrayOf("alt", ALT))
signer.nip04Encrypt(message, recipientPubKey) { content ->
@@ -25,6 +25,7 @@ import com.linkedin.urls.detection.UrlDetector
import com.linkedin.urls.detection.UrlDetectorOptions
import com.vitorpamplona.quartz.encoders.ATag
import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.encoders.Nip29
import com.vitorpamplona.quartz.signers.NostrSigner
import com.vitorpamplona.quartz.utils.TimeUtils
@@ -55,7 +56,7 @@ class TextNoteEvent(
root: String?,
directMentions: Set<HexKey>,
geohash: String? = null,
nip94attachments: List<Event>? = null,
nip94attachments: List<FileHeaderEvent>? = null,
signer: NostrSigner,
createdAt: Long = TimeUtils.now(),
onReady: (TextNoteEvent) -> Unit,
@@ -106,7 +107,9 @@ class TextNoteEvent(
geohash?.let { tags.addAll(geohashMipMap(it)) }
nip94attachments?.let {
it.forEach {
// tags.add(arrayOf("nip94", it.toJson()))
Nip29().convertFromFileHeader(it)?.let {
tags.add(it)
}
}
}