aboutsummaryrefslogtreecommitdiffstats
path: root/whisper/whisperv5/topic.go
diff options
context:
space:
mode:
Diffstat (limited to 'whisper/whisperv5/topic.go')
-rw-r--r--whisper/whisperv5/topic.go32
1 files changed, 9 insertions, 23 deletions
diff --git a/whisper/whisperv5/topic.go b/whisper/whisperv5/topic.go
index 54d7422d1..d1996c460 100644
--- a/whisper/whisperv5/topic.go
+++ b/whisper/whisperv5/topic.go
@@ -19,10 +19,8 @@
package whisperv5
import (
- "fmt"
- "strings"
-
"github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/common/hexutil"
)
// Topic represents a cryptographically secure, probabilistic partial
@@ -46,24 +44,12 @@ func (topic *TopicType) String() string {
return string(common.ToHex(topic[:]))
}
-// UnmarshalJSON parses a hex representation to a topic.
-func (t *TopicType) UnmarshalJSON(input []byte) error {
- length := len(input)
- if length >= 2 && input[0] == '"' && input[length-1] == '"' {
- input = input[1 : length-1]
- }
- // strip "0x" for length check
- if len(input) > 1 && strings.ToLower(string(input[:2])) == "0x" {
- input = input[2:]
- }
- // validate the length of the input
- if len(input) != TopicLength*2 {
- return fmt.Errorf("unmarshalJSON failed: topic must be exactly %d bytes", TopicLength)
- }
- b := common.FromHex(string(input))
- if b == nil {
- return fmt.Errorf("unmarshalJSON failed: wrong topic format")
- }
- *t = BytesToTopic(b)
- return nil
+// MarshalText returns the hex representation of t.
+func (t TopicType) MarshalText() ([]byte, error) {
+ return hexutil.Bytes(t[:]).MarshalText()
+}
+
+// UnmarshalText parses a hex representation to a topic.
+func (t *TopicType) UnmarshalText(input []byte) error {
+ return hexutil.UnmarshalFixedText("Topic", input, t[:])
}