I am trying to build a coinbase for mining with the following steps:
1-Create dummy Input and output.
2-Pass them to CreateRawTransaction as collection/
3-Get a transaction back and fill the empty fields according to coinbase structure (specially set Txout’s Value to 12.5 bitcoin).
4-Serialized transaction into Hex string mode.
With above steps preformed in RegTest mode a new block created and 50 Bitcoins added to the wallet which seems to be correct but as far as I know a transaction should be signed, Is Coinbase excluded from signing procedure ? and why it’s add 50 bitcoins to account instead of amount which I specific set to 12.5 bitcoin ?
public async Task<string> BuildCoinbaseTransaction(string MinerAddress, uint Height)
{
string coinTxSerialized = string.Empty;
scriptData = Tools.RandomNumbers(rEngine);
string pubKey = await GetPublicKey(MinerAddress);
string scriptPubKey = CreateCoinbaseScript(pubKey);
List<object> txi = new List<object>
{ new CoinIn { TxId = "0".PadLeft(64, '0'), Index = 0x0 } };
List<object> txo = new List<object>
{ new Dictionary<string, double> { { MinerAddress, 0.00 } } };
RPCResult<string> rpcResult = await cmd.CreateRawTransactionAsync(txi, txo);
if (rpcResult.Error == null)
{
TotalReward = Math.Min(RewardCoins + CollectedFee, MaxCoinbaseValue);
string rawCoinbase = rpcResult.Result;
string decodedTx = await cmd.DecodeRawTransaction_CommandAsync(rawCoinbase);
RPCResult<string> Transaction jsonTx= JsonConvert.DeserializeObject<RPCResult<string>>(decodedTx);
Transaction coinTx = jsonTx.Result;
coinTx.Height = BuildHeight(Height);
coinTx.TxIn.FirstOrDefault().Coinbase = scriptData;
coinTx.TxIn.FirstOrDefault().Sequence = 0xFFFFFFFF;
coinTx.TxOut.FirstOrDefault().Value = TotalReward;
coinTx.TxOut.FirstOrDefault().ScriptPubKey.Assembely = scriptPubKey;
coinTx.Hex = coinTx.ToString();
coinTxSerialized = coinTx.Hex;
return coinTxSerialized;
}//if
}//BuildCoinbaseTransaction