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?


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:


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)
02: OP_INT_0x02:         type tag INTEGER indicating length
20: OP_LENGTH_0x20:      this is SIG S (32 Bytes)
01: OP_SIGHASHALL:       this terminates the ECDSA signature (ASN1-DER structure)
21: OP_DATA_0x21:        length compressed Public Key (X9.63 form, 33 Bytes)

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:


can be decomposed to this:

    76: OP_DUP
    A9: OP_HASH160
    14: OP_Data14 (= decimal 20)
  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.

  • 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 Nov 8 '17 at 12:05
  • effectively I have a set of (OpenBSD/MacOS/Linux) shell scripts, which do this for me. I created them based on this site here: en.bitcoin.it/wiki/Script. – pebwindkraft Nov 8 '17 at 12:09

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.

  • 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 Nov 7 '17 at 16:51
  • 1
    The second output of blockchain.info/tx/… is a P2PKH output. The only input of blockchain.info/tx/… spends that output. In that input script, there are two pushdatas, the first is the signature, the second is the public key. – Andrew Chow Nov 7 '17 at 16:54
  • 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 Nov 7 '17 at 17:05
  • 1
    The scriptPubKey is the output script – Andrew Chow Nov 7 '17 at 17:14

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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