<p>So, a P2SH scriptPubKey looks like this:</p>

<pre><code>OP_HASH160 &lt;20 byte script hash&gt; OP_EQUAL
</code></pre>

<p>Serialized, in hex, that looks like:</p>

<pre><code>17a9140000000000000000000000000000000000000087

^ ScriptPubkey len 
  ^ OP_HASH160
    ^ Data element len
      ^ Data element
                                            ^ OP_EQUAL
</code></pre>

<p>where the zeros can be any byte.</p>

<p>How does that transform into an address starting with 3? You transform it the <a href="https://bitcoin.org/en/developer-reference#address-conversion" rel="nofollow">same way you would a normal Bitcoin address</a>, but with a different version byte. Instead of <code>00</code>, you would use <code>05</code>. </p>

<p>See also the <a href="https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki" rel="nofollow">P2SH address</a> specification.</p>

CC BY-SA 3.0
2016-09-02T17:39:53.217Z
please see follow-up [here](http://bitcoin.stackexchange.com/questions/48375/p2sh-address-decoding)
Albert S
CC BY-SA 3.0
2016-09-03T02:33:59.343Z
do you transform just the data element?
Albert S
CC BY-SA 3.0
2016-09-03T04:41:23.927Z
@Alberts Yes, you transform just the data element. However, the follow up question *doesn't* contain any P2SH outputs. It has a multisig output that could be stated as a P2SH output, but no P2SH outputs.
Nick ODell
CC BY-SA 3.0
2016-09-06T23:31:34.350Z
I think you are looking at the address that signed for the transaction, When i follow your instructions i get the address that signed for this output. I was interested in the P2SH address (3...), how to extract it from a redeem script. I did figure that out and have posted the answer in the follow up question.
Albert S
CC BY-SA 3.0
2016-09-06T23:39:36.407Z
@Alberts Your extraction is correct for bare multisig outputs. However, people also use P2SH, and your code will not  work correctly for that. What's the difference? P2SH has an output script like `OP_HASH160 <hash> OP_EQUAL` and bare multisig looks like `<something> OP_CHECKMULTISIGVERIFY`
Nick ODell
CC BY-SA 3.0
2016-09-06T23:40:46.997Z
@Alberts To get a sense of what I'm talking about, try your code on this transaction: https://blockchain.info/tx/721df255f1f2b58e30bf1f4859664189202ef58b60c269e385651eb2ac7a781d
Nick ODell
CC BY-SA 3.0
2016-09-02T02:52:22.303Z
2016-09-03T04:47:51.283Z
2016-09-03T04:47:51.283Z
Nick ODell
<p>Im trying to understand how to obtain the p2SH address when decoding an output script.</p>

<pre><code>r.push_back(Pair("p2sh", CBitcoinAddress(CScriptID(script)).ToString()));
</code></pre>

<p><a href="https://github.com/bitcoin/bitcoin/blob/0c8875d23b37bca9e37e0c387ab5365d4e8758c5/src/rpc/rawtransaction.cpp#L557" rel="nofollow">source</a></p>

<p>As far as I understand CScriptID is the hash160 of the script.  </p>

<multi-signature><p2sh>
How is the P2SH address obtained in decodescript
Albert S
# How is the P2SH address obtained in decodescript

(Asked by: username_0 on 2016-09-01)

Im trying to understand how to obtain the p2SH address when decoding an output script.
r.push_back(Pair("p2sh", CBitcoinAddress(CScriptID(script)).ToString()));

source
As far as I understand CScriptID is the hash160 of the script.

**Tags:** multi-signature, p2sh


---

## 1 answers

### Answer by username_1 on 2016-09-02. Score: 2

So, a P2SH scriptPubKey looks like this:
OP_HASH160 <20 byte script hash> OP_EQUAL

Serialized, in hex, that looks like:
17a9140000000000000000000000000000000000000087

^ ScriptPubkey len 
  ^ OP_HASH160
    ^ Data element len
      ^ Data element
                                            ^ OP_EQUAL

where the zeros can be any byte.
How does that transform into an address starting with 3? You transform it the same way you would a normal Bitcoin address, but with a different version byte. Instead of 00, you would use 05. 
See also the P2SH address specification.

> **Comments:**
> **username_0**: please see follow-up [here](http://bitcoin.stackexchange.com/questions/48375/p2sh-address-decoding)  
> **username_0**: do you transform just the data element?  
> **username_1**: @Alberts Yes, you transform just the data element. However, the follow up question *doesn't* contain any P2SH outputs. It has a multisig output that could be stated as a P2SH output, but no P2SH outputs.  
> **username_0**: I think you are looking at the address that signed for the transaction, When i follow your instructions i get the address that signed for this output. I was interested in the P2SH address (3...), how to extract it from a redeem script. I did figure that out and have posted the answer in the follow up question.  
> **username_1**: @Alberts Your extraction is correct for bare multisig outputs. However, people also use P2SH, and your code will not  work correctly for that. What's the difference? P2SH has an output script like `OP_HASH160 <hash> OP_EQUAL` and bare multisig looks like `<something> OP_CHECKMULTISIGVERIFY`  
> **username_1**: @Alberts To get a sense of what I'm talking about, try your code on this transaction: https://blockchain.info/tx/721df255f1f2b58e30bf1f4859664189202ef58b60c269e385651eb2ac7a781d  

---

