<p>You are computing just the P2SH address for the segwit script. However it is not just P2SH, it is a Segwit script wrapped inside of a P2SH. You actually have to first take the witnessScript (the multisig script or segwit redeemScript) and make a P2WSH output script with it. Then this script becomes the redeemScript for the P2SH script.</p>

<p>So given</p>

<pre><code>5221021e6617e06bb90f621c3800e8c37ab081a445ae5527f6c5f68a022e7133f9b5fe2103bea1a8ce6369435bb74ff1584a136a7efeebfe4bc320b4d59113c92acd869f38210280631b27700baf7d472483fadfe1c4a7340a458f28bf6bae5d3234312d684c6553ae
</code></pre>

<p>as the witnessScript, you produce</p>

<pre><code>002044c55c1da36a576217259c3bc21b0c3943f7eb3ff4e3c381d9fd3502434b9e87
</code></pre>

<p>as the redeemScript. This is a P2WSH output script. This is then hashed to create the hash in the P2SH script:</p>

<pre><code>a914771962306e72e479245d48e879dd2a1862225b4c87
</code></pre>

<p>which has an address of</p>

<pre><code>3CYkk3x1XUvdXCdHtRFdjMjp17PuJ8eR8z
</code></pre>

CC BY-SA 4.0
2020-02-18T15:59:38.470Z
Is there any BIP that defines it?
Allan Romanato
CC BY-SA 4.0
2020-02-18T16:04:48.900Z
[BIP 141](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki).
Ava Chow
CC BY-SA 4.0
2020-02-18T16:17:32.333Z
So for us to generate the redeemScript from witnessScript we need to ripemd160 them?
Allan Romanato
CC BY-SA 4.0
2020-02-18T16:39:52.763Z
No. You have to SHA256 the witnessScript. Then put that into a p2wsh script. Then hash160 that script for the p2sh address.
Ava Chow
CC BY-SA 4.0
2020-02-18T16:44:18.480Z
For native Segwit we don't need this right? Since we won't have the sigScript field anymore.
Allan Romanato
CC BY-SA 4.0
2020-02-19T03:25:23.230Z
For native segwit you still need to hash the witnessScript. But there is no need to make another script and hash that. Native segwit also has a different address type.
Ava Chow
CC BY-SA 4.0
2020-02-18T15:46:33.853Z
2020-02-18T15:46:33.853Z
Ava Chow
<p>I was analyzing a Bitcoin transaction that works both with Segwit and non-Segwit for multisig Scripts.
Here is the transaction: <a href="https://www.blockchain.com/en/btc/tx/80975cddebaa93aa21a6477c0d050685d6820fa1068a2731db0f39b535cbd369" rel="nofollow noreferrer">https://www.blockchain.com/en/btc/tx/80975cddebaa93aa21a6477c0d050685d6820fa1068a2731db0f39b535cbd369</a></p>

<p>Note that on indexes 0, 1 and 2 we have the unlocking script in the scriptsig filed and on index 3 it is using witness program. No problems until now.</p>

<p>What is bugging me is why when I get the redeem script from indexes 0, 1 and 2 and execute the ripemd60, sha256, create checksum and encode to base58 it generates the Public address as we can see in the screen of blockchain.com. But when I do the very same thing using the redeem script which is in the witness section, it returns to me a completely different address, why is that happening?</p>

<p>Also, what is the scriptSig on the index 3? How it is generated?</p>

<p>Example:</p>

<p>Redeem Script of index 0:</p>

<pre><code>522102194e1b5671daff4edc82ce01589e7179a874f63d6e5157fa0def116acd2c3a522103a043861e123bc67ddcfcd887b167e7ff9d00702d1466524157cf3b28c7aca71b2102a49a62a9470a31ee51824f0ee859b0534a4f555c0e2d7a9d9915d6986bfc200453ae
</code></pre>

<p>The address generated from index 0 using my script:</p>

<pre><code>3JUJgXbB1WpDEJprE8wP8vEXtba36dAYbk
</code></pre>

<p>It is the same as the transaction.</p>

<p>Redeem Script of index 3 (Segwit):</p>

<pre><code>5221021e6617e06bb90f621c3800e8c37ab081a445ae5527f6c5f68a022e7133f9b5fe2103bea1a8ce6369435bb74ff1584a136a7efeebfe4bc320b4d59113c92acd869f38210280631b27700baf7d472483fadfe1c4a7340a458f28bf6bae5d3234312d684c6553ae
</code></pre>

<p>The address generated from index 3 using my script:</p>

<pre><code>36aKiVksQRLKwByBYVz3KwquFcvHZkwroP
</code></pre>

<p>Address from the transaction recovered from blockchain.com</p>

<pre><code>3CYkk3x1XUvdXCdHtRFdjMjp17PuJ8eR8z
</code></pre>

<bitcoin-core><wallet><segregated-witness><multi-signature><p2sh>
Segwit with Multisig
Allan Romanato
# Segwit with Multisig

(Asked by: username_0 on 2020-02-18)

I was analyzing a Bitcoin transaction that works both with Segwit and non-Segwit for multisig Scripts.
Here is the transaction: https://www.blockchain.com/en/btc/tx/80975cddebaa93aa21a6477c0d050685d6820fa1068a2731db0f39b535cbd369
Note that on indexes 0, 1 and 2 we have the unlocking script in the scriptsig filed and on index 3 it is using witness program. No problems until now.
What is bugging me is why when I get the redeem script from indexes 0, 1 and 2 and execute the ripemd60, sha256, create checksum and encode to base58 it generates the Public address as we can see in the screen of blockchain.com. But when I do the very same thing using the redeem script which is in the witness section, it returns to me a completely different address, why is that happening?
Also, what is the scriptSig on the index 3? How it is generated?
Example:
Redeem Script of index 0:
522102194e1b5671daff4edc82ce01589e7179a874f63d6e5157fa0def116acd2c3a522103a043861e123bc67ddcfcd887b167e7ff9d00702d1466524157cf3b28c7aca71b2102a49a62a9470a31ee51824f0ee859b0534a4f555c0e2d7a9d9915d6986bfc200453ae

The address generated from index 0 using my script:
3JUJgXbB1WpDEJprE8wP8vEXtba36dAYbk

It is the same as the transaction.
Redeem Script of index 3 (Segwit):
5221021e6617e06bb90f621c3800e8c37ab081a445ae5527f6c5f68a022e7133f9b5fe2103bea1a8ce6369435bb74ff1584a136a7efeebfe4bc320b4d59113c92acd869f38210280631b27700baf7d472483fadfe1c4a7340a458f28bf6bae5d3234312d684c6553ae

The address generated from index 3 using my script:
36aKiVksQRLKwByBYVz3KwquFcvHZkwroP

Address from the transaction recovered from blockchain.com
3CYkk3x1XUvdXCdHtRFdjMjp17PuJ8eR8z

**Tags:** bitcoin-core, wallet, segregated-witness, multi-signature, p2sh


---

## 1 answers

### Answer by username_1 on 2020-02-18. Score: 2 | Accepted answer

You are computing just the P2SH address for the segwit script. However it is not just P2SH, it is a Segwit script wrapped inside of a P2SH. You actually have to first take the witnessScript (the multisig script or segwit redeemScript) and make a P2WSH output script with it. Then this script becomes the redeemScript for the P2SH script.
So given
5221021e6617e06bb90f621c3800e8c37ab081a445ae5527f6c5f68a022e7133f9b5fe2103bea1a8ce6369435bb74ff1584a136a7efeebfe4bc320b4d59113c92acd869f38210280631b27700baf7d472483fadfe1c4a7340a458f28bf6bae5d3234312d684c6553ae

as the witnessScript, you produce
002044c55c1da36a576217259c3bc21b0c3943f7eb3ff4e3c381d9fd3502434b9e87

as the redeemScript. This is a P2WSH output script. This is then hashed to create the hash in the P2SH script:
a914771962306e72e479245d48e879dd2a1862225b4c87

which has an address of
3CYkk3x1XUvdXCdHtRFdjMjp17PuJ8eR8z

> **Comments:**
> **username_0**: Is there any BIP that defines it?  
> **username_1**: [BIP 141](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki).  
> **username_0**: So for us to generate the redeemScript from witnessScript we need to ripemd160 them?  
> **username_1**: No. You have to SHA256 the witnessScript. Then put that into a p2wsh script. Then hash160 that script for the p2sh address.  
> **username_0**: For native Segwit we don't need this right? Since we won't have the sigScript field anymore.  
> **username_1**: For native segwit you still need to hash the witnessScript. But there is no need to make another script and hash that. Native segwit also has a different address type.  

---

