Post
Topic
Board Development & Technical Discussion
Re: bitcoin-cli - how to get coinbase address from earlier blocks
by
TheArchaeologist
on 17/06/2020, 13:49:57 UTC
What he meant is P2PK scripts are Pay-to-Public-Key (read it literally to understand  Wink).
You shouldn't derive the address based from that Public Key because the owner wont be able to spend it using the "P2PKH" script even if he has the private key.

I really need a self proclaimed genius to elaborate on this. So I guess I'm lucky you are here  Wink

Say I'm Satoshi (hell anyone claims that nowadays) and I have guarded my private keys very well. When I started  back in '09 every coinbase was paid out to a public key (P2PK). So the genesis block was paid out to this public key:

Code:
04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f

Some/most of the blockexplorers convert this to an address. Hell even bitcoin-cli decoderawtransaction used to do this a few releases back. Using python I've done this before myself:

Code:
def big_public_key_to_address(public_key):
        #Step 1: Create hash of public key:
        hash_of_public_key  = hashlib.sha256(bin).digest()

        #Step 2: Calculate RIPEMD-160 of the public key:
        r = hashlib.new('ripemd160')
        r.update(hash_of_public_key)
        r.hexdigest()

        #Step 3: Adding network bytes (00) to RIPEMD-160
        networked =  binascii.unhexlify('00'+r.hexdigest())

        #Step 4: Double hash the networked RIPEMD-160
        sha4a   = hashlib.sha256(networked).digest()
        sha4b  = hashlib.sha256(sha4a).digest()

        #Step 5: Get the first four bytes of sha4b:
        four_bytes = str(binascii.hexlify(sha4b).decode('utf-8'))[:8]

        #Step 6: Adding the four_bytes to the end the RIPEMD-160 from step 3:
        address_hex = str(binascii.hexlify(networked).decode('utf-8')) + four_bytes

        #Step 7: Convert the hex_address using base58 to bitcoin adres
        address_base58 = base58.b58encode(binascii.unhexlify(address_hex))

Which would produce the famous address:
Code:
1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa

But more importantly step 6 produces:
00 [b]62e907b15cbf27d5425399ebf6f0fb50ebb88f18[/b] c29b7d93

Fast forward, 11 years later. Some of my fans are doing tribute payments. Small amounts keep coming. Through the years i accumulated over 18 bitcoins. However lots of them were made by doing a P2PKH-payment to the 1A1...DifNa address. For example:

Code:
transaction hash:
ce03b5624df568367e63da5cb5eba08c4eccb4bcd13588484d11e1014d07c721

Output:
<snip>
Index:0
Address: 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
Value:0.00001000 BTC
Pkscript
OP_DUP
OP_HASH160
[b]62e907b15cbf27d5425399ebf6f0fb50ebb88f18[/b]
OP_EQUALVERIFY
OP_CHECKSIG

So why is can't I spent the above output when I have all the ingredients needed for prove? Genuine question, I'm sure I miss something. Also: if I can't spent these kind of outputs it would mean all these tribute transactions are unspendable (assuming thet have been paid using P2PKH transactions, not P2PK).