aboutsummaryrefslogtreecommitdiffstats
path: root/whisper/topic.go
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-04-18 08:33:04 +0800
committerobscuren <geffobscura@gmail.com>2015-04-18 08:33:04 +0800
commitc39a7b5c0d2002d72df7a0517ccc6edd7557ab7d (patch)
tree6d5bea52157c4fa04de8402fe4aaf375b78801df /whisper/topic.go
parent89fd752659c5c11b243c30f739781d723cac82c2 (diff)
parent7dc6c338736b05fa239316f5764fb3ffec6ec6c2 (diff)
downloaddexon-c39a7b5c0d2002d72df7a0517ccc6edd7557ab7d.tar
dexon-c39a7b5c0d2002d72df7a0517ccc6edd7557ab7d.tar.gz
dexon-c39a7b5c0d2002d72df7a0517ccc6edd7557ab7d.tar.bz2
dexon-c39a7b5c0d2002d72df7a0517ccc6edd7557ab7d.tar.lz
dexon-c39a7b5c0d2002d72df7a0517ccc6edd7557ab7d.tar.xz
dexon-c39a7b5c0d2002d72df7a0517ccc6edd7557ab7d.tar.zst
dexon-c39a7b5c0d2002d72df7a0517ccc6edd7557ab7d.zip
Merge branch 'develop' of github.com-obscure:ethereum/go-ethereum into develop
Diffstat (limited to 'whisper/topic.go')
-rw-r--r--whisper/topic.go61
1 files changed, 61 insertions, 0 deletions
diff --git a/whisper/topic.go b/whisper/topic.go
new file mode 100644
index 000000000..a965c7cc2
--- /dev/null
+++ b/whisper/topic.go
@@ -0,0 +1,61 @@
+// Contains the Whisper protocol Topic element. For formal details please see
+// the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#topics.
+
+package whisper
+
+import "github.com/ethereum/go-ethereum/crypto"
+
+// Topic represents a cryptographically secure, probabilistic partial
+// classifications of a message, determined as the first (left) 4 bytes of the
+// SHA3 hash of some arbitrary data given by the original author of the message.
+type Topic [4]byte
+
+// NewTopic creates a topic from the 4 byte prefix of the SHA3 hash of the data.
+func NewTopic(data []byte) Topic {
+ prefix := [4]byte{}
+ copy(prefix[:], crypto.Sha3(data)[:4])
+ return Topic(prefix)
+}
+
+// NewTopics creates a list of topics from a list of binary data elements, by
+// iteratively calling NewTopic on each of them.
+func NewTopics(data ...[]byte) []Topic {
+ topics := make([]Topic, len(data))
+ for i, element := range data {
+ topics[i] = NewTopic(element)
+ }
+ return topics
+}
+
+// NewTopicFromString creates a topic using the binary data contents of the
+// specified string.
+func NewTopicFromString(data string) Topic {
+ return NewTopic([]byte(data))
+}
+
+// NewTopicsFromStrings creates a list of topics from a list of textual data
+// elements, by iteratively calling NewTopicFromString on each of them.
+func NewTopicsFromStrings(data ...string) []Topic {
+ topics := make([]Topic, len(data))
+ for i, element := range data {
+ topics[i] = NewTopicFromString(element)
+ }
+ return topics
+}
+
+// String converts a topic byte array to a string representation.
+func (self *Topic) String() string {
+ return string(self[:])
+}
+
+// TopicSet represents a hash set to check if a topic exists or not.
+type topicSet map[string]struct{}
+
+// NewTopicSet creates a topic hash set from a slice of topics.
+func newTopicSet(topics []Topic) topicSet {
+ set := make(map[string]struct{})
+ for _, topic := range topics {
+ set[topic.String()] = struct{}{}
+ }
+ return topicSet(set)
+}