From 98d425fa12e702d87d7d118662c9d97f4b6f4a2f Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sat, 9 Aug 2014 19:34:32 +0100 Subject: [PATCH] New PoC-6 JS API. --- libqethereum/QEthereum.cpp | 12 +++++++++-- libqethereum/QEthereum.h | 43 ++++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/libqethereum/QEthereum.cpp b/libqethereum/QEthereum.cpp index ecc312d07..9dd866a9a 100644 --- a/libqethereum/QEthereum.cpp +++ b/libqethereum/QEthereum.cpp @@ -53,8 +53,11 @@ eth::bytes toBytes(QString const& _s) // Decimal return eth::toCompactBigEndian(eth::bigint(_s.toStdString())); else + { // Binary + cwarn << "THIS FUNCTIONALITY IS DEPRECATED. DO NOT ASSUME ASCII/BINARY-STRINGS WILL BE ACCEPTED. USE eth.fromAscii()."; return asBytes(_s); + } } QString padded(QString const& _s, unsigned _l, unsigned _r) @@ -131,6 +134,11 @@ QString QEthereum::sha3(QString _s) const return toQJS(eth::sha3(asBytes(_s))); } +QString QEthereum::sha3old(QString _s) const +{ + return toQJS(eth::sha3(asBytes(_s))); +} + QString QEthereum::offset(QString _s, int _i) const { return toQJS(toU256(_s) + _i); @@ -449,9 +457,9 @@ unsigned QEthereum::newWatch(QString _json) if (!m_client) return (unsigned)-1; unsigned ret; - if (_json == "chainChanged") + if (_json == "chain") ret = m_client->installWatch(eth::ChainChangedFilter); - else if (_json == "pendingChanged") + else if (_json == "pending") ret = m_client->installWatch(eth::PendingChangedFilter); else ret = m_client->installWatch(toMessageFilter(_json)); diff --git a/libqethereum/QEthereum.h b/libqethereum/QEthereum.h index c1dc2208b..c4b10042a 100644 --- a/libqethereum/QEthereum.h +++ b/libqethereum/QEthereum.h @@ -77,14 +77,20 @@ inline double toFixed(QString const& _s) return (double)toU256(_s) / (double)(eth::u256(1) << 128); } -inline QString fromBinary(eth::bytes const& _s) +inline QString fromFixed(double _s) { + return toQJS(eth::u256(_s * (double)(eth::u256(1) << 128))); +} + +inline QString fromBinary(eth::bytes _s, unsigned _padding = 32) +{ + _s.resize(std::max(_s.size(), _padding)); return QString::fromStdString("0x" + eth::toHex(_s)); } -inline QString fromBinary(QString const& _s) +inline QString fromBinary(QString const& _s, unsigned _padding = 32) { - return fromBinary(asBytes(_s)); + return fromBinary(asBytes(_s), _padding); } class QEthereum: public QObject @@ -110,14 +116,19 @@ public: Q_INVOKABLE QString lll(QString _s) const; Q_INVOKABLE QString sha3(QString _s) const; + Q_INVOKABLE QString sha3old(QString _s) const; Q_INVOKABLE QString offset(QString _s, int _offset) const; + Q_INVOKABLE QString pad(QString _s, unsigned _l) const { return padded(_s, _l); } Q_INVOKABLE QString pad(QString _s, unsigned _l, unsigned _r) const { return padded(_s, _l, _r); } Q_INVOKABLE QString unpad(QString _s) const { return unpadded(_s); } - Q_INVOKABLE QString toBinary(QString _s) const { return ::toBinary(_s); } - Q_INVOKABLE QString fromBinary(QString _s) const { return ::fromBinary(_s); } + + Q_INVOKABLE QString toAscii(QString _s) const { return ::toBinary(_s); } + Q_INVOKABLE QString fromAscii(QString _s) const { return ::fromBinary(_s, 32); } + Q_INVOKABLE QString fromAscii(QString _s, unsigned _padding) const { return ::fromBinary(_s, _padding); } Q_INVOKABLE QString toDecimal(QString _s) const { return ::toDecimal(_s); } Q_INVOKABLE double toFixed(QString _s) const { return ::toFixed(_s); } + Q_INVOKABLE QString fromFixed(double _d) const { return ::fromFixed(_d); } // [NEW API] - Use this instead. Q_INVOKABLE QString/*eth::u256*/ balanceAt(QString/*eth::Address*/ _a, int _block) const; @@ -197,20 +208,20 @@ private: frame->addToJavaScriptWindowObject("eth", eth, QWebFrame::ScriptOwnership); \ frame->evaluateJavaScript("eth.makeWatch = function(a) { var ww = eth.newWatch(a); var ret = { w: ww }; ret.uninstall = function() { eth.killWatch(w); }; ret.changed = function(f) { eth.watchChanged.connect(function(nw) { if (nw == ww) f() }); }; ret.messages = function() { return JSON.parse(eth.watchMessages(this.w)) }; return ret; }"); \ frame->evaluateJavaScript("eth.watch = function(a) { return eth.makeWatch(JSON.stringify(a)) }"); \ - frame->evaluateJavaScript("eth.watchChain = function() { return eth.makeWatch('chainChanged') }"); \ - frame->evaluateJavaScript("eth.watchPending = function() { return eth.makeWatch('pendingChanged') }"); \ - frame->evaluateJavaScript("eth.create = function(s, v, c, g, p, f) { var v = eth.doCreate(s, v, c, g, p); if (f) f(v) }"); \ - frame->evaluateJavaScript("eth.transact = function(a_s, f_v, t, d, g, p, f) { if (t == null) { eth.doTransact(JSON.stringify(a_s)); if (f_v) f_v(); } else { eth.doTransact(a_s, f_v, t, d, g, p); if (f) f() } }"); \ + frame->evaluateJavaScript("eth.watchChain = function() { env.warn('THIS CALL IS DEPRECATED. USE eth.watch('chain') INSTEAD.'); return eth.makeWatch('chain') }"); \ + frame->evaluateJavaScript("eth.watchPending = function() { env.warn('THIS CALL IS DEPRECATED. USE eth.watch('pending') INSTEAD.'); return eth.makeWatch('pending') }"); \ + frame->evaluateJavaScript("eth.create = function(s, v, c, g, p, f) { env.warn('THIS CALL IS DEPRECATED. USE eth.transact INSTEAD.'); var v = eth.doCreate(s, v, c, g, p); if (f) f(v) }"); \ + frame->evaluateJavaScript("eth.transact = function(a_s, f_v, t, d, g, p, f) { if (t == null) { eth.doTransact(JSON.stringify(a_s)); if (f_v) f_v(); } else { env.warn('THIS FORM OF THIS CALL IS DEPRECATED.'); eth.doTransact(a_s, f_v, t, d, g, p); if (f) f() } }"); \ frame->evaluateJavaScript("eth.call = function(a, f) { var ret = eth.doCallJson(JSON.stringify(a)); if (f) f(ret); return ret; }"); \ frame->evaluateJavaScript("eth.messages = function(a) { return JSON.parse(eth.getMessages(JSON.stringify(a))); }"); \ frame->evaluateJavaScript("eth.transactions = function(a) { env.warn('THIS CALL IS DEPRECATED. USE eth.messages INSTEAD.'); return JSON.parse(eth.getMessages(JSON.stringify(a))); }"); \ - frame->evaluateJavaScript("String.prototype.pad = function(l, r) { return eth.pad(this, l, r) }"); \ - frame->evaluateJavaScript("String.prototype.bin = function() { return eth.toBinary(this) }"); \ - frame->evaluateJavaScript("String.prototype.unbin = function(l) { return eth.fromBinary(this) }"); \ - frame->evaluateJavaScript("String.prototype.unpad = function(l) { return eth.unpad(this) }"); \ - frame->evaluateJavaScript("String.prototype.dec = function() { return eth.toDecimal(this) }"); \ - frame->evaluateJavaScript("String.prototype.fix = function() { return eth.toFixed(this) }"); \ - frame->evaluateJavaScript("String.prototype.sha3 = function() { return eth.sha3(this) }"); \ + frame->evaluateJavaScript("String.prototype.pad = function(l, r) { env.warn('THIS CALL IS DEPRECATED. USE eth.* INSTEAD.'); return eth.pad(this, l, r) }"); \ + frame->evaluateJavaScript("String.prototype.bin = function() { env.warn('THIS CALL IS DEPRECATED. USE eth.* INSTEAD.'); return eth.toAscii(this) }"); \ + frame->evaluateJavaScript("String.prototype.unbin = function(l) { env.warn('THIS CALL IS DEPRECATED. USE eth.* INSTEAD.'); return eth.fromAscii(this) }"); \ + frame->evaluateJavaScript("String.prototype.unpad = function(l) { env.warn('THIS CALL IS DEPRECATED. USE eth.* INSTEAD.'); return eth.unpad(this) }"); \ + frame->evaluateJavaScript("String.prototype.dec = function() { env.warn('THIS CALL IS DEPRECATED. USE eth.* INSTEAD.'); return eth.toDecimal(this) }"); \ + frame->evaluateJavaScript("String.prototype.fix = function() { env.warn('THIS CALL IS DEPRECATED. USE eth.* INSTEAD.'); return eth.toFixed(this) }"); \ + frame->evaluateJavaScript("String.prototype.sha3 = function() { env.warn('THIS CALL IS DEPRECATED. USE eth.* INSTEAD.'); return eth.sha3old(this) }"); \ } template inline boost::multiprecision::number> toInt(QString const& _s)