Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common token prefix utility func #299

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we have IsValidToken (as it was before) to understand prefixed tokens as well, not only non-prefixed ones ?

I think it should and it is possible.

It can be done with len of tokenSplit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In mx-chain-go we don't have any function which only does the validation for a simple token, we only have this func(which also does some validations):
ExtractTokenIDAndNonceFromTokenStorageKey.

However, that type of function is not being used in other places, so there was no need to create one and move it to core only to be used in the binary node.

// IsValidPrefixedToken checks if the provided token is valid, and returns if prefix if so
func IsValidPrefixedToken(token string) (string, bool) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

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")))
}
Loading