Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python script for dumping wallet addresses and private keys
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# This software is in the public domain, furnished "as is", without technical
# support, and with no warranty, express or implied, as to its usefulness for
# any purpose.
#
# Usage:
# ./dump_wallet_addresses.py <wallet binary>
#
# Simple Python program that communicates with your <whatever>coin wallet
# (assuming it's bitcoin wallet compatible) in order to list all addresses
# together with their private keys. Both normally visible and hidden/secret
# addresses are listed.
#
# There is no error handling. Make sure the wallet binary is in the search
# path, or pass its full path.
#
# Author: Per Rovegård
# ---------------------------------------------------------------------------
import sys
import subprocess
import json
walletBin = ""
def execWalletCommand(cmd, expectsJson):
p = subprocess.Popen([walletBin] + cmd, stdout=subprocess.PIPE)
out, err = p.communicate()
if expectsJson:
return json.loads(out)
return out
def addAddressesFromGroupings(target):
result = execWalletCommand(["listaddressgroupings"], True)
for grouping in result:
for group in grouping:
addr = group[0]
if addr not in target:
target[addr] = {}
target[addr]["balance"] = group[1]
if len(group) > 2:
target[addr]["account"] = group[2]
def addAddressesFromAccounts(target):
accounts = execWalletCommand(["listaccounts"], True)
for key in accounts:
addresses = execWalletCommand(["getaddressesbyaccount", key], True)
for addr in addresses:
if addr not in target:
target[addr] = {}
target[addr]["account"] = key
def getPrivKeyForAddress(address):
return execWalletCommand(["dumpprivkey", address], False).strip()
def main():
global walletBin
if len(sys.argv) < 2:
print "Usage: %s <wallet binary>" % (sys.argv[0], )
sys.exit(1)
walletBin = sys.argv[1]
target = {}
addAddressesFromAccounts(target)
addAddressesFromGroupings(target)
for addr in target:
obj = target[addr]
balance = obj["balance"] if "balance" in obj else 0
account = obj["account"] if "account" in obj else "<unknown>"
if account == "":
account = '""'
print "%s\t%s\t%.8f\t%s" % (addr, getPrivKeyForAddress(addr), balance, account)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment