0

My goal is to create a transaction. For that I need to know the following things:

  1. The private key signing the transaction.
  2. Who can spend the bitcoins that I'm sending.
  3. Valid UTXOs that the sender can spend.

When creating the outputs, I need to add the scriptPubKey, which in my understanding is a script that specifies how the bitcoin can be spent. I know how to get the scritPubKey from and address (actually, I mean there is a way to do it if using Peter Todd's python-bitcoinlib codebase here. But, given a public key hash, I don't really understand how to do it.

Ex: Given this hash of a public key: mz64DQXTMiSYwZFQqaggUrMG82MA4TW2wx, how to get the scriptPubKey from it?

What I expect in term of answer (not exclusively):

  • Clarification on what is the difference between these things: public key hash, scriptPubKey, addresses.
  • Implementable steps to get the scriptPubKey.
  • I realise that using this hash of the public key, I can send transactions directly to it and treat it as an address and some explorer calls it address, how is that possible?
  • Resource recommendation to go further in the understanding.

What is clear to me: From this site, I understand that the hash comes from hashing the public key with hash160 and adding checksum 4 bytes. I believe by reverting this process, I could get the public key and generate an address and get the scriptPubKey from the address, but I believe there can be better way to do it.

1 Answer 1

2

Clarification on what is the difference between these things: public key hash, scriptPubKey, addresses.

Public key hash represents, as it name says, the hash of the public key. So you take the public key and first do SHA256 hash function on it, and then RIPEMD160.

scriptPubKey represents a locking script that you use to lock satoshis. To unlock them, you must provide a valid unlocking script, also known as scriptSig.

Addresses does not actually exist in Bitcoin protocol. They are just called like that in Blockchain explorer to simplify Bitcoin protocol for end users. Addresses actually represent just a hash of the public key, nothing else (in case of P2PKH). It is worth mentioning that they are more often represented in some encoded form like Bech32 or Base58check.

Implementable steps to get the scriptPubKey

There are no implementable steps to get the scriptPubKey. You can lock your satoshis the way you want. You just need to take care that you are using valid opcodes and that your locking logic is valid since if it is not, you won't be able to unlock funds. There are some standard ways to create this locking script like P2PKH, P2SH etc.

I realise that using this hash of the public key, I can send transactions directly to it and treat it as an address and some explorer calls it address, how is that possible?

It is the one of the standard scriptPubKey, where you lock your funds to hash of the public key. It is called P2PKH.

Resource recommendation to go further in the understanding.

Master Bitcoin book (Andreas Antonopolus), Grokking Bitcoin, Programming Bitcoin (Jimmy Song).

All the mentioned books take you in detail through how to create scripts and how bitcoin works in general. They are worth reading and will give you answers to many questions, including the one you asked.

I believe by reverting this process, I could get the public key and generate an address and get the scriptPubKey from the address, but I believe there can be better way to do it.

You can't revert hashing. Hash functions are one way functions. However, you do not even need to revert it. Since the hash of the public key represents address, you are creating scriptPubKey using it, not directly public key.

2
  • Nicely answered! Thank you for the the time. I actually solve the problem by creating a P2PKH script from it since it's the hash of my pubk. Oct 31 at 19:33
  • @NahumMaurice No problem, glad you made it.
    – LeaBit
    Oct 31 at 19:35

Your Answer

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

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