How to calculate the Public key P2SH from hexadecimal script?

0

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?

vincenzopalazzo

Posted 2019-09-23T16:18:13.023

Reputation: 572

Answers

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.

Pieter Wuille

Posted 2019-09-23T16:18:13.023

Reputation: 54 032

Thanks, Pieter, is my error copy and past, for the P2SH code the parameter of the m_params.Base58Prefix is CChainParams::SCRIPT_ADDRESS, right?vincenzopalazzo 2019-09-23T16:42:10.817