1

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

3

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 ------
03CA6108FABE6D6D15B0663A60E46473FBA0C15DBEDCC466A5B71C741D9FFE721E38E9D161BCE45201000000000000000C65070100FB9B000013D7EC00082F736C7573682F

------ Decoded ------
03 -> Push 3 bytes
CA6108F -> 549,322 (height in littleEndian)
------ Remaining data is arbitrary ------
ABE6D6D15B0663A60E46473FBA0C15DBEDCC466A5B71C741D9FFE721E38E9D161BCE45201000000000000000C65070100FB9B000013D7EC00082F736C7573682F
  • 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:

010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff4503ca6108fabe6d6d15b0663a60e46473fba0c15dbedcc466a5b71c741d9ffe721e38e9d161bce45201000000000000000c65070100fb9b000013d7ec00082f736c7573682f0000000003b33eee4a000000001976a9147c154ed1dc59609e3d26abb2df2ea3d587cd8c4188ac00000000000000002c6a4c2952534b424c4f434b3a8cca119e75a4d70a57346f0fb9facc55f00f150265095a92ca27a9b1b1ac6e000000000000000000266a24aa21a9edb8c0253a8b72023513b3f8c08591fb009e1b89d1651ee044d183a4c627399a710120000000000000000000000000000000000000000000000000000000000000000000000000

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 --
  fabe6d6d15b0663a60e46473fba0c15dbedcc466a5b71c741d9ffe721e38e9d161bce45201000000000000000c65070100fb9b000013d7ec00082f736c7573682f
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
  6a24aa21a9edb8c0253a8b72023513b3f8c08591fb009e1b89d1651ee044d183a4c627399a71

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.