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

  • 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
    Commented Apr 28 at 12:00
  • @Lee I updated, wait for your comment, thanks for give advise about my question))
    – Alex Radik
    Commented Apr 28 at 23:56
  • 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 and at what an MRE is.
    – Lee
    Commented Apr 29 at 8:58
  • 1
    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" in Mastering Bitcoin 3rd Edition?
    – Murch
    Commented Apr 29 at 12:40
  • @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
    Commented Apr 29 at 14:56

1 Answer 1


How to generate coinbase transaction in Python?

Pycoin might be worth looking at. It has a transaction class with a coinbase_tx function.

There also appears to be a python based test framework in the Bitcoin Core repo.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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