-
Notifications
You must be signed in to change notification settings - Fork 6
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing comment There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} |
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"))) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.