Hi there
I can help you out with that. I did the calculation on my end and derived the same address as you got from Blockchair: 1G7mD4PK42vKF8bDEvi93peFwmFpSZuzuJ
Let me explain the process:
First we need to extract the Public Key Hash from the scriptPubkey. The scriptPubkey is in this format:
OP_DUP OP_HASH160 <PubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
We need to strip out the PubKeyHash. Which in your key would look like:
76a914 <PubKeyHash> 88ac
Next we shall create the address:
Address is derived from the <PubKeyHash> as follows:
- Add a version prefix (0x00 for mainnet P2PKH addresses) to the <PubKeyHash>.
- Calculate the double SHA-256 hash of the prefixed <PubKeyHash>.
- Take the first 4 bytes of this hash as the checksum.
- Concatenate the checksum to the prefixed <PubKeyHash>.
- Encode the result in Base58 to get the Bitcoin address.
Here's a small python script for you that will do what you need
from hashlib import sha256
def base58_encode(data):
"""
Encode bytes into a base58-encoded string
"""
alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
n = int.from_bytes(data, 'big')
res = []
while n > 0:
n, r = divmod(n, 58)
res.append(alphabet[r])
res = ''.join(res[::-1])
czero = 0
pad = 0
while czero < len(data) and data[czero] == 0:
pad += 1
czero += 1
return alphabet[0] * pad + res
def script_pub_key_to_address(script_pub_key):
"""
Convert a ScriptPubKey to a Bitcoin address
"""
# Extract the PubKeyHash
pub_key_hash_hex = script_pub_key[6:-4]
pub_key_hash_bytes = bytes.fromhex(pub_key_hash_hex)
# Add netowkr prefix (0x00 for mainnet P2PKH)
version_prefixed = b'\x00' + pub_key_hash_bytes
# Double SHA256
checksum = sha256(sha256(version_prefixed).digest()).digest()[:4]
# Concatenate and encode Base58
address_bytes = version_prefixed + checksum
address = base58_encode(address_bytes)
return address
# How to use
script_pub_key = "76a914a5d14911b042d61db6bff26b0e12f82b2aa590af88ac"
address = script_pub_key_to_address(script_pub_key)
print(address)