Creates an interface for Nip05Client

This commit is contained in:
Vitor Pamplona
2026-03-10 14:37:04 -04:00
parent 9b233c8679
commit 1de38ad9ec
5 changed files with 111 additions and 17 deletions
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2025 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.nip05DnsIdentifiers
import com.vitorpamplona.quartz.nip01Core.core.HexKey
interface INip05Client {
suspend fun verify(
nip05: Nip05Id,
hexKey: HexKey,
): Boolean
suspend fun get(nip05: Nip05Id): Nip05KeyInfo?
suspend fun load(nip05: Nip05Id): KeyInfoSet?
suspend fun list(domain: String): KeyInfoSet
}
class EmptyNip05Client : INip05Client {
override suspend fun verify(
nip05: Nip05Id,
hexKey: HexKey,
) = true
override suspend fun get(nip05: Nip05Id): Nip05KeyInfo? = null
override suspend fun load(nip05: Nip05Id): KeyInfoSet? = null
override suspend fun list(domain: String): KeyInfoSet = KeyInfoSet(emptyMap(), emptyMap())
}
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2025 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.nip05DnsIdentifiers
import kotlinx.serialization.Serializable
@Serializable
data class KeyInfoSet(
val names: Map<String, String>,
val relays: Map<String, List<String>>,
)
@@ -25,19 +25,14 @@ import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.NamecoinNameResolver
import kotlinx.coroutines.CancellationException
data class Nip05KeyInfo(
val pubkey: HexKey,
val relays: List<String>,
)
@Stable
class Nip05Client(
val fetcher: Nip05Fetcher,
val namecoinResolver: NamecoinNameResolver? = null,
) {
) : INip05Client {
val parser = Nip05Parser()
suspend fun verify(
override suspend fun verify(
nip05: Nip05Id,
hexKey: HexKey,
): Boolean {
@@ -64,7 +59,7 @@ class Nip05Client(
}
}
suspend fun get(nip05: Nip05Id): Nip05KeyInfo? {
override suspend fun get(nip05: Nip05Id): Nip05KeyInfo? {
// Namecoin: route .bit domains to blockchain resolution
if (namecoinResolver != null && NamecoinNameResolver.isNamecoinIdentifier(nip05.toValue())) {
val result = namecoinResolver.resolve(nip05.toValue()) ?: return null
@@ -74,9 +69,9 @@ class Nip05Client(
return parser.parseHexKeyAndRelays(nip05, fetchNip05Data(nip05))
}
suspend fun load(nip05: Nip05Id) = parser.parse(fetchNip05Data(nip05))
override suspend fun load(nip05: Nip05Id) = parser.parse(fetchNip05Data(nip05))
suspend fun list(domain: String) = parser.parse(fetchNip05Data(domain))
override suspend fun list(domain: String) = parser.parse(fetchNip05Data(domain))
suspend fun fetchNip05Data(nip05: Nip05Id): String {
val url = nip05.toUserUrl()
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2025 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.nip05DnsIdentifiers
import com.vitorpamplona.quartz.nip01Core.core.HexKey
data class Nip05KeyInfo(
val pubkey: HexKey,
val relays: List<String>,
)
@@ -22,18 +22,11 @@ package com.vitorpamplona.quartz.nip05DnsIdentifiers
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.utils.text
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
@Serializable
data class KeyInfoSet(
val names: Map<String, String>,
val relays: Map<String, List<String>>,
)
class Nip05Parser {
fun toJson(keyInfo: KeyInfoSet) = Json.encodeToString(keyInfo)