Example #1
0
def to_string(proto, addr):
    """
    Properly converts bytes to string or int representation based on the given
    protocol.  Returns string representation of address and the number of bytes
    from the buffer consumed.
    """
    if proto.name == protocols.IP4:
        size = proto.size//8
        string = ip4_bytes_to_string(addr[:size])
    elif proto.name == protocols.IP6:
        size = proto.size//8
        string = ip6_bytes_to_string(addr[:size])
    elif proto.name == protocols.TCP:
        size = proto.size//8
        string = port_from_bytes(addr[:size])
    elif proto.name == protocols.UDP:
        size = proto.size//8
        string = port_from_bytes(addr[:size])
    elif proto.name == protocols.IPFS:
        varint, size = uvarint_decode(addr)
        string = b58encode(varint)
    else:
        msg = "Protocol not implemented: {}".format(proto.name)
        raise AddressException(msg)
    return string, size
Example #2
0
def multihash_to_string(mhash):
    """
    Converts a uvarint encoded multihash into a string.
    """
    return b58encode(uvarint_decode(mhash)[0])
Example #3
0
def proto_from_bytes(code):
    """
    Converts a protocol code from a bytes oject to an int.
    """
    return uvarint_decode(code)[0]