Bitcoin Forum
May 13, 2024, 01:36:44 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: bitcoin-cli - how to get coinbase address from earlier blocks [SOLVED]  (Read 538 times)
naufragus (OP)
Newbie
*
Offline Offline

Activity: 29
Merit: 50


View Profile
June 16, 2020, 08:58:15 PM
Last edit: June 18, 2020, 03:39:36 AM by naufragus
Merited by LoyceV (6), o_e_l_e_o (2)
 #1

hello guys.

i am new to bitcoind and bitcoin-cli, so i am sorry if my questions are newbie.

i want to explore the blockchain and usually i use a my own shell script wrapper of third-party apis, such as blockchain.info and blockchair.com. however, api calls and responses are limited so i downloaded the blockchain. my bitcoind version is 0.19.

my question is, how can i get the address of coinbase transactions of earlier blocks?
for example, in this paste below, i get the hash of block 50 and then the raw transaction from the first transaction id of the block, which is coinbase.
block 500000 example:
https://pastebin.com/P6czHJHM

after this step, i can decode the raw transaction and get an array of addresses with one item, which the coinbase address, as i understand it.
The result element in the addresses array is:

      "scriptPubKey": {
        "asm": "OP_HASH160 228f554bbf766d6f9cc828de1126e3d35d15e5fe OP_EQUAL",
        "hex": "a914228f554bbf766d6f9cc828de1126e3d35d15e5fe87",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "34qkc2iac6RsyxZVfyE2S5U5WcRsbg2dpK"
        ]



now the problem is, when i try the same procedures with blocks 50 or 50000, i can "only" get the following result (for block 50):

      "scriptPubKey": {
        "asm": "041ada81ea00c11098d2f52c20d5aa9f5ba13f9b583fda66f2a478dd7d95a7ab615159d98b63df2 e6f3ecb3ef9eda138e4587e7afd31e7f434cbb6837e17feb0c5 OP_CHECKSIG",
        "hex": "41041ada81ea00c11098d2f52c20d5aa9f5ba13f9b583fda66f2a478dd7d95a7ab615159d98b63d f2e6f3ecb3ef9eda138e4587e7afd31e7f434cbb6837e17feb0c5ac",
        "type": "pubkey"
      }


i see that in older blocks, type is pubkey and in newer block it is scripthash.. i think i should sha256sum that pubKey hex somehow in order to get the coinbase address? i am a newbie but if someone just point me to the right direction, to some reference, i will be most grateful.


block 50 example:
https://pastebin.com/5eWAkqTd

block 50000 example:
https://pastebin.com/b1kBxrS4


Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715564204
Hero Member
*
Offline Offline

Posts: 1715564204

View Profile Personal Message (Offline)

Ignore
1715564204
Reply with quote  #2

1715564204
Report to moderator
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3388
Merit: 6635


Just writing some code


View Profile WWW
June 17, 2020, 12:46:57 AM
 #2

Pay to pubkey type outputs do not have addresses. There is no address for a P2PK script.

Note that you may find software and block explorers which display an address for those outputs. They are converting the pubkey into an address, but this is not actually correct. Such addresses produce different scripts that are not P2PK scripts so saying that is the address for the pubkey is incorrect. This misconception stems from a time when Bitcoin was largely key based and there was only one type of address.

naufragus (OP)
Newbie
*
Offline Offline

Activity: 29
Merit: 50


View Profile
June 17, 2020, 03:22:08 AM
Last edit: June 17, 2020, 03:49:48 PM by naufragus
 #3

hey achow101

thank you very much. that pointed me to the right direction, although it may take me some time to understand what you wrote fully.

so for that example on my previous message, i got the asm hex:

041ada81ea00c11098d2f52c20d5aa9f5ba13f9b583fda66f2a478dd7d95a7ab615159d98b63df2 e6f3ecb3ef9eda138e4587e7afd31e7f434cbb6837e17feb0c5


i was able to encode the pubkey to base58 using shell script and the bitcoin wiki as guide

the result base58 is:  17iyRRXBHJKbv5DKPPkttWewm3CHdNPGQd


if anyone wants to check this script i wrote, it is in my github repo with the provisionally name 'bitcoin.hexToAddress.sh' . it is really an exercise because all functions needed to do these steps are available in bitcoin-bash-tools and this scripts wraps them in a pipeline..
https://github.com/mountaineerbr/scripts
https://github.com/grondilu/bitcoin-bash-tools

cheers!

more resources
https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses#How_to_create_Bitcoin_Address

https://bitcoin.stackexchange.com/questions/8247/how-can-i-convert-a-sha256-hash-into-a-bitcoin-base58-private-key

nc50lc
Legendary
*
Offline Offline

Activity: 2408
Merit: 5599


Self-proclaimed Genius


View Profile
June 17, 2020, 03:39:30 AM
 #4

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.
Those are only common in the old blocks

New widely used scripts are P2PKH short for Pay-to-Public-Key-Hash which is RIPEMD160[SHA256(Public Key)],
that's where addresses work.

.
.HUGE.
▄██████████▄▄
▄█████████████████▄
▄█████████████████████▄
▄███████████████████████▄
▄█████████████████████████▄
███████▌██▌▐██▐██▐████▄███
████▐██▐████▌██▌██▌██▌██
█████▀███▀███▀▐██▐██▐█████

▀█████████████████████████▀

▀███████████████████████▀

▀█████████████████████▀

▀█████████████████▀

▀██████████▀▀
█▀▀▀▀











█▄▄▄▄
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
.
CASINSPORTSBOOK
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀▀█











▄▄▄▄█
naufragus (OP)
Newbie
*
Offline Offline

Activity: 29
Merit: 50


View Profile
June 17, 2020, 05:23:49 AM
Last edit: June 17, 2020, 02:59:55 PM by naufragus
 #5

thanks for the explanation. that makes things clearer.

i will definitely look into history to get a better perspective and understand P2PKH better..

update: in review today, all extra tools are already available in grondilu's bitcoin-bash-tools , referenced above.
see the 'hash160' and 'hexToAddress' funcs, which do all this..

that is just the case we should _not_ send money to P2PK-to-base58-encoded addresses, as stated by the other members.

; )
TheArchaeologist
Sr. Member
****
Offline Offline

Activity: 310
Merit: 727


---------> 1231006505


View Profile WWW
June 17, 2020, 01:49:57 PM
 #6

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 62e907b15cbf27d5425399ebf6f0fb50ebb88f18 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
62e907b15cbf27d5425399ebf6f0fb50ebb88f18
OP_EQUALVERIFY
OP_CHECKSIG

So why 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).


Sooner or later you're going to realize, just as I did, that there's a difference between knowing the path and walking the path
aplistir
Full Member
***
Offline Offline

Activity: 378
Merit: 197



View Profile
June 17, 2020, 08:31:02 PM
Merited by nc50lc (1)
 #7

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.
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:

So why 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).

Well...
You can spend the extra "donations", because they are in a P2PKH address, that you DO have a private key to. The private key is the same for both P2PK and P2PKH "addresses" made from the same key.
The only difference is that P2PK address works with ONLY the one private key it was created with, but the P2PKH address works with 2^96 different private keys. So you could say the P2PK is safer in that way.

But Satoshi made the Genesis-block special. It is impossible to use the 50BTC block reward of the genesis block before all bitcoins are mined. The donated 18BTC can be used though.

My Address: 121f7zb2U4g9iM4MiJTDhEzqeZGHzq5wLh
TheArchaeologist
Sr. Member
****
Offline Offline

Activity: 310
Merit: 727


---------> 1231006505


View Profile WWW
June 17, 2020, 08:50:52 PM
 #8

Well...
You can spend the extra "donations", because they are in a P2PKH address, that you DO have a private key to. The private key is the same for both P2PK and P2PKH "addresses" made from the same key.

Exactly my point, so why is it that nc50lc is saying (as I have seen some others claim as well):

Quote
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.

Just wondering if I'm missing something here..

Sooner or later you're going to realize, just as I did, that there's a difference between knowing the path and walking the path
naufragus (OP)
Newbie
*
Offline Offline

Activity: 29
Merit: 50


View Profile
June 17, 2020, 11:33:19 PM
Last edit: June 17, 2020, 11:56:48 PM by naufragus
 #9

someone ought to be wrong at one point.

i did not mention the block 0 because that is perhaps the most special block..

but that is very interesting.. can someone ask satoshi if she can spend the coins alright?

perhaps if we would see her moving them for some futile thing we all might rest assured the
bitcoin software is backwards compatible and all that stuff..

it does not need be him, and that may have occurred already.

i really need read more this forum..

nc50lc
Legendary
*
Offline Offline

Activity: 2408
Merit: 5599


Self-proclaimed Genius


View Profile
June 18, 2020, 02:00:20 AM
Merited by TheArchaeologist (1)
 #10

Exactly my point, so why is it that nc50lc is saying (as I have seen some others claim as well):
Quote
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.
Just wondering if I'm missing something here..
OP is talking about the actual P2PK transactions here, not the "donations" to the derived address, so am I.
Of course the owner of the private key can spend those donations using P2PKH script but not the UTXO from the P2PK transaction though that "address".

Additionally for naufragus:
Most clients (like Bitcoin Core) can still spend P2PK if the you have the private key since it is still standard,
but it's not a P2PKH script that can be represented as an address which is the point of the earlier replies.

.
.HUGE.
▄██████████▄▄
▄█████████████████▄
▄█████████████████████▄
▄███████████████████████▄
▄█████████████████████████▄
███████▌██▌▐██▐██▐████▄███
████▐██▐████▌██▌██▌██▌██
█████▀███▀███▀▐██▐██▐█████

▀█████████████████████████▀

▀███████████████████████▀

▀█████████████████████▀

▀█████████████████▀

▀██████████▀▀
█▀▀▀▀











█▄▄▄▄
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
.
CASINSPORTSBOOK
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀▀█











▄▄▄▄█
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3388
Merit: 6635


Just writing some code


View Profile WWW
June 18, 2020, 02:31:11 AM
Merited by ABCbits (2), TheArchaeologist (1)
 #11

So why 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).
In theory you can. But your wallet may not be able to. Your wallet software might not be written to recognize transactions sent to those addresses. Your wallet might not be written to watch for transactions that are sent to addresses that it can be spend but did not give out. Your wallet might not be able to produce scriptSigs/scriptWitnesses for those UTXOs. In order to spend such UTXOs, you might need to jump through a lot of hoops and in general do a lot more work even though you have all of the necessary information to create a valid transaction.

A more modern example is mutating a P2PKH into a P2WPKH address. Both P2PKH and P2WPKH encode the hash160 of a public key. If you gave me a P2PKH address, I could use that to create a UTXO that has a scriptPubKey with the P2WPKH script for that key hash in the address. But maybe your wallet doesn't support segwit. Suppose you are using old software or just software that doesn't support segwit at all. The UTXO I created is not spendable by you at this time. Your wallet is not watching for the scriptPubKey in that UTXO, so your wallet doesn't see this transaction. Your wallet does not know how to create a segwit signature and witness even though it has the private key for the public key that is specified by the hash. You have the information, but your wallet is not capable of spending that output.

Scenarios like this are why we shouldn't be doing address mutations at all, whether that be P2PK to P2PKH, P2PKH to P2WPKH, or something else in the future.

TheArchaeologist
Sr. Member
****
Offline Offline

Activity: 310
Merit: 727


---------> 1231006505


View Profile WWW
June 18, 2020, 06:14:25 AM
 #12

@nc50lc, @achow101: Thank you.

In the end I didn't miss anything I guess I just misinterpreted what's been said Smiley. But I rather ask then keep wondering what I'm missing!

To sum up: If you are trying to spent a previous funded P2PK output don't threat that input for your new transaction as if it were a P2PKH output because the signing would be faulty.

Sooner or later you're going to realize, just as I did, that there's a difference between knowing the path and walking the path
naufragus (OP)
Newbie
*
Offline Offline

Activity: 29
Merit: 50


View Profile
June 20, 2020, 03:47:04 PM
Last edit: June 20, 2020, 03:59:32 PM by naufragus
 #13

dear achow101

hey thanks for this last message, mate. there is a lot of fine detail there i find useful and is a great reference for my studies! i can already almost understand what you said..

i have been studying this thread and i also got to explore a little the blockchain. tried and generated all addresses of block 500.000 with blockchain.info as reference.. i used bitcoin-cli, jq and bitcoin-bash-tools and it is perfectly possible.

however a ton of non-existing addresses were generated as well, so i just need understand what i am doing first..

i reckon a good reference here will be Antonopoulos , advanced transactions and scripting, i will check that out someday.
in the meantime, there are really helpful videos at casa code youtube channel:
https://www.youtube.com/channel/UCZMSW_o5F-Jn2EPp2DF-I0g/videos

thanks everybody that replied.  helped me to understand tons in a short period of time..
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!