From a493ace766c314fee17393c1fa095c34f7d37152 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Tue, 15 Nov 2022 21:54:55 +0100 Subject: [PATCH] added api for assets management in wallet(whitelist and custom set) --- src/currency_core/currency_basic.h | 14 +++++++++++++- src/rpc/core_rpc_server.cpp | 11 +++++++++++ src/rpc/core_rpc_server.h | 5 ++++- src/rpc/core_rpc_server_commands_defs.h | 23 +++++++++++++++++++++++ src/wallet/core_default_rpc_proxy.cpp | 5 +++++ src/wallet/core_default_rpc_proxy.h | 1 + src/wallet/core_fast_rpc_proxy.h | 5 +++++ src/wallet/wallet2.cpp | 25 +++++++++++++++++++++++++ src/wallet/wallet2.h | 6 ++++++ 9 files changed, 93 insertions(+), 2 deletions(-) diff --git a/src/currency_core/currency_basic.h b/src/currency_core/currency_basic.h index 9ab8a12f..abfc69ae 100644 --- a/src/currency_core/currency_basic.h +++ b/src/currency_core/currency_basic.h @@ -727,7 +727,6 @@ namespace currency }; - struct asset_descriptor_base { uint64_t total_max_supply = 0; @@ -735,6 +734,7 @@ namespace currency uint8_t decimal_point = 12; std::string ticker; std::string full_name; + std::string meta_info; crypto::public_key owner = currency::null_pkey; BEGIN_VERSIONED_SERIALIZE() @@ -743,6 +743,7 @@ namespace currency FIELD(decimal_point) FIELD(ticker) FIELD(full_name) + FIELD(meta_info) FIELD(owner) END_SERIALIZE() @@ -753,8 +754,19 @@ namespace currency BOOST_SERIALIZE(decimal_point) BOOST_SERIALIZE(ticker) BOOST_SERIALIZE(full_name) + BOOST_SERIALIZE(meta_info) BOOST_SERIALIZE(owner) END_BOOST_SERIALIZATION() + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(total_max_supply) + KV_SERIALIZE(current_supply) + KV_SERIALIZE(decimal_point) + KV_SERIALIZE(ticker) + KV_SERIALIZE(full_name) + KV_SERIALIZE(meta_info) + KV_SERIALIZE_POD_AS_HEX_STRING(owner) + END_KV_SERIALIZE_MAP() }; diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp index 948472c0..19a73be6 100644 --- a/src/rpc/core_rpc_server.cpp +++ b/src/rpc/core_rpc_server.cpp @@ -627,6 +627,17 @@ namespace currency return true; } //------------------------------------------------------------------------------------------------------------------------------ + bool core_rpc_server::on_get_asset_info(const COMMAND_RPC_GET_ASSET_INFO::request& req, COMMAND_RPC_GET_ASSET_INFO::response& res, connection_context& cntx) + { + if (!m_core.get_blockchain_storage().get_asset_info(req.asset_id, res.asset_descriptor)) + { + res.status = API_RETURN_CODE_NOT_FOUND; + return true; + } + res.status = API_RETURN_CODE_OK; + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ bool core_rpc_server::on_get_main_block_details(const COMMAND_RPC_GET_BLOCK_DETAILS::request& req, COMMAND_RPC_GET_BLOCK_DETAILS::response& res, epee::json_rpc::error& error_resp, connection_context& cntx) { if (!m_core.get_blockchain_storage().get_main_block_rpc_details(req.id, res.block_details)) diff --git a/src/rpc/core_rpc_server.h b/src/rpc/core_rpc_server.h index d27ceada..4e52d22d 100644 --- a/src/rpc/core_rpc_server.h +++ b/src/rpc/core_rpc_server.h @@ -82,7 +82,8 @@ namespace currency bool on_get_pool_txs_brief_details(const COMMAND_RPC_GET_POOL_TXS_BRIEF_DETAILS::request& req, COMMAND_RPC_GET_POOL_TXS_BRIEF_DETAILS::response& res, connection_context& cntx); bool on_get_all_pool_tx_list(const COMMAND_RPC_GET_ALL_POOL_TX_LIST::request& req, COMMAND_RPC_GET_ALL_POOL_TX_LIST::response& res, connection_context& cntx); bool on_get_pool_info(const COMMAND_RPC_GET_POOL_INFO::request& req, COMMAND_RPC_GET_POOL_INFO::response& res, connection_context& cntx); - + bool on_get_asset_info(const COMMAND_RPC_GET_ASSET_INFO::request& req, COMMAND_RPC_GET_ASSET_INFO::response& res, connection_context& cntx); + bool on_get_main_block_details(const COMMAND_RPC_GET_BLOCK_DETAILS::request& req, COMMAND_RPC_GET_BLOCK_DETAILS::response& res, epee::json_rpc::error& error_resp, connection_context& cntx); bool on_get_alt_block_details(const COMMAND_RPC_GET_BLOCK_DETAILS::request& req, COMMAND_RPC_GET_BLOCK_DETAILS::response& res, epee::json_rpc::error& error_resp, connection_context& cntx); bool on_get_alt_blocks_details(const COMMAND_RPC_GET_ALT_BLOCKS_DETAILS::request& req, COMMAND_RPC_GET_ALT_BLOCKS_DETAILS::response& res, connection_context& cntx); @@ -137,6 +138,8 @@ namespace currency MAP_JON_RPC ("get_pool_txs_brief_details", on_get_pool_txs_brief_details, COMMAND_RPC_GET_POOL_TXS_BRIEF_DETAILS) MAP_JON_RPC ("get_all_pool_tx_list", on_get_all_pool_tx_list, COMMAND_RPC_GET_ALL_POOL_TX_LIST) MAP_JON_RPC ("get_pool_info", on_get_pool_info, COMMAND_RPC_GET_POOL_INFO) + //assets api + MAP_JON_RPC ("get_asset_info", on_get_asset_info, COMMAND_RPC_GET_ASSET_INFO) MAP_JON_RPC_WE("get_main_block_details", on_get_main_block_details, COMMAND_RPC_GET_BLOCK_DETAILS) MAP_JON_RPC_WE("get_alt_block_details", on_get_alt_block_details, COMMAND_RPC_GET_BLOCK_DETAILS) diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h index 4b737eba..8f2ae19e 100644 --- a/src/rpc/core_rpc_server_commands_defs.h +++ b/src/rpc/core_rpc_server_commands_defs.h @@ -82,6 +82,29 @@ namespace currency }; }; + struct COMMAND_RPC_GET_ASSET_INFO + { + struct request + { + crypto::hash asset_id; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE_POD_AS_HEX_STRING(asset_id) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + std::string status; + asset_descriptor_base asset_descriptor; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(status) + KV_SERIALIZE(asset_descriptor) + END_KV_SERIALIZE_MAP() + }; + }; + struct COMMAND_RPC_GET_HEIGHT { struct request diff --git a/src/wallet/core_default_rpc_proxy.cpp b/src/wallet/core_default_rpc_proxy.cpp index 7e4a4ed6..3643313e 100644 --- a/src/wallet/core_default_rpc_proxy.cpp +++ b/src/wallet/core_default_rpc_proxy.cpp @@ -168,6 +168,11 @@ namespace tools return invoke_http_json_rpc_update_is_disconnect("get_pool_info", req, res); } //------------------------------------------------------------------------------------------------------------------------------ + bool default_http_core_proxy::call_COMMAND_RPC_GET_POOL_INFO(const currency::COMMAND_RPC_GET_ASSET_INFO::request& req, currency::COMMAND_RPC_GET_ASSET_INFO::response& res) + { + return invoke_http_json_rpc_update_is_disconnect("get_asset_info", req, res); + } + //------------------------------------------------------------------------------------------------------------------------------ bool default_http_core_proxy::get_transfer_address(const std::string& adr_str, currency::account_public_address& addr, std::string& payment_id) { return tools::get_transfer_address(adr_str, addr, payment_id, this); diff --git a/src/wallet/core_default_rpc_proxy.h b/src/wallet/core_default_rpc_proxy.h index 89234151..70ec4ea4 100644 --- a/src/wallet/core_default_rpc_proxy.h +++ b/src/wallet/core_default_rpc_proxy.h @@ -51,6 +51,7 @@ namespace tools bool call_COMMAND_RPC_GET_BLOCKS_DETAILS(const currency::COMMAND_RPC_GET_BLOCKS_DETAILS::request& req, currency::COMMAND_RPC_GET_BLOCKS_DETAILS::response& res) override; bool call_COMMAND_RPC_GET_CURRENT_CORE_TX_EXPIRATION_MEDIAN(const currency::COMMAND_RPC_GET_CURRENT_CORE_TX_EXPIRATION_MEDIAN::request& req, currency::COMMAND_RPC_GET_CURRENT_CORE_TX_EXPIRATION_MEDIAN::response& res) override; bool call_COMMAND_RPC_GET_POOL_INFO(const currency::COMMAND_RPC_GET_POOL_INFO::request& req, currency::COMMAND_RPC_GET_POOL_INFO::response& res) override; + bool call_COMMAND_RPC_GET_POOL_INFO(const currency::COMMAND_RPC_GET_ASSET_INFO::request& req, currency::COMMAND_RPC_GET_ASSET_INFO::response& res) override; bool check_connection() override; bool get_transfer_address(const std::string& adr_str, currency::account_public_address& addr, std::string& payment_id) override; diff --git a/src/wallet/core_fast_rpc_proxy.h b/src/wallet/core_fast_rpc_proxy.h index 7e3478c9..3e3f9b40 100644 --- a/src/wallet/core_fast_rpc_proxy.h +++ b/src/wallet/core_fast_rpc_proxy.h @@ -133,6 +133,11 @@ namespace tools return m_rpc.on_get_pool_info(req, res, m_cntxt_stub); } //------------------------------------------------------------------------------------------------------------------------------ + virtual bool call_COMMAND_RPC_GET_ASSET_INFO(const currency::COMMAND_RPC_GET_ASSET_INFO::request& req, currency::COMMAND_RPC_GET_ASSET_INFO::response& res)override + { + return m_rpc.on_get_asset_info(req, res, m_cntxt_stub); + } + //------------------------------------------------------------------------------------------------------------------------------ virtual bool get_transfer_address(const std::string& adr_str, currency::account_public_address& addr, std::string& payment_id) override { return tools::get_transfer_address(adr_str, addr, payment_id, this); diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index f51c6d7e..f727197e 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -3131,6 +3131,31 @@ uint64_t wallet2::balance() const return balance(stub, stub, stub, stub); } //---------------------------------------------------------------------------------------------------- +bool wallet2::add_custom_asset_id(const crypto::hash& asset_id) +{ + currency::COMMAND_RPC_GET_ASSET_INFO::request req = AUTO_VAL_INIT(req); + currency::COMMAND_RPC_GET_ASSET_INFO::response resp = AUTO_VAL_INIT(resp); + + bool r = m_core_proxy->call_COMMAND_RPC_GET_ASSET_INFO(req, resp); + if (resp.status == API_RETURN_CODE_OK) + { + m_custom_assets[asset_id] = resp.asset_descriptor; + return true; + } + return false; +} +//---------------------------------------------------------------------------------------------------- +bool wallet2::delete_custom_asset_id(const crypto::hash& asset_id) +{ + auto it = m_custom_assets.find(asset_id); + if (it != m_custom_assets.end()) + { + m_custom_assets.erase(it); + } + + return true; +} +//---------------------------------------------------------------------------------------------------- void wallet2::get_transfers(wallet2::transfer_container& incoming_transfers) const { incoming_transfers = m_transfers; diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 6d1d470a..c86f5b0e 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -813,6 +813,7 @@ namespace tools return; a & m_own_asset_descriptors; + a & m_custom_assets; } void wipeout_extra_if_needed(std::vector& transfer_history); @@ -894,6 +895,8 @@ namespace tools void set_disable_tor_relay(bool disable); uint64_t get_default_fee() {return TX_DEFAULT_FEE;} void export_transaction_history(std::ostream& ss, const std::string& format, bool include_pos_transactions = true); + + bool add_custom_asset_id(const crypto::hash& asset_id); /* create_htlc_proposal: if htlc_hash == null_hash, then this wallet is originator of the atomic process, and @@ -1091,6 +1094,9 @@ private: std::unordered_set m_unconfirmed_multisig_transfers; std::unordered_map m_tx_keys; std::unordered_map m_own_asset_descriptors; + std::unordered_map m_custom_assets; //assets that manually added by user + std::unordered_map m_whitelisted_assets; //assets that manually added by user + std::multimap m_htlcs; //map [expired_if_more_then] -> height of expiration amount_gindex_to_transfer_id_container m_active_htlcs; // map [amount; gindex] -> transfer index