2014-12-12 23:24:41 +02:00
|
|
|
package qwhisper
|
|
|
|
|
|
|
|
import (
|
2014-12-15 18:14:02 +02:00
|
|
|
"fmt"
|
2014-12-12 23:24:41 +02:00
|
|
|
"time"
|
|
|
|
|
2014-12-12 23:38:54 +02:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2014-12-12 23:29:29 +02:00
|
|
|
"github.com/ethereum/go-ethereum/ethutil"
|
2014-12-12 23:24:41 +02:00
|
|
|
"github.com/ethereum/go-ethereum/whisper"
|
2014-12-15 18:14:02 +02:00
|
|
|
"gopkg.in/qml.v1"
|
2014-12-12 23:24:41 +02: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
|
2014-12-15 18:14:02 +02:00
|
|
|
view qml.Object
|
2014-12-12 23:24:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(w *whisper.Whisper) *Whisper {
|
2014-12-15 18:14:02 +02:00
|
|
|
return &Whisper{w, nil}
|
2014-12-12 23:24:41 +02:00
|
|
|
}
|
|
|
|
|
2014-12-15 18:14:02 +02:00
|
|
|
func (self *Whisper) SetView(view qml.Object) {
|
|
|
|
self.view = view
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Whisper) Post(data string, to, from string, topics []string, pow, ttl uint32) {
|
2014-12-12 23:24:41 +02:00
|
|
|
msg := whisper.NewMessage(fromHex(data))
|
|
|
|
envelope, err := msg.Seal(time.Duration(pow), whisper.Opts{
|
2014-12-15 18:14:02 +02:00
|
|
|
Ttl: time.Duration(ttl),
|
|
|
|
To: crypto.ToECDSAPub(fromHex(to)),
|
|
|
|
From: crypto.ToECDSA(fromHex(from)),
|
|
|
|
Topics: whisper.TopicsFromString(topics),
|
2014-12-12 23:24:41 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2014-12-15 18:14:02 +02:00
|
|
|
fmt.Println(err)
|
2014-12-12 23:24:41 +02:00
|
|
|
// handle error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-12-12 23:38:54 +02:00
|
|
|
if err := self.Whisper.Send(envelope); err != nil {
|
2014-12-15 18:14:02 +02:00
|
|
|
fmt.Println(err)
|
2014-12-12 23:24:41 +02:00
|
|
|
// handle error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Whisper) NewIdentity() string {
|
|
|
|
return toHex(self.Whisper.NewIdentity().D.Bytes())
|
|
|
|
}
|
|
|
|
|
2014-12-15 18:28:51 +02:00
|
|
|
func (self *Whisper) HasIdentity(key string) bool {
|
2014-12-12 23:24:41 +02:00
|
|
|
return self.Whisper.HasIdentity(crypto.ToECDSA(fromHex(key)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Whisper) Watch(opts map[string]interface{}) {
|
|
|
|
filter := filterFromMap(opts)
|
2014-12-12 23:38:54 +02:00
|
|
|
filter.Fn = func(msg *whisper.Message) {
|
2014-12-12 23:24:41 +02:00
|
|
|
// TODO POST TO QT WINDOW
|
|
|
|
}
|
2014-12-12 23:38:54 +02:00
|
|
|
self.Whisper.Watch(filter)
|
2014-12-12 23:24:41 +02:00
|
|
|
}
|
|
|
|
|
2014-12-12 23:38:54 +02:00
|
|
|
func filterFromMap(opts map[string]interface{}) (f whisper.Filter) {
|
2014-12-12 23:24:41 +02:00
|
|
|
if to, ok := opts["to"].(string); ok {
|
2014-12-12 23:38:54 +02:00
|
|
|
f.To = crypto.ToECDSA(fromHex(to))
|
2014-12-12 23:24:41 +02:00
|
|
|
}
|
|
|
|
if from, ok := opts["from"].(string); ok {
|
2014-12-12 23:38:54 +02:00
|
|
|
f.From = crypto.ToECDSAPub(fromHex(from))
|
2014-12-12 23:24:41 +02:00
|
|
|
}
|
|
|
|
|
2014-12-12 23:38:54 +02:00
|
|
|
return
|
2014-12-12 23:24:41 +02:00
|
|
|
}
|