2016-10-29 07:11:37 -05: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 whisperv5
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2017-02-23 02:41:47 -06:00
|
|
|
"fmt"
|
2016-10-29 07:11:37 -05:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-10-06 09:04:21 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-02-22 06:10:07 -06:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2016-10-29 07:11:37 -05: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
|
2017-04-09 16:49:22 -05:00
|
|
|
Topics [][]byte // Topics to filter messages with
|
2016-10-29 07:11:37 -05:00
|
|
|
PoW float64 // Proof of work as described in the Whisper spec
|
2017-04-09 16:49:22 -05:00
|
|
|
AllowP2P bool // Indicates whether this filter is interested in direct peer-to-peer messages
|
2016-10-29 07:11:37 -05:00
|
|
|
SymKeyHash common.Hash // The Keccak256Hash of the symmetric key, needed for optimization
|
|
|
|
|
|
|
|
Messages map[common.Hash]*ReceivedMessage
|
|
|
|
mutex sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type Filters struct {
|
2017-02-23 02:41:47 -06:00
|
|
|
watchers map[string]*Filter
|
2016-10-29 07:11:37 -05:00
|
|
|
whisper *Whisper
|
|
|
|
mutex sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFilters(w *Whisper) *Filters {
|
|
|
|
return &Filters{
|
2017-02-23 02:41:47 -06:00
|
|
|
watchers: make(map[string]*Filter),
|
2016-10-29 07:11:37 -05:00
|
|
|
whisper: w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 02:41:47 -06:00
|
|
|
func (fs *Filters) Install(watcher *Filter) (string, error) {
|
2016-10-29 07:11:37 -05:00
|
|
|
if watcher.Messages == nil {
|
|
|
|
watcher.Messages = make(map[common.Hash]*ReceivedMessage)
|
|
|
|
}
|
|
|
|
|
2017-04-09 16:49:22 -05:00
|
|
|
id, err := GenerateRandomID()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-10-29 07:11:37 -05:00
|
|
|
fs.mutex.Lock()
|
|
|
|
defer fs.mutex.Unlock()
|
|
|
|
|
2017-04-09 16:49:22 -05:00
|
|
|
if fs.watchers[id] != nil {
|
|
|
|
return "", fmt.Errorf("failed to generate unique ID")
|
2017-02-23 02:41:47 -06:00
|
|
|
}
|
2017-04-09 16:49:22 -05:00
|
|
|
|
2017-10-06 09:04:21 -05:00
|
|
|
if watcher.expectsSymmetricEncryption() {
|
|
|
|
watcher.SymKeyHash = crypto.Keccak256Hash(watcher.KeySym)
|
|
|
|
}
|
|
|
|
|
2017-04-09 16:49:22 -05:00
|
|
|
fs.watchers[id] = watcher
|
2017-02-23 02:41:47 -06:00
|
|
|
return id, err
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-04-09 16:49:22 -05:00
|
|
|
func (fs *Filters) Uninstall(id string) bool {
|
2016-10-29 07:11:37 -05:00
|
|
|
fs.mutex.Lock()
|
|
|
|
defer fs.mutex.Unlock()
|
2017-04-09 16:49:22 -05:00
|
|
|
if fs.watchers[id] != nil {
|
|
|
|
delete(fs.watchers, id)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-02-23 02:41:47 -06:00
|
|
|
func (fs *Filters) Get(id string) *Filter {
|
2016-10-29 07:11:37 -05:00
|
|
|
fs.mutex.RLock()
|
|
|
|
defer fs.mutex.RUnlock()
|
2017-02-23 02:41:47 -06:00
|
|
|
return fs.watchers[id]
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2016-12-19 17:58:01 -06:00
|
|
|
func (fs *Filters) NotifyWatchers(env *Envelope, p2pMessage bool) {
|
2016-10-29 07:11:37 -05:00
|
|
|
var msg *ReceivedMessage
|
2017-04-09 16:49:22 -05:00
|
|
|
|
|
|
|
fs.mutex.RLock()
|
|
|
|
defer fs.mutex.RUnlock()
|
|
|
|
|
|
|
|
i := -1 // only used for logging info
|
|
|
|
for _, watcher := range fs.watchers {
|
|
|
|
i++
|
|
|
|
if p2pMessage && !watcher.AllowP2P {
|
|
|
|
log.Trace(fmt.Sprintf("msg [%x], filter [%d]: p2p messages are not allowed", env.Hash(), i))
|
2016-10-29 07:11:37 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-01-09 04:16:06 -06:00
|
|
|
var match bool
|
2016-10-29 07:11:37 -05:00
|
|
|
if msg != nil {
|
|
|
|
match = watcher.MatchMessage(msg)
|
|
|
|
} else {
|
|
|
|
match = watcher.MatchEnvelope(env)
|
|
|
|
if match {
|
|
|
|
msg = env.Open(watcher)
|
2016-12-19 17:58:01 -06:00
|
|
|
if msg == nil {
|
2017-04-09 16:49:22 -05:00
|
|
|
log.Trace("processing message: failed to open", "message", env.Hash().Hex(), "filter", i)
|
2016-12-19 17:58:01 -06:00
|
|
|
}
|
|
|
|
} else {
|
2017-04-09 16:49:22 -05:00
|
|
|
log.Trace("processing message: does not match", "message", env.Hash().Hex(), "filter", i)
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if match && msg != nil {
|
2017-04-09 16:49:22 -05:00
|
|
|
log.Trace("processing message: decrypted", "hash", env.Hash().Hex())
|
2017-10-06 09:04:21 -05:00
|
|
|
if watcher.Src == nil || IsPubKeyEqual(msg.Src, watcher.Src) {
|
|
|
|
watcher.Trigger(msg)
|
|
|
|
}
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
}
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
2016-10-29 07:11:37 -05:00
|
|
|
|
2017-04-09 16:49:22 -05:00
|
|
|
func (f *Filter) processEnvelope(env *Envelope) *ReceivedMessage {
|
|
|
|
if f.MatchEnvelope(env) {
|
|
|
|
msg := env.Open(f)
|
|
|
|
if msg != nil {
|
|
|
|
return msg
|
|
|
|
} else {
|
|
|
|
log.Trace("processing envelope: failed to open", "hash", env.Hash().Hex())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Trace("processing envelope: does not match", "hash", env.Hash().Hex())
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
2017-04-09 16:49:22 -05:00
|
|
|
return nil
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filter) expectsAsymmetricEncryption() bool {
|
|
|
|
return f.KeyAsym != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filter) expectsSymmetricEncryption() bool {
|
|
|
|
return f.KeySym != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2017-06-13 04:49:07 -05:00
|
|
|
|
2016-10-29 07:11:37 -05:00
|
|
|
f.Messages = make(map[common.Hash]*ReceivedMessage) // delete old messages
|
|
|
|
return all
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filter) MatchMessage(msg *ReceivedMessage) bool {
|
|
|
|
if f.PoW > 0 && msg.PoW < f.PoW {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.expectsAsymmetricEncryption() && msg.isAsymmetricEncryption() {
|
2016-12-19 17:58:01 -06:00
|
|
|
return IsPubKeyEqual(&f.KeyAsym.PublicKey, msg.Dst) && f.MatchTopic(msg.Topic)
|
2016-10-29 07:11:37 -05:00
|
|
|
} else if f.expectsSymmetricEncryption() && msg.isSymmetricEncryption() {
|
2016-12-01 13:09:22 -06:00
|
|
|
return f.SymKeyHash == msg.SymKeyHash && f.MatchTopic(msg.Topic)
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filter) MatchEnvelope(envelope *Envelope) bool {
|
|
|
|
if f.PoW > 0 && envelope.pow < f.PoW {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.expectsAsymmetricEncryption() && envelope.isAsymmetric() {
|
2016-12-01 13:09:22 -06:00
|
|
|
return f.MatchTopic(envelope.Topic)
|
2016-10-29 07:11:37 -05:00
|
|
|
} else if f.expectsSymmetricEncryption() && envelope.IsSymmetric() {
|
2016-12-01 13:09:22 -06:00
|
|
|
return f.MatchTopic(envelope.Topic)
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
2016-12-01 13:09:22 -06:00
|
|
|
return false
|
|
|
|
}
|
2016-10-29 07:11:37 -05:00
|
|
|
|
2016-12-01 13:09:22 -06:00
|
|
|
func (f *Filter) MatchTopic(topic TopicType) bool {
|
|
|
|
if len(f.Topics) == 0 {
|
|
|
|
// any topic matches
|
|
|
|
return true
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-04-09 16:49:22 -05:00
|
|
|
for _, bt := range f.Topics {
|
|
|
|
if matchSingleTopic(topic, bt) {
|
2016-12-01 13:09:22 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2016-10-29 07:11:37 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-04-09 16:49:22 -05:00
|
|
|
func matchSingleTopic(topic TopicType, bt []byte) bool {
|
2018-01-26 05:41:53 -06:00
|
|
|
if len(bt) > TopicLength {
|
|
|
|
bt = bt[:TopicLength]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bt) < TopicLength {
|
|
|
|
return false
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for j, b := range bt {
|
|
|
|
if topic[j] != b {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-12-19 17:58:01 -06:00
|
|
|
func IsPubKeyEqual(a, b *ecdsa.PublicKey) bool {
|
2016-10-29 07:11:37 -05:00
|
|
|
if !ValidatePublicKey(a) {
|
|
|
|
return false
|
|
|
|
} else if !ValidatePublicKey(b) {
|
|
|
|
return false
|
|
|
|
}
|
2017-04-09 16:49:22 -05:00
|
|
|
// the curve is always the same, just compare the points
|
2016-10-29 07:11:37 -05:00
|
|
|
return a.X.Cmp(b.X) == 0 && a.Y.Cmp(b.Y) == 0
|
|
|
|
}
|