forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-random-id.cpp
151 lines (130 loc) · 5.26 KB
/
generate-random-id.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
This file is part of TON Blockchain source code.
TON Blockchain is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
TON Blockchain is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TON Blockchain. If not, see <http://www.gnu.org/licenses/>.
In addition, as a special exception, the copyright holders give permission
to link the code of portions of this program with the OpenSSL library.
You must obey the GNU General Public License in all respects for all
of the code used other than OpenSSL. If you modify file(s) with this
exception, you may extend this exception to your version of the file(s),
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version. If you delete this exception statement
from all source files in the program, then also delete it here.
Copyright 2017-2019 Telegram Systems LLP
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cassert>
#include "crypto/ellcurve/Ed25519.h"
#include "adnl/utils.hpp"
#include "auto/tl/ton_api.h"
#include "auto/tl/ton_api_json.h"
#include "tl/tl_json.h"
#include "td/utils/OptionsParser.h"
#include "td/utils/filesystem.h"
#include "keys/encryptor.h"
#include "keys/keys.hpp"
int main(int argc, char *argv[]) {
ton::PrivateKey pk;
ton::tl_object_ptr<ton::ton_api::adnl_addressList> addr_list;
td::OptionsParser p;
p.set_description("generate random id");
std::string mode = "";
std::string name = "id_ton";
p.add_option('m', "mode", "sets mode id/adnl/dht/keys", [&](td::Slice key) {
mode = key.str();
return td::Status::OK();
});
p.add_option('h', "help", "prints_help", [&]() {
char b[10240];
td::StringBuilder sb(td::MutableSlice{b, 10000});
sb << p;
std::cout << sb.as_cslice().c_str();
std::exit(2);
return td::Status::OK();
});
p.add_option('n', "name", "name to keys", [&](td::Slice arg) {
name = arg.str();
return td::Status::OK();
});
p.add_option('k', "key", "private key to import", [&](td::Slice key) {
if (!pk.empty()) {
return td::Status::Error("duplicate '-k' option");
}
TRY_RESULT_PREFIX(data, td::read_file_secure(key.str()), "failed to read private key: ");
TRY_RESULT_PREFIX_ASSIGN(pk, ton::PrivateKey::import(data.as_slice()), "failed to import private key: ");
return td::Status::OK();
});
p.add_option('a', "addr-list", "addr list to sign", [&](td::Slice key) {
if (addr_list) {
return td::Status::Error("duplicate '-a' option");
}
CHECK(!addr_list);
td::BufferSlice bs(key);
TRY_RESULT_PREFIX(as_json_value, td::json_decode(bs.as_slice()), "bad addr list JSON: ");
TRY_STATUS_PREFIX(td::from_json(addr_list, as_json_value), "bad addr list TL: ");
return td::Status::OK();
});
auto S = p.run(argc, argv);
if (S.is_error()) {
std::cerr << S.move_as_error().message().str() << std::endl;
return 2;
}
if (mode.size() == 0) {
std::cerr << "'-m' option missing" << std::endl;
return 2;
}
if (pk.empty()) {
pk = ton::privkeys::Ed25519::random();
}
auto pub_key = pk.compute_public_key();
auto short_key = pub_key.compute_short_id();
if (mode == "id") {
std::string v;
v = td::json_encode<std::string>(td::ToJson(pk.tl()));
std::cout << v << std::endl;
v = td::json_encode<std::string>(td::ToJson(pub_key.tl()));
std::cout << v << std::endl;
v = td::json_encode<std::string>(td::ToJson(ton::adnl::AdnlNodeIdShort{short_key}.tl()));
std::cout << v << std::endl;
} else if (mode == "adnl") {
if (!addr_list) {
std::cerr << "'-a' option missing" << std::endl;
return 2;
}
auto x = ton::create_tl_object<ton::ton_api::adnl_node>(pub_key.tl(), std::move(addr_list));
auto e = pk.create_decryptor().move_as_ok();
auto r = e->sign(ton::serialize_tl_object(x, true).as_slice()).move_as_ok();
auto v = td::json_encode<std::string>(td::ToJson(x));
std::cout << v << std::endl;
} else if (mode == "dht") {
if (!addr_list) {
std::cerr << "'-a' option missing" << std::endl;
return 2;
}
auto x = ton::create_tl_object<ton::ton_api::dht_node>(pub_key.tl(), std::move(addr_list), -1, td::BufferSlice());
auto e = pk.create_decryptor().move_as_ok();
auto r = e->sign(ton::serialize_tl_object(x, true).as_slice()).move_as_ok();
x->signature_ = std::move(r);
auto v = td::json_encode<std::string>(td::ToJson(x));
std::cout << v << "\n";
} else if (mode == "keys") {
td::write_file(name, pk.export_as_slice()).ensure();
td::write_file(name + ".pub", pub_key.export_as_slice().as_slice()).ensure();
std::cout << short_key.bits256_value().to_hex() << " " << td::base64_encode(short_key.as_slice()) << std::endl;
} else {
std::cerr << "unknown mode " << mode;
return 2;
}
return 0;
}