github.com/prysmaticlabs/prysm@v1.4.4/validator/accounts/accounts_helper.go (about)

     1  package accounts
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/pkg/errors"
     9  	"github.com/prysmaticlabs/prysm/shared/bls"
    10  	"github.com/urfave/cli/v2"
    11  )
    12  
    13  func filterPublicKeysFromUserInput(
    14  	cliCtx *cli.Context,
    15  	publicKeysFlag *cli.StringFlag,
    16  	validatingPublicKeys [][48]byte,
    17  	selectionPrompt string,
    18  ) ([]bls.PublicKey, error) {
    19  	var filteredPubKeys []bls.PublicKey
    20  	if cliCtx.IsSet(publicKeysFlag.Name) {
    21  		pubKeyStrings := strings.Split(cliCtx.String(publicKeysFlag.Name), ",")
    22  		if len(pubKeyStrings) == 0 {
    23  			return nil, fmt.Errorf(
    24  				"could not parse %s. It must be a string of comma-separated hex strings",
    25  				publicKeysFlag.Name,
    26  			)
    27  		}
    28  		for _, str := range pubKeyStrings {
    29  			pkString := str
    30  			if strings.Contains(pkString, "0x") {
    31  				pkString = pkString[2:]
    32  			}
    33  			pubKeyBytes, err := hex.DecodeString(pkString)
    34  			if err != nil {
    35  				return nil, errors.Wrapf(err, "could not decode string %s as hex", pkString)
    36  			}
    37  			blsPublicKey, err := bls.PublicKeyFromBytes(pubKeyBytes)
    38  			if err != nil {
    39  				return nil, errors.Wrapf(err, "%#x is not a valid BLS public key", pubKeyBytes)
    40  			}
    41  			filteredPubKeys = append(filteredPubKeys, blsPublicKey)
    42  		}
    43  		return filteredPubKeys, nil
    44  	}
    45  	return selectAccounts(selectionPrompt, validatingPublicKeys)
    46  }