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