shh_args.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/big"
  6. "github.com/ethereum/go-ethereum/rpc/shared"
  7. )
  8. type WhisperMessageArgs struct {
  9. Payload string
  10. To string
  11. From string
  12. Topics []string
  13. Priority uint32
  14. Ttl uint32
  15. }
  16. func (args *WhisperMessageArgs) UnmarshalJSON(b []byte) (err error) {
  17. var obj []struct {
  18. Payload string
  19. To string
  20. From string
  21. Topics []string
  22. Priority interface{}
  23. Ttl interface{}
  24. }
  25. if err = json.Unmarshal(b, &obj); err != nil {
  26. return shared.NewDecodeParamError(err.Error())
  27. }
  28. if len(obj) < 1 {
  29. return shared.NewInsufficientParamsError(len(obj), 1)
  30. }
  31. args.Payload = obj[0].Payload
  32. args.To = obj[0].To
  33. args.From = obj[0].From
  34. args.Topics = obj[0].Topics
  35. var num *big.Int
  36. if num, err = numString(obj[0].Priority); err != nil {
  37. return err
  38. }
  39. args.Priority = uint32(num.Int64())
  40. if num, err = numString(obj[0].Ttl); err != nil {
  41. return err
  42. }
  43. args.Ttl = uint32(num.Int64())
  44. return nil
  45. }
  46. type WhisperIdentityArgs struct {
  47. Identity string
  48. }
  49. func (args *WhisperIdentityArgs) UnmarshalJSON(b []byte) (err error) {
  50. var obj []interface{}
  51. if err := json.Unmarshal(b, &obj); err != nil {
  52. return shared.NewDecodeParamError(err.Error())
  53. }
  54. if len(obj) < 1 {
  55. return shared.NewInsufficientParamsError(len(obj), 1)
  56. }
  57. argstr, ok := obj[0].(string)
  58. if !ok {
  59. return shared.NewInvalidTypeError("arg0", "not a string")
  60. }
  61. args.Identity = argstr
  62. return nil
  63. }
  64. type WhisperFilterArgs struct {
  65. To string
  66. From string
  67. Topics [][]string
  68. }
  69. // UnmarshalJSON implements the json.Unmarshaler interface, invoked to convert a
  70. // JSON message blob into a WhisperFilterArgs structure.
  71. func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) {
  72. // Unmarshal the JSON message and sanity check
  73. var obj []struct {
  74. To interface{} `json:"to"`
  75. From interface{} `json:"from"`
  76. Topics interface{} `json:"topics"`
  77. }
  78. if err := json.Unmarshal(b, &obj); err != nil {
  79. return shared.NewDecodeParamError(err.Error())
  80. }
  81. if len(obj) < 1 {
  82. return shared.NewInsufficientParamsError(len(obj), 1)
  83. }
  84. // Retrieve the simple data contents of the filter arguments
  85. if obj[0].To == nil {
  86. args.To = ""
  87. } else {
  88. argstr, ok := obj[0].To.(string)
  89. if !ok {
  90. return shared.NewInvalidTypeError("to", "is not a string")
  91. }
  92. args.To = argstr
  93. }
  94. if obj[0].From == nil {
  95. args.From = ""
  96. } else {
  97. argstr, ok := obj[0].From.(string)
  98. if !ok {
  99. return shared.NewInvalidTypeError("from", "is not a string")
  100. }
  101. args.From = argstr
  102. }
  103. // Construct the nested topic array
  104. if obj[0].Topics != nil {
  105. // Make sure we have an actual topic array
  106. list, ok := obj[0].Topics.([]interface{})
  107. if !ok {
  108. return shared.NewInvalidTypeError("topics", "is not an array")
  109. }
  110. // Iterate over each topic and handle nil, string or array
  111. topics := make([][]string, len(list))
  112. for idx, field := range list {
  113. switch value := field.(type) {
  114. case nil:
  115. topics[idx] = []string{}
  116. case string:
  117. topics[idx] = []string{value}
  118. case []interface{}:
  119. topics[idx] = make([]string, len(value))
  120. for i, nested := range value {
  121. switch value := nested.(type) {
  122. case nil:
  123. topics[idx][i] = ""
  124. case string:
  125. topics[idx][i] = value
  126. default:
  127. return shared.NewInvalidTypeError(fmt.Sprintf("topic[%d][%d]", idx, i), "is not a string")
  128. }
  129. }
  130. default:
  131. return shared.NewInvalidTypeError(fmt.Sprintf("topic[%d]", idx), "not a string or array")
  132. }
  133. }
  134. args.Topics = topics
  135. }
  136. return nil
  137. }