Beispiel #1
0
def from_bytes(cidbytes):
    """
    Creates a CID object from a encoded form

    :param bytes cidbytes: can be

        - base58-encoded multihash
        - multihash
        - multibase-encoded multihash
    :return: a CID object
    :rtype: :py:class:`cid.CIDv0` or :py:class:`cid.CIDv1`
    """
    if multibase.is_encoded(cidbytes):
        # if the bytestream is multibase encoded
        cid = multibase.decode(cidbytes)
        data = cid[1:]
        version = int(cid[0])
        codec = multicodec.get_codec(data)
        multihash = multicodec.remove_prefix(data)

        return make_cid(version, codec, multihash)
    elif cidbytes[0] in (0, 1):
        # if the bytestream is a CID
        version = cidbytes[0]
        data = cidbytes[1:]
        codec = multicodec.get_codec(data)
        multihash = multicodec.remove_prefix(data)

        return make_cid(version, codec, multihash)
    else:
        # otherwise its just base58-encoded multihash
        return make_cid(0, CIDv0.CODEC, base58.b58decode(cidbytes))
Beispiel #2
0
def from_bytes(cidbytes):
    """
    Creates a CID object from a encoded form

    :param bytes cidbytes: can be

        - base58-encoded multihash
        - multihash
        - multibase-encoded multihash
    :return: a CID object
    :rtype: :py:class:`cid.CIDv0` or :py:class:`cid.CIDv1`
    :raises: `ValueError` if the base58-encoded string is not a valid string
    :raises: `ValueError` if the length of the argument is zero
    :raises: `ValueError` if the length of decoded CID is invalid
    """
    if len(cidbytes) < 2:
        raise ValueError('argument length can not be zero')

    # first byte for identity multibase and CIDv0 is 0x00
    # putting in assumption that multibase for CIDv0 can not be identity
    # refer: https://github.com/ipld/cid/issues/13#issuecomment-326490275
    if cidbytes[0] != 0 and multibase.is_encoded(cidbytes):
        # if the bytestream is multibase encoded
        cid = multibase.decode(cidbytes)

        if len(cid) < 2:
            raise ValueError('cid length is invalid')

        data = cid[1:]
        version = int(cid[0])
        codec = multicodec.get_codec(data)
        multihash = multicodec.remove_prefix(data)
    elif cidbytes[0] in (0, 1):
        # if the bytestream is a CID
        version = cidbytes[0]
        data = cidbytes[1:]
        codec = multicodec.get_codec(data)
        multihash = multicodec.remove_prefix(data)
    else:
        # otherwise its just base58-encoded multihash
        try:
            version = 0
            codec = CIDv0.CODEC
            multihash = base58.b58decode(cidbytes)
        except ValueError:
            raise ValueError(
                'multihash is not a valid base58 encoded multihash')

    try:
        mh.decode(multihash)
    except ValueError:
        raise

    return make_cid(version, codec, multihash)
def test_verify_prefix_complete(multicodec, prefix):
    data = b'testbytesbuffer'
    prefix_int = prefix['prefix']
    prefixed_data = add_prefix(multicodec, data)

    assert is_codec(multicodec)
    assert get_codec(prefixed_data) == multicodec
    assert remove_prefix(prefixed_data) == data
    assert extract_prefix(prefixed_data) == prefix_int
Beispiel #4
0
def from_bytes(cidbytes):
    """
    Creates a CID object from a encoded form

    :param bytes cidbytes: can be

        - base58-encoded multihash
        - multihash
        - multibase-encoded multihash
    :return: a CID object
    :rtype: :py:class:`cid.CIDv0` or :py:class:`cid.CIDv1`
    :raises: `ValueError` if the base58-encoded string is not a valid string
    """
    if multibase.is_encoded(cidbytes):
        # if the bytestream is multibase encoded
        cid = multibase.decode(cidbytes)
        data = cid[1:]
        version = int(cid[0])
        codec = multicodec.get_codec(data)
        multihash = multicodec.remove_prefix(data)
    elif cidbytes[0] in (b'0', b'1'):
        # if the bytestream is a CID
        version = cidbytes[0]
        data = cidbytes[1:]
        codec = multicodec.get_codec(data)
        multihash = multicodec.remove_prefix(data)
    else:
        # otherwise its just base58-encoded multihash
        try:
            version = 0
            codec = CIDv0.CODEC
            multihash = base58.b58decode(cidbytes)
        except ValueError:
            raise ValueError(
                'multihash is not a valid base58 encoded multihash')

    try:
        mh.decode(multihash)
    except ValueError:
        raise

    return make_cid(version, codec, multihash)
Beispiel #5
0
def get_codec(chash):
    """
    Extract the codec of a content hash

    :param str hash: a hex string containing a content hash

    :return: the extracted codec
    :rtype: str
    """

    buffer = multihash.from_hex_string(chash.lstrip('0x'))
    return multicodec.get_codec(buffer)
Beispiel #6
0
def decode(chash):
    """
    Decode a content hash.

    :param str hash: a hex string containing a content hash

    :return: the decoded content
    :rtype: str
    """

    buffer = multihash.from_hex_string(chash.lstrip('0x'))

    codec = multicodec.get_codec(buffer)
    value = multicodec.remove_prefix(buffer)

    profile = get_profile(codec)
    return profile.decode(value)
def test_get_codec_invalid_prefix(_, prefix):
    prefix_bytes = varint.encode(prefix)
    with pytest.raises(ValueError) as excinfo:
        get_codec(prefix_bytes)
    assert 'not present in the lookup table' in str(excinfo.value)