mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-11 04:00:05 +00:00
a069738c20
* update shared/params * update eth2-types deps * update protobufs * update shared/* * fix testutil/state * update beacon-chain/state * update beacon-chain/db * update tests * fix test * update beacon-chain/core * update beacon-chain/blockchain * update beacon-chain/cache * beacon-chain/forkchoice * update beacon-chain/operations * update beacon-chain/p2p * update beacon-chain/rpc * update sync/initial-sync * update deps * update deps * go fmt * update beacon-chain/sync * update endtoend/ * bazel build //beacon-chain - works w/o issues * update slasher code * udpate tools/ * update validator/ * update fastssz * fix build * fix test building * update tests * update ethereumapis deps * fix tests * update state/stategen * fix build * fix test * add FarFutureSlot * go imports * Radek's suggestions * Ivan's suggestions * type conversions * Nishant's suggestions * add more tests to rpc_send_request * fix test * clean up * fix conflicts Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> Co-authored-by: nisdas <nishdas93@gmail.com>
100 lines
2.7 KiB
Go
100 lines
2.7 KiB
Go
package interchangeformat
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/k0kubun/go-ansi"
|
|
"github.com/prysmaticlabs/eth2-types"
|
|
"github.com/schollz/progressbar/v3"
|
|
)
|
|
|
|
func initializeProgressBar(numItems int, msg string) *progressbar.ProgressBar {
|
|
return progressbar.NewOptions(
|
|
numItems,
|
|
progressbar.OptionFullWidth(),
|
|
progressbar.OptionSetWriter(ansi.NewAnsiStdout()),
|
|
progressbar.OptionEnableColorCodes(true),
|
|
progressbar.OptionSetTheme(progressbar.Theme{
|
|
Saucer: "[green]=[reset]",
|
|
SaucerHead: "[green]>[reset]",
|
|
SaucerPadding: " ",
|
|
BarStart: "[",
|
|
BarEnd: "]",
|
|
}),
|
|
progressbar.OptionOnCompletion(func() { fmt.Println() }),
|
|
progressbar.OptionSetDescription(msg),
|
|
)
|
|
}
|
|
|
|
// Uint64FromString converts a string into a uint64 representation.
|
|
func Uint64FromString(str string) (uint64, error) {
|
|
return strconv.ParseUint(str, 10, 64)
|
|
}
|
|
|
|
// Uint64FromString converts a string into Epoch.
|
|
func EpochFromString(str string) (types.Epoch, error) {
|
|
e, err := strconv.ParseUint(str, 10, 64)
|
|
if err != nil {
|
|
return types.Epoch(e), err
|
|
}
|
|
return types.Epoch(e), nil
|
|
}
|
|
|
|
// SlotFromString converts a string into Slot.
|
|
func SlotFromString(str string) (types.Slot, error) {
|
|
s, err := strconv.ParseUint(str, 10, 64)
|
|
if err != nil {
|
|
return types.Slot(s), err
|
|
}
|
|
return types.Slot(s), nil
|
|
}
|
|
|
|
// PubKeyFromHex takes in a hex string, verifies its length as 48 bytes, and converts that representation.
|
|
func PubKeyFromHex(str string) ([48]byte, error) {
|
|
pubKeyBytes, err := hex.DecodeString(strings.TrimPrefix(str, "0x"))
|
|
if err != nil {
|
|
return [48]byte{}, err
|
|
}
|
|
if len(pubKeyBytes) != 48 {
|
|
return [48]byte{}, fmt.Errorf("public key is not correct, 48-byte length: %s", str)
|
|
}
|
|
var pk [48]byte
|
|
copy(pk[:], pubKeyBytes[:48])
|
|
return pk, nil
|
|
}
|
|
|
|
// RootFromHex takes in a hex string, verifies its length as 32 bytes, and converts that representation.
|
|
func RootFromHex(str string) ([32]byte, error) {
|
|
rootHexBytes, err := hex.DecodeString(strings.TrimPrefix(str, "0x"))
|
|
if err != nil {
|
|
return [32]byte{}, err
|
|
}
|
|
if len(rootHexBytes) != 32 {
|
|
return [32]byte{}, fmt.Errorf("wrong root length, 32-byte length: %s", str)
|
|
}
|
|
var root [32]byte
|
|
copy(root[:], rootHexBytes[:32])
|
|
return root, nil
|
|
}
|
|
|
|
func rootToHexString(root []byte) (string, error) {
|
|
// Nil signing roots are allowed in EIP-3076.
|
|
if len(root) == 0 {
|
|
return "", nil
|
|
}
|
|
if len(root) != 32 {
|
|
return "", fmt.Errorf("wanted length 32, received %d", len(root))
|
|
}
|
|
return fmt.Sprintf("%#x", root), nil
|
|
}
|
|
|
|
func pubKeyToHexString(pubKey []byte) (string, error) {
|
|
if len(pubKey) != 48 {
|
|
return "", fmt.Errorf("wanted length 48, received %d", len(pubKey))
|
|
}
|
|
return fmt.Sprintf("%#x", pubKey), nil
|
|
}
|