Updated QWhisper

* changed api
* general whisper debug interface
This commit is contained in:
obscuren 2014-12-21 15:06:35 +01:00
parent 125bdc3253
commit 0e5aed63dd
5 changed files with 80 additions and 13 deletions

View File

@ -9,7 +9,7 @@ import Ethereum 1.0
Rectangle { Rectangle {
id: root id: root
property var title: "Whisper" property var title: "Whisper Traffic"
property var iconSource: "../facet.png" property var iconSource: "../facet.png"
property var menuItem property var menuItem
@ -21,10 +21,22 @@ Rectangle {
identity = shh.newIdentity() identity = shh.newIdentity()
console.log("New identity:", identity) console.log("New identity:", identity)
var t = shh.watch({topics: ["chat"]}) var t = shh.watch({}, root)
}
function onMessage(message) {
whisperModel.insert(0, {data: JSON.stringify({from: message.from, payload: eth.toAscii(message.payload)})})
} }
RowLayout { RowLayout {
id: input
anchors {
left: parent.left
leftMargin: 20
top: parent.top
topMargin: 20
}
TextField { TextField {
id: to id: to
placeholderText: "To" placeholderText: "To"
@ -44,4 +56,20 @@ Rectangle {
} }
} }
} }
TableView {
id: txTableView
anchors {
top: input.bottom
topMargin: 10
bottom: parent.bottom
left: parent.left
right: parent.right
}
TableViewColumn{ role: "data" ; title: "Data" ; width: parent.width - 2 }
model: ListModel {
id: whisperModel
}
}
} }

View File

@ -381,6 +381,14 @@ func (self *UiLib) ToHex(data string) string {
return "0x" + ethutil.Bytes2Hex([]byte(data)) return "0x" + ethutil.Bytes2Hex([]byte(data))
} }
func (self *UiLib) ToAscii(data string) string {
start := 0
if len(data) > 1 && data[0:2] == "0x" {
start = 2
}
return string(ethutil.Hex2Bytes(data[start:]))
}
/* /*
// XXX Refactor me & MOVE // XXX Refactor me & MOVE
func (self *Ethereum) InstallFilter(filter *core.Filter) (id int) { func (self *Ethereum) InstallFilter(filter *core.Filter) (id int) {

23
ui/qt/qwhisper/message.go Normal file
View File

@ -0,0 +1,23 @@
package qwhisper
import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/whisper"
)
type Message struct {
ref *whisper.Message
Flags byte
Payload string
From string
}
func ToQMessage(msg *whisper.Message) *Message {
return &Message{
ref: msg,
Flags: msg.Flags,
Payload: ethutil.Bytes2Hex(msg.Payload),
From: ethutil.Bytes2Hex(crypto.FromECDSAPub(msg.Recover())),
}
}

13
ui/qt/qwhisper/watch.go Normal file
View File

@ -0,0 +1,13 @@
package qwhisper
import (
"fmt"
"unsafe"
)
type Watch struct {
}
func (self *Watch) Arrived(v unsafe.Pointer) {
fmt.Println(v)
}

View File

@ -3,7 +3,6 @@ package qwhisper
import ( import (
"fmt" "fmt"
"time" "time"
"unsafe"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
@ -19,13 +18,6 @@ func fromHex(s string) []byte {
} }
func toHex(b []byte) string { return "0x" + ethutil.Bytes2Hex(b) } func toHex(b []byte) string { return "0x" + ethutil.Bytes2Hex(b) }
type Watch struct {
}
func (self *Watch) Arrived(v unsafe.Pointer) {
fmt.Println(v)
}
type Whisper struct { type Whisper struct {
*whisper.Whisper *whisper.Whisper
view qml.Object view qml.Object
@ -70,15 +62,18 @@ func (self *Whisper) HasIdentity(key string) bool {
return self.Whisper.HasIdentity(crypto.ToECDSA(fromHex(key))) return self.Whisper.HasIdentity(crypto.ToECDSA(fromHex(key)))
} }
func (self *Whisper) Watch(opts map[string]interface{}) *Watch { func (self *Whisper) Watch(opts map[string]interface{}, view *qml.Common) int {
filter := filterFromMap(opts) filter := filterFromMap(opts)
filter.Fn = func(msg *whisper.Message) { filter.Fn = func(msg *whisper.Message) {
fmt.Println(msg) if view != nil {
view.Call("onMessage", ToQMessage(msg))
}
} }
i := self.Whisper.Watch(filter) i := self.Whisper.Watch(filter)
self.watches[i] = &Watch{} self.watches[i] = &Watch{}
return self.watches[i] return i
} }
func filterFromMap(opts map[string]interface{}) (f whisper.Filter) { func filterFromMap(opts map[string]interface{}) (f whisper.Filter) {