Skip to content

Commit

Permalink
Merge pull request #335 from multiversx/MX-15294-random-seq-validity
Browse files Browse the repository at this point in the history
token random seq correct validity check
  • Loading branch information
axenteoctavian authored Nov 19, 2024
2 parents a1d08e6 + e2451e1 commit a0e4044
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
20 changes: 19 additions & 1 deletion data/esdt/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func IsValidPrefixedToken(token string) (string, bool) {
}

tokenRandSeq := tokenSplit[2]
if !(len(tokenRandSeq) >= esdtTickerNumRandChars) {
if !IsRandomSeqValid(tokenRandSeq) {
return "", false
}

Expand Down Expand Up @@ -83,3 +83,21 @@ func IsTickerValid(ticker string) bool {
func IsTokenTickerLenCorrect(tokenTickerLen int) bool {
return !(tokenTickerLen < minLengthForTickerName || tokenTickerLen > maxLengthForTickerName)
}

// IsRandomSeqValid checks if the token random sequence is valid
func IsRandomSeqValid(randomSeq string) bool {
if len(randomSeq) != esdtTickerNumRandChars {
return false
}

for _, ch := range randomSeq {
isSmallCharacter := ch >= 'a' && ch <= 'f'
isNumber := ch >= '0' && ch <= '9'
isReadable := isSmallCharacter || isNumber
if !isReadable {
return false
}
}

return true
}
10 changes: 5 additions & 5 deletions data/esdt/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ import (
)

func TestIsValidPrefixedToken(t *testing.T) {
prefix, valid := IsValidPrefixedToken("sov1-TKN-coffee")
prefix, valid := IsValidPrefixedToken("sov1-TKN-c0ffee")
require.True(t, valid)
require.Equal(t, "sov1", prefix)

prefix, valid = IsValidPrefixedToken("sOv1-TKN-coffee")
prefix, valid = IsValidPrefixedToken("sOv1-TKN-c0ffee")
require.False(t, valid)
require.Equal(t, "", prefix)

prefix, valid = IsValidPrefixedToken("sov1-TkN-coffee")
prefix, valid = IsValidPrefixedToken("sov1-TkN-c0ffee")
require.False(t, valid)
require.Equal(t, "", prefix)

prefix, valid = IsValidPrefixedToken("sov1-TKN-coffe")
prefix, valid = IsValidPrefixedToken("sov1-TKN-c0ffe")
require.False(t, valid)
require.Equal(t, "", prefix)

prefix, valid = IsValidPrefixedToken("sov1-TKN")
require.False(t, valid)
require.Equal(t, "", prefix)

prefix, valid = IsValidPrefixedToken("TKN-coffee")
prefix, valid = IsValidPrefixedToken("TKN-c0ffee")
require.False(t, valid)
require.Equal(t, "", prefix)
}
Expand Down

0 comments on commit a0e4044

Please sign in to comment.