I want to generate P2SH-P2WSH with script P2PKH.

My public key is    027712968e8f2f9dfa79abe10940a3d915a0c95a0b97750881e6201acf2b66da97

SHA256 and RIPEMD160 of Public key

 ---------- Witness Script --------- 

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

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

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

#C4A9149fbb6d60a6d26f67b9673334c3863cdec224169787 base58 cecksum

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

  • Are you trying to build a backward-compatible segwit P2WPKH address (P2SH-P2WPKH)? – Oscar Serna Mar 31 '20 at 17:12
  • 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 Mar 31 '20 at 17:37
  • @OscarSerna thanks to reply I try to add 22, it's my redeem script 2200200c3727796d979c4d9ac585736a33dd6a976a3f290e96a6cc6ddac993ee6b4c8b but I have the same problem – monkeyUser Apr 1 '20 at 15:46
  • are u still working on this? – Oscar Serna Aug 29 '20 at 4:48

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 to calculate it)

  • 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 Mar 30 '20 at 17:17

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()
  • Thanks @Hamza, I don't want use external library. I want to do all the hard work :P – monkeyUser Mar 31 '20 at 11:44
  • 1
    Ohh i see. Please share with us as well when you get it :) – Hamza Yasin Mar 31 '20 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.