Skip to content

Commit

Permalink
fix bug with address during compilation (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
borispovod authored Jul 20, 2020
1 parent f99c836 commit 78c2acd
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 19 deletions.
21 changes: 21 additions & 0 deletions helpers/types/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package types

import (
"encoding/hex"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// Parse string address representation (both libra hex and bech32).
func GetAccAddressFromHexOrBech32(strAddr string) (address sdk.AccAddress, err error) {
address, err = hex.DecodeString(strAddr)
if err != nil {
address, err = sdk.AccAddressFromBech32(strAddr)
if err != nil {
err = fmt.Errorf("can't parse address %q (should be libra hex or bech32): %v", strAddr, err)
}
}

return
}
28 changes: 28 additions & 0 deletions helpers/types/address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// +build unit

package types

import (
"encoding/hex"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/secp256k1"
)

func Test_GetAccAddressFromHexOrBech32(t *testing.T) {
bech32 := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())

addr, err := GetAccAddressFromHexOrBech32(bech32.String())
require.NoError(t, err)

require.Truef(t, bech32.Equals(addr), "bech32 addresses doesn't match", bech32, addr)

libraAddr := hex.EncodeToString(bech32)

addr, err = GetAccAddressFromHexOrBech32(libraAddr)
require.NoError(t, err)

require.Truef(t, addr.Equals(bech32), "libra hex addresses doesn't match", libraAddr, addr)
}
2 changes: 2 additions & 0 deletions helpers/types/str_filters_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build unit

package types

import (
Expand Down
8 changes: 3 additions & 5 deletions x/vm/client/cli/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/dfinance/dvm-proto/go/vm_grpc"

"github.com/dfinance/dnode/cmd/config"
addrHelper "github.com/dfinance/dnode/helpers/types"
"github.com/dfinance/dnode/x/common_vm"
vmClient "github.com/dfinance/dnode/x/vm/client"
"github.com/dfinance/dnode/x/vm/internal/types"
Expand Down Expand Up @@ -99,12 +100,9 @@ func GetData(queryRoute string, cdc *codec.Codec) *cobra.Command {
// extract data
rawAddress := args[0]
var address sdk.AccAddress
address, err := hex.DecodeString(rawAddress)
address, err := addrHelper.GetAccAddressFromHexOrBech32(rawAddress)
if err != nil {
address, err = sdk.AccAddressFromBech32(rawAddress)
if err != nil {
return fmt.Errorf("can't parse address: %s\n, check address format, it could be libra hex or bech32", rawAddress)
}
return fmt.Errorf("error parsing address: %v", err)
}

path, err := hex.DecodeString(args[1])
Expand Down
36 changes: 22 additions & 14 deletions x/vm/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"strings"

"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/dfinance/dvm-proto/go/vm_grpc"
"github.com/gorilla/mux"
"github.com/spf13/viper"

addrHelper "github.com/dfinance/dnode/helpers/types"
"github.com/dfinance/dnode/x/common_vm"
vmClient "github.com/dfinance/dnode/x/vm/client"
"github.com/dfinance/dnode/x/vm/internal/types"
Expand All @@ -36,6 +36,7 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc(fmt.Sprintf("/%s/data/{%s}/{%s}", types.ModuleName, accountAddrName, vmPathName), getData(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/tx/{%s}", types.ModuleName, txHash), getTxVMStatus(cliCtx)).Methods("GET")
}

// Compile godoc
// @Tags vm
// @Summary Get compiled byteCode
Expand All @@ -57,9 +58,20 @@ func compile(cliCtx context.CLIContext) http.HandlerFunc {
}

compilerAddr := viper.GetString(vmClient.FlagCompilerAddr)

address, err := addrHelper.GetAccAddressFromHexOrBech32(req.Account)
if err != nil {
rest.WriteErrorResponse(
w,
http.StatusUnprocessableEntity,
fmt.Sprintf("error parsing address: %v", err),
)
return
}

sourceFile := &vm_grpc.SourceFile{
Text: req.Code,
Address: []byte(req.Account),
Address: common_vm.Bech32ToLibra(address),
}

byteCode, err := vmClient.Compile(compilerAddr, sourceFile)
Expand All @@ -82,7 +94,7 @@ func compile(cliCtx context.CLIContext) http.HandlerFunc {
// @ID vmGetData
// @Accept json
// @Produce json
// @Param accountAddr path string true "account address (Libra HEX Bech32)"
// @Param accountAddr path string true "account address (Libra HEX / Bech32)"
// @Param vmPath path string true "VM path (HEX string)"
// @Success 200 {object} VmData
// @Failure 422 {object} rest.ErrorResponse "Returned if the request doesn't have valid path params"
Expand All @@ -94,18 +106,14 @@ func getData(cliCtx context.CLIContext) http.HandlerFunc {
rawAddress := vars[accountAddrName]
rawPath := vars[vmPathName]

var address sdk.AccAddress
address, err := hex.DecodeString(rawAddress)
address, err := addrHelper.GetAccAddressFromHexOrBech32(rawAddress)
if err != nil {
address, err = sdk.AccAddressFromBech32(rawAddress)
if err != nil {
rest.WriteErrorResponse(
w,
http.StatusUnprocessableEntity,
fmt.Sprintf("can't parse address %q (should be libra hex or bech32): %v", rawAddress, err),
)
return
}
rest.WriteErrorResponse(
w,
http.StatusUnprocessableEntity,
fmt.Sprintf("address parsing error: %v", err),
)
return
}

path, err := hex.DecodeString(rawPath)
Expand Down

0 comments on commit 78c2acd

Please sign in to comment.