-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
65 lines (54 loc) · 1.64 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
package main
import (
"log"
"regexp"
"sort"
)
// A data structure to hold key/value pairs
type Pair struct {
Key interface{}
Value int
}
// A slice of pairs that implements sort.Interface to sort by values
type PairList []Pair
func (p PairList) Len() int { return len(p) }
func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p PairList) Less(i, j int) bool { return p[i].Value < p[j].Value }
// sortMapDescending Sorts any key type high to low, based on the Int value.
func sortMapDescending(presorted map[interface{}]int) PairList {
sortedList := make(PairList, len(presorted))
i := 0
for k, v := range presorted {
sortedList[i] = Pair{k, v}
i++
}
// sorting it highest to lowest
sort.Sort(sort.Reverse(sortedList))
return sortedList
}
// sortMapAscending Sorts any key type low to high, based on the Int value.
func sortMapAscending(presorted map[interface{}]int) PairList {
sortedList := make(PairList, len(presorted))
i := 0
for k, v := range presorted {
sortedList[i] = Pair{k, v}
i++
}
sort.Sort(sortedList)
return sortedList
}
// hasValidHash checks a hash for length and regex of the hex.
// It does _not_ check for existince of a specific hash.
func hasValidHash(object hashable) bool {
return validHash(object.getHash())
}
// validHash checks a hash for length and regex of the hex.
// It does _not_ check for existince of a wallet with this specific hash.
func validHash(hash string) bool {
// fad5e7a92f1c43b1523614336a07f98b894bb80fee06b6763b50ab03b597d5f4
regex, err := regexp.Compile(`[a-f0-9]{64}`)
if err != nil {
log.Fatal("Could not compile regex")
}
return regex.MatchString(hash)
}