10

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?

  • What do you mean PK (but not addresses)? Do you want to exclude RIPEMD(SHA(PK))? Is that it? – linhares May 7 '15 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. – Benedikt Bünz May 8 '15 at 4:03
9

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.

  • 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. – Benedikt Bünz May 8 '15 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 May 8 '15 at 12:10
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, privacy policy and cookie policy

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