-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wallet edit CLI Manager migration #11136
Merged
prylabs-bulldozer
merged 10 commits into
prysmaticlabs:develop
from
michaelneuder:mikeneuder-20220717
Aug 3, 2022
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1211a08
Wallet edit CLI Manager migration
michaelneuder 13b00bb
TODO for code deduplication
michaelneuder 77f001e
Merge branch 'develop' into mikeneuder-20220717
michaelneuder f8d0412
Merge branch 'develop' into mikeneuder-20220717
james-prysm 4132616
s/walletEdit/remoteWalletEdit/g
michaelneuder 8d0ece7
Merge branch 'develop' into mikeneuder-20220717
michaelneuder 2794776
s/walletEdit/remoteWalletEdit/g
michaelneuder 18218ad
remove unused struct field
michaelneuder df7056a
Merge branch 'develop' into mikeneuder-20220717
james-prysm 034ea39
Merge branch 'develop' into mikeneuder-20220717
james-prysm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package wallet | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/prysmaticlabs/prysm/validator/accounts" | ||
"github.com/prysmaticlabs/prysm/validator/accounts/userprompt" | ||
"github.com/prysmaticlabs/prysm/validator/accounts/wallet" | ||
"github.com/prysmaticlabs/prysm/validator/keymanager" | ||
"github.com/prysmaticlabs/prysm/validator/keymanager/remote" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func walletEdit(c *cli.Context) error { | ||
w, err := wallet.OpenWalletOrElseCli(c, func(cliCtx *cli.Context) (*wallet.Wallet, error) { | ||
return nil, wallet.ErrNoWalletFound | ||
}) | ||
if err != nil { | ||
return errors.Wrap(err, "could not open wallet") | ||
} | ||
if w.KeymanagerKind() != keymanager.Remote { | ||
return errors.New( | ||
fmt.Sprintf("Keymanager type: %s doesn't support configuration editing", | ||
w.KeymanagerKind().String())) | ||
} | ||
|
||
enc, err := w.ReadKeymanagerConfigFromDisk(c.Context) | ||
if err != nil { | ||
return errors.Wrap(err, "could not read config") | ||
} | ||
fileOpts, err := remote.UnmarshalOptionsFile(enc) | ||
if err != nil { | ||
return errors.Wrap(err, "could not unmarshal config") | ||
} | ||
log.Info("Current configuration") | ||
// Prints the current configuration to stdout. | ||
fmt.Println(fileOpts) | ||
newCfg, err := userprompt.InputRemoteKeymanagerConfig(c) | ||
if err != nil { | ||
return errors.Wrap(err, "could not get keymanager config") | ||
} | ||
|
||
opts := []accounts.Option{ | ||
accounts.WithWallet(w), | ||
accounts.WithKeymanagerOpts(newCfg), | ||
} | ||
|
||
acc, err := accounts.NewCLIManager(opts...) | ||
if err != nil { | ||
return err | ||
} | ||
return acc.WalletEdit(c.Context) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package wallet | ||
|
||
import ( | ||
"flag" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/prysmaticlabs/prysm/cmd/validator/flags" | ||
"github.com/prysmaticlabs/prysm/config/params" | ||
"github.com/prysmaticlabs/prysm/testing/assert" | ||
"github.com/prysmaticlabs/prysm/testing/require" | ||
"github.com/prysmaticlabs/prysm/validator/accounts" | ||
"github.com/prysmaticlabs/prysm/validator/accounts/wallet" | ||
"github.com/prysmaticlabs/prysm/validator/keymanager" | ||
"github.com/prysmaticlabs/prysm/validator/keymanager/remote" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
const ( | ||
passwordFileName = "password.txt" | ||
password = "OhWOWthisisatest42!$" | ||
) | ||
|
||
// TODO(mikeneuder): Figure out how to shared these functions with | ||
// `cmd/validator/accounts/delete_test.go`. https://pastebin.com/2n2VB7Ez is | ||
// the error I couldn't get around. | ||
func setupWalletAndPasswordsDir(t testing.TB) (string, string, string) { | ||
walletDir := filepath.Join(t.TempDir(), "wallet") | ||
passwordsDir := filepath.Join(t.TempDir(), "passwords") | ||
passwordFileDir := filepath.Join(t.TempDir(), "passwordFile") | ||
require.NoError(t, os.MkdirAll(passwordFileDir, params.BeaconIoConfig().ReadWriteExecutePermissions)) | ||
passwordFilePath := filepath.Join(passwordFileDir, passwordFileName) | ||
require.NoError(t, os.WriteFile(passwordFilePath, []byte(password), os.ModePerm)) | ||
return walletDir, passwordsDir, passwordFilePath | ||
} | ||
|
||
type testWalletConfig struct { | ||
exitAll bool | ||
skipDepositConfirm bool | ||
keymanagerKind keymanager.Kind | ||
numAccounts int64 | ||
grpcHeaders string | ||
privateKeyFile string | ||
accountPasswordFile string | ||
walletPasswordFile string | ||
backupPasswordFile string | ||
backupPublicKeys string | ||
voluntaryExitPublicKeys string | ||
deletePublicKeys string | ||
keysDir string | ||
backupDir string | ||
passwordsDir string | ||
walletDir string | ||
} | ||
|
||
func setupWalletCtx( | ||
tb testing.TB, | ||
cfg *testWalletConfig, | ||
) *cli.Context { | ||
app := cli.App{} | ||
set := flag.NewFlagSet("test", 0) | ||
set.String(flags.WalletDirFlag.Name, cfg.walletDir, "") | ||
set.String(flags.KeysDirFlag.Name, cfg.keysDir, "") | ||
set.String(flags.KeymanagerKindFlag.Name, cfg.keymanagerKind.String(), "") | ||
set.String(flags.DeletePublicKeysFlag.Name, cfg.deletePublicKeys, "") | ||
set.String(flags.VoluntaryExitPublicKeysFlag.Name, cfg.voluntaryExitPublicKeys, "") | ||
set.String(flags.BackupDirFlag.Name, cfg.backupDir, "") | ||
set.String(flags.BackupPasswordFile.Name, cfg.backupPasswordFile, "") | ||
set.String(flags.BackupPublicKeysFlag.Name, cfg.backupPublicKeys, "") | ||
set.String(flags.WalletPasswordFileFlag.Name, cfg.walletPasswordFile, "") | ||
set.String(flags.AccountPasswordFileFlag.Name, cfg.accountPasswordFile, "") | ||
set.Int64(flags.NumAccountsFlag.Name, cfg.numAccounts, "") | ||
set.Bool(flags.SkipDepositConfirmationFlag.Name, cfg.skipDepositConfirm, "") | ||
set.Bool(flags.SkipMnemonic25thWordCheckFlag.Name, true, "") | ||
set.Bool(flags.ExitAllFlag.Name, cfg.exitAll, "") | ||
set.String(flags.GrpcHeadersFlag.Name, cfg.grpcHeaders, "") | ||
|
||
if cfg.privateKeyFile != "" { | ||
set.String(flags.ImportPrivateKeyFileFlag.Name, cfg.privateKeyFile, "") | ||
assert.NoError(tb, set.Set(flags.ImportPrivateKeyFileFlag.Name, cfg.privateKeyFile)) | ||
} | ||
assert.NoError(tb, set.Set(flags.WalletDirFlag.Name, cfg.walletDir)) | ||
assert.NoError(tb, set.Set(flags.SkipMnemonic25thWordCheckFlag.Name, "true")) | ||
assert.NoError(tb, set.Set(flags.KeysDirFlag.Name, cfg.keysDir)) | ||
assert.NoError(tb, set.Set(flags.KeymanagerKindFlag.Name, cfg.keymanagerKind.String())) | ||
assert.NoError(tb, set.Set(flags.DeletePublicKeysFlag.Name, cfg.deletePublicKeys)) | ||
assert.NoError(tb, set.Set(flags.VoluntaryExitPublicKeysFlag.Name, cfg.voluntaryExitPublicKeys)) | ||
assert.NoError(tb, set.Set(flags.BackupDirFlag.Name, cfg.backupDir)) | ||
assert.NoError(tb, set.Set(flags.BackupPublicKeysFlag.Name, cfg.backupPublicKeys)) | ||
assert.NoError(tb, set.Set(flags.BackupPasswordFile.Name, cfg.backupPasswordFile)) | ||
assert.NoError(tb, set.Set(flags.WalletPasswordFileFlag.Name, cfg.walletPasswordFile)) | ||
assert.NoError(tb, set.Set(flags.AccountPasswordFileFlag.Name, cfg.accountPasswordFile)) | ||
assert.NoError(tb, set.Set(flags.NumAccountsFlag.Name, strconv.Itoa(int(cfg.numAccounts)))) | ||
assert.NoError(tb, set.Set(flags.SkipDepositConfirmationFlag.Name, strconv.FormatBool(cfg.skipDepositConfirm))) | ||
assert.NoError(tb, set.Set(flags.ExitAllFlag.Name, strconv.FormatBool(cfg.exitAll))) | ||
assert.NoError(tb, set.Set(flags.GrpcHeadersFlag.Name, cfg.grpcHeaders)) | ||
return cli.NewContext(&app, set, nil) | ||
} | ||
|
||
func TestEditWalletConfiguration(t *testing.T) { | ||
walletDir, _, passwordFile := setupWalletAndPasswordsDir(t) | ||
cliCtx := setupWalletCtx(t, &testWalletConfig{ | ||
walletDir: walletDir, | ||
keymanagerKind: keymanager.Remote, | ||
}) | ||
w, err := accounts.CreateWalletWithKeymanager(cliCtx.Context, &accounts.CreateWalletConfig{ | ||
WalletCfg: &wallet.Config{ | ||
WalletDir: walletDir, | ||
KeymanagerKind: keymanager.Remote, | ||
WalletPassword: "Passwordz0320$", | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
originalCfg := &remote.KeymanagerOpts{ | ||
RemoteCertificate: &remote.CertificateConfig{ | ||
RequireTls: true, | ||
ClientCertPath: "/tmp/a.crt", | ||
ClientKeyPath: "/tmp/b.key", | ||
CACertPath: "/tmp/c.crt", | ||
}, | ||
RemoteAddr: "my.server.com:4000", | ||
} | ||
encodedCfg, err := remote.MarshalOptionsFile(cliCtx.Context, originalCfg) | ||
assert.NoError(t, err) | ||
assert.NoError(t, w.WriteKeymanagerConfigToDisk(cliCtx.Context, encodedCfg)) | ||
|
||
wantCfg := &remote.KeymanagerOpts{ | ||
RemoteCertificate: &remote.CertificateConfig{ | ||
RequireTls: true, | ||
ClientCertPath: "/tmp/client.crt", | ||
ClientKeyPath: "/tmp/client.key", | ||
CACertPath: "/tmp/ca.crt", | ||
}, | ||
RemoteAddr: "host.example.com:4000", | ||
} | ||
app := cli.App{} | ||
set := flag.NewFlagSet("test", 0) | ||
set.String(flags.WalletDirFlag.Name, walletDir, "") | ||
set.String(flags.WalletPasswordFileFlag.Name, passwordFile, "") | ||
set.String(flags.GrpcRemoteAddressFlag.Name, wantCfg.RemoteAddr, "") | ||
set.String(flags.RemoteSignerCertPathFlag.Name, wantCfg.RemoteCertificate.ClientCertPath, "") | ||
set.String(flags.RemoteSignerKeyPathFlag.Name, wantCfg.RemoteCertificate.ClientKeyPath, "") | ||
set.String(flags.RemoteSignerCACertPathFlag.Name, wantCfg.RemoteCertificate.CACertPath, "") | ||
assert.NoError(t, set.Set(flags.WalletDirFlag.Name, walletDir)) | ||
assert.NoError(t, set.Set(flags.WalletPasswordFileFlag.Name, passwordFile)) | ||
assert.NoError(t, set.Set(flags.GrpcRemoteAddressFlag.Name, wantCfg.RemoteAddr)) | ||
assert.NoError(t, set.Set(flags.RemoteSignerCertPathFlag.Name, wantCfg.RemoteCertificate.ClientCertPath)) | ||
assert.NoError(t, set.Set(flags.RemoteSignerKeyPathFlag.Name, wantCfg.RemoteCertificate.ClientKeyPath)) | ||
assert.NoError(t, set.Set(flags.RemoteSignerCACertPathFlag.Name, wantCfg.RemoteCertificate.CACertPath)) | ||
cliCtx = cli.NewContext(&app, set, nil) | ||
|
||
err = walletEdit(cliCtx) | ||
require.NoError(t, err) | ||
encoded, err := w.ReadKeymanagerConfigFromDisk(cliCtx.Context) | ||
require.NoError(t, err) | ||
|
||
cfg, err := remote.UnmarshalOptionsFile(encoded) | ||
assert.NoError(t, err) | ||
assert.DeepEqual(t, wantCfg, cfg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we call this remote wallet edit or something? wallet is pretty generic if the feature is only really used for the remote keymanager configs, might be kind of confusing in the future. one of the old functions is called edit remote wallet which shows more what it's meant for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renamed to
remoteWalletEdit
! good call, thanks!