My study on the scripts goes next.

I'm creating a ScriptPubKey decompiler, now I have a problem with the P2SH,

So how is calculate the key p2sh?

I using this code but I not think is correct

 string scriptHash = hex.substr(4, hex.length() - 6);

 Bytes bytes = hexBytes(scriptHash.c_str());

 char address[36];
 Base58Check::pubkeyHashToBase58Check(bytes.data(), 0x00, address);

 string stringAddr(address);

 cout << "P2SH addresss is " << address;

I have looked this code inside the class DestinationEncoder of the bitcoin core

 std::string operator()(const CKeyID& id) const
        std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
        data.insert(data.end(), id.begin(), id.end());
        return EncodeBase58Check(data);

and with this code it seems that the information is queued to something, can you clarify my ideas?

1 Answer 1


This is specified in BIP 13: https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki

You compute the Base58Check encoding of the P2SH version byte (5 for mainnet, 196 for testnet) followed by the 20-byte Hash160 of the script.

The code you're quoting is for P2PKH addresses, not P2SH.

  • Thanks, Pieter, is my error copy and past, for the P2SH code the parameter of the m_params.Base58Prefix is CChainParams::SCRIPT_ADDRESS, right? Sep 23, 2019 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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