| 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 |
#pragma once |
| 31 |
|
| 32 |
#include <cstdlib> |
| 33 |
#include <string> |
| 34 |
#include <vector> |
| 35 |
#include "crypto/hash.h" |
| 36 |
#include <boost/serialization/vector.hpp> |
| 37 |
#include <boost/program_options/variables_map.hpp> |
| 38 |
#include <boost/program_options/options_description.hpp> |
| 39 |
#include <boost/optional/optional.hpp> |
| 40 |
#include "serialization/serialization.h" |
| 41 |
#include "cryptonote_basic/cryptonote_boost_serialization.h" |
| 42 |
#include "cryptonote_basic/account_boost_serialization.h" |
| 43 |
#include "cryptonote_basic/cryptonote_basic.h" |
| 44 |
#include "common/i18n.h" |
| 45 |
#include "common/command_line.h" |
| 46 |
#include "wipeable_string.h" |
| 47 |
#include "net/abstract_http_client.h" |
| 48 |
#include "serialization/crypto.h" |
| 49 |
#include "serialization/string.h" |
| 50 |
#include "serialization/containers.h" |
| 51 |
#include "message_transporter.h" |
| 52 |
|
| 53 |
#undef MONERO_DEFAULT_LOG_CATEGORY |
| 54 |
#define MONERO_DEFAULT_LOG_CATEGORY "wallet.mms" |
| 55 |
#define AUTO_CONFIG_TOKEN_BYTES 4 |
| 56 |
#define AUTO_CONFIG_TOKEN_PREFIX "mms" |
| 57 |
|
| 58 |
namespace mms |
| 59 |
{ |
| 60 |
enum class message_type |
| 61 |
{ |
| 62 |
key_set, |
| 63 |
additional_key_set, |
| 64 |
multisig_sync_data, |
| 65 |
partially_signed_tx, |
| 66 |
fully_signed_tx, |
| 67 |
note, |
| 68 |
signer_config, |
| 69 |
auto_config_data |
| 70 |
}; |
| 71 |
|
| 72 |
enum class message_direction |
| 73 |
{ |
| 74 |
in, |
| 75 |
out |
| 76 |
}; |
| 77 |
|
| 78 |
enum class message_state |
| 79 |
{ |
| 80 |
ready_to_send, |
| 81 |
sent, |
| 82 |
|
| 83 |
waiting, |
| 84 |
processed, |
| 85 |
|
| 86 |
cancelled |
| 87 |
}; |
| 88 |
|
| 89 |
enum class message_processing |
| 90 |
{ |
| 91 |
prepare_multisig, |
| 92 |
make_multisig, |
| 93 |
exchange_multisig_keys, |
| 94 |
create_sync_data, |
| 95 |
process_sync_data, |
| 96 |
sign_tx, |
| 97 |
send_tx, |
| 98 |
submit_tx, |
| 99 |
process_signer_config, |
| 100 |
process_auto_config_data |
| 101 |
}; |
| 102 |
|
| 103 |
struct message |
| 104 |
{ |
| 105 |
uint32_t id; |
| 106 |
message_type type; |
| 107 |
message_direction direction; |
| 108 |
std::string content; |
| 109 |
uint64_t created; |
| 110 |
uint64_t modified; |
| 111 |
uint64_t sent; |
| 112 |
uint32_t signer_index; |
| 113 |
crypto::hash hash; |
| 114 |
message_state state; |
| 115 |
uint32_t wallet_height; |
| 116 |
uint32_t round; |
| 117 |
uint32_t signature_count; |
| 118 |
std::string transport_id; |
| 119 |
|
| 120 |
BEGIN_SERIALIZE_OBJECT() |
| 121 |
VERSION_FIELD(0) |
| 122 |
VARINT_FIELD(id) |
| 123 |
VARINT_FIELD(type) |
| 124 |
VARINT_FIELD(direction) |
| 125 |
FIELD(content) |
| 126 |
VARINT_FIELD(created) |
| 127 |
VARINT_FIELD(modified) |
| 128 |
VARINT_FIELD(sent) |
| 129 |
VARINT_FIELD(signer_index) |
| 130 |
FIELD(hash) |
| 131 |
VARINT_FIELD(state) |
| 132 |
VARINT_FIELD(wallet_height) |
| 133 |
VARINT_FIELD(round) |
| 134 |
VARINT_FIELD(signature_count) |
| 135 |
FIELD(transport_id) |
| 136 |
END_SERIALIZE() |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
struct authorized_signer |
| 144 |
{ |
| 145 |
std::string label; |
| 146 |
std::string transport_address; |
| 147 |
bool monero_address_known; |
| 148 |
cryptonote::account_public_address monero_address; |
| 149 |
bool me; |
| 150 |
uint32_t index; |
| 151 |
std::string auto_config_token; |
| 152 |
crypto::public_key auto_config_public_key; |
| 153 |
crypto::secret_key auto_config_secret_key; |
| 154 |
std::string auto_config_transport_address; |
| 155 |
bool auto_config_running; |
| 156 |
|
| 157 |
BEGIN_SERIALIZE_OBJECT() |
| 158 |
VERSION_FIELD(0) |
| 159 |
FIELD(label) |
| 160 |
FIELD(transport_address) |
| 161 |
FIELD(monero_address_known) |
| 162 |
FIELD(monero_address) |
| 163 |
FIELD(me) |
| 164 |
VARINT_FIELD(index) |
| 165 |
FIELD(auto_config_token) |
| 166 |
FIELD(auto_config_public_key) |
| 167 |
FIELD(auto_config_secret_key) |
| 168 |
FIELD(auto_config_transport_address) |
| 169 |
FIELD(auto_config_running) |
| 170 |
END_SERIALIZE() |
| 171 |
|
| 172 |
authorized_signer() |
| 173 |
{ |
| 174 |
monero_address_known = false; |
| 175 |
memset(&monero_address, 0, sizeof(cryptonote::account_public_address)); |
| 176 |
me = false; |
| 177 |
index = 0; |
| 178 |
auto_config_public_key = crypto::null_pkey; |
| 179 |
auto_config_secret_key = crypto::null_skey; |
| 180 |
auto_config_running = false; |
| 181 |
}; |
| 182 |
}; |
| 183 |
|
| 184 |
struct processing_data |
| 185 |
{ |
| 186 |
message_processing processing; |
| 187 |
std::vector<uint32_t> message_ids; |
| 188 |
uint32_t receiving_signer_index = 0; |
| 189 |
}; |
| 190 |
|
| 191 |
struct file_transport_message |
| 192 |
{ |
| 193 |
cryptonote::account_public_address sender_address; |
| 194 |
crypto::chacha_iv iv; |
| 195 |
crypto::public_key encryption_public_key; |
| 196 |
message internal_message; |
| 197 |
}; |
| 198 |
|
| 199 |
struct auto_config_data |
| 200 |
{ |
| 201 |
std::string label; |
| 202 |
std::string transport_address; |
| 203 |
cryptonote::account_public_address monero_address; |
| 204 |
|
| 205 |
BEGIN_SERIALIZE_OBJECT() |
| 206 |
VERSION_FIELD(0) |
| 207 |
FIELD(label) |
| 208 |
FIELD(transport_address) |
| 209 |
FIELD(monero_address) |
| 210 |
END_SERIALIZE() |
| 211 |
}; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
struct file_data |
| 216 |
{ |
| 217 |
std::string magic_string; |
| 218 |
uint32_t file_version; |
| 219 |
crypto::chacha_iv iv; |
| 220 |
std::string encrypted_data; |
| 221 |
|
| 222 |
BEGIN_SERIALIZE_OBJECT() |
| 223 |
FIELD(magic_string) |
| 224 |
FIELD(file_version) |
| 225 |
FIELD(iv) |
| 226 |
FIELD(encrypted_data) |
| 227 |
END_SERIALIZE() |
| 228 |
}; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
struct multisig_wallet_state |
| 242 |
{ |
| 243 |
cryptonote::account_public_address address; |
| 244 |
cryptonote::network_type nettype; |
| 245 |
crypto::secret_key view_secret_key; |
| 246 |
bool multisig; |
| 247 |
bool multisig_is_ready; |
| 248 |
bool has_multisig_partial_key_images; |
| 249 |
uint32_t multisig_rounds_passed; |
| 250 |
size_t num_transfer_details; |
| 251 |
std::string mms_file; |
| 252 |
|
| 253 |
BEGIN_SERIALIZE_OBJECT() |
| 254 |
VERSION_FIELD(0) |
| 255 |
FIELD(address) |
| 256 |
VARINT_FIELD(nettype) |
| 257 |
FIELD(view_secret_key) |
| 258 |
FIELD(multisig) |
| 259 |
FIELD(multisig_is_ready) |
| 260 |
FIELD(has_multisig_partial_key_images) |
| 261 |
VARINT_FIELD(multisig_rounds_passed) |
| 262 |
VARINT_FIELD(num_transfer_details) |
| 263 |
FIELD(mms_file) |
| 264 |
END_SERIALIZE() |
| 265 |
}; |
| 266 |
|
| 267 |
class message_store |
| 268 |
{ |
| 269 |
public: |
| 270 |
message_store(std::unique_ptr<epee::net_utils::http::abstract_http_client> http_client); |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
void init(const multisig_wallet_state &state, const std::string &own_label, |
| 276 |
const std::string &own_transport_address, uint32_t num_authorized_signers, uint32_t num_required_signers); |
| 277 |
void set_active(bool active) { m_active = active; }; |
| 278 |
void set_auto_send(bool auto_send) { m_auto_send = auto_send; }; |
| 279 |
void set_options(const boost::program_options::variables_map& vm); |
| 280 |
void set_options(const std::string &bitmessage_address, const epee::wipeable_string &bitmessage_login); |
| 281 |
bool get_active() const { return m_active; }; |
| 282 |
bool get_auto_send() const { return m_auto_send; }; |
| 283 |
uint32_t get_num_required_signers() const { return m_num_required_signers; }; |
| 284 |
uint32_t get_num_authorized_signers() const { return m_num_authorized_signers; }; |
| 285 |
|
| 286 |
void set_signer(const multisig_wallet_state &state, |
| 287 |
uint32_t index, |
| 288 |
const boost::optional<std::string> &label, |
| 289 |
const boost::optional<std::string> &transport_address, |
| 290 |
const boost::optional<cryptonote::account_public_address> monero_address); |
| 291 |
|
| 292 |
const authorized_signer &get_signer(uint32_t index) const; |
| 293 |
bool get_signer_index_by_monero_address(const cryptonote::account_public_address &monero_address, uint32_t &index) const; |
| 294 |
bool get_signer_index_by_label(const std::string label, uint32_t &index) const; |
| 295 |
const std::vector<authorized_signer> &get_all_signers() const { return m_signers; }; |
| 296 |
bool signer_config_complete() const; |
| 297 |
bool signer_labels_complete() const; |
| 298 |
void get_signer_config(std::string &signer_config); |
| 299 |
void unpack_signer_config(const multisig_wallet_state &state, const std::string &signer_config, |
| 300 |
std::vector<authorized_signer> &signers); |
| 301 |
void process_signer_config(const multisig_wallet_state &state, const std::string &signer_config); |
| 302 |
|
| 303 |
void start_auto_config(const multisig_wallet_state &state); |
| 304 |
bool check_auto_config_token(const std::string &raw_token, |
| 305 |
std::string &adjusted_token) const; |
| 306 |
size_t add_auto_config_data_message(const multisig_wallet_state &state, |
| 307 |
const std::string &auto_config_token); |
| 308 |
void process_auto_config_data_message(uint32_t id); |
| 309 |
std::string get_config_checksum() const; |
| 310 |
void stop_auto_config(); |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
void process_wallet_created_data(const multisig_wallet_state &state, message_type type, const std::string &content); |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
bool get_processable_messages(const multisig_wallet_state &state, |
| 329 |
bool force_sync, |
| 330 |
std::vector<processing_data> &data_list, |
| 331 |
std::string &wait_reason); |
| 332 |
void set_messages_processed(const processing_data &data); |
| 333 |
|
| 334 |
size_t add_message(const multisig_wallet_state &state, |
| 335 |
uint32_t signer_index, message_type type, message_direction direction, |
| 336 |
const std::string &content); |
| 337 |
const std::vector<message> &get_all_messages() const { return m_messages; }; |
| 338 |
bool get_message_by_id(uint32_t id, message &m) const; |
| 339 |
message get_message_by_id(uint32_t id) const; |
| 340 |
void set_message_processed_or_sent(uint32_t id); |
| 341 |
void delete_message(uint32_t id); |
| 342 |
void delete_all_messages(); |
| 343 |
static std::string get_sanitized_text(const std::string &text, size_t max_length); |
| 344 |
|
| 345 |
void send_message(const multisig_wallet_state &state, uint32_t id); |
| 346 |
bool check_for_messages(const multisig_wallet_state &state, std::vector<message> &messages); |
| 347 |
void stop() { m_run.store(false, std::memory_order_relaxed); m_transporter.stop(); } |
| 348 |
|
| 349 |
void write_to_file(const multisig_wallet_state &state, const std::string &filename); |
| 350 |
void read_from_file(const multisig_wallet_state &state, const std::string &filename, bool load_deprecated_formats = false); |
| 351 |
|
| 352 |
template <class t_archive> |
| 353 |
inline void serialize(t_archive &a, const unsigned int ver) |
| 354 |
{ |
| 355 |
a & m_active; |
| 356 |
a & m_num_authorized_signers; |
| 357 |
a & m_nettype; |
| 358 |
a & m_num_required_signers; |
| 359 |
a & m_signers; |
| 360 |
a & m_messages; |
| 361 |
a & m_next_message_id; |
| 362 |
a & m_auto_send; |
| 363 |
} |
| 364 |
|
| 365 |
BEGIN_SERIALIZE_OBJECT() |
| 366 |
VERSION_FIELD(0) |
| 367 |
FIELD(m_active) |
| 368 |
VARINT_FIELD(m_num_authorized_signers) |
| 369 |
VARINT_FIELD(m_nettype) |
| 370 |
VARINT_FIELD(m_num_required_signers) |
| 371 |
FIELD(m_signers) |
| 372 |
FIELD(m_messages) |
| 373 |
VARINT_FIELD(m_next_message_id) |
| 374 |
FIELD(m_auto_send) |
| 375 |
END_SERIALIZE() |
| 376 |
|
| 377 |
static const char* message_type_to_string(message_type type); |
| 378 |
static const char* message_direction_to_string(message_direction direction); |
| 379 |
static const char* message_state_to_string(message_state state); |
| 380 |
std::string signer_to_string(const authorized_signer &signer, uint32_t max_width); |
| 381 |
|
| 382 |
static const char *tr(const char *str) { return i18n_translate(str, "tools::mms"); } |
| 383 |
static void init_options(boost::program_options::options_description& desc_params); |
| 384 |
|
| 385 |
private: |
| 386 |
bool m_active; |
| 387 |
uint32_t m_num_authorized_signers; |
| 388 |
uint32_t m_num_required_signers; |
| 389 |
bool m_auto_send; |
| 390 |
cryptonote::network_type m_nettype; |
| 391 |
std::vector<authorized_signer> m_signers; |
| 392 |
std::vector<message> m_messages; |
| 393 |
uint32_t m_next_message_id; |
| 394 |
std::string m_filename; |
| 395 |
message_transporter m_transporter; |
| 396 |
std::atomic<bool> m_run; |
| 397 |
|
| 398 |
bool get_message_index_by_id(uint32_t id, size_t &index) const; |
| 399 |
size_t get_message_index_by_id(uint32_t id) const; |
| 400 |
message& get_message_ref_by_id(uint32_t id); |
| 401 |
bool any_message_of_type(message_type type, message_direction direction) const; |
| 402 |
bool any_message_with_hash(const crypto::hash &hash) const; |
| 403 |
size_t get_other_signers_id_count(const std::vector<uint32_t> &ids) const; |
| 404 |
bool message_ids_complete(const std::vector<uint32_t> &ids) const; |
| 405 |
void encrypt(crypto::public_key public_key, const std::string &plaintext, |
| 406 |
std::string &ciphertext, crypto::public_key &encryption_public_key, crypto::chacha_iv &iv); |
| 407 |
void decrypt(const std::string &ciphertext, const crypto::public_key &encryption_public_key, const crypto::chacha_iv &iv, |
| 408 |
const crypto::secret_key &view_secret_key, std::string &plaintext); |
| 409 |
std::string create_auto_config_token(); |
| 410 |
void setup_signer_for_auto_config(uint32_t index, const std::string token, bool receiving); |
| 411 |
void delete_transport_message(uint32_t id); |
| 412 |
std::string account_address_to_string(const cryptonote::account_public_address &account_address) const; |
| 413 |
void save(const multisig_wallet_state &state); |
| 414 |
}; |
| 415 |
} |
| 416 |
|
| 417 |
BOOST_CLASS_VERSION(mms::file_data, 0) |
| 418 |
BOOST_CLASS_VERSION(mms::message_store, 0) |
| 419 |
BOOST_CLASS_VERSION(mms::message, 0) |
| 420 |
BOOST_CLASS_VERSION(mms::file_transport_message, 0) |
| 421 |
BOOST_CLASS_VERSION(mms::authorized_signer, 1) |
| 422 |
BOOST_CLASS_VERSION(mms::auto_config_data, 0) |
| 423 |
|
| 424 |
namespace boost |
| 425 |
{ |
| 426 |
namespace serialization |
| 427 |
{ |
| 428 |
template <class Archive> |
| 429 |
inline void serialize(Archive &a, mms::file_data &x, const boost::serialization::version_type ver) |
| 430 |
{ |
| 431 |
a & x.magic_string; |
| 432 |
a & x.file_version; |
| 433 |
a & x.iv; |
| 434 |
a & x.encrypted_data; |
| 435 |
} |
| 436 |
|
| 437 |
template <class Archive> |
| 438 |
inline void serialize(Archive &a, mms::message &x, const boost::serialization::version_type ver) |
| 439 |
{ |
| 440 |
a & x.id; |
| 441 |
a & x.type; |
| 442 |
a & x.direction; |
| 443 |
a & x.content; |
| 444 |
a & x.created; |
| 445 |
a & x.modified; |
| 446 |
a & x.sent; |
| 447 |
a & x.signer_index; |
| 448 |
a & x.hash; |
| 449 |
a & x.state; |
| 450 |
a & x.wallet_height; |
| 451 |
a & x.round; |
| 452 |
a & x.signature_count; |
| 453 |
a & x.transport_id; |
| 454 |
} |
| 455 |
|
| 456 |
template <class Archive> |
| 457 |
inline void serialize(Archive &a, mms::authorized_signer &x, const boost::serialization::version_type ver) |
| 458 |
{ |
| 459 |
a & x.label; |
| 460 |
a & x.transport_address; |
| 461 |
a & x.monero_address_known; |
| 462 |
a & x.monero_address; |
| 463 |
a & x.me; |
| 464 |
a & x.index; |
| 465 |
if (ver < 1) |
| 466 |
{ |
| 467 |
return; |
| 468 |
} |
| 469 |
a & x.auto_config_token; |
| 470 |
a & x.auto_config_public_key; |
| 471 |
a & x.auto_config_secret_key; |
| 472 |
a & x.auto_config_transport_address; |
| 473 |
a & x.auto_config_running; |
| 474 |
} |
| 475 |
|
| 476 |
template <class Archive> |
| 477 |
inline void serialize(Archive &a, mms::auto_config_data &x, const boost::serialization::version_type ver) |
| 478 |
{ |
| 479 |
a & x.label; |
| 480 |
a & x.transport_address; |
| 481 |
a & x.monero_address; |
| 482 |
} |
| 483 |
|
| 484 |
template <class Archive> |
| 485 |
inline void serialize(Archive &a, mms::file_transport_message &x, const boost::serialization::version_type ver) |
| 486 |
{ |
| 487 |
a & x.sender_address; |
| 488 |
a & x.iv; |
| 489 |
a & x.encryption_public_key; |
| 490 |
a & x.internal_message; |
| 491 |
} |
| 492 |
|
| 493 |
template <class Archive> |
| 494 |
inline void serialize(Archive &a, crypto::chacha_iv &x, const boost::serialization::version_type ver) |
| 495 |
{ |
| 496 |
a & x.data; |
| 497 |
} |
| 498 |
|
| 499 |
} |
| 500 |
} |