Пример #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
Пример #2
0
def multihash_to_string(mhash):
    """
    Converts a uvarint encoded multihash into a string.
    """
    return b58encode(uvarint_decode(mhash)[0])
Пример #3
0
from multiaddr import MultiAddress
from multiaddr.utils.base58 import b58encode, b58decode

with open("base58_encode_decode.json") as f:
    import json

    tests = json.loads(f.read())

for d, e in tests:
    if len(d) == 0:
        continue
    i = int(d, 16)
    print ("{} -> {} (should be: {})".format(d, b58encode(i), e))


ma = MultiAddress("/ip4/127.0.0.1/tcp/4001/ipfs/QmerTTan8gkTEugcb4DmFpAw8Z7bkDQfhoGh6AHzQaqD1Y")

print ma
print len(ma.str_repr)
print ma.as_bytes().__repr__()
print len(ma.as_bytes())

ma2 = MultiAddress(ma.as_bytes())

print ma2
print len(ma2.str_repr)
print ma2.as_bytes().__repr__()
print len(ma2.as_bytes())