<p>Let's go over this step by step. Suppose say you want to lock some bitcoins in a 2-of-3 multi-sig.</p>

<p><strong>Locking steps</strong>  </p>

<ol>
<li>Create a multi-sig script: <code>OP_2 &lt;pub_key 1&gt; &lt;pub_key2&gt; &lt;pub_key3&gt; OP_3 OP_CHECKMULTISIG</code>.</li>
<li>Hash the multi-sig script with <code>HASH160</code> which gives you <code>multi-sig_scripthash</code>.</li>
<li>Base58Check the <code>multi-sig_scripthash</code> with version 0x05 giving you an address starting with <code>3...</code>.</li>
<li>The <code>scriptPubKey</code> will be <code>OP_HASH160 &lt;multi-sig_scripthash&gt; OP_EQUAL</code>.</li>
</ol>

<p><strong>Unlocking Steps</strong>  </p>

<ol>
<li><p>Your stack will start at <code>&lt;signatures&gt;&lt;redeemScript&gt;</code>. The signature is <code>OP_0 &lt;sig_A&gt; &lt;sig_B&gt;</code> and <code>redeemScript</code> is <code>OP_2 &lt;pub_key 1&gt; &lt;pub_key2&gt; &lt;pub_key3&gt; OP_3 OP_CHECKMULTISIG</code>.</p></li>
<li><p>Since v0.3.7, Bitcoin Core executes the unlocking script first and looks at the resulting stack. So your <code>&lt;signatures&gt;</code> and <code>&lt;redeemScript&gt;</code> are evaluated together first. This would result in a multi-signature evaluation.</p></li>
<li><p>In normal cases the above step should just return 1 as signatures satisfy the redeemScript. But with P2SH there is a caveat added by <a href="https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki" rel="nofollow noreferrer">BIP-16</a>. Whenever you have a <code>OP_HASH160 &lt;hash&gt; OP_EQUAL</code> locking script, it serves as a P2SH identifier. So before the evaluation of <code>&lt;signatures&gt;</code> with <code>&lt;redeemScript&gt;</code> the <code>redeemScript</code> is copied from the stack to another one. Then the evaluation is done with what is on stack (<code>&lt;signatures&gt;&lt;redeemScript&gt;</code> in our case). If successful, replace the current stack with the just <code>redeemScript</code>. Hence, instead of 1 you have <code>&lt;redeemScript&gt;</code> at the top of the stack.</p></li>
<li><p>You run <code>OP_HASH160</code> on the redeemScript. Stack is now <code>&lt;hash160_of_redeemScript&gt;</code></p></li>
<li><p>You push <code>&lt;expectedredeemScripthash&gt;</code>. Stack is now <code>&lt;hash160_of_redeemScript&gt;&lt;expectedredeemScripthash&gt;</code></p></li>
<li><p>You run <code>OP_EQUAL</code> resulting in evaluation and returning 1 to the top of the stack.</p></li>
</ol>

CC BY-SA 4.0
2019-09-18T08:16:54.703Z
Thanks for your answer, I have a question, exist some operator inside bitcoin script for calculate the Base58Check?
vincenzopalazzo
CC BY-SA 4.0
2019-09-18T08:58:39.520Z
@vincenzopalazzo Base58check encoding is for human readability. It is for humans to distinguish whether the address generated/being sent to is P2PKH or P2SH. It does not have any meaning inside the software.
Ugam Kamat
CC BY-SA 4.0
2019-09-18T09:31:25.750Z
My fault, I have jump this note "Note: If you just insert the address starting with 3, the software will automatically calculate the scriptPubKey as above."
vincenzopalazzo
CC BY-SA 4.0
2019-09-18T09:37:39.793Z
@vincenzopalazzo What I meant was, as a user you just enter an address starting with 3. The **wallet** software then transitions it to the scriptPubKey.
Ugam Kamat
CC BY-SA 4.0
2019-09-18T09:46:28.657Z
@vincenzopalazzo as further clarification, addresses are not sent over the wire nor stored in the blockchain. Addresses are only for human readability. So when a user enters an address starting with 3.., the wallet software decodes the base58check, extracts the redeem_script_hash and uses `OP_HASH160 <redeem_scripthash> OP_EQUAL` as the scriptPubKey. If your address began with 1, the wallet software will use `OP_DUP OP_HASH160 <redeem_scripthash> OP_EQUALVERIFY OP_CHECKSIG` as scriptPubKey.
Ugam Kamat
CC BY-SA 4.0
2019-09-18T09:49:43.103Z
Yes thanks for your clarification, you have help me :)
vincenzopalazzo
CC BY-SA 4.0
2019-09-18T06:52:08.420Z
2019-09-18T09:41:37.670Z
2019-09-18T09:41:37.670Z
Ugam Kamat
<p>This question is a continuation of the <a href="https://bitcoin.stackexchange.com/questions/90416/how-does-this-simple-script-use-the-op-checksig-opcode/90417#90417">my post</a> yesterday.</p>

<p>Today I have a question on P2SH execution so, an example of the script should be this</p>

<pre><code>OP_0 &lt;A Signature&gt; &lt;B Signature&gt; OP_2 &lt;Public key A&gt; &lt;Public key B&gt; 
&lt;Public key C&gt; OP_3 OP_CHECKMULTISIG
OP_HASH160 &lt;ScriptSig Hash&gt; OP_EQUAL
</code></pre>

<p>or if used the P2SH key should be is this (I think in this script left-hand something to convert the hash160 inside the P2SH key, right?)</p>

<pre><code>OP_0 &lt;A Signature&gt; &lt;B Signature&gt; OP_2 &lt;Public key A&gt; &lt;Public key B&gt; 
&lt;Public key C&gt; OP_3 OP_CHECKMULTISIG
OP_HASH160 &lt;P2SH key&gt; OP_EQUAL
</code></pre>

<p>Now if I have understood well the simulation of the execution is divided into two phases</p>

<ol>
<li>The hash <code>scriptSig</code> is equal to hash inside the <code>scriptPubKey</code></li>
</ol>

<p>Simulation</p>

<ul>
<li>Put on the <code>scriptSig</code> on the stack, so the stack now is <code>&lt;A sig&gt; &lt;B sig&gt; &lt;A pubkey&gt; &lt;B pubkey&gt; &lt;C pubkey&gt;</code> </li>
<li>Calculate the <code>hash160</code> with the data inside the stack, so now the stack status is <code>&lt;scriptSig hash&gt;</code></li>
<li>Put on the <code>scriptSig</code> hash expected, now the stack status is <code>&lt;hash scriptsig&gt; &lt;hash scriptsigexpected&gt;</code></li>
<li>return the result of the operator <code>OP_EQUAL</code> (true or false)</li>
</ul>

<hr>

<ol start="2">
<li>the <code>scriptSig</code> will execute such as the script multi-signature</li>
</ol>

<p>Simulation</p>

<ul>
<li>put on the stack the <code>scriptSig</code>, the stack status is <code>&lt;A Signature&gt; &lt;B Signature&gt;</code></li>
<li>push the public keys, that stack status is <code>&lt;A Signature&gt; &lt;B Signature&gt; &lt;Public key A&gt; &lt;Public key B&gt; &lt;Public key C&gt;</code></li>
<li>apply the <code>OP_CHECKMULTISIG</code> operator and get the result</li>
</ul>

<p>My questions are:</p>

<ul>
<li>With the P2SH key, the execution is the same? if not, what is the form of the P2SH with the P2SH key?</li>
</ul>

<p>Also, I read this <a href="https://bitcoin.stackexchange.com/questions/73361/how-does-a-p2sh-script-get-executed">post</a> but I don't think contains this answer.</p>

<p>If my example script is wrong, I'm sorry to give me a corrections</p>

<transactions><script><transaction-verification><p2sh>
Clarification on the script P2SH, how is executed to Bitcoin core?
vincenzopalazzo
# Clarification on the script P2SH, how is executed to Bitcoin core?

(Asked by: username_0 on 2019-09-17)

This question is a continuation of the [my post](https://bitcoin.stackexchange.com/questions/90416/how-does-this-simple-script-use-the-op-checksig-opcode/90417#90417) yesterday.

Today I have a question on P2SH execution so, an example of the script should be this

```
OP_0 <A Signature> <B Signature> OP_2 <Public key A> <Public key B> 
<Public key C> OP_3 OP_CHECKMULTISIG
OP_HASH160 <ScriptSig Hash> OP_EQUAL

```

or if used the P2SH key should be is this (I think in this script left-hand something to convert the hash160 inside the P2SH key, right?)

```
OP_0 <A Signature> <B Signature> OP_2 <Public key A> <Public key B> 
<Public key C> OP_3 OP_CHECKMULTISIG
OP_HASH160 <P2SH key> OP_EQUAL

```

Now if I have understood well the simulation of the execution is divided into two phases

1. The hash `scriptSig` is equal to hash inside the `scriptPubKey`

Simulation

* Put on the `scriptSig` on the stack, so the stack now is `<A sig> <B sig> <A pubkey> <B pubkey> <C pubkey>`
* Calculate the `hash160` with the data inside the stack, so now the stack status is `<scriptSig hash>`
* Put on the `scriptSig` hash expected, now the stack status is `<hash scriptsig> <hash scriptsigexpected>`
* return the result of the operator `OP_EQUAL` (true or false)

---

2. the `scriptSig` will execute such as the script multi-signature

Simulation

* put on the stack the `scriptSig`, the stack status is `<A Signature> <B Signature>`
* push the public keys, that stack status is `<A Signature> <B Signature> <Public key A> <Public key B> <Public key C>`
* apply the `OP_CHECKMULTISIG` operator and get the result

My questions are:

* With the P2SH key, the execution is the same? if not, what is the form of the P2SH with the P2SH key?

Also, I read this [post](https://bitcoin.stackexchange.com/questions/73361/how-does-a-p2sh-script-get-executed) but I don't think contains this answer.

If my example script is wrong, I'm sorry to give me a corrections

**Tags:** transactions, script, transaction-verification, p2sh


---

## 1 answers

### Answer by username_1 on 2019-09-18. Score: 1 | Accepted answer

Let's go over this step by step. Suppose say you want to lock some bitcoins in a 2-of-3 multi-sig.

**Locking steps**

1. Create a multi-sig script: `OP_2 <pub_key 1> <pub_key2> <pub_key3> OP_3 OP_CHECKMULTISIG`.
2. Hash the multi-sig script with `HASH160` which gives you `multi-sig_scripthash`.
3. Base58Check the `multi-sig_scripthash` with version 0x05 giving you an address starting with `3...`.
4. The `scriptPubKey` will be `OP_HASH160 <multi-sig_scripthash> OP_EQUAL`.

**Unlocking Steps**

1. Your stack will start at `<signatures><redeemScript>`. The signature is `OP_0 <sig_A> <sig_B>` and `redeemScript` is `OP_2 <pub_key 1> <pub_key2> <pub_key3> OP_3 OP_CHECKMULTISIG`.
2. Since v0.3.7, Bitcoin Core executes the unlocking script first and looks at the resulting stack. So your `<signatures>` and `<redeemScript>` are evaluated together first. This would result in a multi-signature evaluation.
3. In normal cases the above step should just return 1 as signatures satisfy the redeemScript. But with P2SH there is a caveat added by [BIP-16](https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki). Whenever you have a `OP_HASH160 <hash> OP_EQUAL` locking script, it serves as a P2SH identifier. So before the evaluation of `<signatures>` with `<redeemScript>` the `redeemScript` is copied from the stack to another one. Then the evaluation is done with what is on stack (`<signatures><redeemScript>` in our case). If successful, replace the current stack with the just `redeemScript`. Hence, instead of 1 you have `<redeemScript>` at the top of the stack.
4. You run `OP_HASH160` on the redeemScript. Stack is now `<hash160_of_redeemScript>`
5. You push `<expectedredeemScripthash>`. Stack is now `<hash160_of_redeemScript><expectedredeemScripthash>`
6. You run `OP_EQUAL` resulting in evaluation and returning 1 to the top of the stack.

> **Comments:**
> **username_0**: Thanks for your answer, I have a question, exist some operator inside bitcoin script for calculate the Base58Check?  
> **username_1**: @username_0 Base58check encoding is for human readability. It is for humans to distinguish whether the address generated/being sent to is P2PKH or P2SH. It does not have any meaning inside the software.  
> **username_0**: My fault, I have jump this note "Note: If you just insert the address starting with 3, the software will automatically calculate the scriptPubKey as above."  
> **username_1**: @username_0 What I meant was, as a user you just enter an address starting with 3. The **wallet** software then transitions it to the scriptPubKey.  
> **username_1**: @username_0 as further clarification, addresses are not sent over the wire nor stored in the blockchain. Addresses are only for human readability. So when a user enters an address starting with 3.., the wallet software decodes the base58check, extracts the redeem_script_hash and uses `OP_HASH160 <redeem_scripthash> OP_EQUAL` as the scriptPubKey. If your address began with 1, the wallet software will use `OP_DUP OP_HASH160 <redeem_scripthash> OP_EQUALVERIFY OP_CHECKSIG` as scriptPubKey.  
> **username_0**: Yes thanks for your clarification, you have help me :)  

---

