diff --git a/cmd/wnode/main.go b/cmd/wnode/main.go index e48f0b2e51..7f6173090a 100644 --- a/cmd/wnode/main.go +++ b/cmd/wnode/main.go @@ -65,7 +65,7 @@ var ( pub *ecdsa.PublicKey asymKey *ecdsa.PrivateKey nodeid *ecdsa.PrivateKey - topic whisper.TopicType + topic []byte asymKeyID string filterID string symPass string @@ -129,7 +129,7 @@ func processArgs() { if err != nil { utils.Fatalf("Failed to parse the topic: %s", err) } - topic = whisper.BytesToTopic(x) + topic = x } if *asymmetricMode && len(*argPub) > 0 { @@ -355,7 +355,7 @@ func configureNode() { filter := whisper.Filter{ KeySym: symKey, KeyAsym: asymKey, - Topics: []whisper.TopicType{topic}, + Topics: [][]byte{topic}, AllowP2P: p2pAccept, } filterID, err = shh.Watch(&filter) @@ -486,7 +486,7 @@ func sendMsg(payload []byte) common.Hash { Dst: pub, KeySym: symKey, Payload: payload, - Topic: topic, + Topic: whisper.BytesToTopic(topic), TTL: uint32(*argTTL), PoW: *argPoW, WorkTime: uint32(*argWorkTime), diff --git a/whisper/whisperv5/api.go b/whisper/whisperv5/api.go index fb247d29f3..349ef9b6ac 100644 --- a/whisper/whisperv5/api.go +++ b/whisper/whisperv5/api.go @@ -233,7 +233,9 @@ func (api *PublicWhisperAPI) Subscribe(args WhisperFilterArgs) (string, error) { AllowP2P: args.AllowP2P, } - filter.Topics = append(filter.Topics, args.Topics...) + for _, bt := range args.Topics { + filter.Topics = append(filter.Topics, bt) + } err := ValidateKeyID(args.Key) if err != nil { @@ -441,7 +443,7 @@ type WhisperFilterArgs struct { Key string SignedWith string MinPoW float64 - Topics []TopicType + Topics [][]byte AllowP2P bool } @@ -487,13 +489,13 @@ func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) { return fmt.Errorf("topic[%d] is not a string", i) } } - topicsDecoded := make([]TopicType, len(topics)) + topicsDecoded := make([][]byte, len(topics)) for j, s := range topics { x := common.FromHex(s) - if x == nil || len(x) != TopicLength { + if x == nil || len(x) > TopicLength { return fmt.Errorf("topic[%d] is invalid", j) } - topicsDecoded[j] = BytesToTopic(x) + topicsDecoded[j] = x } args.Topics = topicsDecoded } diff --git a/whisper/whisperv5/api_test.go b/whisper/whisperv5/api_test.go index 5c15b7ce11..e6ac1468bb 100644 --- a/whisper/whisperv5/api_test.go +++ b/whisper/whisperv5/api_test.go @@ -204,22 +204,22 @@ func TestUnmarshalFilterArgs(t *testing.T) { } i := 0 - if f.Topics[i] != (TopicType{0x00, 0x00, 0x00, 0x00}) { + if !bytes.Equal(f.Topics[i], []byte{0x00, 0x00, 0x00, 0x00}) { t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i]) } i++ - if f.Topics[i] != (TopicType{0x00, 0x7f, 0x80, 0xff}) { + if !bytes.Equal(f.Topics[i], []byte{0x00, 0x7f, 0x80, 0xff}) { t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i]) } i++ - if f.Topics[i] != (TopicType{0xff, 0x80, 0x7f, 0x00}) { + if !bytes.Equal(f.Topics[i], []byte{0xff, 0x80, 0x7f, 0x00}) { t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i]) } i++ - if f.Topics[i] != (TopicType{0xf2, 0x6e, 0x77, 0x79}) { + if !bytes.Equal(f.Topics[i], []byte{0xf2, 0x6e, 0x77, 0x79}) { t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i]) } } @@ -347,7 +347,9 @@ func TestIntegrationAsym(t *testing.T) { f.Symmetric = false f.Key = key f.SignedWith = sigPubKey - f.Topics = topics[:] + f.Topics = make([][]byte, 2) + f.Topics[0] = topics[0][:] + f.Topics[1] = topics[1][:] f.MinPoW = DefaultMinimumPoW / 2 f.AllowP2P = true @@ -442,7 +444,9 @@ func TestIntegrationSym(t *testing.T) { var f WhisperFilterArgs f.Symmetric = true f.Key = symKeyID - f.Topics = topics[:] + f.Topics = make([][]byte, 2) + f.Topics[0] = topics[0][:] + f.Topics[1] = topics[1][:] f.MinPoW = 0.324 f.SignedWith = sigPubKey f.AllowP2P = false @@ -538,7 +542,9 @@ func TestIntegrationSymWithFilter(t *testing.T) { var f WhisperFilterArgs f.Symmetric = true f.Key = symKeyID - f.Topics = topics[:] + f.Topics = make([][]byte, 2) + f.Topics[0] = topics[0][:] + f.Topics[1] = topics[1][:] f.MinPoW = 0.324 f.SignedWith = sigPubKey f.AllowP2P = false diff --git a/whisper/whisperv5/filter.go b/whisper/whisperv5/filter.go index f18b4ee292..675340d03c 100644 --- a/whisper/whisperv5/filter.go +++ b/whisper/whisperv5/filter.go @@ -29,13 +29,13 @@ type Filter struct { Src *ecdsa.PublicKey // Sender of the message KeyAsym *ecdsa.PrivateKey // Private Key of recipient KeySym []byte // Key associated with the Topic - Topics []TopicType // Topics to filter messages with + Topics [][]byte // Topics to filter messages with PoW float64 // Proof of work as described in the Whisper spec AllowP2P bool // Indicates whether this filter is interested in direct peer-to-peer messages SymKeyHash common.Hash // The Keccak256Hash of the symmetric key, needed for optimization - Messages map[common.Hash]*ReceivedMessage - mutex sync.RWMutex + Messages map[common.Hash]*ReceivedMessage + mutex sync.RWMutex } type Filters struct { @@ -201,12 +201,14 @@ func (f *Filter) MatchTopic(topic TopicType) bool { return true } - for _, t := range f.Topics { - if t == topic { - return true + for _, bt := range f.Topics { + for j, b := range bt { + if topic[j] != b { + return false + } } } - return false + return true } func IsPubKeyEqual(a, b *ecdsa.PublicKey) bool { diff --git a/whisper/whisperv5/filter_test.go b/whisper/whisperv5/filter_test.go index 8552052f63..7e5f4c29f3 100644 --- a/whisper/whisperv5/filter_test.go +++ b/whisper/whisperv5/filter_test.go @@ -53,7 +53,7 @@ func generateFilter(t *testing.T, symmetric bool) (*Filter, error) { f.Messages = make(map[common.Hash]*ReceivedMessage) const topicNum = 8 - f.Topics = make([]TopicType, topicNum) + f.Topics = make([][]byte, topicNum) for i := 0; i < topicNum; i++ { f.Topics[i] = make([]byte, 4) mrand.Read(f.Topics[i][:]) @@ -194,9 +194,15 @@ func TestMatchEnvelope(t *testing.T) { } // encrypt symmetrically +<<<<<<< d4e2ecd2e1164b6a5106b20d135742a35656838f i := mrand.Int() % 4 fsym.Topics[i] = params.Topic fasym.Topics[i] = params.Topic +======= + i := rand.Int() % 4 + fsym.Topics[i] = params.Topic[:] + fasym.Topics[i] = params.Topic[:] +>>>>>>> whisper: variable topic size allowed for a filter msg = NewSentMessage(params) env, err = msg.Wrap(params) if err != nil { @@ -321,7 +327,7 @@ func TestMatchMessageSym(t *testing.T) { const index = 1 params.KeySym = f.KeySym - params.Topic = f.Topics[index] + params.Topic = BytesToTopic(f.Topics[index]) sentMessage := NewSentMessage(params) env, err := sentMessage.Wrap(params) @@ -414,7 +420,7 @@ func TestMatchMessageAsym(t *testing.T) { } const index = 1 - params.Topic = f.Topics[index] + params.Topic = BytesToTopic(f.Topics[index]) params.Dst = &f.KeyAsym.PublicKey keySymOrig := params.KeySym params.KeySym = nil @@ -505,7 +511,7 @@ func generateCompatibeEnvelope(t *testing.T, f *Filter) *Envelope { } params.KeySym = f.KeySym - params.Topic = f.Topics[2] + params.Topic = BytesToTopic(f.Topics[2]) sentMessage := NewSentMessage(params) env, err := sentMessage.Wrap(params) if err != nil { diff --git a/whisper/whisperv5/peer_test.go b/whisper/whisperv5/peer_test.go index 7d8fc1b696..47c9942cb6 100644 --- a/whisper/whisperv5/peer_test.go +++ b/whisper/whisperv5/peer_test.go @@ -118,7 +118,9 @@ func initialize(t *testing.T) { node.shh.Start(nil) topics := make([]TopicType, 0) topics = append(topics, sharedTopic) - f := Filter{KeySym: sharedKey, Topics: topics} + f := Filter{KeySym: sharedKey} + f.Topics = make([][]byte, 1) + f.Topics[0] = topics[0][:] node.filerId, err = node.shh.Watch(&f) if err != nil { t.Fatalf("failed to install the filter: %s.", err) diff --git a/whisper/whisperv5/whisper_test.go b/whisper/whisperv5/whisper_test.go index 26d0da3ad2..fba843b554 100644 --- a/whisper/whisperv5/whisper_test.go +++ b/whisper/whisperv5/whisper_test.go @@ -512,7 +512,7 @@ func TestCustomization(t *testing.T) { } params.KeySym = f.KeySym - params.Topic = f.Topics[2] + params.Topic = BytesToTopic(f.Topics[2]) params.PoW = smallPoW params.TTL = 3600 * 24 // one day msg := NewSentMessage(params)