Marek Kotewicz
10 years ago
5 changed files with 296 additions and 10 deletions
@ -0,0 +1,152 @@ |
|||
#include "CommonJS.h" |
|||
|
|||
namespace dev { |
|||
namespace eth { |
|||
|
|||
bytes dev::eth::jsToBytes(std::string const& _s) |
|||
{ |
|||
if (_s.substr(0, 2) == "0x") |
|||
// Hex
|
|||
return fromHex(_s.substr(2)); |
|||
else if (_s.find_first_not_of("0123456789") == std::string::npos) |
|||
// Decimal
|
|||
return toCompactBigEndian(bigint(_s)); |
|||
else |
|||
// Binary
|
|||
return asBytes(_s); |
|||
} |
|||
|
|||
std::string dev::eth::jsPadded(std::string const& _s, unsigned _l, unsigned _r) |
|||
{ |
|||
bytes b = jsToBytes(_s); |
|||
while (b.size() < _l) |
|||
b.insert(b.begin(), 0); |
|||
while (b.size() < _r) |
|||
b.push_back(0); |
|||
return asString(b).substr(b.size() - std::max(_l, _r)); |
|||
} |
|||
|
|||
std::string dev::eth::jsPadded(std::string const& _s, unsigned _l) |
|||
{ |
|||
if (_s.substr(0, 2) == "0x" || _s.find_first_not_of("0123456789") == std::string::npos) |
|||
// Numeric: pad to right
|
|||
return jsPadded(_s, _l, _l); |
|||
else |
|||
// Text: pad to the left
|
|||
return jsPadded(_s, 0, _l); |
|||
} |
|||
|
|||
std::string dev::eth::jsUnpadded(std::string _s) |
|||
{ |
|||
auto p = _s.find_last_not_of((char)0); |
|||
_s.resize(p == std::string::npos ? 0 : (p + 1)); |
|||
return _s; |
|||
} |
|||
|
|||
dev::eth::Interface* CommonJS::client() const |
|||
{ |
|||
return m_client; |
|||
} |
|||
|
|||
void CommonJS::setAccounts(std::vector<dev::KeyPair> _accounts) |
|||
{ |
|||
m_accounts = _accounts; |
|||
} |
|||
|
|||
std::string CommonJS::coinbase() const |
|||
{ |
|||
return m_client ? toJS(client()->address()) : ""; |
|||
} |
|||
|
|||
bool CommonJS::isListening() const |
|||
{ |
|||
return /*m_client ? client()->haveNetwork() :*/ false; |
|||
} |
|||
|
|||
void CommonJS::setListening(bool _l) |
|||
{ |
|||
if (!m_client) |
|||
return; |
|||
/* if (_l)
|
|||
client()->startNetwork(); |
|||
else |
|||
client()->stopNetwork();*/ |
|||
} |
|||
|
|||
bool CommonJS::isMining() const |
|||
{ |
|||
return m_client ? client()->isMining() : false; |
|||
} |
|||
|
|||
void CommonJS::setMining(bool _l) |
|||
{ |
|||
if (m_client) |
|||
{ |
|||
if (_l) |
|||
client()->startMining(); |
|||
else |
|||
client()->stopMining(); |
|||
} |
|||
} |
|||
|
|||
std::string CommonJS::gasPrice() const |
|||
{ |
|||
return toJS(10 * dev::eth::szabo); |
|||
} |
|||
|
|||
std::string CommonJS::key() const |
|||
{ |
|||
if (m_accounts.empty()) |
|||
return toJS(KeyPair().sec()); |
|||
return toJS(m_accounts[0].sec()); |
|||
} |
|||
|
|||
std::vector<std::string> CommonJS::keys() const |
|||
{ |
|||
std::vector<std::string> ret; |
|||
for (auto i: m_accounts) |
|||
ret.push_back(toJS(i.sec())); |
|||
return ret; |
|||
} |
|||
|
|||
unsigned CommonJS::peerCount() const |
|||
{ |
|||
return /*m_client ? (unsigned)client()->peerCount() :*/ 0; |
|||
} |
|||
|
|||
int CommonJS::defaultBlock() const |
|||
{ |
|||
return m_client ? m_client->getDefault() : 0; |
|||
} |
|||
|
|||
unsigned CommonJS::number() const |
|||
{ |
|||
return m_client ? client()->number() + 1 : 0; |
|||
} |
|||
|
|||
std::string CommonJS::balanceAt(const std::string &_a, int _block) const |
|||
{ |
|||
return m_client ? toJS(client()->balanceAt(jsToAddress(_a), _block)) : ""; |
|||
} |
|||
|
|||
std::string CommonJS::stateAt(const std::string &_a, std::string &_p, int _block) const |
|||
{ |
|||
return m_client ? toJS(client()->stateAt(jsToAddress(_a), jsToU256(_p), _block)) : ""; |
|||
} |
|||
|
|||
double CommonJS::countAt(const std::string &_a, int _block) const |
|||
{ |
|||
return m_client ? (double)(uint64_t)client()->countAt(jsToAddress(_a), _block) : 0; |
|||
} |
|||
|
|||
std::string CommonJS::codeAt(const std::string &_a, int _block) const |
|||
{ |
|||
return m_client ? jsFromBinary(client()->codeAt(jsToAddress(_a), _block)) : ""; |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
} |
|||
} |
@ -0,0 +1,103 @@ |
|||
#pragma once |
|||
|
|||
#include <string> |
|||
#include <vector> |
|||
#include <libethereum/Interface.h> |
|||
#include "Common.h" |
|||
#include "CommonData.h" |
|||
|
|||
namespace dev { |
|||
namespace eth { |
|||
|
|||
bytes jsToBytes(std::string const& _s); |
|||
std::string jsPadded(std::string const& _s, unsigned _l, unsigned _r); |
|||
std::string jsPadded(std::string const& _s, unsigned _l); |
|||
std::string jsUnpadded(std::string _s); |
|||
|
|||
template <unsigned N> FixedHash<N> jsToFixed(std::string const& _s) |
|||
{ |
|||
if (_s.substr(0, 2) == "0x") |
|||
// Hex
|
|||
return FixedHash<N>(_s.substr(2)); |
|||
else if (_s.find_first_not_of("0123456789") == std::string::npos) |
|||
// Decimal
|
|||
return (typename FixedHash<N>::Arith)(_s); |
|||
else |
|||
// Binary
|
|||
return FixedHash<N>(asBytes(jsPadded(_s, N))); |
|||
} |
|||
|
|||
template <unsigned N> boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N * 8, N * 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>> jsToInt(std::string const& _s) |
|||
{ |
|||
if (_s.substr(0, 2) == "0x") |
|||
// Hex
|
|||
return fromBigEndian<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N * 8, N * 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>>(fromHex(_s.substr(2))); |
|||
else if (_s.find_first_not_of("0123456789") == std::string::npos) |
|||
// Decimal
|
|||
return boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N * 8, N * 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>(_s); |
|||
else |
|||
// Binary
|
|||
return fromBigEndian<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N * 8, N * 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>>(asBytes(jsPadded(_s, N))); |
|||
} |
|||
|
|||
inline Address jsToAddress(std::string const& _s) { return jsToFixed<20>(_s); } |
|||
inline Secret jsToSecret(std::string const& _s) { return jsToFixed<32>(_s); } |
|||
inline u256 jsToU256(std::string const& _s) { return jsToInt<32>(_s); } |
|||
|
|||
inline std::string jsFromBinary(dev::bytes _s, unsigned _padding = 32) |
|||
{ |
|||
_s.resize(std::max<unsigned>(_s.size(), _padding)); |
|||
return "0x" + dev::toHex(_s); |
|||
} |
|||
|
|||
inline std::string jsFromBinary(std::string const& _s, unsigned _padding = 32) |
|||
{ |
|||
return jsFromBinary(asBytes(_s), _padding); |
|||
} |
|||
|
|||
|
|||
template <unsigned S> std::string toJS(FixedHash<S> const& _h) { return "0x" + toHex(_h.ref()); } |
|||
template <unsigned N> std::string toJS(boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N, N, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>> const& _n) { return "0x" + toHex(toCompactBigEndian(_n)); } |
|||
|
|||
class CommonJS |
|||
{ |
|||
public: |
|||
CommonJS(dev::eth::Interface* _c) : m_client(_c) {} |
|||
dev::eth::Interface* client() const; |
|||
void setAccounts(std::vector<dev::KeyPair> _accounts); |
|||
|
|||
std::string ethTest() const; |
|||
|
|||
// properties
|
|||
std::string coinbase() const; |
|||
bool isListening() const; |
|||
void setListening(bool _l); |
|||
bool isMining() const; |
|||
void setMining(bool _l); |
|||
std::string /*dev::u256*/ gasPrice() const; |
|||
std::string /*dev::KeyPair*/ key() const; |
|||
std::vector<std::string> /*list of dev::KeyPair*/ keys() const; |
|||
unsigned peerCount() const; |
|||
int defaultBlock() const; |
|||
unsigned /*dev::u256*/ number() const; |
|||
|
|||
// synchronous getters
|
|||
std::string balanceAt(std::string const &_a, int _block) const; |
|||
std::string stateAt(std::string const &_a, std::string &_p, int _block) const; |
|||
double countAt(std::string const &_a, int _block) const; |
|||
std::string codeAt(std::string const &_a, int _block) const; |
|||
|
|||
// transactions
|
|||
void transact(std::string const &_json); |
|||
void call(std::string const &_json); |
|||
|
|||
// blockchain
|
|||
|
|||
|
|||
private: |
|||
dev::eth::Interface* m_client; |
|||
std::vector<dev::KeyPair> m_accounts; |
|||
}; |
|||
} |
|||
} |
|||
|
Loading…
Reference in new issue