Пример #1
0
def check_node_id(node_data):
    id = node_data.get("id")
    try:
        node_cid = cid.from_string(id)
    except:
        raise CidException(f"Invalid CID: {id}! Please refer to spec.")

    assert node_cid.version == 1
    assert node_cid.codec == "dag-cbor"
Пример #2
0
def to_bytes(proto, string):
    expected_codec = PROTO_NAME_TO_CIDv1_CODEC.get(proto.name)

    if len(string) in CIDv0_PREFIX_TO_LENGTH.get(string[0:2], ()):  # CIDv0
        # Upgrade the wire (binary) representation of any received CIDv0 string
        # to CIDv1 if we can determine which multicodec value to use
        if expected_codec:
            return cid.make_cid(1, expected_codec,
                                base58.b58decode(string)).buffer

        return base58.b58decode(string)
    else:  # CIDv1+
        parsed = cid.from_string(string)

        # Ensure CID has correct codec for protocol
        if expected_codec and parsed.codec != expected_codec:
            raise ValueError(
                "“{0}” multiaddr CIDs must use the “{1}” multicodec".format(
                    proto.name, expected_codec))

        return parsed.buffer
Пример #3
0
    def info(self, path, **kwargs):
        logger.debug("info on %s", path)

        def req(endpoint):
            try:
                return self._gw_apipost(endpoint, arg=path)
            except HTTPError as e:
                try:
                    msg = e.response.json()
                except json.JSONDecodeError:
                    raise IOError("unknown error") from e
                else:
                    if "Message" in msg:
                        raise FileNotFoundError(msg["Message"]) from e
                    else:
                        raise IOError(msg) from e

        stat = req("object/stat")
        c = cid.from_string(stat["Hash"])
        if c.codec == "raw":
            size = stat["DataSize"]
            ftype = "file"
        else:
            dag = req("dag/get")
            data = UnixFSData()
            if "data" in dag:
                data.ParseFromString(base64.b64decode(dag["data"]))
            else:
                rawdata = dag["Data"]["/"]["bytes"]
                data.ParseFromString(
                    base64.b64decode(rawdata + "=" * (-len(rawdata) % 4)))

            size = data.filesize
            if data.Type == data.File:
                ftype = "file"
            else:
                ftype = "directory"

        return {"name": path, "size": size, "type": ftype}
Пример #4
0
 def test_cid(self, cidv0, cidv1):
     """ from_string: works for non multibase-encoded strings """
     assert from_string(cidv1.buffer) == cidv1
Пример #5
0
 def test_invalid_base58_encoded_hash(self):
     with pytest.raises(ValueError) as excinfo:
         from_string('!')
     assert 'multihash is not a valid base58 encoded multihash' in str(excinfo.value)
Пример #6
0
 def test_base58_encoded_hash(self, cidv0):
     """ from_string: works for base58-encoded strings """
     assert from_string(cidv0.encode()) == cidv0
Пример #7
0
 def test_multibase_encoded_hash(self, cidv1, codec):
     """ from_string: works for multibase-encoded strings """
     assert from_string(cidv1.encode(codec.encoding)) == cidv1
Пример #8
0
 def test_invalid_cid_length(self):
     with pytest.raises(ValueError) as excinfo:
         from_string('011111111')
     assert 'cid length is invalid' in str(excinfo.value)
Пример #9
0
 def test_invalid_length_zero(self, value):
     with pytest.raises(ValueError) as excinfo:
         from_string(value)
     assert 'argument length can not be zero' in str(excinfo.value)
Пример #10
0
 def test_cid_raw(self, cidv0, cidv1):
     """ from_string: works for raw cidbytes """
     assert from_string(cidv1.buffer) == cidv1