diff options
author | Bas van Kervel <basvankervel@gmail.com> | 2017-06-13 17:49:07 +0800 |
---|---|---|
committer | Bas van Kervel <basvankervel@gmail.com> | 2017-06-15 17:53:15 +0800 |
commit | b58a5016738b92db19e08ec87ef34ce3250fae6b (patch) | |
tree | fa9485b3f711204b14edae5dcbc812e624490c68 /whisper/whisperv5/topic.go | |
parent | 80f7c6c2996ad47f70a5070c400b1fd87a20c59c (diff) | |
download | dexon-b58a5016738b92db19e08ec87ef34ce3250fae6b.tar.gz dexon-b58a5016738b92db19e08ec87ef34ce3250fae6b.tar.zst dexon-b58a5016738b92db19e08ec87ef34ce3250fae6b.zip |
whisperv5: integrate whisper and add whisper RPC simulator
Diffstat (limited to 'whisper/whisperv5/topic.go')
-rw-r--r-- | whisper/whisperv5/topic.go | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/whisper/whisperv5/topic.go b/whisper/whisperv5/topic.go index 54d7422d1..5fea2f28b 100644 --- a/whisper/whisperv5/topic.go +++ b/whisper/whisperv5/topic.go @@ -19,10 +19,11 @@ package whisperv5 import ( + "encoding/json" "fmt" - "strings" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" ) // Topic represents a cryptographically secure, probabilistic partial @@ -46,24 +47,19 @@ func (topic *TopicType) String() string { return string(common.ToHex(topic[:])) } +func (t *TopicType) MarshalJSON() ([]byte, error) { + return json.Marshal(hexutil.Bytes(t[:])) +} + // 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) + var data hexutil.Bytes + if err := json.Unmarshal(input, &data); err != nil { + return err } - b := common.FromHex(string(input)) - if b == nil { - return fmt.Errorf("unmarshalJSON failed: wrong topic format") + if len(data) != TopicLength { + return fmt.Errorf("unmarshalJSON failed: topic must be exactly %d bytes(%d)", TopicLength, len(input)) } - *t = BytesToTopic(b) + *t = BytesToTopic(data) return nil } |