Back to Site
Loading...
Searching...
No Matches
peer_id.h
Go to the documentation of this file.
1#pragma once
2
16#include "util/rats_export.h"
17#include "core/bytes.h"
18
19#include <array>
20#include <cstdint>
21#include <optional>
22#include <string>
23#include <string_view>
24
25namespace librats {
26
27class RATS_API PeerId {
28public:
29 static constexpr size_t kSize = 32;
30
31 PeerId() = default;
32
34 static PeerId from_public_key(const uint8_t* key, size_t len);
35 static PeerId from_public_key(ByteView key) { return from_public_key(key.data(), key.size()); }
36
38 static std::optional<PeerId> from_bytes(ByteView raw);
39
41 static std::optional<PeerId> from_hex(std::string_view hex);
42
43 std::string to_hex() const;
44 std::string short_hex() const;
45
46 const std::array<uint8_t, kSize>& bytes() const noexcept { return bytes_; }
47 bool is_zero() const noexcept;
48
49 bool operator==(const PeerId& o) const noexcept { return bytes_ == o.bytes_; }
50 bool operator!=(const PeerId& o) const noexcept { return bytes_ != o.bytes_; }
51 bool operator<(const PeerId& o) const noexcept { return bytes_ < o.bytes_; }
52
54 struct Hash {
55 size_t operator()(const PeerId& id) const noexcept;
56 };
57
58private:
59 std::array<uint8_t, kSize> bytes_{};
60};
61
62} // namespace librats
bool operator!=(const PeerId &o) const noexcept
Definition peer_id.h:50
const std::array< uint8_t, kSize > & bytes() const noexcept
Definition peer_id.h:46
static std::optional< PeerId > from_hex(std::string_view hex)
Parse a 64-char lowercase/uppercase hex string. nullopt if malformed.
PeerId()=default
std::string to_hex() const
static std::optional< PeerId > from_bytes(ByteView raw)
Wrap raw 32 id-bytes verbatim (NOT hashed). nullopt unless exactly kSize.
static PeerId from_public_key(const uint8_t *key, size_t len)
Derive the PeerId from a raw static public key (SHA-256 of the key).
bool is_zero() const noexcept
static PeerId from_public_key(ByteView key)
Definition peer_id.h:35
std::string short_hex() const
first 8 hex chars, for logs
bool operator<(const PeerId &o) const noexcept
Definition peer_id.h:51
Definition node.h:66
Hash functor for use as an unordered_map/set key.
Definition peer_id.h:54
size_t operator()(const PeerId &id) const noexcept