How to get addresses from scriptPubKey in segwit transactions? I'm running through raw transaction data, and I'd like to be able to gather addresses (where possible).

So far, I've made this diagram to help me understand when it's possible to figure out addresses from the scriptPubKey of each output:



However, as you can see, I'm not sure how I can work out an address from a P2WSH.

Is it possible to get addresses from the scriptPubKey alone, or can I only get them when the witness is revealed when the output has been spent?  (as shown here):

witness:      0 &lt;signature1&gt; &lt;1 &lt;pubkey1&gt; &lt;pubkey2&gt; 2 CHECKMULTISIG&gt;
scriptSig:    (empty)
scriptPubKey: 0 &lt;32-byte-hash&gt;
              (0x0020{32-byte-hash})




EDIT: Also, please let me know if you notice that any of my pattern matching in this diagram is incorrect (or if I'm missing any patterns that allow me to find out more addresses).
There currently exists no native address format for witness outputs, so the answer for P2WPKH and P2WSH is simply: you can't, and shouldn't. If you convert the 20-byte hash inside a P2WPKH output to a 1xxxxxx, and show the result, people may think that is an address more money can be sent to. However, that would result in a P2PKH output, which a pure segwit wallet may not understand.

However, most segwit outputs initially will use the P2SH wrapper (resulting in P2SH-P2WPKH or P2SH-P2WSH programs), which just look like (and behave like) P2SH addresses.

A new native segwit address format is proposed in https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki. That can only be used when both the receiver and sender software support it however.