Example #1
0
def decode(prefix, addr):
    """Decode a cashaddr address."""
    prefixgot, data = disassemble(addr, prefix)
    if prefixgot != prefix:
        return (None, None)
    decoded = convertbits(data, 5, 8, False)
    if decoded is None or not len(decoded):
        return (None, None)
    ver = (decoded[0] & 0x78) >> 3
    return (ver, decoded[1:])
Example #2
0
def decode(hrp, addr):
    """Decode a segwit address."""
    hrpgot, data = bech32_decode(addr)
    if hrpgot != hrp:
        return (None, None)
    decoded = convertbits(data[1:], 5, 8, False)
    if decoded is None or len(decoded) < 2 or len(decoded) > 40:
        return (None, None)
    if data[0] > 16:
        return (None, None)
    if data[0] == 0 and len(decoded) != 20 and len(decoded) != 32:
        return (None, None)
    return (data[0], decoded)
Example #3
0
def valid_version(data):
    """Check that the version is correct for the data.
        Do not include the checksum."""
    converted = convertbits(data, 5, 8, False)
    if converted == None:
        return False
    ver = converted[0]
    # First bit is reserved
    if ver & (1 << 8):
        return False
    # Last three bits specify the length we expect
    bits = 160 + (ver & 3) * 32
    # Bit six is a multiplier
    if ver & 4:
        bits *= 2
    if (len(converted) - 1) != (bits / 8):
        return False
    return True
Example #4
0
def encode(prefix, ver, data):
    """Encode a cashaddr address."""
    if not len(data) in [20, 24, 28, 32, 40, 48, 56, 64]:
        # Make sure length is valid.
        return None
    if not (0 <= ver <= 1 or ver == 15):
        # Only versions (type) 1,2 and 15 are supported.
        return None
    ver <<= 3
    dlen = 0
    tmp = len(data) * 8
    if tmp > 256:
        dlen ^= 0x04
        tmp /= 2
    dlen += (tmp - 160) / 32
    ver += dlen
    bits = convertbits([ver] + data, 8, 5)
    ret = assemble(prefix, bits)
    if decode(prefix, ret) == (None, None):
        return None
    return ret
Example #5
0
def encode(hrp, witver, witprog):
    """Encode a segwit address."""
    ret = bech32_encode(hrp, [witver] + convertbits(witprog, 8, 5))
    if decode(hrp, ret) == (None, None):
        return None
    return ret