-1

I was wondering if it is possible to convert a compressed bitcoin address to its scriptPubKey? Here is a code that i wrote using python

import base58

# Compressed public Bitcoin address
addr = "1MY53uw7T3sLwL6FiGrM2LsiutSktmhSv2"

# Decode Base58Check-encoded address to obtain public key hash
decoded_addr = base58.b58decode_check(addr)
pubkey_hash = decoded_addr[1:].hex()

# Create script pubkey by adding prefix and suffix bytes
prefix = "21"  # Compressed public key prefix byte
suffix = "ac"  # P2PKH script suffix byte
scriptpubkey = prefix + pubkey_hash + suffix

# Print script pubkey in hexadecimal form
print(scriptpubkey)

the output the im getting is

210723a6c599ca6f0db6d95e6e33d6f961c4b4dbd5ac

I don't understand what is this Hexadecimal value, is my code right ? or am i doing something wrong?

Basically Followed these steps,

  1. Decode the Base58Check-encoded address: Start with the compressed public Bitcoin address, which in this case is "1MY53uw7T3sLwL6FiGrM2LsiutSktmhSv2". Decode this address using the Base58Check encoding algorithm to obtain the raw bytes. The resulting bytes should be 21 bytes long.
  2. Extract the public key hash: The first byte of the decoded address is the version byte, which should be 0x00 for a P2PKH address. The next 20 bytes are the public key hash, which is the part that we're interested in. In this case, the public key hash is "0723a6c599ca6f0db6d95e6e33d6f961c4b4dbd5".
  3. Add the script prefix byte: To create the scriptPubKey, we need to add some bytes to the public key hash. The first byte we need to add is the script prefix byte, which indicates the type of script that the address corresponds to. In this case, we're dealing with a compressed public key, so the prefix byte is 0x21.
  4. Add the script suffix byte: The second byte we need to add is the script suffix byte, which completes the scriptPubKey. For a P2PKH script, the suffix byte is always 0xac.
  5. Combine the bytes: Finally, we need to combine the bytes we've generated to create the full scriptPubKey. In this case, the scriptPubKey is the concatenation of the prefix byte, the public key hash, and the suffix byte, in that order. This gives us the following scriptPubKey in hexadecimal format: "210723a6c599ca6f0db6d95e6e33d6f961c4b4dbd5ac".

Did i do something wrong? how do i retrieve the scriptPubKey?

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Melwyn
  • 1
  • 1

1 Answers1

0

Bitcoin scripts don't have a prefix identifying the 'type' (except for segwit, which your Q is not about) nor a suffix as such. Rather they consist of a series of opcodes. Certain sequences (patterns) of opcodes are used for -- and recognized (by some software) as -- conventional script 'types', but it is possible to use scripts that are not any of the conventional 'types'.

A P2PKH (pay to public key hash) script, as detailed near the bottom of the page I linked, consists of

  1. OP_DUP = 0x76
  2. OP_PUSH160 = 0xA9
  3. push pubkeyhash = 0x14 {20byteshash}
  4. OP_EQUALVERIFY = 0x88
  5. OP_CHECKSIG = 0xAC

Thus the combined script in hex is 76A914{40digitshash}88AC .

What you have created is almost the old (but still usable) P2PK (pay to public key) script detailed even lower: it consists only of

  1. push pubkey -- not hash; for compressed = 0x21 {33bytesvalue}
  2. OP_CHECKSIG = 0xAC

This combines to 21{66digitskey}AC and you could consider 21 and AC to be a prefix and suffix, although they aren't defined as such, and 21 by itself does not define the type at all. Also you need the publickey itself not its hash, and if you only have the address that only gives the hash not the publickey.

dave_thompson_085
  • 34,712
  • 6
  • 50
  • 70