validate.gno
2.93 Kb · 82 lines
1package host
2
3import (
4 "errors"
5 "regexp"
6 "strings"
7
8 "gno.land/p/nt/ufmt/v0"
9)
10
11// DefaultMaxCharacterLength defines the default maximum character length used
12// in validation of identifiers including the client, connection, port and
13// channel identifiers.
14//
15// NOTE: this restriction is specific to this golang implementation of IBC. If
16// your use case demands a higher limit, please open an issue and we will consider
17// adjusting this restriction.
18const DefaultMaxCharacterLength = 64
19
20// DefaultMaxPortCharacterLength defines the default maximum character length used
21// in validation of port identifiers.
22var DefaultMaxPortCharacterLength = 128
23
24// IsValidID defines regular expression to check if the string consist of
25// characters in one of the following categories only:
26// - Alphanumeric
27// - `.`, `_`, `+`, `-`, `#`
28// - `[`, `]`, `<`, `>`
29var IsValidID = regexp.MustCompile(`^[a-zA-Z0-9\.\_\+\-\#\[\]\<\>]+$`).MatchString
30
31// ICS 024 Identifier and Path Validation Implementation
32//
33// This file defines ValidateFn to validate identifier and path strings
34// The spec for ICS 024 can be located here:
35// https://github.com/cosmos/ibc/tree/master/spec/core/ics-024-host-requirements
36
37// ValidateFn function type to validate path and identifier bytestrings
38type ValidateFn func(string) error
39
40func defaultIdentifierValidator(id string, minLength, maxLength int) error {
41 if strings.TrimSpace(id) == "" {
42 return errors.New("identifier cannot be blank")
43 }
44 // valid id MUST NOT contain "/" separator
45 if strings.Contains(id, "/") {
46 return ufmt.Errorf("identifier %s cannot contain separator '/'", id)
47 }
48 // valid id must fit the length requirements
49 if len(id) < minLength || len(id) > maxLength {
50 return ufmt.Errorf("identifier %s has invalid length: %d, must be between %d-%d characters", id, len(id), minLength, maxLength)
51 }
52 // valid id must contain only alphanumeric characters and some allowed symbols.
53 if !IsValidID(id) {
54 return ufmt.Errorf(
55 "identifier %s must contain only alphanumeric or the following characters: '.', '_', '+', '-', '#', '[', ']', '<', '>'",
56 id,
57 )
58 }
59 return nil
60}
61
62// ClientIdentifierValidator is the default validator function for Client identifiers.
63// A valid Identifier must be between 4-64 characters and only contain alphanumeric and some allowed
64// special characters (see IsValidID).
65func ClientIdentifierValidator(id string) error {
66 err := defaultIdentifierValidator(id, 4, DefaultMaxCharacterLength)
67 if err != nil {
68 return ufmt.Errorf("validate client identifier: %v", err)
69 }
70 return nil
71}
72
73// PortIdentifierValidator is the default validator function for Port identifiers.
74// A valid Identifier must be between 2-64 characters and only contain alphanumeric and some allowed
75// special characters (see IsValidID).
76func PortIdentifierValidator(id string) error {
77 err := defaultIdentifierValidator(id, 2, DefaultMaxPortCharacterLength)
78 if err != nil {
79 return ufmt.Errorf("validate port identifier: %v", err)
80 }
81 return nil
82}