Skip to content

Commit 45eea40

Browse files
committed
Bech32 addresses in dumpwallet
Output bech32 addresses in dumpwallet if address type is not as legacy
1 parent 895fbd7 commit 45eea40

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/wallet/rpcdump.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,28 @@ std::string DecodeDumpString(const std::string &str) {
7171
return ret.str();
7272
}
7373

74+
bool GetWalletAddressesForKey(CWallet * const pwallet, const CKeyID &keyid, std::string &strAddr, std::string &strLabel)
75+
{
76+
bool fLabelFound = false;
77+
CKey key;
78+
pwallet->GetKey(keyid, key);
79+
for (const auto& dest : GetAllDestinationsForKey(key.GetPubKey())) {
80+
if (pwallet->mapAddressBook.count(dest)) {
81+
if (!strAddr.empty()) {
82+
strAddr += ",";
83+
}
84+
strAddr += EncodeDestination(dest);
85+
strLabel = EncodeDumpString(pwallet->mapAddressBook[dest].name);
86+
fLabelFound = true;
87+
}
88+
}
89+
if (!fLabelFound) {
90+
strAddr = EncodeDestination(GetDestinationForKey(key.GetPubKey(), g_address_type));
91+
}
92+
return fLabelFound;
93+
}
94+
95+
7496
UniValue importprivkey(const JSONRPCRequest& request)
7597
{
7698
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
@@ -729,12 +751,13 @@ UniValue dumpwallet(const JSONRPCRequest& request)
729751
for (std::vector<std::pair<int64_t, CKeyID> >::const_iterator it = vKeyBirth.begin(); it != vKeyBirth.end(); it++) {
730752
const CKeyID &keyid = it->second;
731753
std::string strTime = EncodeDumpTime(it->first);
732-
std::string strAddr = EncodeDestination(keyid);
754+
std::string strAddr;
755+
std::string strLabel;
733756
CKey key;
734757
if (pwallet->GetKey(keyid, key)) {
735758
file << strprintf("%s %s ", CBitcoinSecret(key).ToString(), strTime);
736-
if (pwallet->mapAddressBook.count(keyid)) {
737-
file << strprintf("label=%s", EncodeDumpString(pwallet->mapAddressBook[keyid].name));
759+
if (GetWalletAddressesForKey(pwallet, keyid, strAddr, strLabel)) {
760+
file << strprintf("label=%s", strLabel);
738761
} else if (keyid == masterKeyID) {
739762
file << "hdmaster=1";
740763
} else if (mapKeyPool.count(keyid)) {

test/functional/wallet_dump.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def read_dump(file_name, addrs, script_addrs, hd_master_addr_old):
2020
found_script_addr = 0
2121
found_addr_chg = 0
2222
found_addr_rsv = 0
23+
witness_addr_ret = None
2324
hd_master_addr_ret = None
2425
for line in inputfile:
2526
# only read non comment lines
@@ -47,7 +48,14 @@ def read_dump(file_name, addrs, script_addrs, hd_master_addr_old):
4748

4849
# count key types
4950
for addrObj in addrs:
50-
if addrObj['address'] == addr and addrObj['hdkeypath'] == keypath and keytype == "label=":
51+
if addrObj['address'] == addr.split(",")[0] and addrObj['hdkeypath'] == keypath and keytype == "label=":
52+
# a labled entry in the wallet should contain both a native address
53+
# and the p2sh-p2wpkh address that was added at wallet setup
54+
if len(addr.split(",")) == 2:
55+
addr_list = addr.split(",")
56+
# the entry should be of the first key in the wallet
57+
assert_equal(addrs[0]['address'], addr_list[0])
58+
witness_addr_ret = addr_list[1]
5159
found_addr += 1
5260
break
5361
elif keytype == "change=1":
@@ -63,7 +71,7 @@ def read_dump(file_name, addrs, script_addrs, hd_master_addr_old):
6371
found_script_addr += 1
6472
break
6573

66-
return found_addr, found_script_addr, found_addr_chg, found_addr_rsv, hd_master_addr_ret
74+
return found_addr, found_script_addr, found_addr_chg, found_addr_rsv, hd_master_addr_ret, witness_addr_ret
6775

6876

6977
class WalletDumpTest(BitcoinTestFramework):
@@ -83,6 +91,8 @@ def run_test (self):
8391
tmpdir = self.options.tmpdir
8492

8593
# generate 20 addresses to compare against the dump
94+
# but since we add a p2sh-p2wpkh address for the first pubkey in the
95+
# wallet, we will expect 21 addresses in the dump
8696
test_addr_count = 20
8797
addrs = []
8898
for i in range(0,test_addr_count):
@@ -101,12 +111,13 @@ def run_test (self):
101111
result = self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.unencrypted.dump")
102112
assert_equal(result['filename'], os.path.abspath(tmpdir + "/node0/wallet.unencrypted.dump"))
103113

104-
found_addr, found_script_addr, found_addr_chg, found_addr_rsv, hd_master_addr_unenc = \
114+
found_addr, found_script_addr, found_addr_chg, found_addr_rsv, hd_master_addr_unenc, witness_addr_ret = \
105115
read_dump(tmpdir + "/node0/wallet.unencrypted.dump", addrs, script_addrs, None)
106116
assert_equal(found_addr, test_addr_count) # all keys must be in the dump
107117
assert_equal(found_script_addr, 2) # all scripts must be in the dump
108118
assert_equal(found_addr_chg, 50) # 50 blocks where mined
109119
assert_equal(found_addr_rsv, 90*2) # 90 keys plus 100% internal keys
120+
assert_equal(witness_addr_ret, witness_addr) # p2sh-p2wsh address added to the first key
110121

111122
#encrypt wallet, restart, unlock and dump
112123
self.nodes[0].node_encrypt_wallet('test')
@@ -116,12 +127,13 @@ def run_test (self):
116127
self.nodes[0].keypoolrefill()
117128
self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.encrypted.dump")
118129

119-
found_addr, found_script_addr, found_addr_chg, found_addr_rsv, _ = \
130+
found_addr, found_script_addr, found_addr_chg, found_addr_rsv, _, witness_addr_ret = \
120131
read_dump(tmpdir + "/node0/wallet.encrypted.dump", addrs, script_addrs, hd_master_addr_unenc)
121132
assert_equal(found_addr, test_addr_count)
122133
assert_equal(found_script_addr, 2)
123134
assert_equal(found_addr_chg, 90*2 + 50) # old reserve keys are marked as change now
124135
assert_equal(found_addr_rsv, 90*2)
136+
assert_equal(witness_addr_ret, witness_addr)
125137

126138
# Overwriting should fail
127139
assert_raises_rpc_error(-8, "already exists", self.nodes[0].dumpwallet, tmpdir + "/node0/wallet.unencrypted.dump")

0 commit comments

Comments
 (0)