<blockquote>
<p>How to generate coinbase transaction in Python?</p>
</blockquote>
<p><a href="https://pycoin.readthedocs.io/en/latest/" rel="nofollow noreferrer">Pycoin</a> might be worth looking at. It has a transaction class with a <a href="https://github.com/richardkiss/pycoin/blob/367a58e25aacf85549a7335f7607ba8a53727c81/pycoin/coins/bitcoin/Tx.py#L42" rel="nofollow noreferrer"><code>coinbase_tx</code> function</a>.</p>
<p>There also appears to be a <a href="https://github.com/bitcoin/bitcoin/blob/4d2d91a9e0f7a0647d5a45ed04bcbf6590ce8814/test/functional/test_framework/messages.py#L562" rel="nofollow noreferrer">python based test framework in the Bitcoin Core repo</a>.</p>
<blockquote>
<p>Can someone provide maybe more materials, some samples of code, or
describe details about generating a coinbase transaction?</p>
</blockquote>
<ul>
<li><a href="https://pycoin.readthedocs.io/en/latest/" rel="nofollow noreferrer">Pycoin</a></li>
<li><a href="https://github.com/bitcoin/bitcoin/tree/master/test/functional/test_framework" rel="nofollow noreferrer">Bitcoin Core test framework</a></li>
<li><a href="https://ofek.dev/bit/" rel="nofollow noreferrer">bit</a></li>
</ul>

CC BY-SA 4.0
2024-04-29T13:27:25.667Z
2024-04-29T13:34:32.150Z
2024-04-29T13:34:32.150Z
Lee
<p>I know that on this forum a lot of questions on this topic, I have checked most of these, but can't find the solution for my task. I am creating my own algorithm for solo mining bitcoin on Python. I wrote all necessary methods for that, including hashing the Merkle root, calculating the block header etc. But now, I am stuck for a few weeks on the coinbase transaction. I have studied the <a href="https://learnmeabitcoin.com/technical/mining/coinbase-transaction/" rel="nofollow noreferrer">coinbase transaction page on LearnMeABitcoin.com</a> and tried to implement all logic for it, but can't fully understand how to make one. I want to include new rules (BIP34 - block header, BIP141 - SegWit tx) in this algorithm, but my knowledge about hashing this is not enough. :(</p>
<p>Can someone provide maybe more materials, some samples of code, or describe details about generating a coinbase transaction? Any information will be very helpful, hope on your help.</p>
<p>Also, I am training on Bitcoin core in regtest mode, and when I use &quot;generatetoaddress&quot; method (for generate blocks) this one doesn't gen coinbase tx with BIP34 &amp; BIP141 requirements, maybe I need set more config for this one? I found someone say about blockversion arg, is it true?</p>
<p>Thank you all ^^</p>
<p>P.S. If i miss some details about my task, fell free to ask and I provide all info which I have, of sample of code which I have (it's not working)</p>
<p><strong>UPDATE</strong>
I have greate news, I find the solution and complete this) thanks all for yout support, some later I share my code for gen coinbaseTx in python.
But left only one question, i use object for generate tx like on screen, and it have some difference with coinbase tx from generataing by Bitcoin Core using generatetoaddress only in one place, filed n in first output, and it always &quot;00000000&quot;, like 0 in little-endian with 4 bytes I hope. Can someone explain what it is? I use p2wpkh script for hashing address, maybe this one is type for this? When I add this value all worked fine, and I successfully gen CoinbaseTx, after build block header and full block and broadcast it to node. But just for fully understanding my code, wanna to know what this &quot;00000000&quot; mean) Name &quot;n&quot; for field was by me, just temporary name.
<a href="https://i.sstatic.net/e83jIDev.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/e83jIDev.jpg" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2024-04-28T12:00:12.223Z
Hello, thank you for this - it's an interesting area! However, your question is a bit woolly at the moment. It'd be great if you think about how you could make it clear and then edit it. Maybe show what you've tried in python, or narrow it to some precise aspect you don't understand.
Lee
CC BY-SA 4.0
2024-04-28T23:56:39.883Z
@Lee I updated, wait for your comment, thanks for give advise about my question))
Alex Radik
CC BY-SA 4.0
2024-04-29T08:58:36.280Z
Thank you Alex, I'm afraid that I'm none the wiser as to what your actual question is though! It might help to have look [at the how to ask guide](https://bitcoin.stackexchange.com/help/how-to-ask) and at what an [MRE](https://stackoverflow.com/help/minimal-reproducible-example) is.
Lee
CC BY-SA 4.0
2024-04-29T12:40:14.617Z
Hi Alex, I tried to add a bit of structure to your question. I think it would be easier to answer your question if you were able to hone in a bit closer on what the problem with your coinbase transaction is. Have you seen the section ["Structure of the Coinbase Transaction"](https://github.com/bitcoinbook/bitcoinbook/blob/develop/ch12_mining.adoc#structure-of-the-coinbase-transaction) in Mastering Bitcoin 3rd Edition?
Murch
CC BY-SA 4.0
2024-04-29T14:56:13.580Z
@Murch thank's for really useful material. Now I updated my question and fully structure it, I guess) If you can help I really appreciate it) Looking to your ref, I guess this "00000000" from my question can be output index, is it? If yes, why in second output this one not needed?
Alex Radik
CC BY-SA 4.0
2024-04-29T14:57:48.123Z
@Lee, I update it one more time, now I guess it fully clear and understandable)
Alex Radik
<bitcoin-core><mining-theory><python><coinbase-transaction><regtest>
How to generate coinbase transaction in Python?
Alex Radik
# How to generate coinbase transaction in Python?

(Asked by: username_0 on 2024-04-28)

I know that on this forum a lot of questions on this topic, I have checked most of these, but can't find the solution for my task. I am creating my own algorithm for solo mining bitcoin on Python. I wrote all necessary methods for that, including hashing the Merkle root, calculating the block header etc. But now, I am stuck for a few weeks on the coinbase transaction. I have studied the [coinbase transaction page on LearnMeABitcoin.com](https://learnmeabitcoin.com/technical/mining/coinbase-transaction/) and tried to implement all logic for it, but can't fully understand how to make one. I want to include new rules (BIP34 - block header, BIP141 - SegWit tx) in this algorithm, but my knowledge about hashing this is not enough. :(

Can someone provide maybe more materials, some samples of code, or describe details about generating a coinbase transaction? Any information will be very helpful, hope on your help.

Also, I am training on Bitcoin core in regtest mode, and when I use "generatetoaddress" method (for generate blocks) this one doesn't gen coinbase tx with BIP34 & BIP141 requirements, maybe I need set more config for this one? I found someone say about blockversion arg, is it true?

Thank you all ^^

P.S. If i miss some details about my task, fell free to ask and I provide all info which I have, of sample of code which I have (it's not working)

**UPDATE**
I have greate news, I find the solution and complete this) thanks all for yout support, some later I share my code for gen coinbaseTx in python.
But left only one question, i use object for generate tx like on screen, and it have some difference with coinbase tx from generataing by Bitcoin Core using generatetoaddress only in one place, filed n in first output, and it always "00000000", like 0 in little-endian with 4 bytes I hope. Can someone explain what it is? I use p2wpkh script for hashing address, maybe this one is type for this? When I add this value all worked fine, and I successfully gen CoinbaseTx, after build block header and full block and broadcast it to node. But just for fully understanding my code, wanna to know what this "00000000" mean) Name "n" for field was by me, just temporary name.
[![enter image description here](https://i.sstatic.net/e83jIDev.jpg)](https://i.sstatic.net/e83jIDev.jpg)

**Tags:** bitcoin-core, mining-theory, python, coinbase-transaction, regtest

> **Comments:**
> **username_1**: Hello, thank you for this - it's an interesting area! However, your question is a bit woolly at the moment. It'd be great if you think about how you could make it clear and then edit it. Maybe show what you've tried in python, or narrow it to some precise aspect you don't understand.  
> **username_0**: @username_1 I updated, wait for your comment, thanks for give advise about my question))  
> **username_1**: Thank you username_0, I'm afraid that I'm none the wiser as to what your actual question is though! It might help to have look [at the how to ask guide](https://bitcoin.stackexchange.com/help/how-to-ask) and at what an [MRE](https://stackoverflow.com/help/minimal-reproducible-example) is.  
> **username_2**: Hi username_0, I tried to add a bit of structure to your question. I think it would be easier to answer your question if you were able to hone in a bit closer on what the problem with your coinbase transaction is. Have you seen the section ["Structure of the Coinbase Transaction"](https://github.com/bitcoinbook/bitcoinbook/blob/develop/ch12_mining.adoc#structure-of-the-coinbase-transaction) in Mastering Bitcoin 3rd Edition?  
> **username_0**: @username_2 thank's for really useful material. Now I updated my question and fully structure it, I guess) If you can help I really appreciate it) Looking to your ref, I guess this "00000000" from my question can be output index, is it? If yes, why in second output this one not needed?  
> **username_0**: @username_1, I update it one more time, now I guess it fully clear and understandable)  

---

## 1 answers

### Answer by username_1 on 2024-04-29. Score: 0

> How to generate coinbase transaction in Python?

[Pycoin](https://pycoin.readthedocs.io/en/latest/) might be worth looking at. It has a transaction class with a [`coinbase_tx` function](https://github.com/richardkiss/pycoin/blob/367a58e25aacf85549a7335f7607ba8a53727c81/pycoin/coins/bitcoin/Tx.py#L42).

There also appears to be a [python based test framework in the Bitcoin Core repo](https://github.com/bitcoin/bitcoin/blob/4d2d91a9e0f7a0647d5a45ed04bcbf6590ce8814/test/functional/test_framework/messages.py#L562).

> Can someone provide maybe more materials, some samples of code, or
> describe details about generating a coinbase transaction?

* [Pycoin](https://pycoin.readthedocs.io/en/latest/)
* [Bitcoin Core test framework](https://github.com/bitcoin/bitcoin/tree/master/test/functional/test_framework)
* [bit](https://ofek.dev/bit/)


---

