From ffeba838c89ff3cd8682fe5d6c70a3155fbc4f3b Mon Sep 17 00:00:00 2001 From: LuckyPigeon Date: Thu, 4 Aug 2022 07:15:18 +0800 Subject: [PATCH 01/11] [ioctl] build did command line into new ioctl --- ioctl/newcmd/did/did.go | 35 +++++++++++++++++++++++++++++++++++ ioctl/newcmd/did/did_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 ioctl/newcmd/did/did.go create mode 100644 ioctl/newcmd/did/did_test.go diff --git a/ioctl/newcmd/did/did.go b/ioctl/newcmd/did/did.go new file mode 100644 index 0000000000..a1b98d0f4c --- /dev/null +++ b/ioctl/newcmd/did/did.go @@ -0,0 +1,35 @@ +// Copyright (c) 2022 IoTeX Foundation +// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no +// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent +// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache +// License 2.0 that can be found in the LICENSE file. + +package did + +import ( + "github.com/iotexproject/iotex-core/ioctl" + "github.com/iotexproject/iotex-core/ioctl/config" + "github.com/spf13/cobra" +) + +// Multi-language support +var ( + _dIDCmdShorts = map[config.Language]string{ + config.English: "DID command", + config.Chinese: "DID command", + } +) + +// NewDidCmd represents the did command +func NewDidCmd(client ioctl.Client) *cobra.Command { + short, _ := client.SelectTranslation(_dIDCmdShorts) + + cmd := &cobra.Command{ + Use: "did", + Short: short, + } + + client.SetEndpointWithFlag(cmd.PersistentFlags().StringVar) + client.SetInsecureWithFlag(cmd.PersistentFlags().BoolVar) + return cmd +} diff --git a/ioctl/newcmd/did/did_test.go b/ioctl/newcmd/did/did_test.go new file mode 100644 index 0000000000..f1d995d38a --- /dev/null +++ b/ioctl/newcmd/did/did_test.go @@ -0,0 +1,32 @@ +// Copyright (c) 2022 IoTeX Foundation +// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no +// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent +// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache +// License 2.0 that can be found in the LICENSE file. + +package did + +import ( + "testing" + + "github.com/golang/mock/gomock" + "github.com/iotexproject/iotex-core/ioctl/config" + "github.com/iotexproject/iotex-core/ioctl/util" + "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" + "github.com/stretchr/testify/require" +) + +func TestNewDidCmd(t *testing.T) { + require := require.New(t) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + client := mock_ioctlclient.NewMockClient(ctrl) + + client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes() + client.EXPECT().SetEndpointWithFlag(gomock.Any()).Do(func(_ func(*string, string, string, string)) {}) + client.EXPECT().SetInsecureWithFlag(gomock.Any()).Do(func(_ func(*bool, string, bool, string)) {}) + + cmd := NewDidCmd(client) + _, err := util.ExecuteCmd(cmd) + require.NoError(err) +} From dcdc832d760de7c728c5ea642b3fedf7709351c7 Mon Sep 17 00:00:00 2001 From: LuckyPigeon Date: Thu, 4 Aug 2022 08:35:00 +0800 Subject: [PATCH 02/11] [ioctl] build did register command line into new ioctl --- ioctl/newcmd/did/didregister.go | 81 +++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 ioctl/newcmd/did/didregister.go diff --git a/ioctl/newcmd/did/didregister.go b/ioctl/newcmd/did/didregister.go new file mode 100644 index 0000000000..a95eb79b60 --- /dev/null +++ b/ioctl/newcmd/did/didregister.go @@ -0,0 +1,81 @@ +// Copyright (c) 2022 IoTeX Foundation +// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no +// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent +// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache +// License 2.0 that can be found in the LICENSE file. + +package did + +import ( + "encoding/hex" + "math/big" + "strings" + + "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/pkg/errors" + "github.com/spf13/cobra" + + "github.com/iotexproject/iotex-core/ioctl" + "github.com/iotexproject/iotex-core/ioctl/config" + "github.com/iotexproject/iotex-core/ioctl/newcmd/action" +) + +const ( + _registerDIDName = "registerDID" + _getHashName = "getHash" + _getURIName = "getURI" + _updateDIDName = "updateDID" + _deregisterDIDName = "deregisterDID" + // DIDABI is the did abi + DIDABI = `[{"constant": false,"inputs": [],"name": "deregisterDID","outputs": [],"payable": false,"stateMutability": "nonpayable","type": "function"},{"constant": true,"inputs": [{"internalType": "bytes","name": "did","type": "bytes"}],"name": "getHash","outputs": [{"internalType": "bytes32","name": "","type": "bytes32"}],"payable": false,"stateMutability": "view","type": "function"}, {"constant": true,"inputs": [{"internalType": "bytes","name": "did","type": "bytes"}],"name": "getURI","outputs": [{"internalType": "bytes","name": "","type": "bytes"}],"payable": false,"stateMutability": "view","type": "function"},{"constant": false,"inputs": [{"internalType": "bytes32","name": "h","type": "bytes32"},{"internalType": "bytes","name": "uri","type": "bytes"}],"name": "registerDID","outputs": [],"payable": false,"stateMutability": "nonpayable","type": "function"},{"constant": false,"inputs": [{"internalType": "bytes32","name": "h","type": "bytes32"},{"internalType": "bytes","name": "uri","type": "bytes"}],"name": "updateDID","outputs": [],"payable": false,"stateMutability": "nonpayable","type": "function"}]` +) + +// Multi-language support +var ( + _registerCmdUses = map[config.Language]string{ + config.English: "register (CONTRACT_ADDRESS|ALIAS) hash uri", + config.Chinese: "register (合约地址|别名) hash uri", + } + _registerCmdShorts = map[config.Language]string{ + config.English: "Register DID on IoTeX blockchain", + config.Chinese: "Register 在IoTeX链上注册DID", + } +) + +// NewDidRegisterCmd represents the did register command +func NewDidRegisterCmd(client ioctl.Client) *cobra.Command { + cmd := &cobra.Command{ + Use: config.TranslateInLang(_registerCmdUses, config.UILanguage), + Short: config.TranslateInLang(_registerCmdShorts, config.UILanguage), + Args: cobra.ExactArgs(3), + RunE: func(cmd *cobra.Command, args []string) error { + cmd.SilenceUsage = true + contract, err := client.Address(args[0]) + if err != nil { + return errors.Wrap(err, "failed to get contract address") + } + + hashSlice, err := hex.DecodeString(args[1]) + if err != nil { + return errors.Wrap(err, "failed to decode data") + } + var hashArray [32]byte + copy(hashArray[:], hashSlice) + abi, err := abi.JSON(strings.NewReader(DIDABI)) + if err != nil { + return errors.Wrap(err, "falied to parse abi") + } + _, exist := abi.Methods[_registerDIDName] + if !exist { + return errors.New("method is not found") + } + bytecode, err := abi.Pack(_registerDIDName, hashArray, []byte(args[2])) + if err != nil { + return errors.Wrap(err, "invalid bytecode") + } + return action.Execute(client, cmd, contract, big.NewInt(0), bytecode) + }, + } + action.RegisterWriteCommand(client, cmd) + return cmd +} From 25c88beb2317f23a34fa306ca6f61bc20fe8e552 Mon Sep 17 00:00:00 2001 From: LuckyPigeon Date: Thu, 4 Aug 2022 08:37:17 +0800 Subject: [PATCH 03/11] replace use & short with client interface --- ioctl/newcmd/did/didregister.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ioctl/newcmd/did/didregister.go b/ioctl/newcmd/did/didregister.go index a95eb79b60..b1ebc2b13c 100644 --- a/ioctl/newcmd/did/didregister.go +++ b/ioctl/newcmd/did/didregister.go @@ -44,9 +44,12 @@ var ( // NewDidRegisterCmd represents the did register command func NewDidRegisterCmd(client ioctl.Client) *cobra.Command { + use, _ := client.SelectTranslation(_registerCmdUses) + short, _ := client.SelectTranslation(_registerCmdShorts) + cmd := &cobra.Command{ - Use: config.TranslateInLang(_registerCmdUses, config.UILanguage), - Short: config.TranslateInLang(_registerCmdShorts, config.UILanguage), + Use: use, + Short: short, Args: cobra.ExactArgs(3), RunE: func(cmd *cobra.Command, args []string) error { cmd.SilenceUsage = true From e6ad8712b2369ea6f462231dcd4d1d0b0cce617a Mon Sep 17 00:00:00 2001 From: LuckyPigeon Date: Thu, 4 Aug 2022 08:58:09 +0800 Subject: [PATCH 04/11] build unittest to cover the modification --- ioctl/newcmd/did/didregister_test.go | 83 ++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 ioctl/newcmd/did/didregister_test.go diff --git a/ioctl/newcmd/did/didregister_test.go b/ioctl/newcmd/did/didregister_test.go new file mode 100644 index 0000000000..ca344fad0d --- /dev/null +++ b/ioctl/newcmd/did/didregister_test.go @@ -0,0 +1,83 @@ +// Copyright (c) 2022 IoTeX Foundation +// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no +// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent +// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache +// License 2.0 that can be found in the LICENSE file. + +package did + +import ( + "testing" + + "github.com/ethereum/go-ethereum/accounts/keystore" + "github.com/golang/mock/gomock" + "github.com/iotexproject/iotex-address/address" + "github.com/iotexproject/iotex-core/ioctl/config" + "github.com/iotexproject/iotex-core/ioctl/util" + "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" + "github.com/iotexproject/iotex-proto/golang/iotexapi" + "github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi" + "github.com/iotexproject/iotex-proto/golang/iotextypes" + "github.com/pkg/errors" + "github.com/stretchr/testify/require" +) + +func TestNewJwtSignCmd(t *testing.T) { + require := require.New(t) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + client := mock_ioctlclient.NewMockClient(ctrl) + apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl) + payload := "0a10080118a08d062202313062040a023130124104dc4c548c3a478278a6a09ffa8b5c4b384368e49654b35a6961ee8288fc889cdc39e9f8194e41abdbfac248ef9dc3f37b131a36ee2c052d974c21c1d2cd56730b1a4161e219c2c5d5987f8a9efa33e8df0cde9d5541689fff05784cdc24f12e9d9ee8283a5aa720f494b949535b7969c07633dfb68c4ef9359eb16edb9abc6ebfadc801" + + ks := keystore.NewKeyStore(t.TempDir(), 2, 1) + acc, err := ks.NewAccount("") + require.NoError(err) + accAddr, err := address.FromBytes(acc.Address.Bytes()) + require.NoError(err) + + client.EXPECT().SelectTranslation(gomock.Any()).Return("did", config.English).AnyTimes() + client.EXPECT().Address(gomock.Any()).Return(accAddr.String(), nil).Times(3) + client.EXPECT().Alias(gomock.Any()).Return("producer", nil).AnyTimes() + client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).AnyTimes() + client.EXPECT().IsCryptoSm2().Return(false).Times(3) + client.EXPECT().ReadSecret().Return("", nil).Times(1) + client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return(accAddr.String(), nil).Times(1) + client.EXPECT().NewKeyStore().Return(ks).Times(2) + client.EXPECT().AskToConfirm(gomock.Any()).Return(true, nil).Times(1) + client.EXPECT().Config().Return(config.Config{ + Explorer: "iotexscan", + Endpoint: "testnet1", + }).Times(2) + + accountResp := &iotexapi.GetAccountResponse{ + AccountMeta: &iotextypes.AccountMeta{ + IsContract: false, + PendingNonce: 10, + Balance: "100000000000000000000", + }, + } + chainMetaResp := &iotexapi.GetChainMetaResponse{ + ChainMeta: &iotextypes.ChainMeta{ + ChainID: 0, + }, + } + sendActionResp := &iotexapi.SendActionResponse{} + apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(accountResp, nil).Times(2) + apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(chainMetaResp, nil).Times(1) + apiServiceClient.EXPECT().SendAction(gomock.Any(), gomock.Any()).Return(sendActionResp, nil).Times(1) + + t.Run("did register", func(t *testing.T) { + cmd := NewDidRegisterCmd(client) + _, err := util.ExecuteCmd(cmd, accAddr.String(), payload, "test", "--signer", accAddr.String()) + require.NoError(err) + }) + + t.Run("failed to decode data", func(t *testing.T) { + expectedErr := errors.New("failed to decode data") + + cmd := NewDidRegisterCmd(client) + _, err := util.ExecuteCmd(cmd, accAddr.String(), "test", "test", "--signer", accAddr.String()) + require.Contains(err.Error(), expectedErr.Error()) + }) +} From 6ef58187d219e035a5fc730d3b3a304978a5ecce Mon Sep 17 00:00:00 2001 From: LuckyPigeon Date: Fri, 5 Aug 2022 04:25:15 +0800 Subject: [PATCH 05/11] update short description --- ioctl/newcmd/did/did.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/ioctl/newcmd/did/did.go b/ioctl/newcmd/did/did.go index a1b98d0f4c..c53935d2e3 100644 --- a/ioctl/newcmd/did/did.go +++ b/ioctl/newcmd/did/did.go @@ -7,23 +7,33 @@ package did import ( + "github.com/spf13/cobra" + "github.com/iotexproject/iotex-core/ioctl" "github.com/iotexproject/iotex-core/ioctl/config" - "github.com/spf13/cobra" ) // Multi-language support var ( _dIDCmdShorts = map[config.Language]string{ - config.English: "DID command", - config.Chinese: "DID command", + config.English: "Manage Decentralized Identity of IoTeX blockchain", + config.Chinese: "管理IoTeX区块链上的去中心化数字身份", } ) +const ( + _registerDIDName = "registerDID" + _getHashName = "getHash" + _getURIName = "getURI" + _updateDIDName = "updateDID" + _deregisterDIDName = "deregisterDID" + // DIDABI is the did abi + DIDABI = `[{"constant": false,"inputs": [],"name": "deregisterDID","outputs": [],"payable": false,"stateMutability": "nonpayable","type": "function"},{"constant": true,"inputs": [{"internalType": "bytes","name": "did","type": "bytes"}],"name": "getHash","outputs": [{"internalType": "bytes32","name": "","type": "bytes32"}],"payable": false,"stateMutability": "view","type": "function"}, {"constant": true,"inputs": [{"internalType": "bytes","name": "did","type": "bytes"}],"name": "getURI","outputs": [{"internalType": "bytes","name": "","type": "bytes"}],"payable": false,"stateMutability": "view","type": "function"},{"constant": false,"inputs": [{"internalType": "bytes32","name": "h","type": "bytes32"},{"internalType": "bytes","name": "uri","type": "bytes"}],"name": "registerDID","outputs": [],"payable": false,"stateMutability": "nonpayable","type": "function"},{"constant": false,"inputs": [{"internalType": "bytes32","name": "h","type": "bytes32"},{"internalType": "bytes","name": "uri","type": "bytes"}],"name": "updateDID","outputs": [],"payable": false,"stateMutability": "nonpayable","type": "function"}]` +) + // NewDidCmd represents the did command func NewDidCmd(client ioctl.Client) *cobra.Command { short, _ := client.SelectTranslation(_dIDCmdShorts) - cmd := &cobra.Command{ Use: "did", Short: short, From b6eab0035056d86c1e5dcbfd9f4a6aa929dd3550 Mon Sep 17 00:00:00 2001 From: LuckyPigeon Date: Fri, 5 Aug 2022 04:26:28 +0800 Subject: [PATCH 06/11] align all abi methods --- ioctl/newcmd/did/didregister.go | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/ioctl/newcmd/did/didregister.go b/ioctl/newcmd/did/didregister.go index b1ebc2b13c..b3b613d78e 100644 --- a/ioctl/newcmd/did/didregister.go +++ b/ioctl/newcmd/did/didregister.go @@ -20,16 +20,6 @@ import ( "github.com/iotexproject/iotex-core/ioctl/newcmd/action" ) -const ( - _registerDIDName = "registerDID" - _getHashName = "getHash" - _getURIName = "getURI" - _updateDIDName = "updateDID" - _deregisterDIDName = "deregisterDID" - // DIDABI is the did abi - DIDABI = `[{"constant": false,"inputs": [],"name": "deregisterDID","outputs": [],"payable": false,"stateMutability": "nonpayable","type": "function"},{"constant": true,"inputs": [{"internalType": "bytes","name": "did","type": "bytes"}],"name": "getHash","outputs": [{"internalType": "bytes32","name": "","type": "bytes32"}],"payable": false,"stateMutability": "view","type": "function"}, {"constant": true,"inputs": [{"internalType": "bytes","name": "did","type": "bytes"}],"name": "getURI","outputs": [{"internalType": "bytes","name": "","type": "bytes"}],"payable": false,"stateMutability": "view","type": "function"},{"constant": false,"inputs": [{"internalType": "bytes32","name": "h","type": "bytes32"},{"internalType": "bytes","name": "uri","type": "bytes"}],"name": "registerDID","outputs": [],"payable": false,"stateMutability": "nonpayable","type": "function"},{"constant": false,"inputs": [{"internalType": "bytes32","name": "h","type": "bytes32"},{"internalType": "bytes","name": "uri","type": "bytes"}],"name": "updateDID","outputs": [],"payable": false,"stateMutability": "nonpayable","type": "function"}]` -) - // Multi-language support var ( _registerCmdUses = map[config.Language]string{ @@ -64,18 +54,8 @@ func NewDidRegisterCmd(client ioctl.Client) *cobra.Command { } var hashArray [32]byte copy(hashArray[:], hashSlice) - abi, err := abi.JSON(strings.NewReader(DIDABI)) - if err != nil { - return errors.Wrap(err, "falied to parse abi") - } - _, exist := abi.Methods[_registerDIDName] - if !exist { - return errors.New("method is not found") - } - bytecode, err := abi.Pack(_registerDIDName, hashArray, []byte(args[2])) - if err != nil { - return errors.Wrap(err, "invalid bytecode") - } + abi, _ := abi.JSON(strings.NewReader(DIDABI)) + bytecode, _ := abi.Pack(_registerDIDName, hashArray, []byte(args[2])) return action.Execute(client, cmd, contract, big.NewInt(0), bytecode) }, } From 21e6d9879926263e74bd23a9701550dce8aeb7e3 Mon Sep 17 00:00:00 2001 From: LuckyPigeon Date: Fri, 5 Aug 2022 04:26:54 +0800 Subject: [PATCH 07/11] check result --- ioctl/newcmd/did/didregister_test.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ioctl/newcmd/did/didregister_test.go b/ioctl/newcmd/did/didregister_test.go index ca344fad0d..00d7a60691 100644 --- a/ioctl/newcmd/did/didregister_test.go +++ b/ioctl/newcmd/did/didregister_test.go @@ -12,14 +12,15 @@ import ( "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/golang/mock/gomock" "github.com/iotexproject/iotex-address/address" - "github.com/iotexproject/iotex-core/ioctl/config" - "github.com/iotexproject/iotex-core/ioctl/util" - "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" "github.com/iotexproject/iotex-proto/golang/iotexapi" "github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi" "github.com/iotexproject/iotex-proto/golang/iotextypes" "github.com/pkg/errors" "github.com/stretchr/testify/require" + + "github.com/iotexproject/iotex-core/ioctl/config" + "github.com/iotexproject/iotex-core/ioctl/util" + "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" ) func TestNewJwtSignCmd(t *testing.T) { @@ -69,8 +70,9 @@ func TestNewJwtSignCmd(t *testing.T) { t.Run("did register", func(t *testing.T) { cmd := NewDidRegisterCmd(client) - _, err := util.ExecuteCmd(cmd, accAddr.String(), payload, "test", "--signer", accAddr.String()) + result, err := util.ExecuteCmd(cmd, accAddr.String(), payload, "test", "--signer", accAddr.String()) require.NoError(err) + require.Contains(result, "Action has been sent to blockchain") }) t.Run("failed to decode data", func(t *testing.T) { From aa594dfd634e479912a7dca121f82b5af61840fa Mon Sep 17 00:00:00 2001 From: LuckyPigeon Date: Fri, 5 Aug 2022 04:27:32 +0800 Subject: [PATCH 08/11] check did unittest result --- ioctl/newcmd/did/did.go | 2 +- ioctl/newcmd/did/did_test.go | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/ioctl/newcmd/did/did.go b/ioctl/newcmd/did/did.go index c53935d2e3..9a3f6885e7 100644 --- a/ioctl/newcmd/did/did.go +++ b/ioctl/newcmd/did/did.go @@ -38,7 +38,7 @@ func NewDidCmd(client ioctl.Client) *cobra.Command { Use: "did", Short: short, } - + cmd.AddCommand(NewDidRegisterCmd(client)) client.SetEndpointWithFlag(cmd.PersistentFlags().StringVar) client.SetInsecureWithFlag(cmd.PersistentFlags().BoolVar) return cmd diff --git a/ioctl/newcmd/did/did_test.go b/ioctl/newcmd/did/did_test.go index f1d995d38a..567a7afca4 100644 --- a/ioctl/newcmd/did/did_test.go +++ b/ioctl/newcmd/did/did_test.go @@ -10,10 +10,11 @@ import ( "testing" "github.com/golang/mock/gomock" + "github.com/stretchr/testify/require" + "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/util" "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" - "github.com/stretchr/testify/require" ) func TestNewDidCmd(t *testing.T) { @@ -22,11 +23,12 @@ func TestNewDidCmd(t *testing.T) { defer ctrl.Finish() client := mock_ioctlclient.NewMockClient(ctrl) - client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes() + client.EXPECT().SelectTranslation(gomock.Any()).Return("did", config.English).AnyTimes() client.EXPECT().SetEndpointWithFlag(gomock.Any()).Do(func(_ func(*string, string, string, string)) {}) client.EXPECT().SetInsecureWithFlag(gomock.Any()).Do(func(_ func(*bool, string, bool, string)) {}) cmd := NewDidCmd(client) - _, err := util.ExecuteCmd(cmd) + result, err := util.ExecuteCmd(cmd) require.NoError(err) + require.Contains(result, "Available Commands") } From 47d1d4fb8a9bf89fa2fdc2eb49b7f45f24889007 Mon Sep 17 00:00:00 2001 From: LuckyPigeon Date: Sat, 6 Aug 2022 04:47:22 +0800 Subject: [PATCH 09/11] check abi errors --- ioctl/newcmd/did/didregister.go | 10 ++++++++-- ioctl/newcmd/did/didregister_test.go | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ioctl/newcmd/did/didregister.go b/ioctl/newcmd/did/didregister.go index b3b613d78e..fed3f98212 100644 --- a/ioctl/newcmd/did/didregister.go +++ b/ioctl/newcmd/did/didregister.go @@ -54,8 +54,14 @@ func NewDidRegisterCmd(client ioctl.Client) *cobra.Command { } var hashArray [32]byte copy(hashArray[:], hashSlice) - abi, _ := abi.JSON(strings.NewReader(DIDABI)) - bytecode, _ := abi.Pack(_registerDIDName, hashArray, []byte(args[2])) + abi, err := abi.JSON(strings.NewReader(DIDABI)) + if err != nil { + return errors.Wrap(err, "falied to parse abi") + } + bytecode, err := abi.Pack(_registerDIDName, hashArray, []byte(args[2])) + if err != nil { + return errors.Wrap(err, "invalid bytecode") + } return action.Execute(client, cmd, contract, big.NewInt(0), bytecode) }, } diff --git a/ioctl/newcmd/did/didregister_test.go b/ioctl/newcmd/did/didregister_test.go index 00d7a60691..3fdfccae78 100644 --- a/ioctl/newcmd/did/didregister_test.go +++ b/ioctl/newcmd/did/didregister_test.go @@ -23,7 +23,7 @@ import ( "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" ) -func TestNewJwtSignCmd(t *testing.T) { +func TestNewDidRegisterCmd(t *testing.T) { require := require.New(t) ctrl := gomock.NewController(t) defer ctrl.Finish() From e527441c85a6c3e3815de782c1faf31d651f1d8d Mon Sep 17 00:00:00 2001 From: huof6890 <68298506@qq.com> Date: Wed, 7 Sep 2022 00:35:18 +0800 Subject: [PATCH 10/11] fix cmd flag of action.Execute --- ioctl/newcmd/action/action.go | 35 +++++++++++++++++++++++++++ ioctl/newcmd/action/stake2withdraw.go | 27 ++++----------------- ioctl/newcmd/did/didregister.go | 7 +++++- 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/ioctl/newcmd/action/action.go b/ioctl/newcmd/action/action.go index cd8c6dfbe6..74e85d82a3 100644 --- a/ioctl/newcmd/action/action.go +++ b/ioctl/newcmd/action/action.go @@ -171,6 +171,41 @@ func RegisterWriteCommand(client ioctl.Client, cmd *cobra.Command) { registerPasswordFlag(client, cmd) } +// GetWriteCommandFlag returns action flags for command +func GetWriteCommandFlag(cmd *cobra.Command) (gasPrice, signer, password string, nonce, gasLimit uint64, assumeYes bool, err error) { + gasPrice, err = cmd.Flags().GetString(gasPriceFlagLabel) + if err != nil { + err = errors.Wrap(err, "failed to get flag gas-price") + return + } + signer, err = cmd.Flags().GetString(signerFlagLabel) + if err != nil { + err = errors.Wrap(err, "failed to get flag signer") + return + } + password, err = cmd.Flags().GetString(passwordFlagLabel) + if err != nil { + err = errors.Wrap(err, "failed to get flag password") + return + } + nonce, err = cmd.Flags().GetUint64(nonceFlagLabel) + if err != nil { + err = errors.Wrap(err, "failed to get flag nonce") + return + } + gasLimit, err = cmd.Flags().GetUint64(gasLimitFlagLabel) + if err != nil { + err = errors.Wrap(err, "failed to get flag gas-limit") + return + } + assumeYes, err = cmd.Flags().GetBool(assumeYesFlagLabel) + if err != nil { + err = errors.Wrap(err, "failed to get flag assume-yes") + return + } + return +} + func handleClientRequestError(err error, apiName string) error { sta, ok := status.FromError(err) if ok { diff --git a/ioctl/newcmd/action/stake2withdraw.go b/ioctl/newcmd/action/stake2withdraw.go index 1584e384f4..fb5a1096b1 100644 --- a/ioctl/newcmd/action/stake2withdraw.go +++ b/ioctl/newcmd/action/stake2withdraw.go @@ -56,35 +56,25 @@ func NewStake2WithdrawCmd(client ioctl.Client) *cobra.Command { } } - signer, err := cmd.Flags().GetString(signerFlagLabel) + gasPrice, signer, password, nonce, gasLimit, assumeYes, err := GetWriteCommandFlag(cmd) if err != nil { - return errors.Wrap(err, "failed to get flag signer") + return err } + sender, err := Signer(client, signer) if err != nil { return errors.Wrap(err, "failed to get signed address") } - gasLimit, err := cmd.Flags().GetUint64(gasLimitFlagLabel) - if err != nil { - return errors.Wrap(err, "failed to get flage gas-limit") - } if gasLimit == 0 { gasLimit = action.ReclaimStakeBaseIntrinsicGas + action.ReclaimStakePayloadGas*uint64(len(data)) } - gasPrice, err := cmd.Flags().GetString(gasPriceFlagLabel) - if err != nil { - return errors.Wrap(err, "failed to get flag gas-price") - } gasPriceRau, err := gasPriceInRau(client, gasPrice) if err != nil { return errors.Wrap(err, "failed to get gas price") } - nonce, err := cmd.Flags().GetUint64(nonceFlagLabel) - if err != nil { - return errors.Wrap(err, "failed to get flag nonce") - } + nonce, err = checkNonce(client, nonce, sender) if err != nil { return errors.Wrap(err, "failed to get nonce") @@ -94,14 +84,7 @@ func NewStake2WithdrawCmd(client ioctl.Client) *cobra.Command { if err != nil { return errors.Wrap(err, "failed to make a changeCandidate instance") } - password, err := cmd.Flags().GetString(passwordFlagLabel) - if err != nil { - return errors.Wrap(err, "failed to get flag password") - } - assumeYes, err := cmd.Flags().GetBool(assumeYesFlagLabel) - if err != nil { - return errors.Wrap(err, "failed to get flag assume-yes") - } + return SendAction( client, cmd, diff --git a/ioctl/newcmd/did/didregister.go b/ioctl/newcmd/did/didregister.go index fed3f98212..ad4fd91e71 100644 --- a/ioctl/newcmd/did/didregister.go +++ b/ioctl/newcmd/did/didregister.go @@ -62,7 +62,12 @@ func NewDidRegisterCmd(client ioctl.Client) *cobra.Command { if err != nil { return errors.Wrap(err, "invalid bytecode") } - return action.Execute(client, cmd, contract, big.NewInt(0), bytecode) + + gasPrice, signer, password, nonce, gasLimit, assumeYes, err := action.GetWriteCommandFlag(cmd) + if err != nil { + return err + } + return action.Execute(client, cmd, contract, big.NewInt(0), bytecode, gasPrice, signer, password, nonce, gasLimit, assumeYes) }, } action.RegisterWriteCommand(client, cmd) From 31894f56d17d57cfc179e8ad62cdb727e81a7052 Mon Sep 17 00:00:00 2001 From: huof6890 <68298506@qq.com> Date: Wed, 7 Sep 2022 09:40:50 +0800 Subject: [PATCH 11/11] format --- ioctl/newcmd/action/action.go | 8 ++++---- ioctl/newcmd/action/stake2withdraw.go | 6 ------ 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/ioctl/newcmd/action/action.go b/ioctl/newcmd/action/action.go index 74e85d82a3..4a2085246d 100644 --- a/ioctl/newcmd/action/action.go +++ b/ioctl/newcmd/action/action.go @@ -402,11 +402,11 @@ func Execute(client ioctl.Client, if err != nil { return errors.Wrap(err, "failed to get gas price") } - signer, err = Signer(client, signer) + sender, err := Signer(client, signer) if err != nil { return errors.Wrap(err, "failed to get signer address") } - nonce, err = checkNonce(client, nonce, signer) + nonce, err = checkNonce(client, nonce, sender) if err != nil { return errors.Wrap(err, "failed to get nonce") } @@ -415,7 +415,7 @@ func Execute(client ioctl.Client, return errors.Wrap(err, "failed to make a Execution instance") } if gasLimit == 0 { - tx, err = fixGasLimit(client, signer, tx) + tx, err = fixGasLimit(client, sender, tx) if err != nil || tx == nil { return errors.Wrap(err, "failed to fix Execution gas limit") } @@ -429,7 +429,7 @@ func Execute(client ioctl.Client, SetGasPrice(gasPriceRau). SetGasLimit(gasLimit). SetAction(tx).Build(), - signer, + sender, password, nonce, assumeYes, diff --git a/ioctl/newcmd/action/stake2withdraw.go b/ioctl/newcmd/action/stake2withdraw.go index fb5a1096b1..1bccc4f6ea 100644 --- a/ioctl/newcmd/action/stake2withdraw.go +++ b/ioctl/newcmd/action/stake2withdraw.go @@ -60,31 +60,25 @@ func NewStake2WithdrawCmd(client ioctl.Client) *cobra.Command { if err != nil { return err } - sender, err := Signer(client, signer) if err != nil { return errors.Wrap(err, "failed to get signed address") } - if gasLimit == 0 { gasLimit = action.ReclaimStakeBaseIntrinsicGas + action.ReclaimStakePayloadGas*uint64(len(data)) } - gasPriceRau, err := gasPriceInRau(client, gasPrice) if err != nil { return errors.Wrap(err, "failed to get gas price") } - nonce, err = checkNonce(client, nonce, sender) if err != nil { return errors.Wrap(err, "failed to get nonce") } - s2w, err := action.NewWithdrawStake(nonce, bucketIndex, data, gasLimit, gasPriceRau) if err != nil { return errors.Wrap(err, "failed to make a changeCandidate instance") } - return SendAction( client, cmd,