/
utils.go
97 lines (82 loc) · 2.88 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
package types
import (
"fmt"
transfertypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"
"regexp"
"strings"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
const (
// (?m)^(\d+) remove leading numbers
reLeadingNumbers = `(?m)^(\d+)`
// ^[^A-Za-z] forces first chars to be letters
// [^a-zA-Z0-9/-] deletes special characters
reDnmString = `^[^A-Za-z]|[^a-zA-Z0-9/-]`
)
func removeLeadingNumbers(str string) string {
re := regexp.MustCompile(reLeadingNumbers)
return re.ReplaceAllString(str, "")
}
func removeSpecialChars(str string) string {
re := regexp.MustCompile(reDnmString)
return re.ReplaceAllString(str, "")
}
// recursively remove every invalid prefix
func removeInvalidPrefixes(str string) string {
if strings.HasPrefix(str, "ibc/") {
return removeInvalidPrefixes(str[4:])
}
if strings.HasPrefix(str, "erc20/") {
return removeInvalidPrefixes(str[6:])
}
return str
}
// SanitizeERC20Name enforces 128 max string length, deletes leading numbers
// removes special characters (except /) and spaces from the ERC20 name
func SanitizeERC20Name(name string) string {
name = removeLeadingNumbers(name)
name = removeSpecialChars(name)
if len(name) > 128 {
name = name[:128]
}
name = removeInvalidPrefixes(name)
return name
}
// EqualMetadata checks if all the fields of the provided coin metadata are equal.
func EqualMetadata(a, b banktypes.Metadata) error {
if a.Base == b.Base && a.Description == b.Description && a.Display == b.Display && a.Name == b.Name && a.Symbol == b.Symbol {
if len(a.DenomUnits) != len(b.DenomUnits) {
return fmt.Errorf("metadata provided has different denom units from stored, %d ≠ %d", len(a.DenomUnits), len(b.DenomUnits))
}
for i, v := range a.DenomUnits {
if (v.Exponent != b.DenomUnits[i].Exponent) || (v.Denom != b.DenomUnits[i].Denom) || !EqualStringSlice(v.Aliases, b.DenomUnits[i].Aliases) {
return fmt.Errorf("metadata provided has different denom unit from stored, %s ≠ %s", a.DenomUnits[i], b.DenomUnits[i])
}
}
return nil
}
return fmt.Errorf("metadata provided is different from stored")
}
// EqualStringSlice checks if two string slices are equal.
func EqualStringSlice(aliasesA, aliasesB []string) bool {
if len(aliasesA) != len(aliasesB) {
return false
}
for i := 0; i < len(aliasesA); i++ {
if aliasesA[i] != aliasesB[i] {
return false
}
}
return true
}
// IBCDenom pack denom infos to voucherDenom
func IBCDenom(port, channel, denom string) (string, error) {
// since SendPacket did not prefix the denomination, we must prefix denomination here
sourcePrefix := transfertypes.GetDenomPrefix(port, channel)
// NOTE: sourcePrefix contains the trailing "/"
prefixedDenom := sourcePrefix + denom
// construct the denomination trace from the full raw denomination
denomTrace := transfertypes.ParseDenomTrace(prefixedDenom)
voucherDenom := denomTrace.IBCDenom()
return voucherDenom, nil
}