<p>This is indeed an error in the book: <a href="https://github.com/bitcoinbook/bitcoinbook/issues/440" rel="nofollow noreferrer">https://github.com/bitcoinbook/bitcoinbook/issues/440</a></p>

<p>See <a href="https://github.com/bitcoinbook/bitcoinbook/commit/1ddd89b65841dea02b5c4ab1e31cf1f4de23ba52" rel="nofollow noreferrer">this diff</a> for the correct steps.</p>

CC BY-SA 4.0
2018-08-05T17:25:35.447Z
2018-08-05T17:25:35.447Z
Jonathan Cross
<p>Seems to me that </p>

<blockquote>
  <p>... creating a Pay-to-Witness-Script-Hash (P2WSH) output that would
  <strong>look like this</strong>:
  0 9592d601848d04b172905e0ddb0adde59f1590f1e553ffc81ddc4b0ed927dd73...</p>
</blockquote>

<p>means that this hash is an example not derived from the data above</p>

CC BY-SA 3.0
2017-10-18T11:34:31.730Z
Ok cool, am I doing the rest of the steps right then? / Are there other test vectors I can check against? Would the address then be `Base58(05 + HASH160(OP_0 + OP_32 + {sha256 standard p2sh scrit}))`?
John Dorian
CC BY-SA 3.0
2017-10-18T12:45:43.190Z
I think you can 'extract' the 'test vectors' from current bitcoin/testnet blockchain rather than checking bogus sources from a book :)
amaclin
CC BY-SA 3.0
2017-10-19T06:59:10.753Z
Ok. So is the process correct?
John Dorian
CC BY-SA 3.0
2017-10-19T07:17:13.243Z
I did not received `9592d6018...` with provided data either, but I did not spend much time for this question. I can say, that there are a lot of such transactions in blockchain. for example you can take testnet pair https://testnet.smartbit.com.au/tx/c9348040bbc2024e12dcb4a0b4806b0398646b91acf314da028c3f03dd0179fc and https://testnet.smartbit.com.au/tx/31bc1c88ce6ae337a6b3057a16d5bad0b561ad1dfc047d0a7fbb8814668f91e5 and try to get `1e8dda3...` from `5221038e81669c...`
amaclin
CC BY-SA 3.0
2017-10-18T09:32:59.137Z
2017-10-18T09:32:59.137Z
amaclin
<p>I'm trying to generate a multisignature, segwit compatible address as a <a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#P2WSH_nested_in_BIP16_P2SH" rel="nofollow noreferrer">P2WSH nested in BIP16 P2SH</a> as described in BIP141. Basically, I want to construct a P2WSH-nested-in-P2SH address from n public keys and the m required to sign a valid tx. </p>

<pre><code>(m required, n public keys) -&gt; base 58 bitcoin address
</code></pre>

<p>I'm trying to follow along with <a href="https://github.com/bitcoinbook/bitcoinbook/blob/8d01749bcf45f69f36cf23606bbbf3f0bd540db3/appdx-segwit.asciidoc#pay-to-witness-script-hash-inside-pay-to-script-hash" rel="nofollow noreferrer">the examples provided in Mastering Bitcoin</a> to check the numbers I'm getting, but I'm definitely missing something. (I'm going to link all of the sections I'm referencing but it shouldn't be necessary to actually go and read them—hopefully the flaw in my process will be evident to someone.) </p>

<ol>
<li><p><a href="https://github.com/bitcoinbook/bitcoinbook/blob/8d01749bcf45f69f36cf23606bbbf3f0bd540db3/ch07.asciidoc#pay-to-script-hash-p2sh" rel="nofollow noreferrer">He defines</a> a standard P2SH 2-of-5 multisig: <code>OP_2 key0 key1 key2 key3 key4 OP_5 OP_CHECKMULTISIG</code> where the keys are</p>

<pre><code>04C16B8698A9ABF84250A7C3EA7EEDEF9897D1C8C6ADF47F06CF73370D74DCCA01CDCA79DCC5C395D7EEC6984D83F1F50C900A24DD47F569FD4193AF5DE762C587
04A2192968D8655D6A935BEAF2CA23E3FB87A3495E7AF308EDF08DAC3C1FCBFC2C75B4B0F4D0B1B70CD2423657738C0C2B1D5CE65C97D78D0E34224858008E8B49
047E63248B75DB7379BE9CDA8CE5751D16485F431E46117B9D0C1837C9D5737812F393DA7D4420D7E1A9162F0279CFC10F1E8E8F3020DECDBC3C0DD389D9977965
0421D65CBD7149B255382ED7F78E946580657EE6FDA162A187543A9D85BAAA93A4AB3A8F044DADA618D087227440645ABE8A35DA8C5B73997AD343BE5C2AFD94A5
043752580AFA1ECED3C68D446BCAB69AC0BA7DF50D56231BE0AABF1FDEEC78A6A45E394BA29A1EDF518C022DD618DA774D207D137AAB59E0B000EB7ED238F4D800
</code></pre></li>
</ol>

<p>of which the <code>HASH160</code> is <code>54c557e07dde5bb6cb791c7a540e0a4796f5e97e</code>. Ok so far so good; I got the same value as the book.</p>

<ol start="2">
<li><p>Then <a href="https://github.com/bitcoinbook/bitcoinbook/blob/8d01749bcf45f69f36cf23606bbbf3f0bd540db3/appdx-segwit.asciidoc#pay-to-witness-script-hash-p2wsh" rel="nofollow noreferrer">he discusses a P2WSH transaction</a> and references the same multisig script</p>

<blockquote>
  <p>We saw this type of script in [p2sh]. In that example, P2SH was used
  by Mohammed’s company to express a multisignature script. Payments to
  Mohammed’s company were encoded with a locking script like this:
  Example P2SH output script</p>

<pre><code>HASH160 54c557e07dde5bb6cb791c7a540e0a4796f5e97e EQUAL
</code></pre>
</blockquote>

<p>and then defines the P2WSH output script</p>

<blockquote>
  <p>Example P2WSH output script</p>

<pre><code>0 9592d601848d04b172905e0ddb0adde59f1590f1e553ffc81ddc4b0ed927dd73
</code></pre>
  
  <p>[...] the Segregated Witness program consists of two values pushed to the stack: a witness version (0) and the 32-byte SHA256 hash of the redeem script.</p>
</blockquote></li>
</ol>

<p>How does he get this P2WSH output script?</p>

<p>If I take the example P2SH script, <code>script</code>.</p>

<pre><code>HASH160(script) = 54c557e07dde5bb6cb791c7a540e0a4796f5e97e
SHA256(script) = a9b7b38d972cabc7961dbfbcb841ad4508d133c47ba87457b4a0e8aae86dbb89
SHA256(HASH160(script)) = 9796557ff06241ca75fdbc09359e9916186cb8398dc01fd58afc72a3ac86ce63
</code></pre>

<p>None of which equals the provided:</p>

<pre><code>9592d601848d04b172905e0ddb0adde59f1590f1e553ffc81ddc4b0ed927dd73
</code></pre>

<p>I also tried compressing the public keys, and still couldn't get that value. I'm pretty sure it's still talking about the same example, because it continues with</p>

<blockquote>
  <p>Mohammed’s company can spend outputs the P2WSH output [...]</p>
</blockquote>

<p>If I can figure out the 32-byte SHA256 hash of the redeem script I think I'm good to go, because at that point the address can be calculated</p>

<pre><code>sha256_value = 9592d601848d04b172905e0ddb0adde59f1590f1e553ffc81ddc4b0ed927dd73
p2wsh_output = OP_0 + OP_32 + sha256_value
address = Base58(05 + HASH160(p2wsh_output))
</code></pre>

<segregated-witness><p2sh><mastering-bitcoin><p2wsh>
Create Pay to Witness Script Hash in Pay to Script Hash Address
John Dorian
# Create Pay to Witness Script Hash in Pay to Script Hash Address

(Asked by: username_0 on 2017-10-18)

I'm trying to generate a multisignature, segwit compatible address as a [P2WSH nested in BIP16 P2SH](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#P2WSH_nested_in_BIP16_P2SH) as described in BIP141. Basically, I want to construct a P2WSH-nested-in-P2SH address from n public keys and the m required to sign a valid tx.

```
(m required, n public keys) -> base 58 bitcoin address

```

I'm trying to follow along with [the examples provided in Mastering Bitcoin](https://github.com/bitcoinbook/bitcoinbook/blob/8d01749bcf45f69f36cf23606bbbf3f0bd540db3/appdx-segwit.asciidoc#pay-to-witness-script-hash-inside-pay-to-script-hash) to check the numbers I'm getting, but I'm definitely missing something. (I'm going to link all of the sections I'm referencing but it shouldn't be necessary to actually go and read them—hopefully the flaw in my process will be evident to someone.)

1. [He defines](https://github.com/bitcoinbook/bitcoinbook/blob/8d01749bcf45f69f36cf23606bbbf3f0bd540db3/ch07.asciidoc#pay-to-script-hash-p2sh) a standard P2SH 2-of-5 multisig: `OP_2 key0 key1 key2 key3 key4 OP_5 OP_CHECKMULTISIG` where the keys are

   ```
   04C16B8698A9ABF84250A7C3EA7EEDEF9897D1C8C6ADF47F06CF73370D74DCCA01CDCA79DCC5C395D7EEC6984D83F1F50C900A24DD47F569FD4193AF5DE762C587
   04A2192968D8655D6A935BEAF2CA23E3FB87A3495E7AF308EDF08DAC3C1FCBFC2C75B4B0F4D0B1B70CD2423657738C0C2B1D5CE65C97D78D0E34224858008E8B49
   047E63248B75DB7379BE9CDA8CE5751D16485F431E46117B9D0C1837C9D5737812F393DA7D4420D7E1A9162F0279CFC10F1E8E8F3020DECDBC3C0DD389D9977965
   0421D65CBD7149B255382ED7F78E946580657EE6FDA162A187543A9D85BAAA93A4AB3A8F044DADA618D087227440645ABE8A35DA8C5B73997AD343BE5C2AFD94A5
   043752580AFA1ECED3C68D446BCAB69AC0BA7DF50D56231BE0AABF1FDEEC78A6A45E394BA29A1EDF518C022DD618DA774D207D137AAB59E0B000EB7ED238F4D800

   ```

of which the `HASH160` is `54c557e07dde5bb6cb791c7a540e0a4796f5e97e`. Ok so far so good; I got the same value as the book.

2. Then [he discusses a P2WSH transaction](https://github.com/bitcoinbook/bitcoinbook/blob/8d01749bcf45f69f36cf23606bbbf3f0bd540db3/appdx-segwit.asciidoc#pay-to-witness-script-hash-p2wsh) and references the same multisig script

   > We saw this type of script in [p2sh]. In that example, P2SH was used
   > by Mohammed’s company to express a multisignature script. Payments to
   > Mohammed’s company were encoded with a locking script like this:
   > Example P2SH output script
   >
   > ```
   > HASH160 54c557e07dde5bb6cb791c7a540e0a4796f5e97e EQUAL
   >
   > ```

   and then defines the P2WSH output script

   > Example P2WSH output script
   >
   > ```
   > 0 9592d601848d04b172905e0ddb0adde59f1590f1e553ffc81ddc4b0ed927dd73
   >
   > ```
   >
   > [...] the Segregated Witness program consists of two values pushed to the stack: a witness version (0) and the 32-byte SHA256 hash of the redeem script.

How does he get this P2WSH output script?

If I take the example P2SH script, `script`.

```
HASH160(script) = 54c557e07dde5bb6cb791c7a540e0a4796f5e97e
SHA256(script) = a9b7b38d972cabc7961dbfbcb841ad4508d133c47ba87457b4a0e8aae86dbb89
SHA256(HASH160(script)) = 9796557ff06241ca75fdbc09359e9916186cb8398dc01fd58afc72a3ac86ce63

```

None of which equals the provided:

```
9592d601848d04b172905e0ddb0adde59f1590f1e553ffc81ddc4b0ed927dd73

```

I also tried compressing the public keys, and still couldn't get that value. I'm pretty sure it's still talking about the same example, because it continues with

> Mohammed’s company can spend outputs the P2WSH output [...]

If I can figure out the 32-byte SHA256 hash of the redeem script I think I'm good to go, because at that point the address can be calculated

```
sha256_value = 9592d601848d04b172905e0ddb0adde59f1590f1e553ffc81ddc4b0ed927dd73
p2wsh_output = OP_0 + OP_32 + sha256_value
address = Base58(05 + HASH160(p2wsh_output))

```

**Tags:** segregated-witness, p2sh, mastering-bitcoin, p2wsh


---

## 2 answers

### Answer by username_1 on 2018-08-05. Score: 1 | Accepted answer

This is indeed an error in the book: <https://github.com/bitcoinbook/bitcoinbook/issues/440>

See [this diff](https://github.com/bitcoinbook/bitcoinbook/commit/1ddd89b65841dea02b5c4ab1e31cf1f4de23ba52) for the correct steps.


---

### Answer by username_2 on 2017-10-18. Score: 1

Seems to me that

> ... creating a Pay-to-Witness-Script-Hash (P2WSH) output that would
> **look like this**:
> 0 9592d601848d04b172905e0ddb0adde59f1590f1e553ffc81ddc4b0ed927dd73...

means that this hash is an example not derived from the data above

> **Comments:**
> **username_0**: Ok cool, am I doing the rest of the steps right then? / Are there other test vectors I can check against? Would the address then be `Base58(05 + HASH160(OP_0 + OP_32 + {sha256 standard p2sh scrit}))`?  
> **username_2**: I think you can 'extract' the 'test vectors' from current bitcoin/testnet blockchain rather than checking bogus sources from a book :)  
> **username_0**: Ok. So is the process correct?  
> **username_2**: I did not received `9592d6018...` with provided data either, but I did not spend much time for this question. I can say, that there are a lot of such transactions in blockchain. for example you can take testnet pair https://testnet.smartbit.com.au/tx/c9348040bbc2024e12dcb4a0b4806b0398646b91acf314da028c3f03dd0179fc and https://testnet.smartbit.com.au/tx/31bc1c88ce6ae337a6b3057a16d5bad0b561ad1dfc047d0a7fbb8814668f91e5 and try to get `1e8dda3...` from `5221038e81669c...`  

---

