Generate P2WSH native I want to create P2WSH native like https://en.bitcoin.it/wiki/BIP_0173.
I have already a script to generate P2WPKH and it works, thank the python part and re-arrange group byte works.

I read that witnessScript (scriptPubKey) is hash of SHA256 without RIPEMD160.
Below a little bit of my code.

PB=0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798

#REDEEM SCRIPT &lt;Public Key&gt; OP_CHECKSIG
SCRIPT=210279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798AC

#SCRIPT HASH
$ printf $SCRIPT | xxd -r -p | openssl sha256| sed 's/^.* //'
1863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262

Convert scripthash to base 2



  11000 01100 01100 01010 00011 11000 00101 00110 00101 00010 11001 10100 00000 01001 01111 01000 11001 00100 00000 11001 10101 01101
  10110 10000 10011 01101 10010 01100 00101 01100 11110 00110 01101
  01001 10100 10011 11010 00011 01110 00110 00110 00110 01010 01011
  00000 01001 00100 00001 10010 01100 010


I need to add two zero at the beginning in order to Re-arrange those bits into groups of 5


  00110 00011 00011 00010 10000 11110 00001 01001 10001 01000 10110
  01101 00000 00010 01011 11010 00110 01001 00000 00110 01101 01011
  01101 10100 00100 11011 01100 10011 00001 01011 00111 10001 10011
  01010 01101 00100 11110 10000 11011 10001 10001 10001 10010 10010
  11000 00010 01001 00000 01100 10011 00010


Add witness program


  00000 00110 00011 00011 00010 10000 11110 00001 01001 10001 01000 10110
  01101 00000 00010 01011 11010 00110 01001 00000 00110 01101 01011
  01101 10100 00100 11011 01100 10011 00001 01011 00111 10001 10011
  01010 01101 00100 11110 10000 11011 10001 10001 10001 10010 10010
  11000 00010 01001 00000 01100 10011 00010


Convert each bits to base10

0, 6, 3, 3, 2, 16, 30, 1, 9, 17, 8, 22, 13, 0, 2, 11, 26, 6, 9, 0, 6, 13, 11, 13, 20, 4, 27, 12, 19, 1, 11, 7, 17, 19, 10, 13, 4, 30, 16, 27, 17, 17, 17, 18, 18, 24, 2, 9, 0, 12, 19, 2    


Create the checksum with python code

15
29
21
28
10
6


my result is:

tb1qxrrzs7pf3gkdqzt6xfqxdtd5ymvnpt83n2dy7sm333jjczfqvnz0a4u2x


but the result in BIP 0173 is

tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7


I use the same script to generate P2WPKH that you can find in BIP_173 and it works. Of course I change Public key with public key hash, but the logic of create group of 5 bits and python part is the same.

Another question: can I create these address (P2WPKH and P2SH native) from bitcoin-cli?
Bech32 encoding, apart from the checksum, is a simple matter of converting base-256 (8-bits) to base-32 (5-bits). Base-256 is the byte array (octet string) that you get from a hash function. Only focusing on first 3 bytes of the hash:  

as hex    = 0x18     0x63     0x14 
as bytes  = 24       99       20 
as binary = 00011000 01100011 00010100 


In order to convert the above base-256 (8-bits) input to base-32 (5-bits) we start from the most significant bit and select 5 bits at a time until we run out.  

12345123 45123451 23451234 ...
00011000 01100011 00010100
(00011) (00001) (10001) (10001) (0100 ...


For a bech-32 address there is an extra step here after the base conversion is done. That step is to append of the "witness version" to the beginning of this group as an already converted value in base-32 (since witness version is defined from 0 to 16 inclusive it is already in range).  

[00000] (00011) (00001) (10001) (10001) (0100 ...


This final result can be converted to an array of numbers that are between 0 and 31  

0, 3, 1, 17, 17


To convert this "number" array to human readable format we print the corresponding character from the bech32 charset (qpzry9x8gf2tvdw0s3jn54khce6mua7l)

0-&gt;q, 3-&gt;r, 1-&gt;p, 17-&gt;3, 17-&gt;3 ...


P.S. There is a checksum calculation that I skipped to keep it simple but it is just another appended set of 5-bit values at the end of what we have in last step.