aboutsummaryrefslogtreecommitdiffstats
path: root/whisper/topic.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2015-04-13 17:16:51 +0800
committerPéter Szilágyi <peterke@gmail.com>2015-04-13 17:16:51 +0800
commit9a53390f49b9667db162bf2ef487d0af64b3363d (patch)
treeb71d2daee37a56a7872d5d3c1536ed83a39d7bc2 /whisper/topic.go
parent7b501906db5b4bed0cf9972a1b103cc343d7f2d2 (diff)
downloaddexon-9a53390f49b9667db162bf2ef487d0af64b3363d.tar
dexon-9a53390f49b9667db162bf2ef487d0af64b3363d.tar.gz
dexon-9a53390f49b9667db162bf2ef487d0af64b3363d.tar.bz2
dexon-9a53390f49b9667db162bf2ef487d0af64b3363d.tar.lz
dexon-9a53390f49b9667db162bf2ef487d0af64b3363d.tar.xz
dexon-9a53390f49b9667db162bf2ef487d0af64b3363d.tar.zst
dexon-9a53390f49b9667db162bf2ef487d0af64b3363d.zip
whisper: clean up and integrate topics
Diffstat (limited to 'whisper/topic.go')
-rw-r--r--whisper/topic.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/whisper/topic.go b/whisper/topic.go
new file mode 100644
index 000000000..10069c902
--- /dev/null
+++ b/whisper/topic.go
@@ -0,0 +1,35 @@
+// 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)
+}
+
+// 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)
+}