def __init__(self, publicIpfs: bool = True): self.ipfsConfig = ( config["ipfs"]["nodeUrlDefault"] ["public" if publicIpfs else "private"]) self.ipfs = ipfsapi.connect( self.ipfsConfig["host"], self.ipfsConfig["port"], self.ipfsConfig["protocol"])
def is_available(): """ Return whether the IPFS daemon is reachable or not """ global __is_available if not isinstance(__is_available, bool): try: ipfsapi.connect() except ipfsapi.exceptions.Error as error: __is_available = False # Make sure version incompatiblity is displayed to the user if isinstance(error, ipfsapi.exceptions.VersionMismatch): raise else: __is_available = True return __is_available
def downloadfile(self, localfile): """ query the localfile from DB and then download from the udfs :param localfile: a file path :type localfile: str :return: True os False """ if not self.connect: self.udfs.start(False) self.connect = ipfsapi.connect(self.udfs_host, self.udfs_port) # TODO query the file hash from DB pass
def __init__(self, dataset, client='127.0.0.1', port=5001): """ Build the publisher from a LODataset. """ self.dataset = dataset self.dataset_id = dataset.id self.last_modified = dataset["modified"] self.api = ipfsapi.connect(client, port) self.was_updated = True logging.getLogger().setLevel(logging.INFO) logging.info("Dataset " + dataset.id) logging.info( "Last modified " + self.last_modified.toPython().strftime("%Y-%m-%d %H:%M:%S"))
def __init__(self): self.w3 = Web3(IPCProvider('/tmp/geth.ipc')) with open('../address.txt', 'r') as f: address = f.read().rstrip("\n") with open( '../videos_sharing_smart_contract/build/contracts.json') as f: contract = json.load(f) abi = contract['VideosSharing']['abi'] self.SmartContract = self.w3.eth.contract(address=address, abi=abi) self.ipfs_con = ipfsapi.connect()
def execute(self, quals, columns): import ipfsapi api = ipfsapi.connect('127.0.0.1', 5001) Jres = api.object_get(self.fhash) data = Jres['Data'] Links = Jres['Links'] for line in Links: line = dict((k.lower(), v) for k, v in line.iteritems()) line['data'] = data ltmp = list() for x in self.columns: ltmp.append(line[x]) yield ltmp[:len(self.columns)]
def ipfs_download(multihash): tempdir = gettempdir() os.chdir(tempdir) temp_obj = NamedTemporaryFile(delete=False) res = ipfs_download_file(connect(), multihash.multihash, temp_obj.name) if not res: raise Exception("Can't download objective") messages = {} for topic, msg, timestamp in Bag(temp_obj.name, 'r').read_messages(): messages[topic] = msg return messages
def downloadhash(self, filehash, filepath=None): if not self.connect: self.connect = ipfsapi.connect(self.udfs_host, self.udfs_port) try: start = time.time() self.connect.get(filehash, filepath=filepath) end = time.time() print('download {0} cost:{1}'.format(filehash, (end - start))) print("download {} successfully!".format(filehash)) return True except Exception, e: logging.error("download fail:{}".format(e)) return False
def get_article_raw_html(ipfs_hash="", lastactivity="", articletable=""): # Account for a blank IPFS if ipfs_hash == "": return HttpResponse("NEED THE IPFS HASH") # Try pulling the article HTML from the cache try: hashCache = HashCache.objects.get(ipfs_hash=ipfs_hash) lastCacheRefresh = hashCache.timestamp except: hashCache = "" lastCacheRefresh = dateTimeImportClass(2000, 1, 1, 1, 1, 1, tzinfo=pytz.timezone('UTC')) # Account for the last time the cache was updated. If it is too old, pull from IPFS itself rather than the cache if (lastactivity is None): pullFromIPFS = True else: try: pullFromIPFS = (lastactivity > lastCacheRefresh) except: pullFromIPFS = False # FIX LATER pullFromIPFS = False if pullFromIPFS: # Pull from IPFS # Connect to the IPFS API api = ipfsapi.connect('127.0.0.1', 5001) # Fetch the article HTML and unzip it rawHTML = api.cat(ipfs_hash) rawHTML = gzip.GzipFile(fileobj=StringIO.StringIO(rawHTML)).read() # Get the current time lilStamp = datetime.datetime.now(tz=pytz.utc) try: # Update the HTML cache hashTable = HashCache.objects.get(ipfs_hash=ipfs_hash) hashTable.articletable = articletable hashTable.timestamp = lilStamp hashTable.html_blob = rawHTML hashTable.save() except: print("CREATING A NEW HASHCACHE") # Establish a cache for the HTML hashTable = HashCache.objects.create(ipfs_hash=ipfs_hash, timestamp=lilStamp, html_blob=rawHTML, articletable=articletable) return rawHTML else: # Pull from the HashCache return hashCache.html_blob
def list(self, filehash): if not self.connect: self.connect = ipfsapi.connect(self.udfs_host, self.udfs_port) try: self.objects = self.connect.ls(filehash).get('Objects') if self.objects: for object in self.objects: if 'Links' in object.keys(): for link in object.get('Links'): self.links.append(link) else: self.links = "test" except Exception, e: logging.error("ls fail:{}".format(e))
def upload_stream(self, stream): if not self.connect: self.connect = ipfsapi.connect(self.udfs_host, self.udfs_port) # TODO need fix try: # py-api doesn't support add stream.But the js-api supports.So sad.Maybe need to use HTTP-api. start = time.time() result = self.connect.add(stream) end = time.time() self.log.info('upload stream cost:{}'.format(end - start)) return result.get('Hash') except Exception, e: logging.error("Failed upload.{}".format(e)) return None
def execute(self, quals, columns): import ipfsapi api = ipfsapi.connect('127.0.0.1', 5001) res = api.cat(self.fhash) dlist = res.split("\n") cnt = 0 for line in dlist: if line == "": continue tmp = line.split(self.delimiter) if len(tmp) > len(self.columns): exit("data format error") if cnt >= self.skip_header: yield tmp[:len(self.columns)] cnt += 1
def retrieve_all_files(): api = ipfsapi.connect('127.0.0.1', 5001) mydb, mycursor = db() mycursor.execute("SELECT file_name, hash FROM images") result_set = mycursor.fetchall() for row in result_set: file_name = row[0] Hash = row[1] f = open(app.config['UPLOAD_FOLDER'] + file_name, "w+") image = api.cat(Hash) f.write(image) f.close() return "Retrived all images onto Pi cluster"
def upload(): if request.method == 'POST': if 'file' in request.files: f = request.files['file'] # print(f) api = ipfsapi.connect('localhost', 5001) res = api.add(f) print(res['Hash']) sql = "INSERT INTO main.images(hash,create_) VALUES('%s',datetime('now'))" % res['Hash'] db().dbexec(sql) # f.save('/var/www/uploads/uploaded_file.txt') return 'success'
def upload_data(self, src, dst): host = str(dst['host']) port = int(dst['port']) client = ipfsapi.connect(host, port) file_node = client.add(src) file_addr = { 'host': host, 'port': port, 'file_hash': file_node['Hash'] } return json.dumps(file_addr)
def run(self): # Connect to local node try: api = ipfsapi.connect('127.0.0.1', 5001) print(api) except ipfsapi.exceptions.ConnectionError as ce: print(str(ce)) sys.exit(-1) for event in api.log_tail(): # add to queue only dht event's if event['event'] == 'handleAddProvider': log_queque.put(event, block=False)
def initialize_ipfsapi_connection(self): # establish self.api = ipfsapi.connect(self.ipfsapi_ip, self.ipfsapi_port) self.apiid = self.api.id() self.ipfs_addresses = self.apiid[self.ipfslist[0]] for i in range(1, len(self.ipfslist)): print(self.ipfslist[i], '\n' + self.apiid[self.ipfslist[i]] + '\n') print(self.ipfslist[0]) for i in range(0, len(self.ipfs_addresses)): print(self.ipfs_addresses[i]) # return the api connection instance # adopted from the ipfsapi.connect class return self.api
def retrieve(hash): #CREATE A cryptographic HASHID FROM IPFS SERVER try: connect = ipfsapi.connect('127.0.0.1',5001) #LOCAL IPFS SERVER, PREVOUSLY INSTALLED print("Successful connected to IPFS nectwork") except Exception as e: print("Unexpected Error, on IPFS::") print(e) exit(-1) try: res = connect.get(hash) print('File has been downloaded') except Exception as e: print(e) exit(-1)
def _pull_from_dht(block_addresses, data_q): local_ipfs = ipfsapi.connect('127.0.0.1', 5001) for block_index, block_address in block_addresses: try: # 1.step pull local_ipfs.get(block_address) # 3.step read content from block data_q.put((block_index, block_address.split('/')[-1])) except: pass data_q.put(None)
def map_fulfillment_data(data_hash, bounty_id, fulfillment_id, contract_version): ipfs_hash = data_hash if len(ipfs_hash) != 46 or not ipfs_hash.startswith('Qm'): logger.error( 'Data Hash Incorrect for fulfillment on bounty: {:d} fulfillment: {:d}' .format(bounty_id, fulfillment_id)) data_JSON = "{}" else: try: data_JSON = ipfs.cat(ipfs_hash) except StatusError as e: if e.original.response.status_code == 504: logger.warning( 'Timeout for bounty id {}, trying old IPFS Node'.format( bounty_id)) old_ipfs = ipfsapi.connect( host='https://ipfs.bounties.network', port='443') data_JSON = old_ipfs.cat(ipfs_hash) else: raise e if len(ipfs_hash) == 0: ipfs_hash = 'invalid' data = json.loads(data_JSON) metadata = data.pop('meta', {}) if 'payload' in data: data = data.get('payload') data_fulfiller = data.get('fulfiller', {}) plucked_data = pluck(data, fulfillment_data_keys) return { 'data_json': str(data_JSON), 'data': ipfs_hash, 'data_fulfiller': data_fulfiller, 'fulfiller_name': data_fulfiller.get('name', ''), 'fulfiller_email': data_fulfiller.get('email', '') or data.get('contact', ''), 'fulfiller_githubUsername': data_fulfiller.get('githubUsername', ''), 'fulfiller_address': data_fulfiller.get('address', ''), **plucked_data, **metadata, }
def decrypt(): api = ipfsapi.connect('127.0.0.1', 5001) res = {} cfrags = list() if request.headers['Content-Type'] == 'application/json': account = request.json['account'] ciphertexthex = request.json['ciphertext'] b_ciphertext = bytes.fromhex(ciphertexthex) decryptkey = request.json['decryptkey'] b_decryptkey = bytes.fromhex(decryptkey) deckey = UmbralPrivateKey.from_bytes(b_decryptkey) capsuleaddr = request.json['capsule'] b_capsule_all = api.cat(capsuleaddr) splitarr1 = b_capsule_all.split(b'ZAtech') b_basic_capsule = splitarr1[0] capsule = Capsule.from_bytes(b_basic_capsule, UmbralParameters(Curve(714))) print("0") correctness_keys = splitarr1[1] splitarr2 = correctness_keys.split(b'ZBtech') delegating = UmbralPublicKey.from_bytes(splitarr2[0]) receiving = UmbralPublicKey.from_bytes(splitarr2[1]) verifying = UmbralPublicKey.from_bytes(splitarr2[2]) # 用带入的参数capsule_all的各种byte,重现绑定correctness keys的capsule. capsule.set_correctness_keys(delegating=delegating, receiving=receiving, verifying=verifying) print("1") b_cfrag_all = splitarr1[2].split(b'ZCtech') for b_cfrag in b_cfrag_all: cfrags.append(CapsuleFrag.from_bytes(b_cfrag)) for cfrag in cfrags: capsule.attach_cfrag(cfrag) print("2") print(capsule) print(capsule.get_correctness_keys()) print(cfrags) cleartext = pre.decrypt(ciphertext=b_ciphertext, capsule=capsule, decrypting_key=deckey) print("3") res = {"cleartext": cleartext.decode("utf-8")} print("\nbob_cleartext: ") print(cleartext) return jsonify(res), {'Content-Type': 'application/json'} return
def __init__(self, host='localhost', port=5001): self.last_hash_added = None self.api = ipfsapi.connect(host='127.0.0.1', port=8080) # self.web3 = Web3(HTTPProvider('http://localhost:8545')) ipc_path = os.path.dirname( os.path.realpath(__file__)) + '/data/geth.ipc' print "IPCProvider path: ", ipc_path self.web3 = Web3(IPCProvider(ipc_path)) self.blockNumber = self.web3.eth.blockNumber self.eth_accounts = self.web3.personal.listAccounts self.account_index = 0 self.tx = {} print "Initializing a DDASH Interface object."
def create_hash(file): #CREATE A cryptographic HASHID FROM IPFS SERVER try: connect = ipfsapi.connect( '127.0.0.1', 5001) #LOCAL IPFS SERVER, PREVOUSLY INSTALLED print("Successful connected to IPFS nectwork") except Exception as e: print("Unexpected Error, on IPFS::") print(e) exit(-1) res = connect.add(file) if res["Hash"]: return res["Hash"] else: return None
def connect(self, nucypher_network, ipfs_api_gateway): """ client = ncipfs.Connect( nucypher_network="localhost:11500", ipfs_api_gateway="localhost:5001" ) """ print("connect") self.nucypher_network = nucypher_network self.ipfs_api_gateway = ipfs_api_gateway print("starting connection") try: self.ipfs_gateway_api = ipfsapi.connect('127.0.0.1', 5001) except Exception as e: # should be more specific ConnectionRefusedError, NewConnectionError, MaxRetryError not sure print("Automatic Mode A Public Gateway will be used as a fallback") self.ipfs_gateway_api = ipfsapi.connect('https://ipfs.infura.io', 8080) SEEDNODE_URL = self.nucypher_network POLICY_FILENAME = "policy-metadata.json" # # FOR LOCAL RUNNING NET # self.ursula = Ursula.from_seed_and_stake_info( # seed_uri=SEEDNODE_URL, # federated_only=True, # minimum_stake=0 # ) self.ursula =urs = Ursula.from_teacher_uri( teacher_uri=self.nucypher_network, federated_only=True, min_stake=0 ) print("connected") return True
def __init__(self, account_address): interface_file = open('smart-copyright.info', 'r') interface = json.load(interface_file) abi = interface['abi'] contract_address = interface['contract_address'] interface_file.close() self.contract = Client.w3.eth.contract(abi=abi, address=contract_address) # register this guy... self.account_address = account_address self.contract.functions.userRegister().transact( {'from': self.account_address}) # connect to ipfs self.ipfs = ipfsapi.connect('127.0.0.1', 5001)
def create_objective(period: str, stream_id: str, email: str) -> str: ipfs = connect() tempdir = gettempdir() os.chdir(tempdir) with NamedTemporaryFile(delete=False) as f: bag = Bag(f.name, 'w') bag.write('/period', String(period)) bag.write('/stream_id', String(stream_id)) bag.write('/email', String(email)) bag.close() res = ipfs.add(f.name) rospy.loginfo("Hash of the objective is {}".format(res['Hash'])) return res['Hash']
def __init__(self): rospy.init_node('worker') rospy.loginfo('Launching worker node...') self.ipfs = ipfsapi.connect() rospy.Subscriber('/liability/infochan/incoming/demand', Demand, self.process) rospy.Subscriber('/measurements', String, self.update_measurements) self.pub = rospy.Publisher('/liability/infochan/signing/result', Result, queue_size=128) rospy.loginfo('Worker node launched.')
def _pull_from_dht(block_addresses, data_q): ipfs_host = os.environ.get('IPFS_HOST', '127.0.0.1') local_ipfs = ipfsapi.connect(ipfs_host, 5001) for block_index, block_address in block_addresses: try: # 1.step pull local_ipfs.get(block_address) # 3.step read content from block data_q.put((block_index, block_address.split('/')[-1])) except: pass data_q.put(None)
def encrypt(): api = ipfsapi.connect('127.0.0.1', 5001) res = {} if request.headers['Content-Type'] == 'application/json': plaintextstr = request.json['plaintext'] b_plaintext = bytes(plaintextstr, encoding='utf-8') encryptkeyhex = request.json['public_key'] b_encryptkey = bytes.fromhex(encryptkeyhex) enckey = UmbralPublicKey.from_bytes(b_encryptkey) ciphertext, capsule = pre.encrypt(enckey, b_plaintext) capsuleaddr = api.add_bytes(capsule.to_bytes()) res = {"ciphertext": ciphertext.hex(), "capsule": capsuleaddr} return jsonify(res), {'Content-Type': 'application/json'} return
def main(): frame = "" frame_array = [] fileName = "data_" serial_port = serial.Serial(DEFAULT_PORT, BAUDRATE, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS) while True: try: while serial_port.in_waiting > 0: byte = serial_port.read() if byte == b"\n": print (frame) frame += byte.decode() frame_array.append(frame) frame = "" continue if byte != b"\x86" and byte != b"\x00": frame += byte.decode() time.sleep(INTERVAL) except KeyboardInterrupt: folderName = "./sensor_data/" if not os.path.exists(os.path.dirname(folderName)): try: os.makedirs(os.path.dirname(folderName)) except OSError as exc: # Guard against race condition if exc.errno != errno.EEXIST: raise timestr = time.strftime("%Y%m%d_%H%M%S_") fileName = folderName + fileName + timestr + ".txt" with open (fileName, "w") as f: for item in frame_array: f.write(item) api = ipfsapi.connect('127.0.0.1', 5001) res = api.add(fileName) print (res) if res != None: os.rename(fileName, fileName[:-4] + res["Hash"] + fileName[-4:]) print ("\nExit") break
def publish(): api = ipfsapi.connect('127.0.0.1', 5001) res = api.add('models/model.pkl') ipfsHash = res['Hash'] print(ipfsHash) contract = server.eth.contract(address=CONTRACT_ADDRESS, abi=CONTRACT_ABI) account = session.get('account', DEFAULT_ACCOUNT) print(account) if account is not None: tx_hash = contract.functions.setCheckPointIpfsHash(ipfsHash).transact( {'from': account}) receipt = server.eth.waitForTransactionReceipt(tx_hash) print("Gas Used ", receipt.gasUsed) return redirect(url_for('display'))
def test_ipfs(self): for attempt in range(5): try: api = ipfsapi.connect("ipfs",5001) break except: if attempt == 4: raise ConnectionError print("[+] Attempt {} to connect to IPFS failed. Sleeping for 1 second.".format(attempt)) time.sleep(1) hash = "QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/readme" response_must_be = "Hello and Welcome to IPFS!\n\n\u2588\u2588\u2557\u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\n\u2588\u2588\u2551\u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2554\u2550\u2550\u2550\u2550\u255d\u2588\u2588\u2554\u2550\u2550\u2550\u2550\u255d\n\u2588\u2588\u2551\u2588\u2588\u2588\u2588\u2588\u2588\u2554\u255d\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\n\u2588\u2588\u2551\u2588\u2588\u2554\u2550\u2550\u2550\u255d \u2588\u2588\u2554\u2550\u2550\u255d \u255a\u2550\u2550\u2550\u2550\u2588\u2588\u2551\n\u2588\u2588\u2551\u2588\u2588\u2551 \u2588\u2588\u2551 \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2551\n\u255a\u2550\u255d\u255a\u2550\u255d \u255a\u2550\u255d \u255a\u2550\u2550\u2550\u2550\u2550\u2550\u255d\n\nIf you're seeing this, you have successfully installed\nIPFS and are now interfacing with the ipfs merkledag!\n\n -------------------------------------------------------\n| Warning: |\n| This is alpha software. Use at your own discretion! |\n| Much is missing or lacking polish. There are bugs. |\n| Not yet secure. Read the security notes for more. |\n -------------------------------------------------------\n\nCheck out some of the other files in this directory:\n\n ./about\n ./help\n ./quick-start <-- usage examples\n ./readme <-- this file\n ./security-notes\n" response = api.cat(hash) response = response.decode() #print(response) self.assertEqual(response, response_must_be)
def __init__(self, pin_on_add=True, **kwargs): import ipfsapi self.ipfs_args = kwargs self.pin_on_add = pin_on_add self.api = ipfsapi.connect(**self.ipfs_args)
def _get_ipfs_client(self): ipfs_endpoint = urlparse(self.config.get_ipfs_endpoint()) ipfs_scheme = ipfs_endpoint.scheme if ipfs_endpoint.scheme else "http" ipfs_port = ipfs_endpoint.port if ipfs_endpoint.port else 5001 return ipfsapi.connect(urljoin(ipfs_scheme, ipfs_endpoint.hostname), ipfs_port)
inheaders = 0 if not isinstance(data, str): # Avoid spurious 'str on bytes instance' warning. line = repr(line) output.append(line) filename = str(time()) + '_' + str(uuid.uuid4()) print("Filename generated: {0}".format(filename)) file_path = os.path.join(self.path, filename) print("Filename with full path: {0}".format(file_path)) f = open(file_path, 'w') f.write('\n'.join(output)) f.close() print("File successful saved.") file_hash = self._add_to_ipfs(file_path) print("File added to IPFS with hash: {}".format(file_hash)) if __name__ == '__main__': server = SMTP2FileServer(('127.0.0.1', 1025), None) print('Connecting to IPFS daemon...') ipfs_server = ipfsapi.connect('127.0.0.1', 5001) print("Listening to port 1025...") asyncore.loop()