-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
109 lines (86 loc) · 3.14 KB
/
common.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
package vmcommon
import (
"math/big"
"github.com/bhagyaraj1208117/andes-abc-1/core"
)
const tickerMinLength = 3
const tickerMaxLength = 10
const additionalRandomCharsLength = 6
const identifierMinLength = tickerMinLength + additionalRandomCharsLength + 1
const identifierMaxLength = tickerMaxLength + additionalRandomCharsLength + 1
// DCTDeleteMetadata represents the defined built in function name for dct delete metadata
const DCTDeleteMetadata = "DCTDeleteMetadata"
// DCTAddMetadata represents the defined built in function name for dct add metadata
const DCTAddMetadata = "DCTAddMetadata"
// BuiltInFunctionDCTSetBurnRoleForAll represents the defined built in function name for dct set burn role for all
const BuiltInFunctionDCTSetBurnRoleForAll = "DCTSetBurnRoleForAll"
// BuiltInFunctionDCTUnSetBurnRoleForAll represents the defined built in function name for dct unset burn role for all
const BuiltInFunctionDCTUnSetBurnRoleForAll = "DCTUnSetBurnRoleForAll"
// BuiltInFunctionDCTTransferRoleAddAddress represents the defined built in function name for dct transfer role add address
const BuiltInFunctionDCTTransferRoleAddAddress = "DCTTransferRoleAddAddress"
// BuiltInFunctionDCTTransferRoleDeleteAddress represents the defined built in function name for transfer role delete address
const BuiltInFunctionDCTTransferRoleDeleteAddress = "DCTTransferRoleDeleteAddress"
// DCTRoleBurnForAll represents the role for burn for all
const DCTRoleBurnForAll = "DCTRoleBurnForAll"
// 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
}
// ZeroValueIfNil returns 0 if the input is nil, otherwise returns the input
func ZeroValueIfNil(value *big.Int) *big.Int {
if value == nil {
return big.NewInt(0)
}
return value
}
// ArgsMigrateDataTrieLeaves is the argument structure for the MigrateDataTrieLeaves function
type ArgsMigrateDataTrieLeaves struct {
OldVersion core.TrieNodeVersion
NewVersion core.TrieNodeVersion
TrieMigrator DataTrieMigrator
}