Keymanager-Kind: fix cli kind parsing and allow for backward compatability (#10211)

* initial commit with unit tests

* reverting and adding in comments
This commit is contained in:
james-prysm 2022-02-09 14:40:52 -05:00 committed by GitHub
parent 2a68c69d8f
commit c731b715ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 2 deletions

View File

@ -270,3 +270,61 @@ func TestCreateWallet_Remote(t *testing.T) {
// We assert the created configuration was as desired.
assert.DeepEqual(t, wantCfg, cfg)
}
func TestInputKeymanagerKind(t *testing.T) {
tests := []struct {
name string
args string
want keymanager.Kind
wantErr bool
}{
{
name: "local returns local kind",
args: "local",
want: keymanager.Local,
wantErr: false,
},
{
name: "direct returns local kind",
args: "direct",
want: keymanager.Local,
wantErr: false,
},
{
name: "imported returns local kind",
args: "imported",
want: keymanager.Local,
wantErr: false,
},
{
name: "derived returns derived kind",
args: "derived",
want: keymanager.Derived,
wantErr: false,
},
{
name: "remote returns remote kind",
args: "remote",
want: keymanager.Remote,
wantErr: false,
},
{
name: "REMOTE (capitalized) returns remote kind",
args: "REMOTE",
want: keymanager.Remote,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
app := cli.App{}
set := flag.NewFlagSet("test", 0)
set.String(flags.KeymanagerKindFlag.Name, tt.args, "")
assert.NoError(t, set.Set(flags.KeymanagerKindFlag.Name, tt.args))
cliCtx := cli.NewContext(&app, set, nil)
got, err := inputKeymanagerKind(cliCtx)
assert.NoError(t, err)
assert.Equal(t, tt.want.String(), got.String())
})
}
}

View File

@ -3,6 +3,7 @@ package keymanager
import (
"context"
"fmt"
"strings"
"github.com/prysmaticlabs/prysm/async/event"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
@ -85,6 +86,10 @@ func (k Kind) String() string {
case Derived:
return "derived"
case Local:
// TODO(#10181) need a safe way to migrate away from using direct.
// function is used for directory creation, dangerous to change which may result in multiple directories.
// multiple directories will cause the isValid function to fail in wallet.go
// and may result in using a unintended wallet.
return "direct"
case Remote:
return "remote"
@ -97,10 +102,10 @@ func (k Kind) String() string {
// ParseKind from a raw string, returning a keymanager kind.
func ParseKind(k string) (Kind, error) {
switch k {
switch strings.ToLower(k) {
case "derived":
return Derived, nil
case "direct":
case "direct", "imported", "local":
return Local, nil
case "remote":
return Remote, nil