2017-11-03 20:29:49 +00:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package whisperv6
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
)
|
|
|
|
|
2018-01-26 11:45:10 +00:00
|
|
|
// Filter represents a Whisper message filter
|
2017-11-03 20:29:49 +00:00
|
|
|
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 [][]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
|
2018-02-27 20:16:15 +00:00
|
|
|
id string // unique identifier
|
2017-11-03 20:29:49 +00:00
|
|
|
|
|
|
|
Messages map[common.Hash]*ReceivedMessage
|
|
|
|
mutex sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2018-01-26 11:45:10 +00:00
|
|
|
// Filters represents a collection of filters
|
2017-11-03 20:29:49 +00:00
|
|
|
type Filters struct {
|
2018-02-28 14:05:35 +00:00
|
|
|
watchers map[string]*Filter
|
|
|
|
|
|
|
|
topicMatcher map[TopicType]map[*Filter]struct{} // map a topic to the filters that are interested in being notified when a message matches that topic
|
|
|
|
allTopicsMatcher map[*Filter]struct{} // list all the filters that will be notified of a new message, no matter what its topic is
|
2018-02-27 20:16:15 +00:00
|
|
|
|
|
|
|
whisper *Whisper
|
|
|
|
mutex sync.RWMutex
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 11:45:10 +00:00
|
|
|
// NewFilters returns a newly created filter collection
|
2017-11-03 20:29:49 +00:00
|
|
|
func NewFilters(w *Whisper) *Filters {
|
|
|
|
return &Filters{
|
2018-02-27 20:16:15 +00:00
|
|
|
watchers: make(map[string]*Filter),
|
|
|
|
topicMatcher: make(map[TopicType]map[*Filter]struct{}),
|
|
|
|
allTopicsMatcher: make(map[*Filter]struct{}),
|
|
|
|
whisper: w,
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 11:45:10 +00:00
|
|
|
// Install will add a new filter to the filter collection
|
2017-11-03 20:29:49 +00:00
|
|
|
func (fs *Filters) Install(watcher *Filter) (string, error) {
|
2017-12-08 10:40:59 +00:00
|
|
|
if watcher.KeySym != nil && watcher.KeyAsym != nil {
|
|
|
|
return "", fmt.Errorf("filters must choose between symmetric and asymmetric keys")
|
|
|
|
}
|
|
|
|
|
2017-11-03 20:29:49 +00:00
|
|
|
if watcher.Messages == nil {
|
|
|
|
watcher.Messages = make(map[common.Hash]*ReceivedMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := GenerateRandomID()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.mutex.Lock()
|
|
|
|
defer fs.mutex.Unlock()
|
|
|
|
|
|
|
|
if fs.watchers[id] != nil {
|
|
|
|
return "", fmt.Errorf("failed to generate unique ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
if watcher.expectsSymmetricEncryption() {
|
|
|
|
watcher.SymKeyHash = crypto.Keccak256Hash(watcher.KeySym)
|
|
|
|
}
|
|
|
|
|
2018-02-27 20:16:15 +00:00
|
|
|
watcher.id = id
|
2017-11-03 20:29:49 +00:00
|
|
|
fs.watchers[id] = watcher
|
2018-02-27 20:16:15 +00:00
|
|
|
fs.addTopicMatcher(watcher)
|
2017-11-03 20:29:49 +00:00
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
2018-01-26 11:45:10 +00:00
|
|
|
// Uninstall will remove a filter whose id has been specified from
|
|
|
|
// the filter collection
|
2017-11-03 20:29:49 +00:00
|
|
|
func (fs *Filters) Uninstall(id string) bool {
|
|
|
|
fs.mutex.Lock()
|
|
|
|
defer fs.mutex.Unlock()
|
|
|
|
if fs.watchers[id] != nil {
|
2018-02-27 20:16:15 +00:00
|
|
|
fs.removeFromTopicMatchers(fs.watchers[id])
|
2017-11-03 20:29:49 +00:00
|
|
|
delete(fs.watchers, id)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-02-28 14:05:35 +00:00
|
|
|
// addTopicMatcher adds a filter to the topic matchers.
|
|
|
|
// If the filter's Topics array is empty, it will be tried on every topic.
|
|
|
|
// Otherwise, it will be tried on the topics specified.
|
2018-02-27 20:16:15 +00:00
|
|
|
func (fs *Filters) addTopicMatcher(watcher *Filter) {
|
|
|
|
if len(watcher.Topics) == 0 {
|
|
|
|
fs.allTopicsMatcher[watcher] = struct{}{}
|
|
|
|
} else {
|
|
|
|
for _, t := range watcher.Topics {
|
|
|
|
topic := BytesToTopic(t)
|
|
|
|
if fs.topicMatcher[topic] == nil {
|
|
|
|
fs.topicMatcher[topic] = make(map[*Filter]struct{})
|
|
|
|
}
|
|
|
|
fs.topicMatcher[topic][watcher] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeFromTopicMatchers removes a filter from the topic matchers
|
|
|
|
func (fs *Filters) removeFromTopicMatchers(watcher *Filter) {
|
|
|
|
delete(fs.allTopicsMatcher, watcher)
|
|
|
|
for _, topic := range watcher.Topics {
|
|
|
|
delete(fs.topicMatcher[BytesToTopic(topic)], watcher)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getWatchersByTopic returns a slice containing the filters that
|
|
|
|
// match a specific topic
|
|
|
|
func (fs *Filters) getWatchersByTopic(topic TopicType) []*Filter {
|
|
|
|
res := make([]*Filter, 0, len(fs.allTopicsMatcher))
|
2018-02-28 14:05:35 +00:00
|
|
|
for watcher := range fs.allTopicsMatcher {
|
2018-02-27 20:16:15 +00:00
|
|
|
res = append(res, watcher)
|
|
|
|
}
|
2018-02-28 14:05:35 +00:00
|
|
|
for watcher := range fs.topicMatcher[topic] {
|
2018-02-27 20:16:15 +00:00
|
|
|
res = append(res, watcher)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-01-26 11:45:10 +00:00
|
|
|
// Get returns a filter from the collection with a specific ID
|
2017-11-03 20:29:49 +00:00
|
|
|
func (fs *Filters) Get(id string) *Filter {
|
|
|
|
fs.mutex.RLock()
|
|
|
|
defer fs.mutex.RUnlock()
|
|
|
|
return fs.watchers[id]
|
|
|
|
}
|
|
|
|
|
2018-01-26 11:45:10 +00:00
|
|
|
// NotifyWatchers notifies any filter that has declared interest
|
|
|
|
// for the envelope's topic.
|
2017-11-03 20:29:49 +00:00
|
|
|
func (fs *Filters) NotifyWatchers(env *Envelope, p2pMessage bool) {
|
|
|
|
var msg *ReceivedMessage
|
|
|
|
|
|
|
|
fs.mutex.RLock()
|
|
|
|
defer fs.mutex.RUnlock()
|
|
|
|
|
2018-02-27 20:16:15 +00:00
|
|
|
candidates := fs.getWatchersByTopic(env.Topic)
|
|
|
|
for _, watcher := range candidates {
|
2017-11-03 20:29:49 +00:00
|
|
|
if p2pMessage && !watcher.AllowP2P {
|
2018-02-27 20:16:15 +00:00
|
|
|
log.Trace(fmt.Sprintf("msg [%x], filter [%s]: p2p messages are not allowed", env.Hash(), watcher.id))
|
2017-11-03 20:29:49 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var match bool
|
|
|
|
if msg != nil {
|
|
|
|
match = watcher.MatchMessage(msg)
|
|
|
|
} else {
|
|
|
|
match = watcher.MatchEnvelope(env)
|
|
|
|
if match {
|
|
|
|
msg = env.Open(watcher)
|
|
|
|
if msg == nil {
|
2018-02-27 20:16:15 +00:00
|
|
|
log.Trace("processing message: failed to open", "message", env.Hash().Hex(), "filter", watcher.id)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-02-27 20:16:15 +00:00
|
|
|
log.Trace("processing message: does not match", "message", env.Hash().Hex(), "filter", watcher.id)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if match && msg != nil {
|
|
|
|
log.Trace("processing message: decrypted", "hash", env.Hash().Hex())
|
|
|
|
if watcher.Src == nil || IsPubKeyEqual(msg.Src, watcher.Src) {
|
|
|
|
watcher.Trigger(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filter) expectsAsymmetricEncryption() bool {
|
|
|
|
return f.KeyAsym != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filter) expectsSymmetricEncryption() bool {
|
|
|
|
return f.KeySym != nil
|
|
|
|
}
|
|
|
|
|
2018-01-26 11:45:10 +00:00
|
|
|
// Trigger adds a yet-unknown message to the filter's list of
|
|
|
|
// received messages.
|
2017-11-03 20:29:49 +00:00
|
|
|
func (f *Filter) Trigger(msg *ReceivedMessage) {
|
|
|
|
f.mutex.Lock()
|
|
|
|
defer f.mutex.Unlock()
|
|
|
|
|
|
|
|
if _, exist := f.Messages[msg.EnvelopeHash]; !exist {
|
|
|
|
f.Messages[msg.EnvelopeHash] = msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 11:45:10 +00:00
|
|
|
// Retrieve will return the list of all received messages associated
|
|
|
|
// to a filter.
|
2017-11-03 20:29:49 +00:00
|
|
|
func (f *Filter) Retrieve() (all []*ReceivedMessage) {
|
|
|
|
f.mutex.Lock()
|
|
|
|
defer f.mutex.Unlock()
|
|
|
|
|
|
|
|
all = make([]*ReceivedMessage, 0, len(f.Messages))
|
|
|
|
for _, msg := range f.Messages {
|
|
|
|
all = append(all, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
f.Messages = make(map[common.Hash]*ReceivedMessage) // delete old messages
|
|
|
|
return all
|
|
|
|
}
|
|
|
|
|
2017-12-08 10:40:59 +00:00
|
|
|
// MatchMessage checks if the filter matches an already decrypted
|
|
|
|
// message (i.e. a Message that has already been handled by
|
2018-02-27 22:38:20 +00:00
|
|
|
// MatchEnvelope when checked by a previous filter).
|
|
|
|
// Topics are not checked here, since this is done by topic matchers.
|
2017-11-03 20:29:49 +00:00
|
|
|
func (f *Filter) MatchMessage(msg *ReceivedMessage) bool {
|
|
|
|
if f.PoW > 0 && msg.PoW < f.PoW {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.expectsAsymmetricEncryption() && msg.isAsymmetricEncryption() {
|
2018-02-27 22:38:20 +00:00
|
|
|
return IsPubKeyEqual(&f.KeyAsym.PublicKey, msg.Dst)
|
2017-11-03 20:29:49 +00:00
|
|
|
} else if f.expectsSymmetricEncryption() && msg.isSymmetricEncryption() {
|
2018-02-27 22:38:20 +00:00
|
|
|
return f.SymKeyHash == msg.SymKeyHash
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-01-26 11:45:10 +00:00
|
|
|
// MatchEnvelope checks if it's worth decrypting the message. If
|
2017-12-08 10:40:59 +00:00
|
|
|
// it returns `true`, client code is expected to attempt decrypting
|
|
|
|
// the message and subsequently call MatchMessage.
|
2018-02-27 22:38:20 +00:00
|
|
|
// Topics are not checked here, since this is done by topic matchers.
|
2017-11-03 20:29:49 +00:00
|
|
|
func (f *Filter) MatchEnvelope(envelope *Envelope) bool {
|
2018-02-27 22:38:20 +00:00
|
|
|
return f.PoW <= 0 || envelope.pow >= f.PoW
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func matchSingleTopic(topic TopicType, bt []byte) bool {
|
2018-01-26 11:41:53 +00:00
|
|
|
if len(bt) > TopicLength {
|
|
|
|
bt = bt[:TopicLength]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bt) < TopicLength {
|
|
|
|
return false
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for j, b := range bt {
|
|
|
|
if topic[j] != b {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-01-26 11:45:10 +00:00
|
|
|
// IsPubKeyEqual checks that two public keys are equal
|
2017-11-03 20:29:49 +00:00
|
|
|
func IsPubKeyEqual(a, b *ecdsa.PublicKey) bool {
|
|
|
|
if !ValidatePublicKey(a) {
|
|
|
|
return false
|
|
|
|
} else if !ValidatePublicKey(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// the curve is always the same, just compare the points
|
|
|
|
return a.X.Cmp(b.X) == 0 && a.Y.Cmp(b.Y) == 0
|
|
|
|
}
|