from bc4py_extension import PyAddress
import hashlib


def is_address(ck: PyAddress, hrp, ver):
    """check bech32 format and version"""
    try:
        if ck.hrp != hrp:
            return False
        if ck.version != ver:
            return False
    except ValueError:
        return False
    return True


def get_address(pk, hrp, ver) -> PyAddress:
    """get address from public key"""
    identifier = hashlib.new('ripemd160', hashlib.sha256(pk).digest()).digest()
    return PyAddress.from_param(hrp, ver, identifier)


def convert_address(ck: PyAddress, hrp, ver) -> PyAddress:
    """convert address's version"""
    return PyAddress.from_param(hrp, ver, ck.identifier())


def dummy_address(dummy_identifier) -> PyAddress:
    assert len(dummy_identifier) == 20
    return PyAddress.from_param('dummy', 0, dummy_identifier)


__all__ = [
    "is_address",
    "get_address",
    "convert_address",
    "dummy_address",
]

[((20, 11, 20, 53), 'bc4py_extension.PyAddress.from_param', 'PyAddress.from_param', ({(20, 32, 20, 35): 'hrp', (20, 37, 20, 40): 'ver', (20, 42, 20, 52): 'identifier'}, {}), '(hrp, ver, identifier)', False, 'from bc4py_extension import PyAddress\n'), ((30, 11, 30, 61), 'bc4py_extension.PyAddress.from_param', 'PyAddress.from_param', ({(30, 32, 30, 39): '"""dummy"""', (30, 41, 30, 42): '(0)', (30, 44, 30, 60): 'dummy_identifier'}, {}), "('dummy', 0, dummy_identifier)", False, 'from bc4py_extension import PyAddress\n'), ((19, 42, 19, 60), 'hashlib.sha256', 'hashlib.sha256', ({(19, 57, 19, 59): 'pk'}, {}), '(pk)', False, 'import hashlib\n')]