townforge/tests/unit_tests/multisig.cpp
Crypto City 5e1f9e4b8b wallet2: default to not encrypt keys in memory - faster
For a game, it's the better tradeoff
2021-07-19 11:51:19 +00:00

177 lines
6.0 KiB
C++

// Copyright (c) 2017-2020, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "gtest/gtest.h"
#include <cstdint>
#include "wallet/wallet2.h"
static const struct
{
const char *address;
const char *spendkey;
} test_addresses[] =
{
{
"TF1MMBg4zx18SnZC6oswCRB7DzdVeUKce5NdCMSWUHNY4wNvNhzmFj4WqZY2bFj8R1REAWR3qAH5zD7sjXyHz3tVayzHSswqymx",
"61ad65a28521979232432458f042e667e0c62c751b64e06bc65ffc5fbb49cc01"
},
{
"TF1MM5mG6EQkz899pz3uFDR6D2EUowvZWw75hcE6TETrodxHXSKFK6u3SRtEQzJ6epc5HD85dEgYF7xhgBtoFjMHKNFAEuGg1Lk",
"566a63caed9f5a58d5cbcc6a4d02eccd77578dd97aea900b27b502b1b138610e"
},
{
"TF1MMCHHDFKYSQ1DkLWskTxU2fC7jtZLwcATXHP3uvJo4dV757dtGwUE9ZL6SE9DJhxrvzThAsG4drVmAPHzEvh6TCq19AbzTo4",
"f21a80765bc58d7c53fa32b8a2ae948fe91d28902f003bf67d0f7bd94545b80b"
},
{
"TF1MMA2uV97T4fgC7EXcMYD7q7hhGvkocKX8TV8iCxy3qEEJg4ySPjsEzfjU1tpop44L8YKv2464s6UhL9HevTTvmHYf7F71JZ2",
"4f28f92f703136aa4fb41f4e816fe716c2ac1e8c914144891bbaaca349fab500"
},
{
"TF1MM8gSyksuYZbejbkhmWnwZNhAAUXXF3p8j47zjhGHKvNi5Ghp4hmFC1MH7MFYVQPnuACeJDmzqxW8TRp7jMLaHi9XFVvohwX",
"3836d0d3321b2d9919db24fe50d9cb18324755ec675d3c6db160f3861049320e"
}
};
static const size_t KEYS_COUNT = 5;
static void make_wallet(unsigned int idx, tools::wallet2 &wallet)
{
ASSERT_TRUE(idx < sizeof(test_addresses) / sizeof(test_addresses[0]));
crypto::secret_key spendkey;
epee::string_tools::hex_to_pod(test_addresses[idx].spendkey, spendkey);
try
{
wallet.init("", boost::none, "", 0, true, epee::net_utils::ssl_support_t::e_ssl_support_disabled);
wallet.set_subaddress_lookahead(1, 1);
wallet.generate("", "", spendkey, true, false);
ASSERT_TRUE(test_addresses[idx].address == wallet.get_account().get_public_address_str(cryptonote::MAINNET));
if (wallet.ask_password() == tools::wallet2::AskPasswordToDecrypt)
wallet.decrypt_keys("");
ASSERT_TRUE(test_addresses[idx].spendkey == epee::string_tools::pod_to_hex(wallet.get_account().get_keys().m_spend_secret_key));
if (wallet.ask_password() == tools::wallet2::AskPasswordToDecrypt)
wallet.encrypt_keys("");
}
catch (const std::exception &e)
{
MFATAL("Error creating test wallet: " << e.what());
ASSERT_TRUE(0);
}
}
static std::vector<std::string> exchange_round(std::vector<tools::wallet2>& wallets, const std::vector<std::string>& mis)
{
std::vector<std::string> new_infos;
for (size_t i = 0; i < wallets.size(); ++i) {
new_infos.push_back(wallets[i].exchange_multisig_keys("", mis));
}
return new_infos;
}
static void make_wallets(std::vector<tools::wallet2>& wallets, unsigned int M)
{
ASSERT_TRUE(wallets.size() > 1 && wallets.size() <= KEYS_COUNT);
ASSERT_TRUE(M <= wallets.size());
std::vector<std::string> mis(wallets.size());
for (size_t i = 0; i < wallets.size(); ++i) {
make_wallet(i, wallets[i]);
if (wallets[i].ask_password() == tools::wallet2::AskPasswordToDecrypt)
wallets[i].decrypt_keys("");
mis[i] = wallets[i].get_multisig_info();
if (wallets[i].ask_password() == tools::wallet2::AskPasswordToDecrypt)
wallets[i].encrypt_keys("");
}
for (auto& wallet: wallets) {
ASSERT_FALSE(wallet.multisig());
}
std::vector<std::string> mxis;
for (size_t i = 0; i < wallets.size(); ++i) {
// it's ok to put all of multisig keys in this function. it throws in case of error
mxis.push_back(wallets[i].make_multisig("", mis, M));
}
while (!mxis[0].empty()) {
mxis = exchange_round(wallets, mxis);
}
for (size_t i = 0; i < wallets.size(); ++i) {
ASSERT_TRUE(mxis[i].empty());
bool ready;
uint32_t threshold, total;
ASSERT_TRUE(wallets[i].multisig(&ready, &threshold, &total));
ASSERT_TRUE(ready);
ASSERT_TRUE(threshold == M);
ASSERT_TRUE(total == wallets.size());
if (i != 0) {
// "equals" is transitive relation so we need only to compare first wallet's address to each others' addresses. no need to compare 0's address with itself.
ASSERT_TRUE(wallets[0].get_account().get_public_address_str(cryptonote::MAINNET) == wallets[i].get_account().get_public_address_str(cryptonote::MAINNET));
}
}
}
TEST(multisig, make_2_2)
{
std::vector<tools::wallet2> wallets(2);
make_wallets(wallets, 2);
}
TEST(multisig, make_3_3)
{
std::vector<tools::wallet2> wallets(3);
make_wallets(wallets, 3);
}
TEST(multisig, make_2_3)
{
std::vector<tools::wallet2> wallets(3);
make_wallets(wallets, 2);
}
TEST(multisig, make_2_4)
{
std::vector<tools::wallet2> wallets(4);
make_wallets(wallets, 2);
}
TEST(multisig, make_2_5)
{
std::vector<tools::wallet2> wallets(5);
make_wallets(wallets, 2);
}