From 2dcc729c476453dddc0f966529251c61a4e077a9 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 16 Apr 2021 11:12:51 +0300 Subject: [PATCH 1/8] cli: use AddressFlag for `wallet sign` --- cli/multisig_test.go | 8 +++++++- cli/wallet/multisig.go | 10 +++++----- cli/wallet/wallet.go | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/cli/multisig_test.go b/cli/multisig_test.go index b3d57ac68..bbd95b3de 100644 --- a/cli/multisig_test.go +++ b/cli/multisig_test.go @@ -91,6 +91,12 @@ func TestSignMultisigTx(t *testing.T) { }) } + // missing address + e.RunWithError(t, "neo-go", "wallet", "sign", + "--rpc-endpoint", "http://"+e.RPC.Addr, + "--wallet", wallet2Path, + "--in", txPath, "--out", txPath) + e.In.WriteString("pass\r") e.Run(t, "neo-go", "wallet", "sign", "--rpc-endpoint", "http://"+e.RPC.Addr, @@ -127,7 +133,7 @@ func TestSignMultisigTx(t *testing.T) { e.In.WriteString("pass\r") e.Run(t, "neo-go", "contract", "invokefunction", "--rpc-endpoint", "http://"+e.RPC.Addr, - "--wallet", wallet1Path, "--address", multisigAddr, + "--wallet", wallet1Path, "--address", multisigHash.StringLE(), // test with scripthash instead of address "--out", txPath, e.Chain.GoverningTokenHash().StringLE(), "transfer", "bytes:"+multisigHash.StringBE(), diff --git a/cli/wallet/multisig.go b/cli/wallet/multisig.go index 0ff3d4349..974c3adae 100644 --- a/cli/wallet/multisig.go +++ b/cli/wallet/multisig.go @@ -3,6 +3,7 @@ package wallet import ( "fmt" + "github.com/nspcc-dev/neo-go/cli/flags" "github.com/nspcc-dev/neo-go/cli/options" "github.com/nspcc-dev/neo-go/cli/paramcontext" "github.com/nspcc-dev/neo-go/pkg/core/transaction" @@ -21,12 +22,11 @@ func signStoredTransaction(ctx *cli.Context) error { if err != nil { return cli.NewExitError(err, 1) } - addr := ctx.String("address") - sh, err := address.StringToUint160(addr) - if err != nil { - return cli.NewExitError(fmt.Errorf("invalid address: %w", err), 1) + addrFlag := ctx.Generic("address").(*flags.Address) + if !addrFlag.IsSet { + return cli.NewExitError("address was not provided", 1) } - acc, err := getDecryptedAccount(ctx, wall, sh) + acc, err := getDecryptedAccount(ctx, wall, addrFlag.Uint160()) if err != nil { return cli.NewExitError(err, 1) } diff --git a/cli/wallet/wallet.go b/cli/wallet/wallet.go index 7081e4937..a3cfd48a9 100644 --- a/cli/wallet/wallet.go +++ b/cli/wallet/wallet.go @@ -74,7 +74,7 @@ func NewCommands() []cli.Command { walletPathFlag, outFlag, inFlag, - cli.StringFlag{ + flags.AddressFlag{ Name: "address, a", Usage: "Address to use", }, From ff2b0516469d824f363b7e9282c2e3c195fd9045 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 16 Apr 2021 11:58:04 +0300 Subject: [PATCH 2/8] cli: use AddressFlag for nep17-related commands --- cli/nep17_test.go | 3 --- cli/wallet/nep17.go | 13 +++++-------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/cli/nep17_test.go b/cli/nep17_test.go index 6930cfe85..20e5bb984 100644 --- a/cli/nep17_test.go +++ b/cli/nep17_test.go @@ -98,9 +98,6 @@ func TestNEP17Balance(t *testing.T) { t.Run("Bad wallet", func(t *testing.T) { e.RunWithError(t, append(cmdbalance, "--wallet", "/dev/null")...) }) - t.Run("Bad address", func(t *testing.T) { - e.RunWithError(t, append(cmdbalance, "--rpc-endpoint", "http://"+e.RPC.Addr, "--wallet", validatorWallet, "--address", "xxx")...) - }) return } diff --git a/cli/wallet/nep17.go b/cli/wallet/nep17.go index 76096bb94..09ba5933c 100644 --- a/cli/wallet/nep17.go +++ b/cli/wallet/nep17.go @@ -32,7 +32,7 @@ func newNEP17Commands() []cli.Command { balanceFlags := []cli.Flag{ walletPathFlag, tokenFlag, - cli.StringFlag{ + flags.AddressFlag{ Name: "address, a", Usage: "Address to use", }, @@ -135,15 +135,12 @@ func getNEP17Balance(ctx *cli.Context) error { } defer wall.Close() - addr := ctx.String("address") - if addr != "" { - addrHash, err := address.StringToUint160(addr) - if err != nil { - return cli.NewExitError(fmt.Errorf("invalid address: %w", err), 1) - } + addrFlag := ctx.Generic("address").(*flags.Address) + if addrFlag.IsSet { + addrHash := addrFlag.Uint160() acc := wall.GetAccount(addrHash) if acc == nil { - return cli.NewExitError(fmt.Errorf("can't find account for the address: %s", addr), 1) + return cli.NewExitError(fmt.Errorf("can't find account for the address: %s", address.Uint160ToString(addrHash)), 1) } accounts = append(accounts, acc) } else { From 90b054afd0c457ea553a6a869f8a7e450c246c7a Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 16 Apr 2021 12:03:03 +0300 Subject: [PATCH 3/8] cli: check address flag is set for validator-related commands --- cli/candidate_test.go | 10 ++++++++++ cli/wallet/validator.go | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/cli/candidate_test.go b/cli/candidate_test.go index ff16ba0ba..f247bf9d0 100644 --- a/cli/candidate_test.go +++ b/cli/candidate_test.go @@ -23,6 +23,11 @@ func TestRegisterCandidate(t *testing.T) { "GAS:"+validatorPriv.Address()+":10000") e.checkTxPersisted(t) + // missing address + e.RunWithError(t, "neo-go", "wallet", "candidate", "register", + "--rpc-endpoint", "http://"+e.RPC.Addr, + "--wallet", validatorWallet) + e.In.WriteString("one\r") e.Run(t, "neo-go", "wallet", "candidate", "register", "--rpc-endpoint", "http://"+e.RPC.Addr, @@ -52,6 +57,11 @@ func TestRegisterCandidate(t *testing.T) { require.Equal(t, b, vs[0].Votes) }) + // missing address + e.RunWithError(t, "neo-go", "wallet", "candidate", "unregister", + "--rpc-endpoint", "http://"+e.RPC.Addr, + "--wallet", validatorWallet) + e.In.WriteString("one\r") e.Run(t, "neo-go", "wallet", "candidate", "unregister", "--rpc-endpoint", "http://"+e.RPC.Addr, diff --git a/cli/wallet/validator.go b/cli/wallet/validator.go index 0b6e3a28b..eaf3d492e 100644 --- a/cli/wallet/validator.go +++ b/cli/wallet/validator.go @@ -87,6 +87,9 @@ func handleCandidate(ctx *cli.Context, method string, sysGas int64) error { defer wall.Close() addrFlag := ctx.Generic("address").(*flags.Address) + if !addrFlag.IsSet { + return cli.NewExitError("address was not provided", 1) + } addr := addrFlag.Uint160() acc, err := getDecryptedAccount(ctx, wall, addr) if err != nil { @@ -139,6 +142,9 @@ func handleVote(ctx *cli.Context) error { defer wall.Close() addrFlag := ctx.Generic("address").(*flags.Address) + if !addrFlag.IsSet { + return cli.NewExitError("address was not provided", 1) + } addr := addrFlag.Uint160() acc, err := getDecryptedAccount(ctx, wall, addr) if err != nil { From df24c512625d7dcfcfcbf9118f964e2bee3c62d2 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 16 Apr 2021 12:09:59 +0300 Subject: [PATCH 4/8] cli: use AddressFlag for `calc-hash` --- cli/contract_test.go | 5 +++++ cli/smartcontract/smart_contract.go | 11 +++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cli/contract_test.go b/cli/contract_test.go index 31cab9456..f83d3c073 100644 --- a/cli/contract_test.go +++ b/cli/contract_test.go @@ -211,6 +211,11 @@ func TestComlileAndInvokeFunction(t *testing.T) { e.checkTxPersisted(t) t.Run("check calc hash", func(t *testing.T) { + // missing sender + e.RunWithError(t, "neo-go", "contract", "calc-hash", + "--in", nefName, + "--manifest", manifestName) + e.Run(t, "neo-go", "contract", "calc-hash", "--sender", validatorAddr, "--in", nefName, "--manifest", manifestName) diff --git a/cli/smartcontract/smart_contract.go b/cli/smartcontract/smart_contract.go index faad19f58..9436c4ad6 100644 --- a/cli/smartcontract/smart_contract.go +++ b/cli/smartcontract/smart_contract.go @@ -348,7 +348,7 @@ func NewCommands() []cli.Command { Usage: "calculates hash of a contract after deployment", Action: calcHash, Flags: []cli.Flag{ - cli.StringFlag{ + flags.AddressFlag{ Name: "sender, s", Usage: "sender script hash or address", }, @@ -466,10 +466,9 @@ func contractCompile(ctx *cli.Context) error { } func calcHash(ctx *cli.Context) error { - s := ctx.String("sender") - u, err := flags.ParseAddress(s) - if err != nil { - return cli.NewExitError(errors.New("invalid sender: must be either address or Uint160 in LE form"), 1) + sender := ctx.Generic("sender").(*flags.Address) + if !sender.IsSet { + return cli.NewExitError("sender is not set", 1) } p := ctx.String("in") @@ -497,7 +496,7 @@ func calcHash(ctx *cli.Context) error { if err != nil { return cli.NewExitError(fmt.Errorf("failed to restore manifest file: %w", err), 1) } - fmt.Fprintln(ctx.App.Writer, "Contract hash:", state.CreateContractHash(u, nefFile.Checksum, m.Name).StringLE()) + fmt.Fprintln(ctx.App.Writer, "Contract hash:", state.CreateContractHash(sender.Uint160(), nefFile.Checksum, m.Name).StringLE()) return nil } From 4edc62148b9016e99cc6723a92a727b43b5c631a Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 16 Apr 2021 12:15:00 +0300 Subject: [PATCH 5/8] cli: use AddressFlag for `dump-keys` --- cli/wallet/wallet.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/cli/wallet/wallet.go b/cli/wallet/wallet.go index a3cfd48a9..96c5a0c4a 100644 --- a/cli/wallet/wallet.go +++ b/cli/wallet/wallet.go @@ -137,7 +137,7 @@ func NewCommands() []cli.Command { Action: dumpKeys, Flags: []cli.Flag{ walletPathFlag, - cli.StringFlag{ + flags.AddressFlag{ Name: "address, a", Usage: "address to print public keys for", }, @@ -568,13 +568,10 @@ func dumpKeys(ctx *cli.Context) error { return cli.NewExitError(err, 1) } accounts := wall.Accounts - addr := ctx.String("address") - if addr != "" { - u, err := flags.ParseAddress(addr) - if err != nil { - return cli.NewExitError(fmt.Errorf("invalid address: %w", err), 1) - } - acc := wall.GetAccount(u) + + addrFlag := ctx.Generic("address").(*flags.Address) + if addrFlag.IsSet { + acc := wall.GetAccount(addrFlag.Uint160()) if acc == nil { return cli.NewExitError("account is missing", 1) } @@ -605,8 +602,8 @@ func dumpKeys(ctx *cli.Context) error { hasPrinted = true continue } - if addr != "" { - return cli.NewExitError(fmt.Errorf("Unknown script type for address %s", addr), 1) + if addrFlag.IsSet { + return cli.NewExitError(fmt.Errorf("Unknown script type for address %s", address.Uint160ToString(addrFlag.Uint160())), 1) } } return nil From 87f950a7199430031214d5b899d97b399ae686bf Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 16 Apr 2021 12:15:51 +0300 Subject: [PATCH 6/8] cli: format error message --- cli/wallet/wallet.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/wallet/wallet.go b/cli/wallet/wallet.go index 96c5a0c4a..5568ab23e 100644 --- a/cli/wallet/wallet.go +++ b/cli/wallet/wallet.go @@ -603,7 +603,7 @@ func dumpKeys(ctx *cli.Context) error { continue } if addrFlag.IsSet { - return cli.NewExitError(fmt.Errorf("Unknown script type for address %s", address.Uint160ToString(addrFlag.Uint160())), 1) + return cli.NewExitError(fmt.Errorf("unknown script type for address %s", address.Uint160ToString(addrFlag.Uint160())), 1) } } return nil From 92c6708fa04a7fd8f10ee800ac8c5a6af4c8363b Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 16 Apr 2021 12:23:46 +0300 Subject: [PATCH 7/8] cli: use AddressFlag for `import-deployed` --- cli/wallet/wallet.go | 13 ++++++------- cli/wallet_test.go | 5 +++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/cli/wallet/wallet.go b/cli/wallet/wallet.go index 5568ab23e..b0411ad4c 100644 --- a/cli/wallet/wallet.go +++ b/cli/wallet/wallet.go @@ -197,9 +197,9 @@ func NewCommands() []cli.Command { Flags: append([]cli.Flag{ walletPathFlag, wifFlag, - cli.StringFlag{ + flags.AddressFlag{ Name: "contract, c", - Usage: "Contract hash", + Usage: "Contract hash or address", }, }, options.RPC...), }, @@ -421,10 +421,9 @@ func importDeployed(ctx *cli.Context) error { defer wall.Close() - rawHash := ctx.String("contract") - h, err := flags.ParseAddress(rawHash) - if err != nil { - return cli.NewExitError(fmt.Errorf("invalid contract hash: %w", err), 1) + rawHash := ctx.Generic("contract").(*flags.Address) + if !rawHash.IsSet { + return cli.NewExitError("contract hash was not provided", 1) } acc, err := newAccountFromWIF(ctx.App.Writer, ctx.String("wif")) @@ -440,7 +439,7 @@ func importDeployed(ctx *cli.Context) error { return cli.NewExitError(err, 1) } - cs, err := c.GetContractStateByHash(h) + cs, err := c.GetContractStateByHash(rawHash.Uint160()) if err != nil { return cli.NewExitError(fmt.Errorf("can't fetch contract info: %w", err), 1) } diff --git a/cli/wallet_test.go b/cli/wallet_test.go index 67d44fa4b..e780e32f6 100644 --- a/cli/wallet_test.go +++ b/cli/wallet_test.go @@ -267,6 +267,11 @@ func TestImportDeployed(t *testing.T) { priv, err := keys.NewPrivateKey() require.NoError(t, err) + // missing contract sh + e.RunWithError(t, "neo-go", "wallet", "import-deployed", + "--rpc-endpoint", "http://"+e.RPC.Addr, + "--wallet", walletPath, "--wif", priv.WIF()) + e.In.WriteString("acc\rpass\rpass\r") e.Run(t, "neo-go", "wallet", "import-deployed", "--rpc-endpoint", "http://"+e.RPC.Addr, From ced90e05108cd67c67d671f08775ebddfdecd9dd Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 16 Apr 2021 12:29:46 +0300 Subject: [PATCH 8/8] cli: use AddressFlag for `nep17 import` --- cli/nep17_test.go | 8 +++++++- cli/wallet/nep17.go | 11 ++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/cli/nep17_test.go b/cli/nep17_test.go index 20e5bb984..1f7b9c824 100644 --- a/cli/nep17_test.go +++ b/cli/nep17_test.go @@ -200,6 +200,12 @@ func TestNEP17ImportToken(t *testing.T) { gasContractHash, err := e.Chain.GetNativeContractScriptHash(nativenames.Gas) require.NoError(t, err) e.Run(t, "neo-go", "wallet", "init", "--wallet", walletPath) + + // missing token hash + e.RunWithError(t, "neo-go", "wallet", "nep17", "import", + "--rpc-endpoint", "http://"+e.RPC.Addr, + "--wallet", walletPath) + e.Run(t, "neo-go", "wallet", "nep17", "import", "--rpc-endpoint", "http://"+e.RPC.Addr, "--wallet", walletPath, @@ -207,7 +213,7 @@ func TestNEP17ImportToken(t *testing.T) { e.Run(t, "neo-go", "wallet", "nep17", "import", "--rpc-endpoint", "http://"+e.RPC.Addr, "--wallet", walletPath, - "--token", neoContractHash.StringLE()) + "--token", address.Uint160ToString(neoContractHash)) // try address instead of sh t.Run("Info", func(t *testing.T) { checkGASInfo := func(t *testing.T) { diff --git a/cli/wallet/nep17.go b/cli/wallet/nep17.go index 09ba5933c..a2185946a 100644 --- a/cli/wallet/nep17.go +++ b/cli/wallet/nep17.go @@ -40,9 +40,9 @@ func newNEP17Commands() []cli.Command { balanceFlags = append(balanceFlags, options.RPC...) importFlags := []cli.Flag{ walletPathFlag, - cli.StringFlag{ + flags.AddressFlag{ Name: "token", - Usage: "Token contract hash in LE", + Usage: "Token contract address or hash in LE", }, } importFlags = append(importFlags, options.RPC...) @@ -257,10 +257,11 @@ func importNEP17Token(ctx *cli.Context) error { } defer wall.Close() - tokenHash, err := flags.ParseAddress(ctx.String("token")) - if err != nil { - return cli.NewExitError(fmt.Errorf("invalid token contract hash: %w", err), 1) + tokenHashFlag := ctx.Generic("token").(*flags.Address) + if !tokenHashFlag.IsSet { + return cli.NewExitError("token contract hash was not set", 1) } + tokenHash := tokenHashFlag.Uint160() for _, t := range wall.Extra.Tokens { if t.Hash.Equals(tokenHash) {