diff --git a/rpc/client/args_test.go b/rpc/client/args_test.go new file mode 100644 index 000000000..ccabd0d2c --- /dev/null +++ b/rpc/client/args_test.go @@ -0,0 +1,39 @@ +package rpcclient + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type Tx []byte + +type Foo struct { + Bar int + Baz string +} + +func TestArgToJSON(t *testing.T) { + assert := assert.New(t) + require := require.New(t) + + cases := []struct { + input interface{} + expected string + }{ + {[]byte("1234"), "0x31323334"}, + {Tx("654"), "0x363534"}, + {Foo{7, "hello"}, `{"Bar":7,"Baz":"hello"}`}, + } + + for i, tc := range cases { + args := map[string]interface{}{"data": tc.input} + err := argsToJson(args) + require.Nil(err, "%d: %+v", i, err) + require.Equal(1, len(args), "%d", i) + data, ok := args["data"].(string) + require.True(ok, "%d: %#v", i, args["data"]) + assert.Equal(tc.expected, data, "%d", i) + } +} diff --git a/rpc/client/http_client.go b/rpc/client/http_client.go index ce78da43d..38a7f20fc 100644 --- a/rpc/client/http_client.go +++ b/rpc/client/http_client.go @@ -12,8 +12,8 @@ import ( "strings" "github.com/pkg/errors" - types "github.com/tendermint/tendermint/rpc/types" wire "github.com/tendermint/go-wire" + types "github.com/tendermint/tendermint/rpc/types" ) // HTTPClient is a common interface for JSONRPCClient and URIClient. @@ -179,10 +179,11 @@ func argsToJson(args map[string]interface{}) error { var n int var err error for k, v := range args { - // Convert byte slices to "0x"-prefixed hex - byteSlice, isByteSlice := reflect.ValueOf(v).Interface().([]byte) + rt := reflect.TypeOf(v) + isByteSlice := rt.Kind() == reflect.Slice && rt.Elem().Kind() == reflect.Uint8 if isByteSlice { - args[k] = fmt.Sprintf("0x%X", byteSlice) + bytes := reflect.ValueOf(v).Bytes() + args[k] = fmt.Sprintf("0x%X", bytes) continue }