From 86d6dde5ae53b60e8a697ea3b2f7329ccb726565 Mon Sep 17 00:00:00 2001 From: user Date: Wed, 6 Mar 2024 12:43:41 +0300 Subject: [PATCH] dev@locazia: edit signer method --- app/__main__.py | 3 ++- app/core/_crypto/signer.py | 25 ++++++++++--------------- requirements.txt | 1 + 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/app/__main__.py b/app/__main__.py index 7e4dc60..17f14b3 100644 --- a/app/__main__.py +++ b/app/__main__.py @@ -9,7 +9,7 @@ import time from app.api import app from app.bot import dp as uploader_bot_dp from app.client_bot import dp as client_bot_dp -from app.core._config import SANIC_PORT, MYSQL_URI, TELEGRAM_API_KEY +from app.core._config import SANIC_PORT, MYSQL_URI, PROJECT_HOST from app.core._utils.create_maria_tables import create_maria_tables from app.core.logger import make_log from app.core.models import Memory @@ -36,6 +36,7 @@ async def execute_queue(app): make_log(None, f"Application normally started. HTTP port: {SANIC_PORT}") make_log(None, f"Telegram bot: https://t.me/{telegram_bot_username}") make_log(None, f"MariaDB host: {MYSQL_URI.split('@')[1].split('/')[0].replace('/', '')}") + make_log(None, f"API host: {PROJECT_HOST}") while True: try: _cmd = app.ctx.memory._execute_queue.pop(0) diff --git a/app/core/_crypto/signer.py b/app/core/_crypto/signer.py index 4ebf0d0..1781e1a 100644 --- a/app/core/_crypto/signer.py +++ b/app/core/_crypto/signer.py @@ -1,6 +1,5 @@ -from Crypto.PublicKey import ECC -from Crypto.Hash import SHA256 -from Crypto.Signature import DSS +import nacl.signing +import nacl.encoding import base58 @@ -8,22 +7,18 @@ class Signer: def __init__(self, seed: bytes): if len(seed) != 32: raise ValueError("Seed must be 32 bytes") - - self.key = ECC.generate(curve='P-256', randfunc=lambda n: seed) - self.verifier = DSS.new(self.key, 'fips-186-3') + self.signing_key = nacl.signing.SigningKey(seed) + self.verify_key = self.signing_key.verify_key def sign(self, data_bytes: bytes) -> str: - hash_obj = SHA256.new(data_bytes) - signature = self.verifier.sign(hash_obj) - signature_str = base58.b58encode(signature).decode() - return signature_str + signed_message = self.signing_key.sign(data_bytes) + signature = signed_message.signature + return base58.b58encode(signature).decode() - def verify(self, data_bytes: str, signature: str) -> bool: - data_bytes = base58.b58decode(data_bytes) + def verify(self, data_bytes: bytes, signature: str) -> bool: signature_bytes = base58.b58decode(signature) - hash_obj = SHA256.new(data_bytes) try: - self.verifier.verify(hash_obj, signature_bytes) + self.verify_key.verify(data_bytes, signature_bytes) return True - except ValueError: + except nacl.exceptions.BadSignatureError: return False diff --git a/requirements.txt b/requirements.txt index 5045416..319ee98 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,4 @@ tonsdk==1.0.13 httpx==0.25.0 docker==7.0.0 pycryptodome==3.20.0 +pynacl==1.5.0