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.

87 lines
1.9 KiB

  1. package bytes
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "strings"
  8. )
  9. // HexBytes enables HEX-encoding for json/encoding.
  10. type HexBytes []byte
  11. var (
  12. _ json.Marshaler = HexBytes{}
  13. _ json.Unmarshaler = &HexBytes{}
  14. )
  15. // Marshal needed for protobuf compatibility
  16. func (bz HexBytes) Marshal() ([]byte, error) {
  17. return bz, nil
  18. }
  19. // Unmarshal needed for protobuf compatibility
  20. func (bz *HexBytes) Unmarshal(data []byte) error {
  21. *bz = data
  22. return nil
  23. }
  24. // MarshalJSON implements the json.Marshaler interface. The encoding is a JSON
  25. // quoted string of hexadecimal digits.
  26. func (bz HexBytes) MarshalJSON() ([]byte, error) {
  27. size := hex.EncodedLen(len(bz)) + 2 // +2 for quotation marks
  28. buf := make([]byte, size)
  29. hex.Encode(buf[1:], []byte(bz))
  30. buf[0] = '"'
  31. buf[size-1] = '"'
  32. // Ensure letter digits are capitalized.
  33. for i := 1; i < size-1; i++ {
  34. if buf[i] >= 'a' && buf[i] <= 'f' {
  35. buf[i] = 'A' + (buf[i] - 'a')
  36. }
  37. }
  38. return buf, nil
  39. }
  40. // UnmarshalJSON implements the json.Umarshaler interface.
  41. func (bz *HexBytes) UnmarshalJSON(data []byte) error {
  42. if bytes.Equal(data, []byte("null")) {
  43. return nil
  44. }
  45. if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
  46. return fmt.Errorf("invalid hex string: %s", data)
  47. }
  48. bz2, err := hex.DecodeString(string(data[1 : len(data)-1]))
  49. if err != nil {
  50. return err
  51. }
  52. *bz = bz2
  53. return nil
  54. }
  55. // Bytes fulfills various interfaces in light-client, etc...
  56. func (bz HexBytes) Bytes() []byte {
  57. return bz
  58. }
  59. func (bz HexBytes) String() string {
  60. return strings.ToUpper(hex.EncodeToString(bz))
  61. }
  62. // Format writes either address of 0th element in a slice in base 16 notation,
  63. // with leading 0x (%p), or casts HexBytes to bytes and writes as hexadecimal
  64. // string to s.
  65. func (bz HexBytes) Format(s fmt.State, verb rune) {
  66. switch verb {
  67. case 'p':
  68. s.Write([]byte(fmt.Sprintf("%p", bz)))
  69. default:
  70. s.Write([]byte(fmt.Sprintf("%X", []byte(bz))))
  71. }
  72. }