neuronum 2.0.8__py3-none-any.whl → 3.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of neuronum might be problematic. Click here for more details.
- cli/main.py +284 -158
- neuronum/neuronum.py +393 -349
- neuronum-3.0.1.dist-info/METADATA +120 -0
- neuronum-3.0.1.dist-info/RECORD +12 -0
- neuronum-2.0.8.dist-info/METADATA +0 -385
- neuronum-2.0.8.dist-info/RECORD +0 -12
- {neuronum-2.0.8.dist-info → neuronum-3.0.1.dist-info}/WHEEL +0 -0
- {neuronum-2.0.8.dist-info → neuronum-3.0.1.dist-info}/entry_points.txt +0 -0
- {neuronum-2.0.8.dist-info → neuronum-3.0.1.dist-info}/licenses/LICENSE +0 -0
- {neuronum-2.0.8.dist-info → neuronum-3.0.1.dist-info}/top_level.txt +0 -0
neuronum/neuronum.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
from typing import Optional, Generator
|
|
1
|
+
import aiohttp
|
|
2
|
+
from typing import Optional, AsyncGenerator
|
|
4
3
|
import ssl
|
|
5
|
-
|
|
4
|
+
import websockets
|
|
6
5
|
import json
|
|
6
|
+
import asyncio
|
|
7
|
+
from bleak import BleakScanner
|
|
7
8
|
|
|
8
9
|
class Cell:
|
|
9
10
|
def __init__(self, host: str, password: str, network: str, synapse: str):
|
|
@@ -11,7 +12,7 @@ class Cell:
|
|
|
11
12
|
self.password = password
|
|
12
13
|
self.network = network
|
|
13
14
|
self.synapse = synapse
|
|
14
|
-
self.
|
|
15
|
+
self.queue = asyncio.Queue()
|
|
15
16
|
|
|
16
17
|
def to_dict(self) -> dict:
|
|
17
18
|
return {
|
|
@@ -22,17 +23,9 @@ class Cell:
|
|
|
22
23
|
|
|
23
24
|
def __repr__(self) -> str:
|
|
24
25
|
return f"Cell(host={self.host}, password={self.password}, network={self.network}, synapse={self.synapse})"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
def authenticate(self, stx: Optional[str] = None):
|
|
28
|
-
credentials = f"{self.host}\n{self.password}\n{self.synapse}\n{stx}\n"
|
|
29
|
-
self.sock.sendall(credentials.encode('utf-8'))
|
|
30
|
-
|
|
31
|
-
response = self.sock.recv(1024).decode('utf-8')
|
|
32
|
-
return "Authentication successful" in response
|
|
33
|
-
|
|
26
|
+
|
|
34
27
|
|
|
35
|
-
def create_tx(self, descr: str, key_values: dict, stx: str, label: str, partners: list):
|
|
28
|
+
async def create_tx(self, descr: str, key_values: dict, stx: str, label: str, partners: list):
|
|
36
29
|
url = f"https://{self.network}/api/create_tx"
|
|
37
30
|
|
|
38
31
|
TX = {
|
|
@@ -44,23 +37,20 @@ class Cell:
|
|
|
44
37
|
"cell": self.to_dict()
|
|
45
38
|
}
|
|
46
39
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
url,
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
response.raise_for_status()
|
|
54
|
-
|
|
55
|
-
return response.json()["txID"]
|
|
40
|
+
async with aiohttp.ClientSession() as session:
|
|
41
|
+
try:
|
|
42
|
+
async with session.post(url, json=TX) as response:
|
|
43
|
+
response.raise_for_status()
|
|
44
|
+
data = await response.json()
|
|
45
|
+
return data["txID"]
|
|
56
46
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
47
|
+
except aiohttp.ClientError as e:
|
|
48
|
+
print(f"Error sending request: {e}")
|
|
49
|
+
except Exception as e:
|
|
50
|
+
print(f"Unexpected error: {e}")
|
|
61
51
|
|
|
62
52
|
|
|
63
|
-
def delete_tx(self, txID: str):
|
|
53
|
+
async def delete_tx(self, txID: str):
|
|
64
54
|
url = f"https://{self.network}/api/delete_tx"
|
|
65
55
|
|
|
66
56
|
TX = {
|
|
@@ -68,23 +58,21 @@ class Cell:
|
|
|
68
58
|
"cell": self.to_dict()
|
|
69
59
|
}
|
|
70
60
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
url,
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
print(f"Response from Neuronum: {response.json()}")
|
|
61
|
+
async with aiohttp.ClientSession() as session:
|
|
62
|
+
try:
|
|
63
|
+
async with session.post(url, json=TX) as response:
|
|
64
|
+
response.raise_for_status()
|
|
65
|
+
data = await response.json()
|
|
66
|
+
print(f"Response from Neuronum: {data}")
|
|
67
|
+
return data
|
|
80
68
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
69
|
+
except aiohttp.ClientError as e:
|
|
70
|
+
print(f"Error sending request: {e}")
|
|
71
|
+
except Exception as e:
|
|
72
|
+
print(f"Unexpected error: {e}")
|
|
85
73
|
|
|
86
74
|
|
|
87
|
-
def activate_tx(self, txID: str, data: dict):
|
|
75
|
+
async def activate_tx(self, txID: str, data: dict):
|
|
88
76
|
url = f"https://{self.network}/api/activate_tx/{txID}"
|
|
89
77
|
|
|
90
78
|
TX = {
|
|
@@ -92,23 +80,21 @@ class Cell:
|
|
|
92
80
|
"cell": self.to_dict()
|
|
93
81
|
}
|
|
94
82
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
url,
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
print(f"Response from Neuronum: {response.json()}")
|
|
83
|
+
async with aiohttp.ClientSession() as session:
|
|
84
|
+
try:
|
|
85
|
+
async with session.post(url, json=TX) as response:
|
|
86
|
+
response.raise_for_status()
|
|
87
|
+
response.raise_for_status()
|
|
88
|
+
data = await response.json()
|
|
89
|
+
print(data["message"])
|
|
104
90
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
91
|
+
except aiohttp.ClientError as e:
|
|
92
|
+
print(f"Error sending request: {e}")
|
|
93
|
+
except Exception as e:
|
|
94
|
+
print(f"Unexpected error: {e}")
|
|
109
95
|
|
|
110
96
|
|
|
111
|
-
def create_ctx(self, descr: str, partners: list):
|
|
97
|
+
async def create_ctx(self, descr: str, partners: list):
|
|
112
98
|
url = f"https://{self.network}/api/create_ctx"
|
|
113
99
|
|
|
114
100
|
CTX = {
|
|
@@ -117,23 +103,20 @@ class Cell:
|
|
|
117
103
|
"cell": self.to_dict()
|
|
118
104
|
}
|
|
119
105
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
url,
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
response.raise_for_status()
|
|
127
|
-
|
|
128
|
-
return response.json()["ctxID"]
|
|
106
|
+
async with aiohttp.ClientSession() as session:
|
|
107
|
+
try:
|
|
108
|
+
async with session.post(url, json=CTX) as response:
|
|
109
|
+
response.raise_for_status()
|
|
110
|
+
data = await response.json()
|
|
111
|
+
return data["ctxID"]
|
|
129
112
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
113
|
+
except aiohttp.ClientError as e:
|
|
114
|
+
print(f"Error sending request: {e}")
|
|
115
|
+
except Exception as e:
|
|
116
|
+
print(f"Unexpected error: {e}")
|
|
134
117
|
|
|
135
118
|
|
|
136
|
-
def delete_ctx(self, ctxID: str):
|
|
119
|
+
async def delete_ctx(self, ctxID: str):
|
|
137
120
|
url = f"https://{self.network}/api/delete_ctx"
|
|
138
121
|
|
|
139
122
|
CTX = {
|
|
@@ -141,23 +124,21 @@ class Cell:
|
|
|
141
124
|
"cell": self.to_dict()
|
|
142
125
|
}
|
|
143
126
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
url,
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
print(f"Response from Neuronum: {response.json()}")
|
|
127
|
+
async with aiohttp.ClientSession() as session:
|
|
128
|
+
try:
|
|
129
|
+
async with session.post(url, json=CTX) as response:
|
|
130
|
+
response.raise_for_status()
|
|
131
|
+
data = await response.json()
|
|
132
|
+
print(f"Response from Neuronum: {data}")
|
|
133
|
+
return data
|
|
153
134
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
135
|
+
except aiohttp.ClientError as e:
|
|
136
|
+
print(f"Error sending request: {e}")
|
|
137
|
+
except Exception as e:
|
|
138
|
+
print(f"Unexpected error: {e}")
|
|
158
139
|
|
|
159
140
|
|
|
160
|
-
def create_stx(self, descr: str, partners: list):
|
|
141
|
+
async def create_stx(self, descr: str, partners: list):
|
|
161
142
|
url = f"https://{self.network}/api/create_stx"
|
|
162
143
|
|
|
163
144
|
STX = {
|
|
@@ -166,23 +147,20 @@ class Cell:
|
|
|
166
147
|
"cell": self.to_dict()
|
|
167
148
|
}
|
|
168
149
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
url,
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
response.raise_for_status()["stxID"]
|
|
176
|
-
|
|
177
|
-
return response.json()
|
|
150
|
+
async with aiohttp.ClientSession() as session:
|
|
151
|
+
try:
|
|
152
|
+
async with session.post(url, json=STX) as response:
|
|
153
|
+
response.raise_for_status()
|
|
154
|
+
data = await response.json()
|
|
155
|
+
return data["stxID"]
|
|
178
156
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
157
|
+
except aiohttp.ClientError as e:
|
|
158
|
+
print(f"Error sending request: {e}")
|
|
159
|
+
except Exception as e:
|
|
160
|
+
print(f"Unexpected error: {e}")
|
|
183
161
|
|
|
184
162
|
|
|
185
|
-
def delete_stx(self, stxID: str):
|
|
163
|
+
async def delete_stx(self, stxID: str):
|
|
186
164
|
url = f"https://{self.network}/api/delete_stx"
|
|
187
165
|
|
|
188
166
|
STX = {
|
|
@@ -190,213 +168,236 @@ class Cell:
|
|
|
190
168
|
"cell": self.to_dict()
|
|
191
169
|
}
|
|
192
170
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
url,
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
print(f"Response from Neuronum: {response.json()}")
|
|
171
|
+
async with aiohttp.ClientSession() as session:
|
|
172
|
+
try:
|
|
173
|
+
async with session.post(url, json=STX) as response:
|
|
174
|
+
response.raise_for_status()
|
|
175
|
+
data = await response.json()
|
|
176
|
+
print(f"Response from Neuronum: {data}")
|
|
177
|
+
return data
|
|
202
178
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
179
|
+
except aiohttp.ClientError as e:
|
|
180
|
+
print(f"Error sending request: {e}")
|
|
181
|
+
except Exception as e:
|
|
182
|
+
print(f"Unexpected error: {e}")
|
|
207
183
|
|
|
208
184
|
|
|
209
|
-
def list_cells(self):
|
|
185
|
+
async def list_cells(self):
|
|
210
186
|
full_url = f"https://{self.network}/api/list_cells"
|
|
211
|
-
|
|
212
|
-
|
|
187
|
+
|
|
188
|
+
list_cells_payload = {
|
|
213
189
|
"cell": self.to_dict()
|
|
214
190
|
}
|
|
215
191
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
192
|
+
async with aiohttp.ClientSession() as session:
|
|
193
|
+
try:
|
|
194
|
+
async with session.get(full_url, json=list_cells_payload) as response:
|
|
195
|
+
response.raise_for_status()
|
|
196
|
+
data = await response.json()
|
|
197
|
+
return data.get("Cells", [])
|
|
198
|
+
|
|
199
|
+
except aiohttp.ClientError as e:
|
|
200
|
+
print(f"Error sending request: {e}")
|
|
201
|
+
except Exception as e:
|
|
202
|
+
print(f"Unexpected error: {e}")
|
|
224
203
|
|
|
225
204
|
|
|
226
|
-
def list_tx(self):
|
|
205
|
+
async def list_tx(self):
|
|
227
206
|
full_url = f"https://{self.network}/api/list_tx"
|
|
228
|
-
|
|
229
|
-
|
|
207
|
+
|
|
208
|
+
list_tx_payload = {
|
|
230
209
|
"cell": self.to_dict()
|
|
231
210
|
}
|
|
232
211
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
212
|
+
async with aiohttp.ClientSession() as session:
|
|
213
|
+
try:
|
|
214
|
+
async with session.get(full_url, json=list_tx_payload) as response:
|
|
215
|
+
response.raise_for_status()
|
|
216
|
+
data = await response.json()
|
|
217
|
+
return data.get("Transmitters", [])
|
|
218
|
+
|
|
219
|
+
except aiohttp.ClientError as e:
|
|
220
|
+
print(f"Error sending request: {e}")
|
|
221
|
+
except Exception as e:
|
|
222
|
+
print(f"Unexpected error: {e}")
|
|
241
223
|
|
|
242
224
|
|
|
243
|
-
def list_ctx(self):
|
|
225
|
+
async def list_ctx(self):
|
|
244
226
|
full_url = f"https://{self.network}/api/list_ctx"
|
|
245
|
-
|
|
246
|
-
|
|
227
|
+
|
|
228
|
+
list_ctx_payload = {
|
|
247
229
|
"cell": self.to_dict()
|
|
248
230
|
}
|
|
249
231
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
except Exception as e:
|
|
257
|
-
print(f"Unexpected error: {e}")
|
|
232
|
+
async with aiohttp.ClientSession() as session:
|
|
233
|
+
try:
|
|
234
|
+
async with session.get(full_url, json=list_ctx_payload) as response:
|
|
235
|
+
response.raise_for_status()
|
|
236
|
+
data = await response.json()
|
|
237
|
+
return data.get("Circuits", [])
|
|
258
238
|
|
|
239
|
+
except aiohttp.ClientError as e:
|
|
240
|
+
print(f"Error sending request: {e}")
|
|
241
|
+
except Exception as e:
|
|
242
|
+
print(f"Unexpected error: {e}")
|
|
259
243
|
|
|
260
|
-
|
|
244
|
+
|
|
245
|
+
async def list_stx(self):
|
|
261
246
|
full_url = f"https://{self.network}/api/list_stx"
|
|
262
|
-
|
|
263
|
-
|
|
247
|
+
|
|
248
|
+
list_stx_payload = {
|
|
264
249
|
"cell": self.to_dict()
|
|
265
250
|
}
|
|
266
251
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
except Exception as e:
|
|
274
|
-
print(f"Unexpected error: {e}")
|
|
252
|
+
async with aiohttp.ClientSession() as session:
|
|
253
|
+
try:
|
|
254
|
+
async with session.get(full_url, json=list_stx_payload) as response:
|
|
255
|
+
response.raise_for_status()
|
|
256
|
+
data = await response.json()
|
|
257
|
+
return data.get("Streams", [])
|
|
275
258
|
|
|
259
|
+
except aiohttp.ClientError as e:
|
|
260
|
+
print(f"Error sending request: {e}")
|
|
261
|
+
except Exception as e:
|
|
262
|
+
print(f"Unexpected error: {e}")
|
|
276
263
|
|
|
277
|
-
|
|
264
|
+
|
|
265
|
+
async def list_nodes(self):
|
|
278
266
|
full_url = f"https://{self.network}/api/list_nodes"
|
|
279
|
-
|
|
280
|
-
|
|
267
|
+
|
|
268
|
+
list_nodes_payload = {
|
|
281
269
|
"cell": self.to_dict()
|
|
282
270
|
}
|
|
283
271
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
272
|
+
async with aiohttp.ClientSession() as session:
|
|
273
|
+
try:
|
|
274
|
+
async with session.get(full_url, json=list_nodes_payload) as response:
|
|
275
|
+
response.raise_for_status()
|
|
276
|
+
data = await response.json()
|
|
277
|
+
return data.get("Nodes", [])
|
|
278
|
+
|
|
279
|
+
except aiohttp.ClientError as e:
|
|
280
|
+
print(f"Error sending request: {e}")
|
|
281
|
+
except Exception as e:
|
|
282
|
+
print(f"Unexpected error: {e}")
|
|
292
283
|
|
|
293
284
|
|
|
294
|
-
def store(self, label: str, data: dict, ctx: Optional[str] = None):
|
|
295
|
-
if ctx
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
full_url = f"https://{self.network}/api/store"
|
|
299
|
-
|
|
300
|
-
store = {
|
|
285
|
+
async def store(self, label: str, data: dict, ctx: Optional[str] = None):
|
|
286
|
+
full_url = f"https://{self.network}/api/store_in_ctx/{ctx}" if ctx else f"https://{self.network}/api/store"
|
|
287
|
+
|
|
288
|
+
store_payload = {
|
|
301
289
|
"label": label,
|
|
302
290
|
"data": data,
|
|
303
|
-
"cell": self.to_dict()
|
|
291
|
+
"cell": self.to_dict()
|
|
304
292
|
}
|
|
305
293
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
294
|
+
async with aiohttp.ClientSession() as session:
|
|
295
|
+
try:
|
|
296
|
+
async with session.post(full_url, json=store_payload) as response:
|
|
297
|
+
response.raise_for_status()
|
|
298
|
+
data = await response.json()
|
|
299
|
+
print(f"Response from Neuronum: {data}")
|
|
300
|
+
return data
|
|
301
|
+
|
|
302
|
+
except aiohttp.ClientError as e:
|
|
303
|
+
print(f"Error sending request: {e}")
|
|
304
|
+
except Exception as e:
|
|
305
|
+
print(f"Unexpected error: {e}")
|
|
314
306
|
|
|
315
307
|
|
|
316
|
-
def load(self, label: str, ctx: Optional[str] = None):
|
|
317
|
-
if ctx
|
|
318
|
-
full_url = f"https://{self.network}/api/load_from_ctx/{ctx}"
|
|
319
|
-
else:
|
|
320
|
-
full_url = f"https://{self.network}/api/load"
|
|
308
|
+
async def load(self, label: str, ctx: Optional[str] = None):
|
|
309
|
+
full_url = f"https://{self.network}/api/load_from_ctx/{ctx}" if ctx else f"https://{self.network}/api/load"
|
|
321
310
|
|
|
322
|
-
|
|
311
|
+
load_payload = {
|
|
323
312
|
"label": label,
|
|
324
|
-
"cell": self.to_dict()
|
|
313
|
+
"cell": self.to_dict()
|
|
325
314
|
}
|
|
326
315
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
except Exception as e:
|
|
334
|
-
print(f"Unexpected error: {e}")
|
|
316
|
+
async with aiohttp.ClientSession() as session:
|
|
317
|
+
try:
|
|
318
|
+
async with session.post(full_url, json=load_payload) as response:
|
|
319
|
+
response.raise_for_status()
|
|
320
|
+
data = await response.json()
|
|
321
|
+
return data
|
|
335
322
|
|
|
323
|
+
except aiohttp.ClientError as e:
|
|
324
|
+
print(f"Error sending request: {e}")
|
|
325
|
+
except Exception as e:
|
|
326
|
+
print(f"Unexpected error: {e}")
|
|
336
327
|
|
|
337
|
-
def delete(self, label: str, ctx: Optional[str] = None):
|
|
338
|
-
if ctx:
|
|
339
|
-
full_url = f"https://{self.network}/api/delete_from_ctx/{ctx}"
|
|
340
|
-
else:
|
|
341
|
-
full_url = f"https://{self.network}/api/delete"
|
|
342
328
|
|
|
343
|
-
|
|
329
|
+
async def delete(self, label: str, ctx: Optional[str] = None):
|
|
330
|
+
full_url = f"https://{self.network}/api/delete_from_ctx/{ctx}" if ctx else f"https://{self.network}/api/delete"
|
|
331
|
+
|
|
332
|
+
delete_payload = {
|
|
344
333
|
"label": label,
|
|
345
|
-
"cell": self.to_dict()
|
|
334
|
+
"cell": self.to_dict()
|
|
346
335
|
}
|
|
347
336
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
337
|
+
async with aiohttp.ClientSession() as session:
|
|
338
|
+
try:
|
|
339
|
+
async with session.post(full_url, json=delete_payload) as response:
|
|
340
|
+
response.raise_for_status()
|
|
341
|
+
data = await response.json()
|
|
342
|
+
print(f"Response from Neuronum: {data}")
|
|
343
|
+
return data
|
|
344
|
+
|
|
345
|
+
except aiohttp.ClientError as e:
|
|
346
|
+
print(f"Error sending request: {e}")
|
|
347
|
+
except Exception as e:
|
|
348
|
+
print(f"Unexpected error: {e}")
|
|
356
349
|
|
|
357
350
|
|
|
358
|
-
def clear(self, ctx: Optional[str] = None):
|
|
359
|
-
if ctx
|
|
360
|
-
full_url = f"https://{self.network}/api/clear_ctx/{ctx}"
|
|
361
|
-
else:
|
|
362
|
-
full_url = f"https://{self.network}/api/clear"
|
|
351
|
+
async def clear(self, ctx: Optional[str] = None):
|
|
352
|
+
full_url = f"https://{self.network}/api/clear_ctx/{ctx}" if ctx else f"https://{self.network}/api/clear"
|
|
363
353
|
|
|
364
|
-
|
|
365
|
-
"cell": self.to_dict()
|
|
354
|
+
clear_payload = {
|
|
355
|
+
"cell": self.to_dict()
|
|
366
356
|
}
|
|
367
357
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
358
|
+
async with aiohttp.ClientSession() as session:
|
|
359
|
+
try:
|
|
360
|
+
async with session.post(full_url, json=clear_payload) as response:
|
|
361
|
+
response.raise_for_status()
|
|
362
|
+
data = await response.json()
|
|
363
|
+
print(f"Response from Neuronum: {data}")
|
|
364
|
+
return data
|
|
365
|
+
|
|
366
|
+
except aiohttp.ClientError as e:
|
|
367
|
+
print(f"Error sending request: {e}")
|
|
368
|
+
except Exception as e:
|
|
369
|
+
print(f"Unexpected error: {e}")
|
|
376
370
|
|
|
377
371
|
|
|
378
|
-
def stream(self, label: str, data: dict, stx: Optional[str] = None):
|
|
372
|
+
async def stream(self, label: str, data: dict, stx: Optional[str] = None):
|
|
379
373
|
context = ssl.create_default_context()
|
|
380
374
|
context.check_hostname = True
|
|
381
375
|
context.verify_mode = ssl.CERT_REQUIRED
|
|
382
376
|
|
|
383
|
-
raw_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
384
|
-
self.sock = context.wrap_socket(raw_sock, server_hostname=self.network)
|
|
385
|
-
|
|
386
377
|
try:
|
|
387
|
-
|
|
378
|
+
reader, writer = await asyncio.open_connection(self.network, 55555, ssl=context, server_hostname=self.network)
|
|
379
|
+
|
|
380
|
+
credentials = f"{self.host}\n{self.password}\n{self.synapse}\n{stx}\n"
|
|
381
|
+
writer.write(credentials.encode("utf-8"))
|
|
382
|
+
await writer.drain()
|
|
388
383
|
|
|
389
|
-
|
|
390
|
-
|
|
384
|
+
response = await reader.read(1024)
|
|
385
|
+
response_text = response.decode("utf-8")
|
|
386
|
+
|
|
387
|
+
if "Authentication successful" not in response_text:
|
|
388
|
+
print("Authentication failed")
|
|
389
|
+
writer.close()
|
|
390
|
+
await writer.wait_closed()
|
|
391
391
|
return
|
|
392
392
|
|
|
393
|
-
|
|
393
|
+
stream_payload = {
|
|
394
394
|
"label": label,
|
|
395
395
|
"data": data,
|
|
396
396
|
}
|
|
397
397
|
|
|
398
|
-
|
|
399
|
-
|
|
398
|
+
writer.write(json.dumps(stream_payload).encode("utf-8"))
|
|
399
|
+
await writer.drain()
|
|
400
|
+
print(f"Sent: {stream_payload}")
|
|
400
401
|
|
|
401
402
|
except ssl.SSLError as e:
|
|
402
403
|
print(f"SSL error occurred: {e}")
|
|
@@ -405,174 +406,217 @@ class Cell:
|
|
|
405
406
|
print(f"An unexpected error occurred: {e}")
|
|
406
407
|
|
|
407
408
|
finally:
|
|
408
|
-
|
|
409
|
+
writer.close()
|
|
410
|
+
await writer.wait_closed()
|
|
411
|
+
|
|
409
412
|
|
|
410
413
|
|
|
411
|
-
def sync(self, stx: Optional[str] = None) ->
|
|
412
|
-
|
|
414
|
+
async def sync(self, stx: Optional[str] = None) -> AsyncGenerator[str, None]:
|
|
415
|
+
full_url = f"wss://{self.network}/sync/{stx}"
|
|
416
|
+
|
|
417
|
+
auth_payload = {
|
|
413
418
|
"host": self.host,
|
|
414
419
|
"password": self.password,
|
|
415
420
|
"synapse": self.synapse,
|
|
416
421
|
}
|
|
417
422
|
|
|
418
423
|
try:
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
424
|
+
async with websockets.connect(full_url) as ws:
|
|
425
|
+
await ws.send(json.dumps(auth_payload))
|
|
426
|
+
print("Listening to Stream...")
|
|
422
427
|
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
print("Stream-Synchronization ended!")
|
|
443
|
-
ws.close()
|
|
444
|
-
print("Connection closed. Goodbye!")
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
def sign_contract(self, contractID: str):
|
|
448
|
-
full_url = f"https://{self.network}/api/sign_contract"
|
|
449
|
-
|
|
450
|
-
sign_contract = {
|
|
451
|
-
"contractID": contractID,
|
|
452
|
-
"cell": self.to_dict()
|
|
453
|
-
}
|
|
428
|
+
try:
|
|
429
|
+
while True:
|
|
430
|
+
try:
|
|
431
|
+
raw_operation = await ws.recv()
|
|
432
|
+
operation = json.loads(raw_operation)
|
|
433
|
+
yield operation
|
|
434
|
+
|
|
435
|
+
except asyncio.TimeoutError:
|
|
436
|
+
print("No initial data received. Continuing to listen...")
|
|
437
|
+
continue
|
|
438
|
+
|
|
439
|
+
except asyncio.CancelledError:
|
|
440
|
+
print("Connection closed.")
|
|
441
|
+
|
|
442
|
+
except websockets.exceptions.WebSocketException as e:
|
|
443
|
+
print(f"WebSocket error occurred: {e}")
|
|
444
|
+
|
|
445
|
+
except Exception as e:
|
|
446
|
+
print(f"An unexpected error occurred: {e}")
|
|
454
447
|
|
|
448
|
+
|
|
449
|
+
async def sign_contract(self, contractID: str):
|
|
450
|
+
full_url = f"https://{self.network}/api/sign_contract"
|
|
451
|
+
|
|
452
|
+
sign_contract_payload = {
|
|
453
|
+
"contractID": contractID,
|
|
454
|
+
"cell": self.to_dict()
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
async with aiohttp.ClientSession() as session:
|
|
455
458
|
try:
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
459
|
+
async with session.post(full_url, json=sign_contract_payload) as response:
|
|
460
|
+
response.raise_for_status()
|
|
461
|
+
data = await response.json()
|
|
462
|
+
return data.get("token")
|
|
463
|
+
|
|
464
|
+
except aiohttp.ClientError as e:
|
|
460
465
|
print(f"Error sending request: {e}")
|
|
461
466
|
except Exception as e:
|
|
462
467
|
print(f"Unexpected error: {e}")
|
|
463
468
|
|
|
464
469
|
|
|
465
|
-
def validate_token(self, token: str, cp: str, contractID: str):
|
|
470
|
+
async def validate_token(self, token: str, cp: str, contractID: str):
|
|
466
471
|
full_url = f"https://{self.network}/api/validate_token"
|
|
467
|
-
|
|
468
|
-
|
|
472
|
+
|
|
473
|
+
validate_payload = {
|
|
469
474
|
"token": token,
|
|
470
475
|
"cp": cp,
|
|
471
476
|
"contractID": contractID,
|
|
472
|
-
"cell": self.to_dict()
|
|
477
|
+
"cell": self.to_dict()
|
|
473
478
|
}
|
|
474
479
|
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
except Exception as e:
|
|
482
|
-
print(f"Unexpected error: {e}")
|
|
480
|
+
async with aiohttp.ClientSession() as session:
|
|
481
|
+
try:
|
|
482
|
+
async with session.post(full_url, json=validate_payload) as response:
|
|
483
|
+
response.raise_for_status()
|
|
484
|
+
data = await response.json()
|
|
485
|
+
return data.get("validity")
|
|
483
486
|
|
|
487
|
+
except aiohttp.ClientError as e:
|
|
488
|
+
print(f"Error sending request: {e}")
|
|
489
|
+
except Exception as e:
|
|
490
|
+
print(f"Unexpected error: {e}")
|
|
484
491
|
|
|
485
|
-
|
|
492
|
+
|
|
493
|
+
async def request_token(self, cp: str, contractID: str):
|
|
486
494
|
full_url = f"https://{self.network}/api/request_token"
|
|
487
|
-
|
|
488
|
-
|
|
495
|
+
|
|
496
|
+
request_token_payload = {
|
|
489
497
|
"cp": cp,
|
|
490
498
|
"contractID": contractID,
|
|
491
|
-
"cell": self.to_dict()
|
|
499
|
+
"cell": self.to_dict()
|
|
492
500
|
}
|
|
493
501
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
+
async with aiohttp.ClientSession() as session:
|
|
503
|
+
try:
|
|
504
|
+
async with session.post(full_url, json=request_token_payload) as response:
|
|
505
|
+
response.raise_for_status()
|
|
506
|
+
data = await response.json()
|
|
507
|
+
print(f"Response from Neuronum: {data}")
|
|
508
|
+
return data
|
|
509
|
+
|
|
510
|
+
except aiohttp.ClientError as e:
|
|
511
|
+
print(f"Error sending request: {e}")
|
|
512
|
+
except Exception as e:
|
|
513
|
+
print(f"Unexpected error: {e}")
|
|
502
514
|
|
|
503
515
|
|
|
504
|
-
def present_token(self, token: str, cp: str, contractID: str):
|
|
516
|
+
async def present_token(self, token: str, cp: str, contractID: str):
|
|
505
517
|
full_url = f"https://{self.network}/api/present_token"
|
|
506
|
-
|
|
507
|
-
|
|
518
|
+
|
|
519
|
+
present_token_payload = {
|
|
508
520
|
"token": token,
|
|
509
521
|
"cp": cp,
|
|
510
522
|
"contractID": contractID,
|
|
511
|
-
"cell": self.to_dict()
|
|
523
|
+
"cell": self.to_dict()
|
|
512
524
|
}
|
|
513
525
|
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
526
|
+
async with aiohttp.ClientSession() as session:
|
|
527
|
+
try:
|
|
528
|
+
async with session.post(full_url, json=present_token_payload) as response:
|
|
529
|
+
response.raise_for_status()
|
|
530
|
+
data = await response.json()
|
|
531
|
+
print(f"Response from Neuronum: {data}")
|
|
532
|
+
return data
|
|
533
|
+
|
|
534
|
+
except aiohttp.ClientError as e:
|
|
535
|
+
print(f"Error sending request: {e}")
|
|
536
|
+
except Exception as e:
|
|
537
|
+
print(f"Unexpected error: {e}")
|
|
522
538
|
|
|
523
539
|
|
|
524
|
-
def create_contract(self, descr: str, details: dict, partners: list):
|
|
540
|
+
async def create_contract(self, descr: str, details: dict, partners: list):
|
|
525
541
|
full_url = f"https://{self.network}/api/create_contract"
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
"cell": self.to_dict(),
|
|
542
|
+
|
|
543
|
+
create_contract_payload = {
|
|
544
|
+
"cell": self.to_dict(),
|
|
529
545
|
"descr": descr,
|
|
530
546
|
"details": details,
|
|
531
547
|
"partners": partners
|
|
532
548
|
}
|
|
533
|
-
try:
|
|
534
|
-
response = requests.post(full_url, json=create_contract)
|
|
535
|
-
response.raise_for_status()
|
|
536
|
-
return response.json()["contractID"]
|
|
537
|
-
except requests.exceptions.RequestException as e:
|
|
538
|
-
print(f"Error sending request: {e}")
|
|
539
|
-
except Exception as e:
|
|
540
|
-
print(f"Unexpected error: {e}")
|
|
541
549
|
|
|
550
|
+
async with aiohttp.ClientSession() as session:
|
|
551
|
+
try:
|
|
552
|
+
async with session.post(full_url, json=create_contract_payload) as response:
|
|
553
|
+
response.raise_for_status()
|
|
554
|
+
data = await response.json()
|
|
555
|
+
return data.get("contractID")
|
|
542
556
|
|
|
543
|
-
|
|
557
|
+
except aiohttp.ClientError as e:
|
|
558
|
+
print(f"Error sending request: {e}")
|
|
559
|
+
except Exception as e:
|
|
560
|
+
print(f"Unexpected error: {e}")
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
async def delete_contract(self, contractID: str):
|
|
544
564
|
full_url = f"https://{self.network}/api/delete_contract"
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
"cell": self.to_dict(),
|
|
565
|
+
|
|
566
|
+
request_payload = {
|
|
567
|
+
"cell": self.to_dict(),
|
|
548
568
|
"contractID": contractID
|
|
549
569
|
}
|
|
550
570
|
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
571
|
+
async with aiohttp.ClientSession() as session:
|
|
572
|
+
try:
|
|
573
|
+
async with session.post(full_url, json=request_payload) as response:
|
|
574
|
+
response.raise_for_status()
|
|
575
|
+
data = await response.json()
|
|
576
|
+
print(f"Response from Neuronum: {data}")
|
|
577
|
+
return data
|
|
578
|
+
|
|
579
|
+
except aiohttp.ClientError as e:
|
|
580
|
+
print(f"Error sending request: {e}")
|
|
581
|
+
except Exception as e:
|
|
582
|
+
print(f"Unexpected error: {e}")
|
|
559
583
|
|
|
560
584
|
|
|
561
|
-
def list_contracts(self):
|
|
585
|
+
async def list_contracts(self):
|
|
562
586
|
full_url = f"https://{self.network}/api/list_contracts"
|
|
563
|
-
|
|
564
|
-
|
|
587
|
+
|
|
588
|
+
list_contracts_payload = {
|
|
565
589
|
"cell": self.to_dict()
|
|
566
590
|
}
|
|
567
591
|
|
|
592
|
+
async with aiohttp.ClientSession() as session:
|
|
593
|
+
try:
|
|
594
|
+
async with session.get(full_url, json=list_contracts_payload) as response:
|
|
595
|
+
response.raise_for_status()
|
|
596
|
+
data = await response.json()
|
|
597
|
+
return data.get("Contracts", [])
|
|
598
|
+
|
|
599
|
+
except aiohttp.ClientError as e:
|
|
600
|
+
print(f"Error sending request: {e}")
|
|
601
|
+
except Exception as e:
|
|
602
|
+
print(f"Unexpected error: {e}")
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
def device_found(self, device, advertisement_data):
|
|
606
|
+
if device.name and (device.name.endswith("::cell") or device.name.endswith("::node")):
|
|
607
|
+
asyncio.create_task(self.queue.put(f"{device.name} - {device.address}"))
|
|
608
|
+
|
|
609
|
+
async def scan(self):
|
|
610
|
+
print("Scanning for Neuronum Cells & Nodes")
|
|
611
|
+
|
|
612
|
+
scanner = BleakScanner(self.device_found)
|
|
613
|
+
await scanner.start()
|
|
614
|
+
|
|
568
615
|
try:
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
print(f"Error sending request: {e}")
|
|
574
|
-
except Exception as e:
|
|
575
|
-
print(f"Unexpected error: {e}")
|
|
616
|
+
while True:
|
|
617
|
+
yield await self.queue.get()
|
|
618
|
+
except asyncio.CancelledError:
|
|
619
|
+
await scanner.stop()
|
|
576
620
|
|
|
577
621
|
|
|
578
622
|
__all__ = ['Cell']
|