Browse Source

Merge pull request #2815 from arkpar/az-refactor

Whisper panels moved to plugins
cl-refactor
Gav Wood 10 years ago
parent
commit
2b32249a0f
  1. 14
      alethzero/CMakeLists.txt
  2. 233
      alethzero/Main.ui
  3. 1
      alethzero/MainFace.cpp
  4. 2
      alethzero/MainFace.h
  5. 160
      alethzero/MainWin.cpp
  6. 11
      alethzero/MainWin.h
  7. 2
      alethzero/OurWebThreeStubServer.cpp
  8. 0
      alethzero/plugins/blockchain/AllAccounts.cpp
  9. 0
      alethzero/plugins/blockchain/AllAccounts.h
  10. 0
      alethzero/plugins/blockchain/AllAccounts.ui
  11. 0
      alethzero/plugins/brainwallet/BrainWallet.cpp
  12. 0
      alethzero/plugins/brainwallet/BrainWallet.h
  13. 0
      alethzero/plugins/brainwallet/BrainWallet.ui
  14. 0
      alethzero/plugins/log/LogPanel.cpp
  15. 0
      alethzero/plugins/log/LogPanel.h
  16. 0
      alethzero/plugins/log/LogPanel.ui
  17. 0
      alethzero/plugins/namers/NameRegNamer.cpp
  18. 0
      alethzero/plugins/namers/NameRegNamer.h
  19. 0
      alethzero/plugins/namers/OtherAccounts.cpp
  20. 0
      alethzero/plugins/namers/OtherAccounts.h
  21. 0
      alethzero/plugins/namers/OtherAccounts.ui
  22. 0
      alethzero/plugins/namers/OurAccounts.cpp
  23. 0
      alethzero/plugins/namers/OurAccounts.h
  24. 178
      alethzero/plugins/whisper/Whisper.cpp
  25. 60
      alethzero/plugins/whisper/Whisper.h
  26. 176
      alethzero/plugins/whisper/Whisper.ui
  27. 78
      alethzero/plugins/whisper/WhisperPeers.cpp
  28. 55
      alethzero/plugins/whisper/WhisperPeers.h
  29. 32
      alethzero/plugins/whisper/WhisperPeers.ui

14
alethzero/CMakeLists.txt

@ -14,6 +14,15 @@ endif ()
set(CMAKE_INCLUDE_CURRENT_DIR ON)
aux_source_directory(. SRC_LIST)
file(GLOB_RECURSE SRC_LIST "./plugins/*.cpp")
file(GLOB_RECURSE HEADERS "./plugins/*.h")
aux_source_directory(. SRC_LIST)
file(GLOB_RECURSE PLUGIN_UI RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/plugins/*.ui")
foreach(ITEM ${PLUGIN_UI})
get_filename_component(UI_NAME ${ITEM} NAME_WE)
qt5_wrap_ui(ui_${UI_NAME}.h ./${ITEM})
endforeach()
include_directories(BEFORE ${JSONCPP_INCLUDE_DIRS})
include_directories(BEFORE ..)
@ -39,9 +48,6 @@ qt5_wrap_ui(ui_GasPricing.h GasPricing.ui)
# Extensions
qt5_wrap_ui(ui_AllAccounts.h AllAccounts.ui)
qt5_wrap_ui(ui_LogPanel.h LogPanel.ui)
qt5_wrap_ui(ui_BrainWallet.h BrainWallet.ui)
qt5_wrap_ui(ui_OtherAccounts.h OtherAccounts.ui)
file(GLOB HEADERS "*.h")
@ -54,7 +60,7 @@ endif ()
# eth_add_executable is defined in cmake/EthExecutableHelper.cmake
eth_add_executable(${EXECUTABLE}
ICON alethzero
UI_RESOURCES alethzero.icns Main.ui Connect.ui Debugger.ui Transact.ui ExportState.ui GetPassword.ui GasPricing.ui AllAccounts.ui LogPanel.ui BrainWallet.ui OtherAccounts.ui
UI_RESOURCES ${PLUGIN_UI} alethzero.icns Main.ui Connect.ui Debugger.ui Transact.ui ExportState.ui GetPassword.ui GasPricing.ui
WIN_RESOURCES alethzero.rc
)

233
alethzero/Main.ui

@ -210,12 +210,6 @@
<addaction name="separator"/>
<addaction name="preview"/>
</widget>
<widget class="QMenu" name="menuWhispe">
<property name="title">
<string>&amp;Whisper</string>
</property>
<addaction name="newIdentity"/>
</widget>
<widget class="QMenu" name="menuDebug">
<property name="title">
<string>&amp;Debug</string>
@ -236,7 +230,6 @@
<addaction name="menuView"/>
<addaction name="menuNetwork"/>
<addaction name="menuTools"/>
<addaction name="menuWhispe"/>
<addaction name="menuDebug"/>
<addaction name="menuConfig"/>
<addaction name="menuHelp"/>
@ -979,224 +972,6 @@ font-size: 14pt</string>
</layout>
</widget>
</widget>
<widget class="QDockWidget" name="dockWidget_14">
<property name="features">
<set>QDockWidget::DockWidgetFeatureMask</set>
</property>
<property name="windowTitle">
<string>Whisper</string>
</property>
<attribute name="dockWidgetArea">
<number>1</number>
</attribute>
<widget class="QWidget" name="dockWidgetContents_14">
<layout class="QGridLayout" name="gridLayout_4">
<item row="4" column="0" rowspan="2">
<widget class="QLabel" name="label_8">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Data</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="2" column="2" colspan="2">
<widget class="QLabel" name="label5_6">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Work to Prove</string>
</property>
</widget>
</item>
<item row="5" column="1" colspan="4">
<widget class="QPlainTextEdit" name="shhData">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="lineWidth">
<number>0</number>
</property>
</widget>
</item>
<item row="3" column="1" colspan="4">
<widget class="QPlainTextEdit" name="shhTopic">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="lineWidth">
<number>0</number>
</property>
</widget>
</item>
<item row="1" column="1" colspan="4">
<widget class="QComboBox" name="shhFrom">
<property name="editable">
<bool>false</bool>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_9">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Topic</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label5_3">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>To</string>
</property>
</widget>
</item>
<item row="0" column="1" colspan="4">
<widget class="QComboBox" name="shhTo">
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QSpinBox" name="shhTtl">
<property name="suffix">
<string> seconds</string>
</property>
<property name="minimum">
<number>5</number>
</property>
<property name="maximum">
<number>259200</number>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label5_5">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>TTL</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label5_4">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>From</string>
</property>
</widget>
</item>
<item row="2" column="4">
<widget class="QSpinBox" name="shhWork">
<property name="suffix">
<string> ms</string>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>1000</number>
</property>
<property name="value">
<number>50</number>
</property>
</widget>
</item>
<item row="6" column="4">
<widget class="QPushButton" name="post">
<property name="text">
<string>Post</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QDockWidget" name="dockWidget_15">
<property name="features">
<set>QDockWidget::DockWidgetFeatureMask</set>
</property>
<property name="windowTitle">
<string>Active Whispers</string>
</property>
<attribute name="dockWidgetArea">
<number>2</number>
</attribute>
<widget class="QWidget" name="dockWidgetContents_15">
<layout class="QHBoxLayout" name="horizontalLayout_11">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QListWidget" name="whispers">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<action name="quit">
<property name="text">
<string>&amp;Quit</string>
@ -1636,13 +1411,6 @@ font-size: 14pt</string>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>shhTo</tabstop>
<tabstop>shhFrom</tabstop>
<tabstop>shhTtl</tabstop>
<tabstop>shhTopic</tabstop>
<tabstop>shhWork</tabstop>
<tabstop>shhData</tabstop>
<tabstop>post</tabstop>
<tabstop>tabWidget</tabstop>
<tabstop>urlEdit</tabstop>
<tabstop>listenIP</tabstop>
@ -1652,7 +1420,6 @@ font-size: 14pt</string>
<tabstop>blockChainFilter</tabstop>
<tabstop>nameReg</tabstop>
<tabstop>nodes</tabstop>
<tabstop>whispers</tabstop>
<tabstop>jsInput</tabstop>
</tabstops>
<resources/>

1
alethzero/MainFace.cpp

@ -58,6 +58,7 @@ QDockWidget* Plugin::dock(Qt::DockWidgetArea _area, QString _title)
if (!m_dock)
{
m_dock = new QDockWidget(_title, m_main);
m_dock->setObjectName(_title);
m_main->addDockWidget(_area, m_dock);
m_dock->setFeatures(QDockWidget::AllDockWidgetFeatures | QDockWidget::DockWidgetVerticalTitleBar);
}

2
alethzero/MainFace.h

@ -49,6 +49,7 @@ namespace az
class Plugin;
class MainFace;
class Main;
class OurWebThreeStubServer;
using WatchHandler = std::function<void(dev::eth::LocalisedLogEntries const&)>;
@ -89,6 +90,7 @@ public:
// TODO: provide a set of available web3 modules.
virtual dev::WebThreeDirect* web3() const = 0;
virtual dev::eth::Client* ethereum() const = 0;
virtual OurWebThreeStubServer* web3Server() const = 0;
virtual std::shared_ptr<dev::shh::WhisperHost> whisper() const = 0;
virtual unsigned installWatch(dev::eth::LogFilter const& _tf, WatchHandler const& _f) = 0;

160
alethzero/MainWin.cpp

@ -197,8 +197,6 @@ Main::Main(QWidget* _parent):
auto w3ss = new OurWebThreeStubServer(*m_httpConnector, this);
m_server.reset(w3ss);
auto sessionKey = w3ss->newSession(SessionPermissions{{Privilege::Admin}});
connect(&*m_server, SIGNAL(onNewId(QString)), SLOT(addNewId(QString)));
m_server->setIdentities(keysAsVector(owned()));
m_server->StartListening();
WebPage* webPage = new WebPage(this);
@ -387,29 +385,6 @@ void Main::on_sentinel_triggered()
ethereum()->setSentinel(sentinel.toStdString());
}
void Main::on_newIdentity_triggered()
{
KeyPair kp = KeyPair::create();
m_myIdentities.append(kp);
m_server->setIdentities(keysAsVector(owned()));
refreshWhisper();
}
void Main::refreshWhisper()
{
ui->shhFrom->clear();
for (auto i: m_server->ids())
ui->shhFrom->addItem(QString::fromStdString(toHex(i.first.ref())));
}
void Main::addNewId(QString _ids)
{
KeyPair kp(jsToSecret(_ids.toStdString()));
m_myIdentities.push_back(kp);
m_server->setIdentities(keysAsVector(owned()));
refreshWhisper();
}
NetworkPreferences Main::netPrefs() const
{
auto listenIP = ui->listenIP->text().toStdString();
@ -666,17 +641,6 @@ void Main::addConsoleMessage(QString const& _js, QString const& _s)
ui->jsConsole->setHtml(r);
}
static Public stringToPublic(QString const& _a)
{
string sn = _a.toStdString();
if (_a.size() == sizeof(Public) * 2)
return Public(fromHex(_a.toStdString()));
else if (_a.size() == sizeof(Public) * 2 + 2 && _a.startsWith("0x"))
return Public(fromHex(_a.mid(2).toStdString()));
else
return Public();
}
std::string Main::pretty(dev::Address const& _a) const
{
auto g_newNameReg = getNameReg();
@ -807,17 +771,6 @@ void Main::writeSettings()
{
QSettings s("ethereum", "alethzero");
s.remove("address");
{
QByteArray b;
b.resize(sizeof(Secret) * m_myIdentities.size());
auto p = b.data();
for (auto i: m_myIdentities)
{
memcpy(p, &(i.secret()), sizeof(Secret));
p += sizeof(Secret);
}
s.setValue("identities", b);
}
forEach([&](std::shared_ptr<Plugin> p)
{
@ -905,21 +858,6 @@ void Main::readSettings(bool _skipGeometry)
restoreGeometry(s.value("geometry").toByteArray());
restoreState(s.value("windowState").toByteArray());
{
m_myIdentities.clear();
QByteArray b = s.value("identities").toByteArray();
if (!b.isEmpty())
{
Secret k;
for (unsigned i = 0; i < b.size() / sizeof(Secret); ++i)
{
memcpy(k.writable().data(), b.data() + i * sizeof(Secret), sizeof(Secret));
if (!count(m_myIdentities.begin(), m_myIdentities.end(), KeyPair(k)))
m_myIdentities.append(KeyPair(k));
}
}
}
forEach([&](std::shared_ptr<Plugin> p)
{
p->readSettings(s);
@ -1523,7 +1461,6 @@ void Main::timerEvent(QTimerEvent*)
{
interval = 0;
refreshNetwork();
refreshWhispers();
refreshCache();
refreshBlockCount();
poll();
@ -1953,60 +1890,6 @@ void Main::on_ourAccounts_doubleClicked()
m_logHistory.clear();
}*/
static shh::Topics topicFromText(QString _s)
{
shh::BuildTopic ret;
while (_s.size())
{
QRegExp r("(@|\\$)?\"([^\"]*)\"(\\s.*)?");
QRegExp d("(@|\\$)?([0-9]+)(\\s*(ether)|(finney)|(szabo))?(\\s.*)?");
QRegExp h("(@|\\$)?(0x)?(([a-fA-F0-9])+)(\\s.*)?");
bytes part;
if (r.exactMatch(_s))
{
for (auto i: r.cap(2))
part.push_back((byte)i.toLatin1());
if (r.cap(1) != "$")
for (int i = r.cap(2).size(); i < 32; ++i)
part.push_back(0);
else
part.push_back(0);
_s = r.cap(3);
}
else if (d.exactMatch(_s))
{
u256 v(d.cap(2).toStdString());
if (d.cap(6) == "szabo")
v *= szabo;
else if (d.cap(5) == "finney")
v *= finney;
else if (d.cap(4) == "ether")
v *= ether;
bytes bs = dev::toCompactBigEndian(v);
if (d.cap(1) != "$")
for (auto i = bs.size(); i < 32; ++i)
part.push_back(0);
for (auto b: bs)
part.push_back(b);
_s = d.cap(7);
}
else if (h.exactMatch(_s))
{
bytes bs = fromHex((((h.cap(3).size() & 1) ? "0" : "") + h.cap(3)).toStdString());
if (h.cap(1) != "$")
for (auto i = bs.size(); i < 32; ++i)
part.push_back(0);
for (auto b: bs)
part.push_back(b);
_s = h.cap(5);
}
else
_s = _s.mid(1);
ret.shift(part);
}
return ret;
}
void Main::on_clearPending_triggered()
{
writeSettings();
@ -2293,19 +2176,6 @@ std::string Main::prettyU256(dev::u256 const& _n) const
return s.str();
}
void Main::on_post_clicked()
{
return;
shh::Message m;
m.setTo(stringToPublic(ui->shhTo->currentText()));
m.setPayload(parseData(ui->shhData->toPlainText().toStdString()));
Public f = stringToPublic(ui->shhFrom->currentText());
Secret from;
if (m_server->ids().count(f))
from = m_server->ids().at(f);
whisper()->inject(m.seal(from, topicFromText(ui->shhTopic->toPlainText()), ui->shhTtl->value(), ui->shhWork->value()));
}
int Main::authenticate(QString _title, QString _text)
{
QMessageBox userInput(this);
@ -2318,36 +2188,6 @@ int Main::authenticate(QString _title, QString _text)
return userInput.exec();
}
void Main::refreshWhispers()
{
return;
ui->whispers->clear();
for (auto const& w: whisper()->all())
{
shh::Envelope const& e = w.second;
shh::Message m;
for (pair<Public, Secret> const& i: m_server->ids())
if (!!(m = e.open(shh::Topics(), i.second)))
break;
if (!m)
m = e.open(shh::Topics());
QString msg;
if (m.from())
// Good message.
msg = QString("{%1 -> %2} %3").arg(m.from() ? m.from().abridged().c_str() : "???").arg(m.to() ? m.to().abridged().c_str() : "*").arg(toHex(m.payload()).c_str());
else if (m)
// Maybe message.
msg = QString("{%1 -> %2} %3 (?)").arg(m.from() ? m.from().abridged().c_str() : "???").arg(m.to() ? m.to().abridged().c_str() : "*").arg(toHex(m.payload()).c_str());
time_t ex = e.expiry();
QString t(ctime(&ex));
t.chop(1);
QString item = QString("[%1 - %2s] *%3 %5 %4").arg(t).arg(e.ttl()).arg(e.workProved()).arg(toString(e.topic()).c_str()).arg(msg);
ui->whispers->addItem(item);
}
}
void Main::dappLoaded(Dapp& _dapp)
{
QUrl url = m_dappHost->hostDapp(std::move(_dapp));

11
alethzero/MainWin.h

@ -84,6 +84,7 @@ public:
~Main();
WebThreeDirect* web3() const override { return m_webThree.get(); }
OurWebThreeStubServer* web3Server() const override { return m_server.get(); }
eth::Client* ethereum() const override { return m_webThree->ethereum(); }
std::shared_ptr<shh::WhisperHost> whisper() const override { return m_webThree->whisper(); }
@ -96,8 +97,6 @@ public:
std::pair<Address, bytes> fromString(std::string const& _a) const override;
std::string renderDiff(eth::StateDiff const& _d) const override;
QList<KeyPair> owned() const { return m_myIdentities; }
u256 gasPrice() const override;
eth::KeyManager& keyManager() override { return m_keyManager; }
@ -199,17 +198,11 @@ private slots:
void on_debugDumpStatePre_triggered() { debugDumpState(0); }
void on_dumpBlockState_triggered();
// Whisper
void on_newIdentity_triggered();
void on_post_clicked();
// Config
void on_gasPrices_triggered();
void on_sentinel_triggered();
void refreshWhisper();
void refreshBlockChain();
void addNewId(QString _ids);
// Dapps
void dappLoaded(Dapp& _dapp); //qt does not support rvalue refs for signals
@ -259,7 +252,6 @@ private:
void refreshNetwork();
void refreshMining();
void refreshWhispers();
void refreshCache();
void refreshAll();
@ -281,7 +273,6 @@ private:
QByteArray m_networkConfig;
QStringList m_servers;
QList<KeyPair> m_myIdentities;
eth::KeyManager m_keyManager;
QString m_privateChain;
dev::Address m_nameReg;

2
alethzero/OurWebThreeStubServer.cpp

@ -34,7 +34,7 @@ OurWebThreeStubServer::OurWebThreeStubServer(
jsonrpc::AbstractServerConnector& _conn,
Main* _main
):
WebThreeStubServer(_conn, *_main->web3(), make_shared<OurAccountHolder>(_main), _main->owned().toVector().toStdVector(), _main->keyManager(), *static_cast<TrivialGasPricer*>(_main->ethereum()->gasPricer().get())),
WebThreeStubServer(_conn, *_main->web3(), make_shared<OurAccountHolder>(_main), vector<KeyPair>{}, _main->keyManager(), *static_cast<TrivialGasPricer*>(_main->ethereum()->gasPricer().get())),
m_main(_main)
{
}

0
alethzero/AllAccounts.cpp → alethzero/plugins/blockchain/AllAccounts.cpp

0
alethzero/AllAccounts.h → alethzero/plugins/blockchain/AllAccounts.h

0
alethzero/AllAccounts.ui → alethzero/plugins/blockchain/AllAccounts.ui

0
alethzero/BrainWallet.cpp → alethzero/plugins/brainwallet/BrainWallet.cpp

0
alethzero/BrainWallet.h → alethzero/plugins/brainwallet/BrainWallet.h

0
alethzero/BrainWallet.ui → alethzero/plugins/brainwallet/BrainWallet.ui

0
alethzero/LogPanel.cpp → alethzero/plugins/log/LogPanel.cpp

0
alethzero/LogPanel.h → alethzero/plugins/log/LogPanel.h

0
alethzero/LogPanel.ui → alethzero/plugins/log/LogPanel.ui

0
alethzero/NameRegNamer.cpp → alethzero/plugins/namers/NameRegNamer.cpp

0
alethzero/NameRegNamer.h → alethzero/plugins/namers/NameRegNamer.h

0
alethzero/OtherAccounts.cpp → alethzero/plugins/namers/OtherAccounts.cpp

0
alethzero/OtherAccounts.h → alethzero/plugins/namers/OtherAccounts.h

0
alethzero/OtherAccounts.ui → alethzero/plugins/namers/OtherAccounts.ui

0
alethzero/OurAccounts.cpp → alethzero/plugins/namers/OurAccounts.cpp

0
alethzero/OurAccounts.h → alethzero/plugins/namers/OurAccounts.h

178
alethzero/plugins/whisper/Whisper.cpp

@ -0,0 +1,178 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file Whisper.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2015
*/
#include "Whisper.h"
#include <QSettings>
#include <libethereum/Client.h>
#include <libethereum/Utility.h>
#include <libwhisper/WhisperHost.h>
#include <libweb3jsonrpc/WebThreeStubServerBase.h>
#include "OurWebThreeStubServer.h"
#include "ui_Whisper.h"
using namespace std;
using namespace dev;
using namespace az;
using namespace eth;
DEV_AZ_NOTE_PLUGIN(Whisper);
static Public stringToPublic(QString const& _a)
{
string sn = _a.toStdString();
if (_a.size() == sizeof(Public) * 2)
return Public(fromHex(_a.toStdString()));
else if (_a.size() == sizeof(Public) * 2 + 2 && _a.startsWith("0x"))
return Public(fromHex(_a.mid(2).toStdString()));
else
return Public();
}
static shh::Topics topicFromText(QString _s)
{
shh::BuildTopic ret;
while (_s.size())
{
QRegExp r("(@|\\$)?\"([^\"]*)\"(\\s.*)?");
QRegExp d("(@|\\$)?([0-9]+)(\\s*(ether)|(finney)|(szabo))?(\\s.*)?");
QRegExp h("(@|\\$)?(0x)?(([a-fA-F0-9])+)(\\s.*)?");
bytes part;
if (r.exactMatch(_s))
{
for (auto i: r.cap(2))
part.push_back((byte)i.toLatin1());
if (r.cap(1) != "$")
for (int i = r.cap(2).size(); i < 32; ++i)
part.push_back(0);
else
part.push_back(0);
_s = r.cap(3);
}
else if (d.exactMatch(_s))
{
u256 v(d.cap(2).toStdString());
if (d.cap(6) == "szabo")
v *= szabo;
else if (d.cap(5) == "finney")
v *= finney;
else if (d.cap(4) == "ether")
v *= ether;
bytes bs = dev::toCompactBigEndian(v);
if (d.cap(1) != "$")
for (auto i = bs.size(); i < 32; ++i)
part.push_back(0);
for (auto b: bs)
part.push_back(b);
_s = d.cap(7);
}
else if (h.exactMatch(_s))
{
bytes bs = fromHex((((h.cap(3).size() & 1) ? "0" : "") + h.cap(3)).toStdString());
if (h.cap(1) != "$")
for (auto i = bs.size(); i < 32; ++i)
part.push_back(0);
for (auto b: bs)
part.push_back(b);
_s = h.cap(5);
}
else
_s = _s.mid(1);
ret.shift(part);
}
return ret;
}
Whisper::Whisper(MainFace* _m):
Plugin(_m, "Whisper"),
m_ui(new Ui::Whisper)
{
dock(Qt::RightDockWidgetArea, "Whisper")->setWidget(new QWidget);
m_ui->setupUi(dock()->widget());
connect(addMenuItem("New Whisper identity.", "menuNetwork", true), &QAction::triggered, this, &Whisper::on_newIdentity_triggered);
connect(_m->web3Server(), &OurWebThreeStubServer::onNewId, this, &Whisper::addNewId);
}
void Whisper::readSettings(QSettings const& _s)
{
m_myIdentities.clear();
QByteArray b = _s.value("identities").toByteArray();
if (!b.isEmpty())
{
Secret k;
for (unsigned i = 0; i < b.size() / sizeof(Secret); ++i)
{
memcpy(k.writable().data(), b.data() + i * sizeof(Secret), sizeof(Secret));
if (!count(m_myIdentities.begin(), m_myIdentities.end(), KeyPair(k)))
m_myIdentities.append(KeyPair(k));
}
}
main()->web3Server()->setIdentities(keysAsVector(m_myIdentities));
refreshWhisper();
}
void Whisper::writeSettings(QSettings& _s)
{
QByteArray b;
b.resize(sizeof(Secret) * m_myIdentities.size());
auto p = b.data();
for (auto i: m_myIdentities)
{
memcpy(p, &(i.secret()), sizeof(Secret));
p += sizeof(Secret);
}
_s.setValue("identities", b);
}
void Whisper::addNewId(QString _ids)
{
KeyPair kp(jsToSecret(_ids.toStdString()));
m_myIdentities.push_back(kp);
main()->web3Server()->setIdentities(keysAsVector(m_myIdentities));
refreshWhisper();
}
void Whisper::refreshWhisper()
{
m_ui->shhFrom->clear();
for (auto i: main()->web3Server()->ids())
m_ui->shhFrom->addItem(QString::fromStdString(toHex(i.first.ref())));
}
void Whisper::on_newIdentity_triggered()
{
KeyPair kp = KeyPair::create();
m_myIdentities.append(kp);
main()->web3Server()->setIdentities(keysAsVector(m_myIdentities));
refreshWhisper();
}
void Whisper::on_post_clicked()
{
return;
shh::Message m;
m.setTo(stringToPublic(m_ui->shhTo->currentText()));
m.setPayload(parseData(m_ui->shhData->toPlainText().toStdString()));
Public f = stringToPublic(m_ui->shhFrom->currentText());
Secret from;
if (main()->web3Server()->ids().count(f))
from = main()->web3Server()->ids().at(f);
whisper()->inject(m.seal(from, topicFromText(m_ui->shhTopic->toPlainText()), m_ui->shhTtl->value(), m_ui->shhWork->value()));
}

60
alethzero/plugins/whisper/Whisper.h

@ -0,0 +1,60 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file Whisper.h
* @author Gav Wood <i@gavwood.com>
* @date 2015
*/
#pragma once
#include <QMutex>
#include <QString>
#include <QPair>
#include <QList>
#include "MainFace.h"
namespace Ui
{
class Whisper;
}
namespace dev
{
namespace az
{
class Whisper: public QObject, public Plugin
{
Q_OBJECT
public:
Whisper(MainFace* _m);
private:
void readSettings(QSettings const&) override;
void writeSettings(QSettings&) override;
void refreshWhisper();
void addNewId(QString _ids);
void on_newIdentity_triggered();
void on_post_clicked();
Ui::Whisper* m_ui;
QList<KeyPair> m_myIdentities;
};
}
}

176
alethzero/plugins/whisper/Whisper.ui

@ -0,0 +1,176 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Whisper</class>
<widget class="QWidget" name="WhisperWidget">
<layout class="QGridLayout" name="gridLayout_4">
<item row="4" column="0" rowspan="2">
<widget class="QLabel" name="label_8">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Data</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="2" column="2" colspan="2">
<widget class="QLabel" name="label5_6">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Work to Prove</string>
</property>
</widget>
</item>
<item row="5" column="1" colspan="4">
<widget class="QPlainTextEdit" name="shhData">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="lineWidth">
<number>0</number>
</property>
</widget>
</item>
<item row="3" column="1" colspan="4">
<widget class="QPlainTextEdit" name="shhTopic">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="lineWidth">
<number>0</number>
</property>
</widget>
</item>
<item row="1" column="1" colspan="4">
<widget class="QComboBox" name="shhFrom">
<property name="editable">
<bool>false</bool>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_9">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Topic</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label5_3">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>To</string>
</property>
</widget>
</item>
<item row="0" column="1" colspan="4">
<widget class="QComboBox" name="shhTo">
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QSpinBox" name="shhTtl">
<property name="suffix">
<string> seconds</string>
</property>
<property name="minimum">
<number>5</number>
</property>
<property name="maximum">
<number>259200</number>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label5_5">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>TTL</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label5_4">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>From</string>
</property>
</widget>
</item>
<item row="2" column="4">
<widget class="QSpinBox" name="shhWork">
<property name="suffix">
<string> ms</string>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>1000</number>
</property>
<property name="value">
<number>50</number>
</property>
</widget>
</item>
<item row="6" column="4">
<widget class="QPushButton" name="post">
<property name="text">
<string>Post</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

78
alethzero/plugins/whisper/WhisperPeers.cpp

@ -0,0 +1,78 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file WhisperPeers.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2015
*/
#include "WhisperPeers.h"
#include <QSettings>
#include <libethereum/Client.h>
#include <libwhisper/WhisperHost.h>
#include <libweb3jsonrpc/WebThreeStubServerBase.h>
#include "OurWebThreeStubServer.h"
#include "ui_WhisperPeers.h"
using namespace std;
using namespace dev;
using namespace az;
using namespace eth;
DEV_AZ_NOTE_PLUGIN(WhisperPeers);
WhisperPeers::WhisperPeers(MainFace* _m):
Plugin(_m, "WhisperPeers"),
m_ui(new Ui::WhisperPeers)
{
dock(Qt::RightDockWidgetArea, "Active Whispers")->setWidget(new QWidget);
m_ui->setupUi(dock()->widget());
startTimer(1000);
}
void WhisperPeers::timerEvent(QTimerEvent*)
{
refreshWhispers();
}
void WhisperPeers::refreshWhispers()
{
return;
m_ui->whispers->clear();
for (auto const& w: whisper()->all())
{
shh::Envelope const& e = w.second;
shh::Message m;
for (pair<Public, Secret> const& i: main()->web3Server()->ids())
if (!!(m = e.open(shh::Topics(), i.second)))
break;
if (!m)
m = e.open(shh::Topics());
QString msg;
if (m.from())
// Good message.
msg = QString("{%1 -> %2} %3").arg(m.from() ? m.from().abridged().c_str() : "???").arg(m.to() ? m.to().abridged().c_str() : "*").arg(toHex(m.payload()).c_str());
else if (m)
// Maybe message.
msg = QString("{%1 -> %2} %3 (?)").arg(m.from() ? m.from().abridged().c_str() : "???").arg(m.to() ? m.to().abridged().c_str() : "*").arg(toHex(m.payload()).c_str());
time_t ex = e.expiry();
QString t(ctime(&ex));
t.chop(1);
QString item = QString("[%1 - %2s] *%3 %5 %4").arg(t).arg(e.ttl()).arg(e.workProved()).arg(toString(e.topic()).c_str()).arg(msg);
m_ui->whispers->addItem(item);
}
}

55
alethzero/plugins/whisper/WhisperPeers.h

@ -0,0 +1,55 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file WhisperPeers.h
* @author Gav Wood <i@gavwood.com>
* @date 2015
*/
#pragma once
#include <QMutex>
#include <QString>
#include <QPair>
#include <QList>
#include "MainFace.h"
namespace Ui
{
class WhisperPeers;
}
namespace dev
{
namespace az
{
class WhisperPeers: public QObject, public Plugin
{
Q_OBJECT
public:
WhisperPeers(MainFace* _m);
private:
void timerEvent(QTimerEvent*) override;
void refreshWhispers();
Ui::WhisperPeers* m_ui;
};
}
}

32
alethzero/plugins/whisper/WhisperPeers.ui

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>WhisperPeers</class>
<widget class="QWidget" name="whisperPeersWidget">
<layout class="QHBoxLayout" name="whisperPeersLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QListWidget" name="whispers">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
Loading…
Cancel
Save