コード例 #1
0
ファイル: conversion.py プロジェクト: amstocker/python-libp2p
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
コード例 #2
0
ファイル: conversion.py プロジェクト: zixuanzh/python-libp2p
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
コード例 #3
0
ファイル: conversion.py プロジェクト: amstocker/python-libp2p
def multihash_to_string(mhash):
    """
    Converts a uvarint encoded multihash into a string.
    """
    return b58encode(uvarint_decode(mhash)[0])
コード例 #4
0
ファイル: conversion.py プロジェクト: amstocker/python-libp2p
def proto_from_bytes(code):
    """
    Converts a protocol code from a bytes oject to an int.
    """
    return uvarint_decode(code)
コード例 #5
0
ファイル: conversion.py プロジェクト: zixuanzh/python-libp2p
def multihash_to_string(mhash):
    """
    Converts a uvarint encoded multihash into a string.
    """
    return b58encode(uvarint_decode(mhash)[0])
コード例 #6
0
ファイル: conversion.py プロジェクト: zixuanzh/python-libp2p
def proto_from_bytes(code):
    """
    Converts a protocol code from a bytes oject to an int.
    """
    return uvarint_decode(code)