How Bitcoin decide which scriptPubKey use? I'm studying Bitcoin protocol, and I'm able to create address by my bash script.
The addresses &quot;P2PKH&quot; and &quot;P2PK&quot; 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 &gt;&gt; /dev/null

After that I check my UTXO:
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
  }
]

retrieve the UTXO
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;
        ]
      }
    },

...

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