<p>To compute the 1 of 3 multisig address for the example above, one grabs the 3 public keys from the <em>asm</em> field and simply performs this bx operation:</p>

<p><code>% bx script-to-address -v 5 "1 [ 04398184a2cef0d7b73ed7a3a1d4ad16296c3c6986bed0bd72775060aae9891979eaea1efb28d7eb1da3304ec38a98b42086e3be2ceba82b0e932128ec422a6fc2 ] [ 0250504b2d4245544120506565722d506565722d6e6574776f726b206265746121 ] [ 02553432353135362e313232342020202020202020202020202020202020202020 ] 3 checkmultisig"</code></p>

<p><strong>3KQYMMqMBTv8254UqwmaLzW5NDT879KzK8</strong> </p>

<p>Here is where bitcoin-explorer (bx) wiki page details for <a href="https://github.com/libbitcoin/libbitcoin-explorer/wiki/bx-script-to-address" rel="nofollow">script-to-address</a> can be found.</p>

<p>Be sure the public keys are in the same order; otherwise, multisig addresses computed will be different. (May be why current multisig transactions need to be signed in the proper order.) Also, notice the 1st public key is uncompresses (is longer) while the other 2 are compressed (shorter).</p>

CC BY-SA 3.0
2016-09-08T22:01:20.540Z
2016-09-08T22:12:56.023Z
2016-09-08T22:12:56.023Z
skaht
<p>Here is the example
of the <a href="https://blockchain.info/tx/bea1e7ae7ecdc502215717f30d62a12085bb2866d8db730dfb626b60c337534c" rel="nofollow">transaction</a> from the question</p>

<p>the redeem script is: <code>514104398184a2cef0d7b73ed7a3a1d4ad16296c3c6986bed0bd72775060aae9891979eaea1efb28d7eb1da3304ec38a98b42086e3be2ceba82b0e932128ec422a6fc2210250504b2d4245544120506565722d506565722d6e6574776f726b2062657461212102553432353135362e31323234202020202020202020202020202020202020202053ae</code></p>

<p>to derive the P2SH address we do the following:<br>
<strong>sha256(redeemScript)</strong><br>
using PHP:</p>

<pre><code>$sha256= hash('sha256',hexStringToByteString($redeemScript));
SHA256: 8b1c6ece53385d6ae3cf3c40fc6239389a7006d9f46047d5d67c3fa78dd2afcc
</code></pre>

<p><strong>ripemd160 the resulting hash</strong></p>

<pre><code>$ripemd160=hash('ripemd160',hexStringToByteString($sha256));
RIPEMD160: c25497b8a9bcc393f47abd19a435d1af440916ac
</code></pre>

<p><strong>prepend "05" and hash twice</strong></p>

<pre><code>$hashTwice = hash('sha256',hexStringToByteString (hash('sha256',hexStringToByteString ("05" .  $ripemd160))));
double hash: 479ce4efc25c4195ef3cc015d74c45034a2c72f21e322ba30f5c26d872e58b38
</code></pre>

<p><strong>first 4 bytes is the chscksum</strong></p>

<pre><code>$checksum=substr($hashTwice,0,8);
checksum: 479ce4ef
</code></pre>

<p><strong>compose the address as:</strong></p>

<blockquote>
  <p>base58-encode: [one-byte version][20-byte hash][4-byte checksum]</p>
</blockquote>

<pre><code>$addr= "05" . $ripemd160 . $checksum;
address: 05c25497b8a9bcc393f47abd19a435d1af440916ac479ce4ef
</code></pre>

<p><strong>base58 encode</strong></p>

<pre><code>$p2sh=base58_encode($to_encode);
base58: 3KQYMMqMBTv8254UqwmaLzW5NDT879KzK8
</code></pre>

<p>and we get the correct P2SH address: <strong>3KQYMMqMBTv8254UqwmaLzW5NDT879KzK8</strong></p>

CC BY-SA 3.0
2016-09-06T23:26:35.703Z
2016-09-06T23:26:35.703Z
Albert S
<p>What is the process of decoding the P2SH address?<br>
We have <a href="https://bitcoin.stackexchange.com/questions/48364/how-is-the-p2sh-address-obtained-in-decodescript">this</a> information so far. </p>

<p>Let's run through an example tx <a href="https://blockchain.info/tx/bea1e7ae7ecdc502215717f30d62a12085bb2866d8db730dfb626b60c337534c" rel="nofollow noreferrer">bea1e7ae7ecdc502215717f30d62a12085bb2866d8db730dfb626b60c337534c</a></p>

<p>The hex script (for the first output) is: <code>514104398184a2cef0d7b73ed7a3a1d4ad16296c3c6986bed0bd72775060aae9891979eaea1efb28d7eb1da3304ec38a98b42086e3be2ceba82b0e932128ec422a6fc2210250504b2d4245544120506565722d506565722d6e6574776f726b2062657461212102553432353135362e31323234202020202020202020202020202020202020202053ae</code></p>

<p>script decode gives:   </p>

<pre><code>{
    "asm" : "1 04398184a2cef0d7b73ed7a3a1d4ad16296c3c6986bed0bd72775060aae9891979eaea1efb28d7eb1da3304ec38a98b42086e3be2ceba82b0e932128ec422a6fc2 0250504b2d4245544120506565722d506565722d6e6574776f726b206265746121 02553432353135362e313232342020202020202020202020202020202020202020 3 OP_CHECKMULTISIG",
    "reqSigs" : 1,
    "type" : "multisig",
    "addresses" : [
        "1TestLVFK8DsRYFQkDDwvfsPHqUqWPBe7",
        "1H16KgZg3wHgApvHvZkSocxN6ibzNT9Cc7",
        "1N31mRc4tiGumXdRmb1Bzk9BoG4Bc7Ctbi"
    ],
    "p2sh" : "3KQYMMqMBTv8254UqwmaLzW5NDT879KzK8"
}
</code></pre>

<p>Can we break this script down?</p>

CC BY-SA 3.0
2016-09-03T01:01:15.157Z
I don't believe you can "decode" a script hash. You just have to provide a matching script which when hashed equals the hash provided.
Snowman
CC BY-SA 3.0
2016-09-03T02:32:35.713Z
can we derive the p2sh address from the script? I can do it using pybitcointools, but i want to know the steps involved
Albert S
CC BY-SA 3.0
2016-09-03T02:40:44.457Z
p2sh address = Base58Check(SHA256(RIPEMD160(script)))
Snowman
<multi-signature><p2sh>
P2SH address decoding
Albert S
# P2SH address decoding

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

What is the process of decoding the P2SH address?  
We have [this](https://bitcoin.stackexchange.com/questions/48364/how-is-the-p2sh-address-obtained-in-decodescript) information so far.

Let's run through an example tx [bea1e7ae7ecdc502215717f30d62a12085bb2866d8db730dfb626b60c337534c](https://blockchain.info/tx/bea1e7ae7ecdc502215717f30d62a12085bb2866d8db730dfb626b60c337534c)

The hex script (for the first output) is: `514104398184a2cef0d7b73ed7a3a1d4ad16296c3c6986bed0bd72775060aae9891979eaea1efb28d7eb1da3304ec38a98b42086e3be2ceba82b0e932128ec422a6fc2210250504b2d4245544120506565722d506565722d6e6574776f726b2062657461212102553432353135362e31323234202020202020202020202020202020202020202053ae`

script decode gives:

```
{
    "asm" : "1 04398184a2cef0d7b73ed7a3a1d4ad16296c3c6986bed0bd72775060aae9891979eaea1efb28d7eb1da3304ec38a98b42086e3be2ceba82b0e932128ec422a6fc2 0250504b2d4245544120506565722d506565722d6e6574776f726b206265746121 02553432353135362e313232342020202020202020202020202020202020202020 3 OP_CHECKMULTISIG",
    "reqSigs" : 1,
    "type" : "multisig",
    "addresses" : [
        "1TestLVFK8DsRYFQkDDwvfsPHqUqWPBe7",
        "1H16KgZg3wHgApvHvZkSocxN6ibzNT9Cc7",
        "1N31mRc4tiGumXdRmb1Bzk9BoG4Bc7Ctbi"
    ],
    "p2sh" : "3KQYMMqMBTv8254UqwmaLzW5NDT879KzK8"
}

```

Can we break this script down?

**Tags:** multi-signature, p2sh

> **Comments:**
> **username_1**: I don't believe you can "decode" a script hash. You just have to provide a matching script which when hashed equals the hash provided.  
> **username_0**: can we derive the p2sh address from the script? I can do it using pybitcointools, but i want to know the steps involved  
> **username_1**: p2sh address = Base58Check(SHA256(RIPEMD160(script)))  

---

## 2 answers

### Answer by username_0 on 2016-09-06. Score: 4 | Accepted answer

Here is the example
of the [transaction](https://blockchain.info/tx/bea1e7ae7ecdc502215717f30d62a12085bb2866d8db730dfb626b60c337534c) from the question

the redeem script is: `514104398184a2cef0d7b73ed7a3a1d4ad16296c3c6986bed0bd72775060aae9891979eaea1efb28d7eb1da3304ec38a98b42086e3be2ceba82b0e932128ec422a6fc2210250504b2d4245544120506565722d506565722d6e6574776f726b2062657461212102553432353135362e31323234202020202020202020202020202020202020202053ae`

to derive the P2SH address we do the following:  
**sha256(redeemScript)**  
using PHP:

```
$sha256= hash('sha256',hexStringToByteString($redeemScript));
SHA256: 8b1c6ece53385d6ae3cf3c40fc6239389a7006d9f46047d5d67c3fa78dd2afcc

```

**ripemd160 the resulting hash**

```
$ripemd160=hash('ripemd160',hexStringToByteString($sha256));
RIPEMD160: c25497b8a9bcc393f47abd19a435d1af440916ac

```

**prepend "05" and hash twice**

```
$hashTwice = hash('sha256',hexStringToByteString (hash('sha256',hexStringToByteString ("05" .  $ripemd160))));
double hash: 479ce4efc25c4195ef3cc015d74c45034a2c72f21e322ba30f5c26d872e58b38

```

**first 4 bytes is the chscksum**

```
$checksum=substr($hashTwice,0,8);
checksum: 479ce4ef

```

**compose the address as:**

> base58-encode: [one-byte version][20-byte hash][4-byte checksum]

```
$addr= "05" . $ripemd160 . $checksum;
address: 05c25497b8a9bcc393f47abd19a435d1af440916ac479ce4ef

```

**base58 encode**

```
$p2sh=base58_encode($to_encode);
base58: 3KQYMMqMBTv8254UqwmaLzW5NDT879KzK8

```

and we get the correct P2SH address: **3KQYMMqMBTv8254UqwmaLzW5NDT879KzK8**


---

### Answer by username_2 on 2016-09-08. Score: 3

To compute the 1 of 3 multisig address for the example above, one grabs the 3 public keys from the *asm* field and simply performs this bx operation:

`% bx script-to-address -v 5 "1 [ 04398184a2cef0d7b73ed7a3a1d4ad16296c3c6986bed0bd72775060aae9891979eaea1efb28d7eb1da3304ec38a98b42086e3be2ceba82b0e932128ec422a6fc2 ] [ 0250504b2d4245544120506565722d506565722d6e6574776f726b206265746121 ] [ 02553432353135362e313232342020202020202020202020202020202020202020 ] 3 checkmultisig"`

**3KQYMMqMBTv8254UqwmaLzW5NDT879KzK8**

Here is where bitcoin-explorer (bx) wiki page details for [script-to-address](https://github.com/libbitcoin/libbitcoin-explorer/wiki/bx-script-to-address) can be found.

Be sure the public keys are in the same order; otherwise, multisig addresses computed will be different. (May be why current multisig transactions need to be signed in the proper order.) Also, notice the 1st public key is uncompresses (is longer) while the other 2 are compressed (shorter).


---

