forked from oxen-io/oxen-storage-server
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathpubkey.cpp
More file actions
69 lines (60 loc) · 1.89 KB
/
Copy pathpubkey.cpp
File metadata and controls
69 lines (60 loc) · 1.89 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
#include "pubkey.h"
#include "mainnet.h"
#include "oxenc/endian.h"
#include <oxenc/hex.h>
#include <charconv>
#include <cassert>
namespace oxenss {
uint64_t pubkey_to_swarm_space(const user_pubkey& pk) {
const auto bytes = pk.raw();
assert(bytes.size() == 32);
uint64_t res = 0;
for (size_t i = 0; i < bytes.size(); i += 8)
res ^= oxenc::load_big_to_host<uint64_t>(bytes.data() + i);
return res;
}
user_pubkey& user_pubkey::load(std::string_view pk) {
if (pk.size() == USER_PUBKEY_SIZE_HEX && oxenc::is_hex(pk)) {
uint8_t netid;
oxenc::from_hex(pk.begin(), pk.begin() + 2, &netid);
network_ = netid;
pubkey_ = oxenc::from_hex(pk.substr(2));
} else if (pk.size() == USER_PUBKEY_SIZE_BYTES) {
network_ = static_cast<uint8_t>(pk.front());
pubkey_ = pk.substr(1);
} else if (!is_mainnet && pk.size() == USER_PUBKEY_SIZE_HEX - 2 && oxenc::is_hex(pk)) {
network_ = 5;
pubkey_ = oxenc::from_hex(pk);
} else if (!is_mainnet && pk.size() == USER_PUBKEY_SIZE_BYTES - 1) {
network_ = 5;
pubkey_ = pk;
} else {
network_ = -1;
pubkey_.clear();
}
return *this;
}
std::string user_pubkey::hex() const {
return oxenc::to_hex(pubkey_);
}
std::string user_pubkey::prefixed_hex() const {
std::string hex;
if (pubkey_.empty())
return hex;
hex.reserve(USER_PUBKEY_SIZE_HEX);
auto bi = std::back_inserter(hex);
if (uint8_t netid = type(); !(netid == 0 && !is_mainnet))
oxenc::to_hex(&netid, &netid + 1, bi);
oxenc::to_hex(pubkey_.begin(), pubkey_.end(), bi);
return hex;
}
std::string user_pubkey::prefixed_raw() const {
std::string bytes;
if (pubkey_.empty())
return bytes;
bytes.reserve(1 + USER_PUBKEY_SIZE_BYTES);
bytes += static_cast<uint8_t>(network_);
bytes += pubkey_;
return bytes;
}
} // namespace oxenss