1
0
Fork 0
forked from lthn/blockchain

confidential assets: core & wallet refactoring, various fixes, WIP

This commit is contained in:
sowle 2023-02-09 23:35:33 +01:00
parent f3583c2873
commit 7652a117cd
No known key found for this signature in database
GPG key ID: C07A24B2D89D49FC
11 changed files with 87 additions and 68 deletions

View file

@ -66,7 +66,7 @@ namespace crypto
scalar_t c; // common challenge
};
bool generate_vector_UG_aggregation_proof(const hash& m, const scalar_vec_t& u_secrets, const scalar_vec_t& g_secrets,
inline bool generate_vector_UG_aggregation_proof(const hash& m, const scalar_vec_t& u_secrets, const scalar_vec_t& g_secrets,
const std::vector<crypto::point_t>& amount_commitments,
const std::vector<crypto::point_t>& amount_commitments_for_rp_aggregation,
const std::vector<crypto::point_t>& blinded_asset_ids,
@ -89,7 +89,7 @@ namespace crypto
}
bool verify_vector_UG_aggregation_proof(const hash& m, const std::vector<const public_key*> amount_commitments, const std::vector<const public_key*> blinded_asset_ids,
inline bool verify_vector_UG_aggregation_proof(const hash& m, const std::vector<const public_key*> amount_commitments, const std::vector<const public_key*> blinded_asset_ids,
const vector_UG_aggregation_proof& sig, uint8_t* p_err = nullptr)
{
return false;

View file

@ -3547,7 +3547,7 @@ bool blockchain_storage::unprocess_blockchain_tx_extra(const transaction& tx)
crypto::public_key asset_id = currency::null_pkey;
if (ei.m_asset_operation.operation_type == ASSET_DESCRIPTOR_OPERATION_REGISTER)
{
calculate_asset_id(ei.m_asset_operation.descriptor, nullptr, &asset_id);
calculate_asset_id(ei.m_asset_operation.descriptor.owner, nullptr, &asset_id);
}
else
{
@ -3808,7 +3808,7 @@ bool blockchain_storage::put_asset_info(const transaction & tx, asset_descriptor
if (ado.operation_type == ASSET_DESCRIPTOR_OPERATION_REGISTER)
{
crypto::public_key asset_id{};
calculate_asset_id(ado.descriptor, nullptr, &asset_id);
calculate_asset_id(ado.descriptor.owner, nullptr, &asset_id);
auto asset_history_ptr = m_db_assets.find(asset_id);
CHECK_AND_ASSERT_MES(!asset_history_ptr, false, "Asset id already existing");
assets_container::t_value_type local_asset_history = AUTO_VAL_INIT(local_asset_history);
@ -4948,10 +4948,10 @@ bool blockchain_storage::check_tx_input(const transaction& tx, size_t in_index,
// TODO: consider additional checks here
// build a ring of references
vector<crypto::CLSAG_GG_input_ref_t> ring;
vector<crypto::CLSAG_GGX_input_ref_t> ring;
ring.reserve(scan_contex.zc_outs.size());
for(auto& zc_out : scan_contex.zc_outs)
ring.emplace_back(zc_out.stealth_address, zc_out.amount_commitment);
ring.emplace_back(zc_out.stealth_address, zc_out.amount_commitment, zc_out.blinded_asset_id);
// calculate corresponding tx prefix hash
crypto::hash tx_hash_for_signature = prepare_prefix_hash_for_sign(tx, in_index, tx_prefix_hash);
@ -4961,8 +4961,8 @@ bool blockchain_storage::check_tx_input(const transaction& tx, size_t in_index,
//TIME_MEASURE_START_PD(tx_input_check_clsag_gg);
bool r = crypto::verify_CLSAG_GG(tx_hash_for_signature, ring, sig.pseudo_out_amount_commitment, zc_in.k_image, sig.);
CHECK_AND_ASSERT_MES(r, false, "verify_CLSAG_GG failed");
bool r = crypto::verify_CLSAG_GGX(tx_hash_for_signature, ring, sig.pseudo_out_amount_commitment, sig.pseudo_out_blinded_asset_id, zc_in.k_image, sig.clsags_ggx);
CHECK_AND_ASSERT_MES(r, false, "verify_CLSAG_GGX failed");
//TIME_MEASURE_FINISH_PD(tx_input_check_clsag_gg);
@ -5711,7 +5711,7 @@ bool blockchain_storage::collect_rangeproofs_data_from_tx(std::vector<zc_outs_ra
return true;
}
//@#@ Verify somewhere(maybe here) that all outputs are covered with associated rangeproofs
// TODO @#@# Verify somewhere(maybe here) that all outputs are covered with associated rangeproofs
size_t proofs_count = 0;
size_t current_output_start = 0; //for Consolidated Transactions we'll have multiple zc_outs_range_proof entries
for (const auto& a : tx.attachment)
@ -5720,13 +5720,13 @@ bool blockchain_storage::collect_rangeproofs_data_from_tx(std::vector<zc_outs_ra
{
const zc_outs_range_proof& zcrp = boost::get<zc_outs_range_proof>(a);
agregated_proofs.emplace_back(zcrp);
for (uint8_t i = 0; i != zcrp.outputs_count; i++)
{
CHECK_AND_ASSERT_MES(tx.vout[i + current_output_start].type() == typeid(tx_out_zarcanum), false, "Unexpected type of out in collect_rangeproofs_data_from_tx()");
const tx_out_zarcanum& zc_out = boost::get<tx_out_zarcanum>(tx.vout[i + current_output_start]);
agregated_proofs.back().amount_commitments.emplace_back(zc_out.amount_commitment);
}
current_output_start += zcrp.outputs_count;
// convert amount commitments for aggregation from public_key to point_t form
// TODO: consider refactoring this ugly code
for (uint8_t i = 0; i != zcrp.aggregation_proof.amount_commitments_for_rp_aggregation.size(); i++)
agregated_proofs.back().amount_commitments.emplace_back(zcrp.aggregation_proof.amount_commitments_for_rp_aggregation[i]);
current_output_start += zcrp.aggregation_proof.amount_commitments_for_rp_aggregation.size();
proofs_count++;
}
}

View file

@ -221,6 +221,7 @@ namespace currency
};
// TODO @#@# consider refactoring to eliminate redundant coping and to imporve performance
struct zc_outs_range_proofs_with_commitments
{
zc_outs_range_proofs_with_commitments(const zc_outs_range_proof& range_proof, const std::vector<crypto::point_t>& amount_commitments)

View file

@ -102,7 +102,7 @@ namespace currency
uint64_t amount_to_provide = 0; // amount money that provided by initial creator of tx, used with partially created transactions
uint64_t unlock_time = 0;
destination_option_htlc_out htlc_options; // htlc options
crypto::public_key asset_id = currency::native_coin_asset_id;
crypto::public_key asset_id = currency::native_coin_asset_id; // not blinded, not premultiplied
tx_destination_entry() = default;

View file

@ -226,8 +226,8 @@ simple_wallet::simple_wallet()
m_cmd_binder.set_handler("tor_enable", boost::bind(&simple_wallet::tor_enable, this, ph::_1), "Enable relaying transactions over TOR network(enabled by default)");
m_cmd_binder.set_handler("tor_disable", boost::bind(&simple_wallet::tor_disable, this, ph::_1), "Enable relaying transactions over TOR network(enabled by default)");
m_cmd_binder.set_handler("deploy_new_asset", boost::bind(&simple_wallet::deploy_new_asset, this, ph::_1), "Deploys new asset in the network, with current wallet as a maintainer");
m_cmd_binder.set_handler("add_custom_asset_id", boost::bind(&simple_wallet::add_custom_asset_id, this, _1), "Approve asset id to be recognized in the wallet and returned in balances");
m_cmd_binder.set_handler("remove_custom_asset_id", boost::bind(&simple_wallet::remove_custom_asset_id, this, _1), "Cancel previously made approval for asset id");
m_cmd_binder.set_handler("add_custom_asset_id", boost::bind(&simple_wallet::add_custom_asset_id, this, ph::_1), "Approve asset id to be recognized in the wallet and returned in balances");
m_cmd_binder.set_handler("remove_custom_asset_id", boost::bind(&simple_wallet::remove_custom_asset_id, this, ph::_1), "Cancel previously made approval for asset id");
}
//----------------------------------------------------------------------------------------------------
@ -1238,7 +1238,7 @@ bool simple_wallet::validate_wrap_status(uint64_t amount)
}
}
//----------------------------------------------------------------------------------------------------
bool preprocess_asset_id(std::string& address_arg, crypto::hash& asset_id)
bool preprocess_asset_id(std::string& address_arg, crypto::public_key& asset_id)
{
auto p = address_arg.find(':');
if (p == std::string::npos)
@ -1801,18 +1801,18 @@ bool simple_wallet::deploy_new_asset(const std::vector<std::string> &args)
tx_destination_entry td = AUTO_VAL_INIT(td);
td.addr.push_back(m_wallet->get_account().get_public_address());
td.amount = adb.current_supply;
td.asset_id = currency::ffff_hash;
td.asset_id = currency::ffff_pkey;
std::vector<currency::tx_destination_entry> destinations;
destinations.push_back(td);
currency::transaction result_tx = AUTO_VAL_INIT(result_tx);
crypto::hash result_asset_id = currency::null_hash;
crypto::public_key result_asset_id = currency::null_pkey;
m_wallet->publish_new_asset(adb, destinations, result_tx, result_asset_id);
success_msg_writer(true) << "New asset deployed: " << ENDL
<< "Asset ID: "<< result_asset_id << ENDL
<< "Title: " << adb.full_name << ENDL
<< "Ticker: " << adb.ticker << ENDL
<< "Emitted: " << print_fixed_decimal_point(adb.current_supply, adb.decimal_point) << ENDL
<< "Asset ID: " << result_asset_id << ENDL
<< "Title: " << adb.full_name << ENDL
<< "Ticker: " << adb.ticker << ENDL
<< "Emitted: " << print_fixed_decimal_point(adb.current_supply, adb.decimal_point) << ENDL
<< "Max emission: " << print_fixed_decimal_point(adb.total_max_supply, adb.decimal_point) << ENDL
;
@ -1825,7 +1825,7 @@ bool simple_wallet::add_custom_asset_id(const std::vector<std::string> &args)
{
fail_msg_writer() << "invalid arguments count: " << args.size() << ", expected 1";
}
crypto::hash asset_id = currency::null_hash;
crypto::public_key asset_id = currency::null_pkey;
if (!epee::string_tools::parse_tpod_from_hex_string(args[0], asset_id))
{
fail_msg_writer() << "expected valid asset_id";
@ -1840,11 +1840,11 @@ bool simple_wallet::add_custom_asset_id(const std::vector<std::string> &args)
}
else
{
success_msg_writer() << "Added custom asset:" << ENDL
<< " Id: " << asset_id << ENDL
<< " Title: " << asset_descriptor.full_name << ENDL
<< " Ticker: " << asset_descriptor.ticker << ENDL
<< " Ticker: " << print_fixed_decimal_point(asset_descriptor.current_supply, asset_descriptor.decimal_point) << ENDL
success_msg_writer() << "The following custom asset was successfully added to the wallet:" << ENDL
<< " id: " << asset_id << ENDL
<< " title: " << asset_descriptor.full_name << ENDL
<< " ticker: " << asset_descriptor.ticker << ENDL
<< " supply: " << print_fixed_decimal_point(asset_descriptor.current_supply, asset_descriptor.decimal_point) << ENDL
;
}
return true;
@ -1856,7 +1856,7 @@ bool simple_wallet::remove_custom_asset_id(const std::vector<std::string> &args)
{
fail_msg_writer() << "invalid arguments count: " << args.size() << ", expected 1";
}
crypto::hash asset_id = currency::null_hash;
crypto::public_key asset_id = currency::null_pkey;
if (!epee::string_tools::parse_tpod_from_hex_string(args[0], asset_id))
{
fail_msg_writer() << "expected valid asset_id";

View file

@ -35,7 +35,7 @@ namespace view
{
std::string address;
std::string amount;
crypto::hash asset_id;
crypto::public_key asset_id;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(address)

View file

@ -1432,9 +1432,23 @@ void wallet2::rise_on_transfer2(const wallet_public::wallet_transfer_info& wti)
if (!m_do_rise_transfer)
return;
std::list<wallet_public::asset_balance_entry> balances;
uint64_t mined = 0;
this->balance(balances, mined);
m_wcallback->on_transfer2(wti, balances, mined);
uint64_t mined_balance = 0;
this->balance(balances, mined_balance);
m_wcallback->on_transfer2(wti, balances, mined_balance);
// TODO @#@# bad design, CZ we need to redesign balance() functions regarding getting mined and unlocked balances for the native coin
uint64_t unlocked_balance = 0, native_balance = 0;
for (auto& el : balances)
{
if (el.asset_info.asset_id == currency::native_coin_asset_id)
{
native_balance = el.total;
unlocked_balance = el.unlocked;
break;
}
}
// second call for legacy callback handlers
m_wcallback->on_transfer2(wti, native_balance, unlocked_balance, mined_balance);
}
//----------------------------------------------------------------------------------------------------
void wallet2::handle_money_spent2(const currency::block& b,

View file

@ -119,6 +119,7 @@ namespace tools
virtual ~i_wallet2_callback() = default;
virtual void on_new_block(uint64_t /*height*/, const currency::block& /*block*/) {}
virtual void on_transfer2(const wallet_public::wallet_transfer_info& wti, uint64_t balance, uint64_t unlocked_balance, uint64_t total_mined) {} // this one is left for compatibility, one day we need to get rid of it
virtual void on_transfer2(const wallet_public::wallet_transfer_info& wti, const std::list<wallet_public::asset_balance_entry>& balances, uint64_t total_mined) {}
virtual void on_pos_block_found(const currency::block& /*block*/) {}
virtual void on_sync_progress(const uint64_t& /*percents*/) {}

View file

@ -250,6 +250,7 @@ namespace wallet_public
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(balance)
KV_SERIALIZE(unlocked_balance)
KV_SERIALIZE(balances)
END_KV_SERIALIZE_MAP()
};
};

View file

@ -186,16 +186,16 @@ namespace tools
{
try
{
// res.balance = m_wallet.balance();
// res.unlocked_balance = m_wallet.unlocked_balance();
uint64_t mined = 0;
m_wallet.balance(res.balances, mined);
for (auto it = res.balances.begin(); it != res.balances.end(); it++)
uint64_t stub_mined = 0; // unused
bool r = m_wallet.balance(res.balances, stub_mined);
CHECK_AND_ASSERT_THROW_MES(r, "m_wallet.balance failed");
for (auto it = res.balances.begin(); it != res.balances.end(); ++it)
{
if (it->asset_info.asset_id == currency::null_hash)
if (it->asset_info.asset_id == currency::native_coin_asset_id)
{
res.balance = it->total;
res.unlocked_balance = it->unlocked;
break;
}
}
}

View file

@ -514,6 +514,8 @@ TEST(crypto, basics)
LOG_PRINT_L0("Zano G = " << c_point_G << " = { " << c_point_G.to_hex_comma_separated_bytes_str() << " }");
LOG_PRINT_L0("Zano H = " << c_point_H << " = { " << c_point_H.to_hex_comma_separated_uint64_str() << " }");
LOG_PRINT_L0("Zano H2 = " << c_point_H2 << " = { " << c_point_H2.to_hex_comma_separated_uint64_str() << " }");
return true;
}
@ -628,32 +630,32 @@ TEST(crypto, pos)
boost::multiprecision::uint512_t Lv = boost::multiprecision::uint512_t(c_L_w) * amount;
constexpr uint64_t COIN = 1000000000000;
constexpr uint64_t c_coin = 1000000000000;
const uint64_t amounts[] = {
COIN / 100,
COIN / 50,
COIN / 20,
COIN / 10,
COIN / 5,
COIN / 2,
COIN * 1,
COIN * 2,
COIN * 5,
COIN * 10,
COIN * 20,
COIN * 50,
COIN * 100,
COIN * 200,
COIN * 500,
COIN * 1000,
COIN * 2000,
COIN * 5000,
COIN * 10000,
COIN * 20000,
COIN * 50000,
COIN * 100000,
COIN * 200000,
COIN * 500000
c_coin / 100,
c_coin / 50,
c_coin / 20,
c_coin / 10,
c_coin / 5,
c_coin / 2,
c_coin * 1,
c_coin * 2,
c_coin * 5,
c_coin * 10,
c_coin * 20,
c_coin * 50,
c_coin * 100,
c_coin * 200,
c_coin * 500,
c_coin * 1000,
c_coin * 2000,
c_coin * 5000,
c_coin * 10000,
c_coin * 20000,
c_coin * 50000,
c_coin * 100000,
c_coin * 200000,
c_coin * 500000
};
uint64_t kernel = 0;