What is scriptpubkey for in the coinbase transaction ? (and more..) I'm about to mine and I reached a part when I want to generate the coinbase transaction.
I won't complicate things by putting in the new block transactions and getting the fees for them.
All that matters is to mine a block (seems that the coinbase transaction is counted as 1 transaction for the whole block? True?)
Take a look at this PHP function and check the comments (let me know if I'm wrong somewhere)
function coinbase_gen($block_height){         //(decimal)$block_height would be the last official block number +1
$ver='01000000';                             // seems that this version won't change anytime soon?
$i_count='01';                              // In that situation it is always 01?
$txid='0000000000000000000000000000000000000000000000000000000000000000';   //always as well
$vout='ffffffff';                          // always?

$coin_nextbytes=dechex(strlen($block_height)/2);

$block_height=SwapEndianess(dechex($block_height)); //reassign the height to VarInt
$sig=$coin_nextbytes.$block_height.&quot;f48616f4254432f53756e204368756e2059753a205a6875616e67205975616e2c2077696c6c20796f75206d61727279206d653f2f06fcc9cacc19c5f278560300&quot;;
$sig_size=dechex(strlen($sig)/2);

$sequence='ffffffff';                 // Some people put 0000000 in here.. Why?
$o_count='01';
$o_value='40be402500000000';          //6.25 BTC !!!
$pubkeysize='19';
$script_pubkey='76a914bfd3ebb5485b49a6cf1657824623ead693b5a45888ac'; //Please answer..
$locktime='00000000'; // Seems good
return $ver.$i_count.$txid.$vout.$sig_size.$sig.$sequence.$o_count.$o_value.$pubkeysize.$script_pubkey.$locktime;
}
coinbase_gen((int)$last_blockn); // just example how I'll generate it.

As you can see I have many questions in the code, but I need to know about that $script_pubkey. Some blog posted it as example to mine,but I feel that any bitcoin mined will reward $script_pubkey which is somebodies wallet public key (encrypted) ? OR this does not matter for the coinbase transaction and when I submit the block with bitcoind RPC it will make sure that it will go to my wallet?
This is important to know. Thanks.
The mined Bitcoin &quot;goes&quot; to the scriptPubKey(s) in the coinbase transaction.
Bitcoin operates on spending transaction outputs. Submitting a block does not magically cause a transaction output to appear, you need to specifically create them. This is done through the coinbase transaction (and is really the purpose of it). The only different between a coinbase transaction is that it doesn't have any real inputs. But just like with normal transactions, coinbase transactions can create any number of transaction outputs as it wants, and these just need to follow the rules for output validity.
Thus for a miner to be paid, they need to make sure that the coinbase transaction has transaction outputs that they can spend, and that the amounts for those outputs are what they want to be paid. This means that if you want to collect transaction fees, you need to set the amount to also include the transaction fees of the block, otherwise that Bitcoin will be gone forever. The rule for output amounts in a coinbase transaction is that they must not be greater than the block subsidy + transaction fees for that block.