Skip to content

Commit fcc1c5e

Browse files
author
Martino Salvetti
committed
Add command dumpprivkey
1 parent ab91528 commit fcc1c5e

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

wallet-tool.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
'option -u is chosen (so be careful about private keys). '
2727
'(showseed) Shows the wallet recovery seed '
2828
'and hex seed. (importprivkey) Adds privkeys to this wallet, '
29-
'privkeys are spaces or commas separated. (listwallets) Lists '
30-
'all wallets with creator and timestamp. (history) Show all '
31-
'historical transaction details. Requires Bitcoin Core.')
29+
'privkeys are spaces or commas separated. (dumpprivkey) Export '
30+
'a single private key, specify an hd wallet path (listwallets) '
31+
'Lists all wallets with creator and timestamp. (history) Show '
32+
'all historical transaction details. Requires Bitcoin Core.')
3233

3334
parser = OptionParser(usage='usage: %prog [options] [wallet file] [method]',
3435
description=description)
@@ -69,6 +70,12 @@
6970
default=False,
7071
help=('choose to do fast wallet sync, only for Core and '
7172
'only for previously synced wallet'))
73+
parser.add_option('-H',
74+
'--hd',
75+
action='store',
76+
type='str',
77+
dest='hd_path',
78+
help='hd wallet path (e.g. m/0/0/0/000)')
7279
(options, args) = parser.parse_args()
7380

7481
# if the index_cache stored in wallet.json is longer than the default
@@ -82,7 +89,7 @@
8289
methods = ['display', 'displayall', 'summary', 'showseed', 'importprivkey',
8390
'history', 'showutxos']
8491
methods.extend(noseed_methods)
85-
noscan_methods = ['showseed', 'importprivkey']
92+
noscan_methods = ['showseed', 'importprivkey', 'dumpprivkey']
8693

8794
if len(args) < 1:
8895
parser.error('Needs a wallet file or method')
@@ -252,6 +259,14 @@ def cus_print(s):
252259
fd.write(json.dumps(wallet.walletdata))
253260
fd.close()
254261
print('Private key(s) successfully imported')
262+
elif method == 'dumpprivkey':
263+
if options.hd_path.startswith('m/0/'):
264+
m, forchange, k = [int(y) for y in options.hd_path[4:].split('/')]
265+
key = wallet.get_key(m, forchange, k)
266+
wifkey = btc.wif_compressed_privkey(key, vbyte=get_p2pk_vbyte())
267+
print(wifkey)
268+
else:
269+
print('%s is not a valid hd wallet path' % options.hd_path)
255270
elif method == 'listwallets':
256271
# Fetch list of wallets
257272
possible_wallets = []

0 commit comments

Comments
 (0)