From 1fd6e08e5cd9081a2a3becd17b154310569bbff4 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Wed, 28 Sep 2022 22:06:07 +0200 Subject: [PATCH] initial code for introducing asset descriptor for wallet --- src/crypto/crypto-sugar.h | 3 +-- src/currency_core/currency_format_utils.cpp | 4 ++-- src/currency_core/currency_format_utils.h | 2 +- .../currency_format_utils_abstract.h | 20 +++++++++++++++++++ src/wallet/wallet2.cpp | 7 ++++--- src/wallet/wallet2.h | 4 ++-- 6 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/crypto/crypto-sugar.h b/src/crypto/crypto-sugar.h index 3d2e0457..572e09d5 100644 --- a/src/crypto/crypto-sugar.h +++ b/src/crypto/crypto-sugar.h @@ -1200,7 +1200,6 @@ namespace crypto { // hs won't touch memory if size is 0, so it's safe return hash_helper_t::hs(data(), sizeof(scalar_t) * size()); - } - + } } // namespace crypto diff --git a/src/currency_core/currency_format_utils.cpp b/src/currency_core/currency_format_utils.cpp index d6b870e1..d0e5c424 100644 --- a/src/currency_core/currency_format_utils.cpp +++ b/src/currency_core/currency_format_utils.cpp @@ -807,12 +807,12 @@ namespace currency return derive_public_key_from_target_address(destination_addr, tx_sec_key, index, out_eph_public_key, derivation); } //--------------------------------------------------------------- - bool derive_key_pair_from_key_pair(const crypto::public_key& src_pub_key, const crypto::secret_key& src_sec_key, crypto::public_key& derived_sec_key, crypto::public_key& derived_pub_key, const char(&hs_domain)[32], uint64_t index) + bool derive_key_pair_from_key_pair(const crypto::public_key& src_pub_key, const crypto::secret_key& src_sec_key, crypto::secret_key& derived_sec_key, crypto::public_key& derived_pub_key, const char(&hs_domain)[32], uint64_t index) { crypto::key_derivation derivation = AUTO_VAL_INIT(derivation); bool r = crypto::generate_key_derivation(src_pub_key, src_sec_key, derivation); CHECK_AND_ASSERT_MES(r, false, "at creation outs: failed to generate_key_derivation(" << src_pub_key << ", " << src_sec_key << ")"); - scalar_t sec_key = crypto::hash_helper_t::hs(hs_domain, derivation, index); + crypto::scalar_t sec_key = crypto::hash_helper_t::hs(hs_domain, derivation, index); derived_sec_key = sec_key.as_secret_key(); derived_pub_key = (sec_key * crypto::c_point_G).to_public_key(); return true; diff --git a/src/currency_core/currency_format_utils.h b/src/currency_core/currency_format_utils.h index 51cf782a..ac5e1ef7 100644 --- a/src/currency_core/currency_format_utils.h +++ b/src/currency_core/currency_format_utils.h @@ -315,7 +315,7 @@ namespace currency bool generate_key_image_helper(const account_keys& ack, const crypto::public_key& tx_public_key, size_t real_output_index, keypair& in_ephemeral, crypto::key_image& ki); bool derive_public_key_from_target_address(const account_public_address& destination_addr, const crypto::secret_key& tx_sec_key, size_t index, crypto::public_key& out_eph_public_key, crypto::key_derivation& derivation); bool derive_public_key_from_target_address(const account_public_address& destination_addr, const crypto::secret_key& tx_sec_key, size_t index, crypto::public_key& out_eph_public_key); - bool derive_key_pair_from_key_pair(const crypto::public_key& src_pub_key, const crypto::secret_key& src_sec_key, crypto::public_key& derived_sec_key, crypto::public_key& derived_pub_key, const char(&hs_domain)[32], uint64_t index = 0); + bool derive_key_pair_from_key_pair(const crypto::public_key& src_pub_key, const crypto::secret_key& src_sec_key, crypto::secret_key& derived_sec_key, crypto::public_key& derived_pub_key, const char(&hs_domain)[32], uint64_t index = 0); std::string short_hash_str(const crypto::hash& h); bool is_mixattr_applicable_for_fake_outs_counter(uint8_t mix_attr, uint64_t fake_attr_count); diff --git a/src/currency_core/currency_format_utils_abstract.h b/src/currency_core/currency_format_utils_abstract.h index addfd80a..b26e906f 100644 --- a/src/currency_core/currency_format_utils_abstract.h +++ b/src/currency_core/currency_format_utils_abstract.h @@ -353,6 +353,26 @@ namespace currency size_t get_object_blobsize(const transaction& t, uint64_t prefix_blob_size); + inline + void put_t_to_buff(std::string& buff) + {} + + template + void put_t_to_buff(std::string& buff, const T& var1, Types&... var2) + { + static_assert(std::is_pod::value, "T must be a POD type."); + buff.append((const char*)&var1, sizeof(var1)); + put_t_to_buff(buff, var2...); + } + + template + crypto::hash get_hash_from_POD_objects(Types&... var1) + { + std::string buff; + put_t_to_buff(buff, var1...); + return crypto::cn_fast_hash(buff.data(), buff.size()); + } + #define CHECKED_GET_SPECIFIC_VARIANT(variant_var, specific_type, variable_name, fail_return_val) \ CHECK_AND_ASSERT_MES(variant_var.type() == typeid(specific_type), fail_return_val, "wrong variant type: " << variant_var.type().name() << ", expected " << typeid(specific_type).name()); \ diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 51638b1e..9462b9dc 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -776,7 +776,8 @@ void wallet2::process_new_transaction(const currency::transaction& tx, uint64_t } else { - wallet_own_asset_context& asset_context = m_own_asset_descriptors[hash_helper_t::hs(CRYPTO_HDS_ASSET_ID, ado.descriptor.owner), 0]; + + wallet_own_asset_context& asset_context = m_own_asset_descriptors[get_hash_from_POD_objects(CRYPTO_HDS_ASSET_ID, ado.descriptor.owner)]; asset_context.asset_descriptor = ado.descriptor; std::stringstream ss; ss << "New Asset Registered:" @@ -786,7 +787,7 @@ void wallet2::process_new_transaction(const currency::transaction& tx, uint64_t << ENDL << "Current Supply: " << print_asset_money(asset_context.asset_descriptor.current_supply, asset_context.asset_descriptor.decimal_point) << ENDL << "DecimalPoint: " << asset_context.asset_descriptor.decimal_point; - WLT_LOG_MAGENTA(ss.str() << , LOG_LEVEL_0); + WLT_LOG_MAGENTA(ss.str(), LOG_LEVEL_0); if (m_wcallback) m_wcallback->on_message(i_wallet2_callback::ms_yellow, ss.str()); } @@ -4165,7 +4166,7 @@ void wallet2::request_alias_registration(currency::extra_alias_entry& ai, curren transfer(destinations, 0, 0, fee, extra, attachments, get_current_split_strategy(), tx_dust_policy(DEFAULT_DUST_THRESHOLD), res_tx, CURRENCY_TO_KEY_OUT_RELAXED, false); } //---------------------------------------------------------------------------------------------------- -void wallet2::publish_new_asset(const asset_descriptor_base& asset_info/*, const std::vector& destinations*/, currency::transaction& result_tx) +void wallet2::publish_new_asset(const currency::asset_descriptor_base& asset_info, const std::vector& destinations, currency::transaction& result_tx) { asset_descriptor_operation asset_reg_info = AUTO_VAL_INIT(asset_reg_info); asset_reg_info.descriptor = asset_info; diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 130b8ca3..27acfc69 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -508,7 +508,7 @@ namespace tools struct wallet_own_asset_context { - asset_descriptor_base asset_descriptor; + currency::asset_descriptor_base asset_descriptor; crypto::secret_key control_key; BEGIN_BOOST_SERIALIZATION() @@ -563,7 +563,7 @@ namespace tools void request_alias_update(currency::extra_alias_entry& ai, currency::transaction& res_tx, uint64_t fee, uint64_t reward); bool check_available_sources(std::list& amounts); - void publish_new_asset(const asset_descriptor_base& asset_info/*, const std::vector& destinations*/, currency::transaction& result_tx); + void publish_new_asset(const currency::asset_descriptor_base& asset_info, const std::vector& destinations, currency::transaction& result_tx); bool set_core_proxy(const std::shared_ptr& proxy); void set_pos_mint_packing_size(uint64_t new_size);