stealth_bitfield calculate_bitfield(const data_chunk& stealth_data) { // Calculate stealth bitfield const hash_digest index = generate_sha256_hash(stealth_data); auto deserial = make_deserializer( index.begin(), index.begin() + bitfield_size); stealth_bitfield bitfield = deserial.read_uint_auto<stealth_bitfield>(); return bitfield; }
/** * Copy `binary` data from protobuf's storage format (std::string) * to libbitcoin's storage format (hash_digest). */ static bool unpack_hash(hash_digest& out, const std::string& in) { if (in.size() != hash_size) return false; std::copy(in.begin(), in.end(), out.begin()); return true; }
bool read_hash(hash_digest& hash, const data_chunk& raw_hash) { if (raw_hash.size() != hash.size()) { log_warning(LOG_SUBSCRIBER) << "Wrong size for hash. Dropping."; return false; } std::copy(raw_hash.begin(), raw_hash.end(), hash.begin()); return true; }
bool elliptic_curve_key::verify(hash_digest hash, const data_chunk& signature) { BITCOIN_ASSERT(key_ != nullptr); // SSL likes a reversed hash std::reverse(hash.begin(), hash.end()); // -1 = error, 0 = bad sig, 1 = good if (ECDSA_verify(0, hash.data(), hash.size(), signature.data(), signature.size(), key_) == 1) return true; return false; }
bool verify_signature(const ec_point& public_key, hash_digest hash, const data_chunk& signature) { std::reverse(hash.begin(), hash.end()); init.init(); return 1 == secp256k1_ecdsa_verify( hash.data(), hash.size(), signature.data(), signature.size(), public_key.data(), public_key.size() ); }
bool extract_ephemeral_key(hash_digest& out_unsigned_ephemeral_key, const script& script) { if (!is_stealth_script(script)) return false; const auto& data = script.operations[1].data; std::copy(data.begin(), data.begin() + hash_size, out_unsigned_ephemeral_key.begin()); return true; }
data_chunk elliptic_curve_key::sign(hash_digest hash) const { BITCOIN_ASSERT(key_ != nullptr); // SSL likes a reversed hash std::reverse(hash.begin(), hash.end()); data_chunk signature(ECDSA_size(key_)); unsigned int signature_length = signature.size(); if (!ECDSA_sign(0, hash.data(), hash.size(), signature.data(), &signature_length, key_)) return data_chunk(); signature.resize(signature_length); return signature; }
bool decode_hash(hash_digest& out, const std::string& in) { if (in.size() != 2 * hash_size) return false; hash_digest result; if (!decode_base16_private(result.data(), result.size(), in.data())) return false; // Reverse: std::reverse_copy(result.begin(), result.end(), out.begin()); return true; }
data_chunk sign(ec_secret secret, hash_digest hash, ec_secret nonce) { std::reverse(hash.begin(), hash.end()); init.init(); int out_size = 72; data_chunk signature(out_size); if (!verify_private_key(nonce)) // Needed because of upstream bug return data_chunk(); bool valid = secp256k1_ecdsa_sign(hash.data(), hash.size(), signature.data(), &out_size, secret.data(), nonce.data()) == 1; if (!valid) return data_chunk(); signature.resize(out_size); return signature; }
bool message::dequeue(hash_digest& value) { if (queue_.empty()) return false; const auto& front = queue_.front(); if (front.size() == hash_size) { std::copy(front.begin(), front.end(), value.begin()); queue_.pop(); return true; } queue_.pop(); return false; }
BC_API ec_secret create_nonce(ec_secret secret, hash_digest hash) { std::reverse(hash.begin(), hash.end()); init.init(); hash_digest K {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }}; hash_digest V {{ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }}; K = hmac_sha256_hash(V + byte_array<1>{{0x00}} + secret + hash, K); V = hmac_sha256_hash(V, K); K = hmac_sha256_hash(V + byte_array<1>{{0x01}} + secret + hash, K); V = hmac_sha256_hash(V, K); while (true) { V = hmac_sha256_hash(V, K); if (verify_private_key(V)) return V; K = hmac_sha256_hash(V + byte_array<1>{{0x00}}, K); V = hmac_sha256_hash(V, K); } }
static std::string pack_hash(hash_digest in) { return std::string(in.begin(), in.end()); }
void hash_number::set_hash(const hash_digest& hash) { std::copy(hash.begin(), hash.end(), hash_.begin()); }
void append_hash(czmqpp::message& message, const hash_digest& hash) { message.append(data_chunk(hash.begin(), hash.end())); }
// Bitcoin hash format (these are all reversed): std::string encode_hash(hash_digest hash) { std::reverse(hash.begin(), hash.end()); return encode_base16(hash); }