diff --git a/libs/bytes/bytes.go b/libs/bytes/bytes.go index 5f1382758..86d5d965e 100644 --- a/libs/bytes/bytes.go +++ b/libs/bytes/bytes.go @@ -70,27 +70,23 @@ func (bz HexBytes) Format(s fmt.State, verb rune) { } } -// // This is the point of Bytes. -// func (bz HexBytes) MarshalJSON() ([]byte, error) { -// fmt.Println("In MarshalJSON") -// s := strings.ToUpper(hex.EncodeToString(bz)) -// jbz := make([]byte, len(s)+2) -// jbz[0] = '"' -// copy(jbz[1:], s) -// jbz[len(jbz)-1] = '"' -// return jbz, nil -// } +func (bz HexBytes) MarshalJSON() ([]byte, error) { + s := strings.ToUpper(hex.EncodeToString(bz)) + jbz := make([]byte, len(s)+2) + jbz[0] = '"' + copy(jbz[1:], s) + jbz[len(jbz)-1] = '"' + return jbz, nil +} -// // This is the point of Bytes. -// func (bz *HexBytes) UnmarshalJSON(data []byte) error { -// fmt.Println("In UnmarshalJSON, data: ", string(data)) -// if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' { -// return fmt.Errorf("invalid hex string: %s", data) -// } -// bz2, err := hex.DecodeString(string(data[1 : len(data)-1])) -// if err != nil { -// return err -// } -// *bz = bz2 -// return nil -// } +func (bz *HexBytes) UnmarshalJSON(data []byte) error { + if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' { + return fmt.Errorf("invalid hex string: %s", data) + } + bz2, err := hex.DecodeString(string(data[1 : len(data)-1])) + if err != nil { + return err + } + *bz = bz2 + return nil +}