<p>The ScriptPubKey <code>2103f0cc6d142d14a40937f12dbd99dbd9021328f45759e26f1877f2a838876709e1ac</code> is a pay-to-public-key. There is no defined address for this output type. There is therefore no way to retrieve addresses when <code>type</code> is <code>pubkey</code>.</p>

CC BY-SA 4.0
2023-05-03T05:49:51.283Z
Yes, I know that the mentioned ScriptPubKey is a pay-to-public-key for which there is no defined address for this output type which I mentioned in question too. However, since almost all Bitcoin Explorers are able to display the address for corresponding pub-key, I was wondering if there was a simpler way to retrieve the same using RPC/bitcoin-cli. Yes, I was aware of other solutions like github.com/alecalve/python-bitcoin-blockchain-parser which directly read the data from blocks (.dat files) and calculate addresses from corresponding key.
curious
CC BY-SA 4.0
2023-05-03T06:40:37.983Z
You really shouldn't. It's a dated, confusing practice. More modern block explorers do not show P2PKH addresses for P2PK outputs, FWIW.
Pieter Wuille
CC BY-SA 4.0
2023-05-03T12:26:38.057Z
@pieter-wuille Since most of the explorers I used like blockchain.com, blockchair.com, blockcypher.com etc. were displaying the corresponding addresses, I believed that the process is legitimate. Can you pls explain it a bit further why it is a dated and confusing practice ?
curious
CC BY-SA 4.0
2023-05-03T13:06:35.320Z
For example blockstream.info and mempool.space correctly don't show any address for P2PK. Why is it confusing? Simply because those outputs don't *have* an address - the address corresponds to a different output. It dates from a time when P2PK and P2PKH were the only types used in practice, and addresses were more a way to indicate "identity" rather than "way of conveying a payment output". A block explorer started it, and others copied it for compatibility, but it is nonsensical.
Pieter Wuille
CC BY-SA 4.0
2023-05-04T05:05:19.193Z
@pieter-wuille Thanks for shedding light on this topic.. now it makes some sense to me. Does it mean that other old explorers which are doing the same, are displaying the wrong addresses for that particular txn/output (p2pk type) or was that technique legitimate for some period of time until new output types comes to the picture ?
curious
CC BY-SA 4.0
2023-05-04T05:12:33.517Z
@curious I've written more about the history on https://bitcoin.stackexchange.com/a/117426/208. TL;DR: the meaning of the word address has dramatically shifted starting in 2012.
Pieter Wuille
CC BY-SA 4.0
2023-05-04T08:31:47.060Z
@pieter-wuille Thank you for sharing the link. It has more info I was looking for.
curious
CC BY-SA 4.0
2023-05-02T13:47:46.290Z
2023-05-02T13:47:46.290Z
Antoine Poinsot
<p>If you want to try something beyond using the RPC, you can calculate addresses.</p>
<p>In the ScriptPubKey object, remove <code>21</code> from the start and <code>ac</code> from the end.
The <code>21</code> is hex to indicate the length of the pubkey.
<code>ac</code> is the opcode for <code>OP_CHECKSIG</code>.</p>
<p>You will be left with your pubkey <code>03f0cc6d142d14a40937f12dbd99dbd9021328f45759e26f1877f2a838876709e1</code>. From here you can convert it to a base58 encoded address.</p>
<p>An example of how to do this in with python</p>
<pre><code>import bitcoin
from bitcoin.core import x
from bitcoin.core import CoreMainParams
from bitcoin.wallet import P2PKHBitcoinAddress

def calc_addr_tool(pubkey, pubtype, p2shtype, wiftype):
    class CoinParams(CoreMainParams):
        MESSAGE_START = b'\x24\xe9\x27\x64'
        DEFAULT_PORT = 7770
        BASE58_PREFIXES = {'PUBKEY_ADDR': int(pubtype),
                           'SCRIPT_ADDR': int(p2shtype),
                           'SECRET_KEY': int(wiftype)}
    bitcoin.params = CoinParams

    try:
        address = str(P2PKHBitcoinAddress.from_pubkey(x(pubkey)))
        return {
            &quot;pubkey&quot;: pubkey,
            &quot;pubtype&quot;: pubtype,
            &quot;p2shtype&quot;: p2shtype,
            &quot;wiftype&quot;: wiftype,
            &quot;address&quot;: address
        }

    except Exception as e:
        logger.error(f&quot;[calc_addr_tool] Exception: {e}&quot;)
        return {&quot;error&quot;: str(e)}
</code></pre>
<p>(from <a href="https://github.com/smk762/kmd_ntx_stats_docker/blob/master/code/kmd_ntx_api/lib_base58.py#L242-L263" rel="nofollow noreferrer">https://github.com/smk762/kmd_ntx_stats_docker/blob/master/code/kmd_ntx_api/lib_base58.py#L242-L263</a>)</p>
<p>For a segwit address try</p>
<pre><code>from bip32utils import Base58

def p2wpkh_in_p2sh_addr(pk, testnet=False):
    &quot;&quot;&quot;
    Compressed public key (hex string) -&gt; p2wpkh nested in p2sh address. 'SegWit address.'
    &quot;&quot;&quot;
    # Script sig is just PUSH(20){hash160(cpk)}
    push_20 = bytes.fromhex(&quot;0014&quot;)
    script_sig = push_20 + hash160_bytes(bytes.fromhex(pk))

    # Address is then prefix + hash160(script_sig)
    prefix = b&quot;\xc4&quot; if testnet else b&quot;\x05&quot;
    address = Base58.check_encode(prefix + hash160(script_sig))
    return address
</code></pre>
<p>(from <a href="https://matthewdowney.github.io/create-segwit-address.html" rel="nofollow noreferrer">https://matthewdowney.github.io/create-segwit-address.html</a>)</p>
<p>Be careful with any output and confirm with sending a small amount first.</p>

CC BY-SA 4.0
2023-05-02T16:07:33.023Z
2023-05-02T16:07:33.023Z
smk
<p>We are using <a href="https://github.com/jgarzik/python-bitcoinrpc" rel="nofollow noreferrer">jgarzik/python-bitcoinrpc</a> and in some cases <code>curl</code> to retrieve data from bitcoin core using RPC.</p>
<p>However, for some txns where <code>pubkey</code> is used, bitcoin-cli/rpc gives no address in corresponding vout.</p>
<p>e.g. for <code>bitcoin-cli getrawtransaction 835fffaae22a3932882c50e8755407dc6dba4f41350271405765660beaa45a63 2</code> :</p>
<pre><code>  &quot;vout&quot;: [
    {
      &quot;value&quot;: 0.00010000,
      &quot;n&quot;: 0,
      &quot;scriptPubKey&quot;: {
        &quot;asm&quot;: &quot;03f0cc6d142d14a40937f12dbd99dbd9021328f45759e26f1877f2a838876709e1 OP_CHECKSIG&quot;,
        &quot;hex&quot;: &quot;2103f0cc6d142d14a40937f12dbd99dbd9021328f45759e26f1877f2a838876709e1ac&quot;,
        &quot;type&quot;: &quot;pubkey&quot;
      }
    },
    {
      &quot;value&quot;: 0.00010000,
      &quot;n&quot;: 1,
      &quot;scriptPubKey&quot;: {
        &quot;asm&quot;: &quot;03f0cc6d142d14a40937f12dbd99dbd9021328f45759e26f1877f2a838876709e1 OP_CHECKSIG&quot;,
        &quot;hex&quot;: &quot;2103f0cc6d142d14a40937f12dbd99dbd9021328f45759e26f1877f2a838876709e1ac&quot;,
        &quot;type&quot;: &quot;pubkey&quot;
      }
    },

</code></pre>
<p>Is there any way to retrieve addresses where <code>script type</code> is <code>pubkey</code> using bitcoin-cli/RPC ?</p>
<p>Thanks.</p>

<address><json-rpc><bitcoin-cli><p2pk>
Retrieve address corresponding to pubkey using bitcoin-cli/RPC
curious
# Retrieve address corresponding to pubkey using bitcoin-cli/RPC

(Asked by: username_0 on 2023-05-02)

We are using [jgarzik/python-bitcoinrpc](https://github.com/jgarzik/python-bitcoinrpc) and in some cases `curl` to retrieve data from bitcoin core using RPC.

However, for some txns where `pubkey` is used, bitcoin-cli/rpc gives no address in corresponding vout.

e.g. for `bitcoin-cli getrawtransaction 835fffaae22a3932882c50e8755407dc6dba4f41350271405765660beaa45a63 2` :

```
  "vout": [
    {
      "value": 0.00010000,
      "n": 0,
      "scriptPubKey": {
        "asm": "03f0cc6d142d14a40937f12dbd99dbd9021328f45759e26f1877f2a838876709e1 OP_CHECKSIG",
        "hex": "2103f0cc6d142d14a40937f12dbd99dbd9021328f45759e26f1877f2a838876709e1ac",
        "type": "pubkey"
      }
    },
    {
      "value": 0.00010000,
      "n": 1,
      "scriptPubKey": {
        "asm": "03f0cc6d142d14a40937f12dbd99dbd9021328f45759e26f1877f2a838876709e1 OP_CHECKSIG",
        "hex": "2103f0cc6d142d14a40937f12dbd99dbd9021328f45759e26f1877f2a838876709e1ac",
        "type": "pubkey"
      }
    },

```

Is there any way to retrieve addresses where `script type` is `pubkey` using bitcoin-cli/RPC ?

Thanks.

**Tags:** address, json-rpc, bitcoin-cli, p2pk


---

## 2 answers

### Answer by username_1 on 2023-05-02. Score: 3

The ScriptPubKey `2103f0cc6d142d14a40937f12dbd99dbd9021328f45759e26f1877f2a838876709e1ac` is a pay-to-public-key. There is no defined address for this output type. There is therefore no way to retrieve addresses when `type` is `pubkey`.

> **Comments:**
> **username_0**: Yes, I know that the mentioned ScriptPubKey is a pay-to-public-key for which there is no defined address for this output type which I mentioned in question too. However, since almost all Bitcoin Explorers are able to display the address for corresponding pub-key, I was wondering if there was a simpler way to retrieve the same using RPC/bitcoin-cli. Yes, I was aware of other solutions like github.com/alecalve/python-bitcoin-blockchain-parser which directly read the data from blocks (.dat files) and calculate addresses from corresponding key.  
> **username_2**: You really shouldn't. It's a dated, confusing practice. More modern block explorers do not show P2PKH addresses for P2PK outputs, FWIW.  
> **username_0**: @username_2-username_2 Since most of the explorers I used like blockchain.com, blockchair.com, blockcypher.com etc. were displaying the corresponding addresses, I believed that the process is legitimate. Can you pls explain it a bit further why it is a dated and confusing practice ?  
> **username_2**: For example blockstream.info and mempool.space correctly don't show any address for P2PK. Why is it confusing? Simply because those outputs don't *have* an address - the address corresponds to a different output. It dates from a time when P2PK and P2PKH were the only types used in practice, and addresses were more a way to indicate "identity" rather than "way of conveying a payment output". A block explorer started it, and others copied it for compatibility, but it is nonsensical.  
> **username_0**: @username_2-username_2 Thanks for shedding light on this topic.. now it makes some sense to me. Does it mean that other old explorers which are doing the same, are displaying the wrong addresses for that particular txn/output (p2pk type) or was that technique legitimate for some period of time until new output types comes to the picture ?  
> **username_2**: @username_0 I've written more about the history on https://bitcoin.stackexchange.com/a/117426/208. TL;DR: the meaning of the word address has dramatically shifted starting in 2012.  
> **username_0**: @username_2-username_2 Thank you for sharing the link. It has more info I was looking for.  

---

### Answer by username_3 on 2023-05-02. Score: 0

If you want to try something beyond using the RPC, you can calculate addresses.

In the ScriptPubKey object, remove `21` from the start and `ac` from the end.
The `21` is hex to indicate the length of the pubkey.
`ac` is the opcode for `OP_CHECKSIG`.

You will be left with your pubkey `03f0cc6d142d14a40937f12dbd99dbd9021328f45759e26f1877f2a838876709e1`. From here you can convert it to a base58 encoded address.

An example of how to do this in with python

```
import bitcoin
from bitcoin.core import x
from bitcoin.core import CoreMainParams
from bitcoin.wallet import P2PKHBitcoinAddress

def calc_addr_tool(pubkey, pubtype, p2shtype, wiftype):
    class CoinParams(CoreMainParams):
        MESSAGE_START = b'\x24\xe9\x27\x64'
        DEFAULT_PORT = 7770
        BASE58_PREFIXES = {'PUBKEY_ADDR': int(pubtype),
                           'SCRIPT_ADDR': int(p2shtype),
                           'SECRET_KEY': int(wiftype)}
    bitcoin.params = CoinParams

    try:
        address = str(P2PKHBitcoinAddress.from_pubkey(x(pubkey)))
        return {
            "pubkey": pubkey,
            "pubtype": pubtype,
            "p2shtype": p2shtype,
            "wiftype": wiftype,
            "address": address
        }

    except Exception as e:
        logger.error(f"[calc_addr_tool] Exception: {e}")
        return {"error": str(e)}

```

(from <https://github.com/smk762/kmd_ntx_stats_docker/blob/master/code/kmd_ntx_api/lib_base58.py#L242-L263>)

For a segwit address try

```
from bip32utils import Base58

def p2wpkh_in_p2sh_addr(pk, testnet=False):
    """
    Compressed public key (hex string) -> p2wpkh nested in p2sh address. 'SegWit address.'
    """
    # Script sig is just PUSH(20){hash160(cpk)}
    push_20 = bytes.fromhex("0014")
    script_sig = push_20 + hash160_bytes(bytes.fromhex(pk))

    # Address is then prefix + hash160(script_sig)
    prefix = b"\xc4" if testnet else b"\x05"
    address = Base58.check_encode(prefix + hash160(script_sig))
    return address

```

(from <https://matthewdowney.github.io/create-segwit-address.html>)

Be careful with any output and confirm with sending a small amount first.


---

