forked from oxen-io/oxen-storage-server
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathpubkey.h
More file actions
74 lines (56 loc) · 2.88 KB
/
Copy pathpubkey.h
File metadata and controls
74 lines (56 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#pragma once
#include <cstdint>
#include <string>
namespace oxenss {
// Network byte + Ed25519 pubkey, encoded in bytes or hex. On testnet we allow the network byte
// to be missing (and treat it as an implicit 00).
inline constexpr size_t USER_PUBKEY_SIZE_BYTES = 33;
inline constexpr size_t USER_PUBKEY_SIZE_HEX = USER_PUBKEY_SIZE_BYTES * 2;
class user_pubkey {
int network_ = -1;
std::string pubkey_;
friend class DatabaseImpl;
public:
// Default constructor; constructs an invalid pubkey
user_pubkey() = default;
user_pubkey(int network, std::string raw_pk) : network_{network}, pubkey_{std::move(raw_pk)} {}
// bool conversion: returns true if this object contains a valid pubkey
explicit operator bool() const { return !pubkey_.empty(); }
bool operator==(const user_pubkey& other) const {
return type() == other.type() && raw() == other.raw();
}
// Replaces the stored pubkey with one parsed from the string `pk`. `pk` can be either raw
// bytes (33 bytes of netid + pubkey), or hex (66 hex digits). If `pk` is not a valid
// pubkey then `this` is put into an invalid-pubkey state (i.e. `(bool)pk` will be false).
// Returns a reference to *this (primary that `if (upk.load(pk)) { ... }` can be used to
// load-and-test).
user_pubkey& load(std::string_view pk);
// Returns the network id (0-255) that is typically prefixed on the beginning of the pubkey
// string; currently 5 is used for Session Ed25519 pubkey IDs on mainnet, 0 is used for
// Session IDs on testnet. Returns -1 if this object does not contain a valid pubkey.
int type() const { return network_; }
// Returns the user pubkey hex string, not including the network prefix. Returns an empty
// string for an invalid (default constructed) pubkey.
std::string hex() const;
// Returns the user pubkey hex string, including the network prefix (unless on testnet with
// netid == 0, in which case there is no prefix). Returns an empty string for an invalid
// (default constructed) pubkey.
std::string prefixed_hex() const;
// Returns the raw bytes that make up the pubkey (not including the type/network prefix).
const std::string& raw() const { return pubkey_; }
// Returns the raw bytes that makes up the pubkey, including the type/network prefix byte.
// Returns an empty string for an invalid (default constructed) pubkey.
std::string prefixed_raw() const;
};
/// Maps a pubkey into a 64-bit "swarm space" value; the swarm you belong to is whichever one
/// has a swarm id closest to this pubkey-derived value.
uint64_t pubkey_to_swarm_space(const user_pubkey& pk);
} // namespace oxenss
namespace std {
template <>
struct hash<oxenss::user_pubkey> {
size_t operator()(const oxenss::user_pubkey& pk) const {
return static_cast<size_t>(pk.type()) ^ hash<std::string>{}(pk.raw());
}
};
} // namespace std