<p>Yep, it is <em>partially</em> possible with the <a href="https://en.bitcoin.it/wiki/Getblocktemplate" rel="nofollow noreferrer"><code>getblocktemplate</code></a> RPC command.</p>

<p>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 <a href="https://github.com/bitcoin/bips/blob/master/bip-0022.mediawiki" rel="nofollow noreferrer">BIP 22</a> and <a href="https://github.com/bitcoin/bips/blob/master/bip-0023.mediawiki" rel="nofollow noreferrer">BIP 23</a> (an extension for pooled mining).</p>

<p>These output values will provide the information you need:</p>

<pre><code>"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
</code></pre>

<p>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.</p>

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

<ul>
<li><a href="https://github.com/bitcoin/bips/blob/master/bip-0034.mediawiki" rel="nofollow noreferrer">BIP 34</a> which requires you to prefix the input script with the serialized height of the block.</li>
</ul>

<p>For example, the input script of block 549,322 is:</p>

<pre><code>------ Original ------
03CA6108FABE6D6D15B0663A60E46473FBA0C15DBEDCC466A5B71C741D9FFE721E38E9D161BCE45201000000000000000C65070100FB9B000013D7EC00082F736C7573682F

------ Decoded ------
03 -&gt; Push 3 bytes
CA6108F -&gt; 549,322 (height in littleEndian)
------ Remaining data is arbitrary ------
ABE6D6D15B0663A60E46473FBA0C15DBEDCC466A5B71C741D9FFE721E38E9D161BCE45201000000000000000C65070100FB9B000013D7EC00082F736C7573682F
</code></pre>

<ul>
<li>Must have one TX input</li>
<li>Previous hash =  <code>0x0000000000000000000000000000000000000000000000000000000000000000</code> (since coinbase is the first TX in a block)</li>
<li>Previous output n = <code>0xFFFFFFFF</code> (-1 decimal since coinbase is the 0th transaction)</li>
<li>Output does not exceed (block reward + fees) or the <code>coinbasevalue</code> if you're using the RPC command with the provided transactions.</li>
</ul>

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

<pre><code>010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff4503ca6108fabe6d6d15b0663a60e46473fba0c15dbedcc466a5b71c741d9ffe721e38e9d161bce45201000000000000000c65070100fb9b000013d7ec00082f736c7573682f0000000003b33eee4a000000001976a9147c154ed1dc59609e3d26abb2df2ea3d587cd8c4188ac00000000000000002c6a4c2952534b424c4f434b3a8cca119e75a4d70a57346f0fb9facc55f00f150265095a92ca27a9b1b1ac6e000000000000000000266a24aa21a9edb8c0253a8b72023513b3f8c08591fb009e1b89d1651ee044d183a4c627399a710120000000000000000000000000000000000000000000000000000000000000000000000000
</code></pre>

<p>which decodes to:</p>

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

  0000000000000000 -&gt; output 2 value
  2c -&gt; output 2 script length
  6a4c2952534b424c4f434b3a8cca119e75a4d70a57346f0fb9facc55f00f150265095a92ca27a9b1b1ac6e00 -&gt; provably unspendable

  0000000000000000 -&gt; output 3 value (witness commitment)
  26 -&gt; output 3 length
  6a24aa21a9edb8c0253a8b72023513b3f8c08591fb009e1b89d1651ee044d183a4c627399a71

01 -&gt; number of witnesses
20 -&gt; witness length
0000000000000000000000000000000000000000000000000000000000000000 -&gt; witness
00000000 -&gt; locktime
</code></pre>

<p>For more information on how to actually utilize <code>createrawtransaction</code> with these values, see <a href="https://bitcoin.stackexchange.com/a/58696/85140">this</a> and <a href="https://bitcoin.stackexchange.com/a/58765/85140">this</a>.</p>

CC BY-SA 4.0
2018-11-09T04:08:04.117Z
Nice answer, esp. the references to the BIPs. thanks
Geremia
CC BY-SA 4.0
2018-11-09T04:09:00.817Z
@Geremia Glad I could help! :)
Mason Ticehurst
CC BY-SA 4.0
2018-11-09T04:22:32.317Z
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.
Mason Ticehurst
CC BY-SA 4.0
2018-11-09T04:31:39.707Z
Also, `createrawtransaction` appears to make version 2 transactions (hex begins with 0200…). Does that change anything?
Geremia
CC BY-SA 4.0
2018-11-09T04:38:54.897Z
@Geremia Great question. From what I've read [here](https://bitcoin.stackexchange.com/a/72742/85140) 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
Mason Ticehurst
CC BY-SA 4.0
2018-11-09T17:24:08.627Z
Also, why does `createrawtransaction '[{"txid":"0000000000000000000000000000000000000000000000000000000000000000","vout":0}]' '{"12Evb28oMpkP76HRS2CZ7VjNMD25B84VMv":12.5}'` only return 85 bytes: `020000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff01807c814a000000001976a9140d9a43dc5ffc3ea83ca7f95dd2daf2990865da7a88ac00000000`? (source: [this question](https://bitcoin.stackexchange.com/q/58759/4334))
Geremia
CC BY-SA 4.0
2018-11-09T17:39:15.683Z
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/85552/discussion-between-geremia-and-kappadev).
Geremia
CC BY-SA 4.0
2018-11-09T01:20:18.897Z
2018-11-09T01:50:00.723Z
2018-11-09T01:50:00.723Z
Mason Ticehurst
<p>Is there a way to create coinbase transactions with an RPC command?</p>

<bitcoin-core><coinbase-transaction><createrawtransaction>
Is there a way to create coinbase transactions with an RPC command?
Geremia
# Is there a way to create coinbase transactions with an RPC command?

(Asked by: username_0 on 2018-11-08)

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

**Tags:** bitcoin-core, coinbase-transaction, createrawtransaction


---

## 1 answers

### Answer by username_1 on 2018-11-09. Score: 3 | Accepted answer

Yep, it is *partially* possible with the [`getblocktemplate`](https://en.bitcoin.it/wiki/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](https://github.com/bitcoin/bips/blob/master/bip-0022.mediawiki) and [BIP 23](https://github.com/bitcoin/bips/blob/master/bip-0023.mediawiki) (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](https://github.com/bitcoin/bips/blob/master/bip-0034.mediawiki) 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](https://bitcoin.stackexchange.com/a/58696/85140) and [this](https://bitcoin.stackexchange.com/a/58765/85140).

> **Comments:**
> **username_0**: Nice answer, esp. the references to the BIPs. thanks  
> **username_1**: @username_0 Glad I could help! :)  
> **username_1**: 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.  
> **username_0**: Also, `createrawtransaction` appears to make version 2 transactions (hex begins with 0200…). Does that change anything?  
> **username_1**: @username_0 Great question. From what I've read [here](https://bitcoin.stackexchange.com/a/72742/85140) 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  
> **username_0**: Also, why does `createrawtransaction '[{"txid":"0000000000000000000000000000000000000000000000000000000000000000","vout":0}]' '{"12Evb28oMpkP76HRS2CZ7VjNMD25B84VMv":12.5}'` only return 85 bytes: `020000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff01807c814a000000001976a9140d9a43dc5ffc3ea83ca7f95dd2daf2990865da7a88ac00000000`? (source: [this question](https://bitcoin.stackexchange.com/q/58759/4334))  
> **username_0**: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/85552/discussion-between-username_0-and-kappadev).  

---

