quorum/ui/qt/qwhisper/whisper.go

112 lines
2.4 KiB
Go
Raw Normal View History

2014-12-12 13:24:41 -08:00
package qwhisper
import (
"time"
2014-12-12 13:38:54 -08:00
"github.com/ethereum/go-ethereum/crypto"
2014-12-12 13:29:29 -08:00
"github.com/ethereum/go-ethereum/ethutil"
2015-01-08 12:41:32 -08:00
"github.com/ethereum/go-ethereum/logger"
2014-12-12 13:24:41 -08:00
"github.com/ethereum/go-ethereum/whisper"
"gopkg.in/qml.v1"
2014-12-12 13:24:41 -08:00
)
2015-01-08 12:41:32 -08:00
var qlogger = logger.NewLogger("QSHH")
2014-12-12 13:24:41 -08:00
func fromHex(s string) []byte {
if len(s) > 1 {
return ethutil.Hex2Bytes(s[2:])
}
return nil
}
func toHex(b []byte) string { return "0x" + ethutil.Bytes2Hex(b) }
type Whisper struct {
*whisper.Whisper
view qml.Object
2014-12-16 10:55:57 -08:00
watches map[int]*Watch
2014-12-12 13:24:41 -08:00
}
func New(w *whisper.Whisper) *Whisper {
2014-12-16 10:55:57 -08:00
return &Whisper{w, nil, make(map[int]*Watch)}
2014-12-12 13:24:41 -08:00
}
func (self *Whisper) SetView(view qml.Object) {
self.view = view
}
2014-12-22 05:59:52 -08:00
func (self *Whisper) Post(payload []string, to, from string, topics []string, priority, ttl uint32) {
var data []byte
for _, d := range payload {
2015-01-09 04:36:47 -08:00
data = append(data, fromHex(d)...)
2014-12-22 05:59:52 -08:00
}
msg := whisper.NewMessage(data)
2014-12-22 04:23:11 -08:00
envelope, err := msg.Seal(time.Duration(priority*100000), whisper.Opts{
Ttl: time.Duration(ttl) * time.Second,
To: crypto.ToECDSAPub(fromHex(to)),
From: crypto.ToECDSA(fromHex(from)),
2014-12-16 10:55:57 -08:00
Topics: whisper.TopicsFromString(topics...),
2014-12-12 13:24:41 -08:00
})
if err != nil {
2015-01-08 12:41:32 -08:00
qlogger.Infoln(err)
2014-12-12 13:24:41 -08:00
// handle error
return
}
2014-12-12 13:38:54 -08:00
if err := self.Whisper.Send(envelope); err != nil {
2015-01-08 12:41:32 -08:00
qlogger.Infoln(err)
2014-12-12 13:24:41 -08:00
// handle error
return
}
}
func (self *Whisper) NewIdentity() string {
return toHex(self.Whisper.NewIdentity().D.Bytes())
}
2014-12-15 08:28:51 -08:00
func (self *Whisper) HasIdentity(key string) bool {
2014-12-12 13:24:41 -08:00
return self.Whisper.HasIdentity(crypto.ToECDSA(fromHex(key)))
}
func (self *Whisper) Watch(opts map[string]interface{}, view *qml.Common) int {
2014-12-12 13:24:41 -08:00
filter := filterFromMap(opts)
2014-12-22 04:23:11 -08:00
var i int
2014-12-12 13:38:54 -08:00
filter.Fn = func(msg *whisper.Message) {
if view != nil {
2014-12-22 04:23:11 -08:00
view.Call("onShhMessage", ToQMessage(msg), i)
}
2014-12-12 13:24:41 -08:00
}
2014-12-22 04:23:11 -08:00
i = self.Whisper.Watch(filter)
2014-12-16 10:55:57 -08:00
self.watches[i] = &Watch{}
return i
2014-12-12 13:24:41 -08:00
}
func (self *Whisper) Messages(id int) (messages *ethutil.List) {
msgs := self.Whisper.Messages(id)
messages = ethutil.EmptyList()
for _, message := range msgs {
messages.Append(ToQMessage(message))
}
return
2015-01-12 11:36:45 -08:00
}
2014-12-12 13:38:54 -08:00
func filterFromMap(opts map[string]interface{}) (f whisper.Filter) {
2014-12-12 13:24:41 -08:00
if to, ok := opts["to"].(string); ok {
2014-12-12 13:38:54 -08:00
f.To = crypto.ToECDSA(fromHex(to))
2014-12-12 13:24:41 -08:00
}
if from, ok := opts["from"].(string); ok {
2014-12-12 13:38:54 -08:00
f.From = crypto.ToECDSAPub(fromHex(from))
2014-12-12 13:24:41 -08:00
}
2014-12-16 10:55:57 -08:00
if topicList, ok := opts["topics"].(*qml.List); ok {
var topics []string
topicList.Convert(&topics)
f.Topics = whisper.TopicsFromString(topics...)
}
2014-12-12 13:24:41 -08:00
2014-12-12 13:38:54 -08:00
return
2014-12-12 13:24:41 -08:00
}