2018-01-08 00:55:52 +01:00
// Copyright (c) 2017-2018, The Monero Project
2017-06-29 12:48:34 +02:00
//
// 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 [ ] =
{
{
2018-04-27 04:12:17 +02:00
" T6SkvQ3GyyNRnY5UAw3sMZLTExBSCLvpCAP8sdPuPhJ2BjfsUoBes3eGUJx2fZf7S8Fsa7urJ7LamRTt4uucAtqk1wiLrE6zj " ,
2017-06-29 12:48:34 +02:00
" 2dd6e34a234c3e8b5d29a371789e4601e96dee4ea6f7ef79224d1a2d91164c01 "
} ,
{
2018-04-27 04:12:17 +02:00
" T6TfbGiuGXA1Fz3USjnSYJcb771AkkNmqgmoAiHAu5713pewPFyAA9wcmemSiYxWtqcvARA3r4L6QUD7VXodxC7n2WQS5M3q1 " ,
2017-06-29 12:48:34 +02:00
" fac47aecc948ce9d3531aa042abb18235b1df632087c55a361b632ffdd6ede0c "
} ,
{
2018-04-27 04:12:17 +02:00
" T6SLoeEpwLA6emvHeethY1JkPzMHbAAHjQ4hH3zpo5JZBwZX8UxmjPjQG8tyg5HJaKVK6eWHENbPA4fKPw9eEkG71Jgj85miT " ,
2017-06-29 12:48:34 +02:00
" bbd3175ef9fd9f5eefdc43035f882f74ad14c4cf1799d8b6f9001bc197175d02 "
2018-07-12 11:55:52 +02:00
} ,
{
2018-10-11 07:22:20 +02:00
" T6TrTRFMVcKUFcmoSBScYXdhie6sp2b4zaV5VLHFFJv488jRD62zgYcYzzJWeSz2MP1J9g6kDTDNr674CVuWFy1o2JcQW9Zxq " ,
2018-07-12 11:55:52 +02:00
" f2efae45bef1917a7430cda8fcffc4ee010e3178761aa41d4628e23b1fe2d501 "
} ,
{
2018-10-11 07:22:20 +02:00
" T6ShAAdn81gb2eeTyHSuiuLdEedYwnGMK3PxqRekb2Nj3RXNzB7mjxKGt5aucMN3BRJHAstbGdqsi1oL6Es5tTju362tAWn3N " ,
2018-07-12 11:55:52 +02:00
" a4cef54ed3fd61cd78a2ceb82ecf85a903ad2db9a86fb77ff56c35c56016280a "
2017-06-29 12:48:34 +02:00
}
} ;
2018-07-12 11:55:52 +02:00
static const size_t KEYS_COUNT = 5 ;
2017-06-29 12:48:34 +02:00
static void make_wallet ( unsigned int idx , tools : : wallet2 & wallet )
{
ASSERT_TRUE ( idx < sizeof ( test_addresses ) / sizeof ( test_addresses [ 0 ] ) ) ;
crypto : : secret_key spendkey ;
2020-10-23 22:32:28 +02:00
tools : : hex_to_type ( test_addresses [ idx ] . spendkey , spendkey ) ;
2017-06-29 12:48:34 +02:00
try
{
Replace epee http client with curl-based client
In short: epee's http client is garbage, standard violating, and
unreliable.
This completely removes the epee http client support and replaces it
with cpr, a curl-based C++ wrapper. rpc/http_client.h wraps cpr for RPC
requests specifically, but it is also usable directly.
This replacement has a number of advantages:
- requests are considerably more reliable. The epee http client code
assumes that a connection will be kept alive forever, and returns a
failure if a connection is ever closed. This results in some very
annoying things: for example, preparing a transaction and then waiting
a long tim before confirming it will usually result in an error
communication with the daemon. This is just terribly behaviour: the
right thing to do on a connection failure is to resubmit the request.
- epee's http client is broken in lots of other ways: for example, it
tries throwing SSL at the port to see if it is HTTPS, but this is
protocol violating and just breaks (with a several second timeout) on
anything that *isn't* epee http server (for example, when lokid is
behind a proxying server).
- even when it isn't doing the above, the client breaks in other ways:
for example, there is a comment (replaced in this PR) in the Trezor PR
code that forces a connection close after every request because epee's
http client doesn't do proper keep-alive request handling.
- it seems noticeably faster to me in practical use in this PR; both
simple requests (for example, when running `lokid status`) and
wallet<->daemon connections are faster, probably because of crappy
code in epee. (I think this is also related to the throw-ssl-at-it
junk above: the epee client always generates an ssl certificate during
static initialization because it might need one at some point).
- significantly reduces the amount of code we have to maintain.
- removes all the epee ssl option code: curl can handle all of that just
fine.
- removes the epee socks proxy code; curl can handle that just fine.
(And can do more: it also supports using HTTP/HTTPS proxies).
- When a cli wallet connection fails we know show why it failed (which
now is an error message from curl), which could have all sorts of
reasons like hostname resolution failure, bad ssl certificate, etc.
Previously you just got a useless generic error that tells you
nothing.
Other related changes in this PR:
- Drops the check-for-update and download-update code. To the best of
my knowledge these have never been supported in loki-core and so it
didn't seem worth the trouble to convert them to use cpr for the
requests.
- Cleaned up node_rpc_proxy return values: there was an inconsistent mix
of ways to return errors and how the returned strings were handled.
Instead this cleans it up to return a pair<bool, val>, which (with
C++17) can be transparently captured as:
auto [success, val] = node.whatever(req);
This drops the failure message string, but it was almost always set to
something fairly useless (if we want to resurrect it we could easily
change the first element to be a custom type with a bool operator for
success, and a `.error` attribute containing some error string, but
for the most part the current code wasn't doing much useful with the
failure string).
- changed local detection (for automatic trusted daemon determination)
to just look for localhost, and to not try to resolve anything.
Trusting non-public IPs does not work well (e.g. with lokinet where
all .loki addresses resolve to a local IP).
- ssl fingerprint option is removed; this isn't supported by curl
(because it is essentially just duplicating what a custom cainfo
bundle does)
- --daemon-ssl-allow-chained is removed; it wasn't a useful option (if
you don't want chaining, don't specify a cainfo chain).
- --daemon-address is now a URL instead of just host:port. (If you omit
the protocol, http:// is prepended).
- --daemon-host and --daemon-port are now deprecated and produce a
warning (in simplewallet) if used; the replacement is to use
--daemon-address.
- --daemon-ssl is deprecated; specify --daemon-address=https://whatever
instead.
- the above three are now hidden from --help
- reordered the wallet connection options to make more logical sense.
2020-07-26 22:29:49 +02:00
wallet . init ( " " ) ;
2017-10-21 19:31:30 +02:00
wallet . set_subaddress_lookahead ( 1 , 1 ) ;
2017-06-30 13:12:28 +02:00
wallet . generate ( " " , " " , spendkey , true , false ) ;
2022-05-16 22:55:05 +02:00
ASSERT_TRUE ( test_addresses [ idx ] . address = = wallet . get_account ( ) . get_public_address_str ( cryptonote : : network_type : : TESTNET ) ) ;
2018-07-08 22:12:33 +02:00
wallet . decrypt_keys ( " " ) ;
2020-10-23 22:32:28 +02:00
ASSERT_TRUE ( test_addresses [ idx ] . spendkey = = tools : : type_to_hex ( wallet . get_account ( ) . get_keys ( ) . m_spend_secret_key ) ) ;
2018-07-08 22:12:33 +02:00
wallet . encrypt_keys ( " " ) ;
2017-06-29 12:48:34 +02:00
}
catch ( const std : : exception & e )
{
2022-09-13 08:57:56 +02:00
oxen : : log : : error ( globallogcat , " Error creating test wallet: {} " , e . what ( ) ) ;
2017-06-29 12:48:34 +02:00
ASSERT_TRUE ( 0 ) ;
}
}
2018-07-12 11:55:52 +02:00
static std : : vector < std : : string > exchange_round ( std : : vector < tools : : wallet2 > & wallets , const std : : vector < std : : string > & mis )
2017-06-29 12:48:34 +02:00
{
2018-07-12 11:55:52 +02:00
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 ;
2017-06-29 12:48:34 +02:00
}
2018-07-12 11:55:52 +02:00
static void make_wallets ( std : : vector < tools : : wallet2 > & wallets , unsigned int M )
2017-06-29 12:48:34 +02:00
{
2018-07-12 11:55:52 +02:00
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 ] ) ;
wallets [ i ] . decrypt_keys ( " " ) ;
mis [ i ] = wallets [ i ] . get_multisig_info ( ) ;
wallets [ i ] . encrypt_keys ( " " ) ;
}
for ( auto & wallet : wallets ) {
2018-11-23 14:47:51 +01:00
ASSERT_FALSE ( wallet . multisig ( ) ) ;
2018-07-12 11:55:52 +02:00
}
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 ) ) ;
2017-08-13 16:29:31 +02:00
}
2017-06-29 12:48:34 +02:00
2018-07-12 11:55:52 +02:00
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.
2022-05-16 22:55:05 +02:00
ASSERT_TRUE ( wallets [ 0 ] . get_account ( ) . get_public_address_str ( cryptonote : : network_type : : TESTNET ) = = wallets [ i ] . get_account ( ) . get_public_address_str ( cryptonote : : network_type : : TESTNET ) ) ;
2018-07-12 11:55:52 +02:00
}
}
2017-06-29 12:48:34 +02:00
}
TEST ( multisig , make_2_2 )
{
2018-07-12 11:55:52 +02:00
std : : vector < tools : : wallet2 > wallets ( 2 ) ;
make_wallets ( wallets , 2 ) ;
2017-06-29 12:48:34 +02:00
}
TEST ( multisig , make_3_3 )
{
2018-07-12 11:55:52 +02:00
std : : vector < tools : : wallet2 > wallets ( 3 ) ;
make_wallets ( wallets , 3 ) ;
2017-06-29 12:48:34 +02:00
}
TEST ( multisig , make_2_3 )
{
2018-07-12 11:55:52 +02:00
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 ) ;
2017-06-29 12:48:34 +02:00
}