Thank you sir for the very clear explanation, this is very much helpful and extremely appreciated.
I realized that I have been trying to extract an address from a P2SH script. The transaction in question is
https://live.blockcypher.com/btc/tx/bfa5b2de068fbb1b963e479138b3b5db0670f584d60d45cf4ee50a85b4e1f483/If you decode it here
https://live.blockcypher.com/btc/decodetx/You get
{
"addresses": [
"1EgVG6jkqUTpq9oZtXzSiYuMYDKsYLzrqL"
],
"block_height": -1,
"block_index": -1,
"confirmations": 0,
"double_spend": false,
"fees": 0,
"hash": "bfa5b2de068fbb1b963e479138b3b5db0670f584d60d45cf4ee50a85b4e1f483",
"inputs": [
{
"age": 0,
"output_index": -1,
"script": "042f931d1a028000",
"script_type": "empty",
"sequence": 4294967295
}
],
"outputs": [
{
"addresses": [
"1EgVG6jkqUTpq9oZtXzSiYuMYDKsYLzrqL"
],
"script": "4104827404c816fe73adfe0f02020c8097dc9c0cafb304ca9089c80011825573f61465c17388b76a2ece60121c84773fe9f7c7b2870566dbbdfbce4aaceda35d8c41ac",
"script_type": "pay-to-pubkey",
"value": 5008000000
}
],
"preference": "low",
"received": "2024-08-14T14:31:27.281306319Z",
"relayed_by": "3.231.209.196",
"size": 135,
"total": 5008000000,
"ver": 1,
"vin_sz": 1,
"vout_sz": 1,
"vsize": 135
}
Now I’ve found this message:
https://bitcointalk.org/index.php?topic=5265034.msg54884971#msg54884971Also, some code on the internet:
def address(script_pub_key: Octets, network: str = "mainnet") -> str:
"""Return the bech32/base58 address from a script_pub_key."""
if script_pub_key:
script_type, payload = type_and_payload(script_pub_key)
if script_type in ("p2pkh", "p2sh"):
return b58.address_from_h160(script_type, payload, network)
if script_type in ("p2wsh", "p2wpkh"):
return b32.address_from_witness(0, payload, network)
if script_type == "p2tr":
return b32.address_from_witness(1, payload, network)
# not script_pub_key
# or
# script_type in ("p2pk", "p2ms", "nulldata", "unknown")
return ""
So P2PK script do not seem to be able to hold an address, which was confusing me totally.
Actually, it seems like blockcypher is taking the public key, hash it using
https://learnmeabitcoin.com/technical/keys/public-key/hash/#hash160-tool, add a null byte and base58 encode everything, which effectively gives
from
04827404c816fe73adfe0f02020c8097dc9c0cafb304ca9089c80011825573f61465c17388b76a2ece60121c84773fe9f7c7b2870566dbbdfbce4aaceda35d8c41
The value
>>> base58.b58encode(bytes.fromhex("00961172bf46d12fc3b3ff3d3bf4473d08870079a8f29b9fcf"))
b'1EgVG6jkqUTpq9oZtXzSiYuMYDKsYLzrqL'
Not sure if this address has any practical usage