Ton

Форк
0
/
dht-node.cpp 
79 строк · 3.1 Кб
1
/*
2
    This file is part of TON Blockchain Library.
3

4
    TON Blockchain Library is free software: you can redistribute it and/or modify
5
    it under the terms of the GNU Lesser General Public License as published by
6
    the Free Software Foundation, either version 2 of the License, or
7
    (at your option) any later version.
8

9
    TON Blockchain Library is distributed in the hope that it will be useful,
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
    GNU Lesser General Public License for more details.
13

14
    You should have received a copy of the GNU Lesser General Public License
15
    along with TON Blockchain Library.  If not, see <http://www.gnu.org/licenses/>.
16

17
    Copyright 2017-2020 Telegram Systems LLP
18
*/
19
#include "dht-node.hpp"
20
#include "keys/encryptor.h"
21

22
namespace ton {
23

24
namespace dht {
25

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");
29
  }
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();
37
  } else {
38
    return td::Status::Error(ErrorCode::notready, "invalid length of signature");
39
  }
40
  if (network_id != our_network_id && network_id != -1 && our_network_id != -1) {
41
    // Remove (network_id != -1 && our_network_id != -1) after network update
42
    return td::Status::Error(ErrorCode::notready, PSTRING() << "wrong network id (expected " << our_network_id
43
                                                            << ", found " << network_id << ")");
44
  }
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");
49
  }
50
  if (!addr_list.size()) {
51
    return td::Status::Error(ErrorCode::notready, "dht node must have >0 addresses");
52
  }
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());
55

56
  *this = std::move(new_node);
57
  return td::Status::OK();
58
}
59

60
td::Status DhtNode::check_signature() const {
61
  TRY_RESULT(enc, id_.pubkey().create_encryptor());
62
  auto node2 = clone();
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();
67
}
68

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());
73
  }
74
  return create_tl_object<ton_api::dht_nodes>(std::move(L));
75
}
76

77
}  // namespace dht
78

79
}  // namespace ton
80

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.