Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
def test_is_codec_invalid_prefix(multicodec, _):
    assert not is_codec(multicodec)
Ejemplo n.º 3
0
def make_cid(*args):
    """
    Creates a :py:class:`cid.CIDv0` or :py:class:`cid.CIDv1` object based
    on the given parameters

    The function supports the following signatures:

            make_cid(<base58 encoded multihash CID>) -> CIDv0

            make_cid(<multihash CID>) -> CIDv0

            make_cid(<multibase encoded multihash CID>) -> CIDv1

            make_cid(<version>, <codec>, <multihash>) -> CIDv1

    :param args:
        - base58-encoded multihash (str or bytes)
        - multihash (str or bytes)
        - multibase-encoded multihash (str or bytes)
        - version:int, codec(str), multihash(str or bytes)
    :returns: the respective CID object
    :rtype: :py:class:`cid.CIDv0` or :py:class:`cid.CIDv1`
    :raises ValueError: if the number of arguments is not 1 or 3
    :raises ValueError: if the only argument passed is not a ``str`` or a
        ``byte``
    :raises ValueError: if 3 arguments are passed and version is not 0 or 1
    :raises ValueError: if 3 arguments are passed and the ``codec`` is not
        supported by ``multicodec``
    :raises ValueError: if 3 arguments are passed and the ``multihash`` is not
        ``str`` or ``byte``
    :raises ValueError: if 3 arguments are passed with version 0 and codec is
        not *dag-pb*
    """
    if len(args) == 1:
        data = args[0]
        if isinstance(data, str):
            return from_string(data)
        elif isinstance(data, bytes):
            return from_bytes(data)
        else:
            raise ValueError(
                'invalid argument passed, expected: str or byte, found: {}'.
                format(  # noqa
                    type(data)))

    elif len(args) == 3:
        version, codec, multihash = args
        if version not in (0, 1):
            raise ValueError(
                'version should be 0 or 1, {} was provided'.format(version))
        if not multicodec.is_codec(codec):
            raise ValueError(
                'invalid codec {} provided, please check'.format(codec))
        if not (isinstance(multihash, str) or isinstance(multihash, bytes)):
            raise ValueError(
                'invalid type for multihash provided, should be str or bytes')

        if version == 0:
            if codec != CIDv0.CODEC:
                raise ValueError(
                    'codec for version 0 can only be {}, found: {}'.format(
                        CIDv0.CODEC, codec))
            return CIDv0(multihash)
        else:
            return CIDv1(codec, multihash)
    else:
        raise ValueError('invalid number of arguments, expected 1 or 3')