16

I want to get a database or otherwise retrieve a set off all bitcoin public keys (not addresses) that have non zero balances.

What would be the easiest way to do that? Is there an API that could do such thing? Do I have to download and prune the whole blockchain? If so how would I do that?

2
  • What do you mean PK (but not addresses)? Do you want to exclude RIPEMD(SHA(PK))? Is that it?
    – linhares
    Commented May 7, 2015 at 22:03
  • 1
    I want to get only the (non-zero-balance) addresses that have a "visible" public key, i.e. from which money has been spend from. Commented May 8, 2015 at 4:03

3 Answers 3

13

You can't get a database of all such public keys, but you can get some of them.

An address, as you know, is a hash of a public key. When somebody sends coins to an address, you can think of the associated public key as having a positive balance, but nobody else necessarily knows what the public key actually is. (You can't compute the public key directly from the address, because hash functions are effectively one way.)

However, in order to spend the coins from that address, the spending transaction has to reveal the public key (so that the spending transaction's signature can be verified). So what you need to look for are addresses with a positive balance that have also had some coins spent. I am not aware of any simple way to do this short of making an index of all transactions. It is possible you could rig up a way to produce such a list by querying a fully-synced bitcoind using -txindex (so that all transactions are indexed), but it seems likely to be inefficient.

But as a randomly chosen example, consider the address 1Q6NNpHM1pyh6kEqzinBhEgsRc3nmpTGLm. It is associated with some public key which has a balance of BTC 259.7299, but since (as of this writing) no coins have been spent from that address, nobody except the owner of the address knows what the public key is. So your database will necessarily be incomplete; probably very incomplete.

In particular, most people use a new address for each of their transactions (including change addresses), in which case the transaction that reveals the public key for an address also spends all its coins (so that the balance is now zero), and that public key will likely never be used again. This strategy is used partly because certain theoretical cryptographic attacks are easier if the public key is known, and this strategy mitigates the risk from such attacks; it also increases privacy by making it harder to look at patterns of transactions to try to deduce who the address belongs to.

Another case in which you can learn the public key is so-called P2PK transactions, where the output script includes the actual destination public key, instead of merely its associated address. These were common in the past, but these days all standard clients use the P2PKH transaction type, in which only the address is used.

2
  • 1
    Yes, I know that only for a subset of the bitcoin addresses, there is a public key available. My goal is actually to figure out how many there are. My question is more of a implementation question of how I would go about doing this. Commented May 8, 2015 at 4:02
  • Bitcoinlib (github.com/petertodd/python-bitcoinlib) will give you the python functionality to scan the blocks and create a database with public keys being reused. The rest, it seems, is up to you.
    – linhares
    Commented May 8, 2015 at 12:10
1

You can use btcposbal2csv.py script to export the list to CSV.

To get current addresses with positive balance, let the full node client sync with the network. Stop the bitcoin-core client before running this utility. If you not stop the client, the database might get corrupted.

Then run this program with path to chainstate directory (usually $HOME/.bitcoin/chainstate).

For example, the following will read from ~/.bitcoin/chainstate, and write result to ~/addresses_with_balance.csv:

./btcposbal2csv.py /home/USER/.bitcoin/chainstate /home/USER/addresses_with_balance.csv

Notes:

  • That the output may not be complete as there are some transactions which are not understood by the decoding lib, or that which do not have "address" at all. Such transactions are not processed. Number of them and the total ammount is displayed after the analysis.
  • The output csv file only reflects the chainstate leveldb at your disk. So it will always be few blocks behind the network as you need to stop the bitcoin-core client.

You can also download the full list from some websites, e.g.


For Ethereum, see: How to list ALL Ethereum addresses with a positive balance.

1
  • Although he explicitly wrote "public keys (not addresses)" in the first sentence.
    – Nikolaj-K
    Commented Apr 20, 2021 at 0:09
0

Here is a script based on abe to print all public keys in the blockchain https://github.com/shivaenigma/bitcoin-abe. You can easily exclude the ones that have zero balance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.