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?


$block_height=SwapEndianess(dechex($block_height)); //reassign the height to VarInt

$sequence='ffffffff';                 // Some people put 0000000 in here.. Why?
$o_value='40be402500000000';          //6.25 BTC !!!
$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.

  • Rolled back to last revision as the question doesn't make much sense without the code, especially if you leave in phrases like "Take a look at this PHP function". Commented Jan 15, 2023 at 17:34

1 Answer 1


The mined Bitcoin "goes" 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.