<p>The first example that you have shown is a P2PKH address. The 0x14 indicates how many bytes to push onto the stack. 0x14 is 20 in decimal so the script pushes the next 20 bytes on the stack which is <code>6d 1d 74 58 95 6e 80 cd b4 c3 3f 1e d5 8e 91 c4 92 1a 85 d0</code>.</p>

<p>The second case is a Pay to Script Hash example (P2SH). The script is <code>HASH160 Push 20 bytes &lt;redeem_script&gt; OP_EQUAL&gt;</code>. Hence your redeem script in the second case is <code>16523302f2eed0e0aa6a4c1d0a41396fc26f536e</code>. You would need to then Base58Check it with prefix of 0x05, this gives you the address as <code>33j3G4xMEn4CtKL5iYnsr97ww67wYh8TPY</code>. </p>

<p>Below is the python script that gives you the address:</p>

<pre><code>import base58
from hashlib import sha256

redeem_script = '16523302f2eed0e0aa6a4c1d0a41396fc26f536e'
version = '05'
payload = version + redeem_script
checksum = sha256(sha256(bytes.fromhex(payload)).digest()).hexdigest()[:8]

pre_encoded_address = payload + checksum
address = base58.b58encode(bytes.fromhex(pre_encoded_address)).decode()
</code></pre>

CC BY-SA 4.0
2019-05-18T09:26:26.927Z
thanks, can you splain how you calculate "33j3G4xMEn4CtKL5iYnsr97ww67wYh8TPY" from this raw script : "16523302f2eed0e0aa6a4c1d0a41396fc26f536e" ??
Saeed
CC BY-SA 4.0
2019-05-18T09:57:07.793Z
That's just Base58Check. You first append 0x05 to the redeem script. Then you double hash it with sha256 and then append the first 4 bytes at the end to `0x05+redeemscript`. Those first 4 bytes is the checksum. Then you base58 encode it.
Ugam Kamat
CC BY-SA 4.0
2019-05-18T10:22:51.370Z
Thank you a lot !

you mean that

redeem script is "16523302f2eed0e0aa6a4c1d0a41396fc26f536e"
then  A1 is equal to "0516523302f2eed0e0aa6a4c1d0a41396fc26f536e"

then A2 is  sha256(sha256(first))
and then checksum is first 4 bytes of A2.
then answer is base58encode(first+checksum)
.

but answer is not equal to "33j3G4xMEn4CtKL5iYnsr97ww67wYh8TPY" !
Saeed
CC BY-SA 4.0
2019-05-18T11:30:45.220Z
@Saeed I have edited my answer with a python script that shows you how to get the address from the redeem script. I think the common mistake most people do is hashing the plain script rather than using the bytes from the hexadecimal.
Ugam Kamat
CC BY-SA 4.0
2019-05-19T07:06:15.373Z
Thanks a lot. it is correct. but, what about the other op codes?
it seems that each op code has a different method to extract an output address.
how i can extract address from other types of op_code like "0x00" and "0x40" and etc?
Saeed
CC BY-SA 4.0
2019-05-18T08:32:10.897Z
2019-05-18T11:30:19.863Z
2019-05-18T11:30:19.863Z
Ugam Kamat
<p>This seems to be a classical P2PKH script which start with <code>op_dup</code> flowed by <code>op_hash160</code> if you look at the next byte there is the hex value <code>14</code> which is 20 in decimal and means read 20 byte of data and push it to the stack.</p>

<p>I have created a video in the past showing step by step how to dissect a P2PKH tx. Look: <a href="https://youtu.be/1n4g3eYX1UI" rel="nofollow noreferrer">https://youtu.be/1n4g3eYX1UI</a></p>

CC BY-SA 4.0
2019-05-18T07:26:37.680Z
yes, it is correct about my frist example.

but, what about my second example? which starts with 0xA9 and then 14 apperes?

i assumed it line the first example and i read 20 byte of data to push it to stack, but the result output address is incorrect
Saeed
CC BY-SA 4.0
2019-05-18T08:41:06.640Z
I think the general rule you have been missing is that if a hex value does not translate to an op code you should read that many bytes of data and push it to the stack.
Rene Pickhardt
CC BY-SA 4.0
2019-05-18T07:04:41.587Z
2019-05-18T07:04:41.587Z
Rene Pickhardt
<p>I'm working on extracting output addresses from tx messages in BITCOIN packets.</p>

<p>Currently, I extract address from output scripts which starts with 0x76(OP_DUP)
like this :</p>

<pre><code>76 a9 14 6d 1d 74 58 95 6e 80 cd b4 c3 3f 1e d5 8e 91 c4 92 1a 85 d0 88
</code></pre>

<p>but i don't have any idea about some output scripts which start with 0xA9(OP_HASH160).</p>

<p>As an example, i don't know in below example, what is the raw script, which is used to generate output address?</p>

<pre><code>a9 14 16 52 33 02 f2 ee d0 e0 aa 6a 4c 1d 0a 41 39 6f c2 6f 53 6e 87
</code></pre>

<p>How i can extract output address from output scripts which start with -xA9?</p>

<transactions><script>
How to extract raw script from tx messages when the output script starts with 0xA9?
Saeed
# How to extract raw script from tx messages when the output script starts with 0xA9?

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

I'm working on extracting output addresses from tx messages in BITCOIN packets.

Currently, I extract address from output scripts which starts with 0x76(OP\_DUP)
like this :

```
76 a9 14 6d 1d 74 58 95 6e 80 cd b4 c3 3f 1e d5 8e 91 c4 92 1a 85 d0 88

```

but i don't have any idea about some output scripts which start with 0xA9(OP\_HASH160).

As an example, i don't know in below example, what is the raw script, which is used to generate output address?

```
a9 14 16 52 33 02 f2 ee d0 e0 aa 6a 4c 1d 0a 41 39 6f c2 6f 53 6e 87

```

How i can extract output address from output scripts which start with -xA9?

**Tags:** transactions, script


---

## 2 answers

### Answer by username_1 on 2019-05-18. Score: 1 | Accepted answer

The first example that you have shown is a P2PKH address. The 0x14 indicates how many bytes to push onto the stack. 0x14 is 20 in decimal so the script pushes the next 20 bytes on the stack which is `6d 1d 74 58 95 6e 80 cd b4 c3 3f 1e d5 8e 91 c4 92 1a 85 d0`.

The second case is a Pay to Script Hash example (P2SH). The script is `HASH160 Push 20 bytes <redeem_script> OP_EQUAL>`. Hence your redeem script in the second case is `16523302f2eed0e0aa6a4c1d0a41396fc26f536e`. You would need to then Base58Check it with prefix of 0x05, this gives you the address as `33j3G4xMEn4CtKL5iYnsr97ww67wYh8TPY`.

Below is the python script that gives you the address:

```
import base58
from hashlib import sha256

redeem_script = '16523302f2eed0e0aa6a4c1d0a41396fc26f536e'
version = '05'
payload = version + redeem_script
checksum = sha256(sha256(bytes.fromhex(payload)).digest()).hexdigest()[:8]

pre_encoded_address = payload + checksum
address = base58.b58encode(bytes.fromhex(pre_encoded_address)).decode()

```

> **Comments:**
> **username_0**: thanks, can you splain how you calculate "33j3G4xMEn4CtKL5iYnsr97ww67wYh8TPY" from this raw script : "16523302f2eed0e0aa6a4c1d0a41396fc26f536e" ??  
> **username_1**: That's just Base58Check. You first append 0x05 to the redeem script. Then you double hash it with sha256 and then append the first 4 bytes at the end to `0x05+redeemscript`. Those first 4 bytes is the checksum. Then you base58 encode it.  
> **username_0**: Thank you a lot !  you mean that  redeem script is "16523302f2eed0e0aa6a4c1d0a41396fc26f536e" then  A1 is equal to "0516523302f2eed0e0aa6a4c1d0a41396fc26f536e"  then A2 is  sha256(sha256(first)) and then checksum is first 4 bytes of A2. then answer is base58encode(first+checksum) .  but answer is not equal to "33j3G4xMEn4CtKL5iYnsr97ww67wYh8TPY" !  
> **username_1**: @username_0 I have edited my answer with a python script that shows you how to get the address from the redeem script. I think the common mistake most people do is hashing the plain script rather than using the bytes from the hexadecimal.  
> **username_0**: Thanks a lot. it is correct. but, what about the other op codes? it seems that each op code has a different method to extract an output address. how i can extract address from other types of op_code like "0x00" and "0x40" and etc?  

---

### Answer by username_2 on 2019-05-18. Score: 1

This seems to be a classical P2PKH script which start with `op_dup` flowed by `op_hash160` if you look at the next byte there is the hex value `14` which is 20 in decimal and means read 20 byte of data and push it to the stack.

I have created a video in the past showing step by step how to dissect a P2PKH tx. Look: <https://youtu.be/1n4g3eYX1UI>

> **Comments:**
> **username_0**: yes, it is correct about my frist example.  but, what about my second example? which starts with 0xA9 and then 14 apperes?  i assumed it line the first example and i read 20 byte of data to push it to stack, but the result output address is incorrect  
> **username_2**: I think the general rule you have been missing is that if a hex value does not translate to an op code you should read that many bytes of data and push it to the stack.  

---

