diff --git a/src/utils/MoneroUtils.cpp b/src/utils/MoneroUtils.cpp index fb54fdd..95070a1 100644 --- a/src/utils/MoneroUtils.cpp +++ b/src/utils/MoneroUtils.cpp @@ -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"); //} diff --git a/src/utils/MoneroUtils.h b/src/utils/MoneroUtils.h index 93bb121..7df9b72 100644 --- a/src/utils/MoneroUtils.h +++ b/src/utils/MoneroUtils.h @@ -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 string serialize(const vector types) { -// cout << "serialize(types)" << endl; -// throw runtime_error("Not implemented"); -// } -// // void addNode(boost::property_tree::ptree root, const string& key, shared_ptr 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 boost::property_tree::ptree toPropertyTree(const vector 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 string serialize(const vector 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. * diff --git a/src/wallet/MoneroWallet.h b/src/wallet/MoneroWallet.h index 0aa2fc4..6a0907a 100644 --- a/src/wallet/MoneroWallet.h +++ b/src/wallet/MoneroWallet.h @@ -36,19 +36,39 @@ namespace monero { shared_ptr numUnspentOutputs; shared_ptr isUsed; shared_ptr 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 index; - shared_ptr primaryAddress; + shared_ptr index = nullptr; + shared_ptr primaryAddress = nullptr; shared_ptr label; shared_ptr balance; shared_ptr unlockedBalance; shared_ptr tag; vector 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