<p>Most of the output addresses follow one of the following 'standard outputs':</p>

<p><strong>Legacy Outputs</strong></p>

<ol>
<li><p>P2PK: <code>scriptPubKey: &lt;public_key&gt; OP_CHECKSIG</code>. Output pays the public key directly and hence does not have a direct address.</p></li>
<li><p>P2PKH: <code>scriptPubKey: OP_DUP OP_HASH160 &lt;hash160 of pubkey&gt; OP_EQUALVERIFY OP_CHECKSIG</code>. For this kind of output you just need to base58check the <code>hash160 of pubkey</code> with version <code>0x00</code> and you will get the addresses that starts with 1.</p></li>
<li><p>P2SH: <code>scriptPubKey: OP_HASH160 &lt;redeem_script&gt; OP_EQUAL</code>. You again need to just base58check the <code>redeem_script</code> with version <code>0x05</code> and you will get an address starting with 3.</p></li>
<li><p>Multisig: <code>scriptPubKey: M &lt;public_key1&gt;...&lt;public_keyN&gt; N OP_CHECKMULTISIG</code>. In this case, you cannot get the addresses as the output pays the public keys directly.</p></li>
</ol>

<p><strong>Segwit Outputs</strong></p>

<ol>
<li>P2WPKH: <code>scriptPubKey: &lt;0x00 (version)&gt; &lt;hash160 of pubkey&gt;</code>. You will need to create a bech32 address using the version and 20-byte redeem script and you will get an address starting with bc1. You can find the python script for bech32 encoding <a href="https://github.com/sipa/bech32/blob/master/ref/python/segwit_addr.py" rel="nofollow noreferrer">here</a>.</li>
<li>P2SH: <code>scriptPubKey &lt;version: 0x00&gt; &lt;sha256 of redeem_script&gt;</code>. Again a bech32 address starting bc1 but the redeem_script is 32 bytes rather than 20 bytes for P2WPKH.</li>
<li>P2SH(P2WPKH): <code>scriptPubKey: OP_HASH160 &lt;redeem_script &gt; OP_EQUAL</code>. The encoding is same as legacy P2SH. Base58check with <code>0x05</code> version. For a more detailed overview on how the 'redeem_script` and addresses are generated for P2SH(P2WPKH) see my other answer <a href="https://bitcoin.stackexchange.com/questions/55391/p2wsh-witness-v0-scripthash-how-to-derive-an-address/83423#83423">here</a>.</li>
</ol>

<p><strong>OP_RETURN</strong></p>

<p>Many times people like to encode some data in the Bitcoin blockchain. For that, you use a OP_RETURN opcode. The <code>scriptPubKey:&lt;OP_RETURN&gt;&lt;OP_PUSHDATA1&gt;&lt;bytes to push&gt;&lt;script&gt;</code>.</p>

CC BY-SA 4.0
2019-05-19T11:02:16.063Z
This is technically not true - you have outputs on the chain that don't match any of the above, the most obvious being P2PK outputs from the early blocks before hash160 addresses were commonly used
Raghav Sood
CC BY-SA 4.0
2019-05-20T09:57:52.910Z
So, Tx messages which contains output scripts start with  0x00, is belongs to which category?
it seems that it is a P2SH (like output scripts which start with 0xA9), but generated output address from that way is not valid.
Saeed
CC BY-SA 4.0
2019-05-20T10:13:37.517Z
@Saeed It's segwit outputs. Legacy P2SH needs to have OP_HASH160 <redeemscript> OP_Equal. However, Segwit P2SH is 0x00 <redeem_script>. For a non-segwit aware client, this looks as if this is anyone can spend output. But Segwit aware clients will look for the scriptSig in the witness section of the transaction.
Ugam Kamat
CC BY-SA 4.0
2019-05-25T08:08:24.703Z
@UgamKamat How can i detect between legacy outputs and segwit outputs?
as you said before, there are 6 different way to extract output address based on legacy or segwit outputs. is all these ways?
Saeed
CC BY-SA 4.0
2019-05-25T08:18:14.560Z
@saeed as I said earlier, those are the current STANDARD outputs. Bitcoin is a programmable money, and as a result accept a number of ways in which you can lock the output, but that will not be standard
Ugam Kamat
CC BY-SA 4.0
2019-05-19T10:33:40.613Z
2019-05-19T11:51:46.063Z
2019-05-19T11:51:46.063Z
Ugam Kamat
<p>I want to extract output addresses from tx messages in any bitcoin pcap.</p>

<p>currently, i extract tx messages which it's output script starts with "0x76" or "0xA9", but i don't know how can i extract the other types of op_codes which is introduced here:</p>

<p><a href="https://en.bitcoin.it/wiki/Script" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Script</a></p>

<transactions><address><script>
How i can extract all bitcoin output addresses from tx message?
Saeed
# How i can extract all bitcoin output addresses from tx message?

(Asked by: username_0 on 2019-05-19)

I want to extract output addresses from tx messages in any bitcoin pcap.

currently, i extract tx messages which it's output script starts with "0x76" or "0xA9", but i don't know how can i extract the other types of op\_codes which is introduced here:

<https://en.bitcoin.it/wiki/Script>

**Tags:** transactions, address, script


---

## 1 answers

### Answer by username_1 on 2019-05-19. Score: 3

Most of the output addresses follow one of the following 'standard outputs':

**Legacy Outputs**

1. P2PK: `scriptPubKey:  OP_CHECKSIG`. Output pays the public key directly and hence does not have a direct address.
2. P2PKH: `scriptPubKey: OP_DUP OP_HASH160  OP_EQUALVERIFY OP_CHECKSIG`. For this kind of output you just need to base58check the `hash160 of pubkey` with version `0x00` and you will get the addresses that starts with 1.
3. P2SH: `scriptPubKey: OP_HASH160  OP_EQUAL`. You again need to just base58check the `redeem_script` with version `0x05` and you will get an address starting with 3.
4. Multisig: `scriptPubKey: M ... N OP_CHECKMULTISIG`. In this case, you cannot get the addresses as the output pays the public keys directly.

**Segwit Outputs**

1. P2WPKH: `scriptPubKey: <0x00 (version)>` . You will need to create a bech32 address using the version and 20-byte redeem script and you will get an address starting with bc1. You can find the python script for bech32 encoding [here](https://github.com/sipa/bech32/blob/master/ref/python/segwit_addr.py).
2. P2SH: `scriptPubKey` . Again a bech32 address starting bc1 but the redeem\_script is 32 bytes rather than 20 bytes for P2WPKH.
3. P2SH(P2WPKH): `scriptPubKey: OP_HASH160  OP_EQUAL`. The encoding is same as legacy P2SH. Base58check with `0x05` version. For a more detailed overview on how the 'redeem\_script` and addresses are generated for P2SH(P2WPKH) see my other answer [here](https://bitcoin.stackexchange.com/questions/55391/p2wsh-witness-v0-scripthash-how-to-derive-an-address/83423#83423).

**OP\_RETURN**

Many times people like to encode some data in the Bitcoin blockchain. For that, you use a OP\_RETURN opcode. The `scriptPubKey:`

> **Comments:**
> **username_2**: This is technically not true - you have outputs on the chain that don't match any of the above, the most obvious being P2PK outputs from the early blocks before hash160 addresses were commonly used  
> **username_0**: So, Tx messages which contains output scripts start with  0x00, is belongs to which category? it seems that it is a P2SH (like output scripts which start with 0xA9), but generated output address from that way is not valid.  
> **username_1**: @username_0 It's segwit outputs. Legacy P2SH needs to have OP_HASH160 <redeemscript> OP_Equal. However, Segwit P2SH is 0x00 <redeem_script>. For a non-segwit aware client, this looks as if this is anyone can spend output. But Segwit aware clients will look for the scriptSig in the witness section of the transaction.  
> **username_0**: @UgamKamat How can i detect between legacy outputs and segwit outputs? as you said before, there are 6 different way to extract output address based on legacy or segwit outputs. is all these ways?  
> **username_1**: @saeed as I said earlier, those are the current STANDARD outputs. Bitcoin is a programmable money, and as a result accept a number of ways in which you can lock the output, but that will not be standard  

---

