0

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.

1 Answer 1

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, 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, and is a9 + that 20-byte hash + 87.
  • The native segwit address format introduced in BIP173, 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), 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, 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), 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.