Skip to content
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

[ioctl] Build did geturi command line into new ioctl #3650

Merged
merged 11 commits into from
Sep 30, 2022
1 change: 0 additions & 1 deletion ioctl/newcmd/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,6 @@ func Execute(client ioctl.Client,

// Read reads smart contract on IoTeX blockchain
func Read(client ioctl.Client,
cmd *cobra.Command,
contract address.Address,
amount string,
bytecode []byte,
Expand Down
2 changes: 1 addition & 1 deletion ioctl/newcmd/action/xrc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func parseAmount(client ioctl.Client, cmd *cobra.Command, contract address.Addre
if err != nil {
return nil, errors.Wrap(err, "failed to get flag gas-limit")
}
result, err := Read(client, cmd, contract, "0", decimalBytecode, signer, gasLimit)
result, err := Read(client, contract, "0", decimalBytecode, signer, gasLimit)
if err != nil {
return nil, errors.New("failed to read contract")
}
Expand Down
84 changes: 84 additions & 0 deletions ioctl/newcmd/did/didgeturi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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"
"strings"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/iotexproject/iotex-address/address"
"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"
"github.com/iotexproject/iotex-core/ioctl/util"
)

// Multi-language support
var (
_getURICmdUses = map[config.Language]string{
config.English: "geturi (CONTRACT_ADDRESS|ALIAS) DID",
config.Chinese: "geturi (合约地址|别名) DID",
}
_getURICmdShorts = map[config.Language]string{
config.English: "Geturi get DID URI on IoTeX blockchain",
config.Chinese: "Geturi 在IoTeX链上获取相应DID的uri",
}
)

// NewDidGetURICmd represents the did get uri command
func NewDidGetURICmd(client ioctl.Client) *cobra.Command {
use, _ := client.SelectTranslation(_getURICmdUses)
short, _ := client.SelectTranslation(_getURICmdShorts)

cmd := &cobra.Command{
Use: use,
Short: short,
Args: cobra.ExactArgs(2),
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")
}
addr, err := address.FromString(contract)
if err != nil {
return errors.Wrap(err, "invalid contract address")
}
abi, err := abi.JSON(strings.NewReader(DIDABI))
if err != nil {
return errors.Wrap(err, "failed to read abi")
}
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
bytecode, err := abi.Pack(_getURIName, []byte(args[1]))
if err != nil {
return errors.Wrap(err, "invalid bytecode")
}
result, err := action.Read(client, addr, "0", bytecode, contract, 20000000)
if err != nil {
return errors.Wrap(err, "failed to read contract")
}
ret, err := hex.DecodeString(result)
if err != nil {
return errors.Wrap(err, "failed to decode contract")
}
res, err := abi.Unpack(_getURIName, ret)
if err != nil {
return errors.Wrap(err, "DID does not exist")
}
out, err := util.ToByteSlice(res[0])
if err != nil {
return errors.Wrap(err, "failed to convert hash to bytes")
}
cmd.Println(hex.EncodeToString(out[:]))
return nil
},
}
return cmd
}
48 changes: 48 additions & 0 deletions ioctl/newcmd/did/didgeturi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi"
"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/identityset"
"github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient"
)

func TestNewDidGetURICmd(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := mock_ioctlclient.NewMockClient(ctrl)
apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl)
accAddr := identityset.Address(0).String()
payload := "0000000000000000000000000000000000000000000000000000000000000020" +
"0000000000000000000000000000000000000000000000000000000000000020" +
"0000000000000000000000000000000000000000000000000000000000000001" +
"0000000000000000000000000000000000000000000000000000000000000002"

client.EXPECT().SelectTranslation(gomock.Any()).Return("did", config.English).Times(2)
client.EXPECT().Address(gomock.Any()).Return(accAddr, nil).Times(1)
client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return(accAddr, nil).Times(1)
client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).Times(1)

t.Run("get did hash", func(t *testing.T) {
apiServiceClient.EXPECT().ReadContract(gomock.Any(), gomock.Any()).Return(&iotexapi.ReadContractResponse{
Data: payload,
}, nil)
cmd := NewDidGetURICmd(client)
_, err := util.ExecuteCmd(cmd, accAddr, "did:io:0x11111111111111111")
require.NoError(err)
})
}