2020-11-17 22:37:43 +00:00
|
|
|
package interchangeformat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-11-26 04:09:35 +00:00
|
|
|
|
|
|
|
"github.com/k0kubun/go-ansi"
|
2021-02-22 23:20:57 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
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.
|
|
|
|
func PubKeyFromHex(str string) ([48]byte, error) {
|
2020-11-17 22:37:43 +00:00
|
|
|
pubKeyBytes, err := hex.DecodeString(strings.TrimPrefix(str, "0x"))
|
|
|
|
if err != nil {
|
|
|
|
return [48]byte{}, err
|
|
|
|
}
|
|
|
|
if len(pubKeyBytes) != 48 {
|
2020-11-20 22:33:51 +00:00
|
|
|
return [48]byte{}, fmt.Errorf("public key is not correct, 48-byte length: %s", str)
|
2020-11-17 22:37:43 +00:00
|
|
|
}
|
|
|
|
var pk [48]byte
|
|
|
|
copy(pk[:], pubKeyBytes[:48])
|
|
|
|
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
|
|
|
|
}
|