fix(nip05): stop reporting verification errors as Verified
This commit is contained in:
+13
-6
@@ -47,14 +47,21 @@ data class Nip05Id(
|
||||
fun toDomainUrl(): String = domainUrl(domain)
|
||||
|
||||
companion object {
|
||||
// NIP-05 localpart: a-z, 0-9, -, _, .
|
||||
private val LOCAL_PART_REGEX = Regex("^[a-z0-9._-]+$")
|
||||
|
||||
// Hostname: dot-separated labels of [a-z0-9-], no leading/trailing hyphen,
|
||||
// require at least one dot so single-label garbage (e.g. "s!ayer") is rejected.
|
||||
private val DOMAIN_REGEX =
|
||||
Regex("^([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)(\\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)+$")
|
||||
|
||||
fun parse(nip05address: String): Nip05Id? {
|
||||
val parts = nip05address.trim().lowercase().split("@")
|
||||
|
||||
return when (parts.size) {
|
||||
2 -> Nip05Id(parts[0], parts[1])
|
||||
1 -> Nip05Id(parts[0], "_")
|
||||
else -> null
|
||||
}
|
||||
if (parts.size != 2) return null
|
||||
val (name, domain) = parts[0] to parts[1]
|
||||
if (!LOCAL_PART_REGEX.matches(name)) return null
|
||||
if (!DOMAIN_REGEX.matches(domain)) return null
|
||||
return Nip05Id(name, domain)
|
||||
}
|
||||
|
||||
fun assemble(
|
||||
|
||||
+39
@@ -94,6 +94,45 @@ class Nip05Test {
|
||||
assertNull(parsedNip05)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse rejects empty localpart`() {
|
||||
assertNull(Nip05Id.parse("@example.com"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse rejects empty domain`() {
|
||||
assertNull(Nip05Id.parse("alice@"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse rejects domain without a dot`() {
|
||||
assertNull(Nip05Id.parse("alice@localhost"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse rejects domain with illegal character`() {
|
||||
assertNull(Nip05Id.parse("_@s!ayer"))
|
||||
assertNull(Nip05Id.parse("@s!ayer"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse rejects localpart with illegal character`() {
|
||||
assertNull(Nip05Id.parse("al!ce@example.com"))
|
||||
assertNull(Nip05Id.parse("alice space@example.com"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse rejects bare string without at-sign`() {
|
||||
assertNull(Nip05Id.parse("alice"))
|
||||
assertNull(Nip05Id.parse("example.com"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse rejects domain label with leading or trailing hyphen`() {
|
||||
assertNull(Nip05Id.parse("alice@-example.com"))
|
||||
assertNull(Nip05Id.parse("alice@example-.com"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `execute assemble url with valid value returns nip05 url`() {
|
||||
// given
|
||||
|
||||
Reference in New Issue
Block a user