Spaces:
Sleeping
Sleeping
Update components/middleware.py
Browse files- components/middleware.py +35 -2
components/middleware.py
CHANGED
|
@@ -24,7 +24,8 @@ import time
|
|
| 24 |
from geopy.geocoders import Nominatim
|
| 25 |
from geopy.exc import GeocoderServiceError
|
| 26 |
from .mongodbconnection import provideClient
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
|
|
@@ -106,6 +107,27 @@ def SignatureVerification(X_401_Addr,X_401_Nonce,X_401_Sign,challange):
|
|
| 106 |
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def SecretNonceGenerator():
|
| 110 |
random_bytes = secrets.token_bytes(32)
|
| 111 |
return hashlib.sha256(random_bytes).hexdigest()
|
|
@@ -333,13 +355,24 @@ class x401Kit(BaseHTTPMiddleware):
|
|
| 333 |
helius_api_key = request.headers.get("helius_api_key")
|
| 334 |
geo_code=request.headers.get("geo_code")
|
| 335 |
geo_code_locs=[request.headers.get("geo_code_locs")]
|
|
|
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
challange=f"CHALLENGE::{X_401_Nonce}::{request.url.path}::VELOCITY401"
|
| 341 |
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
tokenverify=None
|
| 345 |
if required_mint =="no":
|
|
|
|
| 24 |
from geopy.geocoders import Nominatim
|
| 25 |
from geopy.exc import GeocoderServiceError
|
| 26 |
from .mongodbconnection import provideClient
|
| 27 |
+
from eth_account import Account
|
| 28 |
+
from eth_account.messages import encode_defunct
|
| 29 |
|
| 30 |
|
| 31 |
|
|
|
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def verify_ethereum_signature(address, signature, original_payload):
|
| 116 |
+
|
| 117 |
+
try:
|
| 118 |
+
|
| 119 |
+
message = encode_defunct(text=original_payload)
|
| 120 |
+
|
| 121 |
+
recovered_address = Account.recover_message(message, signature=signature)
|
| 122 |
+
|
| 123 |
+
return recovered_address.lower() == address.lower()
|
| 124 |
+
except Exception as e:
|
| 125 |
+
print(f"Verification failed: {e}")
|
| 126 |
+
return False
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|
| 131 |
def SecretNonceGenerator():
|
| 132 |
random_bytes = secrets.token_bytes(32)
|
| 133 |
return hashlib.sha256(random_bytes).hexdigest()
|
|
|
|
| 355 |
helius_api_key = request.headers.get("helius_api_key")
|
| 356 |
geo_code=request.headers.get("geo_code")
|
| 357 |
geo_code_locs=[request.headers.get("geo_code_locs")]
|
| 358 |
+
eth=request.headers.get("eth")
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
challange=f"CHALLENGE::{X_401_Nonce}::{request.url.path}::VELOCITY401"
|
| 364 |
|
| 365 |
+
|
| 366 |
+
signverify=None
|
| 367 |
+
if eth==true:
|
| 368 |
+
|
| 369 |
+
signverify=verify_ethereum_signature(X_401_Addr,X_401_Sign,challange)
|
| 370 |
+
|
| 371 |
+
else:
|
| 372 |
+
signverify=SignatureVerification(X_401_Addr,X_401_Nonce,X_401_Sign,challange)
|
| 373 |
+
|
| 374 |
+
|
| 375 |
+
|
| 376 |
|
| 377 |
tokenverify=None
|
| 378 |
if required_mint =="no":
|