/
validation.go
101 lines (88 loc) · 3.41 KB
/
validation.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
package types
import (
fmt "fmt"
"regexp"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (
// MinimumSymbolLen is the minimum limitation for the length of the token's symbol
MinimumSymbolLen = 3
// MaximumSymbolLen is the maximum limitation for the length of the token's symbol
MaximumSymbolLen = 64
// MaximumNameLen is the maximum limitation for the length of the token's name
MaximumNameLen = 32
// MinimumDenomLen is the minimum limitation for the length of the token's denom
MinimumDenomLen = 3
// MaximumMinUnitLen is the maximum limitation for the length of the token's denom
MaximumDenomLen = 64
)
const (
ReservedPeg = "peg"
ReservedIBC = "ibc"
ReservedSwap = "swap"
ReservedHTLT = "htlt"
)
var (
keywords = strings.Join([]string{ReservedPeg, ReservedIBC, ReservedSwap, ReservedHTLT}, "|")
regexpKeywordsFmt = fmt.Sprintf("^(%s).*", keywords)
regexpKeyword = regexp.MustCompile(regexpKeywordsFmt).MatchString
regexpSymbolFmt = fmt.Sprintf("^[a-z][a-z0-9]{%d,%d}$", MinimumSymbolLen-1, MaximumSymbolLen-1)
regexpSymbol = regexp.MustCompile(regexpSymbolFmt).MatchString
regexpDenomFmt = fmt.Sprintf("^[a-z][a-z0-9]{%d,%d}$", MinimumDenomLen-1, MaximumDenomLen-1)
regexpDenom = regexp.MustCompile(regexpDenomFmt).MatchString
)
// ValidateToken checks if the given token is valid
func ValidateToken(token FanToken) error {
if len(token.Owner) > 0 {
if _, err := sdk.AccAddressFromBech32(token.Owner); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid owner address (%s)", err)
}
}
if err := ValidateName(token.Name); err != nil {
return err
}
if err := ValidateSymbol(token.GetSymbol()); err != nil {
return err
}
if err := ValidateDenom(token.GetDenom()); err != nil {
return err
}
return nil
}
// ValidateDenom checks if the given denom is valid
func ValidateDenom(denom string) error {
if !regexpDenom(denom) {
return sdkerrors.Wrapf(ErrInvalidDenom, "invalid denom: %s, only accepts english lowercase letters and numbers, length [%d, %d], and begin with an english letter, regexp: %s", denom, MinimumDenomLen, MaximumDenomLen, regexpDenomFmt)
}
return ValidateKeywords(denom)
}
// ValidateName verifies whether the given name is legal
func ValidateName(name string) error {
if len(name) == 0 || len(name) > MaximumNameLen {
return sdkerrors.Wrapf(ErrInvalidName, "invalid token name %s, only accepts length (0, %d]", name, MaximumNameLen)
}
return nil
}
// ValidateSymbol checks if the given symbol is valid
func ValidateSymbol(symbol string) error {
if !regexpSymbol(symbol) {
return sdkerrors.Wrapf(ErrInvalidSymbol, "invalid symbol: %s, only accepts english lowercase letters and numbers, length [%d, %d], and begin with an english letter, regexp: %s", symbol, MinimumSymbolLen, MaximumSymbolLen, regexpSymbolFmt)
}
return ValidateKeywords(symbol)
}
// ValidateKeywords checks if the given symbol begins with `TokenKeywords`
func ValidateKeywords(denom string) error {
if regexpKeyword(denom) {
return sdkerrors.Wrapf(ErrInvalidSymbol, "invalid token: %s, can not begin with keyword: (%s)", denom, keywords)
}
return nil
}
// ValidateAmount checks if the given symbol begins with `TokenKeywords`
func ValidateAmount(amount sdk.Int) error {
if amount.IsZero() {
return sdkerrors.Wrapf(ErrInvalidMaxSupply, "invalid token amount %d, only accepts positive amount", amount)
}
return nil
}