Skip to content

Commit

Permalink
cmd: migrate to urfave/cli/v2 (ethereum#24751)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Nov 22, 2024
1 parent 962be21 commit 92e07ea
Show file tree
Hide file tree
Showing 36 changed files with 2,219 additions and 1,891 deletions.
49 changes: 26 additions & 23 deletions cmd/XDC/accountcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,31 @@ import (
"github.com/XinFinOrg/XDPoSChain/console"
"github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/log"
"gopkg.in/urfave/cli.v1"
"github.com/urfave/cli/v2"
)

var (
walletCommand = cli.Command{
walletCommand = &cli.Command{
Name: "wallet",
Usage: "Manage XDPoSChain presale wallets",
ArgsUsage: "",
Category: "ACCOUNT COMMANDS",
Description: `
XDC wallet import /path/to/my/presale.wallet
will prompt for your password and imports your ether presale account.
It can be used non-interactively with the --password option taking a
passwordfile as argument containing the wallet password in plaintext.`,
Subcommands: []cli.Command{
Subcommands: []*cli.Command{
{

Name: "import",
Usage: "Import XDPoSChain presale wallet",
ArgsUsage: "<keyFile>",
Action: utils.MigrateFlags(importWallet),
Action: importWallet,
Category: "ACCOUNT COMMANDS",
Flags: []cli.Flag{
utils.DataDirFlag,
utils.XDCXDataDirFlag,
utils.KeyStoreDirFlag,
utils.PasswordFileFlag,
utils.LightKDFFlag,
Expand All @@ -65,10 +65,9 @@ passwordfile as argument containing the wallet password in plaintext.`,
},
}

accountCommand = cli.Command{
Name: "account",
Usage: "Manage accounts",
Category: "ACCOUNT COMMANDS",
accountCommand = &cli.Command{
Name: "account",
Usage: "Manage accounts",
Description: `
Manage accounts, list all existing accounts, import a private key into a new
Expand All @@ -89,13 +88,14 @@ It is safe to transfer the entire directory or the individual keys therein
between ethereum nodes by simply copying.
Make sure you backup your keys regularly.`,
Subcommands: []cli.Command{
Subcommands: []*cli.Command{
{
Name: "list",
Usage: "Print summary of existing accounts",
Action: utils.MigrateFlags(accountList),
Action: accountList,
Flags: []cli.Flag{
utils.DataDirFlag,
utils.XDCXDataDirFlag,
utils.KeyStoreDirFlag,
},
Description: `
Expand All @@ -104,9 +104,10 @@ Print a short summary of all accounts`,
{
Name: "new",
Usage: "Create a new account",
Action: utils.MigrateFlags(accountCreate),
Action: accountCreate,
Flags: []cli.Flag{
utils.DataDirFlag,
utils.XDCXDataDirFlag,
utils.KeyStoreDirFlag,
utils.PasswordFileFlag,
utils.LightKDFFlag,
Expand All @@ -129,10 +130,11 @@ password to file or expose in any other way.
{
Name: "update",
Usage: "Update an existing account",
Action: utils.MigrateFlags(accountUpdate),
Action: accountUpdate,
ArgsUsage: "<address>",
Flags: []cli.Flag{
utils.DataDirFlag,
utils.XDCXDataDirFlag,
utils.KeyStoreDirFlag,
utils.LightKDFFlag,
},
Expand All @@ -158,9 +160,10 @@ changing your password is only possible interactively.
{
Name: "import",
Usage: "Import a private key into a new account",
Action: utils.MigrateFlags(accountImport),
Action: accountImport,
Flags: []cli.Flag{
utils.DataDirFlag,
utils.XDCXDataDirFlag,
utils.KeyStoreDirFlag,
utils.PasswordFileFlag,
utils.LightKDFFlag,
Expand Down Expand Up @@ -293,7 +296,7 @@ func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrErr
func accountCreate(ctx *cli.Context) error {
cfg := XDCConfig{Node: defaultNodeConfig()}
// Load config file.
if file := ctx.GlobalString(configFileFlag.Name); file != "" {
if file := ctx.String(configFileFlag.Name); file != "" {
if err := loadConfig(file, &cfg); err != nil {
utils.Fatalf("%v", err)
}
Expand All @@ -319,13 +322,13 @@ func accountCreate(ctx *cli.Context) error {
// accountUpdate transitions an account from a previous format to the current
// one, also providing the possibility to change the pass-phrase.
func accountUpdate(ctx *cli.Context) error {
if len(ctx.Args()) == 0 {
if ctx.Args().Len() == 0 {
utils.Fatalf("No accounts specified to update")
}
stack, _ := makeConfigNode(ctx)
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)

for _, addr := range ctx.Args() {
for _, addr := range ctx.Args().Slice() {
account, oldPassword := unlockAccount(ctx, ks, addr, 0, nil)
newPassword := getPassPhrase("Please give a new password. Do not forget this password.", true, 0, nil)
if err := ks.Update(account, oldPassword, newPassword); err != nil {
Expand All @@ -336,10 +339,10 @@ func accountUpdate(ctx *cli.Context) error {
}

func importWallet(ctx *cli.Context) error {
keyfile := ctx.Args().First()
if len(keyfile) == 0 {
utils.Fatalf("keyfile must be given as argument")
if ctx.Args().Len() != 1 {
utils.Fatalf("keyfile must be given as the only argument")
}
keyfile := ctx.Args().First()
keyJson, err := os.ReadFile(keyfile)
if err != nil {
utils.Fatalf("Could not read wallet file: %v", err)
Expand All @@ -358,10 +361,10 @@ func importWallet(ctx *cli.Context) error {
}

func accountImport(ctx *cli.Context) error {
keyfile := ctx.Args().First()
if len(keyfile) == 0 {
utils.Fatalf("keyfile must be given as argument")
if ctx.Args().Len() != 1 {
utils.Fatalf("keyfile must be given as the only argument")
}
keyfile := ctx.Args().First()
key, err := crypto.LoadECDSA(keyfile)
if err != nil {
utils.Fatalf("Failed to load the private key: %v", err)
Expand Down
95 changes: 68 additions & 27 deletions cmd/XDC/accountcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,31 @@ func tmpDatadirWithKeystore(t *testing.T) string {
}

func TestAccountListEmpty(t *testing.T) {
XDC := runXDC(t, "account", "list")
datadir := tmpdir(t)
defer os.RemoveAll(datadir)
XDC := runXDC(t, "account", "list", "--datadir", datadir)
XDC.ExpectExit()
}

func TestAccountList(t *testing.T) {
datadir := tmpDatadirWithKeystore(t)
XDC := runXDC(t, "account", "list", "--datadir", datadir)
defer XDC.ExpectExit()
defer os.RemoveAll(datadir)
var want = `
Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}/keystore/UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}/keystore/aaa
Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://{{.Datadir}}/keystore/zzz
`
if runtime.GOOS == "windows" {
XDC.Expect(`
want = `
Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}\keystore\UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}\keystore\aaa
Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://{{.Datadir}}\keystore\zzz
`)
} else {
XDC.Expect(`
Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}/keystore/UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}/keystore/aaa
Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://{{.Datadir}}/keystore/zzz
`)
`
}
{
geth := runXDC(t, "account", "list", "--datadir", datadir)
geth.Expect(want)
geth.ExpectExit()
}
}

Expand Down Expand Up @@ -92,9 +97,8 @@ Fatal: Passphrases do not match

func TestAccountUpdate(t *testing.T) {
datadir := tmpDatadirWithKeystore(t)
XDC := runXDC(t, "account", "update",
"--datadir", datadir, "--lightkdf",
"f466859ead1932d743d622cb74fc058882e8648a")
defer os.RemoveAll(datadir)
XDC := runXDC(t, "account", "update", "--datadir", datadir, "--lightkdf", "f466859ead1932d743d622cb74fc058882e8648a")
defer XDC.ExpectExit()
XDC.Expect(`
Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
Expand All @@ -107,7 +111,9 @@ Repeat passphrase: {{.InputLine "foobar2"}}
}

func TestWalletImport(t *testing.T) {
XDC := runXDC(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
datadir := tmpdir(t)
defer os.RemoveAll(datadir)
XDC := runXDC(t, "wallet", "import", "--datadir", datadir, "--lightkdf", "testdata/guswallet.json")
defer XDC.ExpectExit()
XDC.Expect(`
!! Unsupported terminal, password will be echoed.
Expand All @@ -121,6 +127,36 @@ Address: {xdcd4584b5f6229b7be90727b0fc8c6b91bb427821f}
}
}

func TestAccountHelp(t *testing.T) {
geth := runXDC(t, "account", "-h")
geth.WaitExit()
if have, want := geth.ExitStatus(), 0; have != want {
t.Errorf("exit error, have %d want %d", have, want)
}

geth = runXDC(t, "account", "import", "-h")
geth.WaitExit()
if have, want := geth.ExitStatus(), 0; have != want {
t.Errorf("exit error, have %d want %d", have, want)
}
}

func importAccountWithExpect(t *testing.T, key string, expected string) {
dir := t.TempDir()
defer os.RemoveAll(dir)
keyfile := filepath.Join(dir, "key.prv")
if err := os.WriteFile(keyfile, []byte(key), 0600); err != nil {
t.Error(err)
}
passwordFile := filepath.Join(dir, "password.txt")
if err := os.WriteFile(passwordFile, []byte("foobar"), 0600); err != nil {
t.Error(err)
}
geth := runXDC(t, "account", "import", "--lightkdf", "-password", passwordFile, keyfile)
defer geth.ExpectExit()
geth.Expect(expected)
}

func TestWalletImportBadPassword(t *testing.T) {
XDC := runXDC(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
defer XDC.ExpectExit()
Expand All @@ -133,10 +169,11 @@ Fatal: could not decrypt key with given passphrase

func TestUnlockFlag(t *testing.T) {
datadir := tmpDatadirWithKeystore(t)
defer os.RemoveAll(datadir)
XDC := runXDC(t,
"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
"js", "testdata/empty.js")
"js", "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0",
"--port", "0", "--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
"testdata/empty.js")
XDC.Expect(`
Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
!! Unsupported terminal, password will be echoed.
Expand All @@ -157,6 +194,7 @@ Passphrase: {{.InputLine "foobar"}}

func TestUnlockFlagWrongPassword(t *testing.T) {
datadir := tmpDatadirWithKeystore(t)
defer os.RemoveAll(datadir)
XDC := runXDC(t,
"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
Expand All @@ -176,10 +214,11 @@ Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could
// https://github.com/XinFinOrg/XDPoSChain/issues/1785
func TestUnlockFlagMultiIndex(t *testing.T) {
datadir := tmpDatadirWithKeystore(t)
defer os.RemoveAll(datadir)
XDC := runXDC(t,
"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
"--unlock", "0,2",
"js", "testdata/empty.js")
"js", "--datadir", datadir, "--nat", "none", "--nodiscover",
"--maxpeers", "0", "--port", "0", "--unlock", "0,2",
"testdata/empty.js")
XDC.Expect(`
Unlocking account 0 | Attempt 1/3
!! Unsupported terminal, password will be echoed.
Expand All @@ -203,10 +242,11 @@ Passphrase: {{.InputLine "foobar"}}

func TestUnlockFlagPasswordFile(t *testing.T) {
datadir := tmpDatadirWithKeystore(t)
defer os.RemoveAll(datadir)
XDC := runXDC(t,
"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
"--password", "testdata/passwords.txt", "--unlock", "0,2",
"js", "testdata/empty.js")
"js", "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0",
"--port", "0", "--password", "testdata/passwords.txt", "--unlock", "0,2",
"testdata/empty.js")
XDC.ExpectExit()

wantMessages := []string{
Expand All @@ -223,6 +263,7 @@ func TestUnlockFlagPasswordFile(t *testing.T) {

func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) {
datadir := tmpDatadirWithKeystore(t)
defer os.RemoveAll(datadir)
XDC := runXDC(t,
"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
"--password", "testdata/wrong-passwords.txt", "--unlock", "0,2")
Expand All @@ -235,9 +276,9 @@ Fatal: Failed to unlock account 0 (could not decrypt key with given passphrase)
func TestUnlockFlagAmbiguous(t *testing.T) {
store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
XDC := runXDC(t,
"--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
"js", "testdata/empty.js")
"js", "--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0",
"--port", "0", "--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
"testdata/empty.js")
defer XDC.ExpectExit()

// Helper for the expect template, returns absolute keystore path.
Expand Down
9 changes: 3 additions & 6 deletions cmd/XDC/bugcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,14 @@ import (

"github.com/XinFinOrg/XDPoSChain/cmd/internal/browser"
"github.com/XinFinOrg/XDPoSChain/params"

"github.com/XinFinOrg/XDPoSChain/cmd/utils"
cli "gopkg.in/urfave/cli.v1"
"github.com/urfave/cli/v2"
)

var bugCommand = cli.Command{
Action: utils.MigrateFlags(reportBug),
var bugCommand = &cli.Command{
Action: reportBug,
Name: "bug",
Usage: "opens a window to report a bug on the XDC repo",
ArgsUsage: " ",
Category: "MISCELLANEOUS COMMANDS",
}

const issueUrl = "https://github.com/XinFinOrg/XDPoSChain/issues/new"
Expand Down
Loading

0 comments on commit 92e07ea

Please sign in to comment.