user85140: I am building a small script to decode the raw coinbase transaction data into a human-readable viewer without external scripts to better my understanding around the subject.
I've noticed that there are many different types of output script types among transactions. (P2SH, P2PK (as seen in genesis block), P2PKH, etc)
One thing I have not been able to grasp is how to obtain the Base58 address in the new SegWit P2WPK and P2WSH types.
For example, Block #542748 has this coinbase transaction which my script determines to have a pay to witness public key (P2WPK) output with 97cfc76442fe717f2a3f0cc9c175f7561b661997 as the public key hash.
The full output script: 0[] PUSHDATA(20)[97cfc76442fe717f2a3f0cc9c175f7561b661997]

Traditionally, in P2PK, P2PKH and P2SH I've used the image on this thread which works quite well.
When I apply the same method as the pubkeyhash (P2PKH) with the following operations I get a totally different address:
Append version byte:
0097CFC76442FE717F2A3F0CC9C175F7561B661997
SHA-256 Round 1:
f756795a3b878d16bfa9e426a47d857830bb08a02e691f0487cbeac51b510f40
SHA-256 Digest of Round 1: 
ea7fe8c3b7680b79ff507c7d1b40bfc29ca8269bb1033cc84010506743cce404
Append checksum 4 bytes: 0097CFC76442FE717F2A3F0CC9C175F7561B661997ea7fe8c3
Base58 Encode: 1EqhwhM5t5NxedLoSuJ6BtBvUMLhanBLxa
This is quite different than bc1qjl8uwezzlech723lpnyuza0h2cdkvxvh54v3dn which is listed on blockchain.info's website here
What's the best way to obtain the same address they have listed (starting with bc1)?

user85140: Okay so I think I've figured it out for anyone else wondering what exactly you have to do to derive an address from this type. 
In the case of the already mentioned transaction, (coinbase of block 542748) we first start by looking at the full decoded output script.
0 PUSHDATA(20) 97cfc76442fe717f2a3f0cc9c175f7561b661997

The very first OP_0 in the decoded script is the witness version
We then take the data pushed (in our case 97cfc76442fe717f2a3f0cc9c175f7561b661997) and convert it into binary
Then we divide the binary into 5 bit sections. In our case: 10010 11111 00111 11100 01110 11001 00010 00010 11111 11001 11000 10111 11110 01010 10001 11111 00001 10011 00100 11100 00010 11101 01111 10111 01010 11000 01101 10110 01100 00110 01100 10111
Then we prefix the witness version in 5 bits 00000
Our binary then becomes 00000 10010 11111 00111 11100 01110 11001 00010 00010 11111 11001 11000 10111 11110 01010 10001 11111 00001 10011 00100 11100 00010 11101 01111 10111 01010 11000 01101 10110 01100 00110 01100 10111
The first part of the address is considered the human readable portion.
This is always bc on the mainnet and tb on the testnet
1 is always the seperator between the human readable portion and the rest or data
Now our address starts with bc1 (mainnet is bc + seperator 1)
Now we map each 5 bits into the table here - or this alternative chart I've created
bc1 qjl8uwezzlech723lpnyuza0h2cdkvxvh
The remaining 6 characters is a checksum
Using the provided python code here we can compute the checksum using the bech32_create_checksum function
The first argument is the human readable portion as a STRING without the seperator (1)
The second argument is an array of integers converted from our binary data portion

Our example:
bech32_create_checksum("bc", [0, 18, 31, 7, 28, 14, 25, 2, 2, 31, 25, 24, 23, 30, 10, 17, 31, 1, 19, 4, 28, 2, 29, 15, 23, 10, 24, 13, 22, 12, 6, 12, 23] )

This gives us:
[20, 21, 12, 17, 13, 19]

Which can be mapped with our table to 54v3dn
Finally, we concatenate the human readable, seperator, decoded data, and the checksum for our final address: bc1qjl8uwezzlech723lpnyuza0h2cdkvxvh54v3dn

85140: <p>I am building a small script to decode the raw coinbase transaction data into a human-readable viewer without external scripts to better my understanding around the subject.</p>

<p>I've noticed that there are many different types of output script types among transactions. (P2SH, P2PK (as seen in genesis block), P2PKH, etc)</p>

<p>One thing I have not been able to grasp is how to obtain the Base58 address in the new SegWit P2WPK and P2WSH types.</p>

<p>For example, <a href="https://www.blockchain.com/btc/block/00000000000000000023805d7930291bb237472deb8ef27db6474afd472a0171" rel="nofollow noreferrer">Block #542748</a> has this <a href="https://www.blockchain.com/btc/tx/0b61523f9d48d74fc175518e1347e2b3c859d22505e3b5eba7b8fa40c86d0c8a" rel="nofollow noreferrer">coinbase transaction</a> which my script determines to have a pay to witness public key (P2WPK) output with <code>97cfc76442fe717f2a3f0cc9c175f7561b661997</code> as the public key hash.</p>

<p>The full output script: <code>0[] PUSHDATA(20)[97cfc76442fe717f2a3f0cc9c175f7561b661997]
</code></p>

<p>Traditionally, in P2PK, P2PKH and P2SH I've used the image on <a href="https://bitcoin.stackexchange.com/questions/49596/how-to-get-addresses-from-scriptpubkey-in-segwit-transactions">this thread</a> which works quite well.</p>

<p>When I apply the same method as the pubkeyhash (P2PKH) with the following operations I get a totally different address:</p>

<p>Append version byte:
<code>0097CFC76442FE717F2A3F0CC9C175F7561B661997</code></p>

<p>SHA-256 Round 1:
<code>f756795a3b878d16bfa9e426a47d857830bb08a02e691f0487cbeac51b510f40</code></p>

<p>SHA-256 Digest of Round 1: 
<code>ea7fe8c3b7680b79ff507c7d1b40bfc29ca8269bb1033cc84010506743cce404</code></p>

<p>Append checksum 4 bytes: <code>0097CFC76442FE717F2A3F0CC9C175F7561B661997ea7fe8c3</code></p>

<p>Base58 Encode: <code>1EqhwhM5t5NxedLoSuJ6BtBvUMLhanBLxa</code></p>

<p>This is quite different than <code>bc1qjl8uwezzlech723lpnyuza0h2cdkvxvh54v3dn</code> which is listed on blockchain.info's website <a href="https://www.blockchain.com/btc/tx/0b61523f9d48d74fc175518e1347e2b3c859d22505e3b5eba7b8fa40c86d0c8a" rel="nofollow noreferrer">here</a></p>

<p>What's the best way to obtain the same address they have listed (starting with bc1)?</p>
 85140: <p>Okay so I think I've figured it out for anyone else wondering what <em>exactly</em> you have to do to derive an address from this type. </p>

<p>In the case of the already mentioned transaction, (<a href="https://www.blockchain.com/btc/tx/0b61523f9d48d74fc175518e1347e2b3c859d22505e3b5eba7b8fa40c86d0c8a" rel="noreferrer">coinbase of block 542748</a>) we first start by looking at the full decoded output script.</p>

<p><code>0 PUSHDATA(20) 97cfc76442fe717f2a3f0cc9c175f7561b661997</code></p>

<ul>
<li>The very first <code>OP_0</code> in the decoded script is the <code>witness version</code></li>
<li>We then take the data pushed (in our case <code>97cfc76442fe717f2a3f0cc9c175f7561b661997</code>) and convert it into binary</li>
<li>Then we divide the binary into 5 bit sections. In our case: <code>10010 11111 00111 11100 01110 11001 00010 00010 11111 11001 11000 10111 11110 01010 10001 11111 00001 10011 00100 11100 00010 11101 01111 10111 01010 11000 01101 10110 01100 00110 01100 10111</code></li>
<li>Then we prefix the <code>witness version</code> in 5 bits <code>00000</code></li>
<li>Our binary then becomes <code>00000 10010 11111 00111 11100 01110 11001 00010 00010 11111 11001 11000 10111 11110 01010 10001 11111 00001 10011 00100 11100 00010 11101 01111 10111 01010 11000 01101 10110 01100 00110 01100 10111</code></li>
<li>The first part of the address is considered the <code>human readable</code> portion.
This is always <code>bc</code> on the mainnet and <code>tb</code> on the testnet</li>
<li><code>1</code> is always the seperator between the <code>human readable</code> portion and the rest or <code>data</code></li>
<li>Now our address starts with <code>bc1</code> (mainnet is <code>bc</code> + seperator <code>1</code>)</li>
<li>Now we map each 5 bits into the table <a href="https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#bech32" rel="noreferrer">here</a> - or <a href="https://pastebin.com/raw/7FHjDDXu" rel="noreferrer">this alternative chart</a> I've created</li>
<li><code>bc1</code> <code>qjl8uwezzlech723lpnyuza0h2cdkvxvh</code></li>
<li>The remaining 6 characters is a <a href="https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#specification" rel="noreferrer">checksum</a></li>
<li>Using the provided python code <a href="https://pastebin.com/raw/sBQPZVCz" rel="noreferrer">here</a> we can compute the checksum using the <code>bech32_create_checksum</code> function</li>
<li>The first argument is the <code>human readable</code> portion as a STRING without the seperator (1)</li>
<li>The second argument is an array of integers converted from our binary <code>data</code> portion</li>
</ul>

<p>Our example:</p>

<pre><code>bech32_create_checksum("bc", [0, 18, 31, 7, 28, 14, 25, 2, 2, 31, 25, 24, 23, 30, 10, 17, 31, 1, 19, 4, 28, 2, 29, 15, 23, 10, 24, 13, 22, 12, 6, 12, 23] )
</code></pre>

<p>This gives us:</p>

<pre><code>[20, 21, 12, 17, 13, 19]
</code></pre>

<p>Which can be mapped with our table to <code>54v3dn</code></p>

<p>Finally, we concatenate the <code>human readable</code>, <code>seperator</code>, <code>decoded data</code>, and the <code>checksum</code> for our final address: <code>bc1qjl8uwezzlech723lpnyuza0h2cdkvxvh54v3dn</code></p>
