-
Notifications
You must be signed in to change notification settings - Fork 565
/
version.go
67 lines (53 loc) · 2.26 KB
/
version.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
package types
import (
"fmt"
"regexp"
"strings"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// DefaultMaxAddrLength defines the default maximum character length used in validation of addresses
var DefaultMaxAddrLength = 128
// IsValidAddr defines a regular expression to check if the provided string consists of
// strictly alphanumeric characters
var IsValidAddr = regexp.MustCompile("^[a-zA-Z0-9]*$").MatchString
// NewVersion returns a complete version string in the format: VersionPrefix + Delimter + AccAddress
func NewAppVersion(versionPrefix, accAddr string) string {
return fmt.Sprint(versionPrefix, Delimiter, accAddr)
}
// ParseAddressFromVersion attempts to extract the associated account address from the provided version string
func ParseAddressFromVersion(version string) (string, error) {
s := strings.Split(version, Delimiter)
if len(s) != 2 {
return "", sdkerrors.Wrap(ErrInvalidVersion, "failed to parse version")
}
return s[1], nil
}
// ValidateVersion performs basic validation of the provided ics27 version string.
// An ics27 version string may include an optional account address as per [TODO: Add spec when available]
// ValidateVersion first attempts to split the version string using the standard delimiter, then asserts a supported
// version prefix is included, followed by additional checks which enforce constraints on the account address.
func ValidateVersion(version string) error {
s := strings.Split(version, Delimiter)
if len(s) != 2 {
return sdkerrors.Wrapf(ErrInvalidVersion, "expected format <app-version%saccount-address>, got %s", Delimiter, version)
}
if s[0] != VersionPrefix {
return sdkerrors.Wrapf(ErrInvalidVersion, "expected %s, got %s", VersionPrefix, s[0])
}
if err := ValidateAccountAddress(s[1]); err != nil {
return err
}
return nil
}
// ValidateAccountAddress performs basic validation of interchain account addresses, enforcing constraints
// on address length and character set
func ValidateAccountAddress(addr string) error {
if !IsValidAddr(addr) || len(addr) == 0 || len(addr) > DefaultMaxAddrLength {
return sdkerrors.Wrapf(
ErrInvalidAccountAddress,
"address must contain strictly alphanumeric characters, not exceeding %d characters in length",
DefaultMaxAddrLength,
)
}
return nil
}