<p>This is specified in BIP 13: <a href="https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki" rel="nofollow noreferrer">https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki</a></p>

<p>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.</p>

<p>The code you're quoting is for P2PKH addresses, not P2SH.</p>

CC BY-SA 4.0
2019-09-23T16:42:10.817Z
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
CC BY-SA 4.0
2019-09-23T16:38:24.793Z
2019-09-23T16:38:24.793Z
Pieter Wuille
<p>My study on the scripts goes next.</p>

<p>I'm creating a ScriptPubKey decompiler, now I have a problem with the P2SH,</p>

<p>So how is calculate the key p2sh?</p>

<p>I using this code but I not think is correct</p>

<pre><code> 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 &lt;&lt; "P2SH addresss is " &lt;&lt; address;
</code></pre>

<p>I have looked this code inside the class DestinationEncoder of the bitcoin core</p>

<pre><code> std::string operator()(const CKeyID&amp; id) const
    {
        std::vector&lt;unsigned char&gt; data = m_params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
        data.insert(data.end(), id.begin(), id.end());
        return EncodeBase58Check(data);
    }
</code></pre>

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

<bitcoin-core><script><bitcoin-core-development>
How to calculate the Public key P2SH from hexadecimal script?
vincenzopalazzo
# How to calculate the Public key P2SH from hexadecimal script?

(Asked by: username_0 on 2019-09-23)

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?

**Tags:** bitcoin-core, script, bitcoin-core-development


---

## 1 answers

### Answer by username_1 on 2019-09-23. Score: 1 | Accepted answer

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.

> **Comments:**
> **username_0**: Thanks, username_1, is my error copy and past, for the P2SH code the parameter of the  `m_params.Base58Prefix` is `CChainParams::SCRIPT_ADDRESS`, right?  

---

