-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreator.py
More file actions
106 lines (95 loc) · 3.78 KB
/
Copy pathcreator.py
File metadata and controls
106 lines (95 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import pickle
import requests
import os
import ast
import rsa
import time
class IPFS_Database:
#ATTENTION! Before you input dict, be sure to check the indents!!! (Example: {"mario": 2, "sergo": "vanila"})
@classmethod
def write_to_bytes(self, dict: dict):
self.file = 'buffer.bin'
with open(self.file, 'wb') as db:
content, pk = self.__rsa_encrypt(dict)
db.write(content)
print("File has been completed")
return pk
#Add data to IPFS
def add_data_to_ipfs(self, privatk):
with open(self.file, 'rb') as txt:
text_binary = txt.read()
ipfs_url = "http://127.0.0.1:5001"
endpoint = "/api/v0/add"
response = requests.post(ipfs_url + endpoint, files={"file": text_binary})
ipfs_hash = response.json()["Hash"]
print("File has been added to IPFS")
self._add_pp_to_pickle(ipfs_hash, privatk)
os.remove(self.file)
return ipfs_hash
#Geting data from IPFS
def get_data_from_ipfs(self, hash_ipfs, pk, first_load=False):
try:
if first_load is True:
time.sleep(30)
while descrypted_dict is None:
url = f'https://ipfs.io/ipfs/{hash_ipfs}'
answer = requests.get(url)
crypted_text = answer.content
descrypted_text = self.__rsa_descrypt(crypted_text, pk)
descrypted_dict = ast.literal_eval(descrypted_text)
print("Descryption has been succesfull")
return descrypted_dict
except Exception:
print("Code 504. Try again...")
self.get_data_from_ipfs(hash_ipfs, pk)
#Changing data in IPFS (In fact, we delete the previous file with the connection key and create a new one, then adding new to IPFS)
def change_data_in_ipfs(self, ipfs_hash, dict: dict):
self._get_pk_from_pickle(ipfs_hash, delete=True)
(priv_k, file) = self.write_to_bytes(dict)
(hash_ipfs) = self.add_data_to_ipfs(file)
self._add_pp_to_pickle(hash_ipfs, priv_k)
print("Changing data in ipfs has been completed")
#Saving keys under hash name in "pickles" folder
@classmethod
def _add_pp_to_pickle(cls, ipfs_hash, pp):
folder = os.listdir()
i = "pickles"
if i in folder:
with open(f'pickles/{ipfs_hash}', 'wb') as f:
pickle.dump(pp, f)
else:
os.mkdir("pickles")
with open(f'pickles/{ipfs_hash}', 'wb') as f:
pickle.dump(pp, f)
print("Private key was loaded in file 'pickles'")
#Extracting the private key by its hash in the "pickles" folder
@classmethod
def _get_pk_from_pickle(cls, hash_ipfs, delete=False):
if delete is True:
os.remove(f"./pickles/{hash_ipfs}")
else:
with open(f"./pickles/{hash_ipfs}", "rb") as g:
pk = pickle.load(g)
print("Private key was executed")
return pk
#Encrypting informatinon before sending to IPFS
@staticmethod
def __rsa_encrypt(text1):
(public_key, private_key) = rsa.newkeys(2048)
new_str = str(text1)
text2 = new_str.encode('utf-8')
crypto = rsa.encrypt(text2, public_key)
return crypto, private_key
#Descrypting information after response from IPFS
@staticmethod
def __rsa_descrypt(text, pk):
text = rsa.decrypt(text, pk)
cont = text.decode('utf-8')
return cont
if __name__ == '__main__':
ddb = IPFS_Database()
privk = ddb.write_to_bytes({"Mark Lukan": 60, "Vizimir Zalas": 11}) #Input your dict!
hash_ipfs = ddb.add_data_to_ipfs(privk)
pk = ddb._get_pk_from_pickle(hash_ipfs)
text = ddb.get_data_from_ipfs(hash_ipfs, pk)
print(text)