You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.3 KiB

  1. package common
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "strings"
  6. )
  7. // The main purpose of HexBytes is to enable HEX-encoding for json/encoding.
  8. type HexBytes []byte
  9. // Marshal needed for protobuf compatibility
  10. func (bz HexBytes) Marshal() ([]byte, error) {
  11. return bz, nil
  12. }
  13. // Unmarshal needed for protobuf compatibility
  14. func (bz *HexBytes) Unmarshal(data []byte) error {
  15. *bz = data
  16. return nil
  17. }
  18. // This is the point of Bytes.
  19. func (bz HexBytes) MarshalJSON() ([]byte, error) {
  20. s := strings.ToUpper(hex.EncodeToString(bz))
  21. jbz := make([]byte, len(s)+2)
  22. jbz[0] = '"'
  23. copy(jbz[1:], []byte(s))
  24. jbz[len(jbz)-1] = '"'
  25. return jbz, nil
  26. }
  27. // This is the point of Bytes.
  28. func (bz *HexBytes) UnmarshalJSON(data []byte) error {
  29. if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
  30. return fmt.Errorf("invalid hex string: %s", data)
  31. }
  32. bz2, err := hex.DecodeString(string(data[1 : len(data)-1]))
  33. if err != nil {
  34. return err
  35. }
  36. *bz = bz2
  37. return nil
  38. }
  39. // Allow it to fulfill various interfaces in light-client, etc...
  40. func (bz HexBytes) Bytes() []byte {
  41. return bz
  42. }
  43. func (bz HexBytes) String() string {
  44. return strings.ToUpper(hex.EncodeToString(bz))
  45. }
  46. func (bz HexBytes) Format(s fmt.State, verb rune) {
  47. switch verb {
  48. case 'p':
  49. s.Write([]byte(fmt.Sprintf("%p", bz)))
  50. default:
  51. s.Write([]byte(fmt.Sprintf("%X", []byte(bz))))
  52. }
  53. }