Im trying to understand how to obtain the p2SH address when decoding an output script.

r.push_back(Pair("p2sh", CBitcoinAddress(CScriptID(script)).ToString()));


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


So, a P2SH scriptPubKey looks like this:

OP_HASH160 <20 byte script hash> OP_EQUAL

Serialized, in hex, that looks like:


^ 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.

  • please see follow-up here – Albert s Sep 2 '16 at 17:39
  • do you transform just the data element? – Albert s Sep 3 '16 at 2:33
  • @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 Sep 3 '16 at 4:41
  • 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 Sep 6 '16 at 23:31
  • @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 Sep 6 '16 at 23:39

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.