# Path: bitcoinlib/config/opcodes.py
# def op():
#     pass
# 
# Path: bitcoinlib/encoding.py
# def _bech32_polymod(values):
#     """
#     Internal function that computes the Bech32 checksum
#     """
#     generator = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]
#     chk = 1
#     for value in values:
#         top = chk >> 25
#         chk = (chk & 0x1ffffff) << 5 ^ value
#         for i in range(5):
#             chk ^= generator[i] if ((top >> i) & 1) else 0
#     return chk
# 
# def _codestring_to_array(codestring, base):
#     codestring = bytes(codestring, 'utf8')
#     codebase = code_strings[base]
#     array = []
#     for s in codestring:
#         try:
#             array.append(codebase.index(s))
#         except ValueError:
#             raise EncodingError("Character '%s' not found in codebase" % s)
#     return array


import unittest
from bitcoinlib.config.opcodes import op
from bitcoinlib.encoding import *
from bitcoinlib.encoding import _bech32_polymod, _codestring_to_array
    ('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kemeawh', 'Invalid checksum (Bech32m instead of Bech32)'),
    ('tb1q0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq24jc47', 'Invalid checksum (Bech32m instead of Bech32)'),
    ('bc1p38j9r5y49hruaue7wxjce0updqjuyyx0kh56v8s25huc6995vvpql3jow4', "Character '111' not found in codebase"),
    ('BC130XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ7ZWS8R', 'Invalid decoded data length'),
    ('bc1pw5dgrnzv', 'Invalid decoded data length, must be between 2 and 40'),
    ('bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v8n0nx0muaewav253zgeav',
     'Invalid decoded data length, must be between 2 and 40'),
    ('BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P', 'Invalid decoded data length, must be 20 or 32 bytes'),
    ('bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v07qwwzcrf', "cannot convert 'NoneType' object to bytes"),
    ('tb1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vpggkg4j', "cannot convert 'NoneType' object to bytes"),
    ('bc1gmk9yu', 'Invalid checksum (Bech32 instead of Bech32m)'),
]


class TestEncodingBech32SegwitAddresses(unittest.TestCase):
    """
    Reference tests for bech32 segwit adresses

    Copyright (c) 2017 Pieter Wuille
    Source: https://github.com/sipa/bech32/tree/master/ref/python
    """

    def test_valid_checksum(self):
        """Test checksum creation and validation."""
        for test in VALID_CHECKSUM:
            pos = test.rfind('1')
            test = test.lower()
            hrp = test[:pos]
            data = _codestring_to_array(test[pos + 1:], 'bech32')
            hrp_expanded = [ord(x) >> 5 for x in hrp] + [0] + [ord(x) & 31 for x in hrp]

# Path: bitcoinlib/config/opcodes.py
# def op():
#     pass
# 
# Path: bitcoinlib/encoding.py
# def _bech32_polymod(values):
#     """
#     Internal function that computes the Bech32 checksum
#     """
#     generator = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]
#     chk = 1
#     for value in values:
#         top = chk >> 25
#         chk = (chk & 0x1ffffff) << 5 ^ value
#         for i in range(5):
#             chk ^= generator[i] if ((top >> i) & 1) else 0
#     return chk
# 
# def _codestring_to_array(codestring, base):
#     codestring = bytes(codestring, 'utf8')
#     codebase = code_strings[base]
#     array = []
#     for s in codestring:
#         try:
#             array.append(codebase.index(s))
#         except ValueError:
#             raise EncodingError("Character '%s' not found in codebase" % s)
#     return array


# Path: tests/test_encoding.py
import unittest
from bitcoinlib.config.opcodes import op
from bitcoinlib.encoding import *
from bitcoinlib.encoding import _bech32_polymod, _codestring_to_array

    ('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kemeawh', 'Invalid checksum (Bech32m instead of Bech32)'),
    ('tb1q0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq24jc47', 'Invalid checksum (Bech32m instead of Bech32)'),
    ('bc1p38j9r5y49hruaue7wxjce0updqjuyyx0kh56v8s25huc6995vvpql3jow4', "Character '111' not found in codebase"),
    ('BC130XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ7ZWS8R', 'Invalid decoded data length'),
    ('bc1pw5dgrnzv', 'Invalid decoded data length, must be between 2 and 40'),
    ('bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v8n0nx0muaewav253zgeav',
     'Invalid decoded data length, must be between 2 and 40'),
    ('BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P', 'Invalid decoded data length, must be 20 or 32 bytes'),
    ('bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v07qwwzcrf', "cannot convert 'NoneType' object to bytes"),
    ('tb1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vpggkg4j', "cannot convert 'NoneType' object to bytes"),
    ('bc1gmk9yu', 'Invalid checksum (Bech32 instead of Bech32m)'),
]


class TestEncodingBech32SegwitAddresses(unittest.TestCase):
    """
    Reference tests for bech32 segwit adresses

    Copyright (c) 2017 Pieter Wuille
    Source: https://github.com/sipa/bech32/tree/master/ref/python
    """

    def test_valid_checksum(self):
        """Test checksum creation and validation."""
        for test in VALID_CHECKSUM:
            pos = test.rfind('1')
            test = test.lower()
            hrp = test[:pos]
            data = _codestring_to_array(test[pos + 1:], 'bech32')
            hrp_expanded = [ord(x) >> 5 for x in hrp] + [0] + [ord(x) & 31 for x in hrp]

            self.assertEqual(_bech32_polymod(hrp_expanded + data), 1, msg="Invalid checksum for address %s" % test)