serializing accounts manually;

testGetAccountsWithoutSubaddresse passing
This commit is contained in:
woodser
2019-06-04 23:44:46 -04:00
parent 5751a1bf52
commit c2de4e7212
3 changed files with 97 additions and 27 deletions
+40 -16
View File
@@ -72,22 +72,46 @@ void MoneroUtils::binaryBlocksToJson(const std::string &bin, std::string &json)
json = ss.str();
}
//string MoneroUtils::serialize(const MoneroAccount& account) {
//
// // build property tree from account
// boost::property_tree::ptree accountNode;
// addNode(accountNode, string("index"), account.index);
//
// // convert root to string
// std::stringstream ss;
// boost::property_tree::write_json(ss, accountNode, false);
// return ss.str();
//}
//
//string MoneroUtils::serialize(const MoneroSubaddress& subaddress) {
// throw runtime_error("serialize(subaddress) not implemented");
//}
//
boost::property_tree::ptree MoneroUtils::toPropertyTree(const MoneroAccount& account) {
cout << "toPropertyTree(account)" << endl;
boost::property_tree::ptree accountNode;
if (account.index != nullptr) accountNode.put("index", *account.index);
if (account.primaryAddress != nullptr) accountNode.put("primaryAddress", *account.primaryAddress);
if (account.balance != nullptr) accountNode.put("balance", *account.balance);
if (account.unlockedBalance != nullptr) accountNode.put("unlockedBalance", *account.unlockedBalance);
if (!account.subaddresses.empty()) {
boost::property_tree::ptree subaddressesNode;
for (const auto& subaddress : account.subaddresses) {
subaddressesNode.push_back(std::make_pair("", toPropertyTree(subaddress)));
}
accountNode.add_child("subaddresses", subaddressesNode);
}
return accountNode;
}
boost::property_tree::ptree MoneroUtils::toPropertyTree(const MoneroSubaddress& subaddress) {
cout << "toPropertyTree(subaddress)" << endl;
throw runtime_error("Not implemented");
}
string MoneroUtils::serialize(const MoneroAccount& account) {
cout << "serialize(account)" << endl;
// build property tree from account
boost::property_tree::ptree accountNode;
//addNode(accountNode, string("index"), account.index);
// convert root to string
std::stringstream ss;
boost::property_tree::write_json(ss, accountNode, false);
return ss.str();
}
string MoneroUtils::serialize(const MoneroSubaddress& subaddress) {
cout << "serialize(subaddress)" << endl;
throw runtime_error("serialize(subaddress) not implemented");
}
//string MoneroUtils::serialize(const MoneroBlock& block) {
// throw runtime_error("serialize(block) not implemented");
//}
+35 -9
View File
@@ -23,20 +23,14 @@ namespace MoneroUtils
void binaryBlocksToJson(const std::string &bin, std::string &json);
// // TODO: template implementation here, could move to MoneroUtils.hpp per https://stackoverflow.com/questions/3040480/c-template-function-compiles-in-header-but-not-implementation
// template <class T> string serialize(const vector<T> types) {
// cout << "serialize(types)" << endl;
// throw runtime_error("Not implemented");
// }
//
// void addNode(boost::property_tree::ptree root, const string& key, shared_ptr<void> ptr) {
// cout << "addNode(...)" << endl;
// throw runtime_error("Not implemented");
// }
//
// string serialize(const MoneroAccount& account);
//
// string serialize(const MoneroSubaddress& subaddress);
string serialize(const MoneroAccount& account);
string serialize(const MoneroSubaddress& subaddress);
//
// string serialize(const MoneroBlock& block);
//
@@ -50,6 +44,38 @@ namespace MoneroUtils
//
// void deserializeOutputWallet(const string& outputStr, MoneroOutputWallet& output);
boost::property_tree::ptree toPropertyTree(const MoneroAccount& account);
boost::property_tree::ptree toPropertyTree(const MoneroSubaddress& subaddress);
template <class T> boost::property_tree::ptree toPropertyTree(const vector<T> types) {
cout << "toPropertyTree(types)" << endl;
boost::property_tree::ptree typeNodes;
for (const auto& type : types) {
typeNodes.push_back(std::make_pair("", toPropertyTree(type)));
}
return typeNodes;
}
// TODO: template implementation here, could move to MoneroUtils.hpp per https://stackoverflow.com/questions/3040480/c-template-function-compiles-in-header-but-not-implementation
template <class T> string serialize(const vector<T> types) {
cout << "serialize(types)" << endl;
boost::property_tree::ptree root;
boost::property_tree::ptree typesNode;
for (const auto& type : types) {
boost::property_tree::ptree typeNode;
typeNode.put("", serialize(type));
typesNode.push_back(std::make_pair("", typeNode));
}
root.add_child("types", typesNode);
// serialize property tree to json
std::stringstream ss;
boost::property_tree::write_json(ss, root, false);
string str = ss.str();
return str;
}
/**
* Modified from core_rpc_server.cpp to return a string.
*
+22 -2
View File
@@ -36,19 +36,39 @@ namespace monero {
shared_ptr<uint32_t> numUnspentOutputs;
shared_ptr<bool> isUsed;
shared_ptr<uint32_t> numBlocksToUnlock;
// BEGIN_KV_SERIALIZE_MAP()
// KV_SERIALIZE_OPT_N(accountIndex, "accountIndex", nullptr)
// KV_SERIALIZE_OPT_N(index, "index", nullptr)
// KV_SERIALIZE_OPT_N(address, "address", nullptr)
// KV_SERIALIZE_OPT_N(label, "label", nullptr)
// KV_SERIALIZE_OPT_N(balance, "balance", nullptr)
// KV_SERIALIZE_OPT_N(unlockedBalance, "unlockedBalance", nullptr)
// KV_SERIALIZE_OPT_N(numUnspentOutputs, "numUnspentOutputs", nullptr)
// KV_SERIALIZE_OPT_N(isUsed, "isUsed", nullptr)
// KV_SERIALIZE_OPT_N(numBlocksToUnlock, "numBlocksToUnlock", nullptr)
// END_KV_SERIALIZE_MAP()
};
/**
* Models a Monero account.
*/
struct MoneroAccount {
shared_ptr<uint32_t> index;
shared_ptr<string> primaryAddress;
shared_ptr<uint32_t> index = nullptr;
shared_ptr<string> primaryAddress = nullptr;
shared_ptr<string> label;
shared_ptr<uint64_t> balance;
shared_ptr<uint64_t> unlockedBalance;
shared_ptr<string> tag;
vector<MoneroSubaddress> subaddresses;
// BEGIN_KV_SERIALIZE_MAP()
// KV_SERIALIZE_OPT_N(index, "index", nullptr)
// KV_SERIALIZE_OPT_N(primaryAddress, "primaryAddress", nullptr)
// KV_SERIALIZE_OPT_N(label, "label", nullptr)
// KV_SERIALIZE_OPT_N(balance, "balance", nullptr)
// KV_SERIALIZE_OPT_N(unlockedBalance, "unlockedBalance", nullptr)
// KV_SERIALIZE_OPT_N(tag, "tag", nullptr)
// KV_SERIALIZE(subaddresses)
// END_KV_SERIALIZE_MAP()
};
// forward declarations