Skip to content

Commit

Permalink
removed libwallet supernodes
Browse files Browse the repository at this point in the history
  • Loading branch information
mbg033 authored and LenyKholodov committed Mar 5, 2019
1 parent 2972f5b commit b1ae340
Show file tree
Hide file tree
Showing 13 changed files with 307 additions and 625 deletions.
154 changes: 23 additions & 131 deletions graftlets/WalletAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "WalletAddress.h"
#include "supernode/requestdefines.h"
#include "rta/supernode.h"
#include "lib/graft/graft_exception.h"
#include "cryptonote_basic/cryptonote_basic_impl.h"
#include "cryptonote_protocol/blobdatatype.h"
Expand All @@ -47,21 +48,13 @@ class WalletAddress: public IGraftlet
public:
WalletAddress(const char* name) : IGraftlet(name) { }

virtual void initOnce(const graft::CommonOpts& opts) override
virtual void initOnce(const graft::CommonOpts& /*opts*/) override
{
makeGetWalletAddressResponse(opts);

REGISTER_ENDPOINT("/dapi/v2.0/cryptonode/getwalletaddress", METHOD_GET | METHOD_POST, WalletAddress, getWalletAddressHandler);
}
private:
graft::Status getWalletAddressHandler(const graft::Router::vars_t& vars, const graft::Input& input, graft::Context& ctx, graft::Output& output);
void makeGetWalletAddressResponse(const graft::CommonOpts& opts);
void checkWalletPublicAddress(const graft::CommonOpts& opts);
void prepareIdKeys(const graft::CommonOpts& opts, crypto::public_key& W, crypto::secret_key& w);
bool verifySignature();

graft::supernode::request::GetWalletAddressResponse m_response;
graft::supernode::request::GetWalletAddressErrorResponse m_errorResponse;
};

GRAFTLET_EXPORTS_BEGIN("walletAddress", GRAFTLET_MKVER(1,1));
Expand All @@ -70,136 +63,35 @@ GRAFTLET_EXPORTS_END

GRAFTLET_PLUGIN_DEFAULT_CHECK_FW_VERSION(GRAFTLET_MKVER(0,3))

graft::Status WalletAddress::getWalletAddressHandler(const graft::Router::vars_t& vars, const graft::Input& input, graft::Context& ctx, graft::Output& output)
graft::Status WalletAddress::getWalletAddressHandler(const graft::Router::vars_t& vars, const graft::Input& input, graft::Context& ctx,
graft::Output& output)
{
LOG_PRINT_L2(__FUNCTION__);
assert(ctx.local.getLastStatus() == graft::Status::None);
if(m_response.wallet_public_address.empty())
{
output.load(m_errorResponse);
return graft::Status::Ok;
}
output.load(m_response);
return graft::Status::Ok;
}

/*!
* \brief makeGetWalletAddressResponse - fills and signs m_response
*/
void WalletAddress::makeGetWalletAddressResponse(const graft::CommonOpts& opts)
{
m_errorResponse.testnet = opts.testnet;

if(opts.wallet_public_address.empty()) return;
checkWalletPublicAddress(opts);

crypto::public_key W;
crypto::secret_key w;
prepareIdKeys(opts, W, w);

m_response.testnet = opts.testnet;
m_response.wallet_public_address = opts.wallet_public_address;
m_response.id_key = epee::string_tools::pod_to_hex(W);

crypto::signature sign;
{//sign
std::string data = m_response.wallet_public_address + ":" + m_response.id_key;
crypto::hash hash;
crypto::cn_fast_hash(data.data(), data.size(), hash);
crypto::generate_signature(hash, W, w, sign);
if (ctx.local.getLastStatus() != graft::Status::None) {
graft::supernode::request::GetWalletAddressErrorResponse err;
err.error = string("internal error: wrong status: " + to_string((int)ctx.local.getLastStatus()));
return graft::Status::Error;
}

m_response.signature = epee::string_tools::pod_to_hex(sign);

assert(verifySignature());
}

/*!
* \brief verifySignature - only for testing here, the code can be used on the other side
*/
bool WalletAddress::verifySignature()
{
crypto::public_key W;
bool ok = epee::string_tools::hex_to_pod(m_response.id_key, W);
assert(ok);

crypto::signature sign;
bool ok1 = epee::string_tools::hex_to_pod(m_response.signature, sign);
assert(ok1);

std::string data = m_response.wallet_public_address + ":" + m_response.id_key;
crypto::hash hash;
crypto::cn_fast_hash(data.data(), data.size(), hash);
return crypto::check_signature(hash, W, sign);
}

/*!
* \brief checkWalletPublicAddress - checks that opts.wallet_public_address is valid
* on error throws graft::exit_error exception
*/
void WalletAddress::checkWalletPublicAddress(const graft::CommonOpts& opts)
{
cryptonote::account_public_address acc = AUTO_VAL_INIT(acc);
if(!cryptonote::get_account_address_from_str(acc, opts.testnet, opts.wallet_public_address))
{
std::ostringstream oss;
oss << "invalid wallet-public-address '" << opts.wallet_public_address << "'";
throw graft::exit_error(oss.str());
graft::SupernodePtr supernode = ctx.global.get(CONTEXT_KEY_SUPERNODE, graft::SupernodePtr());
if (!supernode) {
graft::supernode::request::GetWalletAddressErrorResponse err;
err.error = string("supernode was not setup correctly");
return graft::Status::Error;
}
}

/*!
* \brief prepareIdKeys - gets id keys, generates them if required
* on errors throws graft::exit_error exception
*/
void WalletAddress::prepareIdKeys(const graft::CommonOpts& opts, crypto::public_key& W, crypto::secret_key& w)
{
boost::filesystem::path data_path(opts.data_dir);
graft::supernode::request::GetWalletAddressResponse response;
response.testnet = supernode->testnet();
response.id_key = supernode->idKeyAsString();
response.wallet_public_address = supernode->walletAddress();

boost::filesystem::path wallet_keys_file = data_path / "wallet.keys";
if (!boost::filesystem::exists(wallet_keys_file))
{
LOG_PRINT_L0("file '") << wallet_keys_file << "' not found. Generating the keys";
crypto::generate_keys(W, w);
//save secret key
boost::filesystem::path wallet_keys_file_tmp = wallet_keys_file;
wallet_keys_file_tmp += ".tmp";
std::string w_str = epee::string_tools::pod_to_hex(w);
bool r = epee::file_io_utils::save_string_to_file(wallet_keys_file_tmp.string(), w_str);
if(!r)
{
std::ostringstream oss;
oss << "Cannot write to file '" << wallet_keys_file_tmp << "'";
throw graft::exit_error(oss.str());
}
boost::system::error_code errcode;
boost::filesystem::rename(wallet_keys_file_tmp, wallet_keys_file, errcode);
assert(!errcode);
}
else
{
LOG_PRINT_L1(" Reading wallet keys file '") << wallet_keys_file << "'";
std::string w_str;
bool r = epee::file_io_utils::load_file_to_string(wallet_keys_file.string(), w_str);
if(!r)
{
std::ostringstream oss;
oss << "Cannot read file '" << wallet_keys_file << "'";
throw graft::exit_error(oss.str());
}

bool ok = epee::string_tools::hex_to_pod(w_str, w);
if(ok)
{
ok = crypto::secret_key_to_public_key(w,W);
}
if(!ok)
{
std::ostringstream oss;
oss << "Corrupted data in the file '" << wallet_keys_file << "'";
throw graft::exit_error(oss.str());
}
}
crypto::signature sign;
std::string data = supernode->walletAddress() + ":" + supernode->idKeyAsString();
supernode->signMessage(data, sign);
response.signature = epee::string_tools::pod_to_hex(sign);
output.load(response);
return graft::Status::Ok;
}

namespace
Expand Down
21 changes: 7 additions & 14 deletions include/rta/fullsupernodelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class FullSupernodeList

/*!
* \brief remove - removes Supernode from list. closes it's wallet and frees memory
* \param address - supernode address
* \param id - supernode id
* \return - true if supernode removed
*/
bool remove(const std::string &address);
Expand All @@ -72,25 +72,17 @@ class FullSupernodeList

/*!
* \brief exists - checks if supernode with given address exists in list
* \param address - supernode address
* \param id - supernode id
* \return - true if exists
*/
bool exists(const std::string &address) const;

/*!
* \brief update - updates supernode's key images. this will probably cause stake amount change
* \param address - supernode's address
* \param key_images - list of key images
* \return - true of successfully updated
*/
bool update(const std::string &address, const std::vector<Supernode::SignedKeyImage> &key_images);
bool exists(const std::string &id) const;

/*!
* \brief get - returns supernode instance (pointer)
* \param address - supernode's address
* \param id - supernode's public id
* \return - shared pointer to supernode or empty pointer (nullptr) is no such address
*/
SupernodePtr get(const std::string &address) const;
SupernodePtr get(const std::string &id) const;

typedef std::vector<SupernodePtr> supernode_array;

Expand Down Expand Up @@ -176,11 +168,12 @@ class FullSupernodeList
void refreshStakeTransactionsAndBlockchainBasedList(const char* supernode_network_address, const char* supernode_address);

private:
bool loadWallet(const std::string &wallet_path);
// bool loadWallet(const std::string &wallet_path);
void updateStakeTransactionsImpl();
void selectSupernodes(size_t items_count, const std::string& payment_id, const blockchain_based_list_tier& src_array, supernode_array& dst_array);

private:
// key is public id as a string
std::unordered_map<std::string, SupernodePtr> m_list;
stake_transaction_array m_stake_txs;
std::string m_daemon_address;
Expand Down
Loading

0 comments on commit b1ae340

Please sign in to comment.