bitcoind – The right way to construct coinbase transaction from bitcoin core?

0
73


My query is: In BTC ADRESS i put any adress that i select from my bitcoin-qt pockets?

Sure!! You’d mainly use a generated receiving handle belonging to your btc pockets and use that as your worth throughout the createrawtransaction handle parameter discipline.

Though you would wish to name the getnewaddress command by way of any means obtainable to you after which you may make use of it.

.

Assuming that you have already modified listing to the place your bitcoin-cli or bitcoind executable file is positioned

CMD strategy would look one thing like this:

bitcoin-cli getnewaddress which might show newly generated handle within the cmd terminal window.

bitcoin-cli getnewaddress > newlygenerated_BTC_address.txt which might retailer the output right into a textual content file

To make issues extra dynamic you would wish to write down a batch script to deal with features that your programming language is not capable of deal with. However hopefully you get the thought.





And if i double hash the given hex end result returned to me, with out including some other transaction, this may be the merkleroot (with solely the coinbase transaction) and use it to construct the blockheader?

Sure. You’d insert the double hashed results of the uncooked transaction knowledge because the merkleroot with out swapping it( That can be performed later when swapping the blockheader for mining). When you swap twice, you will find yourself with an incorrect coinbase hash.

Aside from that, you would wish to appropriate and insert a couple of issues.

Assuming hex response of createrawtransaction produces -> 02000000010000000000000000000000000000000000000000000000000000000000000000ffffff7f00ffffffff0140be402500000000160014f68d712fa6f49cdfaaa1707a9d0234e2aabb3f1100000000

:::


02000000010000000000000000000000000000000000000000000000000000000000000000ffffff7f00ffffffff0140be402500000000160014f68d712fa6f49cdfaaa1707a9d0234e2aabb3f1100000000

____________________________________________________________________

02000000 - Tx model -- 8 bytes
____________________________________________________________________

01 - enter depend -- 2 bytes
____________________________________________________________________

0000000000000000000000000000000000000000000000000000000000000000 - txid -- 64 bytes
____________________________________________________________________

ffffff7f - vout -- 8 bytes
____________________________________________________________________

00 - dimension of scriptsig -- MAX OF 100 bytes
____________________________________________________________________

 - scriptsig dimension is lacking - hexadecimal worth
 - byte push is lacking - hexadecimal worth
 - block top (little endian) is lacking - hexadecimal worth
 - arbitrary knowledge(extranonce/message) is lacking - hexadecimal worth
____________________________________________________________________

ffffffff - enter sequence -- 8 bytes
____________________________________________________________________

01- variety of output -- 2 bytes
____________________________________________________________________

40be402500000000 - reward/Output Worth -- 16 bytes
____________________________________________________________________

160014f68d712fa6f49cdfaaa1707a9d0234e2aabb3f11 - scriptPubkey -- 46 bytes (dynamic in size)
____________________________________________________________________

00000000  - locktime
____________________________________________________________________


My C++ resolution :


bHeight - Block top as given by **```getblocktemplate```** response 
Headers wanted - sstream, string, iostream.
Namespaces used - utilizing std::stringstream, utilizing std::string.


string SwapBlockHeight(string knowledge)
{
    string bits; // world return variable
    if(knowledge.dimension() == 5)
    {
        string byte1, byte2, byte3;
        byte3 = knowledge.substr(5, 2);
        byte2 = knowledge.substr(3, 2);
        byte1 = knowledge.substr(1, 2);
        bits = byte3 + byte2 + byte1;
    }
    else if(knowledge.dimension() == 6)
    {
        string byte1, byte2, byte3;
        byte3 = knowledge.substr(4, 2);
        byte2 = knowledge.substr(2, 2);
        byte1 = knowledge.substr(0, 2);
        bits = byte3 + byte2 + byte1;
    }
    else if(knowledge.dimension() == 8)
    {
        string byte1, byte2, byte3, byte4;
        byte4 = knowledge.substr(6, 2);
        byte3 = knowledge.substr(4, 2);
        byte2 = knowledge.substr(2, 2);
        byte1 = knowledge.substr(0, 2);
        bits = byte4 + byte3 + byte2 + byte1;
    }
    return bits;
}



string GETLittleEndian_BlockHeight(uint32_t bHeight)
{
    string reversedDATA;
    char i[25];
    sprintf(i, "%x", bHeight);
    stringstream aa;
    aa << i;
    aa >> reversedDATA;
    // reversedDATA = SwapBlockHeight(reversedDATA);
    // Optimized strategy of above perform name is utilized beneath
    // Assuming hex conversion result's = "1d34f589"
    string byte;

    // Optimized strategy
    for (int i = 0, okay = 0; i < reversedDATA.size()/2; i++)
    {
        byte += reversedDATA.substr(okay, 2);
        okay+=2;
    }
    reversedDATA = byte; // or you can simply return byte itself
    // reversedDATA.size()/2 - as a result of we can be performed with our aim in half the entire dimension.

    return reversedDATA;
}


Bytepush = "03";
Blockheight_littleEndian = GETLittleEndian_BlockHeight(bHeight);
ArbitraryData = "abcdefgh"; // Something inside 100 bytes .. 1 char == 2 bytes

scriptsig_Size = Bytepush.size() + Blockheight_littleEndian.size() + ArbitraryData.size();

// ALL VALUES ARE HEXADECIMAL


LOOKING FOR JSON PARSER/SERIALIZATION??

  1. Get nlohmann json library
  2. Embrace in mission folder

A bit of batch script used however nothing too superior.

#embrace "json.hpp" // for json serialization parser
#embrace <fstream> // for ifstream
#embrace <stdlib.h> // for system calls
#embrace "home windows.h" // if on home windows
utilizing namespace nlohmann;
utilizing std::ifstream;


inline void RunCommand_With_Output_Without_SYMBOL_Defined(string Command_To_Run, string Output_FileName)
{
    string xx_combine = Command_To_Run + " >" + Output_FileName;
    char run_command[xx_combine.length()];
    strcpy(run_command, xx_combine.c_str());
    system(run_command); // execute the command 
// std::cout << ifstream(Output_FileName).rdbuf(); // print to console -- FOR DEBUGGING ONLY!!
} // Efficiently compiled on 20/01/2022 10:20PM


void GETBLOCKTEMPLATE()
{
    string getblocktemplate_syntax = "cd "C:CustomersYOUR DESKTOP NAMEDesktopZ Code" && name gt.bat";
    string filename = "getblocktemplate_Response.json"; // Create file Identify
    RunCommand_With_Output_Without_SYMBOL_Defined(getblocktemplate_syntax, filename); // create json file with getblockresponse output
} // Efficiently compiled on 31/01/2022 11:30AM



uint32_t Get_BLOCK_HEIGHT_asInteger()
{
    uint32_t f;
    ifstream file_input("getblocktemplate_Response.json");
    json object = json::parse(file_input); // Parse json knowledge
    file_input.shut(); // shut streamer
    f = object.at("top");
    return f;
}

WHATS INSIDE THE .BAT FILE??

gt.bat

@echo off
cd "C:UsersYOUR DESKTOP NAMEDesktopZ Code" && bitcoin-cli getblocktemplate {"guidelines":["segwit"]}

rem or

cd "C:UsersYOUR DESKTOP NAMEDesktopZ Code" && bitcoin-cli getblocktemplate {"guidelines":["segwit"]} 

Rem copy file created to vacation spot folder
copy "C:UsersYOUR DESKTOP NAMEDesktopZ Codegetblocktemplate_Response.json" "C:UsersYOUR DESKTOP NAMEDesktopZ CodeCOINBASE ONLYBITCOIN_Miner"

NOTE!! – Copying of json file from one path to a different is barely wanted in case your gt.bat shouldn’t be positioned in your mission root folder together with header recordsdata. This might trigger errors in a while as a result of in as a lot because the json file was created and holds block template response, it will not be discovered UNLESS you inform the json reader to enter whichever folder the place it is going to be generated and get it from there.

It is much more than requested however others may discover the information helpful.
I am going to create a repository on github later.

Ignore what you do not want.

Cheers

LEAVE A REPLY

Please enter your comment!
Please enter your name here