Repository navigation
Expand file tree
/
Copy pathutil.cpp
More file actions
150 lines (129 loc) · 5 KB
/
Copy pathutil.cpp
File metadata and controls
150 lines (129 loc) · 5 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) 2021-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <wallet/test/util.h>
#include <chain.h>
#include <key.h>
#include <key_io.h>
#include <test/util/setup_common.h>
#include <validationinterface.h>
#include <wallet/context.h>
#include <wallet/scan.h>
#include <wallet/wallet.h>
#include <wallet/walletdb.h>
#include <sqlite3.h>
#include <memory>
namespace wallet {
std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key)
{
auto wallet = std::make_unique<CWallet>(&chain, "", CreateMockableWalletDatabase());
{
LOCK2(wallet->cs_wallet, ::cs_main);
wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash());
}
{
LOCK(wallet->cs_wallet);
wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
wallet->SetupDescriptorScriptPubKeyMans();
}
AddKey(*wallet, key);
WalletRescanReserver reserver(*wallet);
reserver.reserve();
ScanResult result = wallet->Scanner().Scan(cchain.Genesis()->GetBlockHash(), /*start_height=*/0, /*max_height=*/{}, reserver, /*save_progress=*/false);
assert(result.status == ScanResult::SUCCESS);
assert(result.last_scanned_block == cchain.Tip()->GetBlockHash());
assert(*result.last_scanned_height == cchain.Height());
assert(result.last_failed_block.IsNull());
return wallet;
}
void AddKey(CWallet& wallet, const CKey& key)
{
LOCK(wallet.cs_wallet);
FlatSigningProvider provider;
std::string error;
auto descs = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false);
assert(descs.size() == 1);
auto& desc = descs.at(0);
WalletDescriptor w_desc(std::move(desc), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/1, /*next_index=*/1);
Assert(wallet.AddWalletDescriptor(w_desc, provider, /*label=*/"", /*internal=*/false));
}
std::shared_ptr<CWallet> TestCreateWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags)
{
bilingual_str _error;
std::vector<bilingual_str> _warnings;
auto wallet = CWallet::CreateNew(context, "", std::move(database), create_flags, /*born_encrypted=*/false, _error, _warnings);
NotifyWalletLoaded(context, wallet);
if (context.chain) {
wallet->postInitProcess();
}
return wallet;
}
std::shared_ptr<CWallet> TestCreateWallet(WalletContext& context)
{
DatabaseOptions options;
options.require_create = true;
options.create_flags = WALLET_FLAG_DESCRIPTORS;
DatabaseStatus status;
bilingual_str error;
auto database = MakeWalletDatabase("", options, status, error);
return TestCreateWallet(std::move(database), context, options.create_flags);
}
std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context)
{
bilingual_str error;
std::vector<bilingual_str> warnings;
auto wallet = CWallet::LoadExisting(context, "", std::move(database), error, warnings);
NotifyWalletLoaded(context, wallet);
if (context.chain) {
wallet->postInitProcess();
}
return wallet;
}
std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context)
{
DatabaseOptions options;
options.require_existing = true;
DatabaseStatus status;
bilingual_str error;
auto database = MakeWalletDatabase("", options, status, error);
return TestLoadWallet(std::move(database), context);
}
void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet)
{
// Calls SyncWithValidationInterfaceQueue
wallet->chain().waitForNotificationsIfTipChanged({});
wallet->m_chain_notifications_handler.reset();
WaitForDeleteWallet(std::move(wallet));
}
std::string getnewaddress(CWallet& w)
{
constexpr auto output_type = OutputType::BECH32;
return EncodeDestination(getNewDestination(w, output_type));
}
CTxDestination getNewDestination(CWallet& w, OutputType output_type)
{
return *Assert(w.GetNewDestination(output_type, ""));
}
MockableSQLiteDatabase::MockableSQLiteDatabase()
: InMemoryWalletDatabase()
{}
std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase()
{
return std::make_unique<MockableSQLiteDatabase>();
}
wallet::DescriptorScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, const bool success)
{
keystore.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
FlatSigningProvider keys;
std::string error;
auto parsed_descs = Parse(desc_str, keys, error, false);
Assert(success == (!parsed_descs.empty()));
if (!success) return nullptr;
auto& desc = parsed_descs.at(0);
const int64_t range_start = 0, range_end = 1, next_index = 0, timestamp = 1;
WalletDescriptor w_desc(std::move(desc), timestamp, range_start, range_end, next_index);
LOCK(keystore.cs_wallet);
auto spkm = Assert(keystore.AddWalletDescriptor(w_desc, keys,/*label=*/"", /*internal=*/false));
return &spkm.value().get();
};
} // namespace wallet