aboutsummaryrefslogtreecommitdiffstats
path: root/whisper
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2015-04-14 16:12:09 +0800
committerPéter Szilágyi <peterke@gmail.com>2015-04-14 16:12:09 +0800
commit4af7743663fa3e444668b90878f64d0df4316deb (patch)
tree0ad5898d8510600fe8cfe230d18f2c72b8cf53ed /whisper
parentcb707ba50ce8626aa1b0e87d7526416a9592852a (diff)
downloaddexon-4af7743663fa3e444668b90878f64d0df4316deb.tar.gz
dexon-4af7743663fa3e444668b90878f64d0df4316deb.tar.zst
dexon-4af7743663fa3e444668b90878f64d0df4316deb.zip
whisper: add utility functions for creating topics
Diffstat (limited to 'whisper')
-rw-r--r--whisper/message.go9
-rw-r--r--whisper/topic.go26
-rw-r--r--whisper/topic_test.go31
3 files changed, 58 insertions, 8 deletions
diff --git a/whisper/message.go b/whisper/message.go
index ad31aa592..a4de18f65 100644
--- a/whisper/message.go
+++ b/whisper/message.go
@@ -30,7 +30,7 @@ type Options struct {
From *ecdsa.PrivateKey
To *ecdsa.PublicKey
TTL time.Duration
- Topics [][]byte
+ Topics []Topic
}
// NewMessage creates and initializes a non-signed, non-encrypted Whisper message.
@@ -75,13 +75,8 @@ func (self *Message) Wrap(pow time.Duration, options Options) (*Envelope, error)
return nil, err
}
}
- // Convert the user topic into whisper ones
- topics := make([]Topic, len(options.Topics))
- for i, topic := range options.Topics {
- topics[i] = NewTopic(topic)
- }
// Wrap the processed message, seal it and return
- envelope := NewEnvelope(options.TTL, topics, self)
+ envelope := NewEnvelope(options.TTL, options.Topics, self)
envelope.Seal(pow)
return envelope, nil
diff --git a/whisper/topic.go b/whisper/topic.go
index 10069c902..7792e437f 100644
--- a/whisper/topic.go
+++ b/whisper/topic.go
@@ -17,6 +17,32 @@ func NewTopic(data []byte) Topic {
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[:])
diff --git a/whisper/topic_test.go b/whisper/topic_test.go
index 4626e2ae5..5f8583987 100644
--- a/whisper/topic_test.go
+++ b/whisper/topic_test.go
@@ -15,10 +15,39 @@ var topicCreationTests = []struct {
}
func TestTopicCreation(t *testing.T) {
+ // Create the topics individually
for i, tt := range topicCreationTests {
topic := NewTopic(tt.data)
if bytes.Compare(topic[:], tt.hash[:]) != 0 {
- t.Errorf("test %d: hash mismatch: have %v, want %v.", i, topic, tt.hash)
+ t.Errorf("binary test %d: hash mismatch: have %v, want %v.", i, topic, tt.hash)
+ }
+ }
+ for i, tt := range topicCreationTests {
+ topic := NewTopicFromString(string(tt.data))
+ if bytes.Compare(topic[:], tt.hash[:]) != 0 {
+ t.Errorf("textual test %d: hash mismatch: have %v, want %v.", i, topic, tt.hash)
+ }
+ }
+ // Create the topics in batches
+ binaryData := make([][]byte, len(topicCreationTests))
+ for i, tt := range topicCreationTests {
+ binaryData[i] = tt.data
+ }
+ textualData := make([]string, len(topicCreationTests))
+ for i, tt := range topicCreationTests {
+ textualData[i] = string(tt.data)
+ }
+
+ topics := NewTopics(binaryData...)
+ for i, tt := range topicCreationTests {
+ if bytes.Compare(topics[i][:], tt.hash[:]) != 0 {
+ t.Errorf("binary batch test %d: hash mismatch: have %v, want %v.", i, topics[i], tt.hash)
+ }
+ }
+ topics = NewTopicsFromStrings(textualData...)
+ for i, tt := range topicCreationTests {
+ if bytes.Compare(topics[i][:], tt.hash[:]) != 0 {
+ t.Errorf("textual batch test %d: hash mismatch: have %v, want %v.", i, topics[i], tt.hash)
}
}
}