Skip to content

Commit 84a8403

Browse files
committed
[RPC] dumpwallet exporting hd key path.
`dumpwallet` rpc call exporting the hd key path (if there is any). `wallet_dump.py` test modified to verify the key path export new functionality.
1 parent 2864d82 commit 84a8403

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

src/wallet/rpcdump.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,17 @@ UniValue dumpwallet(const UniValue& params, bool fHelp)
415415
mapKeyBirth.clear();
416416
std::sort(vKeyBirth.begin(), vKeyBirth.end());
417417

418+
CBlockIndex* tip = chainActive.Tip();
418419
// produce output
419420
file << strprintf("# Wallet dump created by PIVX %s (%s)\n", CLIENT_BUILD, CLIENT_DATE);
420421
file << strprintf("# * Created on %s\n", EncodeDumpTime(GetTime()));
421-
file << strprintf("# * Best block at time of backup was %i (%s),\n", chainActive.Height(), chainActive.Tip()->GetBlockHash().ToString());
422-
file << strprintf("# mined on %s\n", EncodeDumpTime(chainActive.Tip()->GetBlockTime()));
422+
if (tip) {
423+
file << strprintf("# * Best block at time of backup was %i (%s),\n", tip->nHeight,
424+
tip->GetBlockHash().ToString());
425+
file << strprintf("# mined on %s\n", EncodeDumpTime(tip->GetBlockTime()));
426+
} else {
427+
file << "# Missing tip information\n";
428+
}
423429
file << "\n";
424430

425431
// Add the base58check encoded extended master if the wallet uses HD
@@ -451,7 +457,8 @@ UniValue dumpwallet(const UniValue& params, bool fHelp)
451457
} else {
452458
file << "change=1";
453459
}
454-
file << strprintf(" # addr=%s\n", strAddr);
460+
const CKeyMetadata& metadata = pwalletMain->mapKeyMetadata[keyid];
461+
file << strprintf(" # addr=%s%s\n", strAddr, (metadata.HasKeyOrigin() ? " hdkeypath="+metadata.key_origin.pathToString() : ""));
455462
}
456463
}
457464
file << "\n";

test/functional/wallet_dump.py

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,47 @@ def read_dump(file_name, addrs, hd_master_addr_old):
2424
# only read non comment lines
2525
if line[0] != "#" and len(line) > 10:
2626
# split out some data
27-
key_label, comment = line.split("#")
28-
# key = key_label.split(" ")[0]
29-
keytype = key_label.split(" ")[2]
30-
addr = comment.split(" addr=")[1].strip()
27+
key_date_label, comment = line.split("#")
28+
key_date_label = key_date_label.split(" ")
29+
30+
date = key_date_label[1]
31+
keytype = key_date_label[2]
32+
33+
imported_key = date == '1970-01-01T00:00:01Z'
34+
if imported_key:
35+
# Imported keys have multiple addresses, no label (keypath) and timestamp
36+
# Skip them
37+
continue
38+
39+
addr_keypath = comment.split(" addr=")[1]
40+
addr = addr_keypath.split(" ")[0]
41+
keypath = None
42+
43+
if keytype == "hdseed=1":
44+
# ensure we have generated a new hd master key
45+
assert hd_master_addr_old != addr
46+
hd_master_addr_ret = addr
47+
elif keytype == "script=1":
48+
# scripts don't have keypaths
49+
keypath = None
50+
else:
51+
keypath = addr_keypath.rstrip().split("hdkeypath=")[1]
3152

3253
# count key types
33-
if addr in addrs:
34-
found_addr += 1
35-
elif keytype == "change=1":
36-
found_addr_chg += 1
37-
elif keytype == "reserve=1":
38-
found_addr_rsv += 1
54+
for addrObj in addrs:
55+
if addrObj['address'] == addr.split(",")[0] and addrObj['hdkeypath'] == keypath and keytype == "label=":
56+
if addr.startswith('x') or addr.startswith('y'):
57+
# P2PKH address
58+
found_addr += 1
59+
# else: todo: add staking/anonymous addresses here
60+
break
61+
elif keytype == "change=1":
62+
found_addr_chg += 1
63+
break
64+
elif keytype == "reserve=1":
65+
found_addr_rsv += 1
66+
break
67+
3968
return found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_ret
4069

4170

@@ -60,8 +89,8 @@ def run_test (self):
6089
addrs = []
6190
for i in range(0,test_addr_count):
6291
addr = self.nodes[0].getnewaddress()
63-
#vaddr= self.nodes[0].validateaddress(addr) #required to get hd keypath
64-
addrs.append(addr)
92+
vaddr = self.nodes[0].getaddressinfo(addr) # required to get hd keypath
93+
addrs.append(vaddr)
6594
# Should be a no-op:
6695
self.nodes[0].keypoolrefill()
6796

0 commit comments

Comments
 (0)