19
#include "dht-node.hpp"
20
#include "keys/encryptor.h"
26
td::Status DhtNode::update(tl_object_ptr<ton_api::dht_node> obj, td::int32 our_network_id) {
27
if (version_ && obj->version_ <= version_) {
28
return td::Status::Error(ErrorCode::notready, "too old version");
30
td::BufferSlice signature;
31
td::int32 network_id = -1;
32
if (obj->signature_.size() == 64) {
33
signature = std::move(obj->signature_);
34
} else if (obj->signature_.size() == 64 + 4) {
35
signature = td::BufferSlice{obj->signature_.as_slice().remove_prefix(4)};
36
network_id = *(td::uint32 *)obj->signature_.as_slice().remove_suffix(64).data();
38
return td::Status::Error(ErrorCode::notready, "invalid length of signature");
40
if (network_id != our_network_id && network_id != -1 && our_network_id != -1) {
42
return td::Status::Error(ErrorCode::notready, PSTRING() << "wrong network id (expected " << our_network_id
43
<< ", found " << network_id << ")");
45
TRY_RESULT(pub, adnl::AdnlNodeIdFull::create(obj->id_));
46
TRY_RESULT(addr_list, adnl::AdnlAddressList::create(std::move(obj->addr_list_)));
47
if (!addr_list.public_only()) {
48
return td::Status::Error(ErrorCode::notready, "dht node must have only public addresses");
50
if (!addr_list.size()) {
51
return td::Status::Error(ErrorCode::notready, "dht node must have >0 addresses");
53
DhtNode new_node{std::move(pub), std::move(addr_list), obj->version_, network_id, std::move(signature)};
54
TRY_STATUS(new_node.check_signature());
56
*this = std::move(new_node);
57
return td::Status::OK();
60
td::Status DhtNode::check_signature() const {
61
TRY_RESULT(enc, id_.pubkey().create_encryptor());
63
node2.signature_ = {};
64
TRY_STATUS_PREFIX(enc->check_signature(serialize_tl_object(node2.tl(), true).as_slice(), signature_.as_slice()),
65
"bad node signature: ");
66
return td::Status::OK();
69
tl_object_ptr<ton_api::dht_nodes> DhtNodesList::tl() const {
70
std::vector<tl_object_ptr<ton_api::dht_node>> L;
71
for (auto &n : list_) {
72
L.emplace_back(n.tl());
74
return create_tl_object<ton_api::dht_nodes>(std::move(L));