Added manual triggering of filters

This commit is contained in:
obscuren 2015-01-12 20:36:45 +01:00
parent 8a1b51c716
commit 34689cb3f3
5 changed files with 51 additions and 9 deletions

View File

@ -150,7 +150,7 @@ var shhWatchMethods = function () {
return [ return [
{ name: 'newFilter', call: 'shh_newFilter' }, { name: 'newFilter', call: 'shh_newFilter' },
{ name: 'uninstallFilter', call: 'shh_uninstallFilter' }, { name: 'uninstallFilter', call: 'shh_uninstallFilter' },
{ name: 'getMessage', call: 'shh_getMessages' } { name: 'getMessages', call: 'shh_getMessages' }
]; ];
}; };

View File

@ -310,7 +310,7 @@ Rectangle {
postData(data._id, id); postData(data._id, id);
break; break;
case "eth_messages": case "eth_filterLogs":
require(1); require(1);
var messages = eth.messages(data.args[0]); var messages = eth.messages(data.args[0]);
@ -352,6 +352,13 @@ Rectangle {
shh.post(params.payload, params.to, params.from, params.topics, params.priority, params.ttl); shh.post(params.payload, params.to, params.from, params.topics, params.priority, params.ttl);
break;
case "shh_getMessages":
require(1);
shh.trigger(data.args[0]);
break; break;
} }
} catch(e) { } catch(e) {

View File

@ -68,3 +68,11 @@ out:
} }
} }
} }
func (self *Filters) Match(a, b Filter) bool {
return reflect.TypeOf(a) == reflect.TypeOf(b) && a.Compare(b)
}
func (self *Filters) Get(i int) Filter {
return self.watchers[i]
}

View File

@ -84,6 +84,10 @@ func (self *Whisper) Watch(opts map[string]interface{}, view *qml.Common) int {
return i return i
} }
func (self *Whisper) Trigger(id int) {
go self.Whisper.Trigger(id)
}
func filterFromMap(opts map[string]interface{}) (f whisper.Filter) { func filterFromMap(opts map[string]interface{}) (f whisper.Filter) {
if to, ok := opts["to"].(string); ok { if to, ok := opts["to"].(string); ok {
f.To = crypto.ToECDSA(fromHex(to)) f.To = crypto.ToECDSA(fromHex(to))

View File

@ -126,6 +126,20 @@ func (self *Whisper) Watch(opts Filter) int {
}) })
} }
func (self *Whisper) Trigger(id int) {
filter := self.filters.Get(id)
if filter != nil {
for _, e := range self.messages {
if msg, key := self.open(e); msg != nil {
f := createFilter(msg, e.Topics, key)
if self.filters.Match(filter, f) {
self.filters.Notify(f, msg)
}
}
}
}
}
// Main handler for passing whisper messages to whisper peer objects // Main handler for passing whisper messages to whisper peer objects
func (self *Whisper) msgHandler(peer *p2p.Peer, ws p2p.MsgReadWriter) error { func (self *Whisper) msgHandler(peer *p2p.Peer, ws p2p.MsgReadWriter) error {
wpeer := NewPeer(self, peer, ws) wpeer := NewPeer(self, peer, ws)
@ -227,19 +241,28 @@ func (self *Whisper) envelopes() (envelopes []*Envelope) {
} }
func (self *Whisper) postEvent(envelope *Envelope) { func (self *Whisper) postEvent(envelope *Envelope) {
if message, key := self.open(envelope); message != nil {
self.filters.Notify(createFilter(message, envelope.Topics, key), message)
}
}
func (self *Whisper) open(envelope *Envelope) (*Message, *ecdsa.PrivateKey) {
for _, key := range self.keys { for _, key := range self.keys {
if message, err := envelope.Open(key); err == nil || (err != nil && err == ecies.ErrInvalidPublicKey) { if message, err := envelope.Open(key); err == nil || (err != nil && err == ecies.ErrInvalidPublicKey) {
self.filters.Notify(filter.Generic{ return message, key
Str1: string(crypto.FromECDSA(key)), Str2: string(crypto.FromECDSAPub(message.Recover())),
Data: bytesToMap(envelope.Topics),
}, message)
break
} else {
wlogger.Infoln(err)
} }
} }
return nil, nil
} }
func (self *Whisper) Protocol() p2p.Protocol { func (self *Whisper) Protocol() p2p.Protocol {
return self.protocol return self.protocol
} }
func createFilter(message *Message, topics [][]byte, key *ecdsa.PrivateKey) filter.Filter {
return filter.Generic{
Str1: string(crypto.FromECDSA(key)), Str2: string(crypto.FromECDSAPub(message.Recover())),
Data: bytesToMap(topics),
}
}