<p>At the time this question was asked, this issue was the result of a bug in Bitcoin Core. It was fixed in <a href="https://github.com/bitcoin/bitcoin/pull/18484" rel="nofollow noreferrer">PR #18484</a> and is available in Bitcoin Core 0.20+.</p>

CC BY-SA 4.0
2021-12-18T05:26:47.803Z
2021-12-18T05:26:47.803Z
Ava Chow
<p>Hashing <code>Redeeem script</code> to use in <code>SCRIPTPUBKEY</code> uses the wrong hash algorithm.</p>

<p><code>RIPEMD160(Redscript) = f23bde7eee22aabca28164cb4b8a977834e5441b</code> but we want  <code>HASH160</code> which is </p>

<p><code>RIPEMD160(SHA256(Redscript)) = c39f7f611a844bd7118dac9166fa9c1398676c30</code></p>

<p>(I used this <a href="https://bitcoin.stackexchange.com/a/88357">script</a> to calculate it)</p>

CC BY-SA 4.0
2020-03-30T17:17:59.637Z
hey thanks :), I updated my question with you suggest. but I have the same problem. the values are differents because I have a script to generate my address
monkeyUser
CC BY-SA 4.0
2020-03-30T16:45:37.170Z
2020-03-30T16:45:37.170Z
MCCCS
<p>You can use bitcoinjs Lib.    </p>

<pre><code>function generateAddress('P2WSH', publicKeyHash, network)   {  

    return redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(publicKeyHash);
                let redeemScriptHash = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript));
                address = bitcoin.address.fromOutputScript(redeemScriptHash, network);
    }
        // create new random key pair of eliptic curves
            let keyPair = bitcoin.ECPair.makeRandom({network: network});

            // public key buffer
            let publicKeyBuffer = keyPair.getPublicKeyBuffer();

            // public key hash
            let publicKeyHash = bitcoin.crypto.hash160(publicKeyBuffer);

            // generate Address
            let address = generateAddress('P2WSH', publicKeyHash, network);

            // return wallet
            let wallet = {
              "address": address,
              "privateKey": keyPair.toWIF()
            };
</code></pre>

CC BY-SA 4.0
2020-03-31T11:44:38.217Z
Thanks @Hamza, I don't want use external library. I want to do all the hard work :P
monkeyUser
CC BY-SA 4.0
2020-03-31T19:22:58.327Z
Ohh i see. Please share with us as well when you get it :)
Hamza Yasin
CC BY-SA 4.0
2020-03-31T11:11:20.707Z
2020-03-31T11:11:20.707Z
Hamza Yasin
<p>I want to generate P2SH-P2WSH with script P2PKH.</p>

<pre><code>My public key is    027712968e8f2f9dfa79abe10940a3d915a0c95a0b97750881e6201acf2b66da97
</code></pre>

<p>SHA256 and RIPEMD160 of Public key</p>

<pre><code> ---------- Witness Script --------- 
#OP_DUP OP_HASH160 &lt;PubKHash&gt; OP_EQUALVERIFY OP_CHECKSIG
76a914651554088a30217e6d0261cce1634e9d90b03f7088AC

 ---------- scripthash --------- 
#SHA 256 of Witness script    
1eb6c6c3ad4e395e66e3521c23486863fe363939db88c38a3c6461cb66f80254

  ---------- Redeeem script ---------     
#add OP_0 (0x0020{32-byte scripthash})
00201eb6c6c3ad4e395e66e3521c23486863fe363939db88c38a3c6461cb66f80254

  ---------- SCRIPTPUBKEY --------- 
#RIPEMD160(redeem script)
#OP_HASH160 hash160(redeemScript) OP_EQUAL
A9149fbb6d60a6d26f67b9673334c3863cdec224169787

ADDRESS:
#C4A9149fbb6d60a6d26f67b9673334c3863cdec224169787 base58 cecksum
2N7ooxTaU8FRgfTrhYn3FjwUcFqeNRvRU5k
</code></pre>

<p>Now I want to try to spend form this address.</p>

<pre><code>bitcoin-cli signrawtransactionwithkey $TX_DATA '["'$PK'"]' '[{"txid":"'$TXID'","vout":'$VOUT',"redeemScript":"'$REDEEMSCRIPT'","scriptPubKey":"'$SCRIPTPUBKEY'","witnessScript":"'$WITNESSCRIPT'","amount":"'$TOTAL_UTXO_AMOUNT'"}]'
</code></pre>

<p>I receive this error</p>

<blockquote>
  <p>redeemScript does not correspond to witnessScript</p>
</blockquote>

CC BY-SA 4.0
2020-03-31T17:12:47.350Z
Are you trying to build a backward-compatible segwit P2WPKH address (P2SH-P2WPKH)?
Oscar Serna
CC BY-SA 4.0
2020-03-31T17:37:44.820Z
I think you have an extra "00" in your redeem script before the {32-byte scripthash}. The serialized version of the redeem script must be: (0x220020{32-byte-hash}). The firs "22" corresponds to the 34 (in hex) of the length of the following script. The next "00" corresponds to the OP_0. The next "20" represents the length of the hash (32 bytes in hex). So the "00" represents the OP_0, but you are repeating this item in your script.
Oscar Serna
CC BY-SA 4.0
2020-04-01T15:46:39.497Z
@OscarSerna thanks to reply I try to add 22, it's my redeem script `2200200c3727796d979c4d9ac585736a33dd6a976a3f290e96a6cc6ddac993ee6b4c8b` but I have the same problem
monkeyUser
CC BY-SA 4.0
2020-08-29T04:48:19.523Z
are u still working on this?
Oscar Serna
<transactions><segregated-witness><witness-data><p2wsh>
Generate P2SH-P2WSH
monkeyUser
# Generate P2SH-P2WSH

(Asked by: username_0 on 2020-03-30)

I want to generate P2SH-P2WSH with script P2PKH.

```
My public key is    027712968e8f2f9dfa79abe10940a3d915a0c95a0b97750881e6201acf2b66da97

```

SHA256 and RIPEMD160 of Public key

```
 ---------- Witness Script --------- 
#OP_DUP OP_HASH160 <PubKHash> OP_EQUALVERIFY OP_CHECKSIG
76a914651554088a30217e6d0261cce1634e9d90b03f7088AC

 ---------- scripthash --------- 
#SHA 256 of Witness script    
1eb6c6c3ad4e395e66e3521c23486863fe363939db88c38a3c6461cb66f80254

  ---------- Redeeem script ---------     
#add OP_0 (0x0020{32-byte scripthash})
00201eb6c6c3ad4e395e66e3521c23486863fe363939db88c38a3c6461cb66f80254

  ---------- SCRIPTPUBKEY --------- 
#RIPEMD160(redeem script)
#OP_HASH160 hash160(redeemScript) OP_EQUAL
A9149fbb6d60a6d26f67b9673334c3863cdec224169787

ADDRESS:
#C4A9149fbb6d60a6d26f67b9673334c3863cdec224169787 base58 cecksum
2N7ooxTaU8FRgfTrhYn3FjwUcFqeNRvRU5k

```

Now I want to try to spend form this address.

```
bitcoin-cli signrawtransactionwithkey $TX_DATA '["'$PK'"]' '[{"txid":"'$TXID'","vout":'$VOUT',"redeemScript":"'$REDEEMSCRIPT'","scriptPubKey":"'$SCRIPTPUBKEY'","witnessScript":"'$WITNESSCRIPT'","amount":"'$TOTAL_UTXO_AMOUNT'"}]'

```

I receive this error

> redeemScript does not correspond to witnessScript

**Tags:** transactions, segregated-witness, witness-data, p2wsh

> **Comments:**
> **username_1**: Are you trying to build a backward-compatible segwit P2WPKH address (P2SH-P2WPKH)?  
> **username_1**: I think you have an extra "00" in your redeem script before the {32-byte scripthash}. The serialized version of the redeem script must be: (0x220020{32-byte-hash}). The firs "22" corresponds to the 34 (in hex) of the length of the following script. The next "00" corresponds to the OP_0. The next "20" represents the length of the hash (32 bytes in hex). So the "00" represents the OP_0, but you are repeating this item in your script.  
> **username_0**: @username_1 thanks to reply I try to add 22, it's my redeem script `2200200c3727796d979c4d9ac585736a33dd6a976a3f290e96a6cc6ddac993ee6b4c8b` but I have the same problem  
> **username_1**: are u still working on this?  

---

## 3 answers

### Answer by username_2 on 2021-12-18. Score: 1

At the time this question was asked, this issue was the result of a bug in Bitcoin Core. It was fixed in [PR #18484](https://github.com/bitcoin/bitcoin/pull/18484) and is available in Bitcoin Core 0.20+.


---

### Answer by username_3 on 2020-03-30. Score: 0

Hashing `Redeeem script` to use in `SCRIPTPUBKEY` uses the wrong hash algorithm.

`RIPEMD160(Redscript) = f23bde7eee22aabca28164cb4b8a977834e5441b` but we want `HASH160` which is

`RIPEMD160(SHA256(Redscript)) = c39f7f611a844bd7118dac9166fa9c1398676c30`

(I used this [script](https://bitcoin.stackexchange.com/a/88357) to calculate it)

> **Comments:**
> **username_0**: hey thanks :), I updated my question with you suggest. but I have the same problem. the values are differents because I have a script to generate my address  

---

### Answer by username_4 on 2020-03-31. Score: 0

You can use bitcoinjs Lib.

```
function generateAddress('P2WSH', publicKeyHash, network)   {  

    return redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(publicKeyHash);
                let redeemScriptHash = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript));
                address = bitcoin.address.fromOutputScript(redeemScriptHash, network);
    }
        // create new random key pair of eliptic curves
            let keyPair = bitcoin.ECPair.makeRandom({network: network});

            // public key buffer
            let publicKeyBuffer = keyPair.getPublicKeyBuffer();

            // public key hash
            let publicKeyHash = bitcoin.crypto.hash160(publicKeyBuffer);

            // generate Address
            let address = generateAddress('P2WSH', publicKeyHash, network);

            // return wallet
            let wallet = {
              "address": address,
              "privateKey": keyPair.toWIF()
            };

```

> **Comments:**
> **username_0**: Thanks @username_4, I don't want use external library. I want to do all the hard work :P  
> **username_4**: Ohh i see. Please share with us as well when you get it :)  

---

