mirror of
https://github.com/wealdtech/ethdo.git
synced 2026-01-09 14:07:56 -05:00
Linting
This commit is contained in:
@@ -34,7 +34,11 @@ func process(ctx context.Context, data *dataIn) (*dataOut, error) {
|
|||||||
if err := locker.Unlock(ctx, []byte(data.walletPassphrase)); err != nil {
|
if err := locker.Unlock(ctx, []byte(data.walletPassphrase)); err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to unlock wallet")
|
return nil, errors.Wrap(err, "failed to unlock wallet")
|
||||||
}
|
}
|
||||||
defer locker.Lock(ctx)
|
defer func() {
|
||||||
|
if err := locker.Lock(ctx); err != nil {
|
||||||
|
util.Log.Trace().Err(err).Msg("Failed to lock wallet")
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
if data.participants == 0 {
|
if data.participants == 0 {
|
||||||
return nil, errors.New("participants is required")
|
return nil, errors.New("participants is required")
|
||||||
|
|||||||
@@ -36,7 +36,11 @@ func process(ctx context.Context, data *dataIn) (*dataOut, error) {
|
|||||||
if err := locker.Unlock(ctx, []byte(data.walletPassphrase)); err != nil {
|
if err := locker.Unlock(ctx, []byte(data.walletPassphrase)); err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to unlock wallet")
|
return nil, errors.Wrap(err, "failed to unlock wallet")
|
||||||
}
|
}
|
||||||
defer locker.Lock(ctx)
|
defer func() {
|
||||||
|
if err := locker.Lock(ctx); err != nil {
|
||||||
|
util.Log.Trace().Err(err).Msg("Failed to lock wallet")
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
results := &dataOut{}
|
results := &dataOut{}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/wealdtech/ethdo/util"
|
||||||
e2wtypes "github.com/wealdtech/go-eth2-wallet-types/v2"
|
e2wtypes "github.com/wealdtech/go-eth2-wallet-types/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -52,7 +53,11 @@ func process(ctx context.Context, data *dataIn) (*dataOut, error) {
|
|||||||
return nil, errors.New("failed to unlock account")
|
return nil, errors.New("failed to unlock account")
|
||||||
}
|
}
|
||||||
// Because we unlocked the accout we should re-lock it when we're done.
|
// Because we unlocked the accout we should re-lock it when we're done.
|
||||||
defer locker.Lock(ctx)
|
defer func() {
|
||||||
|
if err := locker.Lock(ctx); err != nil {
|
||||||
|
util.Log.Trace().Err(err).Msg("Failed to lock account")
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
key, err := privateKeyProvider.PrivateKey(ctx)
|
key, err := privateKeyProvider.PrivateKey(ctx)
|
||||||
|
|||||||
@@ -254,7 +254,7 @@ func outputBlockText(ctx context.Context, data *dataOut, signedBlock *spec.Signe
|
|||||||
res.WriteString(tmp)
|
res.WriteString(tmp)
|
||||||
|
|
||||||
res.WriteString(fmt.Sprintf("Proposer slashings: %d\n", len(body.ProposerSlashings)))
|
res.WriteString(fmt.Sprintf("Proposer slashings: %d\n", len(body.ProposerSlashings)))
|
||||||
// TODO verbose proposer slashings.
|
// Add verbose proposer slashings.
|
||||||
|
|
||||||
tmp, err = outputBlockDeposits(ctx, data.verbose, signedBlock.Message.Body.Deposits)
|
tmp, err = outputBlockDeposits(ctx, data.verbose, signedBlock.Message.Body.Deposits)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -17,11 +17,6 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// getStorePassphrases() fetches the store passphrase supplied by the user.
|
|
||||||
func getStorePassphrase() string {
|
|
||||||
return viper.GetString("store-passphrase")
|
|
||||||
}
|
|
||||||
|
|
||||||
// getWalletPassphrases() fetches the wallet passphrase supplied by the user.
|
// getWalletPassphrases() fetches the wallet passphrase supplied by the user.
|
||||||
func getWalletPassphrase() string {
|
func getWalletPassphrase() string {
|
||||||
return viper.GetString("wallet-passphrase")
|
return viper.GetString("wallet-passphrase")
|
||||||
|
|||||||
51
cmd/root.go
51
cmd/root.go
@@ -25,12 +25,11 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"github.com/wealdtech/ethdo/core"
|
||||||
"github.com/wealdtech/ethdo/util"
|
"github.com/wealdtech/ethdo/util"
|
||||||
e2types "github.com/wealdtech/go-eth2-types/v2"
|
e2types "github.com/wealdtech/go-eth2-types/v2"
|
||||||
e2wallet "github.com/wealdtech/go-eth2-wallet"
|
e2wallet "github.com/wealdtech/go-eth2-wallet"
|
||||||
dirk "github.com/wealdtech/go-eth2-wallet-dirk"
|
dirk "github.com/wealdtech/go-eth2-wallet-dirk"
|
||||||
filesystem "github.com/wealdtech/go-eth2-wallet-store-filesystem"
|
|
||||||
s3 "github.com/wealdtech/go-eth2-wallet-store-s3"
|
|
||||||
e2wtypes "github.com/wealdtech/go-eth2-wallet-types/v2"
|
e2wtypes "github.com/wealdtech/go-eth2-wallet-types/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -39,36 +38,29 @@ var quiet bool
|
|||||||
var verbose bool
|
var verbose bool
|
||||||
var debug bool
|
var debug bool
|
||||||
|
|
||||||
// Root variables, present for all commands.
|
|
||||||
var rootStore string
|
|
||||||
|
|
||||||
// Store for wallet actions.
|
|
||||||
var store e2wtypes.Store
|
|
||||||
|
|
||||||
// RootCmd represents the base command when called without any subcommands
|
// RootCmd represents the base command when called without any subcommands
|
||||||
var RootCmd = &cobra.Command{
|
var RootCmd = &cobra.Command{
|
||||||
Use: "ethdo",
|
Use: "ethdo",
|
||||||
Short: "Ethereum 2 CLI",
|
Short: "Ethereum 2 CLI",
|
||||||
Long: `Manage common Ethereum 2 tasks from the command line.`,
|
Long: `Manage common Ethereum 2 tasks from the command line.`,
|
||||||
PersistentPreRun: persistentPreRun,
|
PersistentPreRunE: persistentPreRunE,
|
||||||
}
|
}
|
||||||
|
|
||||||
func persistentPreRun(cmd *cobra.Command, args []string) {
|
func persistentPreRunE(cmd *cobra.Command, args []string) error {
|
||||||
if cmd.Name() == "help" {
|
if cmd.Name() == "help" {
|
||||||
// User just wants help
|
// User just wants help
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.Name() == "version" {
|
if cmd.Name() == "version" {
|
||||||
// User just wants the version
|
// User just wants the version
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// We bind viper here so that we bind to the correct command.
|
// We bind viper here so that we bind to the correct command.
|
||||||
quiet = viper.GetBool("quiet")
|
quiet = viper.GetBool("quiet")
|
||||||
verbose = viper.GetBool("verbose")
|
verbose = viper.GetBool("verbose")
|
||||||
debug = viper.GetBool("debug")
|
debug = viper.GetBool("debug")
|
||||||
rootStore = viper.GetString("store")
|
|
||||||
// Command-specific bindings.
|
// Command-specific bindings.
|
||||||
switch fmt.Sprintf("%s/%s", cmd.Parent().Name(), cmd.Name()) {
|
switch fmt.Sprintf("%s/%s", cmd.Parent().Name(), cmd.Name()) {
|
||||||
case "account/create":
|
case "account/create":
|
||||||
@@ -100,30 +92,11 @@ func persistentPreRun(cmd *cobra.Command, args []string) {
|
|||||||
fmt.Println("Cannot supply both quiet and debug flags")
|
fmt.Println("Cannot supply both quiet and debug flags")
|
||||||
}
|
}
|
||||||
|
|
||||||
if viper.GetString("remote") == "" {
|
if err := core.SetupStore(); err != nil {
|
||||||
// Set up our wallet store
|
return err
|
||||||
switch rootStore {
|
|
||||||
case "s3":
|
|
||||||
assert(util.GetBaseDir() == "", "--base-dir does not apply for the s3 store")
|
|
||||||
var err error
|
|
||||||
store, err = s3.New(s3.WithPassphrase([]byte(getStorePassphrase())))
|
|
||||||
errCheck(err, "Failed to access Amazon S3 wallet store")
|
|
||||||
case "filesystem":
|
|
||||||
opts := make([]filesystem.Option, 0)
|
|
||||||
if getStorePassphrase() != "" {
|
|
||||||
opts = append(opts, filesystem.WithPassphrase([]byte(getStorePassphrase())))
|
|
||||||
}
|
|
||||||
if util.GetBaseDir() != "" {
|
|
||||||
opts = append(opts, filesystem.WithLocation(util.GetBaseDir()))
|
|
||||||
}
|
|
||||||
store = filesystem.New(opts...)
|
|
||||||
default:
|
|
||||||
die(fmt.Sprintf("Unsupported wallet store %s", rootStore))
|
|
||||||
}
|
|
||||||
err := e2wallet.UseStore(store)
|
|
||||||
viper.Set("store", store)
|
|
||||||
errCheck(err, "Failed to use defined wallet store")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ReleaseVersion is the release version of the codebase.
|
||||||
|
// Usually overrideen by tag names when building binaries.
|
||||||
var ReleaseVersion = "local build from v1.6.1"
|
var ReleaseVersion = "local build from v1.6.1"
|
||||||
|
|
||||||
// versionCmd represents the version command
|
// versionCmd represents the version command
|
||||||
|
|||||||
@@ -32,7 +32,8 @@ import (
|
|||||||
e2wtypes "github.com/wealdtech/go-eth2-wallet-types/v2"
|
e2wtypes "github.com/wealdtech/go-eth2-wallet-types/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func setup() error {
|
// SetupStore sets up the account store.
|
||||||
|
func SetupStore() error {
|
||||||
var store e2wtypes.Store
|
var store e2wtypes.Store
|
||||||
var err error
|
var err error
|
||||||
if viper.GetString("remote") != "" {
|
if viper.GetString("remote") != "" {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetBaseDir() fetches the base directory for wallets.
|
// GetBaseDir fetches the base directory for wallets.
|
||||||
func GetBaseDir() string {
|
func GetBaseDir() string {
|
||||||
baseDir := viper.GetString("base-dir")
|
baseDir := viper.GetString("base-dir")
|
||||||
if baseDir == "" {
|
if baseDir == "" {
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ type depositInfoCLI struct {
|
|||||||
Amount uint64 `json:"amount"`
|
Amount uint64 `json:"amount"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DepositInfoFromJSON obtains deposit info from various possibly formx of JSON.
|
||||||
func DepositInfoFromJSON(input []byte) ([]*DepositInfo, error) {
|
func DepositInfoFromJSON(input []byte) ([]*DepositInfo, error) {
|
||||||
if len(input) == 0 {
|
if len(input) == 0 {
|
||||||
return nil, errors.New("no data supplied")
|
return nil, errors.New("no data supplied")
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ import (
|
|||||||
// Log is the ethdo global logger.
|
// Log is the ethdo global logger.
|
||||||
var Log zerolog.Logger
|
var Log zerolog.Logger
|
||||||
|
|
||||||
// initLogging initialises logging.
|
// InitLogging initialises logging.
|
||||||
func initLogging() error {
|
func InitLogging() error {
|
||||||
// Change the output file.
|
// Change the output file.
|
||||||
if viper.GetString("log-file") != "" {
|
if viper.GetString("log-file") != "" {
|
||||||
f, err := os.OpenFile(viper.GetString("log-file"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
|
f, err := os.OpenFile(viper.GetString("log-file"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetStorePassphrases() fetches the store passphrase supplied by the user.
|
// GetStorePassphrase fetches the store passphrase supplied by the user.
|
||||||
func GetStorePassphrase() string {
|
func GetStorePassphrase() string {
|
||||||
storePassphrase := viper.GetString("store-passphrase")
|
storePassphrase := viper.GetString("store-passphrase")
|
||||||
if storePassphrase == "" {
|
if storePassphrase == "" {
|
||||||
@@ -28,7 +28,7 @@ func GetStorePassphrase() string {
|
|||||||
return storePassphrase
|
return storePassphrase
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetWalletPassphrases() fetches the wallet passphrase supplied by the user.
|
// GetWalletPassphrase fetches the wallet passphrase supplied by the user.
|
||||||
func GetWalletPassphrase() string {
|
func GetWalletPassphrase() string {
|
||||||
walletPassphrase := viper.GetString("wallet-passphrase")
|
walletPassphrase := viper.GetString("wallet-passphrase")
|
||||||
if walletPassphrase == "" {
|
if walletPassphrase == "" {
|
||||||
@@ -38,12 +38,12 @@ func GetWalletPassphrase() string {
|
|||||||
return walletPassphrase
|
return walletPassphrase
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPassphrases() fetches the passphrases supplied by the user.
|
// GetPassphrases fetches the passphrases supplied by the user.
|
||||||
func GetPassphrases() []string {
|
func GetPassphrases() []string {
|
||||||
return viper.GetStringSlice("passphrase")
|
return viper.GetStringSlice("passphrase")
|
||||||
}
|
}
|
||||||
|
|
||||||
// getPassphrase fetches the passphrase supplied by the user.
|
// GetPassphrase fetches the passphrase supplied by the user.
|
||||||
func GetPassphrase() (string, error) {
|
func GetPassphrase() (string, error) {
|
||||||
passphrases := GetPassphrases()
|
passphrases := GetPassphrases()
|
||||||
if len(passphrases) == 0 {
|
if len(passphrases) == 0 {
|
||||||
|
|||||||
@@ -60,36 +60,44 @@ func newScratchAccountFromPubKey(pubKey []byte) (*ScratchAccount, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ID returns the account ID.
|
||||||
func (a *ScratchAccount) ID() uuid.UUID {
|
func (a *ScratchAccount) ID() uuid.UUID {
|
||||||
return a.id
|
return a.id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Name returns the account name.
|
||||||
func (a *ScratchAccount) Name() string {
|
func (a *ScratchAccount) Name() string {
|
||||||
return "scratch"
|
return "scratch"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PublicKey returns the account public key.
|
||||||
func (a *ScratchAccount) PublicKey() e2types.PublicKey {
|
func (a *ScratchAccount) PublicKey() e2types.PublicKey {
|
||||||
return a.pubKey
|
return a.pubKey
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Path returns the account path.
|
||||||
func (a *ScratchAccount) Path() string {
|
func (a *ScratchAccount) Path() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Lock locks the account.
|
||||||
func (a *ScratchAccount) Lock(ctx context.Context) error {
|
func (a *ScratchAccount) Lock(ctx context.Context) error {
|
||||||
a.unlocked = false
|
a.unlocked = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unlock unlocks the account.
|
||||||
func (a *ScratchAccount) Unlock(ctx context.Context, passphrase []byte) error {
|
func (a *ScratchAccount) Unlock(ctx context.Context, passphrase []byte) error {
|
||||||
a.unlocked = true
|
a.unlocked = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsUnlocked returns true if the account is unlocked.
|
||||||
func (a *ScratchAccount) IsUnlocked(ctx context.Context) (bool, error) {
|
func (a *ScratchAccount) IsUnlocked(ctx context.Context) (bool, error) {
|
||||||
return a.unlocked, nil
|
return a.unlocked, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sign signs data with the account's private key.
|
||||||
func (a *ScratchAccount) Sign(ctx context.Context, data []byte) (e2types.Signature, error) {
|
func (a *ScratchAccount) Sign(ctx context.Context, data []byte) (e2types.Signature, error) {
|
||||||
if !a.unlocked {
|
if !a.unlocked {
|
||||||
return nil, errors.New("locked")
|
return nil, errors.New("locked")
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ func TestScratchAccountFromPublicKey(t *testing.T) {
|
|||||||
require.True(t, unlocked)
|
require.True(t, unlocked)
|
||||||
_, err = account.Sign(context.Background(), testutil.HexToBytes("0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"))
|
_, err = account.Sign(context.Background(), testutil.HexToBytes("0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"))
|
||||||
require.EqualError(t, err, "no private key")
|
require.EqualError(t, err, "no private key")
|
||||||
account.Lock(context.Background())
|
require.NoError(t, account.Lock(context.Background()))
|
||||||
unlocked, err = account.IsUnlocked(context.Background())
|
unlocked, err = account.IsUnlocked(context.Background())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.False(t, unlocked)
|
require.False(t, unlocked)
|
||||||
|
|||||||
Reference in New Issue
Block a user