2

The quest is to define a method for searching TXs on the Bitcoin blockchain using only a PublicKey in HEX format - as opposed to searching TXs by popular terms like wallet ID, txid, hash, etc.

I haven't found any such method. Does it exist?

Scenario:

Bob holds a compressed PublicKey of the form

02af31b067645ffe4eb534d609da80332e5c5747543fd97385e832c6fdf2d4b4bf

Hypothetically, Bob has no other info at his disposal and he wants to query the Blockchain to find all TXs associated with the above PublicKey.

If Bob succeeds, he finds this Transaction

Is this type of searching the blockchain possible?

1

In fact, the usual way to consult blockchains is using the public key (purportedly the public key of the searcher) as the result will be all the transactions in which the key is involved: https://www.blockchain.com/btc/address/1EwpnNBdFJykwxp6X8v9AfZnup9bgmrLE1

I suppose you're puzzled about the way to convert the public key to the public address, as the latter is the one used in blockchain explorers (I don't know if there's some one that can use the public key as input) :

:

$ ./bitcoin-address-from-public-key.py af31b067645ffe4eb534d609da80332e5c5747543fd97385e832c6fdf2d4b4bf 00 
pubkey = 04af31b067645ffe4eb534d609da80332e5c5747543fd97385e832c6fdf2d4b4bf00

Compressed public key: 
key_hash =      98f7fbd15e7b30a4ed55aec7baae40729967ba67 
checksum =      c199dbe6fc87793152351aaa25b4ce28e395225dd3abcb4606b159e1ac26b346 
key_hash + checksum =   0098f7fbd15e7b30a4ed55aec7baae40729967ba67 c199dbe6 
bitcoin address =       1EwpnNBdFJykwxp6X8v9AfZnup9bgmrLE1

In case you want to ask directly blockchain.info from command line it is possible to combine various scripts:

C:\> bitcoin-address-from-public-key af31b067645ffe4eb534d609da80332e5c5747543fd97385e832c6fdf2d4b4bf 00 | bitcoin-get-address-balance

address         = 1EwpnNBdFJykwxp6X8v9AfZnup9bgmrLE1
total_received  = 1.57390781 Bitcoin
final_balance   = 0 Bitcoin

In these examples I've modified two lines in bitcoin-address-from-public-key.py with COMPRESS_PUBKEY = 1 and VERBOSE = False.

  • Thanks but I am aware of the facts mentioned in your reply. What I want to know is how to query the blockchain (if possible) using the raw HEX public key as a single search term. As you said, blockchain explorers only allow searching for the hashed public key / address ID, TX id, etc. Perhaps some code is out there but cannot find anything on GitHub or Google. – RobertH Jun 23 at 1:27
  • The proposed Python script code doesn't fit your requirements? You could combinebitcoin-address-from-public-key.pywith bitcoin-get-address-balance.py to directly ask blockchain.info from command line ! I'm going to add this to the answer. – circulosmeos Jun 23 at 11:54
  • Yes, thank you so much. Your script is great and it does the job.It asks the blockchain directly and you are right this can only be done from command line. I will mark your answer :) The only issues I have is 1) when I enter the public key above it returns a diff address 18eCccERvefbyrgL15eK2exczQge9gQit5. And 2) how do I combine your two scripts on Ubuntu, in python so I can reproduce your results total_received = 1.57390781 Bitcoin final_balance = 0 Bitcoin I can see you used Windows in your example? – RobertH Jun 23 at 13:20
  • final edit: All done now, I managed to combine your scripts and also it now returns the correct result. Thanks again. – RobertH Jun 23 at 13:49
1

If the transaction outputs associated to the public key has not been spent, then finding the funds associated with that public key will mostly be a trial and error method.

Searching in outputs

Most outputs in Bitcoin send funds to an address that is the hash of the public key. Pay-to-public-key (P2PK) is the only standard output that sends funds directly to the public key, but is rarely used. Now, I said finding funds from the outputs is a trial and error method, because you do not know what type of address is being used by the person who controls that public key. He may be using P2PKH (addresses starting with 1), P2WPKH (addresses starting with bc1) or P2SH-P2WPKH (addresses starting with 3). You would need to generate those addresses from the public key and see if it matches to that using any explorers. Also, the user might have used the uncompressed public key version to generate the P2PKH address, so you would need to search for that as well.

Moreover, if funds are controlled in multi-sig addresses, where one of the public key involved is the one you know, it is impossible to calculate the address without knowing the other public keys AND the spending condition (m-of-n).

Searching in inputs

However, if the funds are spent, the task is 'easier'. You just have to go through all the inputs of all the transactions, and check that the public key was used to satisfy unlocking condition. Most inputs will have their scriptSig (witness data in case of SegWit) as <sig><pub_key>. You can match the public key against the one that is mentioned in the scriptSig.

This is also helpful with analyzing the public key for multi-sig transaction, if that public key was involved in the spending. However, there is still a gap involved. If the public key was say used in 2-of-3 multi-sig, and the transaction was spent with the other two keys, then there is no way of knowing, whether that public key was involved in that transaction at all.

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.