subtly
10 years ago
97 changed files with 4024 additions and 690 deletions
@ -0,0 +1,49 @@ |
|||
# Find v8 |
|||
# |
|||
# Find the v8 includes and library |
|||
# |
|||
# if you nee to add a custom library search path, do it via via CMAKE_PREFIX_PATH |
|||
# |
|||
# This module defines |
|||
# V8_INCLUDE_DIRS, where to find header, etc. |
|||
# V8_LIBRARIES, the libraries needed to use v8. |
|||
# V8_FOUND, If false, do not try to use v8. |
|||
|
|||
# only look in default directories |
|||
find_path( |
|||
V8_INCLUDE_DIR |
|||
NAMES v8.h |
|||
DOC "v8 include dir" |
|||
) |
|||
|
|||
find_library( |
|||
V8_LIBRARY |
|||
NAMES v8 |
|||
DOC "v8 library" |
|||
) |
|||
|
|||
set(V8_INCLUDE_DIRS ${V8_INCLUDE_DIR}) |
|||
set(V8_LIBRARIES ${V8_LIBRARY}) |
|||
|
|||
# debug library on windows |
|||
# same naming convention as in qt (appending debug library with d) |
|||
# boost is using the same "hack" as us with "optimized" and "debug" |
|||
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") |
|||
|
|||
find_library( |
|||
V8_LIBRARY_DEBUG |
|||
NAMES v8d |
|||
DOC "v8 debug library" |
|||
) |
|||
|
|||
set(V8_LIBRARIES optimized ${V8_LIBRARIES} debug ${V8_LIBRARY_DEBUG}) |
|||
|
|||
endif() |
|||
|
|||
# handle the QUIETLY and REQUIRED arguments and set V8_FOUND to TRUE |
|||
# if all listed variables are TRUE, hide their existence from configuration view |
|||
include(FindPackageHandleStandardArgs) |
|||
find_package_handle_standard_args(v8 DEFAULT_MSG |
|||
V8_INCLUDE_DIR V8_LIBRARY) |
|||
mark_as_advanced (V8_INCLUDE_DIR V8_LIBRARY) |
|||
|
@ -0,0 +1,220 @@ |
|||
/*
|
|||
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 SecretStore.cpp
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#include "SecretStore.h" |
|||
#include <thread> |
|||
#include <mutex> |
|||
#include <boost/filesystem.hpp> |
|||
#include <libdevcore/Log.h> |
|||
#include <libdevcore/Guards.h> |
|||
#include <test/JsonSpiritHeaders.h> |
|||
#include "SHA3.h" |
|||
#include "FileSystem.h" |
|||
using namespace std; |
|||
using namespace dev; |
|||
namespace js = json_spirit; |
|||
namespace fs = boost::filesystem; |
|||
|
|||
SecretStore::SecretStore() |
|||
{ |
|||
load(); |
|||
} |
|||
|
|||
SecretStore::~SecretStore() |
|||
{ |
|||
} |
|||
|
|||
bytes SecretStore::secret(h128 const& _uuid, function<std::string()> const& _pass) const |
|||
{ |
|||
auto rit = m_cached.find(_uuid); |
|||
if (rit != m_cached.end()) |
|||
return rit->second; |
|||
auto it = m_keys.find(_uuid); |
|||
if (it == m_keys.end()) |
|||
return bytes(); |
|||
bytes key = decrypt(it->second.first, _pass()); |
|||
if (!key.empty()) |
|||
m_cached[_uuid] = key; |
|||
return key; |
|||
} |
|||
|
|||
h128 SecretStore::importSecret(bytes const& _s, std::string const& _pass) |
|||
{ |
|||
h128 r = h128::random(); |
|||
m_cached[r] = _s; |
|||
m_keys[r] = make_pair(encrypt(_s, _pass), std::string()); |
|||
save(); |
|||
return r; |
|||
} |
|||
|
|||
void SecretStore::kill(h128 const& _uuid) |
|||
{ |
|||
m_cached.erase(_uuid); |
|||
if (m_keys.count(_uuid)) |
|||
{ |
|||
boost::filesystem::remove(m_keys[_uuid].second); |
|||
m_keys.erase(_uuid); |
|||
} |
|||
} |
|||
|
|||
void SecretStore::clearCache() const |
|||
{ |
|||
m_cached.clear(); |
|||
} |
|||
|
|||
void SecretStore::save(std::string const& _keysPath) |
|||
{ |
|||
fs::path p(_keysPath); |
|||
boost::filesystem::create_directories(p); |
|||
for (auto& k: m_keys) |
|||
{ |
|||
std::string uuid = toUUID(k.first); |
|||
std::string filename = (p / uuid).string() + ".json"; |
|||
js::mObject v; |
|||
js::mValue crypto; |
|||
js::read_string(k.second.first, crypto); |
|||
v["crypto"] = crypto; |
|||
v["id"] = uuid; |
|||
v["version"] = 2; |
|||
writeFile(filename, js::write_string(js::mValue(v), true)); |
|||
if (!k.second.second.empty() && k.second.second != filename) |
|||
boost::filesystem::remove(k.second.second); |
|||
k.second.second = filename; |
|||
} |
|||
} |
|||
|
|||
void SecretStore::load(std::string const& _keysPath) |
|||
{ |
|||
fs::path p(_keysPath); |
|||
js::mValue v; |
|||
for (fs::directory_iterator it(p); it != fs::directory_iterator(); ++it) |
|||
if (is_regular_file(it->path())) |
|||
{ |
|||
cdebug << "Reading" << it->path(); |
|||
js::read_string(contentsString(it->path().string()), v); |
|||
if (v.type() == js::obj_type) |
|||
{ |
|||
js::mObject o = v.get_obj(); |
|||
int version = o.count("Version") ? stoi(o["Version"].get_str()) : o.count("version") ? o["version"].get_int() : 0; |
|||
if (version == 2) |
|||
m_keys[fromUUID(o["id"].get_str())] = make_pair(js::write_string(o["crypto"], false), it->path().string()); |
|||
else |
|||
cwarn << "Cannot read key version" << version; |
|||
} |
|||
// else
|
|||
// cwarn << "Invalid JSON in key file" << it->path().string();
|
|||
} |
|||
} |
|||
|
|||
std::string SecretStore::encrypt(bytes const& _v, std::string const& _pass) |
|||
{ |
|||
js::mObject ret; |
|||
|
|||
// KDF info
|
|||
unsigned dklen = 16; |
|||
unsigned iterations = 262144; |
|||
bytes salt = h256::random().asBytes(); |
|||
ret["kdf"] = "pbkdf2"; |
|||
{ |
|||
js::mObject params; |
|||
params["prf"] = "hmac-sha256"; |
|||
params["c"] = (int)iterations; |
|||
params["salt"] = toHex(salt); |
|||
params["dklen"] = (int)dklen; |
|||
ret["kdfparams"] = params; |
|||
} |
|||
bytes derivedKey = pbkdf2(_pass, salt, iterations, dklen); |
|||
|
|||
// cipher info
|
|||
ret["cipher"] = "aes-128-cbc"; |
|||
h128 key(sha3(h128(derivedKey, h128::AlignRight)), h128::AlignRight); |
|||
h128 iv = h128::random(); |
|||
{ |
|||
js::mObject params; |
|||
params["iv"] = toHex(iv.ref()); |
|||
ret["cipherparams"] = params; |
|||
} |
|||
|
|||
// cipher text
|
|||
bytes cipherText = encryptSymNoAuth(key, iv, &_v); |
|||
ret["ciphertext"] = toHex(cipherText); |
|||
|
|||
// and mac.
|
|||
h256 mac = sha3(bytesConstRef(&derivedKey).cropped(derivedKey.size() - 16).toBytes() + cipherText); |
|||
ret["mac"] = toHex(mac.ref()); |
|||
|
|||
return js::write_string((js::mValue)ret, true); |
|||
} |
|||
|
|||
bytes SecretStore::decrypt(std::string const& _v, std::string const& _pass) |
|||
{ |
|||
js::mObject o; |
|||
{ |
|||
js::mValue ov; |
|||
js::read_string(_v, ov); |
|||
o = ov.get_obj(); |
|||
} |
|||
|
|||
// derive key
|
|||
bytes derivedKey; |
|||
if (o["kdf"].get_str() == "pbkdf2") |
|||
{ |
|||
auto params = o["kdfparams"].get_obj(); |
|||
if (params["prf"].get_str() != "hmac-sha256") |
|||
{ |
|||
cwarn << "Unknown PRF for PBKDF2" << params["prf"].get_str() << "not supported."; |
|||
return bytes(); |
|||
} |
|||
unsigned iterations = params["c"].get_int(); |
|||
bytes salt = fromHex(params["salt"].get_str()); |
|||
derivedKey = pbkdf2(_pass, salt, iterations, params["dklen"].get_int()); |
|||
} |
|||
else |
|||
{ |
|||
cwarn << "Unknown KDF" << o["kdf"].get_str() << "not supported."; |
|||
return bytes(); |
|||
} |
|||
|
|||
bytes cipherText = fromHex(o["ciphertext"].get_str()); |
|||
|
|||
// check MAC
|
|||
h256 mac(o["mac"].get_str()); |
|||
h256 macExp = sha3(bytesConstRef(&derivedKey).cropped(derivedKey.size() - 16).toBytes() + cipherText); |
|||
if (mac != macExp) |
|||
{ |
|||
cwarn << "Invalid key - MAC mismatch; expected" << toString(macExp) << ", got" << toString(mac); |
|||
return bytes(); |
|||
} |
|||
|
|||
// decrypt
|
|||
if (o["cipher"].get_str() == "aes-128-cbc") |
|||
{ |
|||
auto params = o["cipherparams"].get_obj(); |
|||
h128 key(sha3(h128(derivedKey, h128::AlignRight)), h128::AlignRight); |
|||
h128 iv(params["iv"].get_str()); |
|||
return decryptSymNoAuth(key, iv, &cipherText); |
|||
} |
|||
else |
|||
{ |
|||
cwarn << "Unknown cipher" << o["cipher"].get_str() << "not supported."; |
|||
return bytes(); |
|||
} |
|||
} |
@ -0,0 +1,56 @@ |
|||
/*
|
|||
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 SecretStore.h
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <functional> |
|||
#include <mutex> |
|||
#include "Common.h" |
|||
#include "FileSystem.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
|
|||
class SecretStore |
|||
{ |
|||
public: |
|||
SecretStore(); |
|||
~SecretStore(); |
|||
|
|||
bytes secret(h128 const& _uuid, std::function<std::string()> const& _pass) const; |
|||
h128 importSecret(bytes const& _s, std::string const& _pass); |
|||
void kill(h128 const& _uuid); |
|||
|
|||
// Clear any cached keys.
|
|||
void clearCache() const; |
|||
|
|||
private: |
|||
void save(std::string const& _keysPath = getDataDir("web3") + "/keys"); |
|||
void load(std::string const& _keysPath = getDataDir("web3") + "/keys"); |
|||
static std::string encrypt(bytes const& _v, std::string const& _pass); |
|||
static bytes decrypt(std::string const& _v, std::string const& _pass); |
|||
|
|||
mutable std::map<h128, bytes> m_cached; |
|||
std::map<h128, std::pair<std::string, std::string>> m_keys; |
|||
}; |
|||
|
|||
} |
|||
|
@ -0,0 +1,203 @@ |
|||
/*
|
|||
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 KeyManager.cpp
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#include "KeyManager.h" |
|||
#include <thread> |
|||
#include <mutex> |
|||
#include <boost/filesystem.hpp> |
|||
#include <libdevcore/Log.h> |
|||
#include <libdevcore/Guards.h> |
|||
#include <libdevcore/RLP.h> |
|||
using namespace std; |
|||
using namespace dev; |
|||
namespace fs = boost::filesystem; |
|||
|
|||
KeyManager::KeyManager(std::string const& _keysFile): |
|||
m_keysFile(_keysFile) |
|||
{} |
|||
|
|||
KeyManager::~KeyManager() |
|||
{} |
|||
|
|||
bool KeyManager::exists() const |
|||
{ |
|||
return !contents(m_keysFile + ".salt").empty() && !contents(m_keysFile).empty(); |
|||
} |
|||
|
|||
void KeyManager::create(std::string const& _pass) |
|||
{ |
|||
m_password = asString(h256::random().asBytes()); |
|||
write(_pass, m_keysFile); |
|||
} |
|||
|
|||
bool KeyManager::load(std::string const& _pass) |
|||
{ |
|||
try { |
|||
bytes salt = contents(m_keysFile + ".salt"); |
|||
bytes encKeys = contents(m_keysFile); |
|||
m_key = h128(pbkdf2(_pass, salt, 262144, 16)); |
|||
bytes bs = decryptSymNoAuth(m_key, h128(), &encKeys); |
|||
RLP s(bs); |
|||
unsigned version = (unsigned)s[0]; |
|||
if (version == 1) |
|||
{ |
|||
for (auto const& i: s[1]) |
|||
m_keyInfo[m_addrLookup[(Address)i[0]] = (h128)i[1]] = KeyInfo((h256)i[2], (std::string)i[3]); |
|||
for (auto const& i: s[2]) |
|||
m_passwordInfo[(h256)i[0]] = (std::string)i[1]; |
|||
m_password = (string)s[3]; |
|||
} |
|||
m_cachedPasswords[hashPassword(m_password)] = m_password; |
|||
return true; |
|||
} |
|||
catch (...) { |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
Secret KeyManager::secret(Address const& _address, function<std::string()> const& _pass) const |
|||
{ |
|||
auto it = m_addrLookup.find(_address); |
|||
if (it == m_addrLookup.end()) |
|||
return Secret(); |
|||
return secret(it->second, _pass); |
|||
} |
|||
|
|||
Secret KeyManager::secret(h128 const& _uuid, function<std::string()> const& _pass) const |
|||
{ |
|||
return Secret(m_store.secret(_uuid, [&](){ |
|||
auto kit = m_keyInfo.find(_uuid); |
|||
if (kit != m_keyInfo.end()) |
|||
{ |
|||
auto it = m_cachedPasswords.find(kit->second.passHash); |
|||
if (it != m_cachedPasswords.end()) |
|||
return it->second; |
|||
} |
|||
std::string p = _pass(); |
|||
m_cachedPasswords[hashPassword(p)] = p; |
|||
return p; |
|||
})); |
|||
} |
|||
|
|||
h128 KeyManager::uuid(Address const& _a) const |
|||
{ |
|||
auto it = m_addrLookup.find(_a); |
|||
if (it == m_addrLookup.end()) |
|||
return h128(); |
|||
return it->second; |
|||
} |
|||
|
|||
Address KeyManager::address(h128 const& _uuid) const |
|||
{ |
|||
for (auto const& i: m_addrLookup) |
|||
if (i.second == _uuid) |
|||
return i.first; |
|||
return Address(); |
|||
} |
|||
|
|||
h128 KeyManager::import(Secret const& _s, string const& _info, std::string const& _pass, string const& _passInfo) |
|||
{ |
|||
Address addr = KeyPair(_s).address(); |
|||
auto passHash = hashPassword(_pass); |
|||
m_cachedPasswords[passHash] = _pass; |
|||
m_passwordInfo[passHash] = _passInfo; |
|||
auto uuid = m_store.importSecret(_s.asBytes(), _pass); |
|||
m_keyInfo[uuid] = KeyInfo{passHash, _info}; |
|||
m_addrLookup[addr] = uuid; |
|||
write(m_keysFile); |
|||
return uuid; |
|||
} |
|||
|
|||
void KeyManager::importExisting(h128 const& _uuid, std::string const& _info, std::string const& _pass, std::string const& _passInfo) |
|||
{ |
|||
bytes key = m_store.secret(_uuid, [&](){ return _pass; }); |
|||
if (key.empty()) |
|||
return; |
|||
Address a = KeyPair(Secret(key)).address(); |
|||
auto passHash = hashPassword(_pass); |
|||
if (!m_passwordInfo.count(passHash)) |
|||
m_passwordInfo[passHash] = _passInfo; |
|||
if (!m_cachedPasswords.count(passHash)) |
|||
m_cachedPasswords[passHash] = _pass; |
|||
m_addrLookup[a] = _uuid; |
|||
m_keyInfo[_uuid].passHash = passHash; |
|||
m_keyInfo[_uuid].info = _info; |
|||
write(m_keysFile); |
|||
} |
|||
|
|||
void KeyManager::kill(Address const& _a) |
|||
{ |
|||
auto id = m_addrLookup[_a]; |
|||
m_addrLookup.erase(_a); |
|||
m_keyInfo.erase(id); |
|||
m_store.kill(id); |
|||
} |
|||
|
|||
std::map<Address, std::pair<std::string, std::string>> KeyManager::keys() const |
|||
{ |
|||
std::map<Address, std::pair<std::string, std::string>> ret; |
|||
for (auto const& i: m_addrLookup) |
|||
if (m_keyInfo.count(i.second) > 0) |
|||
ret[i.first] = make_pair(m_keyInfo.at(i.second).info, m_passwordInfo.at(m_keyInfo.at(i.second).passHash)); |
|||
return ret; |
|||
} |
|||
|
|||
h256 KeyManager::hashPassword(std::string const& _pass) const |
|||
{ |
|||
// TODO SECURITY: store this a bit more securely; Scrypt perhaps?
|
|||
return h256(pbkdf2(_pass, asBytes(m_password), 262144, 32)); |
|||
} |
|||
|
|||
bool KeyManager::write(std::string const& _keysFile) const |
|||
{ |
|||
if (!m_key) |
|||
return false; |
|||
write(m_key, _keysFile); |
|||
return true; |
|||
} |
|||
|
|||
void KeyManager::write(std::string const& _pass, std::string const& _keysFile) const |
|||
{ |
|||
bytes salt = h256::random().asBytes(); |
|||
writeFile(_keysFile + ".salt", salt); |
|||
auto key = h128(pbkdf2(_pass, salt, 262144, 16)); |
|||
write(key, _keysFile); |
|||
} |
|||
|
|||
void KeyManager::write(h128 const& _key, std::string const& _keysFile) const |
|||
{ |
|||
RLPStream s(4); |
|||
s << 1; |
|||
s.appendList(m_addrLookup.size()); |
|||
for (auto const& i: m_addrLookup) |
|||
if (m_keyInfo.count(i.second)) |
|||
{ |
|||
auto ki = m_keyInfo.at(i.second); |
|||
s.appendList(4) << i.first << i.second << ki.passHash << ki.info; |
|||
} |
|||
s.appendList(m_passwordInfo.size()); |
|||
for (auto const& i: m_passwordInfo) |
|||
s.appendList(2) << i.first << i.second; |
|||
s.append(m_password); |
|||
|
|||
writeFile(_keysFile, encryptSymNoAuth(_key, h128(), &s.out())); |
|||
m_key = _key; |
|||
} |
@ -0,0 +1,110 @@ |
|||
/*
|
|||
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 KeyManager.h
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <functional> |
|||
#include <mutex> |
|||
#include <libdevcrypto/SecretStore.h> |
|||
#include <libdevcrypto/FileSystem.h> |
|||
|
|||
namespace dev |
|||
{ |
|||
|
|||
class UnknownPassword: public Exception {}; |
|||
|
|||
struct KeyInfo |
|||
{ |
|||
KeyInfo() = default; |
|||
KeyInfo(h256 const& _passHash, std::string const& _info): passHash(_passHash), info(_info) {} |
|||
h256 passHash; |
|||
std::string info; |
|||
}; |
|||
|
|||
static const auto DontKnowThrow = [](){ throw UnknownPassword(); return std::string(); }; |
|||
|
|||
// TODO: This one is specifically for Ethereum, but we can make it generic in due course.
|
|||
// TODO: hidden-partition style key-store.
|
|||
/**
|
|||
* @brief High-level manager of keys for Ethereum. |
|||
* Usage: |
|||
* |
|||
* Call exists() to check whether there is already a database. If so, get the master password from |
|||
* the user and call load() with it. If not, get a new master password from the user (get them to type |
|||
* it twice and keep some hint around!) and call create() with it. |
|||
*/ |
|||
class KeyManager |
|||
{ |
|||
public: |
|||
KeyManager(std::string const& _keysFile = getDataDir("ethereum") + "/keys.info"); |
|||
~KeyManager(); |
|||
|
|||
void setKeysFile(std::string const& _keysFile) { m_keysFile = _keysFile; } |
|||
std::string const& keysFile() const { return m_keysFile; } |
|||
|
|||
bool exists() const; |
|||
void create(std::string const& _pass); |
|||
bool load(std::string const& _pass); |
|||
void save(std::string const& _pass) const { write(_pass, m_keysFile); } |
|||
|
|||
std::map<Address, std::pair<std::string, std::string>> keys() const; |
|||
|
|||
h128 uuid(Address const& _a) const; |
|||
Address address(h128 const& _uuid) const; |
|||
|
|||
h128 import(Secret const& _s, std::string const& _info, std::string const& _pass, std::string const& _passInfo); |
|||
h128 import(Secret const& _s, std::string const& _info) { return import(_s, _info, m_password, std::string()); } |
|||
|
|||
SecretStore& store() { return m_store; } |
|||
void importExisting(h128 const& _uuid, std::string const& _info, std::string const& _pass, std::string const& _passInfo); |
|||
|
|||
Secret secret(Address const& _address, std::function<std::string()> const& _pass = DontKnowThrow) const; |
|||
Secret secret(h128 const& _uuid, std::function<std::string()> const& _pass = DontKnowThrow) const; |
|||
|
|||
void kill(h128 const& _id) { kill(address(_id)); } |
|||
void kill(Address const& _a); |
|||
|
|||
private: |
|||
h256 hashPassword(std::string const& _pass) const; |
|||
|
|||
// Only use if previously loaded ok.
|
|||
// @returns false if wasn't previously loaded ok.
|
|||
bool write(std::string const& _keysFile) const; |
|||
void write(std::string const& _pass, std::string const& _keysFile) const; |
|||
void write(h128 const& _key, std::string const& _keysFile) const; |
|||
|
|||
// Ethereum keys.
|
|||
std::map<Address, h128> m_addrLookup; |
|||
std::map<h128, KeyInfo> m_keyInfo; |
|||
std::map<h256, std::string> m_passwordInfo; |
|||
|
|||
// Passwords that we're storing.
|
|||
mutable std::map<h256, std::string> m_cachedPasswords; |
|||
|
|||
// The default password for keys in the keystore - protected by the master password.
|
|||
std::string m_password; |
|||
|
|||
SecretStore m_store; |
|||
mutable h128 m_key; |
|||
mutable std::string m_keysFile; |
|||
}; |
|||
|
|||
} |
@ -0,0 +1,30 @@ |
|||
cmake_policy(SET CMP0015 NEW) |
|||
# this policy was introduced in cmake 3.0 |
|||
# remove if, once 3.0 will be used on unix |
|||
if (${CMAKE_MAJOR_VERSION} GREATER 2) |
|||
# old policy do not use MACOSX_RPATH |
|||
cmake_policy(SET CMP0042 OLD) |
|||
endif() |
|||
|
|||
set(CMAKE_AUTOMOC OFF) |
|||
|
|||
aux_source_directory(. SRC_LIST) |
|||
|
|||
include_directories(BEFORE ${V8_INCLUDE_DIRS}) |
|||
include_directories(BEFORE ..) |
|||
include_directories(${READLINE_INCLUDE_DIRS}) |
|||
include_directories(${JSON_RPC_CPP_INCLUDE_DIRS}) |
|||
|
|||
set(EXECUTABLE jsconsole) |
|||
|
|||
file(GLOB HEADERS "*.h") |
|||
|
|||
add_library(${EXECUTABLE} ${SRC_LIST} ${HEADERS}) |
|||
|
|||
target_link_libraries(${EXECUTABLE} jsengine) |
|||
target_link_libraries(${EXECUTABLE} devcore) |
|||
target_link_libraries(${EXECUTABLE} ${READLINE_LIBRARIES}) |
|||
target_link_libraries(${EXECUTABLE} web3jsonrpc) |
|||
|
|||
install( TARGETS ${EXECUTABLE} RUNTIME DESTINATION bin ARCHIVE DESTINATION lib LIBRARY DESTINATION lib ) |
|||
install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} ) |
@ -0,0 +1,86 @@ |
|||
/*
|
|||
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 JSConsole.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#include <iostream> |
|||
#include <libdevcore/Log.h> |
|||
#include <libweb3jsonrpc/WebThreeStubServer.h> |
|||
#include "JSConsole.h" |
|||
#include "JSV8Connector.h" |
|||
#include "libjsconsole/JSConsoleResources.hpp" |
|||
|
|||
// TODO! make readline optional!
|
|||
#include <readline/readline.h> |
|||
#include <readline/history.h> |
|||
|
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
|
|||
JSConsole::JSConsole(WebThreeDirect& _web3, std::vector<dev::KeyPair> const& _accounts): |
|||
m_engine(), |
|||
m_printer(m_engine) |
|||
{ |
|||
m_jsonrpcConnector.reset(new JSV8Connector(m_engine)); |
|||
m_jsonrpcServer.reset(new WebThreeStubServer(*m_jsonrpcConnector.get(), _web3, _accounts)); |
|||
} |
|||
|
|||
JSConsole::~JSConsole() {} |
|||
|
|||
void JSConsole::repl() const |
|||
{ |
|||
string cmd = ""; |
|||
g_logPost = [](std::string const& a, char const*) { cout << "\r \r" << a << endl << flush; rl_forced_update_display(); }; |
|||
|
|||
bool isEmpty = true; |
|||
int openBrackets = 0; |
|||
do { |
|||
char* buff = readline(promptForIndentionLevel(openBrackets).c_str()); |
|||
isEmpty = !(buff && *buff); |
|||
if (!isEmpty) |
|||
{ |
|||
cmd += string(buff); |
|||
cmd += " "; |
|||
free(buff); |
|||
int open = count(cmd.begin(), cmd.end(), '{'); |
|||
open += count(cmd.begin(), cmd.end(), '('); |
|||
int closed = count(cmd.begin(), cmd.end(), '}'); |
|||
closed += count(cmd.begin(), cmd.end(), ')'); |
|||
openBrackets = open - closed; |
|||
} |
|||
} while (openBrackets > 0); |
|||
|
|||
if (!isEmpty) |
|||
{ |
|||
add_history(cmd.c_str()); |
|||
auto value = m_engine.eval(cmd.c_str()); |
|||
string result = m_printer.prettyPrint(value).cstr(); |
|||
cout << result << endl; |
|||
} |
|||
} |
|||
|
|||
std::string JSConsole::promptForIndentionLevel(int _i) const |
|||
{ |
|||
if (_i == 0) |
|||
return "> "; |
|||
|
|||
return string((_i + 1) * 2, ' '); |
|||
} |
@ -0,0 +1,53 @@ |
|||
/*
|
|||
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 JSConsole.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <libjsengine/JSV8Engine.h> |
|||
#include <libjsengine/JSV8Printer.h> |
|||
|
|||
class WebThreeStubServer; |
|||
namespace jsonrpc { class AbstractServerConnector; } |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSConsole |
|||
{ |
|||
public: |
|||
JSConsole(WebThreeDirect& _web3, std::vector<dev::KeyPair> const& _accounts); |
|||
~JSConsole(); |
|||
void repl() const; |
|||
|
|||
private: |
|||
std::string promptForIndentionLevel(int _i) const; |
|||
|
|||
JSV8Engine m_engine; |
|||
JSV8Printer m_printer; |
|||
std::unique_ptr<WebThreeStubServer> m_jsonrpcServer; |
|||
std::unique_ptr<jsonrpc::AbstractServerConnector> m_jsonrpcConnector; |
|||
}; |
|||
|
|||
} |
|||
} |
@ -0,0 +1,54 @@ |
|||
/*
|
|||
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 JSV8Connector.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#include "JSV8Connector.h" |
|||
|
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
|
|||
bool JSV8Connector::StartListening() |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
bool JSV8Connector::StopListening() |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
bool JSV8Connector::SendResponse(std::string const& _response, void* _addInfo) |
|||
{ |
|||
(void)_addInfo; |
|||
m_lastResponse = _response.c_str(); |
|||
return true; |
|||
} |
|||
|
|||
void JSV8Connector::onSend(char const* payload) |
|||
{ |
|||
OnRequest(payload, NULL); |
|||
} |
|||
|
|||
JSV8Connector::~JSV8Connector() |
|||
{ |
|||
StopListening(); |
|||
} |
@ -0,0 +1,50 @@ |
|||
/*
|
|||
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 JSV8Connector.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <jsonrpccpp/server/abstractserverconnector.h> |
|||
#include <libjsengine/JSV8RPC.h> |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSV8Connector: public jsonrpc::AbstractServerConnector, public JSV8RPC |
|||
{ |
|||
|
|||
public: |
|||
JSV8Connector(JSV8Engine const& _engine): JSV8RPC(_engine) {} |
|||
virtual ~JSV8Connector(); |
|||
|
|||
// implement AbstractServerConnector interface
|
|||
bool StartListening(); |
|||
bool StopListening(); |
|||
bool SendResponse(std::string const& _response, void* _addInfo = nullptr); |
|||
|
|||
// implement JSV8RPC interface
|
|||
void onSend(char const* payload); |
|||
}; |
|||
|
|||
} |
|||
} |
@ -0,0 +1,36 @@ |
|||
cmake_policy(SET CMP0015 NEW) |
|||
# this policy was introduced in cmake 3.0 |
|||
# remove if, once 3.0 will be used on unix |
|||
if (${CMAKE_MAJOR_VERSION} GREATER 2) |
|||
# old policy do not use MACOSX_RPATH |
|||
cmake_policy(SET CMP0042 OLD) |
|||
endif() |
|||
|
|||
set(CMAKE_AUTOMOC OFF) |
|||
|
|||
aux_source_directory(. SRC_LIST) |
|||
|
|||
include_directories(BEFORE ${V8_INCLUDE_DIRS}) |
|||
include_directories(BEFORE ..) |
|||
|
|||
set(EXECUTABLE jsengine) |
|||
|
|||
file(GLOB HEADERS "*.h") |
|||
|
|||
include(EthUtils) |
|||
eth_add_resources("${CMAKE_CURRENT_SOURCE_DIR}/JSResources.cmake" "JSRES") |
|||
message(STATUS "HERE!!! ${JSRES}") |
|||
add_library(${EXECUTABLE} ${SRC_LIST} ${HEADERS} ${JSRES}) |
|||
|
|||
# macos brew version of v8 needs to be compiled with libstdc++ |
|||
# it also needs to be dynamic library |
|||
# xcode needs libstdc++ to be explicitly set as it's attribute |
|||
if (APPLE) |
|||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libstdc++") |
|||
set_property(TARGET ${EXECUTABLE} PROPERTY XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libstdc++") |
|||
endif() |
|||
|
|||
target_link_libraries(${EXECUTABLE} ${V8_LIBRARIES}) |
|||
|
|||
install( TARGETS ${EXECUTABLE} RUNTIME DESTINATION bin ARCHIVE DESTINATION lib LIBRARY DESTINATION lib ) |
|||
install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} ) |
@ -0,0 +1,11 @@ |
|||
console = {}; |
|||
console.log = function () { |
|||
}; |
|||
console.warn = function () { |
|||
}; |
|||
console.error = function () { |
|||
}; |
|||
|
|||
setTimeout = function () { |
|||
}; |
|||
|
@ -0,0 +1,36 @@ |
|||
/*
|
|||
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 JSEngine.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#include <string.h> |
|||
#include <stdlib.h> |
|||
#include "JSEngine.h" |
|||
|
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
|
|||
JSString::JSString(char const* _cstr): m_cstr(strdup(_cstr)) {} |
|||
|
|||
JSString::~JSString() |
|||
{ |
|||
if (m_cstr) |
|||
free(m_cstr); |
|||
} |
@ -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 JSEngine.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#pragma once |
|||
#include <exception> |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSException: public std::exception {}; |
|||
class JSPrintException: public JSException { char const* what() const noexcept { return "Cannot print expression!"; } }; |
|||
|
|||
class JSString |
|||
{ |
|||
public: |
|||
JSString(char const* _cstr); |
|||
~JSString(); |
|||
char const* cstr() const { return m_cstr; } |
|||
|
|||
private: |
|||
char* m_cstr; |
|||
}; |
|||
|
|||
class JSValue |
|||
{ |
|||
public: |
|||
virtual JSString toString() const = 0; |
|||
}; |
|||
|
|||
template <typename T> |
|||
class JSEngine |
|||
{ |
|||
public: |
|||
// should be used to evalute javascript expression
|
|||
virtual T eval(char const* _cstr) const = 0; |
|||
}; |
|||
|
|||
} |
|||
} |
@ -0,0 +1,23 @@ |
|||
/*
|
|||
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 JSPrinter.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#include "JSPrinter.h" |
@ -0,0 +1,41 @@ |
|||
/*
|
|||
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 JSPrinter.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include "JSEngine.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
template <typename T> |
|||
class JSPrinter |
|||
{ |
|||
public: |
|||
virtual JSString print(T const& _value) const { return _value.toString(); } |
|||
virtual JSString prettyPrint(T const& _value) const { return print(_value); } |
|||
}; |
|||
|
|||
} |
|||
} |
@ -0,0 +1,8 @@ |
|||
|
|||
set(web3 "${CMAKE_CURRENT_LIST_DIR}/../libjsqrc/ethereumjs/dist/web3.js") |
|||
set(pretty_print "${CMAKE_CURRENT_LIST_DIR}/PrettyPrint.js") |
|||
set(common "${CMAKE_CURRENT_LIST_DIR}/Common.js") |
|||
|
|||
set(ETH_RESOURCE_NAME "JSEngineResources") |
|||
set(ETH_RESOURCE_LOCATION "${CMAKE_CURRENT_BINARY_DIR}") |
|||
set(ETH_RESOURCES "web3" "pretty_print" "common") |
@ -0,0 +1,187 @@ |
|||
/*
|
|||
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 JSV8Engine.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#include <memory> |
|||
#include "JSV8Engine.h" |
|||
#include "libjsengine/JSEngineResources.hpp" |
|||
|
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
static char const* toCString(v8::String::Utf8Value const& _value) |
|||
{ |
|||
if (*_value) |
|||
return *_value; |
|||
throw JSPrintException(); |
|||
} |
|||
|
|||
// from: https://github.com/v8/v8-git-mirror/blob/master/samples/shell.cc
|
|||
// v3.15 from: https://chromium.googlesource.com/v8/v8.git/+/3.14.5.9/samples/shell.cc
|
|||
void reportException(v8::TryCatch* _tryCatch) |
|||
{ |
|||
v8::HandleScope handle_scope; |
|||
v8::String::Utf8Value exception(_tryCatch->Exception()); |
|||
char const* exceptionString = toCString(exception); |
|||
v8::Handle<v8::Message> message = _tryCatch->Message(); |
|||
|
|||
// V8 didn't provide any extra information about this error; just
|
|||
// print the exception.
|
|||
if (message.IsEmpty()) |
|||
printf("%s\n", exceptionString); |
|||
else |
|||
{ |
|||
// Print (filename):(line number): (message).
|
|||
v8::String::Utf8Value filename(message->GetScriptResourceName()); |
|||
char const* filenameString = toCString(filename); |
|||
int linenum = message->GetLineNumber(); |
|||
printf("%s:%i: %s\n", filenameString, linenum, exceptionString); |
|||
|
|||
// Print line of source code.
|
|||
v8::String::Utf8Value sourceline(message->GetSourceLine()); |
|||
char const* sourcelineString = toCString(sourceline); |
|||
printf("%s\n", sourcelineString); |
|||
|
|||
// Print wavy underline (GetUnderline is deprecated).
|
|||
int start = message->GetStartColumn(); |
|||
for (int i = 0; i < start; i++) |
|||
printf(" "); |
|||
|
|||
int end = message->GetEndColumn(); |
|||
|
|||
for (int i = start; i < end; i++) |
|||
printf("^"); |
|||
|
|||
printf("\n"); |
|||
|
|||
v8::String::Utf8Value stackTrace(_tryCatch->StackTrace()); |
|||
if (stackTrace.length() > 0) |
|||
{ |
|||
char const* stackTraceString = toCString(stackTrace); |
|||
printf("%s\n", stackTraceString); |
|||
} |
|||
} |
|||
} |
|||
|
|||
class JSV8Env |
|||
{ |
|||
public: |
|||
~JSV8Env() |
|||
{ |
|||
v8::V8::Dispose(); |
|||
} |
|||
}; |
|||
|
|||
class JSV8Scope |
|||
{ |
|||
public: |
|||
JSV8Scope(): |
|||
m_handleScope(), |
|||
m_context(v8::Context::New(NULL, v8::ObjectTemplate::New())), |
|||
m_contextScope(m_context) |
|||
{ |
|||
m_context->Enter(); |
|||
} |
|||
|
|||
~JSV8Scope() |
|||
{ |
|||
m_context->Exit(); |
|||
m_context.Dispose(); |
|||
} |
|||
|
|||
v8::Persistent <v8::Context> const& context() const { return m_context; } |
|||
|
|||
private: |
|||
v8::HandleScope m_handleScope; |
|||
v8::Persistent <v8::Context> m_context; |
|||
v8::Context::Scope m_contextScope; |
|||
}; |
|||
|
|||
} |
|||
} |
|||
|
|||
JSV8Env JSV8Engine::s_env = JSV8Env(); |
|||
|
|||
JSString JSV8Value::toString() const |
|||
{ |
|||
if (m_value.IsEmpty()) |
|||
return ""; |
|||
|
|||
else if (m_value->IsUndefined()) |
|||
return "undefined"; |
|||
|
|||
v8::String::Utf8Value str(m_value); |
|||
return toCString(str); |
|||
} |
|||
|
|||
JSV8Engine::JSV8Engine(): m_scope(new JSV8Scope()) |
|||
{ |
|||
JSEngineResources resources; |
|||
string common = resources.loadResourceAsString("common"); |
|||
string web3 = resources.loadResourceAsString("web3"); |
|||
eval(common.c_str()); |
|||
eval(web3.c_str()); |
|||
eval("web3 = require('web3');"); |
|||
} |
|||
|
|||
JSV8Engine::~JSV8Engine() |
|||
{ |
|||
delete m_scope; |
|||
} |
|||
|
|||
JSV8Value JSV8Engine::eval(char const* _cstr) const |
|||
{ |
|||
v8::HandleScope handleScope; |
|||
v8::TryCatch tryCatch; |
|||
v8::Local<v8::String> source = v8::String::New(_cstr); |
|||
v8::Local<v8::String> name(v8::String::New("(shell)")); |
|||
v8::ScriptOrigin origin(name); |
|||
v8::Handle<v8::Script> script = v8::Script::Compile(source, &origin); |
|||
|
|||
// Make sure to wrap the exception in a new handle because
|
|||
// the handle returned from the TryCatch is destroyed
|
|||
if (script.IsEmpty()) |
|||
{ |
|||
reportException(&tryCatch); |
|||
return v8::Exception::Error(v8::Local<v8::String>::New(tryCatch.Message()->Get())); |
|||
} |
|||
|
|||
auto result = script->Run(); |
|||
|
|||
if (result.IsEmpty()) |
|||
{ |
|||
reportException(&tryCatch); |
|||
return v8::Exception::Error(v8::Local<v8::String>::New(tryCatch.Message()->Get())); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
v8::Handle<v8::Context> const& JSV8Engine::context() const |
|||
{ |
|||
return m_scope->context(); |
|||
} |
@ -0,0 +1,61 @@ |
|||
/*
|
|||
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 JSV8Engine.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <v8.h> |
|||
#include "JSEngine.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSV8Env; |
|||
class JSV8Scope; |
|||
|
|||
class JSV8Value: public JSValue |
|||
{ |
|||
public: |
|||
JSV8Value(v8::Handle<v8::Value> _value): m_value(_value) {} |
|||
JSString toString() const; |
|||
v8::Handle<v8::Value> const& value() const { return m_value; } |
|||
|
|||
private: |
|||
v8::Handle<v8::Value> m_value; |
|||
}; |
|||
|
|||
class JSV8Engine: public JSEngine<JSV8Value> |
|||
{ |
|||
public: |
|||
JSV8Engine(); |
|||
virtual ~JSV8Engine(); |
|||
JSV8Value eval(char const* _cstr) const; |
|||
v8::Handle<v8::Context> const& context() const; |
|||
|
|||
private: |
|||
static JSV8Env s_env; |
|||
JSV8Scope* m_scope; |
|||
}; |
|||
|
|||
} |
|||
} |
@ -0,0 +1,49 @@ |
|||
/*
|
|||
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 JSV8Printer.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#include <string> |
|||
#include "JSV8Printer.h" |
|||
#include "libjsengine/JSEngineResources.hpp" |
|||
|
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace eth; |
|||
|
|||
JSV8Printer::JSV8Printer(JSV8Engine const& _engine): m_engine(_engine) |
|||
{ |
|||
JSEngineResources resources; |
|||
string prettyPrint = resources.loadResourceAsString("pretty_print"); |
|||
m_engine.eval(prettyPrint.c_str()); |
|||
} |
|||
|
|||
JSString JSV8Printer::prettyPrint(JSV8Value const& _value) const |
|||
{ |
|||
v8::HandleScope handleScope; |
|||
v8::Local<v8::String> pp = v8::String::New("prettyPrint"); |
|||
v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(m_engine.context()->Global()->Get(pp)); |
|||
v8::Local<v8::Value> values[1] = {v8::Local<v8::Value>::New(_value.value())}; |
|||
v8::Local<v8::Value> res = func->Call(func, 1, values); |
|||
v8::String::Utf8Value str(res); |
|||
if (*str) |
|||
return *str; |
|||
throw JSPrintException(); |
|||
} |
@ -0,0 +1,43 @@ |
|||
/*
|
|||
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 JSV8Printer.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include "JSPrinter.h" |
|||
#include "JSV8Engine.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSV8Printer: public JSPrinter<JSV8Value> |
|||
{ |
|||
public: |
|||
JSV8Printer(JSV8Engine const& _engine); |
|||
JSString prettyPrint(JSV8Value const& _value) const; |
|||
private: |
|||
JSV8Engine const& m_engine; |
|||
}; |
|||
|
|||
} |
|||
} |
@ -0,0 +1,84 @@ |
|||
/*
|
|||
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 JSV8RPC.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#include "JSV8RPC.h" |
|||
|
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
v8::Handle<v8::Value> JSV8RPCSend(v8::Arguments const& _args) |
|||
{ |
|||
v8::Local<v8::String> JSON = v8::String::New("JSON"); |
|||
v8::Local<v8::String> parse = v8::String::New("parse"); |
|||
v8::Local<v8::String> stringify = v8::String::New("stringify"); |
|||
v8::Handle<v8::Object> jsonObject = v8::Handle<v8::Object>::Cast(v8::Context::GetCurrent()->Global()->Get(JSON)); |
|||
v8::Handle<v8::Function> parseFunc = v8::Handle<v8::Function>::Cast(jsonObject->Get(parse)); |
|||
v8::Handle<v8::Function> stringifyFunc = v8::Handle<v8::Function>::Cast(jsonObject->Get(stringify)); |
|||
|
|||
v8::Local<v8::Object> self = _args.Holder(); |
|||
v8::Local<v8::External> wrap = v8::Local<v8::External>::Cast(self->GetInternalField(0)); |
|||
JSV8RPC* that = static_cast<JSV8RPC*>(wrap->Value()); |
|||
v8::Local<v8::Value> vals[1] = {_args[0]->ToObject()}; |
|||
v8::Local<v8::Value> stringifiedArg = stringifyFunc->Call(stringifyFunc, 1, vals); |
|||
v8::String::Utf8Value str(stringifiedArg); |
|||
that->onSend(*str); |
|||
|
|||
v8::Local<v8::Value> values[1] = {v8::String::New(that->lastResponse())}; |
|||
return parseFunc->Call(parseFunc, 1, values); |
|||
} |
|||
|
|||
} |
|||
} |
|||
|
|||
JSV8RPC::JSV8RPC(JSV8Engine const& _engine): m_engine(_engine) |
|||
{ |
|||
v8::HandleScope scope; |
|||
v8::Local<v8::ObjectTemplate> rpcTemplate = v8::ObjectTemplate::New(); |
|||
rpcTemplate->SetInternalFieldCount(1); |
|||
rpcTemplate->Set(v8::String::New("send"), |
|||
v8::FunctionTemplate::New(JSV8RPCSend)); |
|||
rpcTemplate->Set(v8::String::New("sendAsync"), |
|||
v8::FunctionTemplate::New(JSV8RPCSend)); |
|||
|
|||
v8::Local<v8::Object> obj = rpcTemplate->NewInstance(); |
|||
obj->SetInternalField(0, v8::External::New(this)); |
|||
|
|||
v8::Local<v8::String> web3 = v8::String::New("web3"); |
|||
v8::Local<v8::String> setProvider = v8::String::New("setProvider"); |
|||
v8::Handle<v8::Object> web3object = v8::Handle<v8::Object>::Cast(m_engine.context()->Global()->Get(web3)); |
|||
v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(web3object->Get(setProvider)); |
|||
v8::Local<v8::Value> values[1] = {obj}; |
|||
func->Call(func, 1, values); |
|||
|
|||
m_lastResponse = R"( |
|||
{ |
|||
"id": 1, |
|||
"jsonrpc": "2.0", |
|||
"error": "Uninitalized JSV8RPC!" |
|||
} |
|||
)"; |
|||
} |
@ -0,0 +1,47 @@ |
|||
/*
|
|||
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 JSV8RPC.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <libjsengine/JSV8Engine.h> |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSV8RPC |
|||
{ |
|||
public: |
|||
JSV8RPC(JSV8Engine const& _engine); |
|||
virtual void onSend(char const* _payload) = 0; |
|||
char const* lastResponse() const { return m_lastResponse; } |
|||
|
|||
private: |
|||
JSV8Engine const& m_engine; |
|||
|
|||
protected: |
|||
char const* m_lastResponse; |
|||
}; |
|||
|
|||
} |
|||
} |
@ -0,0 +1,91 @@ |
|||
var prettyPrint = (function () { |
|||
function pp(object, indent) { |
|||
try { |
|||
JSON.stringify(object) |
|||
} catch(e) { |
|||
return pp(e, indent); |
|||
} |
|||
var str = ""; |
|||
if(object instanceof Array) { |
|||
str += "["; |
|||
for(var i = 0, l = object.length; i < l; i++) { |
|||
str += pp(object[i], indent); |
|||
if(i < l-1) { |
|||
str += ", "; |
|||
} |
|||
} |
|||
str += " ]"; |
|||
} else if (object instanceof Error) { |
|||
str += "\033[31m" + "Error:\033[0m " + object.message; |
|||
} else if (object === null) { |
|||
str += "\033[1m\033[30m" + "null"; |
|||
} else if(typeof(object) === "undefined") { |
|||
str += "\033[1m\033[30m" + object; |
|||
} else if (isBigNumber(object)) { |
|||
str += "\033[32m'" + object.toString(10) + "'"; |
|||
} else if(typeof(object) === "object") { |
|||
str += "{\n"; |
|||
indent += " "; |
|||
var last = getFields(object).pop() |
|||
getFields(object).forEach(function (k) { |
|||
str += indent + k + ": "; |
|||
try { |
|||
str += pp(object[k], indent); |
|||
} catch (e) { |
|||
str += pp(e, indent); |
|||
} |
|||
if(k !== last) { |
|||
str += ","; |
|||
} |
|||
str += "\n"; |
|||
}); |
|||
str += indent.substr(2, indent.length) + "}"; |
|||
} else if(typeof(object) === "string") { |
|||
str += "\033[32m'" + object + "'"; |
|||
} else if(typeof(object) === "number") { |
|||
str += "\033[31m" + object; |
|||
} else if(typeof(object) === "function") { |
|||
str += "\033[35m[Function]"; |
|||
} else { |
|||
str += object; |
|||
} |
|||
str += "\033[0m"; |
|||
return str; |
|||
} |
|||
var redundantFields = [ |
|||
'valueOf', |
|||
'toString', |
|||
'toLocaleString', |
|||
'hasOwnProperty', |
|||
'isPrototypeOf', |
|||
'propertyIsEnumerable', |
|||
'constructor', |
|||
'__defineGetter__', |
|||
'__defineSetter__', |
|||
'__lookupGetter__', |
|||
'__lookupSetter__', |
|||
'__proto__' |
|||
]; |
|||
var getFields = function (object) { |
|||
var result = Object.getOwnPropertyNames(object); |
|||
if (object.constructor && object.constructor.prototype) { |
|||
result = result.concat(Object.getOwnPropertyNames(object.constructor.prototype)); |
|||
} |
|||
return result.filter(function (field) { |
|||
return redundantFields.indexOf(field) === -1; |
|||
}); |
|||
}; |
|||
var isBigNumber = function (object) { |
|||
return (!!object.constructor && object.constructor.name === 'BigNumber') || |
|||
(typeof BigNumber !== 'undefined' && object instanceof BigNumber) |
|||
}; |
|||
function prettyPrintInner(/* */) { |
|||
var args = arguments; |
|||
var ret = ""; |
|||
for(var i = 0, l = args.length; i < l; i++) { |
|||
ret += pp(args[i], "") + "\n"; |
|||
} |
|||
return ret; |
|||
}; |
|||
return prettyPrintInner; |
|||
})(); |
File diff suppressed because it is too large
@ -0,0 +1,5 @@ |
|||
cmake_policy(SET CMP0015 NEW) |
|||
|
|||
aux_source_directory(. SRCS) |
|||
|
|||
add_sources(${SRCS}) |
@ -0,0 +1,71 @@ |
|||
//
|
|||
// Created by Marek Kotewicz on 27/04/15.
|
|||
//
|
|||
|
|||
#include <boost/test/unit_test.hpp> |
|||
#include <libjsengine/JSV8Engine.h> |
|||
#include <libjsengine/JSV8Printer.h> |
|||
|
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
|
|||
BOOST_AUTO_TEST_SUITE(jsv8engine) |
|||
|
|||
BOOST_AUTO_TEST_CASE(evalInteger) |
|||
{ |
|||
JSV8Engine engine; |
|||
JSV8Printer printer(engine); |
|||
auto value = engine.eval("1 + 1"); |
|||
string result = printer.print(value).cstr(); |
|||
BOOST_CHECK_EQUAL(result, "2"); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(evalString) |
|||
{ |
|||
JSV8Engine engine; |
|||
JSV8Printer printer(engine); |
|||
auto value = engine.eval("'hello ' + 'world'"); |
|||
string result = printer.print(value).cstr(); |
|||
BOOST_CHECK_EQUAL(result, "hello world"); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(evalEmpty) |
|||
{ |
|||
JSV8Engine engine; |
|||
JSV8Printer printer(engine); |
|||
auto value = engine.eval(""); |
|||
string result = printer.print(value).cstr(); |
|||
BOOST_CHECK_EQUAL(result, "undefined"); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(evalAssignment) |
|||
{ |
|||
JSV8Engine engine; |
|||
JSV8Printer printer(engine); |
|||
auto value = engine.eval("x = 5"); |
|||
string result = printer.print(value).cstr(); |
|||
BOOST_CHECK_EQUAL(result, "5"); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(evalIncorrectExpression) |
|||
{ |
|||
JSV8Engine engine; |
|||
JSV8Printer printer(engine); |
|||
auto value = engine.eval("["); |
|||
string result = printer.print(value).cstr(); |
|||
BOOST_CHECK_EQUAL(result, "Error: Uncaught SyntaxError: Unexpected end of input"); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(evalNull) |
|||
{ |
|||
JSV8Engine engine; |
|||
JSV8Printer printer(engine); |
|||
auto value = engine.eval("null"); |
|||
string result = printer.print(value).cstr(); |
|||
string prettyResult = printer.prettyPrint(value).cstr(); |
|||
BOOST_CHECK_EQUAL(result, "null"); |
|||
BOOST_CHECK_EQUAL(prettyResult.find("null") != std::string::npos, true); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_SUITE_END() |
Loading…
Reference in new issue