<p>The protocol is not what decides the scriptPubKey, it's your wallet that creates the transaction. Your wallet chooses what scriptPubKey to use based on the address that it is given. It uses P2PKH for <code>1...</code> addresses because that's the scriptPubKey that that type of address specifies. It cannot use those addresses for P2PK because they do not contain enough information for P2PK - they only have the hash160 of the pubkey, not the full pubkey itself. Additionally, using a different scriptPubKey for those addresses would mean that the Bitcoin is sent to a scriptPubKey that the receiver is not necessarily watching.</p>
<p>The same applies to all of the other address types - the address type specifies the scriptPubKey to create.</p>
<hr />
<blockquote>
<p>Why I have P2PKH instead of P2PK?</p>
</blockquote>
<p>Because that's the address type you imported. You are using regtest, so P2PKH addresses begin with <code>m</code> or <code>n</code>.</p>
<blockquote>
<p>And how can have the hash of my Public key if I imported just address (HASH160(PB))?</p>
</blockquote>
<p>The address specifies the hash.</p>
<blockquote>
<p>And the last question, I understand that if the wallet find the 1 as the first char it apply P2PKH by default. How Can I use P2PK instead (study purpose).</p>
</blockquote>
<p>You cannot. P2PK does not have an address type. There are no P2PK addresses.</p>
<p>Some software and blockchain explorers will show P2PKH addresses for P2PK scriptPubKeys, but this is incorrect. P2PK scriptPubKeys do not have a corresponding address.</p>

CC BY-SA 4.0
2020-12-11T17:28:57.633Z
Thanks for your comments, I updated my question. thanks again
monkeyUser
CC BY-SA 4.0
2020-12-11T18:37:53.953Z
I've updated my answer to answer your questions.
Ava Chow
CC BY-SA 4.0
2020-12-11T18:40:59.327Z
thanks again, I understand the the wallet can decode the address and retrieve HASH160(PB) and put in scriptPubKey like: `printf mwqCgoJBcyciRUawXerkvom8sbg3zkjrvV" | base58 -c -d | xxd -p` =>     `6fb2f58389a224c2fac62b51e78aad7ed058b50f5f`
monkeyUser
CC BY-SA 4.0
2020-12-11T18:59:47.927Z
but in this case https://btc.bitaps.com/f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16 the address is: `1Q2TWHE3GMdB6BZKafqwxXtWAWgFt5Jvm3` and the public key (uncompressed) is: `04ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84c`. How can I generate `1Q2TWHE3GMdB6BZKafqwxXtWAWgFt5Jvm3`? In P2SH I can create a script like `$PBLENGTH$PB"AC"`.
monkeyUser
CC BY-SA 4.0
2020-12-11T19:11:40.937Z
There is no address for that scriptPubKey. It is incorrect for the block explorer to be showing that. You can create an address for a given pubkey by hashing it, however that address will not correspond to a P2PK scriptPubKey. You can read https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses#How_to_create_Bitcoin_Address for some more information on how an address can be created from a pubkey.
Ava Chow
CC BY-SA 4.0
2020-12-11T19:14:10.030Z
Thanks again, I understand and I can reproduce the generation of P2PKH, P2SH, wrapped and native SegWit (from my script). 
But i'd like (study purpose) to replicate in regtest a P2PK transaction, is it possible (at the moment i'm using 0.20.0)? thanks again
monkeyUser
CC BY-SA 4.0
2020-12-11T19:19:06.607Z
Ok before bitcoin was locked on Public key, and only who provides a signature the satifies the OP_CHECKSIG (stack with 1 result) can move it. Now We paid to hash of it (in P2PKH). Anyway, is it possible to ricreate the old time? :)
monkeyUser
CC BY-SA 4.0
2020-12-11T19:25:06.880Z
I presume that I need to do something like that

`$ bitcoin-cli generatetoaddress 101 uncompressed_public_key
error code: -5
error message:
Error: Invalid address`
monkeyUser
CC BY-SA 4.0
2020-12-11T19:28:03.583Z
It is not possible to give a pubkey as an address, pubkeys are not addresses and do not have addresses. You cannot create a P2PK transaction in Bitcoin Core.
Ava Chow
CC BY-SA 4.0
2020-12-11T19:35:51.217Z
@monkeyUser Bitcoin _never_ had any address type corresponding to P2PK. What it did (very briefly) have was a way to ask another node (over the P2P network!) how to pay it, and it would respond not with an address, but with a scriptPubKey directly. That scriptPubKey could be a P2PK. But since the introduction of bitcoin addresses (before the first release!), they have never had the ability to encode a P2PK script.
Pieter Wuille
CC BY-SA 4.0
2020-12-11T17:05:58.370Z
2020-12-11T18:37:42.793Z
2020-12-11T18:37:42.793Z
Ava Chow
<p>I'm studying Bitcoin protocol, and I'm able to create address by my bash script.</p>
<p>The addresses &quot;P2PKH&quot; and &quot;P2PK&quot; have the same procedure.</p>
<p>Compressed or uncompressed public key – SHA256 – RIPEMD160.
When I create a transaction using <code>createrawtransaction</code> I can see that the protocol use P2PKH validation by default, and in <code>listunspent</code> I can find its scriptPubKey.</p>
<p>My question is: How the protocol (and where in the code) recognize the validation? I'd like to understand when the protocol add
<code>OP_PUSHBYTES[33] PBKEY OP_CHECKSIG</code> instead of <code>OP_DUP OP_HASH160 OP_PUSHBYTES[20] HASH160(PB) OP_EQUALVERIFY OP_CHECKSIG</code> and How can I use <code>P2PK</code> instead of <code>P2PKH</code>?</p>
<p><em><strong>UPDATE</strong></em></p>
<p>For istance, my address is: (testnet)</p>
<p><code>mwqCgoJBcyciRUawXerkvom8sbg3zkjrvV</code></p>
<p>Import my address (I don't have it in my wallet, I created this address from outside of bitcoin-core)</p>
<pre><code>bitcoin-cli importaddress mwqCgoJBcyciRUawXerkvom8sbg3zkjrvV
</code></pre>
<p>then I mined 101 blocks</p>
<pre><code>bitcoin-cli generatetoaddress 101 mwqCgoJBcyciRUawXerkvom8sbg3zkjrvV &gt;&gt; /dev/null
</code></pre>
<p>After that I check my UTXO:</p>
<pre><code>bitcoin-cli listunspent 1 101 '[&quot;'mwqCgoJBcyciRUawXerkvom8sbg3zkjrvV'&quot;]'
[
  {
    &quot;txid&quot;: &quot;531dcfeea3f87feb4101bfea6fe91ffb0262072cb27acc56f885677ccca089cd&quot;,
    &quot;vout&quot;: 0,
    &quot;address&quot;: &quot;mwqCgoJBcyciRUawXerkvom8sbg3zkjrvV&quot;,
    &quot;label&quot;: &quot;&quot;,
    &quot;scriptPubKey&quot;: &quot;76a914b2f58389a224c2fac62b51e78aad7ed058b50f5f88ac&quot;,
    &quot;amount&quot;: 50.00000000,
    &quot;confirmations&quot;: 101,
    &quot;spendable&quot;: false,
    &quot;solvable&quot;: false,
    &quot;safe&quot;: true
  }
]
</code></pre>
<p>retrieve the UTXO</p>
<pre><code>bitcoin-cli getrawtransaction 531dcfeea3f87feb4101bfea6fe91ffb0262072cb27acc56f885677ccca089cd 2 | jq

...
{
      &quot;value&quot;: 50,
      &quot;n&quot;: 0,
      &quot;scriptPubKey&quot;: {
        &quot;asm&quot;: &quot;OP_DUP OP_HASH160 b2f58389a224c2fac62b51e78aad7ed058b50f5f OP_EQUALVERIFY OP_CHECKSIG&quot;,
        &quot;hex&quot;: &quot;76a914b2f58389a224c2fac62b51e78aad7ed058b50f5f88ac&quot;,
        &quot;reqSigs&quot;: 1,
        &quot;type&quot;: &quot;pubkeyhash&quot;,
        &quot;addresses&quot;: [
          &quot;mwqCgoJBcyciRUawXerkvom8sbg3zkjrvV&quot;
        ]
      }
    },

...
</code></pre>
<p>Why I have P2PKH instead of P2PK? And how can have the hash of my Public key if I imported just address (HASH160(PB))? And the last question, I understand that if the wallet find the <code>1</code> as the first char it apply P2PKH by default. How Can I use P2PK instead (study purpose).</p>

<transactions><address><p2pkh><p2pk>
How Bitcoin decide which scriptPubKey use?
monkeyUser
# How Bitcoin decide which scriptPubKey use?

(Asked by: username_0 on 2020-12-11)

I'm studying Bitcoin protocol, and I'm able to create address by my bash script.

The addresses "P2PKH" and "P2PK" have the same procedure.

Compressed or uncompressed public key – SHA256 – RIPEMD160.
When I create a transaction using `createrawtransaction` I can see that the protocol use P2PKH validation by default, and in `listunspent` I can find its scriptPubKey.

My question is: How the protocol (and where in the code) recognize the validation? I'd like to understand when the protocol add
`OP_PUSHBYTES[33] PBKEY OP_CHECKSIG` instead of `OP_DUP OP_HASH160 OP_PUSHBYTES[20] HASH160(PB) OP_EQUALVERIFY OP_CHECKSIG` and How can I use `P2PK` instead of `P2PKH`?

***UPDATE***

For istance, my address is: (testnet)

`mwqCgoJBcyciRUawXerkvom8sbg3zkjrvV`

Import my address (I don't have it in my wallet, I created this address from outside of bitcoin-core)

```
bitcoin-cli importaddress mwqCgoJBcyciRUawXerkvom8sbg3zkjrvV

```

then I mined 101 blocks

```
bitcoin-cli generatetoaddress 101 mwqCgoJBcyciRUawXerkvom8sbg3zkjrvV >> /dev/null

```

After that I check my UTXO:

```
bitcoin-cli listunspent 1 101 '["'mwqCgoJBcyciRUawXerkvom8sbg3zkjrvV'"]'
[
  {
    "txid": "531dcfeea3f87feb4101bfea6fe91ffb0262072cb27acc56f885677ccca089cd",
    "vout": 0,
    "address": "mwqCgoJBcyciRUawXerkvom8sbg3zkjrvV",
    "label": "",
    "scriptPubKey": "76a914b2f58389a224c2fac62b51e78aad7ed058b50f5f88ac",
    "amount": 50.00000000,
    "confirmations": 101,
    "spendable": false,
    "solvable": false,
    "safe": true
  }
]

```

retrieve the UTXO

```
bitcoin-cli getrawtransaction 531dcfeea3f87feb4101bfea6fe91ffb0262072cb27acc56f885677ccca089cd 2 | jq

...
{
      "value": 50,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 b2f58389a224c2fac62b51e78aad7ed058b50f5f OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a914b2f58389a224c2fac62b51e78aad7ed058b50f5f88ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "mwqCgoJBcyciRUawXerkvom8sbg3zkjrvV"
        ]
      }
    },

...

```

Why I have P2PKH instead of P2PK? And how can have the hash of my Public key if I imported just address (HASH160(PB))? And the last question, I understand that if the wallet find the `1` as the first char it apply P2PKH by default. How Can I use P2PK instead (study purpose).

**Tags:** transactions, address, p2pkh, p2pk


---

## 1 answers

### Answer by username_1 on 2020-12-11. Score: 1 | Accepted answer

The protocol is not what decides the scriptPubKey, it's your wallet that creates the transaction. Your wallet chooses what scriptPubKey to use based on the address that it is given. It uses P2PKH for `1...` addresses because that's the scriptPubKey that that type of address specifies. It cannot use those addresses for P2PK because they do not contain enough information for P2PK - they only have the hash160 of the pubkey, not the full pubkey itself. Additionally, using a different scriptPubKey for those addresses would mean that the Bitcoin is sent to a scriptPubKey that the receiver is not necessarily watching.

The same applies to all of the other address types - the address type specifies the scriptPubKey to create.

---

> Why I have P2PKH instead of P2PK?

Because that's the address type you imported. You are using regtest, so P2PKH addresses begin with `m` or `n`.

> And how can have the hash of my Public key if I imported just address (HASH160(PB))?

The address specifies the hash.

> And the last question, I understand that if the wallet find the 1 as the first char it apply P2PKH by default. How Can I use P2PK instead (study purpose).

You cannot. P2PK does not have an address type. There are no P2PK addresses.

Some software and blockchain explorers will show P2PKH addresses for P2PK scriptPubKeys, but this is incorrect. P2PK scriptPubKeys do not have a corresponding address.

> **Comments:**
> **username_0**: Thanks for your comments, I updated my question. thanks again  
> **username_1**: I've updated my answer to answer your questions.  
> **username_0**: thanks again, I understand the the wallet can decode the address and retrieve HASH160(PB) and put in scriptPubKey like: `printf mwqCgoJBcyciRUawXerkvom8sbg3zkjrvV" | base58 -c -d | xxd -p` =>     `6fb2f58389a224c2fac62b51e78aad7ed058b50f5f`  
> **username_0**: but in this case https://btc.bitaps.com/f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16 the address is: `1Q2TWHE3GMdB6BZKafqwxXtWAWgFt5Jvm3` and the public key (uncompressed) is: `04ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84c`. How can I generate `1Q2TWHE3GMdB6BZKafqwxXtWAWgFt5Jvm3`? In P2SH I can create a script like `$PBLENGTH$PB"AC"`.  
> **username_1**: There is no address for that scriptPubKey. It is incorrect for the block explorer to be showing that. You can create an address for a given pubkey by hashing it, however that address will not correspond to a P2PK scriptPubKey. You can read https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses#How_to_create_Bitcoin_Address for some more information on how an address can be created from a pubkey.  
> **username_0**: Thanks again, I understand and I can reproduce the generation of P2PKH, P2SH, wrapped and native SegWit (from my script).  But i'd like (study purpose) to replicate in regtest a P2PK transaction, is it possible (at the moment i'm using 0.20.0)? thanks again  
> **username_0**: Ok before bitcoin was locked on Public key, and only who provides a signature the satifies the OP_CHECKSIG (stack with 1 result) can move it. Now We paid to hash of it (in P2PKH). Anyway, is it possible to ricreate the old time? :)  
> **username_0**: I presume that I need to do something like that  `$ bitcoin-cli generatetoaddress 101 uncompressed_public_key error code: -5 error message: Error: Invalid address`  
> **username_1**: It is not possible to give a pubkey as an address, pubkeys are not addresses and do not have addresses. You cannot create a P2PK transaction in Bitcoin Core.  
> **username_2**: @username_0 Bitcoin _never_ had any address type corresponding to P2PK. What it did (very briefly) have was a way to ask another node (over the P2P network!) how to pay it, and it would respond not with an address, but with a scriptPubKey directly. That scriptPubKey could be a P2PK. But since the introduction of bitcoin addresses (before the first release!), they have never had the ability to encode a P2PK script.  

---

