<p><a href="http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.html" rel="nofollow noreferrer">http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.html</a> explains manually signing a transaction, and helped me alot (next to the online readable book "Mastering Bitcoin" from Andreas). Looking at the example tx from before: <a href="https://blockchain.info/rawtx/e46a88ed211c1ee7f34f0f4828611da52404c3282416ae1e3b7096f9dddc6c4e?format=hex" rel="nofollow noreferrer">https://blockchain.info/rawtx/e46a88ed211c1ee7f34f0f4828611da52404c3282416ae1e3b7096f9dddc6c4e?format=hex</a>
one can see the sigscript in this section:</p>

<blockquote>
  <p>483045022100B8E264B50017806D4095740E5523188C78884DCFCBA3AA7C1E608F28A1A763AF0220159680F7870BE5B46B6CD3110629AEAB221E6D658DD396AE319B071E0D2B339601210214A307355361CC5571154EEBA68E7F4799F5520656E1AA6BA5E80BA65C6AA190</p>
</blockquote>

<p>when decomposing, it looks like this:</p>

<pre><code>48: OP_DATA_0x48:        push hex 48 (decimal 72) bytes on stack
30: OP_SEQUENCE_0x30:    type tag indicating SEQUENCE, begin sigscript
45: OP_LENGTH_0x44:      length of R + S
02: OP_INT_0x02:         type tag INTEGER indicating length
21: OP_LENGTH_0x20:      this is SIG R (33 Bytes)
    00B8E264B5001780:6D4095740E552318
    8C78884DCFCBA3AA:7C1E608F28A1A763
    AF
02: OP_INT_0x02:         type tag INTEGER indicating length
20: OP_LENGTH_0x20:      this is SIG S (32 Bytes)
    159680F7870BE5B4:6B6CD3110629AEAB
    221E6D658DD396AE:319B071E0D2B3396
01: OP_SIGHASHALL:       this terminates the ECDSA signature (ASN1-DER structure)
21: OP_DATA_0x21:        length compressed Public Key (X9.63 form, 33 Bytes)
        0214A307355361CC:5571154EEBA68E7F
        4799F5520656E1AA:6BA5E80BA65C6AA1
        90
</code></pre>

<p>This last line(s) after OP_DATA_0x21 is the pubkey, which allows to verify the signature. The corresponding bitcoin address is: 1FNtjbmxmGyt3MvHpWjhoR4ztTabUox3vp.</p>

<p>This whole structure is part of the "input" of a transaction. It shows that the owner had the right priv key to sign the tx, so he is allowed to spend the funds. Now the specification "Pay 2 Public Key Hash" is output related, aka "where shall the funds go to". And this is part of the output structure in this transaction. This string:</p>

<blockquote>
  <p>76A914E1315C0FA59687EF4E035C184151CFCB096BE4EE88AC</p>
</blockquote>

<p>can be decomposed to this:</p>

<pre><code>    76: OP_DUP
    A9: OP_HASH160
    14: OP_Data14 (= decimal 20)
        E1315C0FA59687EF:4E035C184151CFCB
        096BE4EE
    88: OP_EQUALVERIFY
    AC: OP_CHECKSIG
  This is a P2PKH script
</code></pre>

<p>which contains after the OP_DATA_14 the <strong>public key hash</strong>. You can base58check encode this value, which returns this bitcoin address:       1MXiAjDmJzySYm6Re6vV3WgSP7rVc57LMh</p>

<p>So coming back to your question ("How it is possible to validate existing Pay to Public Key Hash transaction without public key?"): 
I think it is phrased in a way, that doesn't fit into the logic of bitcoin. You have to prove with the signature, that you are able to spend the funds, and this is done in the input part of the tx. And yes, therefor you need the pubkey, and this pubkey is usually appended after the signature. 
The wording "p2pkh tx" specifies an output related way of spending the funds. You could also spend to a multisig, or even to a smart contract tx. This doesn't change the rules of the input/signature/pubkey part. </p>

CC BY-SA 3.0
2017-11-08T12:05:04.087Z
Thank you for very detailed answer. I will read this and the articles again and again. A question, Can you please tell me how do you decode scriptSig which is this one 4830........ in your example
Freshblood
CC BY-SA 3.0
2017-11-08T12:09:39.477Z
effectively I have a set of (OpenBSD/MacOS/Linux) shell scripts, which do this for me. I created them based on this site here: https://en.bitcoin.it/wiki/Script.
pebwindkraft
CC BY-SA 3.0
2017-11-07T22:41:16.903Z
2017-11-07T22:41:16.903Z
pebwindkraft
<p>A transaction which spends from a P2PKH output provides both the signature and public key in the input script. So the public key is part of the transaction data, just the spending transaction, not the output creating transaction.</p>

CC BY-SA 3.0
2017-11-07T16:51:46.830Z
It is Pay 2 Public Key Hash so it is expected that it will only have Public Key Hash. I can only see scriptSig and Public Key hash but not public key.

Can you please give me an example from blockexplorer ?
Freshblood
CC BY-SA 3.0
2017-11-07T16:54:42.067Z
The second output of https://blockchain.info/tx/e46a88ed211c1ee7f34f0f4828611da52404c3282416ae1e3b7096f9dddc6c4e?show_adv=true is a P2PKH output. The only input of https://blockchain.info/tx/6bb09e10bb685a032f88689cb4c1b881ce3834f8b918a175032cdc20ba98dc0f?show_adv=true spends that output. In that input script, there are two pushdatas, the first is the signature, the second is the public key.
Ava Chow
CC BY-SA 3.0
2017-11-07T17:05:04.317Z
I see script data there but not scriptPubKey. I am expected that there should to be scriptPubKey there if it is P2PKH transaction. Am i wrong ?
Freshblood
CC BY-SA 3.0
2017-11-07T17:14:58.310Z
The scriptPubKey is the output script
Ava Chow
CC BY-SA 3.0
2017-11-07T16:47:36.197Z
2017-11-07T16:47:36.197Z
Ava Chow
<blockquote>
  <p>And verification of signature requires public key, signature and
  message as below VerifySignature(signature, message , public key)</p>
</blockquote>

<p>... and the public key itself can be derived from signature and message :)</p>

CC BY-SA 3.0
2017-11-07T22:08:24.440Z
Can you prove this ? At least  can you point an article for me ? I spent my days understand these details : (
Freshblood
CC BY-SA 3.0
2017-11-08T04:41:04.710Z
https://bitcointalk.org/index.php?topic=6430.0 note: this is not used in bitcoin
amaclin
CC BY-SA 3.0
2017-11-07T17:20:43.710Z
2017-11-07T17:20:43.710Z
amaclin
<p>These are my understanding or assumptions.</p>

<p>Sign a document requires secret key and message only as below</p>

<pre><code>SignDocument(secret key, message) =&gt; signature
</code></pre>

<p>And verification of signature requires public key, signature and message as below</p>

<pre><code>VerifySignature(signature, message , public key)
</code></pre>

<p>Purpose of scriptPubKey is a script for validating signature for P2PKH transactions. </p>

<p>It is clear that we need public key in order to validate transaction but existing transaction data doesn't have public key data but hash of public key only. </p>

<p>I can easily understand Pay to Public Key transaction because public key is part of transaction data. </p>

<p>If public key is not stored in blockchain so how it is possible to validate P2PKH transaction without public key? </p>

<transactions><transaction-verification><p2pkh>
How it is possible to validate existing Pay to Public Key Hash transaction without public key?
Freshblood
# How it is possible to validate existing Pay to Public Key Hash transaction without public key?

(Asked by: username_0 on 2017-11-07)

These are my understanding or assumptions.

Sign a document requires secret key and message only as below

```
SignDocument(secret key, message) => signature

```

And verification of signature requires public key, signature and message as below

```
VerifySignature(signature, message , public key)

```

Purpose of scriptPubKey is a script for validating signature for P2PKH transactions.

It is clear that we need public key in order to validate transaction but existing transaction data doesn't have public key data but hash of public key only.

I can easily understand Pay to Public Key transaction because public key is part of transaction data.

If public key is not stored in blockchain so how it is possible to validate P2PKH transaction without public key?

**Tags:** transactions, transaction-verification, p2pkh


---

## 3 answers

### Answer by username_1 on 2017-11-07. Score: 2 | Accepted answer

<http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.html> explains manually signing a transaction, and helped me alot (next to the online readable book "Mastering Bitcoin" from Andreas). Looking at the example tx from before: <https://blockchain.info/rawtx/e46a88ed211c1ee7f34f0f4828611da52404c3282416ae1e3b7096f9dddc6c4e?format=hex>
one can see the sigscript in this section:

> 483045022100B8E264B50017806D4095740E5523188C78884DCFCBA3AA7C1E608F28A1A763AF0220159680F7870BE5B46B6CD3110629AEAB221E6D658DD396AE319B071E0D2B339601210214A307355361CC5571154EEBA68E7F4799F5520656E1AA6BA5E80BA65C6AA190

when decomposing, it looks like this:

```
48: OP_DATA_0x48:        push hex 48 (decimal 72) bytes on stack
30: OP_SEQUENCE_0x30:    type tag indicating SEQUENCE, begin sigscript
45: OP_LENGTH_0x44:      length of R + S
02: OP_INT_0x02:         type tag INTEGER indicating length
21: OP_LENGTH_0x20:      this is SIG R (33 Bytes)
    00B8E264B5001780:6D4095740E552318
    8C78884DCFCBA3AA:7C1E608F28A1A763
    AF
02: OP_INT_0x02:         type tag INTEGER indicating length
20: OP_LENGTH_0x20:      this is SIG S (32 Bytes)
    159680F7870BE5B4:6B6CD3110629AEAB
    221E6D658DD396AE:319B071E0D2B3396
01: OP_SIGHASHALL:       this terminates the ECDSA signature (ASN1-DER structure)
21: OP_DATA_0x21:        length compressed Public Key (X9.63 form, 33 Bytes)
        0214A307355361CC:5571154EEBA68E7F
        4799F5520656E1AA:6BA5E80BA65C6AA1
        90

```

This last line(s) after OP\_DATA\_0x21 is the pubkey, which allows to verify the signature. The corresponding bitcoin address is: 1FNtjbmxmGyt3MvHpWjhoR4ztTabUox3vp.

This whole structure is part of the "input" of a transaction. It shows that the owner had the right priv key to sign the tx, so he is allowed to spend the funds. Now the specification "Pay 2 Public Key Hash" is output related, aka "where shall the funds go to". And this is part of the output structure in this transaction. This string:

> 76A914E1315C0FA59687EF4E035C184151CFCB096BE4EE88AC

can be decomposed to this:

```
    76: OP_DUP
    A9: OP_HASH160
    14: OP_Data14 (= decimal 20)
        E1315C0FA59687EF:4E035C184151CFCB
        096BE4EE
    88: OP_EQUALVERIFY
    AC: OP_CHECKSIG
  This is a P2PKH script

```

which contains after the OP\_DATA\_14 the **public key hash**. You can base58check encode this value, which returns this bitcoin address: 1MXiAjDmJzySYm6Re6vV3WgSP7rVc57LMh

So coming back to your question ("How it is possible to validate existing Pay to Public Key Hash transaction without public key?"):
I think it is phrased in a way, that doesn't fit into the logic of bitcoin. You have to prove with the signature, that you are able to spend the funds, and this is done in the input part of the tx. And yes, therefor you need the pubkey, and this pubkey is usually appended after the signature.
The wording "p2pkh tx" specifies an output related way of spending the funds. You could also spend to a multisig, or even to a smart contract tx. This doesn't change the rules of the input/signature/pubkey part.

> **Comments:**
> **username_0**: Thank you for very detailed answer. I will read this and the articles again and again. A question, Can you please tell me how do you decode scriptSig which is this one 4830........ in your example  
> **username_1**: effectively I have a set of (OpenBSD/MacOS/Linux) shell scripts, which do this for me. I created them based on this site here: https://en.bitcoin.it/wiki/Script.  

---

### Answer by username_2 on 2017-11-07. Score: 3

A transaction which spends from a P2PKH output provides both the signature and public key in the input script. So the public key is part of the transaction data, just the spending transaction, not the output creating transaction.

> **Comments:**
> **username_0**: It is Pay 2 Public Key Hash so it is expected that it will only have Public Key Hash. I can only see scriptSig and Public Key hash but not public key.  Can you please give me an example from blockexplorer ?  
> **username_2**: The second output of https://blockchain.info/tx/e46a88ed211c1ee7f34f0f4828611da52404c3282416ae1e3b7096f9dddc6c4e?show_adv=true is a P2PKH output. The only input of https://blockchain.info/tx/6bb09e10bb685a032f88689cb4c1b881ce3834f8b918a175032cdc20ba98dc0f?show_adv=true spends that output. In that input script, there are two pushdatas, the first is the signature, the second is the public key.  
> **username_0**: I see script data there but not scriptPubKey. I am expected that there should to be scriptPubKey there if it is P2PKH transaction. Am i wrong ?  
> **username_2**: The scriptPubKey is the output script  

---

### Answer by username_3 on 2017-11-07. Score: 0

> And verification of signature requires public key, signature and
> message as below VerifySignature(signature, message , public key)

... and the public key itself can be derived from signature and message :)

> **Comments:**
> **username_0**: Can you prove this ? At least  can you point an article for me ? I spent my days understand these details : (  
> **username_3**: https://bitcointalk.org/index.php?topic=6430.0 note: this is not used in bitcoin  

---

