github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/whisper/whisperv5/topic.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  // Contains the Whisper protocol Topic element.
    13  
    14  package whisperv5
    15  
    16  import (
    17  	"github.com/Sberex/go-sberex/common"
    18  	"github.com/Sberex/go-sberex/common/hexutil"
    19  )
    20  
    21  // Topic represents a cryptographically secure, probabilistic partial
    22  // classifications of a message, determined as the first (left) 4 bytes of the
    23  // SHA3 hash of some arbitrary data given by the original author of the message.
    24  type TopicType [TopicLength]byte
    25  
    26  func BytesToTopic(b []byte) (t TopicType) {
    27  	sz := TopicLength
    28  	if x := len(b); x < TopicLength {
    29  		sz = x
    30  	}
    31  	for i := 0; i < sz; i++ {
    32  		t[i] = b[i]
    33  	}
    34  	return t
    35  }
    36  
    37  // String converts a topic byte array to a string representation.
    38  func (t *TopicType) String() string {
    39  	return common.ToHex(t[:])
    40  }
    41  
    42  // MarshalText returns the hex representation of t.
    43  func (t TopicType) MarshalText() ([]byte, error) {
    44  	return hexutil.Bytes(t[:]).MarshalText()
    45  }
    46  
    47  // UnmarshalText parses a hex representation to a topic.
    48  func (t *TopicType) UnmarshalText(input []byte) error {
    49  	return hexutil.UnmarshalFixedText("Topic", input, t[:])
    50  }