From 01a6db93241a01e98a0467b628423c9b5b1361cb Mon Sep 17 00:00:00 2001
From: obscuren <geffobscura@gmail.com>
Date: Mon, 15 Dec 2014 17:14:02 +0100
Subject: [PATCH] Added whisper debug interface + whisper fixes

---
 cmd/mist/assets/qml/main.qml |  1 +
 cmd/mist/gui.go              |  8 +++++++-
 cmd/mist/ui_lib.go           |  4 ++++
 ui/qt/qwhisper/whisper.go    | 20 +++++++++++++++-----
 whisper/main.go              | 14 ++------------
 whisper/whisper.go           | 13 +++----------
 6 files changed, 32 insertions(+), 28 deletions(-)

diff --git a/cmd/mist/assets/qml/main.qml b/cmd/mist/assets/qml/main.qml
index 9f1f214a6..285757080 100644
--- a/cmd/mist/assets/qml/main.qml
+++ b/cmd/mist/assets/qml/main.qml
@@ -50,6 +50,7 @@ ApplicationWindow {
 		addPlugin("./views/miner.qml", {noAdd: true, close: false, section: "ethereum", active: true});
 
 		addPlugin("./views/transaction.qml", {noAdd: true, close: false, section: "legacy"});
+		addPlugin("./views/whisper.qml", {noAdd: true, close: false, section: "legacy"});
 		addPlugin("./views/chain.qml", {noAdd: true, close: false, section: "legacy"});
 		addPlugin("./views/pending_tx.qml", {noAdd: true, close: false, section: "legacy"});
 		addPlugin("./views/info.qml", {noAdd: true, close: false, section: "legacy"});
diff --git a/cmd/mist/gui.go b/cmd/mist/gui.go
index 40499ad7f..ba031e6c3 100644
--- a/cmd/mist/gui.go
+++ b/cmd/mist/gui.go
@@ -38,6 +38,7 @@ import (
 	"github.com/ethereum/go-ethereum/logger"
 	"github.com/ethereum/go-ethereum/miner"
 	"github.com/ethereum/go-ethereum/p2p"
+	"github.com/ethereum/go-ethereum/ui/qt/qwhisper"
 	"github.com/ethereum/go-ethereum/xeth"
 	"gopkg.in/qml.v1"
 )
@@ -87,7 +88,8 @@ type Gui struct {
 	eth *eth.Ethereum
 
 	// The public Ethereum library
-	uiLib *UiLib
+	uiLib   *UiLib
+	whisper *qwhisper.Whisper
 
 	txDb *ethdb.LDBDatabase
 
@@ -138,10 +140,12 @@ func (gui *Gui) Start(assetPath string) {
 	gui.engine = qml.NewEngine()
 	context := gui.engine.Context()
 	gui.uiLib = NewUiLib(gui.engine, gui.eth, assetPath)
+	gui.whisper = qwhisper.New(gui.eth.Whisper())
 
 	// Expose the eth library and the ui library to QML
 	context.SetVar("gui", gui)
 	context.SetVar("eth", gui.uiLib)
+	context.SetVar("shh", gui.whisper)
 
 	// Load the main QML interface
 	data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
@@ -391,6 +395,8 @@ func (gui *Gui) update() {
 		gui.setPeerInfo()
 	}()
 
+	gui.whisper.SetView(gui.win.Root().ObjectByName("whisperView"))
+
 	for _, plugin := range gui.plugins {
 		guilogger.Infoln("Loading plugin ", plugin.Name)
 
diff --git a/cmd/mist/ui_lib.go b/cmd/mist/ui_lib.go
index d34b527ce..68f333563 100644
--- a/cmd/mist/ui_lib.go
+++ b/cmd/mist/ui_lib.go
@@ -377,6 +377,10 @@ func (self *UiLib) ToggleMining() bool {
 	}
 }
 
+func (self *UiLib) ToHex(data string) string {
+	return "0x" + ethutil.Bytes2Hex([]byte(data))
+}
+
 /*
 // XXX Refactor me & MOVE
 func (self *Ethereum) InstallFilter(filter *core.Filter) (id int) {
diff --git a/ui/qt/qwhisper/whisper.go b/ui/qt/qwhisper/whisper.go
index bed23c8a7..3e1ca7ab9 100644
--- a/ui/qt/qwhisper/whisper.go
+++ b/ui/qt/qwhisper/whisper.go
@@ -1,11 +1,13 @@
 package qwhisper
 
 import (
+	"fmt"
 	"time"
 
 	"github.com/ethereum/go-ethereum/crypto"
 	"github.com/ethereum/go-ethereum/ethutil"
 	"github.com/ethereum/go-ethereum/whisper"
+	"gopkg.in/qml.v1"
 )
 
 func fromHex(s string) []byte {
@@ -18,25 +20,33 @@ func toHex(b []byte) string { return "0x" + ethutil.Bytes2Hex(b) }
 
 type Whisper struct {
 	*whisper.Whisper
+	view qml.Object
 }
 
 func New(w *whisper.Whisper) *Whisper {
-	return &Whisper{w}
+	return &Whisper{w, nil}
 }
 
-func (self *Whisper) Post(data string, pow, ttl uint32, to, from string) {
+func (self *Whisper) SetView(view qml.Object) {
+	self.view = view
+}
+
+func (self *Whisper) Post(data string, to, from string, topics []string, pow, ttl uint32) {
 	msg := whisper.NewMessage(fromHex(data))
 	envelope, err := msg.Seal(time.Duration(pow), whisper.Opts{
-		Ttl:  time.Duration(ttl),
-		To:   crypto.ToECDSAPub(fromHex(to)),
-		From: crypto.ToECDSA(fromHex(from)),
+		Ttl:    time.Duration(ttl),
+		To:     crypto.ToECDSAPub(fromHex(to)),
+		From:   crypto.ToECDSA(fromHex(from)),
+		Topics: whisper.TopicsFromString(topics),
 	})
 	if err != nil {
+		fmt.Println(err)
 		// handle error
 		return
 	}
 
 	if err := self.Whisper.Send(envelope); err != nil {
+		fmt.Println(err)
 		// handle error
 		return
 	}
diff --git a/whisper/main.go b/whisper/main.go
index 2ee2f3ff1..edd5f7004 100644
--- a/whisper/main.go
+++ b/whisper/main.go
@@ -5,10 +5,8 @@ package main
 import (
 	"fmt"
 	"log"
-	"net"
 	"os"
 
-	"github.com/ethereum/go-ethereum/event"
 	"github.com/ethereum/go-ethereum/logger"
 	"github.com/ethereum/go-ethereum/p2p"
 	"github.com/ethereum/go-ethereum/whisper"
@@ -20,12 +18,12 @@ func main() {
 
 	pub, _ := secp256k1.GenerateKeyPair()
 
-	whisper := whisper.New(&event.TypeMux{})
+	whisper := whisper.New()
 
 	srv := p2p.Server{
 		MaxPeers:   10,
 		Identity:   p2p.NewSimpleClientIdentity("whisper-go", "1.0", "", string(pub)),
-		ListenAddr: ":30303",
+		ListenAddr: ":30300",
 		NAT:        p2p.UPNP(),
 
 		Protocols: []p2p.Protocol{whisper.Protocol()},
@@ -35,13 +33,5 @@ func main() {
 		os.Exit(1)
 	}
 
-	// add seed peers
-	seed, err := net.ResolveTCPAddr("tcp", "poc-7.ethdev.com:30300")
-	if err != nil {
-		fmt.Println("couldn't resolve:", err)
-		os.Exit(1)
-	}
-	srv.SuggestPeer(seed.IP, seed.Port, nil)
-
 	select {}
 }
diff --git a/whisper/whisper.go b/whisper/whisper.go
index 1b3f54b67..f91b61d9a 100644
--- a/whisper/whisper.go
+++ b/whisper/whisper.go
@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"crypto/ecdsa"
 	"errors"
+	"fmt"
 	"sync"
 	"time"
 
@@ -71,16 +72,6 @@ func New() *Whisper {
 	}
 	whisper.filters.Start()
 
-	// XXX TODO REMOVE TESTING CODE
-	//msg := NewMessage([]byte(fmt.Sprintf("Hello world. This is whisper-go. Incase you're wondering; the time is %v", time.Now())))
-	//envelope, _ := msg.Seal(DefaultPow, Opts{
-	//	Ttl: DefaultTtl,
-	//})
-	//if err := whisper.Send(envelope); err != nil {
-	//	fmt.Println(err)
-	//}
-	// XXX TODO REMOVE TESTING CODE
-
 	// p2p whisper sub protocol handler
 	whisper.protocol = p2p.Protocol{
 		Name:    "shh",
@@ -158,6 +149,7 @@ func (self *Whisper) msgHandler(peer *p2p.Peer, ws p2p.MsgReadWriter) error {
 			continue
 		}
 
+		fmt.Println("recv")
 		if err := self.add(envelope); err != nil {
 			// TODO Punish peer here. Invalid envelope.
 			peer.Infoln(err)
@@ -184,6 +176,7 @@ func (self *Whisper) add(envelope *Envelope) error {
 	if !self.expiry[envelope.Expiry].Has(hash) {
 		self.expiry[envelope.Expiry].Add(hash)
 		self.postEvent(envelope)
+		fmt.Println("envelope added", envelope)
 	}
 
 	return nil
-- 
GitLab