From 0b7a24df6e7be85f21ef35c8087fa702a024e1eb Mon Sep 17 00:00:00 2001 From: Jim McDonald Date: Tue, 17 Nov 2020 15:53:42 +0000 Subject: [PATCH] Linting --- cmd/account/create/process.go | 6 ++++- cmd/account/import/process.go | 6 ++++- cmd/account/key/process.go | 7 ++++- cmd/block/info/output.go | 2 +- cmd/passphrases.go | 5 ---- cmd/root.go | 51 +++++++++-------------------------- cmd/version.go | 2 ++ core/account.go | 3 ++- util/basedir.go | 2 +- util/depositinfo.go | 1 + util/logging.go | 4 +-- util/passphrases.go | 8 +++--- util/scratchaccount.go | 8 ++++++ util/scratchaccount_test.go | 2 +- 14 files changed, 50 insertions(+), 57 deletions(-) diff --git a/cmd/account/create/process.go b/cmd/account/create/process.go index 6b1a3e7..8d465f5 100644 --- a/cmd/account/create/process.go +++ b/cmd/account/create/process.go @@ -34,7 +34,11 @@ func process(ctx context.Context, data *dataIn) (*dataOut, error) { if err := locker.Unlock(ctx, []byte(data.walletPassphrase)); err != nil { 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 { return nil, errors.New("participants is required") diff --git a/cmd/account/import/process.go b/cmd/account/import/process.go index b921097..a4e5c64 100644 --- a/cmd/account/import/process.go +++ b/cmd/account/import/process.go @@ -36,7 +36,11 @@ func process(ctx context.Context, data *dataIn) (*dataOut, error) { if err := locker.Unlock(ctx, []byte(data.walletPassphrase)); err != nil { 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{} diff --git a/cmd/account/key/process.go b/cmd/account/key/process.go index 85a6658..9e43b84 100644 --- a/cmd/account/key/process.go +++ b/cmd/account/key/process.go @@ -17,6 +17,7 @@ import ( "context" "github.com/pkg/errors" + "github.com/wealdtech/ethdo/util" 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") } // 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) diff --git a/cmd/block/info/output.go b/cmd/block/info/output.go index f900a2a..102fed6 100644 --- a/cmd/block/info/output.go +++ b/cmd/block/info/output.go @@ -254,7 +254,7 @@ func outputBlockText(ctx context.Context, data *dataOut, signedBlock *spec.Signe res.WriteString(tmp) 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) if err != nil { diff --git a/cmd/passphrases.go b/cmd/passphrases.go index d5b087b..a71e372 100644 --- a/cmd/passphrases.go +++ b/cmd/passphrases.go @@ -17,11 +17,6 @@ import ( "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. func getWalletPassphrase() string { return viper.GetString("wallet-passphrase") diff --git a/cmd/root.go b/cmd/root.go index 3ef89eb..71b822e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -25,12 +25,11 @@ import ( "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/viper" + "github.com/wealdtech/ethdo/core" "github.com/wealdtech/ethdo/util" e2types "github.com/wealdtech/go-eth2-types/v2" e2wallet "github.com/wealdtech/go-eth2-wallet" 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" ) @@ -39,36 +38,29 @@ var quiet bool var verbose 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 var RootCmd = &cobra.Command{ - Use: "ethdo", - Short: "Ethereum 2 CLI", - Long: `Manage common Ethereum 2 tasks from the command line.`, - PersistentPreRun: persistentPreRun, + Use: "ethdo", + Short: "Ethereum 2 CLI", + Long: `Manage common Ethereum 2 tasks from the command line.`, + PersistentPreRunE: persistentPreRunE, } -func persistentPreRun(cmd *cobra.Command, args []string) { +func persistentPreRunE(cmd *cobra.Command, args []string) error { if cmd.Name() == "help" { // User just wants help - return + return nil } if cmd.Name() == "version" { // User just wants the version - return + return nil } // We bind viper here so that we bind to the correct command. quiet = viper.GetBool("quiet") verbose = viper.GetBool("verbose") debug = viper.GetBool("debug") - rootStore = viper.GetString("store") // Command-specific bindings. switch fmt.Sprintf("%s/%s", cmd.Parent().Name(), cmd.Name()) { case "account/create": @@ -100,30 +92,11 @@ func persistentPreRun(cmd *cobra.Command, args []string) { fmt.Println("Cannot supply both quiet and debug flags") } - if viper.GetString("remote") == "" { - // Set up our wallet store - 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") + if err := core.SetupStore(); err != nil { + return err } + + return nil } // Execute adds all child commands to the root command and sets flags appropriately. diff --git a/cmd/version.go b/cmd/version.go index 230028b..6e0d941 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -22,6 +22,8 @@ import ( "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" // versionCmd represents the version command diff --git a/core/account.go b/core/account.go index dc30aa3..51a4c5e 100644 --- a/core/account.go +++ b/core/account.go @@ -32,7 +32,8 @@ import ( 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 err error if viper.GetString("remote") != "" { diff --git a/util/basedir.go b/util/basedir.go index 1b629b4..10d094b 100644 --- a/util/basedir.go +++ b/util/basedir.go @@ -17,7 +17,7 @@ import ( "github.com/spf13/viper" ) -// GetBaseDir() fetches the base directory for wallets. +// GetBaseDir fetches the base directory for wallets. func GetBaseDir() string { baseDir := viper.GetString("base-dir") if baseDir == "" { diff --git a/util/depositinfo.go b/util/depositinfo.go index 5469ca7..2589b1a 100644 --- a/util/depositinfo.go +++ b/util/depositinfo.go @@ -74,6 +74,7 @@ type depositInfoCLI struct { Amount uint64 `json:"amount"` } +// DepositInfoFromJSON obtains deposit info from various possibly formx of JSON. func DepositInfoFromJSON(input []byte) ([]*DepositInfo, error) { if len(input) == 0 { return nil, errors.New("no data supplied") diff --git a/util/logging.go b/util/logging.go index 947c81a..88792f2 100644 --- a/util/logging.go +++ b/util/logging.go @@ -26,8 +26,8 @@ import ( // Log is the ethdo global logger. var Log zerolog.Logger -// initLogging initialises logging. -func initLogging() error { +// InitLogging initialises logging. +func InitLogging() error { // Change the output file. if viper.GetString("log-file") != "" { f, err := os.OpenFile(viper.GetString("log-file"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) diff --git a/util/passphrases.go b/util/passphrases.go index b5cfaec..08e1fb4 100644 --- a/util/passphrases.go +++ b/util/passphrases.go @@ -18,7 +18,7 @@ import ( "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 { storePassphrase := viper.GetString("store-passphrase") if storePassphrase == "" { @@ -28,7 +28,7 @@ func GetStorePassphrase() string { return storePassphrase } -// GetWalletPassphrases() fetches the wallet passphrase supplied by the user. +// GetWalletPassphrase fetches the wallet passphrase supplied by the user. func GetWalletPassphrase() string { walletPassphrase := viper.GetString("wallet-passphrase") if walletPassphrase == "" { @@ -38,12 +38,12 @@ func GetWalletPassphrase() string { return walletPassphrase } -// GetPassphrases() fetches the passphrases supplied by the user. +// GetPassphrases fetches the passphrases supplied by the user. func GetPassphrases() []string { return viper.GetStringSlice("passphrase") } -// getPassphrase fetches the passphrase supplied by the user. +// GetPassphrase fetches the passphrase supplied by the user. func GetPassphrase() (string, error) { passphrases := GetPassphrases() if len(passphrases) == 0 { diff --git a/util/scratchaccount.go b/util/scratchaccount.go index 39262d5..224f3f6 100644 --- a/util/scratchaccount.go +++ b/util/scratchaccount.go @@ -60,36 +60,44 @@ func newScratchAccountFromPubKey(pubKey []byte) (*ScratchAccount, error) { }, nil } +// ID returns the account ID. func (a *ScratchAccount) ID() uuid.UUID { return a.id } +// Name returns the account name. func (a *ScratchAccount) Name() string { return "scratch" } +// PublicKey returns the account public key. func (a *ScratchAccount) PublicKey() e2types.PublicKey { return a.pubKey } +// Path returns the account path. func (a *ScratchAccount) Path() string { return "" } +// Lock locks the account. func (a *ScratchAccount) Lock(ctx context.Context) error { a.unlocked = false return nil } +// Unlock unlocks the account. func (a *ScratchAccount) Unlock(ctx context.Context, passphrase []byte) error { a.unlocked = true return nil } +// IsUnlocked returns true if the account is unlocked. func (a *ScratchAccount) IsUnlocked(ctx context.Context) (bool, error) { 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) { if !a.unlocked { return nil, errors.New("locked") diff --git a/util/scratchaccount_test.go b/util/scratchaccount_test.go index 500aa64..8eafee2 100644 --- a/util/scratchaccount_test.go +++ b/util/scratchaccount_test.go @@ -138,7 +138,7 @@ func TestScratchAccountFromPublicKey(t *testing.T) { require.True(t, unlocked) _, err = account.Sign(context.Background(), testutil.HexToBytes("0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")) require.EqualError(t, err, "no private key") - account.Lock(context.Background()) + require.NoError(t, account.Lock(context.Background())) unlocked, err = account.IsUnlocked(context.Background()) require.NoError(t, err) require.False(t, unlocked)