I'm trying to sign an input in my transaction. The question is: which key has to be used in the "script," private or public? It would be silly to disclose a private key there, right? But how will the network be sure it's me, if I provide a public key (which is known by everybody)?


The public key is used in the script. Technically, it is the pubKeyHash, which is RIPEMD160(SHA256(publicKey)), which means the public key isn't revealed until the transaction gets spent.

The way that validators can ensure it is you who spends the money locked up with a specific public key is by digitally signing the transaction which spends it, with the private key matching the public key. A digital signature can be verified against the public key to ensure that only the corresponding private key could have possibly created the signature.

Assuming a P2PKH transaction having scriptPubKey of the form:


The input script to spend simply has the form

signature publicKey

When the output is spent, the signature of the spending transaction and the public key are pushed on the stack before the scriptPubKey is evaluated.

The script first duplicates the public key on the top of the stack with with OP_DUP, and performs the OP_HASH160 on the top item. (HASH160 performs the same RIPEM160 and SHA256 which created the original pubKeyHash). This removes the duplicated public key from the stack and replaces it with the public key hash.

The script then pushes the pubKeyHash which was embedded in the scriptPubKey onto the stack. The two items at the top of the stack are then compared for equality (OP_EQUALVERIFY), and popped off the stack. If equality test fails, the script fails early.

The remaining public key and signature at the top of the stack become the arguments to OP_CHECKSIG, which verifies that the signature of the entire transaction corresponds to the public key. The signature and public key are popped from the stack and replaced with either true or false, indicating the final result of the script evaluation.

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.