github.com/prysmaticlabs/prysm@v1.4.4/validator/slashing-protection/local/standard-protection-format/helpers.go (about)

     1  package interchangeformat
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/k0kubun/go-ansi"
    10  	types "github.com/prysmaticlabs/eth2-types"
    11  	"github.com/schollz/progressbar/v3"
    12  )
    13  
    14  func initializeProgressBar(numItems int, msg string) *progressbar.ProgressBar {
    15  	return progressbar.NewOptions(
    16  		numItems,
    17  		progressbar.OptionFullWidth(),
    18  		progressbar.OptionSetWriter(ansi.NewAnsiStdout()),
    19  		progressbar.OptionEnableColorCodes(true),
    20  		progressbar.OptionSetTheme(progressbar.Theme{
    21  			Saucer:        "[green]=[reset]",
    22  			SaucerHead:    "[green]>[reset]",
    23  			SaucerPadding: " ",
    24  			BarStart:      "[",
    25  			BarEnd:        "]",
    26  		}),
    27  		progressbar.OptionOnCompletion(func() { fmt.Println() }),
    28  		progressbar.OptionSetDescription(msg),
    29  	)
    30  }
    31  
    32  // Uint64FromString converts a string into a uint64 representation.
    33  func Uint64FromString(str string) (uint64, error) {
    34  	return strconv.ParseUint(str, 10, 64)
    35  }
    36  
    37  // EpochFromString converts a string into Epoch.
    38  func EpochFromString(str string) (types.Epoch, error) {
    39  	e, err := strconv.ParseUint(str, 10, 64)
    40  	if err != nil {
    41  		return types.Epoch(e), err
    42  	}
    43  	return types.Epoch(e), nil
    44  }
    45  
    46  // SlotFromString converts a string into Slot.
    47  func SlotFromString(str string) (types.Slot, error) {
    48  	s, err := strconv.ParseUint(str, 10, 64)
    49  	if err != nil {
    50  		return types.Slot(s), err
    51  	}
    52  	return types.Slot(s), nil
    53  }
    54  
    55  // PubKeyFromHex takes in a hex string, verifies its length as 48 bytes, and converts that representation.
    56  func PubKeyFromHex(str string) ([48]byte, error) {
    57  	pubKeyBytes, err := hex.DecodeString(strings.TrimPrefix(str, "0x"))
    58  	if err != nil {
    59  		return [48]byte{}, err
    60  	}
    61  	if len(pubKeyBytes) != 48 {
    62  		return [48]byte{}, fmt.Errorf("public key is not correct, 48-byte length: %s", str)
    63  	}
    64  	var pk [48]byte
    65  	copy(pk[:], pubKeyBytes[:48])
    66  	return pk, nil
    67  }
    68  
    69  // RootFromHex takes in a hex string, verifies its length as 32 bytes, and converts that representation.
    70  func RootFromHex(str string) ([32]byte, error) {
    71  	rootHexBytes, err := hex.DecodeString(strings.TrimPrefix(str, "0x"))
    72  	if err != nil {
    73  		return [32]byte{}, err
    74  	}
    75  	if len(rootHexBytes) != 32 {
    76  		return [32]byte{}, fmt.Errorf("wrong root length, 32-byte length: %s", str)
    77  	}
    78  	var root [32]byte
    79  	copy(root[:], rootHexBytes[:32])
    80  	return root, nil
    81  }
    82  
    83  func rootToHexString(root []byte) (string, error) {
    84  	// Nil signing roots are allowed in EIP-3076.
    85  	if len(root) == 0 {
    86  		return "", nil
    87  	}
    88  	if len(root) != 32 {
    89  		return "", fmt.Errorf("wanted length 32, received %d", len(root))
    90  	}
    91  	return fmt.Sprintf("%#x", root), nil
    92  }
    93  
    94  func pubKeyToHexString(pubKey []byte) (string, error) {
    95  	if len(pubKey) != 48 {
    96  		return "", fmt.Errorf("wanted length 48, received %d", len(pubKey))
    97  	}
    98  	return fmt.Sprintf("%#x", pubKey), nil
    99  }