Allow dot in usernames per NIP-05 local-part spec

Extend usernameRE to [a-z0-9_.-], preserve dots in SanitizeForUsername,
and add tests for validation, sanitization, and nip05 sync precedence.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-05-05 06:10:14 +00:00
parent 7a1ceb49c3
commit bbfc64733a
4 changed files with 19 additions and 6 deletions

View File

@@ -5,7 +5,7 @@ import (
)
// SanitizeForUsername coerces an arbitrary profile string into a candidate
// that matches usernameRE: lowercase ASCII alphanumerics, `_`, and `-`,
// that matches usernameRE: lowercase ASCII alphanumerics, `_`, `-`, and `.`,
// length <= 30, with an alphanumeric first character. Returns "" when no
// usable handle can be derived.
func SanitizeForUsername(s string) string {
@@ -22,7 +22,7 @@ func SanitizeForUsername(s string) string {
case (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9'):
b.WriteRune(r)
prevSep = false
case r == '-' || r == '_':
case r == '-' || r == '_' || r == '.':
if b.Len() == 0 {
continue
}
@@ -42,9 +42,9 @@ func SanitizeForUsername(s string) string {
prevSep = true
}
}
out := strings.TrimRight(b.String(), "_-")
out := strings.TrimRight(b.String(), "_-.")
if len(out) > 30 {
out = strings.TrimRight(out[:30], "_-")
out = strings.TrimRight(out[:30], "_-.")
}
return out
}