This commit is contained in:
Jim McDonald
2020-11-17 15:53:42 +00:00
parent e042be75ce
commit 0b7a24df6e
14 changed files with 50 additions and 57 deletions

View File

@@ -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")

View File

@@ -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{}

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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")

View File

@@ -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.

View File

@@ -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

View File

@@ -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") != "" {

View File

@@ -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 == "" {

View File

@@ -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")

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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")

View File

@@ -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)