Add CashAddr Address Format
Ported from Bitcoin Unlimited, Bitcoin ABC
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include "rpc/client.h"
|
||||
|
||||
#include "base58.h"
|
||||
#include "dstencode.h"
|
||||
#include "main.h"
|
||||
#include "wallet/wallet.h"
|
||||
|
||||
@@ -35,18 +36,17 @@ BOOST_AUTO_TEST_CASE(rpc_addmultisig)
|
||||
const char address2Hex[] = "0388c2037017c62240b6b72ac1a2a5f94da790596ebd06177c8572752922165cb4";
|
||||
|
||||
UniValue v;
|
||||
CBitcoinAddress address;
|
||||
BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex), false));
|
||||
address.SetString(v.get_str());
|
||||
BOOST_CHECK(address.IsValid() && address.IsScript());
|
||||
CTxDestination address = DecodeDestination(v.get_str());
|
||||
BOOST_CHECK(IsValidDestination(address));
|
||||
|
||||
BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex, address2Hex), false));
|
||||
address.SetString(v.get_str());
|
||||
BOOST_CHECK(address.IsValid() && address.IsScript());
|
||||
address = DecodeDestination(v.get_str());
|
||||
BOOST_CHECK(IsValidDestination(address));
|
||||
|
||||
BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(2, address1Hex, address2Hex), false));
|
||||
address.SetString(v.get_str());
|
||||
BOOST_CHECK(address.IsValid() && address.IsScript());
|
||||
address = DecodeDestination(v.get_str());
|
||||
BOOST_CHECK(IsValidDestination(address));
|
||||
|
||||
BOOST_CHECK_THROW(addmultisig(createArgs(0), false), runtime_error);
|
||||
BOOST_CHECK_THROW(addmultisig(createArgs(1), false), runtime_error);
|
||||
@@ -67,15 +67,15 @@ BOOST_AUTO_TEST_CASE(rpc_wallet)
|
||||
// Test RPC calls for various wallet statistics
|
||||
UniValue r;
|
||||
CPubKey demoPubkey;
|
||||
CBitcoinAddress demoAddress;
|
||||
CTxDestination demoAddress;
|
||||
UniValue retValue;
|
||||
string strAccount = "walletDemoAccount";
|
||||
CBitcoinAddress setaccountDemoAddress;
|
||||
CTxDestination setaccountDemoAddress;
|
||||
{
|
||||
LOCK(pwalletMain->cs_wallet);
|
||||
|
||||
demoPubkey = pwalletMain->GenerateNewKey();
|
||||
demoAddress = CBitcoinAddress(CTxDestination(demoPubkey.GetID()));
|
||||
demoAddress = CTxDestination(demoPubkey.GetID());
|
||||
string strPurpose = "receive";
|
||||
BOOST_CHECK_NO_THROW({ /*Initialize Wallet with an account */
|
||||
CWalletDB walletdb(pwalletMain->strWalletFile);
|
||||
@@ -86,12 +86,12 @@ BOOST_AUTO_TEST_CASE(rpc_wallet)
|
||||
});
|
||||
|
||||
CPubKey setaccountDemoPubkey = pwalletMain->GenerateNewKey();
|
||||
setaccountDemoAddress = CBitcoinAddress(CTxDestination(setaccountDemoPubkey.GetID()));
|
||||
setaccountDemoAddress = CTxDestination(setaccountDemoPubkey.GetID());
|
||||
}
|
||||
/*********************************
|
||||
* setaccount
|
||||
*********************************/
|
||||
BOOST_CHECK_NO_THROW(CallRPC("setaccount " + setaccountDemoAddress.ToString() + " nullaccount"));
|
||||
BOOST_CHECK_NO_THROW(CallRPC("setaccount " + EncodeDestination(setaccountDemoAddress) + " nullaccount"));
|
||||
/* 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XZ is not owned by the test wallet. */
|
||||
BOOST_CHECK_THROW(CallRPC("setaccount 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XZ nullaccount"), runtime_error);
|
||||
BOOST_CHECK_THROW(CallRPC("setaccount"), runtime_error);
|
||||
@@ -103,7 +103,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet)
|
||||
* getbalance
|
||||
*********************************/
|
||||
BOOST_CHECK_NO_THROW(CallRPC("getbalance"));
|
||||
BOOST_CHECK_NO_THROW(CallRPC("getbalance " + demoAddress.ToString()));
|
||||
BOOST_CHECK_NO_THROW(CallRPC("getbalance " + EncodeDestination(demoAddress)));
|
||||
|
||||
/*********************************
|
||||
* listunspent
|
||||
@@ -145,10 +145,10 @@ BOOST_AUTO_TEST_CASE(rpc_wallet)
|
||||
* listtransactions
|
||||
*********************************/
|
||||
BOOST_CHECK_NO_THROW(CallRPC("listtransactions"));
|
||||
BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString()));
|
||||
BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " 20"));
|
||||
BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " 20 0"));
|
||||
BOOST_CHECK_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " not_int"), runtime_error);
|
||||
BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + EncodeDestination(demoAddress)));
|
||||
BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + EncodeDestination(demoAddress) + " 20"));
|
||||
BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + EncodeDestination(demoAddress) + " 20 0"));
|
||||
BOOST_CHECK_THROW(CallRPC("listtransactions " + EncodeDestination(demoAddress) + " not_int"), runtime_error);
|
||||
|
||||
/*********************************
|
||||
* listlockunspent
|
||||
@@ -182,33 +182,33 @@ BOOST_AUTO_TEST_CASE(rpc_wallet)
|
||||
BOOST_CHECK_NO_THROW(CallRPC("getaccountaddress \"\""));
|
||||
BOOST_CHECK_NO_THROW(CallRPC("getaccountaddress accountThatDoesntExists")); // Should generate a new account
|
||||
BOOST_CHECK_NO_THROW(retValue = CallRPC("getaccountaddress " + strAccount));
|
||||
BOOST_CHECK(CBitcoinAddress(retValue.get_str()).Get() == demoAddress.Get());
|
||||
BOOST_CHECK(retValue.get_str() == EncodeDestination(demoAddress));
|
||||
|
||||
/*********************************
|
||||
* getaccount
|
||||
*********************************/
|
||||
BOOST_CHECK_THROW(CallRPC("getaccount"), runtime_error);
|
||||
BOOST_CHECK_NO_THROW(CallRPC("getaccount " + demoAddress.ToString()));
|
||||
BOOST_CHECK_NO_THROW(CallRPC("getaccount " + EncodeDestination(demoAddress)));
|
||||
|
||||
/*********************************
|
||||
* signmessage + verifymessage
|
||||
*********************************/
|
||||
BOOST_CHECK_NO_THROW(retValue = CallRPC("signmessage " + demoAddress.ToString() + " mymessage"));
|
||||
BOOST_CHECK_NO_THROW(retValue = CallRPC("signmessage " + EncodeDestination(demoAddress) + " mymessage"));
|
||||
BOOST_CHECK_THROW(CallRPC("signmessage"), runtime_error);
|
||||
/* Should throw error because this address is not loaded in the wallet */
|
||||
BOOST_CHECK_THROW(CallRPC("signmessage 1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ mymessage"), runtime_error);
|
||||
|
||||
/* missing arguments */
|
||||
BOOST_CHECK_THROW(CallRPC("verifymessage " + demoAddress.ToString()), runtime_error);
|
||||
BOOST_CHECK_THROW(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str()), runtime_error);
|
||||
BOOST_CHECK_THROW(CallRPC("verifymessage " + EncodeDestination(demoAddress)), runtime_error);
|
||||
BOOST_CHECK_THROW(CallRPC("verifymessage " + EncodeDestination(demoAddress) + " " + retValue.get_str()), runtime_error);
|
||||
/* Illegal address */
|
||||
BOOST_CHECK_THROW(CallRPC("verifymessage 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4X " + retValue.get_str() + " mymessage"), runtime_error);
|
||||
/* wrong address */
|
||||
BOOST_CHECK(CallRPC("verifymessage 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XZ " + retValue.get_str() + " mymessage").get_bool() == false);
|
||||
/* Correct address and signature but wrong message */
|
||||
BOOST_CHECK(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str() + " wrongmessage").get_bool() == false);
|
||||
BOOST_CHECK(CallRPC("verifymessage " + EncodeDestination(demoAddress) + " " + retValue.get_str() + " wrongmessage").get_bool() == false);
|
||||
/* Correct address, message and signature*/
|
||||
BOOST_CHECK(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str() + " mymessage").get_bool() == true);
|
||||
BOOST_CHECK(CallRPC("verifymessage " + EncodeDestination(demoAddress) + " " + retValue.get_str() + " mymessage").get_bool() == true);
|
||||
|
||||
/*********************************
|
||||
* getaddressesbyaccount
|
||||
@@ -217,7 +217,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet)
|
||||
BOOST_CHECK_NO_THROW(retValue = CallRPC("getaddressesbyaccount " + strAccount));
|
||||
UniValue arr = retValue.get_array();
|
||||
BOOST_CHECK(arr.size() > 0);
|
||||
BOOST_CHECK(CBitcoinAddress(arr[0].get_str()).Get() == demoAddress.Get());
|
||||
BOOST_CHECK(arr[0].get_str() == EncodeDestination(demoAddress));
|
||||
|
||||
/*********************************
|
||||
* fundrawtransaction
|
||||
|
||||
140
src/wallet/test/walletdb_tests.cpp
Normal file
140
src/wallet/test/walletdb_tests.cpp
Normal file
@@ -0,0 +1,140 @@
|
||||
// Copyright (c) 2017 The Bitcoin developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include "test/test_bitcoin.h"
|
||||
#include "random.h"
|
||||
#include "fs.h"
|
||||
|
||||
#include "wallet/wallet.h"
|
||||
#include "wallet/walletdb.h"
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
namespace {
|
||||
struct WalletDBTestingSetup : public TestingSetup
|
||||
{
|
||||
WalletDBTestingSetup(const std::string &chainName = CBaseChainParams::MAIN)
|
||||
{
|
||||
bitdb.MakeMock();
|
||||
}
|
||||
|
||||
~WalletDBTestingSetup()
|
||||
{
|
||||
bitdb.Flush(true);
|
||||
bitdb.Reset();
|
||||
}
|
||||
};
|
||||
|
||||
static std::unique_ptr<CWalletDB> TmpDB(const fs::path &pathTemp, const std::string &testname)
|
||||
{
|
||||
fs::path dir = pathTemp / testname;
|
||||
BOOST_CHECK_MESSAGE(fs::create_directory(dir),
|
||||
"Unable to create a directory for test " + testname);
|
||||
fs::path path = dir / strprintf("testwallet%i", static_cast<int>(insecure_rand() % 1000000));
|
||||
return std::unique_ptr<CWalletDB>(new CWalletDB(path.string(), "cr+"));
|
||||
}
|
||||
|
||||
static std::unique_ptr<CWallet> LoadWallet(CWalletDB *db) {
|
||||
std::unique_ptr<CWallet> wallet(new CWallet);
|
||||
DBErrors res = db->LoadWallet(wallet.get());
|
||||
BOOST_CHECK(res == DB_LOAD_OK);
|
||||
return wallet;
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(walletdb_tests, WalletDBTestingSetup);
|
||||
|
||||
BOOST_AUTO_TEST_CASE(write_erase_name) {
|
||||
auto walletdb = TmpDB(pathTemp, "write_erase_name");
|
||||
|
||||
CTxDestination dst1 = CKeyID(uint160S("c0ffee"));
|
||||
CTxDestination dst2 = CKeyID(uint160S("f00d"));
|
||||
|
||||
BOOST_CHECK(walletdb->WriteName(dst1, "name1"));
|
||||
BOOST_CHECK(walletdb->WriteName(dst2, "name2"));
|
||||
{
|
||||
auto w = LoadWallet(walletdb.get());
|
||||
BOOST_CHECK_EQUAL(1, w->mapAddressBook.count(dst1));
|
||||
BOOST_CHECK_EQUAL("name1", w->mapAddressBook[dst1].name);
|
||||
BOOST_CHECK_EQUAL("name2", w->mapAddressBook[dst2].name);
|
||||
}
|
||||
|
||||
walletdb->EraseName(dst1);
|
||||
|
||||
{
|
||||
auto w = LoadWallet(walletdb.get());
|
||||
BOOST_CHECK_EQUAL(0, w->mapAddressBook.count(dst1));
|
||||
BOOST_CHECK_EQUAL(1, w->mapAddressBook.count(dst2));
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(write_erase_purpose) {
|
||||
auto walletdb = TmpDB(pathTemp, "write_erase_purpose");
|
||||
|
||||
CTxDestination dst1 = CKeyID(uint160S("c0ffee"));
|
||||
CTxDestination dst2 = CKeyID(uint160S("f00d"));
|
||||
|
||||
BOOST_CHECK(walletdb->WritePurpose(dst1, "purpose1"));
|
||||
BOOST_CHECK(walletdb->WritePurpose(dst2, "purpose2"));
|
||||
{
|
||||
auto w = LoadWallet(walletdb.get());
|
||||
BOOST_CHECK_EQUAL(1, w->mapAddressBook.count(dst1));
|
||||
BOOST_CHECK_EQUAL("purpose1", w->mapAddressBook[dst1].purpose);
|
||||
BOOST_CHECK_EQUAL("purpose2", w->mapAddressBook[dst2].purpose);
|
||||
}
|
||||
|
||||
walletdb->ErasePurpose(dst1);
|
||||
|
||||
{
|
||||
auto w = LoadWallet(walletdb.get());
|
||||
BOOST_CHECK_EQUAL(0, w->mapAddressBook.count(dst1));
|
||||
BOOST_CHECK_EQUAL(1, w->mapAddressBook.count(dst2));
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(write_erase_destdata) {
|
||||
auto walletdb = TmpDB(pathTemp, "write_erase_destdata");
|
||||
|
||||
CTxDestination dst1 = CKeyID(uint160S("c0ffee"));
|
||||
CTxDestination dst2 = CKeyID(uint160S("f00d"));
|
||||
|
||||
BOOST_CHECK(walletdb->WriteDestData(dst1, "key1", "value1"));
|
||||
BOOST_CHECK(walletdb->WriteDestData(dst1, "key2", "value2"));
|
||||
BOOST_CHECK(walletdb->WriteDestData(dst2, "key1", "value3"));
|
||||
BOOST_CHECK(walletdb->WriteDestData(dst2, "key2", "value4"));
|
||||
{
|
||||
auto w = LoadWallet(walletdb.get());
|
||||
std::string val;
|
||||
BOOST_CHECK(w->GetDestData(dst1, "key1", &val));
|
||||
BOOST_CHECK_EQUAL("value1", val);
|
||||
BOOST_CHECK(w->GetDestData(dst1, "key2", &val));
|
||||
BOOST_CHECK_EQUAL("value2", val);
|
||||
BOOST_CHECK(w->GetDestData(dst2, "key1", &val));
|
||||
BOOST_CHECK_EQUAL("value3", val);
|
||||
BOOST_CHECK(w->GetDestData(dst2, "key2", &val));
|
||||
BOOST_CHECK_EQUAL("value4", val);
|
||||
}
|
||||
|
||||
walletdb->EraseDestData(dst1, "key2");
|
||||
|
||||
{
|
||||
auto w = LoadWallet(walletdb.get());
|
||||
std::string dummy;
|
||||
BOOST_CHECK(w->GetDestData(dst1, "key1", &dummy));
|
||||
BOOST_CHECK(!w->GetDestData(dst1, "key2", &dummy));
|
||||
BOOST_CHECK(w->GetDestData(dst2, "key1", &dummy));
|
||||
BOOST_CHECK(w->GetDestData(dst2, "key2", &dummy));
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(no_dest_fails) {
|
||||
auto walletdb = TmpDB(pathTemp, "no_dest_fails");
|
||||
|
||||
CTxDestination dst = CNoDestination{};
|
||||
BOOST_CHECK(!walletdb->WriteName(dst, "name"));
|
||||
BOOST_CHECK(!walletdb->WritePurpose(dst, "purpose"));
|
||||
BOOST_CHECK(!walletdb->WriteDestData(dst, "key", "value"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
Reference in New Issue
Block a user