<p>P2PK scripts have the form <code>PUSH &lt;KEY&gt; OP_CHECKSIG</code>.</p>
<p>Your example:</p>
<pre><code>41
0400d0ade32217e076945e0946ef7bed72d9aea035aa8891e4bf0749ae6e24f8a7d3ea56efafe472ac3943dbed3af7c093729720ac9ab04e8eba09286e3a00fe41
ac
</code></pre>
<p>Means push <code>0x41</code> (65) bytes. This is followed by the public key (starting with <code>04</code> incidicating it is an uncompressed public key). Finally, <code>0xac</code> is OP_CHECKSIG.</p>
<p>So to summarise, the key is <code>0400d0ade32217e076945e0946ef7bed72d9aea035aa8891e4bf0749ae6e24f8a7d3ea56efafe472ac3943dbed3af7c093729720ac9ab04e8eba09286e3a00fe41</code></p>
<p>Question 3 is achieved following the exact steps on the wiki page you linked.</p>
<p>Common other formats of scriptPubKey are:</p>
<ul>
<li><code>OP_DUP OP_HASH160 &lt;push of 20-byte public key hash&gt; OP_EQUALVERIFY OP_CHECKSIG</code> (P2PKH)</li>
<li><code>OP_HASH160 &lt;push of 20-byte script hash&gt; OP_EQUAL</code> (P2SH)</li>
<li><code>OP_0 &lt;push of 20-byte key hash&gt;</code> (P2WPKH)</li>
<li><code>OP_0 &lt;push of 32-byte script hash&gt;</code> (P2WSH)</li>
<li><code>OP_1 &lt;push of 32-byte public key&gt;</code> (P2TR)</li>
</ul>
<p>Only P2PKH and P2SH are encoded in the same way as the wiki (known as 'legacy addresses&quot;). P2WPKH, P2WSH and P2TR addresses are all encoded with Bech32(m), known as native segwit addresses. The method for encoding these addresses is a bit more technically complex, but there are plenty of code snippets to do so online.</p>
<p>Note that a 20-byte push is <code>0x14</code> and a 32-byte push is <code>0x20</code>.</p>

CC BY-SA 4.0
2022-01-04T06:28:00.637Z
Thank you for your answer. Let me send a new answer that contains a snippet code written in Python.
sci9
CC BY-SA 4.0
2022-01-04T06:41:02.220Z
I wound be grateful if you could provide some references for patterns that involve in other type of scripts.
sci9
CC BY-SA 4.0
2022-01-04T08:10:38.367Z
Done, added to my answer
meshcollider
CC BY-SA 4.0
2022-01-04T08:24:43.423Z
merci beaucoup.
sci9
CC BY-SA 4.0
2022-01-02T18:14:28.780Z
2022-01-06T07:32:39.257Z
2022-01-06T07:32:39.257Z
meshcollider
<p>Here is a snippet code that demonstrate how one can create a Bitcoin address from scriptPubKey:</p>
<pre><code>import binascii
import hashlib

import base58


script_pub_key_hex = &quot;410400d0ade32217e076945e0946ef7bed72d9aea035aa8891e4bf0749ae6e24f8a7d3ea56efafe472ac3943dbed3af7c093729720ac9ab04e8eba09286e3a00fe41ac&quot;

# generate public key hash from scriptPubKey hex
ripemd160 = hashlib.new('ripemd160')
ripemd160.update(hashlib.sha256(binascii.unhexlify(
        script_pub_key_hex[2:-2])).digest())
pub_key_hash_with_version = b'\x00' + ripemd160.digest()

# generate base58check address from public key hash
address = base58.b58encode(
        pub_key_hash_with_version + hashlib.sha256(hashlib.sha256(pub_key_hash_with_version).digest()).digest()[:4])
    
</code></pre>

CC BY-SA 4.0
2022-01-04T06:35:19.323Z
2022-01-04T08:51:42.417Z
2022-01-04T08:51:42.417Z
sci9
<p>This is a recurring question (see <a href="https://bitcoin.stackexchange.com/questions/111548">Parse the output field of getrawtransaction to generate a common address hash</a> ), but I have a different spin.</p>
<p>Consider the following <code>scriptPubKey</code> field:</p>
<pre><code>  &quot;scriptPubKey&quot;: {
    &quot;asm&quot;: &quot;0400d0ade32217e076945e0946ef7bed72d9aea035aa8891e4bf0749ae6e24f8a7d3ea56efafe472ac3943dbed3af7c093729720ac9ab04e8eba09286e3a00fe41 OP_CHECKSIG&quot;,
    &quot;hex&quot;: &quot;410400d0ade32217e076945e0946ef7bed72d9aea035aa8891e4bf0749ae6e24f8a7d3ea56efafe472ac3943dbed3af7c093729720ac9ab04e8eba09286e3a00fe41ac&quot;,
    &quot;type&quot;: &quot;pubkey&quot;
  }
</code></pre>
<p>I was unable to construct Public Key based on the information provided in the wiki <a href="https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses</a>.</p>
<p>1- Given the following HEX, what is the number of bytes in the Public Key? How we can calculate it? <code>410400d0ade32217e076945e0946ef7bed72d9aea035aa8891e4bf0749ae6e24f8a7d3ea56efafe472ac3943dbed3af7c093729720ac9ab04e8eba09286e3a00fe41ac</code></p>
<p>2- How to reconstruct Public Key given the HEX above?</p>
<p>3- How are websites like Blockchair.com translating this HEX to the address <code>1JAL1WRvwbJqWWd1Ktks4Q89zCeUYSQM12</code>?</p>

<address><public-key><address-generation>
How to convert script_pub_key of type pubkey to public_key
sci9
# How to convert script_pub_key of type pubkey to public_key

(Asked by: username_0 on 2022-01-02)

This is a recurring question (see [Parse the output field of getrawtransaction to generate a common address hash](https://bitcoin.stackexchange.com/questions/111548) ), but I have a different spin.

Consider the following `scriptPubKey` field:

```
  "scriptPubKey": {
    "asm": "0400d0ade32217e076945e0946ef7bed72d9aea035aa8891e4bf0749ae6e24f8a7d3ea56efafe472ac3943dbed3af7c093729720ac9ab04e8eba09286e3a00fe41 OP_CHECKSIG",
    "hex": "410400d0ade32217e076945e0946ef7bed72d9aea035aa8891e4bf0749ae6e24f8a7d3ea56efafe472ac3943dbed3af7c093729720ac9ab04e8eba09286e3a00fe41ac",
    "type": "pubkey"
  }

```

I was unable to construct Public Key based on the information provided in the wiki <https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses>.

1- Given the following HEX, what is the number of bytes in the Public Key? How we can calculate it? `410400d0ade32217e076945e0946ef7bed72d9aea035aa8891e4bf0749ae6e24f8a7d3ea56efafe472ac3943dbed3af7c093729720ac9ab04e8eba09286e3a00fe41ac`

2- How to reconstruct Public Key given the HEX above?

3- How are websites like Blockchair.com translating this HEX to the address `1JAL1WRvwbJqWWd1Ktks4Q89zCeUYSQM12`?

**Tags:** address, public-key, address-generation


---

## 2 answers

### Answer by username_1 on 2022-01-02. Score: 1 | Accepted answer

P2PK scripts have the form `PUSH <KEY> OP_CHECKSIG`.

Your example:

```
41
0400d0ade32217e076945e0946ef7bed72d9aea035aa8891e4bf0749ae6e24f8a7d3ea56efafe472ac3943dbed3af7c093729720ac9ab04e8eba09286e3a00fe41
ac

```

Means push `0x41` (65) bytes. This is followed by the public key (starting with `04` incidicating it is an uncompressed public key). Finally, `0xac` is OP\_CHECKSIG.

So to summarise, the key is `0400d0ade32217e076945e0946ef7bed72d9aea035aa8891e4bf0749ae6e24f8a7d3ea56efafe472ac3943dbed3af7c093729720ac9ab04e8eba09286e3a00fe41`

Question 3 is achieved following the exact steps on the wiki page you linked.

Common other formats of scriptPubKey are:

* `OP_DUP OP_HASH160 <push of 20-byte public key hash> OP_EQUALVERIFY OP_CHECKSIG` (P2PKH)
* `OP_HASH160 <push of 20-byte script hash> OP_EQUAL` (P2SH)
* `OP_0 <push of 20-byte key hash>` (P2WPKH)
* `OP_0 <push of 32-byte script hash>` (P2WSH)
* `OP_1 <push of 32-byte public key>` (P2TR)

Only P2PKH and P2SH are encoded in the same way as the wiki (known as 'legacy addresses"). P2WPKH, P2WSH and P2TR addresses are all encoded with Bech32(m), known as native segwit addresses. The method for encoding these addresses is a bit more technically complex, but there are plenty of code snippets to do so online.

Note that a 20-byte push is `0x14` and a 32-byte push is `0x20`.

> **Comments:**
> **username_0**: Thank you for your answer. Let me send a new answer that contains a snippet code written in Python.  
> **username_0**: I wound be grateful if you could provide some references for patterns that involve in other type of scripts.  
> **username_1**: Done, added to my answer  
> **username_0**: merci beaucoup.  

---

### Answer by username_0 on 2022-01-04. Score: 0

Here is a snippet code that demonstrate how one can create a Bitcoin address from scriptPubKey:

```
import binascii
import hashlib

import base58

script_pub_key_hex = "410400d0ade32217e076945e0946ef7bed72d9aea035aa8891e4bf0749ae6e24f8a7d3ea56efafe472ac3943dbed3af7c093729720ac9ab04e8eba09286e3a00fe41ac"

# generate public key hash from scriptPubKey hex
ripemd160 = hashlib.new('ripemd160')
ripemd160.update(hashlib.sha256(binascii.unhexlify(
        script_pub_key_hex[2:-2])).digest())
pub_key_hash_with_version = b'\x00' + ripemd160.digest()

# generate base58check address from public key hash
address = base58.b58encode(
        pub_key_hash_with_version + hashlib.sha256(hashlib.sha256(pub_key_hash_with_version).digest()).digest()[:4])
    

```


---

