Moves Relay information to Quartz

Renames Nip19 object for better readability
This commit is contained in:
Vitor Pamplona
2024-02-22 16:08:36 -05:00
parent 606c483d09
commit 2ee4b24c9b
25 changed files with 181 additions and 179 deletions
@@ -30,10 +30,10 @@ data class ATag(val kind: Int, val pubKeyHex: String, val dTag: String, val rela
fun toNAddr(): String {
return TlvBuilder()
.apply {
addString(Nip19.TlvTypes.SPECIAL, dTag)
addStringIfNotNull(Nip19.TlvTypes.RELAY, relay)
addHex(Nip19.TlvTypes.AUTHOR, pubKeyHex)
addInt(Nip19.TlvTypes.KIND, kind)
addString(Nip19Bech32.TlvTypes.SPECIAL, dTag)
addStringIfNotNull(Nip19Bech32.TlvTypes.RELAY, relay)
addHex(Nip19Bech32.TlvTypes.AUTHOR, pubKeyHex)
addInt(Nip19Bech32.TlvTypes.KIND, kind)
}
.build()
.toNAddress()
@@ -76,10 +76,10 @@ data class ATag(val kind: Int, val pubKeyHex: String, val dTag: String, val rela
if (key.startsWith("naddr")) {
val tlv = Tlv.parse(key.bechToBytes())
val d = tlv.firstAsString(Nip19.TlvTypes.SPECIAL) ?: ""
val relay = tlv.firstAsString(Nip19.TlvTypes.RELAY)
val author = tlv.firstAsHex(Nip19.TlvTypes.AUTHOR)
val kind = tlv.firstAsInt(Nip19.TlvTypes.KIND)
val d = tlv.firstAsString(Nip19Bech32.TlvTypes.SPECIAL) ?: ""
val relay = tlv.firstAsString(Nip19Bech32.TlvTypes.RELAY)
val author = tlv.firstAsHex(Nip19Bech32.TlvTypes.AUTHOR)
val kind = tlv.firstAsInt(Nip19Bech32.TlvTypes.KIND)
if (kind != null && author != null) {
return ATag(kind, author, d, relay)
@@ -23,14 +23,16 @@ package com.vitorpamplona.quartz.encoders
import android.util.Log
import java.util.regex.Pattern
val lnurlpPattern = Pattern.compile("(?i:http|https):\\/\\/((.+)\\/)*\\.well-known\\/lnurlp\\/(.*)")
class Lud06 {
companion object {
val LNURLP_PATTERN = Pattern.compile("(?i:http|https):\\/\\/((.+)\\/)*\\.well-known\\/lnurlp\\/(.*)")
}
fun toLud16(str: String): String? {
return try {
val url = toLnUrlp(str)
val matcher = lnurlpPattern.matcher(url)
val matcher = LNURLP_PATTERN.matcher(url)
matcher.find()
val domain = matcher.group(2)
val username = matcher.group(3)
@@ -0,0 +1,81 @@
/**
* Copyright (c) 2024 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 androidx.compose.runtime.Stable
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
@Stable
class Nip11RelayInformation(
val id: String?,
val name: String?,
val description: String?,
val pubkey: String?,
val contact: String?,
val supported_nips: List<Int>?,
val supported_nip_extensions: List<String>?,
val software: String?,
val version: String?,
val limitation: RelayInformationLimitation?,
val relay_countries: List<String>?,
val language_tags: List<String>?,
val tags: List<String>?,
val posting_policy: String?,
val payments_url: String?,
val fees: RelayInformationFees?,
) {
companion object {
val mapper =
jacksonObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
fun fromJson(json: String): Nip11RelayInformation = mapper.readValue(json, Nip11RelayInformation::class.java)
}
}
@Stable
class RelayInformationFee(
val amount: Int?,
val unit: String?,
val period: Int?,
val kinds: List<Int>?,
)
class RelayInformationFees(
val admission: List<RelayInformationFee>?,
val subscription: List<RelayInformationFee>?,
val publication: List<RelayInformationFee>?,
val retention: List<RelayInformationFee>?,
)
class RelayInformationLimitation(
val max_message_length: Int?,
val max_subscriptions: Int?,
val max_filters: Int?,
val max_limit: Int?,
val max_subid_length: Int?,
val min_prefix: Int?,
val max_event_tags: Int?,
val max_content_length: Int?,
val min_pow_difficulty: Int?,
val auth_required: Boolean?,
val payment_required: Boolean?,
)
@@ -25,7 +25,7 @@ import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.crypto.KeyPair
import java.util.regex.Pattern
object Nip19 {
object Nip19Bech32 {
enum class Type {
USER,
NOTE,
@@ -173,7 +173,7 @@ object Nip19 {
}
fun decodePublicKey(key: String): ByteArray {
val parsed = Nip19.uriToRoute(key)
val parsed = Nip19Bech32.uriToRoute(key)
val pubKeyParsed = parsed?.hex?.hexToByteArray()
return if (key.startsWith("nsec")) {
@@ -187,7 +187,7 @@ fun decodePublicKey(key: String): ByteArray {
fun decodePublicKeyAsHexOrNull(key: String): HexKey? {
return try {
val parsed = Nip19.uriToRoute(key)
val parsed = Nip19Bech32.uriToRoute(key)
val pubKeyParsed = parsed?.hex
if (key.startsWith("nsec")) {
@@ -203,37 +203,37 @@ fun decodePublicKeyAsHexOrNull(key: String): HexKey? {
}
fun TlvBuilder.addString(
type: Nip19.TlvTypes,
type: Nip19Bech32.TlvTypes,
string: String,
) = addString(type.id, string)
fun TlvBuilder.addHex(
type: Nip19.TlvTypes,
type: Nip19Bech32.TlvTypes,
key: HexKey,
) = addHex(type.id, key)
fun TlvBuilder.addInt(
type: Nip19.TlvTypes,
type: Nip19Bech32.TlvTypes,
data: Int,
) = addInt(type.id, data)
fun TlvBuilder.addStringIfNotNull(
type: Nip19.TlvTypes,
type: Nip19Bech32.TlvTypes,
data: String?,
) = addStringIfNotNull(type.id, data)
fun TlvBuilder.addHexIfNotNull(
type: Nip19.TlvTypes,
type: Nip19Bech32.TlvTypes,
data: HexKey?,
) = addHexIfNotNull(type.id, data)
fun TlvBuilder.addIntIfNotNull(
type: Nip19.TlvTypes,
type: Nip19Bech32.TlvTypes,
data: Int?,
) = addIntIfNotNull(type.id, data)
fun Tlv.firstAsInt(type: Nip19.TlvTypes) = firstAsInt(type.id)
fun Tlv.firstAsInt(type: Nip19Bech32.TlvTypes) = firstAsInt(type.id)
fun Tlv.firstAsHex(type: Nip19.TlvTypes) = firstAsHex(type.id)
fun Tlv.firstAsHex(type: Nip19Bech32.TlvTypes) = firstAsHex(type.id)
fun Tlv.firstAsString(type: Nip19.TlvTypes) = firstAsString(type.id)
fun Tlv.firstAsString(type: Nip19Bech32.TlvTypes) = firstAsString(type.id)
@@ -23,8 +23,8 @@ package com.vitorpamplona.quartz.events
import android.util.Log
import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.encoders.Nip19
import com.vitorpamplona.quartz.encoders.Nip19.nip19regex
import com.vitorpamplona.quartz.encoders.Nip19Bech32
import com.vitorpamplona.quartz.encoders.Nip19Bech32.nip19regex
import java.util.regex.Pattern
val tagSearch = Pattern.compile("(?:\\s|\\A)\\#\\[([0-9]+)\\]")
@@ -93,10 +93,10 @@ open class BaseTextNoteEvent(
val additionalChars = matcher2.group(4) // additional chars
try {
val parsed = Nip19.parseComponents(uriScheme, type, key, additionalChars)
val parsed = Nip19Bech32.parseComponents(uriScheme, type, key, additionalChars)
if (parsed != null) {
if (parsed.type == Nip19.Type.USER) {
if (parsed.type == Nip19Bech32.Type.USER) {
returningList.add(parsed.hex)
}
}
@@ -137,10 +137,10 @@ open class BaseTextNoteEvent(
val key = matcher2.group(3) // bech32
val additionalChars = matcher2.group(4) // additional chars
val parsed = Nip19.parseComponents(uriScheme, type, key, additionalChars)
val parsed = Nip19Bech32.parseComponents(uriScheme, type, key, additionalChars)
if (parsed != null) {
if (parsed.type == Nip19.Type.EVENT || parsed.type == Nip19.Type.ADDRESS || parsed.type == Nip19.Type.NOTE) {
if (parsed.type == Nip19Bech32.Type.EVENT || parsed.type == Nip19Bech32.Type.ADDRESS || parsed.type == Nip19Bech32.Type.NOTE) {
citations.add(parsed.hex)
}
}
@@ -37,7 +37,7 @@ import com.vitorpamplona.quartz.crypto.CryptoUtils
import com.vitorpamplona.quartz.encoders.ATag
import com.vitorpamplona.quartz.encoders.Hex
import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.encoders.Nip19
import com.vitorpamplona.quartz.encoders.Nip19Bech32
import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.signers.NostrSigner
import com.vitorpamplona.quartz.utils.TimeUtils
@@ -253,7 +253,7 @@ open class Event(
return if (this is AddressableEvent) {
ATag(kind, pubKey, dTag(), null).toNAddr()
} else {
Nip19.createNEvent(id, pubKey, kind, null)
Nip19Bech32.createNEvent(id, pubKey, kind, null)
}
}