Skip to content

Commit 31d09b3

Browse files
committed
Merge bitcoin#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 bitcoin#11485. Tree-SHA512: 5843e3dbd1e0449f55bb8ea7c241a536078ff6ffcaad88ce5fcf8963971d48c78600fbc4f44919523b8a92329d5d8a5f567a3e0ccb0270fdd27366e19603a716
1 parent 03bca03 commit 31d09b3

File tree

7 files changed

+126
-0
lines changed

7 files changed

+126
-0
lines changed

src/dummywallet.cpp

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

4949
const WalletInitInterface& g_wallet_init_interface = DummyWalletInit();
5050

51+
fs::path GetWalletDir()
52+
{
53+
throw std::logic_error("Wallet function called in non-wallet build.");
54+
}
55+
56+
std::vector<fs::path> ListWalletDir()
57+
{
58+
throw std::logic_error("Wallet function called in non-wallet build.");
59+
}
60+
5161
std::vector<std::shared_ptr<CWallet>> GetWallets()
5262
{
5363
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
@@ -45,6 +45,8 @@
4545
#include <univalue.h>
4646

4747
class CWallet;
48+
fs::path GetWalletDir();
49+
std::vector<fs::path> ListWalletDir();
4850
std::vector<std::shared_ptr<CWallet>> GetWallets();
4951

5052
namespace interfaces {
@@ -350,6 +352,18 @@ class NodeImpl : public Node
350352
LOCK(::cs_main);
351353
return ::pcoinsTip->GetCoin(output, coin);
352354
}
355+
std::string getWalletDir() override
356+
{
357+
return GetWalletDir().string();
358+
}
359+
std::vector<std::string> listWalletDir() override
360+
{
361+
std::vector<std::string> paths;
362+
for (auto& path : ListWalletDir()) {
363+
paths.push_back(path.string());
364+
}
365+
return paths;
366+
}
353367
std::vector<std::unique_ptr<Wallet>> getWallets() override
354368
{
355369
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
@@ -238,6 +238,12 @@ class Node
238238
//! Get unspent outputs associated with a transaction.
239239
virtual bool getUnspentOutput(const COutPoint& output, Coin& coin) = 0;
240240

241+
//! Return default wallet directory.
242+
virtual std::string getWalletDir() = 0;
243+
244+
//! Return available wallets in wallet directory.
245+
virtual std::vector<std::string> listWalletDir() = 0;
246+
241247
//! Return interfaces for accessing wallets (if any).
242248
virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
243249

src/wallet/rpcwallet.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3125,6 +3125,38 @@ static UniValue getwalletinfo(const JSONRPCRequest& request)
31253125
return obj;
31263126
}
31273127

3128+
static UniValue listwalletdir(const JSONRPCRequest& request)
3129+
{
3130+
if (request.fHelp || request.params.size() != 0) {
3131+
throw std::runtime_error(
3132+
"listwalletdir\n"
3133+
"Returns a list of wallets in the wallet directory.\n"
3134+
"{\n"
3135+
" \"wallets\" : [ (json array of objects)\n"
3136+
" {\n"
3137+
" \"name\" : \"name\" (string) The wallet name\n"
3138+
" }\n"
3139+
" ,...\n"
3140+
" ]\n"
3141+
"}\n"
3142+
"\nExamples:\n"
3143+
+ HelpExampleCli("listwalletdir", "")
3144+
+ HelpExampleRpc("listwalletdir", "")
3145+
);
3146+
}
3147+
3148+
UniValue wallets(UniValue::VARR);
3149+
for (const auto& path : ListWalletDir()) {
3150+
UniValue wallet(UniValue::VOBJ);
3151+
wallet.pushKV("name", path.string());
3152+
wallets.push_back(wallet);
3153+
}
3154+
3155+
UniValue result(UniValue::VOBJ);
3156+
result.pushKV("wallets", wallets);
3157+
return result;
3158+
}
3159+
31283160
static UniValue listwallets(const JSONRPCRequest& request)
31293161
{
31303162
if (request.fHelp || request.params.size() != 0)
@@ -4717,6 +4749,7 @@ static const CRPCCommand commands[] =
47174749
{ "wallet", "listsinceblock", &listsinceblock, {"blockhash","target_confirmations","include_watchonly","include_removed"} },
47184750
{ "wallet", "listtransactions", &listtransactions, {"account|label|dummy","count","skip","include_watchonly"} },
47194751
{ "wallet", "listunspent", &listunspent, {"minconf","maxconf","addresses","include_unsafe","query_options"} },
4752+
{ "wallet", "listwalletdir", &listwalletdir, {} },
47204753
{ "wallet", "listwallets", &listwallets, {} },
47214754
{ "wallet", "loadwallet", &loadwallet, {"filename"} },
47224755
{ "wallet", "lockunspent", &lockunspent, {"unlock","transactions"} },

src/wallet/walletutil.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <wallet/walletutil.h>
6+
#include <util/system.h>
67

78
fs::path GetWalletDir()
89
{
@@ -26,6 +27,54 @@ fs::path GetWalletDir()
2627
return path;
2728
}
2829

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

src/wallet/walletutil.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@
77

88
#include <chainparamsbase.h>
99
#include <util/system.h>
10+
#include <fs.h>
11+
12+
#include <vector>
1013

1114
//! Get the path of the wallet directory.
1215
fs::path GetWalletDir();
1316

17+
//! Get wallets in wallet directory.
18+
std::vector<fs::path> ListWalletDir();
19+
1420
//! The WalletLocation class provides wallet information.
1521
class WalletLocation final
1622
{

test/functional/wallet_multiwallet.py

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

39+
assert_equal(self.nodes[0].listwalletdir(), { 'wallets': [{ 'name': '' }] })
40+
3941
# check wallet.dat is created
4042
self.stop_nodes()
4143
assert_equal(os.path.isfile(wallet_dir('wallet.dat')), True)
@@ -66,6 +68,8 @@ def wallet_file(name):
6668
wallet_names = ['w1', 'w2', 'w3', 'w', 'sub/w5', os.path.join(self.options.tmpdir, 'extern/w6'), 'w7_symlink', 'w8', '']
6769
extra_args = ['-wallet={}'.format(n) for n in wallet_names]
6870
self.start_node(0, extra_args)
71+
assert_equal(set(map(lambda w: w['name'], self.nodes[0].listwalletdir()['wallets'])), set(['', 'w3', 'w2', 'sub/w5', 'w7', 'w7', 'w1', 'w8', 'w']))
72+
6973
assert_equal(set(node.listwallets()), set(wallet_names))
7074

7175
# check that all requested wallets were created
@@ -137,6 +141,8 @@ def wallet_file(name):
137141

138142
self.restart_node(0, extra_args)
139143

144+
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']))
145+
140146
wallets = [wallet(w) for w in wallet_names]
141147
wallet_bad = wallet("bad")
142148

@@ -285,6 +291,8 @@ def wallet_file(name):
285291
assert_equal(self.nodes[0].listwallets(), ['w1'])
286292
assert_equal(w1.getwalletinfo()['walletname'], 'w1')
287293

294+
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']))
295+
288296
# Test backing up and restoring wallets
289297
self.log.info("Test wallet backup")
290298
self.restart_node(0, ['-nowallet'])

0 commit comments

Comments
 (0)