0

One part of coinbase transaction is p2pkh ScriptPubKey (according to the book i read). Coinbase transaction here refers to the first transaction in a block , not Coinbase software/company.

Suppose i have all the data needed to build coinbase transaction (version,output,ect) except the p2pkh ScriptPubKey , and i have my address that i generate using bitcoin rpc getnewaddress , how do i generate the p2pkh ScriptPubKey ?

Extra question : when i generate new address using bitcoin rpc getnewaddress , is the generated address a public key or private key ?

Sorry i'm new to bitcoin, i honestly don't know.

1 Answer 1

1

One part of coinbase transaction is p2pkh ScriptPubKey (according to the book i read).

That is (or at least, was) commonly the case, but there is no such requirement.

Suppose i have all the data needed to build coinbase transaction (version,output,ect) except the p2pkh ScriptPubKey , and i have my address that i generate using bitcoin rpc getnewaddress , how do i generate the p2pkh ScriptPubKey ?

The coinbase transaction needs to have one or more outputs, which distribute the subsidy and fees gained to the miner(s). The miner can decide to pay out thenselves in whatever way they want. In that sense, the transaction outputs of the coinbase transaction are no different from those of any other output.

Now, most wallets want to be paid using one of the standard output types (P2PKH, P2SH, P2WPKH; there are a few more but that doesn't matter here). Each has a corresponding address type, so if you want the payout to such an address, you just need a way to compute the corresponding scriptPubKey. This is a very standard operation, implemented in pretty much any Bitcoin software library, and literally every wallet.

For P2PKH specifically, there are plenty of questions on how to do that on this site. For P2SH, refer to BIP13. For P2WPKH, see BIP141.

Extra question : when i generate new address using bitcoin rpc getnewaddress , is the generated address a public key or private key ?

Modern wallets won't generate P2PKH addresses anymore; they are legacy. But the same applies for other address types.

In general, first the private key is generated (usually deterministically using BIP32). Then the corresponding public key is computed. Then that public key is hashed, and the hash is encoded as an address (for P2PKH or P2WPKH; for P2SH or P2WSH a script is involved as well).

5
  • wow i'm amazed how bitcoin has changed so much that the book i read probably obsolete now . How did you know all of this ? is there any book / documentation you can recommend me to understand this and other aspect of bitcoin ? Commented Oct 3, 2021 at 1:39
  • 1
    It's not that the book sounds obsolete - it sounds more like it has always been misleading if it claims coinbase always have a P2PKH output. I'm not sure about books, but all big protocol/standard changes since ~2012 or so is described in BIPs. It sounds like you're trying to write mining software for some purpose; I suggest you look through the relevant BIPs (at least 13, 16, 141, 173, 350), and perhaps study other mining software implementations. Commented Oct 3, 2021 at 1:58
  • 1
    And I know because it is my job. Commented Oct 3, 2021 at 1:58
  • I'm still trying to search on internet how to generate scriptpubkey , in the meanwhile i will read the bip you suggested ^^ txn , i will come back here when i finish Commented Oct 3, 2021 at 1:59
  • Here is one for P2PKH: bitcoin.stackexchange.com/a/65807/208 Commented Oct 3, 2021 at 2:05

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.