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,
- 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.
- 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".
- 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.
- 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.
- 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?