Skip to content

Commit

Permalink
Use HexBytes with Marshal/UnmarshalJSON functions
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed Apr 25, 2023
1 parent fbb898c commit 8b29f3e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,4 @@ replace (
// the following version across all dependencies.
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
google.golang.org/grpc => google.golang.org/grpc v1.33.2

)
3 changes: 1 addition & 2 deletions proto/cosmwasm/wasm/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ message AbsoluteTxPosition {
// Model is a struct that holds a KV pair
message Model {
// hex-encode key to read it better (this is often ascii)
bytes key = 1 [ (gogoproto.casttype) =
"github.com/tendermint/tendermint/libs/bytes.HexBytes" ];
bytes key = 1 [ (gogoproto.casttype) = "HexBytes" ];
// base64-encode raw value
bytes value = 2;
}
5 changes: 2 additions & 3 deletions x/wasm/keeper/test_fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
fuzz "github.com/google/gofuzz"
tmBytes "github.com/tendermint/tendermint/libs/bytes"

"github.com/CosmWasm/wasmd/x/wasm/types"
)
Expand Down Expand Up @@ -50,9 +49,9 @@ func FuzzContractCodeHistory(m *types.ContractCodeHistoryEntry, c fuzz.Continue)
}

func FuzzStateModel(m *types.Model, c fuzz.Continue) {
m.Key = tmBytes.HexBytes(c.RandString())
m.Key = types.HexBytes(c.RandString())
if len(m.Key) == 0 {
m.Key = tmBytes.HexBytes("non empty key")
m.Key = types.HexBytes("non empty key")
}
c.Fuzz(&m.Value)
}
Expand Down
77 changes: 77 additions & 0 deletions x/wasm/types/bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package types

import (
"encoding/base64"
"encoding/hex"
"fmt"
"strings"
)

// HexBytes is a wrapper around []byte that encodes data as hexadecimal strings
// for use in JSON.
type HexBytes []byte

// Marshal needed for protobuf compatibility
func (bz HexBytes) Marshal() ([]byte, error) {
return bz, nil
}

// Unmarshal needed for protobuf compatibility
func (bz *HexBytes) Unmarshal(data []byte) error {
*bz = data
return nil
}

// MarshalText encodes a HexBytes value as hexadecimal digits.
// This method is used by json.Marshal.
func (bz HexBytes) MarshalText() ([]byte, error) {
enc := hex.EncodeToString([]byte(bz))
return []byte(strings.ToUpper(enc)), nil
}

// UnmarshalText handles decoding of HexBytes from JSON strings.
// This method is used by json.Unmarshal.
// It allows decoding of both hex and base64-encoded byte arrays.
func (bz *HexBytes) UnmarshalText(data []byte) error {
input := string(data)
if input == "" || input == "null" {
return nil
}
dec, err := hex.DecodeString(input)
if err != nil {
dec, err = base64.StdEncoding.DecodeString(input)
if err != nil {
return err
}
}
*bz = HexBytes(dec)
return nil
}

// Bytes fulfills various interfaces in light-client, etc...
func (bz HexBytes) Bytes() []byte {
return bz
}
func (bz HexBytes) String() string {
return strings.ToUpper(hex.EncodeToString(bz))
}

// Format writes either address of 0th element in a slice in base 16 notation,
// with leading 0x (%p), or casts HexBytes to bytes and writes as hexadecimal
// string to s.
func (bz HexBytes) Format(s fmt.State, verb rune) {
switch verb {
case 'p':
s.Write([]byte(fmt.Sprintf("%p", bz)))
default:
s.Write([]byte(fmt.Sprintf("%X", []byte(bz))))
}
}

func (bz HexBytes) MarshalJSON() ([]byte, error) {
return bz.MarshalText()
}

func (bz *HexBytes) UnmarshalJSON(data []byte) error {
return bz.UnmarshalText(data)
}
3 changes: 1 addition & 2 deletions x/wasm/types/types.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8b29f3e

Please sign in to comment.