Debian Codegraph @

src/gen_multisig/gen_multisig.cpp

raw symbols SCIP
1 // Copyright (c) 2017-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31 /*!
32 * \file gen_multisig.cpp
33 *
34 * \brief Generates a set of multisig wallets
35 */
36 #include <iostream>
37 #include <sstream>
38 #include <boost/program_options.hpp>
39 #include <boost/algorithm/string.hpp>
40 #include <boost/format.hpp>
41 #include "include_base_utils.h"
42 #include "crypto/crypto.h" // for crypto::secret_key definition
43 #include "common/i18n.h"
44 #include "common/command_line.h"
45 #include "common/util.h"
46 #include "common/scoped_message_writer.h"
47 #include "wallet/wallet_args.h"
48 #include "wallet/wallet2.h"
49
50 using namespace std;
51 using namespace epee;
52 using namespace cryptonote;
53 namespace po = boost::program_options;
54
55 #undef MONERO_DEFAULT_LOG_CATEGORY
56 #define MONERO_DEFAULT_LOG_CATEGORY "wallet.gen_multisig"
57
58 namespace genms
59 {
60 const char* tr(const char* str)
61 {
62 return i18n_translate(str, "tools::gen_multisig");
63 }
64
65 }
66
67 namespace
68 {
69 const command_line::arg_descriptor<std::string> arg_filename_base = {"filename-base", genms::tr("Base filename (-1, -2, etc suffixes will be appended as needed)"), ""};
70 const command_line::arg_descriptor<std::string> arg_scheme = {"scheme", genms::tr("Give threshold and participants at once as M/N"), ""};
71 const command_line::arg_descriptor<uint32_t> arg_participants = {"participants", genms::tr("How many participants will share parts of the multisig wallet"), 0};
72 const command_line::arg_descriptor<uint32_t> arg_threshold = {"threshold", genms::tr("How many signers are required to sign a valid transaction"), 0};
73 const command_line::arg_descriptor<bool, false> arg_testnet = {"testnet", genms::tr("Create testnet multisig wallets"), false};
74 const command_line::arg_descriptor<bool, false> arg_stagenet = {"stagenet", genms::tr("Create stagenet multisig wallets"), false};
75 const command_line::arg_descriptor<bool, false> arg_create_address_file = {"create-address-file", genms::tr("Create an address file for new wallets"), false};
76 }
77
78 static bool generate_multisig(uint32_t threshold, uint32_t total, const std::string &basename, network_type nettype, bool create_address_file)
79 {
80 tools::msg_writer() << (boost::format(genms::tr("Generating %u %u/%u multisig wallets")) % total % threshold % total).str();
81
82 const auto pwd_container = tools::password_container::prompt(true, "Enter password for new multisig wallets");
83
84 try
85 {
86 if (total == 0)
87 throw std::runtime_error("Signer group of size 0 is not allowed.");
88
89 // create M wallets first
90 std::vector<boost::shared_ptr<tools::wallet2>> wallets(total);
91 for (size_t n = 0; n < total; ++n)
92 {
93 std::string name = basename + "-" + std::to_string(n + 1);
94 wallets[n].reset(new tools::wallet2(nettype, 1, false));
95 wallets[n]->init("");
96 wallets[n]->generate(name, pwd_container->password(), rct::rct2sk(rct::skGen()), false, false, create_address_file);
97 }
98
99 // gather the keys
100 std::vector<std::string> first_round_msgs;
101 first_round_msgs.reserve(total);
102 for (size_t n = 0; n < total; ++n)
103 {
104 wallets[n]->decrypt_keys(pwd_container->password());
105
106 first_round_msgs.emplace_back(wallets[n]->get_multisig_first_kex_msg());
107
108 wallets[n]->encrypt_keys(pwd_container->password());
109 }
110
111 // make the wallets multisig
112 std::vector<std::string> kex_msgs_intermediate(total);
113 std::stringstream ss;
114 for (size_t n = 0; n < total; ++n)
115 {
116 std::string name = basename + "-" + std::to_string(n + 1);
117
118 kex_msgs_intermediate[n] = wallets[n]->make_multisig(pwd_container->password(), first_round_msgs, threshold);
119
120 ss << " " << name << std::endl;
121 }
122
123 // exchange keys until the wallets are done
124 bool ready{false};
125 wallets[0]->multisig(&ready);
126 while (!ready)
127 {
128 std::vector<std::string> kex_msgs_for_round(total);
129
130 for (size_t n = 0; n < total; ++n)
131 {
132 kex_msgs_for_round[n] = wallets[n]->exchange_multisig_keys(pwd_container->password(), kex_msgs_intermediate);
133 }
134
135 // Update messages for next round
136 kex_msgs_intermediate = kex_msgs_for_round;
137
138 wallets[0]->multisig(&ready);
139 }
140
141 std::string address = wallets[0]->get_account().get_public_address_str(wallets[0]->nettype());
142 tools::success_msg_writer() << genms::tr("Generated multisig wallets for address ") << address << std::endl << ss.str();
143 }
144 catch (const std::exception &e)
145 {
146 tools::fail_msg_writer() << genms::tr("Error creating multisig wallets: ") << e.what();
147 return false;
148 }
149
150 return true;
151 }
152
153 int main(int argc, char* argv[])
154 {
155 TRY_ENTRY();
156
157 po::options_description desc_params(wallet_args::tr("Wallet options"));
158 command_line::add_arg(desc_params, arg_filename_base);
159 command_line::add_arg(desc_params, arg_scheme);
160 command_line::add_arg(desc_params, arg_threshold);
161 command_line::add_arg(desc_params, arg_participants);
162 command_line::add_arg(desc_params, arg_testnet);
163 command_line::add_arg(desc_params, arg_stagenet);
164 command_line::add_arg(desc_params, arg_create_address_file);
165
166 boost::optional<po::variables_map> vm;
167 bool should_terminate = false;
168 std::tie(vm, should_terminate) = wallet_args::main(
169 argc, argv,
170 "monero-gen-multisig [(--testnet|--stagenet)] [--filename-base=<filename>] [--scheme=M/N] [--threshold=M] [--participants=N]",
171 genms::tr("This program generates a set of multisig wallets - use this simpler scheme only if all the participants trust each other"),
172 desc_params,
173 boost::program_options::positional_options_description(),
174 [](const std::string &s, bool emphasis){ tools::scoped_message_writer(emphasis ? epee::console_color_white : epee::console_color_default, true) << s; },
175 "monero-gen-multisig.log"
176 );
177 if (!vm)
178 return 1;
179 if (should_terminate)
180 return 0;
181
182 bool testnet, stagenet;
183 uint32_t threshold = 0, total = 0;
184 std::string basename;
185
186 testnet = command_line::get_arg(*vm, arg_testnet);
187 stagenet = command_line::get_arg(*vm, arg_stagenet);
188 if (testnet && stagenet)
189 {
190 tools::fail_msg_writer() << genms::tr("Error: Can't specify more than one of --testnet and --stagenet");
191 return 1;
192 }
193 if (command_line::has_arg(*vm, arg_scheme))
194 {
195 if (sscanf(command_line::get_arg(*vm, arg_scheme).c_str(), "%u/%u", &threshold, &total) != 2)
196 {
197 tools::fail_msg_writer() << genms::tr("Error: expected N/M, but got: ") << command_line::get_arg(*vm, arg_scheme);
198 return 1;
199 }
200 }
201 if (!(*vm)["threshold"].defaulted())
202 {
203 if (threshold)
204 {
205 tools::fail_msg_writer() << genms::tr("Error: either --scheme or both of --threshold and --participants may be given");
206 return 1;
207 }
208 threshold = command_line::get_arg(*vm, arg_threshold);
209 }
210 if (!(*vm)["participants"].defaulted())
211 {
212 if (total)
213 {
214 tools::fail_msg_writer() << genms::tr("Error: either --scheme or both of --threshold and --participants may be given");
215 return 1;
216 }
217 total = command_line::get_arg(*vm, arg_participants);
218 }
219 if (threshold <= 1 || threshold > total)
220 {
221 tools::fail_msg_writer() << (boost::format(genms::tr("Error: expected N > 1 and N <= M, but got N==%u and M==%d")) % threshold % total).str();
222 return 1;
223 }
224 if (!(*vm)["filename-base"].defaulted() && !command_line::get_arg(*vm, arg_filename_base).empty())
225 {
226 basename = command_line::get_arg(*vm, arg_filename_base);
227 }
228 else
229 {
230 tools::fail_msg_writer() << genms::tr("Error: --filename-base is required");
231 return 1;
232 }
233
234 bool create_address_file = command_line::get_arg(*vm, arg_create_address_file);
235 if (!generate_multisig(threshold, total, basename, testnet ? TESTNET : stagenet ? STAGENET : MAINNET, create_address_file))
236 return 1;
237
238 return 0;
239 CATCH_ENTRY_L0("main", 1);
240 }