@@ -40,35 +40,6 @@ func init() {
4040 }
4141}
4242
43- // basicValidation performs basic validation on the given identifier
44- func basicValidation (id string , minLength , maxLength int ) error {
45- if strings .TrimSpace (id ) == "" {
46- return coreTypes .ErrIBCInvalidID (id , "cannot be blank" )
47- }
48-
49- if len (id ) < minLength || len (id ) > maxLength {
50- return coreTypes .ErrIBCInvalidID (id , fmt .Sprintf ("length must be between %d and %d" , minLength , maxLength ))
51- }
52-
53- if ! strings .HasPrefix (id , identifierPrefix ) {
54- return coreTypes .ErrIBCInvalidID (id , fmt .Sprintf ("must start with '%s'" , identifierPrefix ))
55- }
56-
57- for _ , c := range invalidIdChars {
58- if _ , ok := invalidIdMap [c ]; ok {
59- return coreTypes .ErrIBCInvalidID (id , fmt .Sprintf ("cannot contain '%s'" , string (c )))
60- }
61- }
62-
63- for _ , c := range id {
64- if _ , ok := validIdMap [c ]; ! ok {
65- return coreTypes .ErrIBCInvalidID (id , fmt .Sprintf ("contains invalid character '%c'" , c ))
66- }
67- }
68-
69- return nil
70- }
71-
7243// ValidateClientID validates the client identifier string
7344func ValidateClientID (id string ) error {
7445 return basicValidation (id , minClientIdLength , defaultMaxIdLength )
@@ -89,6 +60,26 @@ func ValidatePortID(id string) error {
8960 return basicValidation (id , minPortIdLength , maxPortIdLength )
9061}
9162
63+ // GenerateClientIdentifier generates a new client identifier
64+ func GenerateClientIdentifier () string {
65+ return generateNewIdentifier (minClientIdLength , defaultMaxIdLength )
66+ }
67+
68+ // GenerateConnectionIdentifier generates a new connection identifier
69+ func GenerateConnectionIdentifier () string {
70+ return generateNewIdentifier (minConnectionIdLength , defaultMaxIdLength )
71+ }
72+
73+ // GenerateChannelIdentifier generates a new channel identifier
74+ func GenerateChannelIdentifier () string {
75+ return generateNewIdentifier (minChannelIdLength , defaultMaxIdLength )
76+ }
77+
78+ // GeneratePortIdentifier generates a new port identifier
79+ func GeneratePortIdentifier () string {
80+ return generateNewIdentifier (minPortIdLength , maxPortIdLength )
81+ }
82+
9283// generateNewIdentifier generates a new identifier in the given range
9384func generateNewIdentifier (min , max int ) string {
9485 return generateNewIdentifierWithSeed (min , max , time .Now ().UnixNano ())
@@ -111,22 +102,31 @@ func generateNewIdentifierWithSeed(min, max int, seed int64) string {
111102 return identifierPrefix + string (b )
112103}
113104
114- // GenerateClientIdentifier generates a new client identifier
115- func GenerateClientIdentifier () string {
116- return generateNewIdentifier (minClientIdLength , defaultMaxIdLength )
117- }
105+ // basicValidation performs basic validation on the given identifier
106+ func basicValidation (id string , minLength , maxLength int ) error {
107+ if strings .TrimSpace (id ) == "" {
108+ return coreTypes .ErrIBCInvalidID (id , "cannot be blank" )
109+ }
118110
119- // GenerateConnectionIdentifier generates a new connection identifier
120- func GenerateConnectionIdentifier () string {
121- return generateNewIdentifier (minConnectionIdLength , defaultMaxIdLength )
122- }
111+ if len (id ) < minLength || len (id ) > maxLength {
112+ return coreTypes .ErrIBCInvalidID (id , fmt .Sprintf ("length must be between %d and %d" , minLength , maxLength ))
113+ }
123114
124- // GenerateChannelIdentifier generates a new channel identifier
125- func GenerateChannelIdentifier () string {
126- return generateNewIdentifier (minChannelIdLength , defaultMaxIdLength )
127- }
115+ if ! strings .HasPrefix (id , identifierPrefix ) {
116+ return coreTypes .ErrIBCInvalidID (id , fmt .Sprintf ("must start with '%s'" , identifierPrefix ))
117+ }
128118
129- // GeneratePortIdentifier generates a new port identifier
130- func GeneratePortIdentifier () string {
131- return generateNewIdentifier (minPortIdLength , maxPortIdLength )
119+ for _ , c := range id {
120+ if _ , ok := invalidIdMap [c ]; ok {
121+ return coreTypes .ErrIBCInvalidID (id , fmt .Sprintf ("cannot contain '%s'" , string (c )))
122+ }
123+ }
124+
125+ for _ , c := range id {
126+ if _ , ok := validIdMap [c ]; ! ok {
127+ return coreTypes .ErrIBCInvalidID (id , fmt .Sprintf ("contains invalid character '%c'" , c ))
128+ }
129+ }
130+
131+ return nil
132132}
0 commit comments