naeural-client 2.0.0__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.
- naeural_client/__init__.py +13 -0
- naeural_client/_ver.py +13 -0
- naeural_client/base/__init__.py +6 -0
- naeural_client/base/distributed_custom_code_presets.py +44 -0
- naeural_client/base/generic_session.py +1763 -0
- naeural_client/base/instance.py +616 -0
- naeural_client/base/payload/__init__.py +1 -0
- naeural_client/base/payload/payload.py +66 -0
- naeural_client/base/pipeline.py +1499 -0
- naeural_client/base/plugin_template.py +5209 -0
- naeural_client/base/responses.py +209 -0
- naeural_client/base/transaction.py +157 -0
- naeural_client/base_decentra_object.py +143 -0
- naeural_client/bc/__init__.py +3 -0
- naeural_client/bc/base.py +1046 -0
- naeural_client/bc/chain.py +0 -0
- naeural_client/bc/ec.py +324 -0
- naeural_client/certs/__init__.py +0 -0
- naeural_client/certs/r9092118.ala.eu-central-1.emqxsl.com.crt +22 -0
- naeural_client/code_cheker/__init__.py +1 -0
- naeural_client/code_cheker/base.py +520 -0
- naeural_client/code_cheker/checker.py +294 -0
- naeural_client/comm/__init__.py +2 -0
- naeural_client/comm/amqp_wrapper.py +338 -0
- naeural_client/comm/mqtt_wrapper.py +539 -0
- naeural_client/const/README.md +3 -0
- naeural_client/const/__init__.py +9 -0
- naeural_client/const/base.py +101 -0
- naeural_client/const/comms.py +80 -0
- naeural_client/const/environment.py +26 -0
- naeural_client/const/formatter.py +7 -0
- naeural_client/const/heartbeat.py +111 -0
- naeural_client/const/misc.py +20 -0
- naeural_client/const/payload.py +190 -0
- naeural_client/default/__init__.py +1 -0
- naeural_client/default/instance/__init__.py +4 -0
- naeural_client/default/instance/chain_dist_custom_job_01_plugin.py +54 -0
- naeural_client/default/instance/custom_web_app_01_plugin.py +118 -0
- naeural_client/default/instance/net_mon_01_plugin.py +45 -0
- naeural_client/default/instance/view_scene_01_plugin.py +28 -0
- naeural_client/default/session/mqtt_session.py +72 -0
- naeural_client/io_formatter/__init__.py +2 -0
- naeural_client/io_formatter/base/__init__.py +1 -0
- naeural_client/io_formatter/base/base_formatter.py +80 -0
- naeural_client/io_formatter/default/__init__.py +3 -0
- naeural_client/io_formatter/default/a_dummy.py +51 -0
- naeural_client/io_formatter/default/aixp1.py +113 -0
- naeural_client/io_formatter/default/default.py +22 -0
- naeural_client/io_formatter/io_formatter_manager.py +96 -0
- naeural_client/logging/__init__.py +1 -0
- naeural_client/logging/base_logger.py +2056 -0
- naeural_client/logging/logger_mixins/__init__.py +12 -0
- naeural_client/logging/logger_mixins/class_instance_mixin.py +92 -0
- naeural_client/logging/logger_mixins/computer_vision_mixin.py +443 -0
- naeural_client/logging/logger_mixins/datetime_mixin.py +344 -0
- naeural_client/logging/logger_mixins/download_mixin.py +421 -0
- naeural_client/logging/logger_mixins/general_serialization_mixin.py +242 -0
- naeural_client/logging/logger_mixins/json_serialization_mixin.py +481 -0
- naeural_client/logging/logger_mixins/pickle_serialization_mixin.py +301 -0
- naeural_client/logging/logger_mixins/process_mixin.py +63 -0
- naeural_client/logging/logger_mixins/resource_size_mixin.py +81 -0
- naeural_client/logging/logger_mixins/timers_mixin.py +501 -0
- naeural_client/logging/logger_mixins/upload_mixin.py +260 -0
- naeural_client/logging/logger_mixins/utils_mixin.py +675 -0
- naeural_client/logging/small_logger.py +93 -0
- naeural_client/logging/tzlocal/__init__.py +20 -0
- naeural_client/logging/tzlocal/unix.py +231 -0
- naeural_client/logging/tzlocal/utils.py +113 -0
- naeural_client/logging/tzlocal/win32.py +151 -0
- naeural_client/logging/tzlocal/windows_tz.py +718 -0
- naeural_client/plugins_manager_mixin.py +273 -0
- naeural_client/utils/__init__.py +2 -0
- naeural_client/utils/comm_utils.py +44 -0
- naeural_client/utils/dotenv.py +75 -0
- naeural_client-2.0.0.dist-info/METADATA +365 -0
- naeural_client-2.0.0.dist-info/RECORD +78 -0
- naeural_client-2.0.0.dist-info/WHEEL +4 -0
- naeural_client-2.0.0.dist-info/licenses/LICENSE +201 -0
File without changes
|
naeural_client/bc/ec.py
ADDED
@@ -0,0 +1,324 @@
|
|
1
|
+
import base64
|
2
|
+
import hashlib
|
3
|
+
import os
|
4
|
+
import binascii
|
5
|
+
|
6
|
+
from cryptography.hazmat.primitives import hashes
|
7
|
+
from cryptography.hazmat.primitives.asymmetric import ec
|
8
|
+
from cryptography.hazmat.primitives import serialization
|
9
|
+
from cryptography.hazmat.backends import default_backend
|
10
|
+
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
11
|
+
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
|
12
|
+
|
13
|
+
from .base import BaseBlockEngine, VerifyMessage, BCct
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
class BaseBCEllipticCurveEngine(BaseBlockEngine):
|
18
|
+
MAX_ADDRESS_VALUE = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
|
19
|
+
|
20
|
+
def _get_pk(self, private_key : ec.EllipticCurvePrivateKey) -> ec.EllipticCurvePublicKey:
|
21
|
+
"""
|
22
|
+
Simple wrapper to generate pk from sk
|
23
|
+
|
24
|
+
|
25
|
+
Returns
|
26
|
+
-------
|
27
|
+
public_key : EllipticCurvePublicKey
|
28
|
+
|
29
|
+
"""
|
30
|
+
return super(BaseBCEllipticCurveEngine, self)._get_pk(private_key)
|
31
|
+
|
32
|
+
|
33
|
+
def _sk_to_text(
|
34
|
+
self,
|
35
|
+
private_key : ec.EllipticCurvePrivateKey,
|
36
|
+
password=None,
|
37
|
+
fn=None
|
38
|
+
):
|
39
|
+
"""
|
40
|
+
Serialize a EllipticCurvePrivateKey as text
|
41
|
+
|
42
|
+
Parameters
|
43
|
+
----------
|
44
|
+
private_key : EllipticCurvePrivateKey
|
45
|
+
the secret key object.
|
46
|
+
|
47
|
+
password: str
|
48
|
+
password to be used for sk serialization
|
49
|
+
|
50
|
+
fn: str:
|
51
|
+
text file where to save the pk
|
52
|
+
|
53
|
+
Returns
|
54
|
+
-------
|
55
|
+
the sk as text string
|
56
|
+
|
57
|
+
"""
|
58
|
+
return super(BaseBCEllipticCurveEngine, self)._sk_to_text(
|
59
|
+
private_key=private_key,
|
60
|
+
password=password,
|
61
|
+
fn=fn
|
62
|
+
)
|
63
|
+
|
64
|
+
#############################################################################
|
65
|
+
##
|
66
|
+
## MANDATORY DEFINITIONS:
|
67
|
+
##
|
68
|
+
#############################################################################
|
69
|
+
|
70
|
+
def _create_new_sk(self) -> ec.EllipticCurvePrivateKey:
|
71
|
+
"""
|
72
|
+
Simple wrapper to generated pk
|
73
|
+
|
74
|
+
|
75
|
+
Returns
|
76
|
+
-------
|
77
|
+
private_key : EllipticCurvePrivateKey
|
78
|
+
|
79
|
+
"""
|
80
|
+
private_key = ec.generate_private_key(curve=ec.SECP256K1())
|
81
|
+
return private_key
|
82
|
+
|
83
|
+
def _create_new_sk_from_words(self, words: list[str]) -> ec.EllipticCurvePrivateKey:
|
84
|
+
"""
|
85
|
+
Simple wrapper to generate pk using a seed
|
86
|
+
|
87
|
+
Parameters
|
88
|
+
----------
|
89
|
+
words : list[str]
|
90
|
+
The words to be used as seed.
|
91
|
+
|
92
|
+
Returns
|
93
|
+
-------
|
94
|
+
private_key : EllipticCurvePrivateKey
|
95
|
+
"""
|
96
|
+
|
97
|
+
seedString = ';'.join(words)
|
98
|
+
|
99
|
+
encodedString = seedString.encode()
|
100
|
+
|
101
|
+
# Hash the seed to ensure it has enough entropy
|
102
|
+
digest = hashlib.sha256(encodedString).digest()
|
103
|
+
|
104
|
+
# Convert the hash to an integer
|
105
|
+
private_value = int.from_bytes(digest, 'big')
|
106
|
+
|
107
|
+
# Ensure the integer is within the valid range for the curve
|
108
|
+
private_value = private_value % self.MAX_ADDRESS_VALUE
|
109
|
+
|
110
|
+
# Create the private key from the integer
|
111
|
+
private_key = ec.derive_private_key(private_value, ec.SECP256K1(), default_backend())
|
112
|
+
return private_key
|
113
|
+
|
114
|
+
def _sign(
|
115
|
+
self,
|
116
|
+
data : bytes,
|
117
|
+
private_key : ec.EllipticCurvePrivateKey,
|
118
|
+
text=False
|
119
|
+
):
|
120
|
+
"""
|
121
|
+
Sign a binary message with Elliptic Curve
|
122
|
+
|
123
|
+
|
124
|
+
Parameters
|
125
|
+
----------
|
126
|
+
data : bytes
|
127
|
+
the binary message.
|
128
|
+
|
129
|
+
private_key : ec.EllipticCurvePrivateKey
|
130
|
+
the private key object.
|
131
|
+
|
132
|
+
text : bool, optional
|
133
|
+
return the signature as text. The default is False.
|
134
|
+
|
135
|
+
Returns
|
136
|
+
-------
|
137
|
+
signature as text or binary
|
138
|
+
|
139
|
+
"""
|
140
|
+
signature = private_key.sign(
|
141
|
+
data=data,
|
142
|
+
signature_algorithm=ec.ECDSA(hashes.SHA256())
|
143
|
+
)
|
144
|
+
txt_signature = self._binary_to_text(signature)
|
145
|
+
return txt_signature if text else signature
|
146
|
+
|
147
|
+
def _verify(
|
148
|
+
self,
|
149
|
+
public_key : ec.EllipticCurvePublicKey,
|
150
|
+
signature : bytes,
|
151
|
+
data : bytes
|
152
|
+
):
|
153
|
+
"""
|
154
|
+
Verifies a `EllipticCurvePublicKey` signature on a binary `data` package
|
155
|
+
|
156
|
+
|
157
|
+
Parameters
|
158
|
+
----------
|
159
|
+
public_key : ec.EllipticCurvePublicKey
|
160
|
+
the pk object.
|
161
|
+
|
162
|
+
signature : bytes
|
163
|
+
the binary signature.
|
164
|
+
|
165
|
+
data : bytes
|
166
|
+
the binary message.
|
167
|
+
|
168
|
+
|
169
|
+
Returns
|
170
|
+
-------
|
171
|
+
result: VerifyMessage
|
172
|
+
contains `result.valid` and `result.message`
|
173
|
+
|
174
|
+
"""
|
175
|
+
result = VerifyMessage()
|
176
|
+
try:
|
177
|
+
public_key.verify(signature, data, ec.ECDSA(hashes.SHA256()))
|
178
|
+
result.valid = True
|
179
|
+
result.message = '<Signature OK>'
|
180
|
+
except Exception as exp:
|
181
|
+
err = str(exp)
|
182
|
+
if len(err) == 0:
|
183
|
+
err = exp.__class__.__name__
|
184
|
+
result.message = err
|
185
|
+
result.valid = False
|
186
|
+
return result
|
187
|
+
|
188
|
+
def _pk_to_address(self, public_key):
|
189
|
+
"""
|
190
|
+
Given a EllipticCurvePublicKey object will return the simple text address
|
191
|
+
|
192
|
+
|
193
|
+
Parameters
|
194
|
+
----------
|
195
|
+
public_key : ec.EllipticCurvePublicKey
|
196
|
+
the pk object.
|
197
|
+
|
198
|
+
Returns
|
199
|
+
-------
|
200
|
+
text address
|
201
|
+
|
202
|
+
"""
|
203
|
+
data = public_key.public_bytes(
|
204
|
+
encoding=serialization.Encoding.X962,
|
205
|
+
format=serialization.PublicFormat.CompressedPoint,
|
206
|
+
)
|
207
|
+
txt = BCct.ADDR_PREFIX + self._binary_to_text(data)
|
208
|
+
return txt
|
209
|
+
|
210
|
+
|
211
|
+
def _address_to_pk(self, address):
|
212
|
+
"""
|
213
|
+
Given a address will return the EllipticCurvePublicKey object
|
214
|
+
|
215
|
+
|
216
|
+
Parameters
|
217
|
+
----------
|
218
|
+
address : str
|
219
|
+
the text address (pk).
|
220
|
+
|
221
|
+
|
222
|
+
Returns
|
223
|
+
-------
|
224
|
+
pk : EllipticCurvePublicKey
|
225
|
+
the pk object.
|
226
|
+
|
227
|
+
"""
|
228
|
+
simple_address = self._remove_prefix(address)
|
229
|
+
bpublic_key = self._text_to_binary(simple_address)
|
230
|
+
public_key = ec.EllipticCurvePublicKey.from_encoded_point(
|
231
|
+
curve=ec.SECP256K1(),
|
232
|
+
data=bpublic_key
|
233
|
+
)
|
234
|
+
return public_key
|
235
|
+
|
236
|
+
def __derive_shared_key(self, peer_public_key : str, info : str = BCct.DEFAULT_INFO, debug : bool = False):
|
237
|
+
"""
|
238
|
+
Derives a shared key using own private key and peer's public key.
|
239
|
+
|
240
|
+
Parameters
|
241
|
+
----------
|
242
|
+
private_key : cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey
|
243
|
+
The private key to use for derivation.
|
244
|
+
peer_public_key : cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey
|
245
|
+
The peer's public key.
|
246
|
+
|
247
|
+
Returns
|
248
|
+
-------
|
249
|
+
bytes
|
250
|
+
The derived shared key.
|
251
|
+
"""
|
252
|
+
if info is not None:
|
253
|
+
info = info.encode()
|
254
|
+
private_key = self.private_key
|
255
|
+
shared_key = private_key.exchange(ec.ECDH(), peer_public_key)
|
256
|
+
if debug:
|
257
|
+
print('sk-pk-shared_key: ', binascii.hexlify(shared_key).decode('utf-8'))
|
258
|
+
derived_key = HKDF(
|
259
|
+
algorithm=hashes.SHA256(),
|
260
|
+
length=32,
|
261
|
+
salt=None,
|
262
|
+
info=info,
|
263
|
+
backend=default_backend()
|
264
|
+
).derive(shared_key)
|
265
|
+
if debug:
|
266
|
+
print('derived-shared_key: ', base64.b64encode(derived_key))
|
267
|
+
return derived_key
|
268
|
+
|
269
|
+
def encrypt(self, plaintext: str, receiver_address: str, info: str = BCct.DEFAULT_INFO, debug: bool = False):
|
270
|
+
"""
|
271
|
+
Encrypts plaintext using the sender's private key and receiver's public key,
|
272
|
+
then base64 encodes the output.
|
273
|
+
|
274
|
+
Parameters
|
275
|
+
----------
|
276
|
+
receiver_address : str
|
277
|
+
The receiver's address
|
278
|
+
|
279
|
+
plaintext : str
|
280
|
+
The plaintext to encrypt.
|
281
|
+
|
282
|
+
Returns
|
283
|
+
-------
|
284
|
+
str
|
285
|
+
The base64 encoded nonce and ciphertext.
|
286
|
+
"""
|
287
|
+
receiver_pk = self._address_to_pk(receiver_address)
|
288
|
+
shared_key = self.__derive_shared_key(receiver_pk, info=info, debug=debug)
|
289
|
+
aesgcm = AESGCM(shared_key)
|
290
|
+
nonce = os.urandom(12) # Generate a unique nonce for each encryption
|
291
|
+
ciphertext = aesgcm.encrypt(nonce, plaintext.encode(), None)
|
292
|
+
encrypted_data = nonce + ciphertext # Prepend the nonce to the ciphertext
|
293
|
+
return base64.b64encode(encrypted_data).decode() # Encode to base64
|
294
|
+
|
295
|
+
def decrypt(self, encrypted_data_b64 : str, sender_address : str, info: str = BCct.DEFAULT_INFO, debug: bool = False):
|
296
|
+
"""
|
297
|
+
Decrypts base64 encoded encrypted data using the receiver's private key.
|
298
|
+
|
299
|
+
Parameters
|
300
|
+
----------
|
301
|
+
encrypted_data_b64 : str
|
302
|
+
The base64 encoded nonce and ciphertext.
|
303
|
+
|
304
|
+
sender_address : str
|
305
|
+
The sender's address.
|
306
|
+
|
307
|
+
Returns
|
308
|
+
-------
|
309
|
+
str
|
310
|
+
The decrypted plaintext.
|
311
|
+
|
312
|
+
"""
|
313
|
+
try:
|
314
|
+
sender_pk = self._address_to_pk(sender_address)
|
315
|
+
encrypted_data = base64.b64decode(encrypted_data_b64) # Decode from base64
|
316
|
+
nonce = encrypted_data[:12] # Extract the nonce
|
317
|
+
ciphertext = encrypted_data[12:] # The rest is the ciphertext
|
318
|
+
shared_key = self.__derive_shared_key(sender_pk, info=info, debug=debug)
|
319
|
+
aesgcm = AESGCM(shared_key)
|
320
|
+
plaintext = aesgcm.decrypt(nonce, ciphertext, None)
|
321
|
+
result = plaintext.decode()
|
322
|
+
except Exception as exc:
|
323
|
+
result = None
|
324
|
+
return result
|
File without changes
|
@@ -0,0 +1,22 @@
|
|
1
|
+
-----BEGIN CERTIFICATE-----
|
2
|
+
MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh
|
3
|
+
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
|
4
|
+
d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD
|
5
|
+
QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT
|
6
|
+
MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2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