Is there a way to create coinbase transactions with an RPC command?


Yep, it is partially possible with the getblocktemplate RPC command.

This is what miners routinely use to obtain the information necessary to begin mining a block. The exact parameters and response has been specified by BIP 22 and BIP 23 (an extension for pooled mining).

These output values will provide the information you need:

"coinbaseaux" : {                 (json object) data that should be included in the coinbase's scriptSig content
      "flags" : "xx"                  (string) key name is to be ignored, and value included in scriptSig
  "coinbasevalue" : n,              (numeric) maximum allowable input to coinbase transaction, including the generation award and transaction fees (in satoshis)
  "coinbasetxn" : { ... },          (json object) information for coinbase transaction

Unfortunately, although specified, it does not appear that Bitcoin core provides the "coinbasetxn" JSON object for you, so you'll have to create it manually. However, this will at minimum give you the coinbase value and transactions to include in the block.

You can create the coinbase transactions just like any other transaction, however it MUST abide by the following rules:

  • BIP 34 which requires you to prefix the input script with the serialized height of the block.

For example, the input script of block 549,322 is:

------ Original ------

------ Decoded ------
03 -> Push 3 bytes
CA6108F -> 549,322 (height in littleEndian)
------ Remaining data is arbitrary ------
  • Must have one TX input
  • Previous hash = 0x0000000000000000000000000000000000000000000000000000000000000000 (since coinbase is the first TX in a block)
  • Previous output n = 0xFFFFFFFF (-1 decimal since coinbase is the 0th transaction)
  • Output does not exceed (block reward + fees) or the coinbasevalue if you're using the RPC command with the provided transactions.

In our example block (549,332), the coinbase transaction's raw hexadecimal (the output found in createrawtransaction RPC command) is:


which decodes to:

01000000 -> txVersion
00 -> SegWit marker
01 -> SegWit flag
01 -> Input count (important that it is 1 for coinbase transactions!)
0000000000000000000000000000000000000000000000000000000000000000 -> Previous hash
ffffffff -> Previous output #
45 -> input script length
  03 -> Push 3 bytes
  ca6108 -> block height (littleEndian)
  -- arbitrary data --
00000000 -> sequence
03 -> output count
  b33eee4a00000000 -> output 1 value
  19 -> output 1 script length
  76a9147c154ed1dc59609e3d26abb2df2ea3d587cd8c4188ac -> Pay-to-Public-Key-Hash script

  0000000000000000 -> output 2 value
  2c -> output 2 script length
  6a4c2952534b424c4f434b3a8cca119e75a4d70a57346f0fb9facc55f00f150265095a92ca27a9b1b1ac6e00 -> provably unspendable

  0000000000000000 -> output 3 value (witness commitment)
  26 -> output 3 length

01 -> number of witnesses
20 -> witness length
0000000000000000000000000000000000000000000000000000000000000000 -> witness
00000000 -> locktime

For more information on how to actually utilize createrawtransaction with these values, see this and this.

  • Nice answer, esp. the references to the BIPs. thanks – Geremia Nov 9 '18 at 4:08
  • @Geremia Glad I could help! :) – KappaDev Nov 9 '18 at 4:09
  • I'm not entirely sure if you can due to the specialized input script, and witness commitment in the output script. I'm referring to the output that any other createrawtransaction returns, which is just the raw hexadecimal of the transaction. – KappaDev Nov 9 '18 at 4:22
  • Also, createrawtransaction appears to make version 2 transactions (hex begins with 0200…). Does that change anything? – Geremia Nov 9 '18 at 4:31
  • 1
    @Geremia Great question. From what I've read here version 2 transactions only add support for a new OP_ code. Specifically OP_CHECKSEQUENCEVERIFY. I've looked through many of the recent coinbase transactions and they mostly appear to use version 1 although I am unaware of any direct reason beyond that – KappaDev Nov 9 '18 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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