Skip to content

Commit f91a309

Browse files
committed
feat: validate seeds and peers
1 parent 810ccbc commit f91a309

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

models/weaveinit/run_l1_node.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,13 @@ type MinGasPriceInput struct {
301301
}
302302

303303
func NewMinGasPriceInput(state *RunL1NodeState) *MinGasPriceInput {
304-
return &MinGasPriceInput{
304+
model := &MinGasPriceInput{
305305
TextInput: utils.NewTextInput(),
306306
state: state,
307307
question: "Please specify min-gas-price (uinit)",
308308
}
309+
model.WithValidatorFn(utils.ValidateDecCoin)
310+
return model
309311
}
310312

311313
func (m *MinGasPriceInput) GetQuestion() string {
@@ -401,11 +403,13 @@ type SeedsInput struct {
401403
}
402404

403405
func NewSeedsInput(state *RunL1NodeState) *SeedsInput {
404-
return &SeedsInput{
406+
model := &SeedsInput{
405407
TextInput: utils.NewTextInput(),
406408
state: state,
407409
question: "Please specify the seeds",
408410
}
411+
model.WithValidatorFn(utils.IsValidPeerOrSeed)
412+
return model
409413
}
410414

411415
func (m *SeedsInput) GetQuestion() string {
@@ -438,11 +442,13 @@ type PersistentPeersInput struct {
438442
}
439443

440444
func NewPersistentPeersInput(state *RunL1NodeState) *PersistentPeersInput {
441-
return &PersistentPeersInput{
445+
model := &PersistentPeersInput{
442446
TextInput: utils.NewTextInput(),
443447
state: state,
444448
question: "Please specify the persistent_peers",
445449
}
450+
model.WithValidatorFn(utils.IsValidPeerOrSeed)
451+
return model
446452
}
447453

448454
func (m *PersistentPeersInput) GetQuestion() string {

utils/validate.go

+36
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,39 @@ func ValidateMnemonic(mnemonic string) error {
145145
}
146146
return nil
147147
}
148+
149+
// 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+
func IsValidPeerOrSeed(addresses string) error {
152+
// Compile the regular expression once
153+
peerRegex, err := regexp.Compile(`^[a-f0-9]{40}@[a-zA-Z0-9\.\-]+(:[0-9]+)?$`)
154+
if err != nil {
155+
return fmt.Errorf("failed to compile regex: %v", err)
156+
}
157+
158+
// Split the input string by commas to handle multiple addresses
159+
addressList := strings.Split(addresses, ",")
160+
161+
var invalidAddresses []string
162+
163+
// Iterate over each address and validate
164+
for _, address := range addressList {
165+
address = strings.TrimSpace(address) // Remove any leading/trailing spaces
166+
167+
// Skip empty strings, as they're considered valid
168+
if address == "" {
169+
continue
170+
}
171+
172+
if !peerRegex.MatchString(address) {
173+
invalidAddresses = append(invalidAddresses, address)
174+
}
175+
}
176+
177+
if len(invalidAddresses) > 0 {
178+
// Return an error listing all invalid addresses
179+
return errors.New("invalid peer/seed addresses: " + strings.Join(invalidAddresses, ", "))
180+
}
181+
182+
return nil
183+
}

0 commit comments

Comments
 (0)