/
eei_helpers.go
121 lines (102 loc) · 2.76 KB
/
eei_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package elrondapi
const esdtRoleLocalMint = "ESDTRoleLocalMint"
const esdtRoleLocalBurn = "ESDTRoleLocalBurn"
const esdtRoleNFTCreate = "ESDTRoleNFTCreate"
const esdtRoleNFTAddQuantity = "ESDTRoleNFTAddQuantity"
const esdtRoleNFTBurn = "ESDTRoleNFTBurn"
const tickerMinLength = 3
const tickerMaxLength = 10
const additionalRandomCharsLength = 6
const identifierMinLength = tickerMinLength + additionalRandomCharsLength + 1
const identifierMaxLength = tickerMaxLength + additionalRandomCharsLength + 1
const (
RoleMint = 1 << iota
RoleBurn
RoleNFTCreate
RoleNFTAddQuantity
RoleNFTBurn
)
func roleFromByteArray(bytes []byte) int64 {
stringValue := string(bytes)
switch stringValue {
case esdtRoleLocalMint:
return RoleMint
case esdtRoleLocalBurn:
return RoleBurn
case esdtRoleNFTCreate:
return RoleNFTCreate
case esdtRoleNFTAddQuantity:
return RoleNFTAddQuantity
case esdtRoleNFTBurn:
return RoleNFTBurn
default:
return 0
}
}
func getESDTRoles(data_buffer []byte) int64 {
result := int64(0)
current_index := 0
value_len := len(data_buffer)
for current_index < value_len {
// first character before each role is a \n, so we skip it
current_index += 1
// next is the length of the role as string
role_len := int(data_buffer[current_index])
current_index += 1
// next is role's ASCII string representation
end_index := current_index + role_len
role_name := data_buffer[current_index:end_index]
current_index = end_index
result |= roleFromByteArray(role_name)
}
return result
}
// ValidateToken - validates the token ID
func ValidateToken(tokenID []byte) bool {
tokenIDLen := len(tokenID)
if tokenIDLen < identifierMinLength || tokenIDLen > identifierMaxLength {
return false
}
tickerLen := tokenIDLen - additionalRandomCharsLength
if !isTickerValid(tokenID[0 : tickerLen-1]) {
return false
}
// dash char between the random chars and the ticker
if tokenID[tickerLen-1] != '-' {
return false
}
if !randomCharsAreValid(tokenID[tickerLen:tokenIDLen]) {
return false
}
return true
}
// ticker must be all uppercase alphanumeric
func isTickerValid(tickerName []byte) bool {
if len(tickerName) < tickerMinLength || len(tickerName) > tickerMaxLength {
return false
}
for _, ch := range tickerName {
isBigCharacter := ch >= 'A' && ch <= 'Z'
isNumber := ch >= '0' && ch <= '9'
isReadable := isBigCharacter || isNumber
if !isReadable {
return false
}
}
return true
}
// random chars are alphanumeric lowercase
func randomCharsAreValid(chars []byte) bool {
if len(chars) != additionalRandomCharsLength {
return false
}
for _, ch := range chars {
isSmallCharacter := ch >= 'a' && ch <= 'f'
isNumber := ch >= '0' && ch <= '9'
isReadable := isSmallCharacter || isNumber
if !isReadable {
return false
}
}
return true
}