-
Notifications
You must be signed in to change notification settings - Fork 199
/
tokensParser.go
62 lines (46 loc) · 1.79 KB
/
tokensParser.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
package common
import (
"math/big"
"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
)
// TODO: move this to core
// ExtractTokenIDAndNonceFromTokenKey will parse the token's storage key and extract the identifier and the nonce
func ExtractTokenIDAndNonceFromTokenStorageKey(tokenKey []byte) ([]byte, uint64) {
// ALC-1q2w3e for fungible
// ALC-2w3e4rX for non fungible
token := string(tokenKey)
// filtering by the index of first occurrence is faster than splitting
indexOfFirstHyphen := strings.Index(token, separatorChar)
if indexOfFirstHyphen < 0 {
return tokenKey, 0
}
tokenTicker := token[:indexOfFirstHyphen]
randomSequencePlusNonce := token[indexOfFirstHyphen+1:]
tokenTickerLen := len(tokenTicker)
areTickerAndRandomSequenceInvalid := tokenTickerLen == 0 ||
tokenTickerLen < minLengthForTickerName ||
tokenTickerLen > maxLengthForTickerName ||
len(randomSequencePlusNonce) == 0
if areTickerAndRandomSequenceInvalid {
return tokenKey, 0
}
if len(randomSequencePlusNonce) < esdtTickerNumRandChars+1 {
return tokenKey, 0
}
// ALC-1q2w3eX - X is the nonce
nonceStr := randomSequencePlusNonce[esdtTickerNumRandChars:]
nonceBigInt := big.NewInt(0).SetBytes([]byte(nonceStr))
numCharsSinceNonce := len(token) - len(nonceStr)
tokenID := token[:numCharsSinceNonce]
return []byte(tokenID), nonceBigInt.Uint64()
}