-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
121 lines (102 loc) · 2.98 KB
/
utils.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 datafield
import (
"bytes"
"encoding/hex"
"fmt"
"math/big"
"unicode"
"github.com/kalyan3104/k-core/core"
)
const (
dctIdentifierSeparator = "-"
dctRandomSequenceLength = 6
)
func getAllBuiltInFunctions() []string {
return []string{
core.BuiltInFunctionClaimDeveloperRewards,
core.BuiltInFunctionChangeOwnerAddress,
core.BuiltInFunctionSetUserName,
core.BuiltInFunctionSaveKeyValue,
core.BuiltInFunctionDCTTransfer,
core.BuiltInFunctionDCTBurn,
core.BuiltInFunctionDCTFreeze,
core.BuiltInFunctionDCTUnFreeze,
core.BuiltInFunctionDCTWipe,
core.BuiltInFunctionDCTPause,
core.BuiltInFunctionDCTUnPause,
core.BuiltInFunctionSetDCTRole,
core.BuiltInFunctionUnSetDCTRole,
core.BuiltInFunctionDCTSetLimitedTransfer,
core.BuiltInFunctionDCTUnSetLimitedTransfer,
core.BuiltInFunctionDCTLocalMint,
core.BuiltInFunctionDCTLocalBurn,
core.BuiltInFunctionDCTNFTTransfer,
core.BuiltInFunctionDCTNFTCreate,
core.BuiltInFunctionDCTNFTAddQuantity,
core.BuiltInFunctionDCTNFTCreateRoleTransfer,
core.BuiltInFunctionDCTNFTBurn,
core.BuiltInFunctionDCTNFTAddURI,
core.BuiltInFunctionDCTNFTUpdateAttributes,
core.BuiltInFunctionMultiDCTNFTTransfer,
core.DCTRoleLocalMint,
core.DCTRoleLocalBurn,
core.DCTRoleNFTCreate,
core.DCTRoleNFTCreateMultiShard,
core.DCTRoleNFTAddQuantity,
core.DCTRoleNFTBurn,
core.DCTRoleNFTAddURI,
core.DCTRoleNFTUpdateAttributes,
core.DCTRoleTransfer,
}
}
func isBuiltInFunction(builtInFunctionsList []string, function string) bool {
for _, builtInFunction := range builtInFunctionsList {
if builtInFunction == function {
return true
}
}
return false
}
// EncodeBytesSlice will encode the provided bytes slice with a provided function
func EncodeBytesSlice(encodeFunc func(b []byte) string, rcvs [][]byte) []string {
if encodeFunc == nil {
return nil
}
encodedSlice := make([]string, 0, len(rcvs))
for _, rcv := range rcvs {
encodedSlice = append(encodedSlice, encodeFunc(rcv))
}
return encodedSlice
}
func computeTokenIdentifier(token string, nonce uint64) string {
if token == "" || nonce == 0 {
return ""
}
nonceBig := big.NewInt(0).SetUint64(nonce)
hexEncodedNonce := hex.EncodeToString(nonceBig.Bytes())
return fmt.Sprintf("%s-%s", token, hexEncodedNonce)
}
func extractTokenAndNonce(arg []byte) (string, uint64) {
argsSplit := bytes.Split(arg, []byte(dctIdentifierSeparator))
if len(argsSplit) < 2 {
return string(arg), 0
}
if len(argsSplit[1]) <= dctRandomSequenceLength {
return string(arg), 0
}
identifier := []byte(fmt.Sprintf("%s-%s", argsSplit[0], argsSplit[1][:dctRandomSequenceLength]))
nonce := big.NewInt(0).SetBytes(argsSplit[1][dctRandomSequenceLength:])
return string(identifier), nonce.Uint64()
}
func isEmptyAddr(addrLength int, address []byte) bool {
emptyAddr := make([]byte, addrLength)
return bytes.Equal(address, emptyAddr)
}
func isASCIIString(input string) bool {
for i := 0; i < len(input); i++ {
if input[i] > unicode.MaxASCII {
return false
}
}
return true
}