2022-08-15 17:13:45 +00:00
|
|
|
package cliutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-09-04 21:44:41 +00:00
|
|
|
"strconv"
|
2022-08-15 17:13:45 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AddressValue struct {
|
|
|
|
Value *common.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *AddressValue) Set(value string) error {
|
|
|
|
if !common.IsHexAddress(value) {
|
|
|
|
return errors.New("invalid ethereum address")
|
|
|
|
}
|
|
|
|
|
|
|
|
*v.Value = common.HexToAddress(value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *AddressValue) String() string {
|
|
|
|
if v.Value == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return (*v.Value).Hex()
|
|
|
|
}
|
|
|
|
|
|
|
|
type PrivateKeyValue struct {
|
|
|
|
Value **ecdsa.PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *PrivateKeyValue) Set(value string) error {
|
|
|
|
prvKey, err := crypto.ToECDSA(common.FromHex(value))
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("invalid private key")
|
|
|
|
}
|
|
|
|
|
|
|
|
*v.Value = prvKey
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *PrivateKeyValue) String() string {
|
|
|
|
if v.Value == nil || *v.Value == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return "0x" + common.Bytes2Hex(crypto.FromECDSA(*v.Value))
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChoiceValue struct {
|
|
|
|
Choices []string // the choices that this value can take
|
|
|
|
Value *string // the actual value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *ChoiceValue) Set(value string) error {
|
|
|
|
for _, choice := range v.Choices {
|
|
|
|
if strings.Compare(choice, value) == 0 {
|
|
|
|
*v.Value = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%s is not a valid option. need %+v", value, v.Choices)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *ChoiceValue) String() string {
|
|
|
|
if v.Value == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return *v.Value
|
|
|
|
}
|
2023-09-04 21:44:41 +00:00
|
|
|
|
|
|
|
// OptionalUint represents a urfave/cli flag to store uint values that can be
|
|
|
|
// optionally set and not have any default value assigned to it
|
|
|
|
type OptionalUint struct {
|
|
|
|
Value **uint
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set assigns a value to the flag only if it represents a valid uint value
|
|
|
|
func (v *OptionalUint) Set(value string) error {
|
|
|
|
if value != "" {
|
|
|
|
uintVal, err := strconv.ParseUint(value, 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
uVal := uint(uintVal)
|
|
|
|
*v.Value = &uVal
|
|
|
|
} else {
|
|
|
|
v.Value = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the string representation of the OptionalUint flag, if set
|
|
|
|
func (v *OptionalUint) String() string {
|
|
|
|
if v.Value == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%d", *v.Value)
|
|
|
|
}
|