From 2655c8bc46873c074d96f5b3933884cc05cd4458 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 1 May 2014 00:06:49 +0100 Subject: [PATCH] New JS-API more/less working. --- libethereum/AddressState.h | 2 +- libqethereum/QEthereum.cpp | 6 ++++-- libqethereum/QEthereum.h | 34 +++++++++++++++++++++++++++------- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/libethereum/AddressState.h b/libethereum/AddressState.h index 45c4b9ead..4efee6751 100644 --- a/libethereum/AddressState.h +++ b/libethereum/AddressState.h @@ -46,7 +46,7 @@ public: u256 const& nonce() const { return m_nonce; } void incNonce() { m_nonce++; } - h256 oldRoot() const { assert(m_storageOverlay.empty()); return m_storageRoot; } + h256 oldRoot() const { return m_storageRoot; } std::map const& storage() const { return m_storageOverlay; } void setStorage(u256 _p, u256 _v) { m_storageOverlay[_p] = _v; } diff --git a/libqethereum/QEthereum.cpp b/libqethereum/QEthereum.cpp index 163028a4d..1d2b96f58 100644 --- a/libqethereum/QEthereum.cpp +++ b/libqethereum/QEthereum.cpp @@ -193,7 +193,7 @@ eth::bytes toBytes(QString const& _s) return eth::toCompactBigEndian(eth::bigint(_s.toStdString())); else // Binary - return eth::asBytes(_s.toStdString()); + return asBytes(_s); } QString padded(QString const& _s, unsigned _l, unsigned _r) @@ -206,6 +206,8 @@ QString padded(QString const& _s, unsigned _l, unsigned _r) return QString::fromStdString(eth::asString(b).substr(b.size() - max(_l, _r))); } +//"0xff".bin().unbin() + QString padded(QString const& _s, unsigned _l) { if (_s.startsWith("0x") || !_s.contains(QRegExp("[^0-9]"))) @@ -245,7 +247,7 @@ void QEthereum::setup(QWebFrame* _e) _e->evaluateJavaScript("eth.create = function(s, v, c, g, p, f) { eth.doCreate(s, v, c, g, p); if (f) f() }"); _e->evaluateJavaScript("eth.transact = function(s, v, t, d, g, p, f) { eth.doTransact(s, v, t, d, g, p); if (f) f() }"); _e->evaluateJavaScript("String.prototype.pad = function(l, r) { return eth.pad(this, l, r) }"); - _e->evaluateJavaScript("String.prototype.bin = function(l) { return eth.toBinary(this) }"); + _e->evaluateJavaScript("String.prototype.bin = function() { return eth.toBinary(this) }"); _e->evaluateJavaScript("String.prototype.unbin = function(l) { return eth.fromBinary(this) }"); _e->evaluateJavaScript("String.prototype.unpad = function(l) { return eth.unpad(this) }"); } diff --git a/libqethereum/QEthereum.h b/libqethereum/QEthereum.h index 1de5aab59..4a00e1977 100644 --- a/libqethereum/QEthereum.h +++ b/libqethereum/QEthereum.h @@ -285,6 +285,24 @@ public: }; #endif +inline eth::bytes asBytes(QString const& _s) +{ + eth::bytes ret; + ret.reserve(_s.size()); + for (QChar c: _s) + ret.push_back(c.cell()); + return ret; +} + +inline QString asQString(eth::bytes const& _s) +{ + QString ret; + ret.reserve(_s.size()); + for (auto c: _s) + ret.push_back(QChar(c, 0)); + return ret; +} + eth::bytes toBytes(QString const& _s); QString padded(QString const& _s, unsigned _l, unsigned _r); QString padded(QString const& _s, unsigned _l); @@ -300,17 +318,19 @@ template eth::FixedHash toFixed(QString const& _s) return (typename eth::FixedHash::Arith)(_s.toStdString()); else // Binary - return eth::FixedHash(eth::asBytes(padded(_s, N).toStdString())); + return eth::FixedHash(asBytes(padded(_s, N))); } -template boost::multiprecision::number> toInt(QString const& _s) +template boost::multiprecision::number> toInt(QString const& _s) { - if (_s.startsWith("0x") || !_s.contains(QRegExp("[^0-9]"))) + if (_s.startsWith("0x")) + return eth::fromBigEndian>>(eth::fromHex(_s.toStdString().substr(2))); + else if (!_s.contains(QRegExp("[^0-9]"))) // Hex or Decimal - return boost::multiprecision::number>(_s.toStdString()); + return boost::multiprecision::number>(_s.toStdString()); else // Binary - return eth::fromBigEndian>>(eth::asBytes(padded(_s, N).toStdString())); + return eth::fromBigEndian>>(asBytes(padded(_s, N))); } inline eth::Address toAddress(QString const& _s) { return toFixed<20>(_s); } @@ -322,12 +342,12 @@ template QString toQJS(boost::multiprecision::number