Segwit with Multisig 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
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