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.

65 lines
1.5 KiB

  1. package bytes
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "strings"
  6. )
  7. // HexBytes enables 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:], 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. // Bytes fulfils 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. // Format writes either address of 0th element in a slice in base 16 notation,
  47. // with leading 0x (%p), or casts HexBytes to bytes and writes as hexadecimal
  48. // string to s.
  49. func (bz HexBytes) Format(s fmt.State, verb rune) {
  50. switch verb {
  51. case 'p':
  52. s.Write([]byte(fmt.Sprintf("%p", bz)))
  53. default:
  54. s.Write([]byte(fmt.Sprintf("%X", []byte(bz))))
  55. }
  56. }