Skip to content

Commit 8eb2cd1

Browse files
committed
Merge #14291: wallet: Add ListWalletDir utility function
d56a068 docs: Add release notes for listwalletdir RPC (João Barbosa) 0cb3cad qa: Add tests for listwalletdir RPC (João Barbosa) cc33773 rpc: Add listwalletdir RPC (João Barbosa) d1b03b8 interfaces: Add getWalletDir and listWalletDir to Node (João Barbosa) fc4db35 wallet: Add ListWalletDir utility (João Barbosa) Pull request description: `ListWalletDir` returns all available wallets in the current wallet directory. Based on MeshCollider work in pull #11485. Tree-SHA512: 5843e3dbd1e0449f55bb8ea7c241a536078ff6ffcaad88ce5fcf8963971d48c78600fbc4f44919523b8a92329d5d8a5f567a3e0ccb0270fdd27366e19603a716
2 parents 3715b24 + d56a068 commit 8eb2cd1

File tree

8 files changed

+129
-3
lines changed

8 files changed

+129
-3
lines changed

doc/release-notes-13152.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
New RPC methods
22
------------
33

4-
- `getnodeaddresses` returns peer addresses known to this node. It may be used to connect to nodes over TCP without using the DNS seeds.
4+
- `getnodeaddresses` returns peer addresses known to this node. It may be used to connect to nodes over TCP without using the DNS seeds.
5+
- `listwalletdir` returns a list of wallets in the wallet directory which is configured with `-walletdir` parameter.

src/dummywallet.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ void DummyWalletInit::AddWalletOptions() const
3434

3535
const WalletInitInterface& g_wallet_init_interface = DummyWalletInit();
3636

37+
fs::path GetWalletDir()
38+
{
39+
throw std::logic_error("Wallet function called in non-wallet build.");
40+
}
41+
42+
std::vector<fs::path> ListWalletDir()
43+
{
44+
throw std::logic_error("Wallet function called in non-wallet build.");
45+
}
46+
3747
std::vector<std::shared_ptr<CWallet>> GetWallets()
3848
{
3949
throw std::logic_error("Wallet function called in non-wallet build.");

src/interfaces/node.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#include <univalue.h>
3939

4040
class CWallet;
41+
fs::path GetWalletDir();
42+
std::vector<fs::path> ListWalletDir();
4143
std::vector<std::shared_ptr<CWallet>> GetWallets();
4244

4345
namespace interfaces {
@@ -218,6 +220,18 @@ class NodeImpl : public Node
218220
LOCK(::cs_main);
219221
return ::pcoinsTip->GetCoin(output, coin);
220222
}
223+
std::string getWalletDir() override
224+
{
225+
return GetWalletDir().string();
226+
}
227+
std::vector<std::string> listWalletDir() override
228+
{
229+
std::vector<std::string> paths;
230+
for (auto& path : ListWalletDir()) {
231+
paths.push_back(path.string());
232+
}
233+
return paths;
234+
}
221235
std::vector<std::unique_ptr<Wallet>> getWallets() override
222236
{
223237
std::vector<std::unique_ptr<Wallet>> wallets;

src/interfaces/node.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ class Node
173173
//! Get unspent outputs associated with a transaction.
174174
virtual bool getUnspentOutput(const COutPoint& output, Coin& coin) = 0;
175175

176+
//! Return default wallet directory.
177+
virtual std::string getWalletDir() = 0;
178+
179+
//! Return available wallets in wallet directory.
180+
virtual std::vector<std::string> listWalletDir() = 0;
181+
176182
//! Return interfaces for accessing wallets (if any).
177183
virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
178184

src/wallet/rpcwallet.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,6 +2443,38 @@ static UniValue getwalletinfo(const JSONRPCRequest& request)
24432443
return obj;
24442444
}
24452445

2446+
static UniValue listwalletdir(const JSONRPCRequest& request)
2447+
{
2448+
if (request.fHelp || request.params.size() != 0) {
2449+
throw std::runtime_error(
2450+
"listwalletdir\n"
2451+
"Returns a list of wallets in the wallet directory.\n"
2452+
"{\n"
2453+
" \"wallets\" : [ (json array of objects)\n"
2454+
" {\n"
2455+
" \"name\" : \"name\" (string) The wallet name\n"
2456+
" }\n"
2457+
" ,...\n"
2458+
" ]\n"
2459+
"}\n"
2460+
"\nExamples:\n"
2461+
+ HelpExampleCli("listwalletdir", "")
2462+
+ HelpExampleRpc("listwalletdir", "")
2463+
);
2464+
}
2465+
2466+
UniValue wallets(UniValue::VARR);
2467+
for (const auto& path : ListWalletDir()) {
2468+
UniValue wallet(UniValue::VOBJ);
2469+
wallet.pushKV("name", path.string());
2470+
wallets.push_back(wallet);
2471+
}
2472+
2473+
UniValue result(UniValue::VOBJ);
2474+
result.pushKV("wallets", wallets);
2475+
return result;
2476+
}
2477+
24462478
static UniValue listwallets(const JSONRPCRequest& request)
24472479
{
24482480
if (request.fHelp || request.params.size() != 0)
@@ -4096,6 +4128,7 @@ static const CRPCCommand commands[] =
40964128
{ "wallet", "listsinceblock", &listsinceblock, {"blockhash","target_confirmations","include_watchonly","include_removed"} },
40974129
{ "wallet", "listtransactions", &listtransactions, {"dummy","count","skip","include_watchonly"} },
40984130
{ "wallet", "listunspent", &listunspent, {"minconf","maxconf","addresses","include_unsafe","query_options"} },
4131+
{ "wallet", "listwalletdir", &listwalletdir, {} },
40994132
{ "wallet", "listwallets", &listwallets, {} },
41004133
{ "wallet", "loadwallet", &loadwallet, {"filename"} },
41014134
{ "wallet", "lockunspent", &lockunspent, {"unlock","transactions"} },

src/wallet/walletutil.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <wallet/walletutil.h>
66

7+
#include <util.h>
8+
79
fs::path GetWalletDir()
810
{
911
fs::path path;
@@ -25,3 +27,51 @@ fs::path GetWalletDir()
2527

2628
return path;
2729
}
30+
31+
static bool IsBerkeleyBtree(const fs::path& path)
32+
{
33+
// A Berkeley DB Btree file has at least 4K.
34+
// This check also prevents opening lock files.
35+
boost::system::error_code ec;
36+
if (fs::file_size(path, ec) < 4096) return false;
37+
38+
fs::ifstream file(path.string(), std::ios::binary);
39+
if (!file.is_open()) return false;
40+
41+
file.seekg(12, std::ios::beg); // Magic bytes start at offset 12
42+
uint32_t data = 0;
43+
file.read((char*) &data, sizeof(data)); // Read 4 bytes of file to compare against magic
44+
45+
// Berkeley DB Btree magic bytes, from:
46+
// https://github.com/file/file/blob/5824af38469ec1ca9ac3ffd251e7afe9dc11e227/magic/Magdir/database#L74-L75
47+
// - big endian systems - 00 05 31 62
48+
// - little endian systems - 62 31 05 00
49+
return data == 0x00053162 || data == 0x62310500;
50+
}
51+
52+
std::vector<fs::path> ListWalletDir()
53+
{
54+
const fs::path wallet_dir = GetWalletDir();
55+
std::vector<fs::path> paths;
56+
57+
for (auto it = fs::recursive_directory_iterator(wallet_dir); it != end(it); ++it) {
58+
if (it->status().type() == fs::directory_file && IsBerkeleyBtree(it->path() / "wallet.dat")) {
59+
// Found a directory which contains wallet.dat btree file, add it as a wallet.
60+
paths.emplace_back(fs::relative(it->path(), wallet_dir));
61+
} else if (it.level() == 0 && it->symlink_status().type() == fs::regular_file && IsBerkeleyBtree(it->path())) {
62+
if (it->path().filename() == "wallet.dat") {
63+
// Found top-level wallet.dat btree file, add top level directory ""
64+
// as a wallet.
65+
paths.emplace_back();
66+
} else {
67+
// Found top-level btree file not called wallet.dat. Current bitcoin
68+
// software will never create these files but will allow them to be
69+
// opened in a shared database environment for backwards compatibility.
70+
// Add it to the list of available wallets.
71+
paths.emplace_back(fs::relative(it->path(), wallet_dir));
72+
}
73+
}
74+
}
75+
76+
return paths;
77+
}

src/wallet/walletutil.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
#ifndef BITCOIN_WALLET_WALLETUTIL_H
66
#define BITCOIN_WALLET_WALLETUTIL_H
77

8-
#include <chainparamsbase.h>
9-
#include <util.h>
8+
#include <fs.h>
9+
10+
#include <vector>
1011

1112
//! Get the path of the wallet directory.
1213
fs::path GetWalletDir();
1314

15+
//! Get wallets in wallet directory.
16+
std::vector<fs::path> ListWalletDir();
17+
1418
#endif // BITCOIN_WALLET_WALLETUTIL_H

test/functional/wallet_multiwallet.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def wallet_file(name):
3838
return wallet_dir(name, "wallet.dat")
3939
return wallet_dir(name)
4040

41+
assert_equal(self.nodes[0].listwalletdir(), { 'wallets': [{ 'name': '' }] })
42+
4143
# check wallet.dat is created
4244
self.stop_nodes()
4345
assert_equal(os.path.isfile(wallet_dir('wallet.dat')), True)
@@ -71,6 +73,8 @@ def wallet_file(name):
7173
wallet_names.remove('w7_symlink')
7274
extra_args = ['-wallet={}'.format(n) for n in wallet_names]
7375
self.start_node(0, extra_args)
76+
assert_equal(set(map(lambda w: w['name'], self.nodes[0].listwalletdir()['wallets'])), set(['', 'w3', 'w2', 'sub/w5', 'w7', 'w7', 'w1', 'w8', 'w']))
77+
7478
assert_equal(set(node.listwallets()), set(wallet_names))
7579

7680
# check that all requested wallets were created
@@ -143,6 +147,8 @@ def wallet_file(name):
143147

144148
self.restart_node(0, extra_args)
145149

150+
assert_equal(set(map(lambda w: w['name'], self.nodes[0].listwalletdir()['wallets'])), set(['', 'w3', 'w2', 'sub/w5', 'w7', 'w7', 'w8_copy', 'w1', 'w8', 'w']))
151+
146152
wallets = [wallet(w) for w in wallet_names]
147153
wallet_bad = wallet("bad")
148154

@@ -281,6 +287,8 @@ def wallet_file(name):
281287
assert_equal(self.nodes[0].listwallets(), ['w1'])
282288
assert_equal(w1.getwalletinfo()['walletname'], 'w1')
283289

290+
assert_equal(set(map(lambda w: w['name'], self.nodes[0].listwalletdir()['wallets'])), set(['', 'w3', 'w2', 'sub/w5', 'w7', 'w9', 'w7', 'w8_copy', 'w1', 'w8', 'w']))
291+
284292
# Test backing up and restoring wallets
285293
self.log.info("Test wallet backup")
286294
self.restart_node(0, ['-nowallet'])

0 commit comments

Comments
 (0)