diff --git a/CMakeLists.txt b/CMakeLists.txt index 00338bb7b..6e9d159cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -153,8 +153,10 @@ add_subdirectory(libethereum) add_subdirectory(test) add_subdirectory(eth) if (NOT HEADLESS) - add_subdirectory(alethzero) - add_subdirectory(walleth) + add_subdirectory(eth-ncurses-cli) + add_subdirectory(libqethereum) + add_subdirectory(alethzero) + add_subdirectory(walleth) endif () enable_testing() diff --git a/alethzero/CMakeLists.txt b/alethzero/CMakeLists.txt index 2e3c37b41..35bfa1f14 100644 --- a/alethzero/CMakeLists.txt +++ b/alethzero/CMakeLists.txt @@ -10,6 +10,7 @@ aux_source_directory(. SRC_LIST) include_directories(..) link_directories(../libethereum) +link_directories(../libqethereum) # Find Qt5 for Apple and update src_list for windows if (APPLE) @@ -54,7 +55,7 @@ else () endif () qt5_use_modules(${EXECUTEABLE} Core Gui Widgets Network WebKit WebKitWidgets) -target_link_libraries(${EXECUTEABLE} ethereum secp256k1 ${CRYPTOPP_LIBRARIES}) +target_link_libraries(${EXECUTEABLE} qethereum ethereum secp256k1 ${CRYPTOPP_LIBRARIES}) if (APPLE) set_target_properties(${EXECUTEABLE} PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/EthereumMacOSXBundleInfo.plist.in") @@ -78,7 +79,7 @@ if (APPLE) install(CODE " include(BundleUtilities) set(BU_CHMOD_BUNDLE_ITEMS 1) - fixup_bundle(\"${APPS}\" \"${BUNDLELIBS}\" \"../libethereum ../secp256k1\") + fixup_bundle(\"${APPS}\" \"${BUNDLELIBS}\" \"../libqethereum ../libethereum ../secp256k1\") " COMPONENT RUNTIME ) add_custom_target(addframeworks ALL diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index 5b19ad670..04f745ff5 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -63,183 +63,6 @@ using eth::c_instructionInfo; // Can get rid of this once we've sorted out ITC for signalling & multiplexed querying. Main* g_main = nullptr; -QEthereum::QEthereum(QObject* _p): QObject(_p) -{ - connect(g_main, SIGNAL(changed()), SIGNAL(changed())); -} - -QEthereum::~QEthereum() -{ -} - -Client* QEthereum::client() const -{ - return g_main->client(); -} - -QVariant QEthereum::coinbase() const -{ - return toQJS(client()->address()); -} - -QVariant QEthereum::account() const -{ - if (g_main->owned().empty()) - return toQJS(Address()); - return toQJS(g_main->owned()[0].address()); -} - -QList QEthereum::accounts() const -{ - QList ret; - for (auto i: g_main->owned()) - ret.push_back(toQJS(i.address())); - return ret; -} - -QVariant QEthereum::key() const -{ - if (g_main->owned().empty()) - return toQJS(KeyPair()); - return toQJS(g_main->owned()[0]); -} - -QList QEthereum::keys() const -{ - QList ret; - for (auto i: g_main->owned()) - ret.push_back(toQJS(i)); - return ret; -} - -void QEthereum::setCoinbase(QVariant _a) -{ - if (client()->address() != to
(_a)) - { - client()->setAddress(to
(_a)); - changed(); - } -} - -QAccount::QAccount(QObject*) -{ -} - -QAccount::~QAccount() -{ -} - -void QAccount::setEthereum(QEthereum* _eth) -{ - if (m_eth == _eth) - return; - if (m_eth) - disconnect(m_eth, SIGNAL(changed()), this, SIGNAL(changed())); - m_eth = _eth; - if (m_eth) - connect(m_eth, SIGNAL(changed()), this, SIGNAL(changed())); - ethChanged(); - changed(); -} - -QVariant QAccount::balance() const -{ - if (m_eth) - return toQJS(m_eth->balanceAt(m_address)); - return toQJS(0); -} - -double QAccount::txCount() const -{ - if (m_eth) - return m_eth->txCountAt(m_address); - return 0; -} - -bool QAccount::isContract() const -{ - if (m_eth) - return m_eth->isContractAt(m_address); - return 0; -} - -QVariant QEthereum::balanceAt(QVariant _a) const -{ - return toQJS(client()->postState().balance(to
(_a))); -} - -QVariant QEthereum::storageAt(QVariant _a, QVariant _p) const -{ - return toQJS(client()->postState().contractStorage(to
(_a), to(_p))); -} - -u256 QEthereum::balanceAt(Address _a) const -{ - return client()->postState().balance(_a); -} - -bool QEthereum::isContractAt(QVariant _a) const -{ - return client()->postState().isContractAddress(to
(_a)); -} - -bool QEthereum::isContractAt(Address _a) const -{ - return client()->postState().isContractAddress(_a); -} - -bool QEthereum::isMining() const -{ - return client()->isMining(); -} - -bool QEthereum::isListening() const -{ - return client()->haveNetwork(); -} - -void QEthereum::setMining(bool _l) -{ - if (_l) - client()->startMining(); - else - client()->stopMining(); -} - -void QEthereum::setListening(bool _l) -{ - if (_l) - client()->startNetwork(); - else - client()->stopNetwork(); -} - -double QEthereum::txCountAt(QVariant _a) const -{ - return (double)client()->postState().transactionsFrom(to
(_a)); -} - -double QEthereum::txCountAt(Address _a) const -{ - return (double)client()->postState().transactionsFrom(_a); -} - -unsigned QEthereum::peerCount() const -{ - return (unsigned)client()->peerCount(); -} - -QVariant QEthereum::create(QVariant _secret, QVariant _amount, QByteArray _code, QByteArray _init, QVariant _gas, QVariant _gasPrice) -{ - return toQJS(client()->transact(to(_secret), to(_amount), bytes(_code.data(), _code.data() + _code.size()), bytes(_init.data(), _init.data() + _init.size()), to(_gas), to(_gasPrice))); -} - -void QEthereum::transact(QVariant _secret, QVariant _amount, QVariant _dest, QByteArray _data, QVariant _gas, QVariant _gasPrice) -{ - client()->transact(to(_secret), to(_amount), to
(_dest), bytes(_data.data(), _data.data() + _data.size()), to(_gas), to(_gasPrice)); -} - - static void initUnits(QComboBox* _b) { for (auto n = (::uint)units().size(); n-- != 0; ) @@ -383,7 +206,7 @@ Main::Main(QWidget *parent) : QWebFrame* f = ui->webView->page()->currentFrame(); connect(f, &QWebFrame::javaScriptWindowObjectCleared, [=](){ - f->addToJavaScriptWindowObject("eth", new QEthereum, QWebFrame::ScriptOwnership); + f->addToJavaScriptWindowObject("eth", new QEthereum(this, m_client.get(), owned()), QWebFrame::ScriptOwnership); f->addToJavaScriptWindowObject("u256", new U256Helper, QWebFrame::ScriptOwnership); f->addToJavaScriptWindowObject("key", new KeyHelper, QWebFrame::ScriptOwnership); f->addToJavaScriptWindowObject("bytes", new BytesHelper, QWebFrame::ScriptOwnership); @@ -677,7 +500,7 @@ void Main::refresh(bool _override) void Main::ourAccountsRowsMoved() { - QVector myKeys; + QList myKeys; for (int i = 0; i < ui->ourAccounts->count(); ++i) { auto hba = ui->ourAccounts->item(i)->data(Qt::UserRole).toByteArray(); diff --git a/alethzero/MainWin.h b/alethzero/MainWin.h index 1c336009c..162ae53a8 100644 --- a/alethzero/MainWin.h +++ b/alethzero/MainWin.h @@ -9,6 +9,7 @@ #include #include #include +#include namespace Ui { class Main; @@ -20,234 +21,6 @@ class State; } class QQuickView; -class QQmlEngine; -class QJSEngine; - -class QEthereum; -class QAccount; - -Q_DECLARE_METATYPE(eth::u256) -Q_DECLARE_METATYPE(eth::Address) -Q_DECLARE_METATYPE(eth::Secret) -Q_DECLARE_METATYPE(eth::KeyPair) -Q_DECLARE_METATYPE(QEthereum*) -Q_DECLARE_METATYPE(QAccount*) - -template T to(QVariant const& _s) { if (_s.type() != QVariant::String) return T(); auto s = _s.toString().toLatin1(); assert(s.size() == sizeof(T)); return *(T*)s.data(); } -template QVariant toQJS(T const& _s) { QLatin1String ret((char*)&_s, sizeof(T)); assert(QVariant(QString(ret)).toString().toLatin1().size() == sizeof(T)); assert(*(T*)(QVariant(QString(ret)).toString().toLatin1().data()) == _s); return QVariant(QString(ret)); } - -class U256Helper: public QObject -{ - Q_OBJECT - -public: - U256Helper(QObject* _p = nullptr): QObject(_p) {} - - static eth::u256 in(QVariant const& _s) { return to(_s); } - static QVariant out(eth::u256 const& _s) { return toQJS(_s); } - - Q_INVOKABLE QVariant add(QVariant _a, QVariant _b) const { return out(in(_a) + in(_b)); } - Q_INVOKABLE QVariant sub(QVariant _a, QVariant _b) const { return out(in(_a) - in(_b)); } - Q_INVOKABLE QVariant mul(QVariant _a, int _b) const { return out(in(_a) * in(_b)); } - Q_INVOKABLE QVariant mul(int _a, QVariant _b) const { return out(in(_a) * in(_b)); } - Q_INVOKABLE QVariant div(QVariant _a, int _b) const { return out(in(_a) / in(_b)); } - - Q_INVOKABLE QVariant wei(double _s) const { return out(eth::u256(_s)); } - Q_INVOKABLE QVariant szabo(double _s) const { return out(eth::u256(_s * (double)eth::szabo)); } - Q_INVOKABLE QVariant finney(double _s) const { return out(eth::u256(_s * (double)eth::finney)); } - Q_INVOKABLE QVariant ether(double _s) const { return out(eth::u256(_s * (double)eth::ether)); } - Q_INVOKABLE QVariant wei(unsigned _s) const { return value(_s); } - Q_INVOKABLE QVariant szabo(unsigned _s) const { return out(eth::u256(_s) * eth::szabo); } - Q_INVOKABLE QVariant finney(unsigned _s) const { return out(eth::u256(_s) * eth::finney); } - Q_INVOKABLE QVariant ether(unsigned _s) const { return out(eth::u256(_s) * eth::ether); } - Q_INVOKABLE double toWei(QVariant _t) const { return toValue(_t); } - Q_INVOKABLE double toSzabo(QVariant _t) const { return toWei(_t) / (double)eth::szabo; } - Q_INVOKABLE double toFinney(QVariant _t) const { return toWei(_t) / (double)eth::finney; } - Q_INVOKABLE double toEther(QVariant _t) const { return toWei(_t) / (double)eth::ether; } - - Q_INVOKABLE QVariant value(unsigned _s) const { return out(eth::u256(_s)); } - Q_INVOKABLE double toValue(QVariant _t) const { return (double)in(_t); } - - Q_INVOKABLE QString ethOf(QVariant _t) const { return QString::fromStdString(eth::formatBalance(in(_t))); } - Q_INVOKABLE QString stringOf(QVariant _t) const { return QString::fromStdString(eth::toString(in(_t))); } - - Q_INVOKABLE QByteArray bytesOf(QVariant _t) const { eth::h256 b = in(_t); return QByteArray((char const*)&b, sizeof(eth::h256)); } - - Q_INVOKABLE QVariant fromAddress(QVariant/*eth::Address*/ _a) const { return out((eth::u160)to(_a)); } - Q_INVOKABLE QVariant toAddress(QVariant/*eth::Address*/ _a) const { return toQJS((eth::u160)in(_a)); } - - Q_INVOKABLE bool isNull(QVariant/*eth::Address*/ _a) const { return !in(_a); } -}; - -class KeyHelper: public QObject -{ - Q_OBJECT - -public: - KeyHelper(QObject* _p = nullptr): QObject(_p) {} - - static eth::Address in(QVariant const& _s) { return to(_s); } - static QVariant out(eth::Address const& _s) { return toQJS(_s); } - - Q_INVOKABLE QVariant/*eth::KeyPair*/ create() const { return toQJS(eth::KeyPair::create()); } - Q_INVOKABLE QVariant/*eth::Address*/ address(QVariant/*eth::KeyPair*/ _p) const { return out(to(_p).address()); } - Q_INVOKABLE QVariant/*eth::Secret*/ secret(QVariant/*eth::KeyPair*/ _p) const { return toQJS(to(_p).secret()); } - Q_INVOKABLE QVariant/*eth::KeyPair*/ keypair(QVariant/*eth::Secret*/ _k) const { return toQJS(eth::KeyPair(to(_k))); } - - Q_INVOKABLE bool isNull(QVariant/*eth::Address*/ _a) const { return !in(_a); } - - Q_INVOKABLE QVariant/*eth::Address*/ addressOf(QString _s) const { return out(eth::Address(_s.toStdString())); } - Q_INVOKABLE QString stringOf(QVariant/*eth::Address*/ _a) const { return QString::fromStdString(eth::toHex(in(_a).asArray())); } - Q_INVOKABLE QString toAbridged(QVariant/*eth::Address*/ _a) const { return QString::fromStdString(in(_a).abridged()); } - -}; - -class BytesHelper: public QObject -{ - Q_OBJECT - -public: - BytesHelper(QObject* _p = nullptr): QObject(_p) {} - - Q_INVOKABLE QByteArray concat(QVariant _v, QVariant _w) const - { - QByteArray ba; - if (_v.type() == QVariant::ByteArray) - ba = _v.toByteArray(); - else - ba = _v.toString().toLatin1(); - QByteArray ba2; - if (_w.type() == QVariant::ByteArray) - ba2 = _w.toByteArray(); - else - ba2 = _w.toString().toLatin1(); - ba.append(ba2); - return QByteArray(ba); - } - Q_INVOKABLE QByteArray concat(QByteArray _v, QByteArray _w) const - { - _v.append(_w); - return _v; - } - Q_INVOKABLE QByteArray fromString(QString _s) const - { - return _s.toLatin1(); - } - Q_INVOKABLE QByteArray fromString(QString _s, unsigned _padding) const - { - QByteArray b = _s.toLatin1(); - for (unsigned i = b.size(); i < _padding; ++i) - b.append((char)0); - b.resize(_padding); - return b; - } - Q_INVOKABLE QString toString(QByteArray _b) const - { - while (_b.size() && !_b[_b.size() - 1]) - _b.resize(_b.size() - 1); - return QString::fromLatin1(_b); - } - Q_INVOKABLE QVariant u256of(QByteArray _s) const - { - while (_s.size() < 32) - _s.append((char)0); - eth::h256 ret((uint8_t const*)_s.data(), eth::h256::ConstructFromPointer); - return toQJS(ret); - } -}; - -class QAccount: public QObject -{ - Q_OBJECT - -public: - QAccount(QObject* _p = nullptr); - virtual ~QAccount(); - - Q_INVOKABLE QEthereum* ethereum() const { return m_eth; } - Q_INVOKABLE QVariant balance() const; - Q_INVOKABLE double txCount() const; - Q_INVOKABLE bool isContract() const; - - Q_INVOKABLE QVariant address() { return toQJS(m_address); } - - // TODO: past transactions models. - -public slots: - void setEthereum(QEthereum* _eth); - void setAddress(QVariant _a) { m_address = to(_a); changed(); } - -signals: - void changed(); - void ethChanged(); - -private: - QEthereum* m_eth = nullptr; - eth::Address m_address; - - Q_PROPERTY(QVariant balance READ balance NOTIFY changed STORED false) - Q_PROPERTY(double txCount READ txCount NOTIFY changed STORED false) - Q_PROPERTY(bool isContract READ isContract NOTIFY changed STORED false) - Q_PROPERTY(QVariant address READ address WRITE setAddress NOTIFY changed) - Q_PROPERTY(QEthereum* ethereum READ ethereum WRITE setEthereum NOTIFY ethChanged) -}; - -class QEthereum: public QObject -{ - Q_OBJECT - -public: - QEthereum(QObject* _p = nullptr); - virtual ~QEthereum(); - - eth::Client* client() const; - - Q_INVOKABLE QVariant/*eth::Address*/ coinbase() const; - - Q_INVOKABLE bool isListening() const; - Q_INVOKABLE bool isMining() const; - - Q_INVOKABLE QVariant/*eth::u256*/ balanceAt(QVariant/*eth::Address*/ _a) const; - Q_INVOKABLE QVariant/*eth::u256*/ storageAt(QVariant/*eth::Address*/ _a, QVariant/*eth::u256*/ _p) const; - Q_INVOKABLE double txCountAt(QVariant/*eth::Address*/ _a) const; - Q_INVOKABLE bool isContractAt(QVariant/*eth::Address*/ _a) const; - - Q_INVOKABLE QVariant gasPrice() const { return toQJS(10 * eth::szabo); } - - Q_INVOKABLE QString ethTest() const { return "Hello world!"; } - - Q_INVOKABLE QVariant/*eth::KeyPair*/ key() const; - Q_INVOKABLE QList keys() const; - Q_INVOKABLE QVariant/*eth::Address*/ account() const; - Q_INVOKABLE QList accounts() const; - - Q_INVOKABLE unsigned peerCount() const; - - Q_INVOKABLE QEthereum* self() { return this; } - - Q_INVOKABLE QVariant create(QVariant _secret, QVariant _amount, QByteArray _code, QByteArray _init, QVariant _gas, QVariant _gasPrice); - Q_INVOKABLE void transact(QVariant _secret, QVariant _amount, QVariant _dest, QByteArray _data, QVariant _gas, QVariant _gasPrice); - - eth::u256 balanceAt(eth::Address _a) const; - double txCountAt(eth::Address _a) const; - bool isContractAt(eth::Address _a) const; - -public slots: - void setCoinbase(QVariant/*eth::Address*/); - void setMining(bool _l); - - void setListening(bool _l); - -signals: - void changed(); -// void netChanged(); -// void miningChanged(); - -private: -// Q_PROPERTY(QVariant coinbase READ coinbase WRITE setCoinbase NOTIFY changed) -// Q_PROPERTY(bool listening READ isListening WRITE setListening) -// Q_PROPERTY(bool mining READ isMining WRITE setMining) -}; struct WorldState { @@ -268,7 +41,7 @@ public: eth::Client* client() { return m_client.get(); } - QVector const& owned() const { return m_myKeys; } + QList const& owned() const { return m_myKeys; } private slots: void on_connect_triggered(); @@ -337,7 +110,7 @@ private: QTimer* m_refresh; QTimer* m_refreshNetwork; QStringList m_servers; - QVector m_myKeys; + QList m_myKeys; bool m_keysChanged = false; eth::bytes m_data; eth::bytes m_init; diff --git a/eth-ncurses-cli/CMakeLists.txt b/eth-ncurses-cli/CMakeLists.txt index 54f26ccee..9e5e71e51 100644 --- a/eth-ncurses-cli/CMakeLists.txt +++ b/eth-ncurses-cli/CMakeLists.txt @@ -5,44 +5,44 @@ aux_source_directory(. SRC_LIST) include_directories(../libethereum) link_directories(../libethereum) -add_executable(eth ${SRC_LIST}) +add_executable(eth-ncurses-cli ${SRC_LIST}) if (${TARGET_PLATFORM} STREQUAL "w64") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++") - target_link_libraries(eth gcc) - target_link_libraries(eth gdi32) - target_link_libraries(eth ws2_32) - target_link_libraries(eth mswsock) - target_link_libraries(eth shlwapi) - target_link_libraries(eth iphlpapi) - target_link_libraries(eth cryptopp) - target_link_libraries(eth ncurses) - target_link_libraries(eth form) - target_link_libraries(eth boost_system-mt-s) - target_link_libraries(eth boost_filesystem-mt-s) - target_link_libraries(eth boost_thread_win32-mt-s) + target_link_libraries(eth-ncurses-cli gcc) + target_link_libraries(eth-ncurses-cli gdi32) + target_link_libraries(eth-ncurses-cli ws2_32) + target_link_libraries(eth-ncurses-cli mswsock) + target_link_libraries(eth-ncurses-cli shlwapi) + target_link_libraries(eth-ncurses-cli iphlpapi) + target_link_libraries(eth-ncurses-cli cryptopp) + target_link_libraries(eth-ncurses-cli ncurses) + target_link_libraries(eth-ncurses-cli form) + target_link_libraries(eth-ncurses-cli boost_system-mt-s) + target_link_libraries(eth-ncurses-cli boost_filesystem-mt-s) + target_link_libraries(eth-ncurses-cli boost_thread_win32-mt-s) set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS) elseif (UNIX) - target_link_libraries(eth ncurses) - target_link_libraries(eth form) + target_link_libraries(eth-ncurses-cli ncurses) + target_link_libraries(eth-ncurses-cli form) else () - target_link_libraries(eth ${CRYPTOPP_LIBRARIES}) - target_link_libraries(eth boost_system) - target_link_libraries(eth boost_filesystem) - target_link_libraries(eth ncurses) - target_link_libraries(eth form) + target_link_libraries(eth-ncurses-cli ${CRYPTOPP_LIBRARIES}) + target_link_libraries(eth-ncurses-cli boost_system) + target_link_libraries(eth-ncurses-cli boost_filesystem) + target_link_libraries(eth-ncurses-cli ncurses) + target_link_libraries(eth-ncurses-cli form) find_package(Threads REQUIRED) - target_link_libraries(eth ${CMAKE_THREAD_LIBS_INIT}) + target_link_libraries(eth-ncurses-cli ${CMAKE_THREAD_LIBS_INIT}) endif () if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") include_directories(/usr/local/include) endif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") -target_link_libraries(eth ethereum) -target_link_libraries(eth miniupnpc) -target_link_libraries(eth leveldb) -target_link_libraries(eth gmp) +target_link_libraries(eth-ncurses-cli ethereum) +target_link_libraries(eth-ncurses-cli miniupnpc) +target_link_libraries(eth-ncurses-cli leveldb) +target_link_libraries(eth-ncurses-cli gmp) -install( TARGETS eth DESTINATION bin ) +install( TARGETS eth-ncurses-cli DESTINATION bin ) diff --git a/walleth/CMakeLists.txt b/walleth/CMakeLists.txt index a2b536cac..fbeb99c99 100644 --- a/walleth/CMakeLists.txt +++ b/walleth/CMakeLists.txt @@ -10,6 +10,7 @@ aux_source_directory(. SRC_LIST) include_directories(..) link_directories(../libethereum) +link_directories(../libqethereum) # Find Qt5 for Apple and update src_list for windows if (APPLE) @@ -57,7 +58,7 @@ else () endif () qt5_use_modules(${EXECUTEABLE} Core Gui Widgets Network Quick Qml) -target_link_libraries(${EXECUTEABLE} ethereum secp256k1 ${CRYPTOPP_LIBRARIES}) +target_link_libraries(${EXECUTEABLE} qethereum ethereum secp256k1 ${CRYPTOPP_LIBRARIES}) if (APPLE) if (${ADDFRAMEWORKS}) @@ -83,7 +84,7 @@ if (APPLE) install(CODE " include(BundleUtilities) set(BU_CHMOD_BUNDLE_ITEMS 1) - fixup_bundle(\"${APPS}\" \"${BUNDLELIBS}\" \"../libethereum ../secp256k1\") + fixup_bundle(\"${APPS}\" \"${BUNDLELIBS}\" \"../libqethereum ../libethereum ../secp256k1\") " COMPONENT RUNTIME ) if (${ADDFRAMEWORKS}) diff --git a/walleth/MainWin.cpp b/walleth/MainWin.cpp index 44315cac4..a35ff4063 100644 --- a/walleth/MainWin.cpp +++ b/walleth/MainWin.cpp @@ -54,136 +54,6 @@ using eth::g_logPost; using eth::g_logVerbosity; using eth::c_instructionInfo; -// Horrible global for the mainwindow. Needed for the QEthereums to find the Main window which acts as multiplexer for now. -// Can get rid of this once we've sorted out ITC for signalling & multiplexed querying. -Main* g_main = nullptr; - -QEthereum::QEthereum(QObject* _p): QObject(_p) -{ - connect(g_main, SIGNAL(changed()), SIGNAL(changed())); -} - -QEthereum::~QEthereum() -{ -} - -Client* QEthereum::client() const -{ - return g_main->client(); -} - -Address QEthereum::coinbase() const -{ - return client()->address(); -} - -void QEthereum::setCoinbase(Address _a) -{ - if (client()->address() != _a) - { - client()->setAddress(_a); - changed(); - } -} - -QAccount::QAccount(QObject*) -{ -} - -QAccount::~QAccount() -{ -} - -void QAccount::setEthereum(QEthereum* _eth) -{ - if (m_eth == _eth) - return; - if (m_eth) - disconnect(m_eth, SIGNAL(changed()), this, SIGNAL(changed())); - m_eth = _eth; - if (m_eth) - connect(m_eth, SIGNAL(changed()), this, SIGNAL(changed())); - ethChanged(); - changed(); -} - -u256 QAccount::balance() const -{ - if (m_eth) - return m_eth->balanceAt(m_address); - return 0; -} - -double QAccount::txCount() const -{ - if (m_eth) - return m_eth->txCountAt(m_address); - return 0; -} - -bool QAccount::isContract() const -{ - if (m_eth) - return m_eth->isContractAt(m_address); - return 0; -} - -u256 QEthereum::balanceAt(Address _a) const -{ - return client()->postState().balance(_a); -} - -bool QEthereum::isContractAt(Address _a) const -{ - return client()->postState().isContractAddress(_a); -} - -bool QEthereum::isMining() const -{ - return client()->isMining(); -} - -bool QEthereum::isListening() const -{ - return client()->haveNetwork(); -} - -void QEthereum::setMining(bool _l) -{ - if (_l) - client()->startMining(); - else - client()->stopMining(); -} - -void QEthereum::setListening(bool _l) -{ - if (_l) - client()->startNetwork(); - else - client()->stopNetwork(); -} - -double QEthereum::txCountAt(Address _a) const -{ - return (double)client()->postState().transactionsFrom(_a); -} - -unsigned QEthereum::peerCount() const -{ - return (unsigned)client()->peerCount(); -} - -void QEthereum::transact(Secret _secret, u256 _amount, u256 _gasPrice, u256 _gas, QByteArray _code, QByteArray _init) -{ - client()->transact(_secret, _amount, bytes(_code.data(), _code.data() + _code.size()), bytes(_init.data(), _init.data() + _init.size()), _gas, _gasPrice); -} - -void QEthereum::transact(Secret _secret, Address _dest, u256 _amount, u256 _gasPrice, u256 _gas, QByteArray _data) -{ - client()->transact(_secret, _amount, _dest, bytes(_data.data(), _data.data() + _data.size()), _gas, _gasPrice); -} - Main::Main(QWidget *parent) : QMainWindow(parent), ui(new Ui::Main) @@ -192,21 +62,23 @@ Main::Main(QWidget *parent) : ui->setupUi(this); setWindowIcon(QIcon(":/Ethereum.png")); - g_main = this; + g_qmlMain = this; m_client.reset(new Client("Walleth", Address(), eth::getDataDir() + "/Walleth")); + g_qmlClient = m_client.get(); + qRegisterMetaType("eth::u256"); qRegisterMetaType("eth::KeyPair"); qRegisterMetaType("eth::Secret"); qRegisterMetaType("eth::Address"); - qRegisterMetaType("QAccount*"); - qRegisterMetaType("QEthereum*"); + qRegisterMetaType("QmlAccount*"); + qRegisterMetaType("QmlEthereum*"); - qmlRegisterType("org.ethereum", 1, 0, "Ethereum"); - qmlRegisterType("org.ethereum", 1, 0, "Account"); - qmlRegisterSingletonType("org.ethereum", 1, 0, "Balance", QEthereum::constructU256Helper); - qmlRegisterSingletonType("org.ethereum", 1, 0, "Key", QEthereum::constructKeyHelper); + qmlRegisterType("org.ethereum", 1, 0, "Ethereum"); + qmlRegisterType("org.ethereum", 1, 0, "Account"); + qmlRegisterSingletonType("org.ethereum", 1, 0, "Balance", QmlEthereum::constructU256Helper); + qmlRegisterSingletonType("org.ethereum", 1, 0, "Key", QmlEthereum::constructKeyHelper); /* ui->librariesView->setModel(m_libraryMan); diff --git a/walleth/MainWin.h b/walleth/MainWin.h index 54f8e4d93..4cc524035 100644 --- a/walleth/MainWin.h +++ b/walleth/MainWin.h @@ -6,6 +6,7 @@ #include #include #include +#include namespace Ui { class Main; @@ -20,144 +21,6 @@ class QQuickView; class QQmlEngine; class QJSEngine; -class QEthereum; -class QAccount; - -Q_DECLARE_METATYPE(eth::u256) -Q_DECLARE_METATYPE(eth::Address) -Q_DECLARE_METATYPE(eth::Secret) -Q_DECLARE_METATYPE(eth::KeyPair) -Q_DECLARE_METATYPE(QEthereum*) -Q_DECLARE_METATYPE(QAccount*) - -class U256Helper: public QObject -{ - Q_OBJECT - -public: - U256Helper(QObject* _p = nullptr): QObject(_p) {} - - Q_INVOKABLE eth::u256 add(eth::u256 _a, eth::u256 _b) const { return _a + _b; } - Q_INVOKABLE eth::u256 sub(eth::u256 _a, eth::u256 _b) const { return _a - _b; } - Q_INVOKABLE eth::u256 mul(eth::u256 _a, int _b) const { return _a * _b; } - Q_INVOKABLE eth::u256 mul(int _a, eth::u256 _b) const { return _a * _b; } - Q_INVOKABLE eth::u256 div(eth::u256 _a, int _b) const { return _a / _b; } - - Q_INVOKABLE eth::u256 wei(double _s) const { return (eth::u256)_s; } - Q_INVOKABLE eth::u256 szabo(double _s) const { return (eth::u256)(_s * (double)eth::szabo); } - Q_INVOKABLE eth::u256 finney(double _s) const { return (eth::u256)(_s * (double)eth::finney); } - Q_INVOKABLE eth::u256 ether(double _s) const { return (eth::u256)(_s * (double)eth::ether); } - Q_INVOKABLE eth::u256 wei(unsigned _s) const { return (eth::u256)_s; } - Q_INVOKABLE eth::u256 szabo(unsigned _s) const { return (eth::u256)(_s * eth::szabo); } - Q_INVOKABLE eth::u256 finney(unsigned _s) const { return (eth::u256)(_s * eth::finney); } - Q_INVOKABLE eth::u256 ether(unsigned _s) const { return (eth::u256)(_s * eth::ether); } - Q_INVOKABLE double toWei(eth::u256 _t) const { return (double)_t; } - Q_INVOKABLE double toSzabo(eth::u256 _t) const { return toWei(_t) / (double)eth::szabo; } - Q_INVOKABLE double toFinney(eth::u256 _t) const { return toWei(_t) / (double)eth::finney; } - Q_INVOKABLE double toEther(eth::u256 _t) const { return toWei(_t) / (double)eth::ether; } - - Q_INVOKABLE double value(eth::u256 _t) const { return (double)_t; } - - Q_INVOKABLE QString stringOf(eth::u256 _t) const { return QString::fromStdString(eth::formatBalance(_t)); } -}; - -class KeyHelper: public QObject -{ - Q_OBJECT - -public: - KeyHelper(QObject* _p = nullptr): QObject(_p) {} - - Q_INVOKABLE eth::KeyPair create() const { return eth::KeyPair::create(); } - Q_INVOKABLE eth::Address address(eth::KeyPair _p) const { return _p.address(); } - Q_INVOKABLE eth::Secret secret(eth::KeyPair _p) const { return _p.secret(); } - Q_INVOKABLE eth::KeyPair keypair(eth::Secret _k) const { return eth::KeyPair(_k); } - - Q_INVOKABLE bool isNull(eth::Address _a) const { return !_a; } - - Q_INVOKABLE eth::Address addressOf(QString _s) const { return eth::Address(_s.toStdString()); } - Q_INVOKABLE QString stringOf(eth::Address _a) const { return QString::fromStdString(eth::toHex(_a.asArray())); } - Q_INVOKABLE QString toAbridged(eth::Address _a) const { return QString::fromStdString(_a.abridged()); } -}; - -class QAccount: public QObject -{ - Q_OBJECT - -public: - QAccount(QObject* _p = nullptr); - virtual ~QAccount(); - - Q_INVOKABLE QEthereum* ethereum() const { return m_eth; } - Q_INVOKABLE eth::u256 balance() const; - Q_INVOKABLE double txCount() const; - Q_INVOKABLE bool isContract() const; - - // TODO: past transactions models. - -public slots: - void setEthereum(QEthereum* _eth); - -signals: - void changed(); - void ethChanged(); - -private: - QEthereum* m_eth = nullptr; - eth::Address m_address; - - Q_PROPERTY(eth::u256 balance READ balance NOTIFY changed STORED false) - Q_PROPERTY(double txCount READ txCount NOTIFY changed STORED false) - Q_PROPERTY(bool isContract READ isContract NOTIFY changed STORED false) - Q_PROPERTY(eth::Address address MEMBER m_address NOTIFY changed) - Q_PROPERTY(QEthereum* ethereum READ ethereum WRITE setEthereum NOTIFY ethChanged) -}; - -class QEthereum: public QObject -{ - Q_OBJECT - -public: - QEthereum(QObject* _p = nullptr); - virtual ~QEthereum(); - - eth::Client* client() const; - - static QObject* constructU256Helper(QQmlEngine*, QJSEngine*) { return new U256Helper; } - static QObject* constructKeyHelper(QQmlEngine*, QJSEngine*) { return new KeyHelper; } - - Q_INVOKABLE eth::Address coinbase() const; - - Q_INVOKABLE bool isListening() const; - Q_INVOKABLE bool isMining() const; - - Q_INVOKABLE eth::u256 balanceAt(eth::Address _a) const; - Q_INVOKABLE double txCountAt(eth::Address _a) const; - Q_INVOKABLE bool isContractAt(eth::Address _a) const; - - Q_INVOKABLE unsigned peerCount() const; - - Q_INVOKABLE QEthereum* self() { return this; } - -public slots: - void transact(eth::Secret _secret, eth::Address _dest, eth::u256 _amount, eth::u256 _gasPrice, eth::u256 _gas, QByteArray _data); - void transact(eth::Secret _secret, eth::u256 _amount, eth::u256 _gasPrice, eth::u256 _gas, QByteArray _code, QByteArray _init); - void setCoinbase(eth::Address); - void setMining(bool _l); - - void setListening(bool _l); - -signals: - void changed(); -// void netChanged(); -// void miningChanged(); - -private: - Q_PROPERTY(eth::Address coinbase READ coinbase WRITE setCoinbase NOTIFY changed) - Q_PROPERTY(bool listening READ isListening WRITE setListening) - Q_PROPERTY(bool mining READ isMining WRITE setMining) -}; - class Main : public QMainWindow { Q_OBJECT