I'm trying to generate a multisignature, segwit compatible address as a 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 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.)
He defines a standard P2SH 2-of-5 multisig:
OP_2 key0 key1 key2 key3 key4 OP_5 OP_CHECKMULTISIGwhere the keys are
04C16B8698A9ABF84250A7C3EA7EEDEF9897D1C8C6ADF47F06CF73370D74DCCA01CDCA79DCC5C395D7EEC6984D83F1F50C900A24DD47F569FD4193AF5DE762C587 04A2192968D8655D6A935BEAF2CA23E3FB87A3495E7AF308EDF08DAC3C1FCBFC2C75B4B0F4D0B1B70CD2423657738C0C2B1D5CE65C97D78D0E34224858008E8B49 047E63248B75DB7379BE9CDA8CE5751D16485F431E46117B9D0C1837C9D5737812F393DA7D4420D7E1A9162F0279CFC10F1E8E8F3020DECDBC3C0DD389D9977965 0421D65CBD7149B255382ED7F78E946580657EE6FDA162A187543A9D85BAAA93A4AB3A8F044DADA618D087227440645ABE8A35DA8C5B73997AD343BE5C2AFD94A5 043752580AFA1ECED3C68D446BCAB69AC0BA7DF50D56231BE0AABF1FDEEC78A6A45E394BA29A1EDF518C022DD618DA774D207D137AAB59E0B000EB7ED238F4D800
of which the
54c557e07dde5bb6cb791c7a540e0a4796f5e97e. Ok so far so good; I got the same value as the book.
Then he discusses a P2WSH transaction 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
[...] 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,
HASH160(script) = 54c557e07dde5bb6cb791c7a540e0a4796f5e97e SHA256(script) = a9b7b38d972cabc7961dbfbcb841ad4508d133c47ba87457b4a0e8aae86dbb89 SHA256(HASH160(script)) = 9796557ff06241ca75fdbc09359e9916186cb8398dc01fd58afc72a3ac86ce63
None of which equals the provided:
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))