COOPENOMICS  v1
Кооперативная Экономика
utils.hpp
См. документацию.
1#include <eosio/eosio.hpp>
2#include <eosio/transaction.hpp>
3#include <eosio/crypto.hpp>
4
5static uint128_t combine_ids(const uint64_t &x, const uint64_t &y) {
6 return (uint128_t{x} << 64) | y;
7};
8
9static uint128_t combine_checksum_ids(const checksum256 &hash, eosio::name username) {
10 auto hash_bytes = hash.extract_as_byte_array();
11 uint64_t truncated_hash = *reinterpret_cast<const uint64_t*>(hash_bytes.data()); // Берем первые 8 байт
12 return combine_ids(truncated_hash, username.value);
13}
14
15
16std::string generate_random_name() {
17 const char ALPHABET[] = "abcdefghijklmnopqrstuvwxyz";
18 const int ALPHABET_SIZE = sizeof(ALPHABET) - 1;
19 const int NAME_LENGTH = 12;
20
21 uint64_t current_time =
22 eosio::current_time_point().sec_since_epoch(); // Получаем текущее время
23 std::string random_name;
24
25 for (int i = 0; i < NAME_LENGTH; ++i) {
26 current_time = (current_time * 1103515245 + 12345) %
27 0x100000000; // Линейный конгруэнтный метод
28 int random_index = (current_time % ALPHABET_SIZE);
29 random_name += ALPHABET[random_index];
30 }
31
32 return random_name;
33}
34
35eosio::checksum256 hashit(std::string str) {
36 return eosio::sha256(const_cast<char *>(str.c_str()), str.size());
37}
38
39uint64_t hash64(const char *buf, size_t len) {
40 eosio::checksum256 hash = eosio::sha256(buf, len);
41 return *(reinterpret_cast<const uint64_t *>(&hash));
42}
43
44uint64_t hash64(const std::string &arg) {
45 return hash64(arg.c_str(), arg.size());
46}
47
48uint64_t generate() {
49 auto size = eosio::transaction_size();
50 char buf[size];
51 uint32_t read = eosio::read_transaction(buf, size);
52 eosio::check(size == read, "read_transaction failed");
53
54 uint64_t seed = eosio::current_time_point().sec_since_epoch();
55 for (int i = 0; i < size; i++) {
56 seed ^= ((uint64_t)buf[i] << (i % 8));
57 }
58
59 return seed;
60}
61
62std::string checksum256_to_hex(const eosio::checksum256& hash) {
63 auto hash_bytes = hash.extract_as_byte_array();
64 std::string hash_hex;
65 for (const auto& byte : hash_bytes) {
66 char hex_char[3];
67 sprintf(hex_char, "%02x", byte);
68 hash_hex += hex_char;
69 }
70 return hash_hex;
71}
72
73uint64_t extract_registry_id_from_meta(const std::string& meta_json) {
74 // Ищем "registry_id": и извлекаем число после двоеточия
75 std::string search_key = "\"registry_id\":";
76 size_t pos = meta_json.find(search_key);
77
78 eosio::check(pos != std::string::npos, "registry_id not found in meta");
79
80 // Перемещаемся после двоеточия
81 pos += search_key.length();
82
83 // Ищем первую цифру
84 while (pos < meta_json.length() && (meta_json[pos] < '0' || meta_json[pos] > '9')) {
85 pos++;
86 }
87
88 eosio::check(pos < meta_json.length(), "registry_id value not found");
89
90 // Собираем цифры
91 uint64_t result = 0;
92 while (pos < meta_json.length() && meta_json[pos] >= '0' && meta_json[pos] <= '9') {
93 result = result * 10 + (meta_json[pos] - '0');
94 pos++;
95 }
96
97 eosio::check(result > 0, "invalid registry_id value");
98
99 return result;
100}
eosio::checksum256 hashit(std::string str)
Definition: utils.hpp:35
uint64_t generate()
Definition: utils.hpp:48
uint64_t extract_registry_id_from_meta(const std::string &meta_json)
Definition: utils.hpp:73
std::string checksum256_to_hex(const eosio::checksum256 &hash)
Definition: utils.hpp:62
static uint128_t combine_checksum_ids(const checksum256 &hash, eosio::name username)
Definition: utils.hpp:9
std::string generate_random_name()
Definition: utils.hpp:16
static uint128_t combine_ids(const uint64_t &x, const uint64_t &y)
Definition: utils.hpp:5
uint64_t hash64(const char *buf, size_t len)
Definition: utils.hpp:39