From ff5550db955094b1f0565fa235948e0c01dfe643 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sun, 13 Apr 2025 13:55:20 +0200 Subject: [PATCH] Ruff --- pbc_client/__init__.py | 6 +++--- pbc_client/crypto.py | 17 +++++++++++++---- pbc_client/pbc_types.py | 9 ++++----- test/test_crypto.py | 38 ++++++++++++++++++++++---------------- 4 files changed, 42 insertions(+), 28 deletions(-) diff --git a/pbc_client/__init__.py b/pbc_client/__init__.py index e70a929..92c2909 100644 --- a/pbc_client/__init__.py +++ b/pbc_client/__init__.py @@ -12,6 +12,7 @@ import datetime import email.utils import json import logging +import time from collections.abc import Mapping from decimal import Decimal from typing import Any @@ -21,12 +22,11 @@ import requests from frozendict import frozendict from ._version import __version__ # noqa: F401 +from .crypto import SenderAuthentication, sign_transaction from .pbc_types import ( Address, SignedTransaction, ) -from .crypto import sign_transaction, SenderAuthentication -import time logger = logging.getLogger(__name__) @@ -52,7 +52,7 @@ def shard_id_for_address(address: str | Address) -> str: if address is None: msg = 'Address must not be None' raise TypeError(msg) - if address.endswith(('a','d', 'c')): + if address.endswith(('a', 'd', 'c')): return 'shards/Shard0/' if address.endswith('2'): return 'shards/Shard1/' diff --git a/pbc_client/crypto.py b/pbc_client/crypto.py index 0be2fd9..18af8cd 100644 --- a/pbc_client/crypto.py +++ b/pbc_client/crypto.py @@ -1,6 +1,7 @@ +import dataclasses import coincurve -import hashlib + from .pbc_types import ( Address, HashSha256, @@ -9,9 +10,13 @@ from .pbc_types import ( SignedTransactionInnerPart, Transaction, ) -import dataclasses -def find_recovery_id(der_sig: bytes, message_hash: HashSha256, expected_public_key: coincurve.PublicKey) -> int: + +def find_recovery_id( + der_sig: bytes, + message_hash: HashSha256, + expected_public_key: coincurve.PublicKey, +) -> int: r, s = coincurve.der.parse_signature(der_sig) for recovery_id in range(4): @@ -26,6 +31,7 @@ def find_recovery_id(der_sig: bytes, message_hash: HashSha256, expected_public_k return None + @dataclasses.dataclass(frozen=True) class SenderAuthentication: _private_key_hex: str @@ -38,7 +44,10 @@ class SenderAuthentication: def sign_hash(self, hash_to_sign: HashSha256) -> Signature: private_key = self._private_key() - signature_wrong_location = private_key.sign_recoverable(hash_to_sign._bytes, hasher = lambda x: x) + signature_wrong_location = private_key.sign_recoverable( + hash_to_sign._bytes, + hasher=lambda x: x, + ) signature = signature_wrong_location[64:] + signature_wrong_location[:64] diff --git a/pbc_client/pbc_types.py b/pbc_client/pbc_types.py index 84afa81..06acfbb 100644 --- a/pbc_client/pbc_types.py +++ b/pbc_client/pbc_types.py @@ -89,7 +89,9 @@ class SignedTransactionInnerPart: ) def hash(self, chain_id: str) -> HashSha256: - return HashSha256.of_bytes(self.rpc_serialize() + size_prefixed(chain_id.encode('utf8'))) + return HashSha256.of_bytes( + self.rpc_serialize() + size_prefixed(chain_id.encode('utf8')), + ) @dataclasses.dataclass(frozen=True) @@ -101,7 +103,4 @@ class SignedTransaction: return self.inner.hash(chain_id) def rpc_serialize(self) -> bytes: - return ( - self.signature.rpc_serialize() - + self.inner.rpc_serialize() - ) + return self.signature.rpc_serialize() + self.inner.rpc_serialize() diff --git a/test/test_crypto.py b/test/test_crypto.py index 4533486..7f57f79 100644 --- a/test/test_crypto.py +++ b/test/test_crypto.py @@ -1,24 +1,30 @@ +from pbc_client.crypto import SenderAuthentication, sign_transaction -from pbc_client.crypto import sign_transaction, SenderAuthentication def test_sign(): - sender_authentication = SenderAuthentication("01") - chain_id = "SpecificChainIDLocatable" + sender_authentication = SenderAuthentication('01') + chain_id = 'SpecificChainIDLocatable' contract_address = '000000000000000000000000000000000000000001' signed = sign_transaction( - sender_authentication = sender_authentication, - nonce = 2, - valid_to_time = 3, - gas_cost = 2, - chain_id = chain_id, - contract_address = contract_address, - transaction_rpc = bytes([0 for i in range(99)]), + sender_authentication=sender_authentication, + nonce=2, + valid_to_time=3, + gas_cost=2, + chain_id=chain_id, + contract_address=contract_address, + transaction_rpc=bytes([0 for i in range(99)]), ) print(signed) - assert str(signed.inner.rpc_serialize().hex()) == '00000000000000020000000000000003000000000000000200000000000000000000000000000000000000000100000063000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' - assert str(signed.hash(chain_id)) == '1be2895a85862e8dd3cc75b290cc28a22e960ae02dd5c07a73b93716f9adbee8' - - - assert str(signed.signature) == '01f0f7d8f44919466eedbebc76bcb369bbb4c6f4f076e25c0ffe8ec9285890e53b4e39098540d088878a019c345ad73963543ce813fb9ccf4b84b0c25770452bd1' - + assert ( + str(signed.inner.rpc_serialize().hex()) + == '00000000000000020000000000000003000000000000000200000000000000000000000000000000000000000100000063000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' + ) + assert ( + str(signed.hash(chain_id)) + == '1be2895a85862e8dd3cc75b290cc28a22e960ae02dd5c07a73b93716f9adbee8' + ) + assert ( + str(signed.signature) + == '01f0f7d8f44919466eedbebc76bcb369bbb4c6f4f076e25c0ffe8ec9285890e53b4e39098540d088878a019c345ad73963543ce813fb9ccf4b84b0c25770452bd1' + )