Merge pull request #1911 from nspcc-dev/cli/refactoring

cli: inspect and refactor wallet-related commands
This commit is contained in:
Roman Khimov 2021-04-21 17:17:18 +03:00 committed by GitHub
commit c35171d1c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 21 deletions

View file

@ -154,9 +154,10 @@ func NewCommands() []cli.Command {
}, },
}, },
{ {
Name: "import", Name: "import",
Usage: "import WIF", Usage: "import WIF of a standard signature contract",
Action: importWallet, UsageText: "import --wallet <path> --wif <wif> [--name <account_name>]",
Action: importWallet,
Flags: []cli.Flag{ Flags: []cli.Flag{
walletPathFlag, walletPathFlag,
wifFlag, wifFlag,
@ -173,7 +174,7 @@ func NewCommands() []cli.Command {
{ {
Name: "import-multisig", Name: "import-multisig",
Usage: "import multisig contract", Usage: "import multisig contract",
UsageText: "import-multisig --wallet <path> --wif <wif> --min <n>" + UsageText: "import-multisig --wallet <path> --wif <wif> [--name <account_name>] --min <n>" +
" [<pubkey1> [<pubkey2> [...]]]", " [<pubkey1> [<pubkey2> [...]]]",
Action: importMultisig, Action: importMultisig,
Flags: []cli.Flag{ Flags: []cli.Flag{
@ -192,11 +193,15 @@ func NewCommands() []cli.Command {
{ {
Name: "import-deployed", Name: "import-deployed",
Usage: "import deployed contract", Usage: "import deployed contract",
UsageText: "import-multisig --wallet <path> --wif <wif> --contract <hash>", UsageText: "import-deployed --wallet <path> --wif <wif> --contract <hash> [--name <account_name>]",
Action: importDeployed, Action: importDeployed,
Flags: append([]cli.Flag{ Flags: append([]cli.Flag{
walletPathFlag, walletPathFlag,
wifFlag, wifFlag,
cli.StringFlag{
Name: "name, n",
Usage: "Optional account name",
},
flags.AddressFlag{ flags.AddressFlag{
Name: "contract, c", Name: "contract, c",
Usage: "Contract hash or address", Usage: "Contract hash or address",
@ -206,17 +211,21 @@ func NewCommands() []cli.Command {
{ {
Name: "remove", Name: "remove",
Usage: "remove an account from the wallet", Usage: "remove an account from the wallet",
UsageText: "remove --wallet <path> [--force] <addr>", UsageText: "remove --wallet <path> [--force] --address <addr>",
Action: removeAccount, Action: removeAccount,
Flags: []cli.Flag{ Flags: []cli.Flag{
walletPathFlag, walletPathFlag,
forceFlag, forceFlag,
flags.AddressFlag{
Name: "address, a",
Usage: "Account address or hash in LE form to be removed",
},
}, },
}, },
{ {
Name: "sign", Name: "sign",
Usage: "cosign transaction with multisig/contract/additional account", Usage: "cosign transaction with multisig/contract/additional account",
UsageText: "sign --wallet <path> --address <address> --in <file.in> --out <file.out>", UsageText: "sign --wallet <path> --address <address> --in <file.in> --out <file.out> [-r <endpoint>]",
Action: signStoredTransaction, Action: signStoredTransaction,
Flags: signFlags, Flags: signFlags,
}, },
@ -309,7 +318,7 @@ func addAccount(ctx *cli.Context) error {
defer wall.Close() defer wall.Close()
if err := createAccount(ctx, wall); err != nil { if err := createAccount(wall); err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
@ -406,6 +415,9 @@ func importMultisig(ctx *cli.Context) error {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
if acc.Label == "" {
acc.Label = ctx.String("name")
}
if err := addAccountAndSave(wall, acc); err != nil { if err := addAccountAndSave(wall, acc); err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
@ -458,6 +470,9 @@ func importDeployed(ctx *cli.Context) error {
} }
acc.Contract.Deployed = true acc.Contract.Deployed = true
if acc.Label == "" {
acc.Label = ctx.String("name")
}
if err := addAccountAndSave(wall, acc); err != nil { if err := addAccountAndSave(wall, acc); err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
@ -502,18 +517,17 @@ func removeAccount(ctx *cli.Context) error {
} }
defer wall.Close() defer wall.Close()
addrArg := ctx.Args().First() addr := ctx.Generic("address").(*flags.Address)
addr, err := address.StringToUint160(addrArg) if !addr.IsSet {
if err != nil { cli.NewExitError("valid account address must be provided", 1)
return cli.NewExitError("valid address must be provided", 1)
} }
acc := wall.GetAccount(addr) acc := wall.GetAccount(addr.Uint160())
if acc == nil { if acc == nil {
return cli.NewExitError("account wasn't found", 1) return cli.NewExitError("account wasn't found", 1)
} }
if !ctx.Bool("force") { if !ctx.Bool("force") {
fmt.Fprintf(ctx.App.Writer, "Account %s will be removed. This action is irreversible.\n", addrArg) fmt.Fprintf(ctx.App.Writer, "Account %s will be removed. This action is irreversible.\n", addr.Uint160())
if ok := askForConsent(ctx.App.Writer); !ok { if ok := askForConsent(ctx.App.Writer); !ok {
return nil return nil
} }
@ -622,7 +636,7 @@ func createWallet(ctx *cli.Context) error {
} }
if ctx.Bool("account") { if ctx.Bool("account") {
if err := createAccount(ctx, wall); err != nil { if err := createAccount(wall); err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
} }
@ -632,7 +646,7 @@ func createWallet(ctx *cli.Context) error {
return nil return nil
} }
func readAccountInfo(w io.Writer) (string, string, error) { func readAccountInfo() (string, string, error) {
rawName, _ := input.ReadLine("Enter the name of the account > ") rawName, _ := input.ReadLine("Enter the name of the account > ")
phrase, err := input.ReadPassword("Enter passphrase > ") phrase, err := input.ReadPassword("Enter passphrase > ")
if err != nil { if err != nil {
@ -647,12 +661,12 @@ func readAccountInfo(w io.Writer) (string, string, error) {
return "", "", errPhraseMismatch return "", "", errPhraseMismatch
} }
name := strings.TrimRight(string(rawName), "\n") name := strings.TrimRight(rawName, "\n")
return name, phrase, nil return name, phrase, nil
} }
func createAccount(ctx *cli.Context, wall *wallet.Wallet) error { func createAccount(wall *wallet.Wallet) error {
name, phrase, err := readAccountInfo(ctx.App.Writer) name, phrase, err := readAccountInfo()
if err != nil { if err != nil {
return err return err
} }
@ -684,7 +698,7 @@ func newAccountFromWIF(w io.Writer, wif string) (*wallet.Account, error) {
} }
fmt.Fprintln(w, "Provided WIF was unencrypted. Wallet can contain only encrypted keys.") fmt.Fprintln(w, "Provided WIF was unencrypted. Wallet can contain only encrypted keys.")
name, pass, err := readAccountInfo(w) name, pass, err := readAccountInfo()
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -71,7 +71,7 @@ func TestWalletInit(t *testing.T) {
addr := w.Accounts[0].Address addr := w.Accounts[0].Address
e.In.WriteString("y\r") e.In.WriteString("y\r")
e.Run(t, "neo-go", "wallet", "remove", e.Run(t, "neo-go", "wallet", "remove",
"--wallet", walletPath, addr) "--wallet", walletPath, "--address", addr)
w, err := wallet.NewWalletFromFile(walletPath) w, err := wallet.NewWalletFromFile(walletPath)
require.NoError(t, err) require.NoError(t, err)
require.Nil(t, w.GetAccount(sh)) require.Nil(t, w.GetAccount(sh))