<blockquote>
<p>Clarification on what is the difference between these things: public key hash, scriptPubKey, addresses.</p>
</blockquote>
<p><code>Public key hash</code> 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.</p>
<p><code>scriptPubKey</code> represents a locking script that you use to lock satoshis. To unlock them, you must provide a valid unlocking script, also known as <code>scriptSig</code>.</p>
<p><code>Addresses</code> does not actually exist in Bitcoin protocol. They are just called like that in Blockchain explorer to simplify Bitcoin protocol for end users. <strong>Addresses actually represent just a hash of the public key, nothing else (in case of P2PKH).</strong> It is worth mentioning that they are more often represented in some encoded form like Bech32 or Base58check.</p>
<blockquote>
<p>Implementable steps to get the scriptPubKey</p>
</blockquote>
<p>There are no implementable steps to get the <code>scriptPubKey</code>. 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.</p>
<blockquote>
<p>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?</p>
</blockquote>
<p>It is the one of the standard scriptPubKey, where you lock your funds to hash of the public key. It is called P2PKH.</p>
<blockquote>
<p>Resource recommendation to go further in the understanding.</p>
</blockquote>
<p>Master Bitcoin book (Andreas Antonopolus), Grokking Bitcoin, Programming Bitcoin (Jimmy Song).</p>
<p>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.</p>
<blockquote>
<p>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.</p>
</blockquote>
<p>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.</p>

CC BY-SA 4.0
2023-10-31T19:33:31.677Z
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.
Nahum Maurice
CC BY-SA 4.0
2023-10-31T19:35:02.113Z
@NahumMaurice No problem, glad you made it.
LeaBit
CC BY-SA 4.0
2023-10-31T18:37:30.890Z
2023-10-31T18:37:30.890Z
LeaBit
<p>My goal is to create a transaction. For that I need to know the following things:</p>
<ol>
<li>The private key signing the transaction.</li>
<li>Who can spend the bitcoins that I'm sending.</li>
<li>Valid UTXOs that the sender can spend.</li>
</ol>
<p>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 <a href="https://github.com/petertodd/python-bitcoinlib/tree/51eeb58e04b07bf2b25f2be20293fa182cf0f93a" rel="nofollow noreferrer">codebase here</a>. But, given a public key hash, I don't really understand how to do it.</p>
<p>Ex: Given this hash of a public key: mz64DQXTMiSYwZFQqaggUrMG82MA4TW2wx, how to get the scriptPubKey from it?</p>
<p>What I expect in term of answer (not exclusively):</p>
<ul>
<li>Clarification on what is the difference between these things: public key hash, scriptPubKey, addresses.</li>
<li>Implementable steps to get the scriptPubKey.</li>
<li>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?</li>
<li>Resource recommendation to go further in the understanding.</li>
</ul>
<p><strong>What is clear to me:</strong>
From <a href="https://allprivatekeys.com/bitcoin-address-format" rel="nofollow noreferrer">this site</a>, 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.</p>

<bitcoin-core><private-key><python-bitcoinlib><bitcoin-literature>
How to get scriptPubKey from public key hash
Nahum Maurice
# How to get scriptPubKey from public key hash

(Asked by: username_0 on 2023-10-31)

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](https://github.com/petertodd/python-bitcoinlib/tree/51eeb58e04b07bf2b25f2be20293fa182cf0f93a). 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](https://allprivatekeys.com/bitcoin-address-format), 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.

**Tags:** bitcoin-core, private-key, python-bitcoinlib, bitcoin-literature


---

## 1 answers

### Answer by username_1 on 2023-10-31. Score: 2 | Accepted answer

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

> **Comments:**
> **username_0**: 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.  
> **username_1**: @username_0 No problem, glad you made it.  

---

