2021-11-09 16:49:28 +00:00
|
|
|
package history
|
2020-11-17 22:37:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-11-26 04:09:35 +00:00
|
|
|
|
|
|
|
"github.com/k0kubun/go-ansi"
|
2022-01-06 17:33:08 +00:00
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
|
2022-04-29 14:32:11 +00:00
|
|
|
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
|
2020-11-26 04:09:35 +00:00
|
|
|
"github.com/schollz/progressbar/v3"
|
2020-11-17 22:37:43 +00:00
|
|
|
)
|
|
|
|
|
2020-11-26 04:09:35 +00:00
|
|
|
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),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:12:22 +00:00
|
|
|
// Uint64FromString converts a string into a uint64 representation.
|
|
|
|
func Uint64FromString(str string) (uint64, error) {
|
2020-11-17 22:37:43 +00:00
|
|
|
return strconv.ParseUint(str, 10, 64)
|
|
|
|
}
|
|
|
|
|
2021-04-23 12:06:05 +00:00
|
|
|
// EpochFromString converts a string into Epoch.
|
2021-02-09 10:05:22 +00:00
|
|
|
func EpochFromString(str string) (types.Epoch, error) {
|
|
|
|
e, err := strconv.ParseUint(str, 10, 64)
|
|
|
|
if err != nil {
|
2021-02-16 07:45:34 +00:00
|
|
|
return types.Epoch(e), err
|
2021-02-09 10:05:22 +00:00
|
|
|
}
|
|
|
|
return types.Epoch(e), nil
|
|
|
|
}
|
|
|
|
|
2021-02-16 07:45:34 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:12:22 +00:00
|
|
|
// PubKeyFromHex takes in a hex string, verifies its length as 48 bytes, and converts that representation.
|
2022-01-06 17:33:08 +00:00
|
|
|
func PubKeyFromHex(str string) ([fieldparams.BLSPubkeyLength]byte, error) {
|
2020-11-17 22:37:43 +00:00
|
|
|
pubKeyBytes, err := hex.DecodeString(strings.TrimPrefix(str, "0x"))
|
|
|
|
if err != nil {
|
2022-01-06 17:33:08 +00:00
|
|
|
return [fieldparams.BLSPubkeyLength]byte{}, err
|
2020-11-17 22:37:43 +00:00
|
|
|
}
|
|
|
|
if len(pubKeyBytes) != 48 {
|
2022-01-06 17:33:08 +00:00
|
|
|
return [fieldparams.BLSPubkeyLength]byte{}, fmt.Errorf("public key is not correct, 48-byte length: %s", str)
|
2020-11-17 22:37:43 +00:00
|
|
|
}
|
2022-01-06 17:33:08 +00:00
|
|
|
var pk [fieldparams.BLSPubkeyLength]byte
|
|
|
|
copy(pk[:], pubKeyBytes[:fieldparams.BLSPubkeyLength])
|
2020-11-17 22:37:43 +00:00
|
|
|
return pk, nil
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:12:22 +00:00
|
|
|
// RootFromHex takes in a hex string, verifies its length as 32 bytes, and converts that representation.
|
|
|
|
func RootFromHex(str string) ([32]byte, error) {
|
2020-11-17 22:37:43 +00:00
|
|
|
rootHexBytes, err := hex.DecodeString(strings.TrimPrefix(str, "0x"))
|
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
|
|
|
if len(rootHexBytes) != 32 {
|
2020-11-20 22:33:51 +00:00
|
|
|
return [32]byte{}, fmt.Errorf("wrong root length, 32-byte length: %s", str)
|
2020-11-17 22:37:43 +00:00
|
|
|
}
|
|
|
|
var root [32]byte
|
|
|
|
copy(root[:], rootHexBytes[:32])
|
|
|
|
return root, nil
|
|
|
|
}
|
2020-11-26 04:09:35 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|