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:
M
2026-03-24 09:53:31 +11:00
committed by m
parent 0b636d62aa
commit 5369694b4d
10 changed files with 52 additions and 230 deletions
@@ -20,6 +20,7 @@
*/ */
package com.vitorpamplona.amethyst.service.namecoin package com.vitorpamplona.amethyst.service.namecoin
import com.vitorpamplona.amethyst.commons.model.nip05DnsIdentifiers.namecoin.NamecoinResolveState
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.DEFAULT_ELECTRUMX_SERVERS import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.DEFAULT_ELECTRUMX_SERVERS
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumXClient import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumXClient
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumxServer import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumxServer
@@ -151,20 +152,3 @@ class NamecoinNameService(
*/ */
suspend fun clearCache() = cache.clear() 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,181 +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.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.usePinnedTrustStore)
}
@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].usePinnedTrustStore)
}
@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)
}
}
@@ -0,0 +1,40 @@
/*
* 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.commons.model.nip05DnsIdentifiers.namecoin
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.NamecoinNostrResult
/**
* 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()
}
@@ -18,10 +18,11 @@
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * 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. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
package com.vitorpamplona.amethyst.desktop.service.namecoin package com.vitorpamplona.amethyst.commons.model.nip05DnsIdentifiers.namecoin
import androidx.compose.runtime.Stable import androidx.compose.runtime.Stable
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumxServer import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumxServer
import kotlinx.serialization.Serializable
/** /**
* Immutable data class representing the current Namecoin resolution config. * Immutable data class representing the current Namecoin resolution config.
@@ -30,6 +31,7 @@ import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumxServer
* hardcoded defaults are ignored. This gives privacy-conscious users full * hardcoded defaults are ignored. This gives privacy-conscious users full
* control over which ElectrumX servers observe their name lookups. * control over which ElectrumX servers observe their name lookups.
*/ */
@Serializable
@Stable @Stable
data class NamecoinSettings( data class NamecoinSettings(
/** Whether Namecoin resolution is enabled at all. */ /** Whether Namecoin resolution is enabled at all. */
@@ -18,7 +18,7 @@
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * 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. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
package com.vitorpamplona.amethyst.desktop.service.namecoin package com.vitorpamplona.amethyst.commons.model.nip05DnsIdentifiers.namecoin
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumxServer import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumxServer
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
@@ -56,7 +56,7 @@ class NamecoinSettingsTest {
assertEquals("abc123def.onion", s!!.host) assertEquals("abc123def.onion", s!!.host)
assertEquals(50001, s.port) assertEquals(50001, s.port)
assertFalse(s.useSsl) assertFalse(s.useSsl)
assertTrue(s.trustAllCerts) assertTrue(s.usePinnedTrustStore)
} }
@Test @Test
@@ -129,7 +129,7 @@ class NamecoinSettingsTest {
assertTrue(servers[0].useSsl) assertTrue(servers[0].useSsl)
assertEquals("server2.onion", servers[1].host) assertEquals("server2.onion", servers[1].host)
assertFalse(servers[1].useSsl) assertFalse(servers[1].useSsl)
assertTrue(servers[1].trustAllCerts) assertTrue(servers[1].usePinnedTrustStore)
} }
@Test @Test
@@ -20,6 +20,8 @@
*/ */
package com.vitorpamplona.amethyst.desktop.service.namecoin 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.DEFAULT_ELECTRUMX_SERVERS
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumXClient import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumXClient
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumxServer import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumxServer
@@ -135,20 +137,3 @@ class DesktopNamecoinNameService(
*/ */
suspend fun clearCache() = cache.clear() 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()
}
@@ -20,6 +20,7 @@
*/ */
package com.vitorpamplona.amethyst.desktop.service.namecoin 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.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.ElectrumxServer
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.readValue
@@ -193,7 +193,6 @@ fun ImportFollowListDialog(
// Use connected relays only — available relays may include disconnected ones // Use connected relays only — available relays may include disconnected ones
val relays = relayManager.connectedRelays.value val relays = relayManager.connectedRelays.value
println("[ImportFollows] Subscribing for kind 3 of $pubkey on ${relays.size} connected relays: $relays")
if (relays.isEmpty()) { if (relays.isEmpty()) {
importState = ImportState.Error("No relays connected. Check your relay settings.") importState = ImportState.Error("No relays connected. Check your relay settings.")
@@ -217,7 +216,6 @@ fun ImportFollowListDialog(
relay: NormalizedRelayUrl, relay: NormalizedRelayUrl,
forFilters: List<Filter>?, forFilters: List<Filter>?,
) { ) {
println("[ImportFollows] Received event kind=${event.kind} from $relay id=${event.id.take(12)}")
if (event.kind == ContactListEvent.KIND && !receivedContactList) { if (event.kind == ContactListEvent.KIND && !receivedContactList) {
receivedContactList = true receivedContactList = true
val contactList = ContactListEvent( val contactList = ContactListEvent(
@@ -314,7 +312,6 @@ fun ImportFollowListDialog(
relay: NormalizedRelayUrl, relay: NormalizedRelayUrl,
forFilters: List<Filter>?, forFilters: List<Filter>?,
) { ) {
println("[ImportFollows] EOSE from $relay receivedContactList=$receivedContactList")
} }
}, },
) )
@@ -357,7 +354,6 @@ fun ImportFollowListDialog(
try { try {
// Try raw hex pubkey first (exact 64-char hex) // Try raw hex pubkey first (exact 64-char hex)
if (trimmed.length == 64 && trimmed.matches(Regex("^[0-9a-fA-F]{64}$"))) { 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()) importState = ImportState.IdentifierResolved(trimmed.lowercase())
return@launch return@launch
} }
@@ -366,7 +362,6 @@ fun ImportFollowListDialog(
if (trimmed.startsWith("npub1") || trimmed.startsWith("nprofile1") || trimmed.startsWith("nsec1")) { if (trimmed.startsWith("npub1") || trimmed.startsWith("nprofile1") || trimmed.startsWith("nsec1")) {
val bech32Result = decodePublicKeyAsHexOrNull(trimmed) val bech32Result = decodePublicKeyAsHexOrNull(trimmed)
if (bech32Result != null && bech32Result.length == 64) { if (bech32Result != null && bech32Result.length == 64) {
println("[ImportFollows] Resolved bech32 to ${bech32Result.take(16)}...")
importState = ImportState.IdentifierResolved(bech32Result) importState = ImportState.IdentifierResolved(bech32Result)
return@launch return@launch
} }
@@ -376,10 +371,8 @@ fun ImportFollowListDialog(
// Try Namecoin (.bit, d/, id/) // Try Namecoin (.bit, d/, id/)
if (NamecoinNameResolver.isNamecoinIdentifier(trimmed) && namecoinService != null) { if (NamecoinNameResolver.isNamecoinIdentifier(trimmed) && namecoinService != null) {
println("[ImportFollows] Resolving Namecoin identifier: $trimmed")
val result = namecoinService.resolvePubkey(trimmed) val result = namecoinService.resolvePubkey(trimmed)
if (result != null && result.length == 64) { if (result != null && result.length == 64) {
println("[ImportFollows] Namecoin resolved to ${result.take(16)}...")
importState = ImportState.IdentifierResolved(result) importState = ImportState.IdentifierResolved(result)
return@launch return@launch
} }
@@ -389,10 +382,8 @@ fun ImportFollowListDialog(
// Try NIP-05 HTTP (user@domain) // Try NIP-05 HTTP (user@domain)
if (trimmed.contains("@")) { if (trimmed.contains("@")) {
println("[ImportFollows] Resolving NIP-05 HTTP: $trimmed")
val result = resolveNip05Http(trimmed) val result = resolveNip05Http(trimmed)
if (result != null && result.length == 64) { if (result != null && result.length == 64) {
println("[ImportFollows] NIP-05 resolved to ${result.take(16)}...")
importState = ImportState.IdentifierResolved(result) importState = ImportState.IdentifierResolved(result)
return@launch 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.DesktopNamecoinNameService
import com.vitorpamplona.amethyst.desktop.service.namecoin.LocalNamecoinPreferences import com.vitorpamplona.amethyst.desktop.service.namecoin.LocalNamecoinPreferences
import com.vitorpamplona.amethyst.desktop.service.namecoin.LocalNamecoinService 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.nip01Core.metadata.MetadataEvent
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.NamecoinNameResolver import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.NamecoinNameResolver
import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.NamecoinResolveOutcome import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.NamecoinResolveOutcome
@@ -66,7 +66,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp 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 import com.vitorpamplona.quartz.nip05DnsIdentifiers.namecoin.DEFAULT_ELECTRUMX_SERVERS
/** /**