<p>Bitcoin Script is a programming language. As such, it is similar to other languages - You need to know what you are trying to achieve to be able to write in it.</p>

<p>The script you've presented is a P2PKH (pay-to-pubkey-hash) script. The goal here is to allow someone to spend Bitcoin with the corresponding private key by applying a signature. In this kind of verification, you take the input pubkey, duplicate it (OP_DUP), hash160 the duplicate (OP_HASH160), push the hashed value, check if the value you created in the first two operations matches the one you just pushed (OP_EQUALVERIFY), and finally check the signature against the original, pre-dup pubkey (OP_CHECKSIG)</p>

<p>Since Bitcoin Script is a stack based language, it looks a little different to the day to day ones we see (Python, Java, etc). However, it is just another programming language, and you can write code in it just the same way.</p>

<p>Just for reference, a similar program in a more conventional language would look something like:</p>

<pre><code>verifyInputScript(signature, pubkey, outputscript):
    inputPubkeyHash160 = hash160(pubkey)
    if inputPubkeyHash160 != outputscript.hash160:
        return "Invalid"
    else:
        isSignatureValid = verifySignature(signature, pubkey)
        return isSignatureValid
</code></pre>

CC BY-SA 4.0
2018-05-06T14:18:36.203Z
So does that mean that the P2PKH can be written in another way? using more variables but achieving the same goal?
Kyle Graham
CC BY-SA 4.0
2018-05-06T14:22:27.687Z
You could possibly rewrite it in a number of ways - For instance, a very simple expansion would be to split OP_HASH160 into the OP_SHA256 and OP_RIPEMD160 (hash160 is just `ripemd160(sha256(x))`).
Raghav Sood
CC BY-SA 4.0
2018-05-06T14:24:09.563Z
The parameters cannot change though? Also, what's stopping me from just using OP_1 and returning true, for a scriptSig?
Kyle Graham
CC BY-SA 4.0
2018-05-06T14:27:39.773Z
An input script is combined with the output script (the one you posted). Input scripts can only contain pushes, so any non-push would invalide the transaction right away. You can push any parameters you like, but they must validate against the output script. Using OP_1 will push 1, then duplicate it, then hash160, then equalverify it. At this point, the equalverify will fail. Moreover, even if you pushed just the pubkey, the equalverify would success, but the stack would be empty since OP_EQUALVERIFY consumes two entries, making your script fail on OP_CHECKSIG
Raghav Sood
CC BY-SA 4.0
2018-05-06T14:32:24.900Z
How does the input script get combined with the output script? or when does it happen? 

If I wanted to spend from input1 in TransA, how would this be setup?
Kyle Graham
CC BY-SA 4.0
2018-05-06T14:42:24.510Z
It happens during the transaction validation. When you select your inputs, you attach the data that acts as parameters. The bitcoin node validating the tx will then look up your input at the output point, and pick the output script from there. It then combines your input script with the original output script, and evaluates it.
Raghav Sood
CC BY-SA 4.0
2018-05-06T14:43:12.697Z
[The First example](https://en.bitcoin.it/wiki/Script#Script_examples) on that page walks you through a standard P2PKH transaction step by step
Raghav Sood
CC BY-SA 4.0
2018-05-06T14:44:57.453Z
Ohh I see. Thanks for the link, it makes a lot of sense now. The locking script puts items onto the stack and it is the job of the output script to make it evaluate to true.
Kyle Graham
CC BY-SA 4.0
2018-05-06T14:16:10.987Z
2018-05-06T14:16:10.987Z
Raghav Sood
<p>The scriptSig is: </p>

<pre><code>    OP_DUP OP_HASH160 404371705fa9bd789a2fcd52d2c580b65d35549d
    OP_EQUALVERIFY OP_CHECKSIG
</code></pre>

<p>How did people know that this was the correct permutation of OPCodes and data?</p>

<transactions><bitcoin-core><checksig>
How do people know how to construct the OPCodes?
Kyle Graham
# How do people know how to construct the OPCodes?

(Asked by: username_0 on 2018-05-06)

The scriptSig is:

```
    OP_DUP OP_HASH160 404371705fa9bd789a2fcd52d2c580b65d35549d
    OP_EQUALVERIFY OP_CHECKSIG

```

How did people know that this was the correct permutation of OPCodes and data?

**Tags:** transactions, bitcoin-core, checksig


---

## 1 answers

### Answer by username_1 on 2018-05-06. Score: 1 | Accepted answer

Bitcoin Script is a programming language. As such, it is similar to other languages - You need to know what you are trying to achieve to be able to write in it.

The script you've presented is a P2PKH (pay-to-pubkey-hash) script. The goal here is to allow someone to spend Bitcoin with the corresponding private key by applying a signature. In this kind of verification, you take the input pubkey, duplicate it (OP\_DUP), hash160 the duplicate (OP\_HASH160), push the hashed value, check if the value you created in the first two operations matches the one you just pushed (OP\_EQUALVERIFY), and finally check the signature against the original, pre-dup pubkey (OP\_CHECKSIG)

Since Bitcoin Script is a stack based language, it looks a little different to the day to day ones we see (Python, Java, etc). However, it is just another programming language, and you can write code in it just the same way.

Just for reference, a similar program in a more conventional language would look something like:

```
verifyInputScript(signature, pubkey, outputscript):
    inputPubkeyHash160 = hash160(pubkey)
    if inputPubkeyHash160 != outputscript.hash160:
        return "Invalid"
    else:
        isSignatureValid = verifySignature(signature, pubkey)
        return isSignatureValid

```

> **Comments:**
> **username_0**: So does that mean that the P2PKH can be written in another way? using more variables but achieving the same goal?  
> **username_1**: You could possibly rewrite it in a number of ways - For instance, a very simple expansion would be to split OP_HASH160 into the OP_SHA256 and OP_RIPEMD160 (hash160 is just `ripemd160(sha256(x))`).  
> **username_0**: The parameters cannot change though? Also, what's stopping me from just using OP_1 and returning true, for a scriptSig?  
> **username_1**: An input script is combined with the output script (the one you posted). Input scripts can only contain pushes, so any non-push would invalide the transaction right away. You can push any parameters you like, but they must validate against the output script. Using OP_1 will push 1, then duplicate it, then hash160, then equalverify it. At this point, the equalverify will fail. Moreover, even if you pushed just the pubkey, the equalverify would success, but the stack would be empty since OP_EQUALVERIFY consumes two entries, making your script fail on OP_CHECKSIG  
> **username_0**: How does the input script get combined with the output script? or when does it happen?   If I wanted to spend from input1 in TransA, how would this be setup?  
> **username_1**: It happens during the transaction validation. When you select your inputs, you attach the data that acts as parameters. The bitcoin node validating the tx will then look up your input at the output point, and pick the output script from there. It then combines your input script with the original output script, and evaluates it.  
> **username_1**: [The First example](https://en.bitcoin.it/wiki/Script#Script_examples) on that page walks you through a standard P2PKH transaction step by step  
> **username_0**: Ohh I see. Thanks for the link, it makes a lot of sense now. The locking script puts items onto the stack and it is the job of the output script to make it evaluate to true.  

---

