refactor: move NamecoinSettings and NamecoinResolveState to commons module
Eliminates code duplication between Android and Desktop: - Move NamecoinSettings to commons/model/nip05DnsIdentifiers/namecoin/ (with @Serializable and @Stable annotations) - Extract NamecoinResolveState sealed class to its own file in commons - Move NamecoinSettingsTest to commons (shared by both platforms) - Replace Android NamecoinSettings.kt with a typealias to commons - Update all imports in Desktop and Android modules - Remove debug println statements from ImportFollowListDialog and Main
This commit is contained in:
+2
-17
@@ -20,6 +20,8 @@
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.desktop.service.namecoin
|
||||
|
||||
import com.vitorpamplona.amethyst.commons.model.nip05DnsIdentifiers.namecoin.NamecoinResolveState
|
||||
import com.vitorpamplona.amethyst.commons.model.nip05DnsIdentifiers.namecoin.NamecoinSettings
|
||||
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.DEFAULT_ELECTRUMX_SERVERS
|
||||
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumXClient
|
||||
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumxServer
|
||||
@@ -135,20 +137,3 @@ class DesktopNamecoinNameService(
|
||||
*/
|
||||
suspend fun clearCache() = cache.clear()
|
||||
}
|
||||
|
||||
/**
|
||||
* Observable state for a Namecoin resolution in progress.
|
||||
*/
|
||||
sealed class NamecoinResolveState {
|
||||
data object Loading : NamecoinResolveState()
|
||||
|
||||
data class Resolved(
|
||||
val result: NamecoinNostrResult,
|
||||
) : NamecoinResolveState()
|
||||
|
||||
data object NotFound : NamecoinResolveState()
|
||||
|
||||
data class Error(
|
||||
val message: String,
|
||||
) : NamecoinResolveState()
|
||||
}
|
||||
|
||||
+1
@@ -20,6 +20,7 @@
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.desktop.service.namecoin
|
||||
|
||||
import com.vitorpamplona.amethyst.commons.model.nip05DnsIdentifiers.namecoin.NamecoinSettings
|
||||
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumxServer
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
|
||||
-94
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
* 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.amethyst.desktop.service.namecoin
|
||||
|
||||
import androidx.compose.runtime.Stable
|
||||
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumxServer
|
||||
|
||||
/**
|
||||
* Immutable data class representing the current Namecoin resolution config.
|
||||
*
|
||||
* When custom servers are configured, they are used EXCLUSIVELY and the
|
||||
* hardcoded defaults are ignored. This gives privacy-conscious users full
|
||||
* control over which ElectrumX servers observe their name lookups.
|
||||
*/
|
||||
@Stable
|
||||
data class NamecoinSettings(
|
||||
/** Whether Namecoin resolution is enabled at all. */
|
||||
val enabled: Boolean = true,
|
||||
/**
|
||||
* Custom ElectrumX servers. When non-empty, these replace the defaults.
|
||||
*
|
||||
* Each entry is `host:port` (TLS) or `host:port:tcp` (plaintext).
|
||||
*/
|
||||
val customServers: List<String> = emptyList(),
|
||||
) {
|
||||
/** True when the user has configured at least one custom server. */
|
||||
val hasCustomServers: Boolean get() = customServers.isNotEmpty()
|
||||
|
||||
/**
|
||||
* Convert to [ElectrumxServer] instances used by the resolver.
|
||||
* Returns `null` when no valid custom servers are configured (use defaults).
|
||||
*/
|
||||
fun toElectrumxServers(): List<ElectrumxServer>? {
|
||||
if (customServers.isEmpty()) return null
|
||||
return customServers
|
||||
.mapNotNull { parseServerString(it) }
|
||||
.ifEmpty { null }
|
||||
}
|
||||
|
||||
companion object {
|
||||
val DEFAULT = NamecoinSettings()
|
||||
|
||||
/**
|
||||
* Parse `host:port` or `host:port:tcp` into an [ElectrumxServer].
|
||||
*
|
||||
* TLS is the default protocol. Append `:tcp` for plaintext
|
||||
* (useful for `.onion` addresses and local servers).
|
||||
*
|
||||
* `.onion` addresses automatically get `trustAllCerts = true`
|
||||
* since certificate verification is meaningless over Tor.
|
||||
*/
|
||||
fun parseServerString(s: String): ElectrumxServer? {
|
||||
val parts = s.trim().split(":")
|
||||
if (parts.size < 2) return null
|
||||
val host = parts[0].trim()
|
||||
val port = parts[1].trim().toIntOrNull() ?: return null
|
||||
if (host.isEmpty() || port <= 0 || port > 65535) return null
|
||||
val useSsl = parts.getOrNull(2)?.trim()?.lowercase() != "tcp"
|
||||
val isOnion = host.endsWith(".onion")
|
||||
return ElectrumxServer(
|
||||
host = host,
|
||||
port = port,
|
||||
useSsl = useSsl,
|
||||
trustAllCerts = isOnion || !useSsl,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Format an [ElectrumxServer] back to the `host:port[:tcp]` string form.
|
||||
*/
|
||||
fun formatServerString(server: ElectrumxServer): String {
|
||||
val base = "${server.host}:${server.port}"
|
||||
return if (server.useSsl) base else "$base:tcp"
|
||||
}
|
||||
}
|
||||
}
|
||||
-9
@@ -193,7 +193,6 @@ fun ImportFollowListDialog(
|
||||
|
||||
// Use connected relays only — available relays may include disconnected ones
|
||||
val relays = relayManager.connectedRelays.value
|
||||
println("[ImportFollows] Subscribing for kind 3 of $pubkey on ${relays.size} connected relays: $relays")
|
||||
|
||||
if (relays.isEmpty()) {
|
||||
importState = ImportState.Error("No relays connected. Check your relay settings.")
|
||||
@@ -217,7 +216,6 @@ fun ImportFollowListDialog(
|
||||
relay: NormalizedRelayUrl,
|
||||
forFilters: List<Filter>?,
|
||||
) {
|
||||
println("[ImportFollows] Received event kind=${event.kind} from $relay id=${event.id.take(12)}")
|
||||
if (event.kind == ContactListEvent.KIND && !receivedContactList) {
|
||||
receivedContactList = true
|
||||
val contactList = ContactListEvent(
|
||||
@@ -314,7 +312,6 @@ fun ImportFollowListDialog(
|
||||
relay: NormalizedRelayUrl,
|
||||
forFilters: List<Filter>?,
|
||||
) {
|
||||
println("[ImportFollows] EOSE from $relay receivedContactList=$receivedContactList")
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -357,7 +354,6 @@ fun ImportFollowListDialog(
|
||||
try {
|
||||
// Try raw hex pubkey first (exact 64-char hex)
|
||||
if (trimmed.length == 64 && trimmed.matches(Regex("^[0-9a-fA-F]{64}$"))) {
|
||||
println("[ImportFollows] Resolved raw hex: ${trimmed.take(16)}...")
|
||||
importState = ImportState.IdentifierResolved(trimmed.lowercase())
|
||||
return@launch
|
||||
}
|
||||
@@ -366,7 +362,6 @@ fun ImportFollowListDialog(
|
||||
if (trimmed.startsWith("npub1") || trimmed.startsWith("nprofile1") || trimmed.startsWith("nsec1")) {
|
||||
val bech32Result = decodePublicKeyAsHexOrNull(trimmed)
|
||||
if (bech32Result != null && bech32Result.length == 64) {
|
||||
println("[ImportFollows] Resolved bech32 to ${bech32Result.take(16)}...")
|
||||
importState = ImportState.IdentifierResolved(bech32Result)
|
||||
return@launch
|
||||
}
|
||||
@@ -376,10 +371,8 @@ fun ImportFollowListDialog(
|
||||
|
||||
// Try Namecoin (.bit, d/, id/)
|
||||
if (NamecoinNameResolver.isNamecoinIdentifier(trimmed) && namecoinService != null) {
|
||||
println("[ImportFollows] Resolving Namecoin identifier: $trimmed")
|
||||
val result = namecoinService.resolvePubkey(trimmed)
|
||||
if (result != null && result.length == 64) {
|
||||
println("[ImportFollows] Namecoin resolved to ${result.take(16)}...")
|
||||
importState = ImportState.IdentifierResolved(result)
|
||||
return@launch
|
||||
}
|
||||
@@ -389,10 +382,8 @@ fun ImportFollowListDialog(
|
||||
|
||||
// Try NIP-05 HTTP (user@domain)
|
||||
if (trimmed.contains("@")) {
|
||||
println("[ImportFollows] Resolving NIP-05 HTTP: $trimmed")
|
||||
val result = resolveNip05Http(trimmed)
|
||||
if (result != null && result.length == 64) {
|
||||
println("[ImportFollows] NIP-05 resolved to ${result.take(16)}...")
|
||||
importState = ImportState.IdentifierResolved(result)
|
||||
return@launch
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ import com.vitorpamplona.amethyst.desktop.ui.search.SearchSyncBanner
|
||||
import com.vitorpamplona.amethyst.desktop.service.namecoin.DesktopNamecoinNameService
|
||||
import com.vitorpamplona.amethyst.desktop.service.namecoin.LocalNamecoinPreferences
|
||||
import com.vitorpamplona.amethyst.desktop.service.namecoin.LocalNamecoinService
|
||||
import com.vitorpamplona.amethyst.desktop.service.namecoin.NamecoinResolveState
|
||||
import com.vitorpamplona.amethyst.commons.model.nip05DnsIdentifiers.namecoin.NamecoinResolveState
|
||||
import com.vitorpamplona.quartz.nip01Core.metadata.MetadataEvent
|
||||
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.NamecoinNameResolver
|
||||
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.NamecoinResolveOutcome
|
||||
|
||||
+1
-1
@@ -66,7 +66,7 @@ import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.vitorpamplona.amethyst.desktop.service.namecoin.NamecoinSettings
|
||||
import com.vitorpamplona.amethyst.commons.model.nip05DnsIdentifiers.namecoin.NamecoinSettings
|
||||
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.DEFAULT_ELECTRUMX_SERVERS
|
||||
|
||||
/**
|
||||
|
||||
-177
@@ -1,177 +0,0 @@
|
||||
/*
|
||||
* 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.amethyst.desktop.service.namecoin
|
||||
|
||||
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumxServer
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class NamecoinSettingsTest {
|
||||
// ── Server string parsing ──────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
fun `parses host colon port as TLS`() {
|
||||
val s = NamecoinSettings.parseServerString("example.com:50006")
|
||||
assertNotNull(s)
|
||||
assertEquals("example.com", s!!.host)
|
||||
assertEquals(50006, s.port)
|
||||
assertTrue(s.useSsl)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parses host colon port colon tcp as plaintext`() {
|
||||
val s = NamecoinSettings.parseServerString("example.com:50001:tcp")
|
||||
assertNotNull(s)
|
||||
assertEquals("example.com", s!!.host)
|
||||
assertEquals(50001, s.port)
|
||||
assertFalse(s.useSsl)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parses onion address`() {
|
||||
val s = NamecoinSettings.parseServerString("abc123def.onion:50001:tcp")
|
||||
assertNotNull(s)
|
||||
assertEquals("abc123def.onion", s!!.host)
|
||||
assertEquals(50001, s.port)
|
||||
assertFalse(s.useSsl)
|
||||
assertTrue(s.trustAllCerts)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `trims whitespace`() {
|
||||
val s = NamecoinSettings.parseServerString(" example.com : 50006 ")
|
||||
assertNotNull(s)
|
||||
assertEquals("example.com", s!!.host)
|
||||
assertEquals(50006, s.port)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `rejects empty host`() {
|
||||
assertNull(NamecoinSettings.parseServerString(":50006"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `rejects invalid port`() {
|
||||
assertNull(NamecoinSettings.parseServerString("example.com:abc"))
|
||||
assertNull(NamecoinSettings.parseServerString("example.com:0"))
|
||||
assertNull(NamecoinSettings.parseServerString("example.com:99999"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `rejects no port`() {
|
||||
assertNull(NamecoinSettings.parseServerString("example.com"))
|
||||
}
|
||||
|
||||
// ── Format round-trip ──────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
fun `formats TLS server without suffix`() {
|
||||
val server = ElectrumxServer("example.com", 50006, true)
|
||||
assertEquals("example.com:50006", NamecoinSettings.formatServerString(server))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `formats TCP server with tcp suffix`() {
|
||||
val server = ElectrumxServer("example.com", 50001, false)
|
||||
assertEquals("example.com:50001:tcp", NamecoinSettings.formatServerString(server))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `round-trips server string through parse and format`() {
|
||||
val original = "myserver.onion:50001:tcp"
|
||||
val parsed = NamecoinSettings.parseServerString(original)!!
|
||||
val formatted = NamecoinSettings.formatServerString(parsed)
|
||||
assertEquals(original, formatted)
|
||||
}
|
||||
|
||||
// ── toElectrumxServers ─────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
fun `returns null when no custom servers`() {
|
||||
val settings = NamecoinSettings(customServers = emptyList())
|
||||
assertNull(settings.toElectrumxServers())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `returns parsed list for valid custom servers`() {
|
||||
val settings = NamecoinSettings(
|
||||
customServers = listOf(
|
||||
"server1.com:50006",
|
||||
"server2.onion:50001:tcp",
|
||||
),
|
||||
)
|
||||
val servers = settings.toElectrumxServers()
|
||||
assertNotNull(servers)
|
||||
assertEquals(2, servers!!.size)
|
||||
assertEquals("server1.com", servers[0].host)
|
||||
assertTrue(servers[0].useSsl)
|
||||
assertEquals("server2.onion", servers[1].host)
|
||||
assertFalse(servers[1].useSsl)
|
||||
assertTrue(servers[1].trustAllCerts)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `skips invalid entries in custom server list`() {
|
||||
val settings = NamecoinSettings(
|
||||
customServers = listOf(
|
||||
"valid.com:50006",
|
||||
"invalid", // no port
|
||||
"also-invalid:abc", // non-numeric port
|
||||
),
|
||||
)
|
||||
val servers = settings.toElectrumxServers()
|
||||
assertNotNull(servers)
|
||||
assertEquals(1, servers!!.size)
|
||||
assertEquals("valid.com", servers[0].host)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `returns null when all custom servers are invalid`() {
|
||||
val settings = NamecoinSettings(customServers = listOf("bad", "also-bad"))
|
||||
assertNull(settings.toElectrumxServers())
|
||||
}
|
||||
|
||||
// ── hasCustomServers flag ──────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
fun `hasCustomServers is false when empty`() {
|
||||
assertFalse(NamecoinSettings().hasCustomServers)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hasCustomServers is true when populated`() {
|
||||
assertTrue(NamecoinSettings(customServers = listOf("x:1")).hasCustomServers)
|
||||
}
|
||||
|
||||
// ── Default settings ───────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
fun `default settings are enabled with no custom servers`() {
|
||||
val d = NamecoinSettings.DEFAULT
|
||||
assertTrue(d.enabled)
|
||||
assertTrue(d.customServers.isEmpty())
|
||||
assertFalse(d.hasCustomServers)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user