Are the transactions added to the genesis block spendable, just like
any other transactions from other blocks, or is that an unspendable
coinbase transaction.
The coinbase transaction of the genesis block is not spendable.
If I fork the Bitcoin code, and generate my own genesis block. Can I
use the private key/public key I used as bitcoin recipient of the
transactions hard-coded with the genesis block?
Yes, but you have to make some modifications to the source code, because it's "unspendability" has been hard coded to avoid a hard fork.
How to make it spendable
chainparams.cpp
- Mine your own genesis block by providing all the necessary information in
chainparams.cpp
, mainly CreateGenesisBlock and Line #123 (for mainnet, similar for testnet and regtest in same file).
validation.cpp
You need to make it so the transaction is added to the index, and bypass some checks that make it unspendable. In v0.16
:
- You can find where it is not added to the index in
validation.cpp
Line #1812. Comment out Line 1817 return true
to avoid skipping this.
You might also need to skip validation tests (for example if your genesis block difficulty is lower, if you have a premine) in places such as:
Skip nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE;
on Line #1891 by adding a check around it:
if (block.GetHash() != chainparams.GetConsensus().hashGenesisBlock)
{
if (VersionBitsState(pindex->pprev, chainparams.GetConsensus(), Consensus::DEPLOYMENT_CSV, versionbitscache) == THRESHOLD_ACTIVE) {
nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE;
}
}
Also similar exceptions (check that != genesis block) for if (!WriteUndoDataForBlock(blockundo, state, pindex, chainparams))
return false;
on Line #1988
Similar exception for Line #1805, Lines #1873-1876
Basically, you need to run it and try and see where it fails in the logs, because you're breaking a bunch of rules.