plugeth/ui/qt/qwhisper/whisper.go

114 lines
2.6 KiB
Go
Raw Normal View History

// QWhisper package. This package is temporarily on hold until QML DApp dev will reemerge.
2014-12-12 21:24:41 +00:00
package qwhisper
import (
"time"
2015-03-16 10:27:38 +00:00
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
2015-01-08 20:41:32 +00:00
"github.com/ethereum/go-ethereum/logger"
2014-12-12 21:24:41 +00:00
"github.com/ethereum/go-ethereum/whisper"
2015-01-28 13:51:54 +00:00
"github.com/obscuren/qml"
2014-12-12 21:24:41 +00:00
)
2015-01-08 20:41:32 +00:00
var qlogger = logger.NewLogger("QSHH")
2014-12-12 21:24:41 +00:00
type Whisper struct {
*whisper.Whisper
view qml.Object
2014-12-16 18:55:57 +00:00
watches map[int]*Watch
2014-12-12 21:24:41 +00:00
}
func New(w *whisper.Whisper) *Whisper {
2014-12-16 18:55:57 +00:00
return &Whisper{w, nil, make(map[int]*Watch)}
2014-12-12 21:24:41 +00:00
}
func (self *Whisper) SetView(view qml.Object) {
self.view = view
}
2014-12-22 13:59:52 +00:00
func (self *Whisper) Post(payload []string, to, from string, topics []string, priority, ttl uint32) {
var data []byte
for _, d := range payload {
2015-03-16 10:27:38 +00:00
data = append(data, common.FromHex(d)...)
2014-12-22 13:59:52 +00:00
}
2015-03-16 10:27:38 +00:00
pk := crypto.ToECDSAPub(common.FromHex(from))
if key := self.Whisper.GetIdentity(pk); key != nil {
msg := whisper.NewMessage(data)
envelope, err := msg.Wrap(time.Duration(priority*100000), whisper.Options{
TTL: time.Duration(ttl) * time.Second,
2015-03-16 10:27:38 +00:00
To: crypto.ToECDSAPub(common.FromHex(to)),
From: key,
Topics: whisper.NewTopicsFromStrings(topics...),
})
if err != nil {
qlogger.Infoln(err)
// handle error
return
}
2014-12-12 21:24:41 +00:00
if err := self.Whisper.Send(envelope); err != nil {
qlogger.Infoln(err)
// handle error
return
}
} else {
qlogger.Infoln("unmatched pub / priv for seal")
2014-12-12 21:24:41 +00:00
}
2014-12-12 21:24:41 +00:00
}
func (self *Whisper) NewIdentity() string {
key := self.Whisper.NewIdentity()
return common.ToHex(crypto.FromECDSAPub(&key.PublicKey))
2014-12-12 21:24:41 +00:00
}
2014-12-15 16:28:51 +00:00
func (self *Whisper) HasIdentity(key string) bool {
2015-03-16 10:27:38 +00:00
return self.Whisper.HasIdentity(crypto.ToECDSAPub(common.FromHex(key)))
2014-12-12 21:24:41 +00:00
}
func (self *Whisper) Watch(opts map[string]interface{}, view *qml.Common) int {
2014-12-12 21:24:41 +00:00
filter := filterFromMap(opts)
2014-12-22 12:23:11 +00:00
var i int
2014-12-12 21:38:54 +00:00
filter.Fn = func(msg *whisper.Message) {
if view != nil {
2014-12-22 12:23:11 +00:00
view.Call("onShhMessage", ToQMessage(msg), i)
}
2014-12-12 21:24:41 +00:00
}
2014-12-22 12:23:11 +00:00
i = self.Whisper.Watch(filter)
2014-12-16 18:55:57 +00:00
self.watches[i] = &Watch{}
return i
2014-12-12 21:24:41 +00:00
}
2015-03-16 10:27:38 +00:00
func (self *Whisper) Messages(id int) (messages *common.List) {
msgs := self.Whisper.Messages(id)
2015-03-16 10:27:38 +00:00
messages = common.EmptyList()
for _, message := range msgs {
messages.Append(ToQMessage(message))
}
return
2015-01-12 19:36:45 +00:00
}
2014-12-12 21:38:54 +00:00
func filterFromMap(opts map[string]interface{}) (f whisper.Filter) {
2014-12-12 21:24:41 +00:00
if to, ok := opts["to"].(string); ok {
2015-03-16 10:27:38 +00:00
f.To = crypto.ToECDSAPub(common.FromHex(to))
2014-12-12 21:24:41 +00:00
}
if from, ok := opts["from"].(string); ok {
2015-03-16 10:27:38 +00:00
f.From = crypto.ToECDSAPub(common.FromHex(from))
2014-12-12 21:24:41 +00:00
}
2014-12-16 18:55:57 +00:00
if topicList, ok := opts["topics"].(*qml.List); ok {
var topics []string
topicList.Convert(&topics)
f.Topics = whisper.NewTopicsFromStrings(topics...)
2014-12-16 18:55:57 +00:00
}
2014-12-12 21:24:41 +00:00
2014-12-12 21:38:54 +00:00
return
2014-12-12 21:24:41 +00:00
}