Skip to content

Commit

Permalink
Merge branch 'rc/v1.7.next1' into merge-rc-1-7-next-1-into-feat-sover…
Browse files Browse the repository at this point in the history
…eign
  • Loading branch information
mariusmihaic committed May 31, 2024
2 parents 2ee16f8 + 7befa59 commit 9f259f2
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
85 changes: 85 additions & 0 deletions data/esdt/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package esdt

import "strings"

const (
// esdtTickerNumRandChars represents the number of hex-encoded random characters sequence of a ticker
esdtTickerNumRandChars = 6
// separatorChar represents the character that separated the token ticker by the random sequence
separatorChar = "-"
// minLengthForTickerName represents the minimum number of characters a token's ticker can have
minLengthForTickerName = 3
// maxLengthForTickerName represents the maximum number of characters a token's ticker can have
maxLengthForTickerName = 10
// maxLengthESDTPrefix represents the maximum number of characters a token's prefix can have
maxLengthESDTPrefix = 4
// minLengthESDTPrefix represents the minimum number of characters a token's prefix can have
minLengthESDTPrefix = 1
)

// IsValidPrefixedToken checks if the provided token is valid, and returns if prefix if so
func IsValidPrefixedToken(token string) (string, bool) {
tokenSplit := strings.Split(token, separatorChar)
if len(tokenSplit) < 3 {
return "", false
}

prefix := tokenSplit[0]
if !IsValidTokenPrefix(prefix) {
return "", false
}

tokenTicker := tokenSplit[1]
if !IsTickerValid(tokenTicker) {
return "", false
}

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

return prefix, true
}

// IsValidTokenPrefix checks if the token prefix is valid
func IsValidTokenPrefix(prefix string) bool {
prefixLen := len(prefix)
if prefixLen > maxLengthESDTPrefix || prefixLen < minLengthESDTPrefix {
return false
}

for _, ch := range prefix {
isLowerCaseCharacter := ch >= 'a' && ch <= 'z'
isNumber := ch >= '0' && ch <= '9'
isAllowedChar := isLowerCaseCharacter || isNumber
if !isAllowedChar {
return false
}
}

return true
}

// IsTickerValid checks if the token ticker is valid
func IsTickerValid(ticker string) bool {
if !IsTokenTickerLenCorrect(len(ticker)) {
return false
}

for _, ch := range ticker {
isUpperCaseCharacter := ch >= 'A' && ch <= 'Z'
isNumber := ch >= '0' && ch <= '9'
isAllowedChar := isUpperCaseCharacter || isNumber
if !isAllowedChar {
return false
}
}

return true
}

// IsTokenTickerLenCorrect checks if the token ticker len is correct
func IsTokenTickerLenCorrect(tokenTickerLen int) bool {
return !(tokenTickerLen < minLengthForTickerName || tokenTickerLen > maxLengthForTickerName)
}
62 changes: 62 additions & 0 deletions data/esdt/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package esdt

import (
"testing"

"github.com/stretchr/testify/require"
)

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

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

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

prefix, valid = IsValidPrefixedToken("sov1-TKN-coffe")
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")
require.False(t, valid)
require.Equal(t, "", prefix)
}

func TestIsValidTokenPrefix(t *testing.T) {
require.False(t, IsValidTokenPrefix(""))
require.False(t, IsValidTokenPrefix("-"))
require.False(t, IsValidTokenPrefix("prefix"))
require.False(t, IsValidTokenPrefix("prefi"))
require.False(t, IsValidTokenPrefix("Prfx"))
require.False(t, IsValidTokenPrefix("pX4"))
require.False(t, IsValidTokenPrefix("px-4"))

require.True(t, IsValidTokenPrefix("pref"))
require.True(t, IsValidTokenPrefix("sv1"))
}

func TestIsTickerValid(t *testing.T) {
require.False(t, IsTickerValid("TK"))
require.False(t, IsTickerValid("TKn"))
require.False(t, IsTickerValid("T0KEN-"))

require.True(t, IsTickerValid("T0KEN"))
}

func TestIsTokenTickerLenCorrect(t *testing.T) {
require.False(t, IsTokenTickerLenCorrect(len("TOKENALICEALICE")))
require.False(t, IsTokenTickerLenCorrect(len("AL")))

require.True(t, IsTokenTickerLenCorrect(len("ALC")))
require.True(t, IsTokenTickerLenCorrect(len("ALICE")))
}

0 comments on commit 9f259f2

Please sign in to comment.