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

<p>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.</p>

<p>Assuming a P2PKH transaction having <code>scriptPubKey</code> of the form:</p>

<pre><code>OP_DUP OP_HAS160 pubKeyHash OP_EQUALVERIFY OP_CHECKSIG
</code></pre>

<p>The input script to spend simply has the form</p>

<pre><code>signature publicKey
</code></pre>

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

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

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

<p>The remaining public key and signature at the top of the stack become the arguments to <code>OP_CHECKSIG</code>, 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.</p>

CC BY-SA 4.0
2019-04-10T08:20:16.697Z
2019-04-10T08:26:06.427Z
2019-04-10T08:26:06.427Z
Mark H
<p>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)?</p>

<protocol>
What key is used to create a script?
yegor256
# What key is used to create a script?

(Asked by: username_0 on 2019-04-10)

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

**Tags:** protocol


---

## 1 answers

### Answer by username_1 on 2019-04-10. Score: 1 | Accepted answer

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:

```
OP_DUP OP_HAS160 pubKeyHash OP_EQUALVERIFY OP_CHECKSIG

```

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.


---

