-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from multiversx/merge-rc-1-7-next-1-into-feat…
…-sovereign Merge rc 1 7 next 1 into feat sovereign
- Loading branch information
Showing
2 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))) | ||
} |