aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/blockchain_db/asset_db.cpp92
-rw-r--r--src/blockchain_db/asset_db.h25
-rw-r--r--src/blockchain_db/blockchain_db.cpp5
-rw-r--r--src/blockchain_db/blockchain_db.h14
-rw-r--r--src/blockchain_db/lmdb/db_lmdb.cpp156
-rw-r--r--src/blockchain_db/lmdb/db_lmdb.h11
-rw-r--r--src/blockchain_db/testdb.h6
-rw-r--r--src/cryptonote_basic/CMakeLists.txt1
-rw-r--r--src/cryptonote_basic/asset_wire.cpp355
-rw-r--r--src/cryptonote_basic/asset_wire.h68
10 files changed, 731 insertions, 2 deletions
diff --git a/src/blockchain_db/asset_db.cpp b/src/blockchain_db/asset_db.cpp
index d80b61028..800ea78fb 100644
--- a/src/blockchain_db/asset_db.cpp
+++ b/src/blockchain_db/asset_db.cpp
@@ -3,6 +3,8 @@
#include <algorithm>
#include <cstring>
+#include "ringct/rctOps.h"
+
namespace cryptonote
{
namespace assets
@@ -92,5 +94,95 @@ bool apply_block_extensions_to_db(BlockchainDB& db,
asset_ids = std::move(candidate_ids);
return true;
}
+
+bool verify_asset_ownership_against_db(const BlockchainDB& db,
+ const asset_ownership_proof& proof, network_type expected_network,
+ const crypto::hash& carrier_prefix_hash, std::string* error)
+{
+ if (db.has_asset_key_image(proof.key_image))
+ return fail(error, "asset key image is already spent");
+
+ for (const asset_ring_member& member : proof.ring)
+ {
+ asset_output_data_t stored{};
+ if (!db.get_asset_output(member.output_id, stored))
+ return fail(error, "asset ownership ring references an unknown output");
+ if (stored.asset_id != member.asset_id || member.asset_id != proof.asset_id)
+ return fail(error, "asset ownership ring member has the wrong asset id");
+ if (!rct::equalKeys(stored.destination, member.public_output.dest))
+ return fail(error, "asset ownership ring destination does not match consensus state");
+ if (!rct::equalKeys(stored.commitment, member.public_output.mask))
+ return fail(error, "asset ownership ring commitment does not match consensus state");
+ }
+ return verify_asset_ownership_proof(
+ proof, expected_network, carrier_prefix_hash, error);
+}
+
+bool apply_asset_transaction_to_db(BlockchainDB& db,
+ const asset_transaction_payload& payload, network_type expected_network,
+ const crypto::hash& expected_carrier_prefix_hash, uint64_t height,
+ std::vector<crypto::hash>& output_ids, std::string* error)
+{
+ asset_registry registry;
+ if (!load_registry_from_db(db, expected_network, registry, error)
+ || !verify_asset_transaction_payload(payload, registry.known_assets(),
+ expected_network, expected_carrier_prefix_hash, error))
+ return false;
+ for (const asset_ownership_proof& proof : payload.ownership_proofs)
+ if (!verify_asset_ownership_against_db(db, proof, expected_network,
+ expected_carrier_prefix_hash, error))
+ return false;
+
+ crypto::hash issued_id{};
+ std::vector<uint8_t> encoded_issuance;
+ if (payload.issuance)
+ {
+ if (!registry.apply_issuance(*payload.issuance, height, issued_id, error)
+ || !encode_issuance_payload(*payload.issuance, encoded_issuance, error))
+ return false;
+ }
+
+ std::vector<crypto::hash> candidate_ids;
+ uint32_t global_output_index = 0;
+ for (size_t group = 0; group < payload.balances.size(); ++group)
+ {
+ const confidential_asset_balance& balance = payload.balances[group];
+ for (size_t index = 0; index < balance.outputs.size(); ++index)
+ {
+ confidential_asset_output output{
+ payload.output_destinations[group][index], balance.outputs[index]};
+ crypto::hash output_id{};
+ if (!derive_asset_output_id(expected_network, expected_carrier_prefix_hash,
+ balance.asset_id, global_output_index++, output, output_id, error))
+ return false;
+ asset_output_data_t existing{};
+ if (db.get_asset_output(output_id, existing))
+ return fail(error, "asset output identity already exists");
+ candidate_ids.push_back(output_id);
+ }
+ }
+
+ if (payload.issuance)
+ {
+ const blobdata_ref bytes{reinterpret_cast<const char*>(encoded_issuance.data()),
+ encoded_issuance.size()};
+ db.add_asset_record(issued_id, height, bytes);
+ }
+ for (const asset_ownership_proof& proof : payload.ownership_proofs)
+ db.add_asset_key_image(proof.key_image, height);
+ size_t candidate_index = 0;
+ for (size_t group = 0; group < payload.balances.size(); ++group)
+ {
+ const confidential_asset_balance& balance = payload.balances[group];
+ for (size_t index = 0; index < balance.outputs.size(); ++index)
+ {
+ const asset_output_data_t output{balance.asset_id,
+ payload.output_destinations[group][index], balance.outputs[index], height};
+ db.add_asset_output(candidate_ids[candidate_index++], output);
+ }
+ }
+ output_ids = std::move(candidate_ids);
+ return true;
+}
}
}
diff --git a/src/blockchain_db/asset_db.h b/src/blockchain_db/asset_db.h
index 0df5721e0..a43d86585 100644
--- a/src/blockchain_db/asset_db.h
+++ b/src/blockchain_db/asset_db.h
@@ -4,7 +4,9 @@
#include <vector>
#include "blockchain_db.h"
+#include "cryptonote_basic/asset_confidential.h"
#include "cryptonote_basic/asset_types.h"
+#include "cryptonote_basic/asset_wire.h"
namespace cryptonote
{
@@ -27,5 +29,28 @@ namespace assets
uint64_t height,
std::vector<crypto::hash>& asset_ids,
std::string* error = nullptr);
+
+ // Resolve every claimed ring member against consensus storage and reject
+ // key images already spent by an earlier accepted asset transaction before
+ // performing the cryptographic ownership check.
+ bool verify_asset_ownership_against_db(
+ const BlockchainDB& db,
+ const asset_ownership_proof& proof,
+ network_type expected_network,
+ const crypto::hash& carrier_prefix_hash,
+ std::string* error = nullptr);
+
+ // The caller owns the outer blockchain write transaction. Every semantic,
+ // ownership, collision, and collection-authority check completes before the
+ // first write so an accepted payload changes registry, output, and spent-key
+ // state atomically.
+ bool apply_asset_transaction_to_db(
+ BlockchainDB& db,
+ const asset_transaction_payload& payload,
+ network_type expected_network,
+ const crypto::hash& expected_carrier_prefix_hash,
+ uint64_t height,
+ std::vector<crypto::hash>& output_ids,
+ std::string* error = nullptr);
}
}
diff --git a/src/blockchain_db/blockchain_db.cpp b/src/blockchain_db/blockchain_db.cpp
index b1c4a4136..38203ddb2 100644
--- a/src/blockchain_db/blockchain_db.cpp
+++ b/src/blockchain_db/blockchain_db.cpp
@@ -318,7 +318,10 @@ void BlockchainDB::pop_block(block& blk, std::vector<transaction>& txs)
// Asset records created by the detached block are consensus state and must
// disappear in the same write transaction as the native block and tx data.
- remove_asset_records_from_height(height() - 1);
+ const uint64_t detached_height = height() - 1;
+ remove_asset_records_from_height(detached_height);
+ remove_asset_outputs_from_height(detached_height);
+ remove_asset_key_images_from_height(detached_height);
remove_block();
diff --git a/src/blockchain_db/blockchain_db.h b/src/blockchain_db/blockchain_db.h
index c176a8ef5..a52f2bdad 100644
--- a/src/blockchain_db/blockchain_db.h
+++ b/src/blockchain_db/blockchain_db.h
@@ -128,6 +128,14 @@ struct output_data_t
uint64_t height; //!< the height of the block which created the output
rct::key commitment; //!< the output's amount commitment (for spend verification)
};
+
+struct asset_output_data_t
+{
+ crypto::hash asset_id;
+ rct::key destination;
+ rct::key commitment;
+ uint64_t height;
+};
#pragma pack(pop)
#pragma pack(push, 1)
@@ -1791,6 +1799,12 @@ public:
virtual bool get_asset_record(const crypto::hash &asset_id, uint64_t &height, cryptonote::blobdata &payload) const = 0;
virtual void remove_asset_records_from_height(uint64_t height) = 0;
virtual bool for_all_asset_records(std::function<bool(const crypto::hash&, uint64_t, const cryptonote::blobdata_ref&)>) const = 0;
+ virtual void add_asset_output(const crypto::hash &output_id, const asset_output_data_t &output) = 0;
+ virtual bool get_asset_output(const crypto::hash &output_id, asset_output_data_t &output) const = 0;
+ virtual void add_asset_key_image(const crypto::key_image &key_image, uint64_t height) = 0;
+ virtual bool has_asset_key_image(const crypto::key_image &key_image) const = 0;
+ virtual void remove_asset_outputs_from_height(uint64_t height) = 0;
+ virtual void remove_asset_key_images_from_height(uint64_t height) = 0;
//
diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp
index 0c3a4f470..4ba91b72b 100644
--- a/src/blockchain_db/lmdb/db_lmdb.cpp
+++ b/src/blockchain_db/lmdb/db_lmdb.cpp
@@ -58,7 +58,7 @@ using epee::string_tools::pod_to_hex;
using namespace crypto;
// Increase when the DB structure changes
-#define VERSION 6
+#define VERSION 7
namespace
{
@@ -239,6 +239,10 @@ const char* const LMDB_ALT_BLOCKS = "alt_blocks";
const char* const LMDB_ASSET_RECORDS = "asset_records";
const char* const LMDB_ASSET_HEIGHTS = "asset_heights";
+const char* const LMDB_ASSET_OUTPUTS = "asset_outputs";
+const char* const LMDB_ASSET_OUTPUT_HEIGHTS = "asset_output_heights";
+const char* const LMDB_ASSET_KEY_IMAGES = "asset_key_images";
+const char* const LMDB_ASSET_KEY_IMAGE_HEIGHTS = "asset_key_image_heights";
const char* const LMDB_HF_STARTING_HEIGHTS = "hf_starting_heights";
const char* const LMDB_HF_VERSIONS = "hf_versions";
@@ -1511,6 +1515,10 @@ void BlockchainLMDB::open(const std::string& filename, const int db_flags)
lmdb_db_open(txn, LMDB_ASSET_RECORDS, MDB_CREATE, m_asset_records, "Failed to open db handle for m_asset_records");
lmdb_db_open(txn, LMDB_ASSET_HEIGHTS, MDB_INTEGERKEY | MDB_CREATE | MDB_DUPSORT | MDB_DUPFIXED, m_asset_heights, "Failed to open db handle for m_asset_heights");
+ lmdb_db_open(txn, LMDB_ASSET_OUTPUTS, MDB_CREATE, m_asset_outputs, "Failed to open db handle for m_asset_outputs");
+ lmdb_db_open(txn, LMDB_ASSET_OUTPUT_HEIGHTS, MDB_INTEGERKEY | MDB_CREATE | MDB_DUPSORT | MDB_DUPFIXED, m_asset_output_heights, "Failed to open db handle for m_asset_output_heights");
+ lmdb_db_open(txn, LMDB_ASSET_KEY_IMAGES, MDB_CREATE, m_asset_key_images, "Failed to open db handle for m_asset_key_images");
+ lmdb_db_open(txn, LMDB_ASSET_KEY_IMAGE_HEIGHTS, MDB_INTEGERKEY | MDB_CREATE | MDB_DUPSORT | MDB_DUPFIXED, m_asset_key_image_heights, "Failed to open db handle for m_asset_key_image_heights");
// this subdb is dropped on sight, so it may not be present when we open the DB.
// Since we use MDB_CREATE, we'll get an exception if we open read-only and it does not exist.
@@ -1538,6 +1546,10 @@ void BlockchainLMDB::open(const std::string& filename, const int db_flags)
mdb_set_compare(txn, m_alt_blocks, compare_hash32);
mdb_set_compare(txn, m_asset_records, compare_hash32);
mdb_set_dupsort(txn, m_asset_heights, compare_hash32);
+ mdb_set_compare(txn, m_asset_outputs, compare_hash32);
+ mdb_set_dupsort(txn, m_asset_output_heights, compare_hash32);
+ mdb_set_compare(txn, m_asset_key_images, compare_hash32);
+ mdb_set_dupsort(txn, m_asset_key_image_heights, compare_hash32);
mdb_set_compare(txn, m_properties, compare_string);
if (!(mdb_flags & MDB_RDONLY))
@@ -1714,6 +1726,14 @@ void BlockchainLMDB::reset()
throw0(DB_ERROR(lmdb_error("Failed to drop m_asset_records: ", result).c_str()));
if (auto result = mdb_drop(txn, m_asset_heights, 0))
throw0(DB_ERROR(lmdb_error("Failed to drop m_asset_heights: ", result).c_str()));
+ if (auto result = mdb_drop(txn, m_asset_outputs, 0))
+ throw0(DB_ERROR(lmdb_error("Failed to drop m_asset_outputs: ", result).c_str()));
+ if (auto result = mdb_drop(txn, m_asset_output_heights, 0))
+ throw0(DB_ERROR(lmdb_error("Failed to drop m_asset_output_heights: ", result).c_str()));
+ if (auto result = mdb_drop(txn, m_asset_key_images, 0))
+ throw0(DB_ERROR(lmdb_error("Failed to drop m_asset_key_images: ", result).c_str()));
+ if (auto result = mdb_drop(txn, m_asset_key_image_heights, 0))
+ throw0(DB_ERROR(lmdb_error("Failed to drop m_asset_key_image_heights: ", result).c_str()));
// init with current version
MDB_val_str(k, "version");
@@ -2550,6 +2570,125 @@ bool BlockchainLMDB::for_all_asset_records(std::function<bool(const crypto::hash
return ret;
}
+void BlockchainLMDB::add_asset_output(const crypto::hash &output_id, const asset_output_data_t &output)
+{
+ check_open();
+ TXN_BLOCK_PREFIX(0);
+ MDB_val key = {sizeof(output_id), const_cast<crypto::hash*>(&output_id)};
+ MDB_val value = {sizeof(output), const_cast<asset_output_data_t*>(&output)};
+ int result = mdb_put(*txn_ptr, m_asset_outputs, &key, &value, MDB_NOOVERWRITE);
+ if (result == MDB_KEYEXIST)
+ throw1(DB_ERROR("Attempting to add an asset output that already exists"));
+ if (result)
+ throw1(DB_ERROR(lmdb_error("Error adding asset output: ", result).c_str()));
+ MDB_val_copy<uint64_t> height_key(output.height);
+ MDB_val id_value = {sizeof(output_id), const_cast<crypto::hash*>(&output_id)};
+ if ((result = mdb_put(*txn_ptr, m_asset_output_heights, &height_key, &id_value, MDB_NODUPDATA)))
+ throw1(DB_ERROR(lmdb_error("Error indexing asset output height: ", result).c_str()));
+ TXN_BLOCK_POSTFIX_SUCCESS();
+}
+
+bool BlockchainLMDB::get_asset_output(const crypto::hash &output_id, asset_output_data_t &output) const
+{
+ check_open();
+ TXN_PREFIX_RDONLY();
+ MDB_val key = {sizeof(output_id), const_cast<crypto::hash*>(&output_id)}, value;
+ const int result = mdb_get(m_txn, m_asset_outputs, &key, &value);
+ if (result == MDB_NOTFOUND)
+ return false;
+ if (result)
+ throw0(DB_ERROR(lmdb_error("Error retrieving asset output: ", result).c_str()));
+ if (value.mv_size != sizeof(output))
+ throw0(DB_ERROR("Asset output record has an invalid size"));
+ std::memcpy(&output, value.mv_data, sizeof(output));
+ return true;
+}
+
+void BlockchainLMDB::add_asset_key_image(const crypto::key_image &key_image, uint64_t height)
+{
+ check_open();
+ TXN_BLOCK_PREFIX(0);
+ MDB_val key = {sizeof(key_image), const_cast<crypto::key_image*>(&key_image)};
+ MDB_val_copy<uint64_t> value(height);
+ int result = mdb_put(*txn_ptr, m_asset_key_images, &key, &value, MDB_NOOVERWRITE);
+ if (result == MDB_KEYEXIST)
+ throw1(KEY_IMAGE_EXISTS("Attempting to spend an asset key image that already exists"));
+ if (result)
+ throw1(DB_ERROR(lmdb_error("Error adding asset key image: ", result).c_str()));
+ MDB_val_copy<uint64_t> height_key(height);
+ MDB_val image_value = {sizeof(key_image), const_cast<crypto::key_image*>(&key_image)};
+ if ((result = mdb_put(*txn_ptr, m_asset_key_image_heights, &height_key, &image_value, MDB_NODUPDATA)))
+ throw1(DB_ERROR(lmdb_error("Error indexing asset key image height: ", result).c_str()));
+ TXN_BLOCK_POSTFIX_SUCCESS();
+}
+
+bool BlockchainLMDB::has_asset_key_image(const crypto::key_image &key_image) const
+{
+ check_open();
+ TXN_PREFIX_RDONLY();
+ MDB_val key = {sizeof(key_image), const_cast<crypto::key_image*>(&key_image)}, value;
+ const int result = mdb_get(m_txn, m_asset_key_images, &key, &value);
+ if (result == MDB_NOTFOUND)
+ return false;
+ if (result)
+ throw0(DB_ERROR(lmdb_error("Error retrieving asset key image: ", result).c_str()));
+ return true;
+}
+
+void BlockchainLMDB::remove_asset_outputs_from_height(uint64_t height)
+{
+ check_open();
+ TXN_BLOCK_PREFIX(0);
+ MDB_cursor *cursor = nullptr;
+ int result = mdb_cursor_open(*txn_ptr, m_asset_output_heights, &cursor);
+ if (result)
+ throw1(DB_ERROR(lmdb_error("Error opening asset output height cursor: ", result).c_str()));
+ MDB_val_copy<uint64_t> key(height);
+ MDB_val value;
+ result = mdb_cursor_get(cursor, &key, &value, MDB_SET_RANGE);
+ while (result == MDB_SUCCESS)
+ {
+ MDB_val output_key = {value.mv_size, value.mv_data};
+ const int deleted = mdb_del(*txn_ptr, m_asset_outputs, &output_key, nullptr);
+ if (deleted != MDB_SUCCESS && deleted != MDB_NOTFOUND)
+ throw1(DB_ERROR(lmdb_error("Error removing asset output: ", deleted).c_str()));
+ if ((result = mdb_cursor_del(cursor, 0)) != MDB_SUCCESS)
+ throw1(DB_ERROR(lmdb_error("Error removing asset output height: ", result).c_str()));
+ result = mdb_cursor_get(cursor, &key, &value, MDB_NEXT);
+ }
+ mdb_cursor_close(cursor);
+ if (result != MDB_NOTFOUND)
+ throw1(DB_ERROR(lmdb_error("Error iterating asset output heights: ", result).c_str()));
+ TXN_BLOCK_POSTFIX_SUCCESS();
+}
+
+void BlockchainLMDB::remove_asset_key_images_from_height(uint64_t height)
+{
+ check_open();
+ TXN_BLOCK_PREFIX(0);
+ MDB_cursor *cursor = nullptr;
+ int result = mdb_cursor_open(*txn_ptr, m_asset_key_image_heights, &cursor);
+ if (result)
+ throw1(DB_ERROR(lmdb_error("Error opening asset key-image height cursor: ", result).c_str()));
+ MDB_val_copy<uint64_t> key(height);
+ MDB_val value;
+ result = mdb_cursor_get(cursor, &key, &value, MDB_SET_RANGE);
+ while (result == MDB_SUCCESS)
+ {
+ MDB_val image_key = {value.mv_size, value.mv_data};
+ const int deleted = mdb_del(*txn_ptr, m_asset_key_images, &image_key, nullptr);
+ if (deleted != MDB_SUCCESS && deleted != MDB_NOTFOUND)
+ throw1(DB_ERROR(lmdb_error("Error removing asset key image: ", deleted).c_str()));
+ if ((result = mdb_cursor_del(cursor, 0)) != MDB_SUCCESS)
+ throw1(DB_ERROR(lmdb_error("Error removing asset key-image height: ", result).c_str()));
+ result = mdb_cursor_get(cursor, &key, &value, MDB_NEXT);
+ }
+ mdb_cursor_close(cursor);
+ if (result != MDB_NOTFOUND)
+ throw1(DB_ERROR(lmdb_error("Error iterating asset key-image heights: ", result).c_str()));
+ TXN_BLOCK_POSTFIX_SUCCESS();
+}
+
bool BlockchainLMDB::block_exists(const crypto::hash& h, uint64_t *height) const
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
@@ -5831,6 +5970,19 @@ void BlockchainLMDB::migrate_5_6()
txn.commit();
}
+void BlockchainLMDB::migrate_6_7()
+{
+ LOG_PRINT_L3("BlockchainLMDB::" << __func__);
+ mdb_txn_safe txn(false);
+ if (const int result = mdb_txn_begin(m_env, nullptr, 0, txn))
+ throw0(DB_ERROR(lmdb_error("Failed to create transaction for DB v7 migration: ", result).c_str()));
+ MDB_val_str(key, "version");
+ MDB_val_copy<uint32_t> value(7);
+ if (const int result = mdb_put(txn, m_properties, &key, &value, 0))
+ throw0(DB_ERROR(lmdb_error("Failed to update DB version to 7: ", result).c_str()));
+ txn.commit();
+}
+
void BlockchainLMDB::migrate(const uint32_t oldversion)
{
if (oldversion < 1)
@@ -5845,6 +5997,8 @@ void BlockchainLMDB::migrate(const uint32_t oldversion)
migrate_4_5();
if (oldversion < 6)
migrate_5_6();
+ if (oldversion < 7)
+ migrate_6_7();
}
} // namespace cryptonote
diff --git a/src/blockchain_db/lmdb/db_lmdb.h b/src/blockchain_db/lmdb/db_lmdb.h
index d116bcec5..c7851c117 100644
--- a/src/blockchain_db/lmdb/db_lmdb.h
+++ b/src/blockchain_db/lmdb/db_lmdb.h
@@ -315,6 +315,12 @@ public:
virtual bool get_asset_record(const crypto::hash &asset_id, uint64_t &height, cryptonote::blobdata &payload) const;
virtual void remove_asset_records_from_height(uint64_t height);
virtual bool for_all_asset_records(std::function<bool(const crypto::hash&, uint64_t, const cryptonote::blobdata_ref&)>) const;
+ virtual void add_asset_output(const crypto::hash &output_id, const asset_output_data_t &output);
+ virtual bool get_asset_output(const crypto::hash &output_id, asset_output_data_t &output) const;
+ virtual void add_asset_key_image(const crypto::key_image &key_image, uint64_t height);
+ virtual bool has_asset_key_image(const crypto::key_image &key_image) const;
+ virtual void remove_asset_outputs_from_height(uint64_t height);
+ virtual void remove_asset_key_images_from_height(uint64_t height);
virtual uint64_t add_block( const std::pair<block, blobdata>& blk
, size_t block_weight
@@ -449,6 +455,7 @@ private:
// migrate from DB version 5 to 6
void migrate_5_6();
+ void migrate_6_7();
void cleanup_batch();
@@ -479,6 +486,10 @@ private:
MDB_dbi m_asset_records;
MDB_dbi m_asset_heights;
+ MDB_dbi m_asset_outputs;
+ MDB_dbi m_asset_output_heights;
+ MDB_dbi m_asset_key_images;
+ MDB_dbi m_asset_key_image_heights;
MDB_dbi m_hf_starting_heights;
MDB_dbi m_hf_versions;
diff --git a/src/blockchain_db/testdb.h b/src/blockchain_db/testdb.h
index fb97ccee4..c14348245 100644
--- a/src/blockchain_db/testdb.h
+++ b/src/blockchain_db/testdb.h
@@ -170,6 +170,12 @@ public:
virtual bool get_asset_record(const crypto::hash&, uint64_t&, cryptonote::blobdata&) const override { return false; }
virtual void remove_asset_records_from_height(uint64_t) override {}
virtual bool for_all_asset_records(std::function<bool(const crypto::hash&, uint64_t, const cryptonote::blobdata_ref&)>) const override { return true; }
+ virtual void add_asset_output(const crypto::hash&, const asset_output_data_t&) override {}
+ virtual bool get_asset_output(const crypto::hash&, asset_output_data_t&) const override { return false; }
+ virtual void add_asset_key_image(const crypto::key_image&, uint64_t) override {}
+ virtual bool has_asset_key_image(const crypto::key_image&) const override { return false; }
+ virtual void remove_asset_outputs_from_height(uint64_t) override {}
+ virtual void remove_asset_key_images_from_height(uint64_t) override {}
};
}
diff --git a/src/cryptonote_basic/CMakeLists.txt b/src/cryptonote_basic/CMakeLists.txt
index 84e86803c..c8b41a03e 100644
--- a/src/cryptonote_basic/CMakeLists.txt
+++ b/src/cryptonote_basic/CMakeLists.txt
@@ -46,6 +46,7 @@ target_link_libraries(cryptonote_format_utils_basic
set(cryptonote_basic_sources
asset_confidential.cpp
+ asset_wire.cpp
account.cpp
asset_types.cpp
connection_context.cpp
diff --git a/src/cryptonote_basic/asset_wire.cpp b/src/cryptonote_basic/asset_wire.cpp
new file mode 100644
index 000000000..a0d77bf33
--- /dev/null
+++ b/src/cryptonote_basic/asset_wire.cpp
@@ -0,0 +1,355 @@
+#include "asset_wire.h"
+
+#include <cstring>
+#include <limits>
+
+#include "ringct/rctOps.h"
+
+namespace cryptonote
+{
+namespace assets
+{
+namespace
+{
+ constexpr char OUTPUT_ID_DOMAIN[] = "MonzeroAssetOutputIdV1";
+
+ bool fail(std::string* error, const std::string& message)
+ {
+ if (error)
+ *error = message;
+ return false;
+ }
+
+ class writer
+ {
+ public:
+ template<typename T> void pod(const T& value)
+ {
+ const auto* begin = reinterpret_cast<const uint8_t*>(&value);
+ bytes.insert(bytes.end(), begin, begin + sizeof(value));
+ }
+ void count(size_t value) { pod(static_cast<uint8_t>(value)); }
+ void u16(uint16_t value)
+ {
+ bytes.push_back(static_cast<uint8_t>(value));
+ bytes.push_back(static_cast<uint8_t>(value >> 8));
+ }
+ void u32(uint32_t value)
+ {
+ for (unsigned shift = 0; shift < 32; shift += 8)
+ bytes.push_back(static_cast<uint8_t>(value >> shift));
+ }
+ std::vector<uint8_t> bytes;
+ };
+
+ class reader
+ {
+ public:
+ explicit reader(const std::vector<uint8_t>& source) : source_(source) {}
+ template<typename T> bool pod(T& value)
+ {
+ if (offset_ > source_.size() || sizeof(value) > source_.size() - offset_)
+ return false;
+ std::memcpy(&value, source_.data() + offset_, sizeof(value));
+ offset_ += sizeof(value);
+ return true;
+ }
+ bool count(size_t limit, size_t& value)
+ {
+ uint8_t encoded = 0;
+ if (!pod(encoded) || encoded > limit)
+ return false;
+ value = encoded;
+ return true;
+ }
+ bool u16(uint16_t& value)
+ {
+ uint8_t low = 0, high = 0;
+ if (!pod(low) || !pod(high))
+ return false;
+ value = static_cast<uint16_t>(low) | (static_cast<uint16_t>(high) << 8);
+ return true;
+ }
+ bool done() const { return offset_ == source_.size(); }
+ private:
+ const std::vector<uint8_t>& source_;
+ size_t offset_ = 0;
+ };
+
+ void write_keys(writer& out, const rct::keyV& keys)
+ {
+ out.count(keys.size());
+ for (const rct::key& key : keys)
+ out.pod(key);
+ }
+
+ bool read_keys(reader& in, size_t limit, rct::keyV& keys)
+ {
+ size_t count = 0;
+ if (!in.count(limit, count))
+ return false;
+ keys.resize(count);
+ for (rct::key& key : keys)
+ if (!in.pod(key))
+ return false;
+ return true;
+ }
+
+ void write_range_proof(writer& out, const rct::BulletproofPlus& proof)
+ {
+ write_keys(out, proof.V);
+ out.pod(proof.A); out.pod(proof.A1); out.pod(proof.B);
+ out.pod(proof.r1); out.pod(proof.s1); out.pod(proof.d1);
+ write_keys(out, proof.L);
+ write_keys(out, proof.R);
+ }
+
+ bool read_range_proof(reader& in, rct::BulletproofPlus& proof)
+ {
+ return read_keys(in, MAX_CONFIDENTIAL_ASSET_OUTPUTS, proof.V)
+ && in.pod(proof.A) && in.pod(proof.A1) && in.pod(proof.B)
+ && in.pod(proof.r1) && in.pod(proof.s1) && in.pod(proof.d1)
+ && read_keys(in, 16, proof.L) && read_keys(in, 16, proof.R)
+ && !proof.L.empty() && proof.L.size() == proof.R.size();
+ }
+}
+
+bool validate_asset_transaction_payload_shape(const asset_transaction_payload& payload,
+ std::string* error)
+{
+ if (payload.version != ASSET_TRANSACTION_WIRE_VERSION)
+ return fail(error, "unsupported asset transaction wire version");
+ if (payload.network != MAINNET && payload.network != TESTNET && payload.network != STAGENET)
+ return fail(error, "asset transaction requires a public network");
+ if (payload.carrier_prefix_hash == crypto::null_hash)
+ return fail(error, "asset transaction has a zero carrier hash");
+ if (payload.balances.empty() || payload.balances.size() > MAX_ASSET_BALANCE_GROUPS)
+ return fail(error, "asset transaction has an invalid balance-group count");
+ if (payload.output_destinations.size() != payload.balances.size())
+ return fail(error, "asset transaction output destinations do not match balance groups");
+ if (payload.ownership_proofs.size() > MAX_ASSET_OWNERSHIP_PROOFS)
+ return fail(error, "asset transaction has too many ownership proofs");
+ size_t total_inputs = 0, total_destinations = 0;
+ for (size_t group = 0; group < payload.balances.size(); ++group)
+ {
+ const confidential_asset_balance& balance = payload.balances[group];
+ if (balance.pseudo_inputs.empty()
+ || balance.pseudo_inputs.size() > MAX_CONFIDENTIAL_ASSET_INPUTS
+ || balance.outputs.size() + balance.burns.size() > MAX_CONFIDENTIAL_ASSET_OUTPUTS
+ || balance.range_proofs.empty()
+ || balance.range_proofs.size() > MAX_ASSET_RANGE_PROOFS)
+ return fail(error, "asset balance group exceeds canonical limits");
+ if (payload.output_destinations[group].size() != balance.outputs.size())
+ return fail(error, "asset output destination count does not match commitments");
+ total_inputs += balance.pseudo_inputs.size();
+ total_destinations += balance.outputs.size() + balance.burns.size();
+ if (total_inputs > MAX_ASSET_TOTAL_INPUTS
+ || total_destinations > MAX_ASSET_TOTAL_DESTINATIONS)
+ return fail(error, "asset transaction exceeds aggregate input or destination limits");
+ }
+ for (const asset_ownership_proof& proof : payload.ownership_proofs)
+ if (proof.ring.size() != CONFIDENTIAL_ASSET_RING_SIZE
+ || proof.signature.s.size() != CONFIDENTIAL_ASSET_RING_SIZE)
+ return fail(error, "asset ownership proof has a noncanonical ring size");
+ return true;
+}
+
+bool encode_asset_transaction_payload(const asset_transaction_payload& payload,
+ std::vector<uint8_t>& encoded, std::string* error)
+{
+ if (!validate_asset_transaction_payload_shape(payload, error))
+ return false;
+ writer out;
+ out.pod(payload.version);
+ out.pod(static_cast<uint8_t>(payload.network));
+ out.pod(payload.carrier_prefix_hash);
+ out.pod(static_cast<uint8_t>(payload.issuance ? 1 : 0));
+ if (payload.issuance)
+ {
+ std::vector<uint8_t> issuance;
+ if (!encode_issuance_payload(*payload.issuance, issuance, error))
+ return false;
+ if (issuance.size() > std::numeric_limits<uint16_t>::max())
+ return fail(error, "asset issuance payload is too large");
+ out.u16(static_cast<uint16_t>(issuance.size()));
+ out.bytes.insert(out.bytes.end(), issuance.begin(), issuance.end());
+ }
+ out.count(payload.balances.size());
+ for (size_t group = 0; group < payload.balances.size(); ++group)
+ {
+ const confidential_asset_balance& balance = payload.balances[group];
+ out.pod(balance.asset_id);
+ out.count(balance.pseudo_inputs.size());
+ for (const confidential_pseudo_input& input : balance.pseudo_inputs)
+ {
+ out.pod(input.source_asset_id);
+ out.pod(input.commitment);
+ }
+ out.count(balance.outputs.size());
+ for (size_t index = 0; index < balance.outputs.size(); ++index)
+ {
+ out.pod(payload.output_destinations[group][index]);
+ out.pod(balance.outputs[index]);
+ }
+ write_keys(out, balance.burns);
+ out.count(balance.range_proofs.size());
+ for (const rct::BulletproofPlus& proof : balance.range_proofs)
+ write_range_proof(out, proof);
+ }
+ out.count(payload.ownership_proofs.size());
+ for (const asset_ownership_proof& proof : payload.ownership_proofs)
+ {
+ out.pod(proof.asset_id);
+ out.pod(proof.pseudo_input);
+ out.pod(proof.key_image);
+ for (const asset_ring_member& member : proof.ring)
+ {
+ out.pod(member.asset_id); out.pod(member.output_id);
+ out.pod(member.public_output.dest); out.pod(member.public_output.mask);
+ }
+ out.pod(proof.signature.c1);
+ out.pod(proof.signature.D);
+ for (const rct::key& response : proof.signature.s)
+ out.pod(response);
+ }
+ if (out.bytes.size() > MAX_ASSET_WIRE_BYTES)
+ return fail(error, "asset transaction payload exceeds maximum size");
+ encoded = std::move(out.bytes);
+ return true;
+}
+
+bool decode_asset_transaction_payload(const std::vector<uint8_t>& encoded,
+ asset_transaction_payload& payload, std::string* error)
+{
+ if (encoded.empty() || encoded.size() > MAX_ASSET_WIRE_BYTES)
+ return fail(error, "asset transaction payload has an invalid size");
+ reader in(encoded);
+ asset_transaction_payload candidate;
+ uint8_t network = 0, has_issuance = 0;
+ if (!in.pod(candidate.version) || !in.pod(network)
+ || !in.pod(candidate.carrier_prefix_hash) || !in.pod(has_issuance)
+ || has_issuance > 1)
+ return fail(error, "truncated asset transaction header");
+ candidate.network = static_cast<network_type>(network);
+ if (has_issuance)
+ {
+ uint16_t size = 0;
+ if (!in.u16(size))
+ return fail(error, "truncated asset issuance length");
+ std::vector<uint8_t> issuance(size);
+ for (uint8_t& byte : issuance)
+ if (!in.pod(byte))
+ return fail(error, "truncated asset issuance payload");
+ issuance_payload decoded;
+ if (!decode_issuance_payload(issuance, decoded, error))
+ return false;
+ candidate.issuance = decoded;
+ }
+ size_t groups = 0;
+ if (!in.count(MAX_ASSET_BALANCE_GROUPS, groups) || groups == 0)
+ return fail(error, "invalid asset balance-group count");
+ candidate.balances.resize(groups);
+ candidate.output_destinations.resize(groups);
+ for (size_t group = 0; group < groups; ++group)
+ {
+ confidential_asset_balance& balance = candidate.balances[group];
+ size_t inputs = 0, outputs = 0, proofs = 0;
+ if (!in.pod(balance.asset_id) || !in.count(MAX_CONFIDENTIAL_ASSET_INPUTS, inputs) || inputs == 0)
+ return fail(error, "truncated or invalid asset inputs");
+ balance.pseudo_inputs.resize(inputs);
+ for (confidential_pseudo_input& input : balance.pseudo_inputs)
+ if (!in.pod(input.source_asset_id) || !in.pod(input.commitment))
+ return fail(error, "truncated asset pseudo input");
+ if (!in.count(MAX_CONFIDENTIAL_ASSET_OUTPUTS, outputs))
+ return fail(error, "invalid asset output count");
+ balance.outputs.resize(outputs);
+ candidate.output_destinations[group].resize(outputs);
+ for (size_t index = 0; index < outputs; ++index)
+ if (!in.pod(candidate.output_destinations[group][index]) || !in.pod(balance.outputs[index]))
+ return fail(error, "truncated asset output");
+ if (!read_keys(in, MAX_CONFIDENTIAL_ASSET_OUTPUTS - outputs, balance.burns)
+ || !in.count(MAX_ASSET_RANGE_PROOFS, proofs) || proofs == 0)
+ return fail(error, "invalid asset burn or range-proof count");
+ balance.range_proofs.resize(proofs);
+ for (rct::BulletproofPlus& proof : balance.range_proofs)
+ if (!read_range_proof(in, proof))
+ return fail(error, "truncated or noncanonical asset range proof");
+ }
+ size_t ownership_count = 0;
+ if (!in.count(MAX_ASSET_OWNERSHIP_PROOFS, ownership_count))
+ return fail(error, "invalid asset ownership-proof count");
+ candidate.ownership_proofs.resize(ownership_count);
+ for (asset_ownership_proof& proof : candidate.ownership_proofs)
+ {
+ if (!in.pod(proof.asset_id) || !in.pod(proof.pseudo_input) || !in.pod(proof.key_image))
+ return fail(error, "truncated asset ownership proof");
+ proof.ring.resize(CONFIDENTIAL_ASSET_RING_SIZE);
+ for (asset_ring_member& member : proof.ring)
+ if (!in.pod(member.asset_id) || !in.pod(member.output_id)
+ || !in.pod(member.public_output.dest) || !in.pod(member.public_output.mask))
+ return fail(error, "truncated asset ownership ring");
+ if (!in.pod(proof.signature.c1) || !in.pod(proof.signature.D))
+ return fail(error, "truncated asset CLSAG header");
+ proof.signature.s.resize(CONFIDENTIAL_ASSET_RING_SIZE);
+ for (rct::key& response : proof.signature.s)
+ if (!in.pod(response))
+ return fail(error, "truncated asset CLSAG responses");
+ std::memcpy(&proof.signature.I, &proof.key_image, sizeof(proof.signature.I));
+ }
+ if (!in.done() || !validate_asset_transaction_payload_shape(candidate, error))
+ return fail(error, in.done() ? "invalid asset transaction payload" : "trailing asset transaction bytes");
+ payload = std::move(candidate);
+ return true;
+}
+
+bool verify_asset_transaction_payload(const asset_transaction_payload& payload,
+ const std::set<crypto::hash>& known_assets, network_type expected_network,
+ const crypto::hash& expected_carrier_prefix_hash, std::string* error)
+{
+ if (!validate_asset_transaction_payload_shape(payload, error))
+ return false;
+ if (payload.network != expected_network)
+ return fail(error, "asset transaction belongs to a different network");
+ if (payload.carrier_prefix_hash != expected_carrier_prefix_hash)
+ return fail(error, "asset transaction carrier hash mismatch");
+ boost::optional<issuance_descriptor> descriptor;
+ if (payload.issuance)
+ {
+ if (!verify_issuance_authorization(payload.issuance->descriptor,
+ payload.issuance->issuer_signature, error))
+ return false;
+ descriptor = payload.issuance->descriptor;
+ }
+ for (const std::vector<rct::key>& destinations : payload.output_destinations)
+ for (const rct::key& destination : destinations)
+ if (!rct::isInMainSubgroup(destination))
+ return fail(error, "asset transaction contains an invalid destination key");
+ return verify_confidential_asset_transaction_with_ownership(
+ payload.balances, payload.ownership_proofs, known_assets, descriptor,
+ expected_network, expected_carrier_prefix_hash, error);
+}
+
+bool derive_asset_output_id(network_type network,
+ const crypto::hash& carrier_prefix_hash,
+ const crypto::hash& asset_id, uint32_t output_index,
+ const confidential_asset_output& output, crypto::hash& output_id,
+ std::string* error)
+{
+ if (network != MAINNET && network != TESTNET && network != STAGENET)
+ return fail(error, "asset output identity requires a public network");
+ if (carrier_prefix_hash == crypto::null_hash || asset_id == crypto::null_hash)
+ return fail(error, "asset output identity has a zero carrier or asset id");
+ if (!rct::isInMainSubgroup(output.destination)
+ || !rct::isInMainSubgroup(output.commitment))
+ return fail(error, "asset output identity contains an invalid point");
+ writer bytes;
+ bytes.bytes.insert(bytes.bytes.end(), OUTPUT_ID_DOMAIN,
+ OUTPUT_ID_DOMAIN + sizeof(OUTPUT_ID_DOMAIN) - 1);
+ bytes.pod(get_config(network).NETWORK_ID);
+ bytes.pod(carrier_prefix_hash); bytes.pod(asset_id);
+ bytes.u32(output_index); bytes.pod(output.destination); bytes.pod(output.commitment);
+ output_id = crypto::cn_fast_hash(bytes.bytes.data(), bytes.bytes.size());
+ return true;
+}
+}
+}
diff --git a/src/cryptonote_basic/asset_wire.h b/src/cryptonote_basic/asset_wire.h
new file mode 100644
index 000000000..e5f63a01b
--- /dev/null
+++ b/src/cryptonote_basic/asset_wire.h
@@ -0,0 +1,68 @@
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include <boost/optional.hpp>
+
+#include "asset_confidential.h"
+
+namespace cryptonote
+{
+namespace assets
+{
+ constexpr uint8_t ASSET_TRANSACTION_WIRE_VERSION = 1;
+ constexpr size_t MAX_ASSET_BALANCE_GROUPS = 8;
+ constexpr size_t MAX_ASSET_TOTAL_INPUTS = 64;
+ constexpr size_t MAX_ASSET_TOTAL_DESTINATIONS = 64;
+ constexpr size_t MAX_ASSET_OWNERSHIP_PROOFS = 64;
+ constexpr size_t MAX_ASSET_RANGE_PROOFS = 16;
+ constexpr size_t MAX_ASSET_WIRE_BYTES = 256 * 1024;
+
+ struct confidential_asset_output
+ {
+ rct::key destination{};
+ rct::key commitment{};
+ };
+
+ // Inactive canonical payload prototype. The carrier hash is computed from
+ // the native transaction prefix with this envelope omitted; activation code
+ // must enforce that procedure to avoid a self-referential hash.
+ struct asset_transaction_payload
+ {
+ uint8_t version = ASSET_TRANSACTION_WIRE_VERSION;
+ network_type network = UNDEFINED;
+ crypto::hash carrier_prefix_hash{};
+ boost::optional<issuance_payload> issuance;
+ std::vector<confidential_asset_balance> balances;
+ std::vector<std::vector<rct::key>> output_destinations;
+ std::vector<asset_ownership_proof> ownership_proofs;
+ };
+
+ bool validate_asset_transaction_payload_shape(
+ const asset_transaction_payload& payload,
+ std::string* error = nullptr);
+ bool encode_asset_transaction_payload(
+ const asset_transaction_payload& payload,
+ std::vector<uint8_t>& encoded,
+ std::string* error = nullptr);
+ bool decode_asset_transaction_payload(
+ const std::vector<uint8_t>& encoded,
+ asset_transaction_payload& payload,
+ std::string* error = nullptr);
+ bool verify_asset_transaction_payload(
+ const asset_transaction_payload& payload,
+ const std::set<crypto::hash>& known_assets,
+ network_type expected_network,
+ const crypto::hash& expected_carrier_prefix_hash,
+ std::string* error = nullptr);
+ bool derive_asset_output_id(
+ network_type network,
+ const crypto::hash& carrier_prefix_hash,
+ const crypto::hash& asset_id,
+ uint32_t output_index,
+ const confidential_asset_output& output,
+ crypto::hash& output_id,
+ std::string* error = nullptr);
+}
+}