[#180] nns: allow hyphen in names

Both amazon bucket naming rules and DNS label names prohibit hyphen as
first or last character, but allow it in the middle.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-11-26 17:05:06 +03:00 committed by Alex Vanin
parent f0868c89fd
commit c7a02f0259
2 changed files with 16 additions and 3 deletions

View file

@ -687,6 +687,8 @@ func checkCommittee() {
} }
// checkFragment validates root or a part of domain name. // checkFragment validates root or a part of domain name.
// 1. Root domain must start with a letter.
// 2. All other fragments must start and end in a letter or a digit.
func checkFragment(v string, isRoot bool) bool { func checkFragment(v string, isRoot bool) bool {
maxLength := maxDomainNameFragmentLength maxLength := maxDomainNameFragmentLength
if isRoot { if isRoot {
@ -705,12 +707,12 @@ func checkFragment(v string, isRoot bool) bool {
return false return false
} }
} }
for i := 1; i < len(v); i++ { for i := 1; i < len(v)-1; i++ {
if !isAlNum(v[i]) { if v[i] != '-' && !isAlNum(v[i]) {
return false return false
} }
} }
return true return isAlNum(v[len(v)-1])
} }
// isAlNum checks whether provided char is a lowercase letter or a number. // isAlNum checks whether provided char is a lowercase letter or a number.

View file

@ -82,6 +82,17 @@ func TestNNSRegister(t *testing.T) {
"myemail@nspcc.ru", refresh, retry, expire, ttl) "myemail@nspcc.ru", refresh, retry, expire, ttl)
c3 := c.WithSigners(accTop, acc) c3 := c.WithSigners(accTop, acc)
t.Run("domain names with hyphen", func(t *testing.T) {
c3.InvokeFail(t, "invalid domain name format", "register",
"-testdomain.com", acc.ScriptHash(),
"myemail@nspcc.ru", refresh, retry, expire, ttl)
c3.InvokeFail(t, "invalid domain name format", "register",
"testdomain-.com", acc.ScriptHash(),
"myemail@nspcc.ru", refresh, retry, expire, ttl)
c3.Invoke(t, true, "register",
"test-domain.com", acc.ScriptHash(),
"myemail@nspcc.ru", refresh, retry, expire, ttl)
})
c3.Invoke(t, true, "register", c3.Invoke(t, true, "register",
"testdomain.com", acc.ScriptHash(), "testdomain.com", acc.ScriptHash(),
"myemail@nspcc.ru", refresh, retry, expire, ttl) "myemail@nspcc.ru", refresh, retry, expire, ttl)