-
Notifications
You must be signed in to change notification settings - Fork 117
/
slice.go
94 lines (77 loc) · 2.16 KB
/
slice.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
package utils
import (
"crypto/sha256"
"encoding/binary"
"fmt"
"strings"
"unicode/utf8"
sdk "github.com/cosmos/cosmos-sdk/types"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
"golang.org/x/text/unicode/norm"
)
// IndexOf returns the index of str in the slice; -1 if not found
func IndexOf(strs []string, str string) int {
for i := range strs {
if strs[i] == str {
return i
}
}
return -1
}
// NormalizeString normalizes a string as NFKC
func NormalizeString(str string) string {
return norm.NFKC.String(str)
}
// ValidateString checks if the given string is:
//
// 1. non-empty
// 2. entirely composed of utf8 runes
// 3. normalized as NFKC
// 4. does not contain any forbidden Unicode code points
func ValidateString(str string, forbidden ...string) error {
var f string
if len(forbidden) == 0 {
f = DefaultDelimiter
} else {
f = strings.Join(forbidden, "")
}
return validateString(str, false, f)
}
// ValidateStringAllowEmpty checks if the given string is:
//
// 1. entirely composed of utf8 runes
// 2. normalized as NFKC
// 3. does not contain any forbidden Unicode code points
func ValidateStringAllowEmpty(str string, forbidden string) error {
return validateString(str, true, forbidden)
}
func validateString(str string, canBeEmpty bool, forbidden string) error {
if !canBeEmpty && len(str) == 0 {
return fmt.Errorf("string is empty")
}
if !utf8.ValidString(str) {
return fmt.Errorf("not an utf8 string")
}
if !norm.NFKC.IsNormalString(str) {
return fmt.Errorf("wrong normalization")
}
if len(forbidden) == 0 {
return nil
}
forbidden = norm.NFKC.String(forbidden)
if strings.ContainsAny(str, forbidden) {
return fmt.Errorf("string '%s' must not contain any of '%s'", str, forbidden)
}
return nil
}
// Nonce defines a 32 byte array representing a deterministically-generated nonce
type Nonce [sha256.Size]byte
// GetNonce deterministically calculates a nonce using a hash and gas meter
func GetNonce(hash tmbytes.HexBytes, gasMeter sdk.GasMeter) Nonce {
bz := make([]byte, 16)
if gasMeter != nil {
binary.LittleEndian.PutUint64(bz, uint64(gasMeter.GasConsumed()))
bz = append(bz, hash...)
}
return sha256.Sum256(bz)
}