<p>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:
<a href="https://www.blockchain.com/btc/address/1EwpnNBdFJykwxp6X8v9AfZnup9bgmrLE1" rel="nofollow noreferrer">https://www.blockchain.com/btc/address/1EwpnNBdFJykwxp6X8v9AfZnup9bgmrLE1</a></p>

<p>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) :</p>

<ul>
<li><p>You can use online tools like this one: <a href="https://iancoleman.io/bitcoin-key-compression/" rel="nofollow noreferrer">https://iancoleman.io/bitcoin-key-compression/</a></p></li>
<li><p>Or some code, for example using <a href="https://github.com/circulosmeos/bitcoin-in-tiny-pieces" rel="nofollow noreferrer">my very simple Python scripts <code>bitcoin-in-tiny-pieces</code></a>, in this case, as the prefix of the compressed pub key is <code>02</code> we know that <code>y</code> is even, and as <code>y</code> is not necessary in compressed form we can use <code>0x00</code> as <code>y</code> coordinate:</p></li>
</ul>

<p>:</p>

<pre><code>$ ./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
</code></pre>

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

<pre><code>C:\&gt; bitcoin-address-from-public-key af31b067645ffe4eb534d609da80332e5c5747543fd97385e832c6fdf2d4b4bf 00 | bitcoin-get-address-balance

address         = 1EwpnNBdFJykwxp6X8v9AfZnup9bgmrLE1
total_received  = 1.57390781 Bitcoin
final_balance   = 0 Bitcoin
</code></pre>

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

CC BY-SA 4.0
2019-06-23T01:27:33.930Z
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.
Robert
CC BY-SA 4.0
2019-06-23T11:54:55.103Z
The proposed Python script code doesn't fit your requirements? You could combine`bitcoin-address-from-public-key.py`with `bitcoin-get-address-balance.py` to directly ask `blockchain.info` from command line ! I'm going to add this to the answer.
circulosmeos
CC BY-SA 4.0
2019-06-23T13:20:26.547Z
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?
Robert
CC BY-SA 4.0
2019-06-23T13:49:01.407Z
final edit: All done now, I managed to combine your scripts and also it now returns the correct result. Thanks again.
Robert
CC BY-SA 4.0
2019-06-22T23:35:31.397Z
2019-06-23T12:00:21.153Z
2019-06-23T12:00:21.153Z
circulosmeos
<p>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. </p>

<p><strong>Searching in outputs</strong></p>

<p>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.</p>

<p>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).</p>

<p><strong>Searching in inputs</strong></p>

<p>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 <code>scriptSig</code> (witness data in case of SegWit) as <code>&lt;sig&gt;&lt;pub_key&gt;</code>. You can match the public key against the one that is mentioned in the <code>scriptSig</code>. </p>

<p>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.</p>

CC BY-SA 4.0
2019-06-23T09:08:10.187Z
2019-06-23T09:08:10.187Z
Ugam Kamat
<p>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. </p>

<p>I haven't found any such method. Does it exist?  </p>

<p><strong>Scenario:</strong></p>

<p>Bob holds a compressed PublicKey of the form  </p>

<p><code>02af31b067645ffe4eb534d609da80332e5c5747543fd97385e832c6fdf2d4b4bf</code></p>

<p>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. </p>

<p>If Bob succeeds, he finds <a href="https://www.blockchain.com/btc/tx/470e8254ba43caa133b4c8ed19c1383e8f4e276e2bc997ca5a13c2e587b51ec2" rel="nofollow noreferrer">this Transaction</a></p>

<p>Is this type of searching the blockchain possible?</p>

<transactions><blockchain>
How to search for TXs using only the PublicKey linked to those TXs?
Robert
# How to search for TXs using only the PublicKey linked to those TXs?

(Asked by: username_0 on 2019-06-22)

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](https://www.blockchain.com/btc/tx/470e8254ba43caa133b4c8ed19c1383e8f4e276e2bc997ca5a13c2e587b51ec2)

Is this type of searching the blockchain possible?

**Tags:** transactions, blockchain


---

## 2 answers

### Answer by username_1 on 2019-06-22. Score: 2 | Accepted answer

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) :

* You can use online tools like this one: <https://iancoleman.io/bitcoin-key-compression/>
* Or some code, for example using [my very simple Python scripts `bitcoin-in-tiny-pieces`](https://github.com/username_1/bitcoin-in-tiny-pieces), in this case, as the prefix of the compressed pub key is `02` we know that `y` is even, and as `y` is not necessary in compressed form we can use `0x00` as `y` coordinate:

:

```
$ ./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`.

> **Comments:**
> **username_0**: 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.  
> **username_1**: The proposed Python script code doesn't fit your requirements? You could combine`bitcoin-address-from-public-key.py`with `bitcoin-get-address-balance.py` to directly ask `blockchain.info` from command line ! I'm going to add this to the answer.  
> **username_0**: 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?  
> **username_0**: final edit: All done now, I managed to combine your scripts and also it now returns the correct result. Thanks again.  

---

### Answer by username_2 on 2019-06-23. Score: 2

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.


---

