github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/whisper/whisperv5/topic.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:46</date>
    10  //</624450124856168448>
    11  
    12  
    13  //包含耳语协议主题元素。
    14  
    15  package whisperv5
    16  
    17  import (
    18  	"github.com/ethereum/go-ethereum/common"
    19  	"github.com/ethereum/go-ethereum/common/hexutil"
    20  )
    21  
    22  //TopicType表示密码安全的概率部分
    23  //消息的分类,确定为
    24  //sha3消息原始作者给出的某些任意数据的散列。
    25  type TopicType [TopicLength]byte
    26  
    27  func BytesToTopic(b []byte) (t TopicType) {
    28  	sz := TopicLength
    29  	if x := len(b); x < TopicLength {
    30  		sz = x
    31  	}
    32  	for i := 0; i < sz; i++ {
    33  		t[i] = b[i]
    34  	}
    35  	return t
    36  }
    37  
    38  //字符串将主题字节数组转换为字符串表示形式。
    39  func (t *TopicType) String() string {
    40  	return common.ToHex(t[:])
    41  }
    42  
    43  //marshalText返回t的十六进制表示形式。
    44  func (t TopicType) MarshalText() ([]byte, error) {
    45  	return hexutil.Bytes(t[:]).MarshalText()
    46  }
    47  
    48  //UnmarshalText解析主题的十六进制表示。
    49  func (t *TopicType) UnmarshalText(input []byte) error {
    50  	return hexutil.UnmarshalFixedText("Topic", input, t[:])
    51  }
    52