4 #include <fc/crypto/sha512.hpp>
5 #include <fc/crypto/hex.hpp>
6 #include <fc/crypto/ripemd160.hpp>
12 namespace atomicswap {
14 std::string
get_secret_hex(
const std::string& secret,
const uint8_t secret_length )
16 fc::sha512 hash = fc::sha512().hash(secret);
17 FC_ASSERT(secret_length <= hash.data_size(),
"Required secret string is too long. Use length less or equal ${1}",
18 (
"1", hash.data_size()));
19 std::size_t out_sz = secret_length;
22 static const size_t entropy_percent = (std::size_t)50;
23 FC_ASSERT(entropy_percent > 0);
24 std::size_t entropy = secret.size();
26 entropy = entropy_percent + entropy % entropy_percent;
29 out_sz = hash.data_size() * entropy / entropy_percent / (std::size_t)2;
37 return fc::to_hex(hash.data(), out_sz);
42 fc::ripemd160::encoder e;
45 for (
char ch : secret_hex)
50 ch_out = fc::from_hex(ch) << 4;
54 ch_out |= fc::from_hex(ch);
63 fc::ripemd160 encode = e.result();
69 FC_ASSERT(!secret_hex.empty(),
"Empty secret.");
71 for (
char ch : secret_hex)
80 FC_ASSERT(secret_hash.size() == fmt.data_size() * 2,
"Invalid hash format. It must be in hex RIPEMD160 format.");
83 fmt = fc::ripemd160(secret_hash);
85 FC_CAPTURE_AND_RETHROW((secret_hash))
91 "Contract metadata string is too long.");
97 std::stringstream store;
98 store << from << to << secret_hash;
99 return fc::sha256().hash(store.str());
116 FC_ASSERT(encode.data_size() ==
sizeof(ret.data));
117 memcpy((
char*)&ret.data, encode.data(), encode.data_size());
119 ret.data = boost::endian::big_to_native(ret.data);
#define SCORUM_ATOMICSWAP_SECRET_MAX_LENGTH
#define SCORUM_ATOMICSWAP_CONTRACT_METADATA_MAX_LENGTH
fc::fixed_string_32 hash_index_type
void validate_contract_metadata(const std::string &metadata)
void validate_secret(const std::string &secret_hex)
hash_index_type get_contract_hash(const account_name_type &from, const account_name_type &to, const std::string &secret_hash)
void validate_secret_hash(const std::string &secret_hash)
fc::sha256 get_contract_hash_obj(const account_name_type &from, const account_name_type &to, const std::string &secret_hash)
std::string get_secret_hash(const std::string &secret_hex)
std::string get_secret_hex(const std::string &secret, const uint8_t secret_length)
std::string get_contract_hash_hex(const account_name_type &from, const account_name_type &to, const std::string &secret_hash)
fc::fixed_string_16 account_name_type