[staking] Verify BLS key input

fix-bls-key-not-found
Daniel Van Der Maden 4 years ago
parent ec0a498945
commit ed1b42c454
No known key found for this signature in database
GPG Key ID: AEA215877D53EE19
  1. 35
      cmd/subcommands/staking.go

@ -1,7 +1,6 @@
package cmd package cmd
import ( import (
"errors"
"fmt" "fmt"
"math/big" "math/big"
"strconv" "strconv"
@ -27,6 +26,7 @@ import (
"github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/shard"
"github.com/harmony-one/harmony/staking/effective" "github.com/harmony-one/harmony/staking/effective"
staking "github.com/harmony-one/harmony/staking/types" staking "github.com/harmony-one/harmony/staking/types"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -284,10 +284,31 @@ func ensureLength(d staking.Description) (staking.Description, error) {
if len(d.Details) > MaxDetailsLength { if len(d.Details) > MaxDetailsLength {
return d, errInvalidDescFieldDetails return d, errInvalidDescFieldDetails
} }
return d, nil return d, nil
} }
func assertValidOptionStringInputForNonNumbers(input string) error {
if input != "" && strings.HasPrefix(input, "-") {
return fmt.Errorf("invalid or missing option")
}
return nil
}
func validateBlsKeyInput(cmd *cobra.Command, args []string) error {
if err := assertValidOptionStringInputForNonNumbers(slotKeyToAdd); err != nil {
return errors.WithMessage(err, "BLS key to add error")
}
if err := assertValidOptionStringInputForNonNumbers(slotKeyToRemove); err != nil {
return errors.WithMessage(err, "BLS key to remove error")
}
for _, str := range stakingBlsPubKeys {
if err := assertValidOptionStringInputForNonNumbers(str); err != nil {
return errors.WithMessage(err, "BLS key to create validator error")
}
}
return nil
}
func stakingSubCommands() []*cobra.Command { func stakingSubCommands() []*cobra.Command {
subCmdNewValidator := &cobra.Command{ subCmdNewValidator := &cobra.Command{
@ -297,6 +318,7 @@ func stakingSubCommands() []*cobra.Command {
Long: ` Long: `
Create a new validator" Create a new validator"
`, `,
PreRunE: validateBlsKeyInput,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
networkHandler, err := handlerForShard(0, node) networkHandler, err := handlerForShard(0, node)
if err != nil { if err != nil {
@ -455,10 +477,11 @@ Create a new validator"
} }
subCmdEditValidator := &cobra.Command{ subCmdEditValidator := &cobra.Command{
Use: "edit-validator", Use: "edit-validator",
Short: "edit a validator", Short: "edit a validator",
Long: "Edit an existing validator", Long: "Edit an existing validator",
Args: cobra.ExactArgs(0), Args: cobra.ExactArgs(0),
PreRunE: validateBlsKeyInput,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
networkHandler, err := handlerForShard(shard.BeaconChainShardID, node) networkHandler, err := handlerForShard(shard.BeaconChainShardID, node)
if err != nil { if err != nil {

Loading…
Cancel
Save