class IPFS: client=None def __init__(self, addr:str,port:int): self.client=Client(Multiaddr(addr)) log("Adresse du client IPFS: " + addr) def add_file(self, file): cid=self.client.add(file) log("Enregistrement du fichier "+cid["Hash"]+" sur IPFS") return cid["Hash"] def get(self,token): file=self.client.get(token) return file def add(self,body:str): if type(body)==dict: s=json.dumps(body) filename="./temp/metadata"+hex(int(datetime.now().timestamp()*1000))+".json" with open(filename,"w") as f: f.write(s) f.close() if type(body)==str: if body.startswith("data:"): data=base64.b64decode(body.split("base64,")[1]) else: data=bytes(body,"utf8") filename="./temp/image"+hex(int(datetime.now().timestamp()*1000))+".jpg" with open(filename,"wb") as f: f.write(data) f.close() cid=self.client.add(filename) cid=cid["Hash"] os.remove(filename) log("Enregistrement du fichier https://ipfs.io/ipfs/" + cid + " sur IPFS") return cid def get_dict(self,token): if len(token)!=46: return token url="https://ipfs.io/ipfs/"+token r=requests.get(url) try: return json.loads(r.text.replace("'","\"")) except: return r def get_link(self, cid): return "htts://ipfs.io/ipfs/"+cid
def ipfs_hashing( client: ipfshttpclient.Client, configuration: PackageConfiguration, package_type: PackageType, ) -> Tuple[str, str, List[Dict]]: """ Hashes a package and its components. :param client: a connected IPFS client. :param configuration: the package configuration. :param package_type: the package type. :return: the identifier of the hash (e.g. 'fetchai/protocols/default') | and the hash of the whole package. """ # hash again to get outer hash (this time all files) # we still need to ignore some files # use ignore patterns somehow # ignore_patterns = configuration.fingerprint_ignore_patterns # noqa: E800 assert configuration.directory is not None result_list = client.add( configuration.directory, recursive=True, period_special=False, follow_symlinks=False, ) key = os.path.join( configuration.author, package_type.to_plural(), configuration.directory.name, ) # check that the last result of the list is for the whole package directory assert result_list[-1]["Name"] == configuration.directory.name directory_hash = result_list[-1]["Hash"] return key, directory_hash, result_list
def assert_hash_consistency(fingerprint, path_prefix, client: ipfshttpclient.Client) -> None: """ Check that our implementation of IPFS hashing for a package is correct against the true IPFS. :param fingerprint: the fingerprint dictionary. :param path_prefix: the path prefix to prepend. :return: None. :raises AssertionError: if the IPFS hashes don't match. """ # confirm ipfs only generates same hash: for file_name, ipfs_hash in fingerprint.items(): path = path_prefix / file_name expected_ipfs_hash = client.add(path)["Hash"] assert (expected_ipfs_hash == ipfs_hash ), "WARNING, hashes don't match for: {}".format(path)
def __init__(self, addr:str,port:int): self.client=Client(Multiaddr(addr)) log("Adresse du client IPFS: " + addr)