From 9ba698bcdbd282c18533be664cf20babe059105f Mon Sep 17 00:00:00 2001 From: Marius C Date: Tue, 23 Apr 2024 12:19:37 +0300 Subject: [PATCH 1/2] FEAT: Common token prefix utility func --- data/esdt/common.go | 84 ++++++++++++++++++++++++++++++++++++++++ data/esdt/common_test.go | 62 +++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 data/esdt/common.go create mode 100644 data/esdt/common_test.go diff --git a/data/esdt/common.go b/data/esdt/common.go new file mode 100644 index 00000000..9af4a4fb --- /dev/null +++ b/data/esdt/common.go @@ -0,0 +1,84 @@ +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 +) + +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) +} diff --git a/data/esdt/common_test.go b/data/esdt/common_test.go new file mode 100644 index 00000000..8052c23a --- /dev/null +++ b/data/esdt/common_test.go @@ -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"))) +} From 2f768609cec07168f37392006e2740c413d1c16d Mon Sep 17 00:00:00 2001 From: Marius C Date: Fri, 17 May 2024 13:15:13 +0300 Subject: [PATCH 2/2] FIX: Comm --- data/esdt/common.go | 1 + 1 file changed, 1 insertion(+) diff --git a/data/esdt/common.go b/data/esdt/common.go index 9af4a4fb..d2467ec4 100644 --- a/data/esdt/common.go +++ b/data/esdt/common.go @@ -17,6 +17,7 @@ const ( 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 {