4
4
"errors"
5
5
"fmt"
6
6
"math/big"
7
+ "net"
7
8
"regexp"
8
9
"strings"
9
10
@@ -147,12 +148,12 @@ func ValidateMnemonic(mnemonic string) error {
147
148
}
148
149
149
150
// IsValidPeerOrSeed checks if each address in a comma-separated list is valid
150
- // It allows empty strings and returns an error if any address is invalid
151
+ // It allows empty strings and returns an error with detailed reasons if any address is invalid
151
152
func IsValidPeerOrSeed (addresses string ) error {
152
153
// Compile the regular expression once
153
- peerRegex , err := regexp .Compile (`^[a-f0-9]{40}@[a-zA-Z0-9\.\-]+(:[0-9]+)? $` )
154
+ nodeIDRegex , err := regexp .Compile (`^[a-f0-9]{40}$` )
154
155
if err != nil {
155
- return fmt .Errorf ("failed to compile regex: %v" , err )
156
+ return fmt .Errorf ("failed to compile nodeID regex: %v" , err )
156
157
}
157
158
158
159
// Split the input string by commas to handle multiple addresses
@@ -169,14 +170,46 @@ func IsValidPeerOrSeed(addresses string) error {
169
170
continue
170
171
}
171
172
172
- if ! peerRegex .MatchString (address ) {
173
- invalidAddresses = append (invalidAddresses , address )
173
+ parts := strings .Split (address , "@" )
174
+ if len (parts ) != 2 {
175
+ invalidAddresses = append (invalidAddresses , fmt .Sprintf ("'%s': must be in format nodeID@ip:port" , address ))
176
+ continue
177
+ }
178
+
179
+ nodeID := parts [0 ]
180
+ peerAddr := parts [1 ]
181
+
182
+ // Validate node ID
183
+ if ! nodeIDRegex .MatchString (nodeID ) {
184
+ invalidAddresses = append (invalidAddresses , fmt .Sprintf ("'%s': invalid node ID (must be 40-character hex string)" , address ))
185
+ continue
186
+ }
187
+
188
+ // Split address into IP and optional port
189
+ host , port , err := net .SplitHostPort (peerAddr )
190
+ if err != nil && ! strings .Contains (err .Error (), "missing port in address" ) {
191
+ invalidAddresses = append (invalidAddresses , fmt .Sprintf ("'%s': invalid address (IP:Port format required)" , address ))
192
+ continue
193
+ }
194
+
195
+ // Validate IP (host part)
196
+ if net .ParseIP (host ) == nil {
197
+ invalidAddresses = append (invalidAddresses , fmt .Sprintf ("'%s': invalid IP address" , address ))
198
+ continue
199
+ }
200
+
201
+ // Validate port if present
202
+ if port != "" {
203
+ if _ , err := fmt .Sscanf (port , "%d" , new (int )); err != nil {
204
+ invalidAddresses = append (invalidAddresses , fmt .Sprintf ("'%s': invalid port" , address ))
205
+ continue
206
+ }
174
207
}
175
208
}
176
209
177
210
if len (invalidAddresses ) > 0 {
178
- // Return an error listing all invalid addresses
179
- return errors .New ("invalid peer/seed addresses: " + strings .Join (invalidAddresses , ", " ))
211
+ // Return an error with detailed messages
212
+ return errors .New ("invalid peer/seed addresses:" + strings .Join (invalidAddresses , "," ))
180
213
}
181
214
182
215
return nil
0 commit comments