4

I am creating a miner in C++ for solo mining, and am struggling to create the proper coinbase transaction.

Say I have a bc1 address, like bc1qfc7fameteuguetm0kfzypnvf2ju6wppdvj6tkc , what would be and example of a coinbase transaction with this, and how would I get to it?

https://developer.bitcoin.org/reference/transactions.html

01000000 .............................. Version

01 .................................... Number of inputs
| 00000000000000000000000000000000
| 00000000000000000000000000000000 ...  Previous outpoint TXID
| ffffffff ............................ Previous outpoint index
|
| 29 .................................. Bytes in coinbase // height+message below, byte length
| |
| | 03 ................................ Bytes in height
| | | 4e0105 .......................... Height: 328014
| |
| | 062f503253482f0472d35454085fffed
| | f2400000f90f54696d65202620486561
| | 6c74682021 ........................ Arbitrary data // custom data, include extranonce here
| 00000000 ............................ Sequence // i thought this was all Fs?

01 .................................... Output count
| 2c37449500000000 .................... // coinbase value
| ????????????????? ............. // here, how do i properly fill this part??
| 00000000 ............................ Locktime

These are the parts needed to create the data for the transaction.

Everything up to the output, i understand. but i dont know what to put between the coinbase value and locktime. how does the address convert to hex? what OP codes do i need? and is this transaction layout correct?

And then I would then hash it and input it as a new transaction into the block as like so,

tx = {
  "data" : "<hex string mentioned above>",
  "hash" : "<double sha256 of data above>",
}

is that the only data i need for the json?

Thanks in advance.

2 Answers 2

1

To create a coinbase transaction you need to send this RPC command to your Bitcoin-Qt

createrawtransaction '[{"txid":"0000000000000000000000000000000000000000000000000000000000000000","vout":2147483647}]{"Your Bitcoin Adress":Amount}'

Why 2147483647 ?

Because the vout must be numeric, and you can't put 0xffffffff

So this will give you 0x7fffffff and the easiest way to do that is to change manually the 7 to f

Now as required in BIP34 you must include the block height inside the scriptSig otherwise your coinbase transaction will not be valid

PS: Be aware of little and big endian

0

i dont know what to put between the coinbase value and locktime.

This is the output scriptPubkey, the locking mechanism placed on a transaction output. In the case of a coinbase transaction it should spend the newly minted coins and any transaction fees.

how does the address convert to hex?

See this answer

what OP codes do i need?

You could use P2PK:

<pub key> OP_CHECKSIG

or P2PKH:

OP_DUP OP_HASH160 <RIPEMD160(SHA256(publickey))> OP_EQUALVERIFY OP_CHECKSIG

and is this transaction layout correct?

You could check it against Greg Walker's learnmeabitcoin coinbase description, or its block explorer:

https://learnmeabitcoin.com/explorer/transaction/json.php?txid=8b50f51b49f27e7bb0efb0b3bf38d12ce4f7e6258b90a75802a394cb585c879d

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.