<blockquote>
<p>In this python code i'm trying to create a pubkey_script:</p>
<p><code>...</code></p>
</blockquote>
<p>That code is incorrect, as it is ignoring the version byte in the address, which indicates it's a P2SH address, while the scriptPubKey you're using is for P2PKH addresses.</p>
<p>There are currently 4 different address formats in use in Bitcoin:</p>
<ul>
<li>The traditional P2PKH (&quot;pay to public key hash&quot;) address format, which has existed since the original release of the Bitcoin software in 2009. It is the Base58Check encoding of <code>00</code> (the version byte) + the 20-byte hash160 of the public key. The corresponding scriptPubKey is as you say, <code>76a914</code> + that 20-byte hash + <code>88ac</code>.</li>
<li>The P2SH (&quot;pay to script hash&quot;) address format introduced in <a href="https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki" rel="nofollow noreferrer">BIP13</a>, in 2012. It is the Base58Check encoding of <code>05</code> (the version byte) + the 20-byte hash160 of the script. The corresponding scriptPubKey is described in <a href="https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki" rel="nofollow noreferrer">BIP16</a>, and is <code>a9</code> + that 20-byte hash + <code>87</code>.</li>
<li>The native segwit address format introduced in <a href="https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki" rel="nofollow noreferrer">BIP173</a>, in 2017. It is the Bech32 encoding (see the same BIP) with prefix &quot;bc&quot; of the witness version number concatenated with the 8-to-5 bit conversion of the witness program. BIP173 addresses are only used for P2WPKH and P2WSH addresses (defined in <a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki" rel="nofollow noreferrer">BIP141</a>), which have version number 0, and 20-byte (public key hash160, P2WPKH) or 32 byte (script sha256, P2WSH) programs. The corresponding scriptPubKey is <code>00</code> plus the witness program.</li>
<li>The amended native segwit address format introduced in <a href="https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki" rel="nofollow noreferrer">BIP350</a>, in 2021. It is the Bech32m encoding (see the same BIP) with prefix &quot;bc&quot; of the witness version number concatenated with the 8-to-5 bit conversion of the witness program. BIP350 addresses are <em>currently</em> only used for P2TR addresses (defined in <a href="https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki" rel="nofollow noreferrer">BIP341</a>), which have version number 1, and 32-byte x-only tweaked public keys as witness program, but BIP350 may also be used for future address types. The corresponding scriptPubKey is byte(0x50 + witness version) plus the witness program.</li>
</ul>
<p>The version numbers and prefixes listed above are only for Bitcoin mainnet. Testnet and other testing networks like regtest/signet have other prefixes.</p>

CC BY-SA 4.0
2022-12-20T15:44:01.027Z
2022-12-20T15:49:27.320Z
2022-12-20T15:49:27.320Z
Pieter Wuille
<p>This may be a simple question for others but I'm struggling trying to understand pubkey_script in the generation transaction.</p>
<p>In this Python code I'm trying to create a pubkey_script:</p>
<pre><code>from base58 import b58decode_check

wallet_address = '35XJFXPUKGXqMyXZ2jFNSMgZqpfnWNQBQV'
pubkey_script = &quot;76a914&quot; + b58decode_check(wallet_address[1:].hex() + &quot;88ac&quot;
print(pubkey_script)
</code></pre>
<p>output:</p>
<blockquote>
<p>76a9142a09f90b21e949ccdaa85366d1bad2d841c9054a88ac</p>
</blockquote>
<p>But when I try to decode this hex string in RPC 'decodescript'
I am getting this response:</p>
<pre><code>{
    &quot;asm&quot;: &quot;OP_DUP OP_HASH160 2a09f90b21e949ccdaa85366d1bad2d841c9054a OP_EQUALVERIFY OP_CHECKSIG&quot;,
    &quot;desc&quot;: &quot;addr(14qHKyu2mNDTGoq7udan1jKdhJP4u6MQGD)#c7zupad7&quot;,
    &quot;address&quot;: &quot;14qHKyu2mNDTGoq7udan1jKdhJP4u6MQGD&quot;,
    &quot;type&quot;: &quot;pubkeyhash&quot;,
    &quot;p2sh&quot;: &quot;3D7pDTkWYGGeMbH7XEEWFxL1SwuL3fzNhu&quot;,
    &quot;segwit&quot;: {
        &quot;asm&quot;: &quot;0 2a09f90b21e949ccdaa85366d1bad2d841c9054a&quot;,
        &quot;desc&quot;: &quot;addr(bc1q9gyljzepa9yuek4g2dndrwkjmpqujp223l6h2y)#lz4ua3xn&quot;,
        &quot;hex&quot;: &quot;00142a09f90b21e949ccdaa85366d1bad2d841c9054a&quot;,
        &quot;address&quot;: &quot;bc1q9gyljzepa9yuek4g2dndrwkjmpqujp223l6h2y&quot;,
        &quot;type&quot;: &quot;witness_v0_keyhash&quot;,
        &quot;p2sh-segwit&quot;: &quot;3BbpKmKS2MdM3tJxT6nsPdvfHRVEyByVpy&quot;
    }
}
</code></pre>
<p>From what I see in the decoded response, the original address is nowhere to be seen. I'd like to know what am I doing wrong.</p>

<python>
What is the correct way of formatting pubkey_script in coinbase generation transaction?
chronus
# What is the correct way of formatting pubkey_script in coinbase generation transaction?

(Asked by: username_0 on 2022-12-20)

This may be a simple question for others but I'm struggling trying to understand pubkey\_script in the generation transaction.

In this Python code I'm trying to create a pubkey\_script:

```
from base58 import b58decode_check

wallet_address = '35XJFXPUKGXqMyXZ2jFNSMgZqpfnWNQBQV'
pubkey_script = "76a914" + b58decode_check(wallet_address[1:].hex() + "88ac"
print(pubkey_script)

```

output:

> 76a9142a09f90b21e949ccdaa85366d1bad2d841c9054a88ac

But when I try to decode this hex string in RPC 'decodescript'
I am getting this response:

```
{
    "asm": "OP_DUP OP_HASH160 2a09f90b21e949ccdaa85366d1bad2d841c9054a OP_EQUALVERIFY OP_CHECKSIG",
    "desc": "addr(14qHKyu2mNDTGoq7udan1jKdhJP4u6MQGD)#c7zupad7",
    "address": "14qHKyu2mNDTGoq7udan1jKdhJP4u6MQGD",
    "type": "pubkeyhash",
    "p2sh": "3D7pDTkWYGGeMbH7XEEWFxL1SwuL3fzNhu",
    "segwit": {
        "asm": "0 2a09f90b21e949ccdaa85366d1bad2d841c9054a",
        "desc": "addr(bc1q9gyljzepa9yuek4g2dndrwkjmpqujp223l6h2y)#lz4ua3xn",
        "hex": "00142a09f90b21e949ccdaa85366d1bad2d841c9054a",
        "address": "bc1q9gyljzepa9yuek4g2dndrwkjmpqujp223l6h2y",
        "type": "witness_v0_keyhash",
        "p2sh-segwit": "3BbpKmKS2MdM3tJxT6nsPdvfHRVEyByVpy"
    }
}

```

From what I see in the decoded response, the original address is nowhere to be seen. I'd like to know what am I doing wrong.

**Tags:** python


---

## 1 answers

### Answer by username_1 on 2022-12-20. Score: 1

> In this python code i'm trying to create a pubkey\_script:
>
> `...`

That code is incorrect, as it is ignoring the version byte in the address, which indicates it's a P2SH address, while the scriptPubKey you're using is for P2PKH addresses.

There are currently 4 different address formats in use in Bitcoin:

* The traditional P2PKH ("pay to public key hash") address format, which has existed since the original release of the Bitcoin software in 2009. It is the Base58Check encoding of `00` (the version byte) + the 20-byte hash160 of the public key. The corresponding scriptPubKey is as you say, `76a914` + that 20-byte hash + `88ac`.
* The P2SH ("pay to script hash") address format introduced in [BIP13](https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki), in 2012. It is the Base58Check encoding of `05` (the version byte) + the 20-byte hash160 of the script. The corresponding scriptPubKey is described in [BIP16](https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki), and is `a9` + that 20-byte hash + `87`.
* The native segwit address format introduced in [BIP173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki), in 2017. It is the Bech32 encoding (see the same BIP) with prefix "bc" of the witness version number concatenated with the 8-to-5 bit conversion of the witness program. BIP173 addresses are only used for P2WPKH and P2WSH addresses (defined in [BIP141](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki)), which have version number 0, and 20-byte (public key hash160, P2WPKH) or 32 byte (script sha256, P2WSH) programs. The corresponding scriptPubKey is `00` plus the witness program.
* The amended native segwit address format introduced in [BIP350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki), in 2021. It is the Bech32m encoding (see the same BIP) with prefix "bc" of the witness version number concatenated with the 8-to-5 bit conversion of the witness program. BIP350 addresses are *currently* only used for P2TR addresses (defined in [BIP341](https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki)), which have version number 1, and 32-byte x-only tweaked public keys as witness program, but BIP350 may also be used for future address types. The corresponding scriptPubKey is byte(0x50 + witness version) plus the witness program.

The version numbers and prefixes listed above are only for Bitcoin mainnet. Testnet and other testing networks like regtest/signet have other prefixes.


---

