Ruff
Some checks failed
Run Python tests (through Pytest) / Test (push) Failing after 24s
Verify Python project can be installed, loaded and have version checked / Test (push) Failing after 21s

This commit is contained in:
Jon Michael Aanes 2025-04-13 13:55:20 +02:00
parent fbded540e2
commit ff5550db95
4 changed files with 42 additions and 28 deletions

View File

@ -12,6 +12,7 @@ import datetime
import email.utils import email.utils
import json import json
import logging import logging
import time
from collections.abc import Mapping from collections.abc import Mapping
from decimal import Decimal from decimal import Decimal
from typing import Any from typing import Any
@ -21,12 +22,11 @@ import requests
from frozendict import frozendict from frozendict import frozendict
from ._version import __version__ # noqa: F401 from ._version import __version__ # noqa: F401
from .crypto import SenderAuthentication, sign_transaction
from .pbc_types import ( from .pbc_types import (
Address, Address,
SignedTransaction, SignedTransaction,
) )
from .crypto import sign_transaction, SenderAuthentication
import time
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -52,7 +52,7 @@ def shard_id_for_address(address: str | Address) -> str:
if address is None: if address is None:
msg = 'Address must not be None' msg = 'Address must not be None'
raise TypeError(msg) raise TypeError(msg)
if address.endswith(('a','d', 'c')): if address.endswith(('a', 'd', 'c')):
return 'shards/Shard0/' return 'shards/Shard0/'
if address.endswith('2'): if address.endswith('2'):
return 'shards/Shard1/' return 'shards/Shard1/'

View File

@ -1,6 +1,7 @@
import dataclasses
import coincurve import coincurve
import hashlib
from .pbc_types import ( from .pbc_types import (
Address, Address,
HashSha256, HashSha256,
@ -9,9 +10,13 @@ from .pbc_types import (
SignedTransactionInnerPart, SignedTransactionInnerPart,
Transaction, 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) r, s = coincurve.der.parse_signature(der_sig)
for recovery_id in range(4): 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 return None
@dataclasses.dataclass(frozen=True) @dataclasses.dataclass(frozen=True)
class SenderAuthentication: class SenderAuthentication:
_private_key_hex: str _private_key_hex: str
@ -38,7 +44,10 @@ class SenderAuthentication:
def sign_hash(self, hash_to_sign: HashSha256) -> Signature: def sign_hash(self, hash_to_sign: HashSha256) -> Signature:
private_key = self._private_key() 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] signature = signature_wrong_location[64:] + signature_wrong_location[:64]

View File

@ -89,7 +89,9 @@ class SignedTransactionInnerPart:
) )
def hash(self, chain_id: str) -> HashSha256: 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) @dataclasses.dataclass(frozen=True)
@ -101,7 +103,4 @@ class SignedTransaction:
return self.inner.hash(chain_id) return self.inner.hash(chain_id)
def rpc_serialize(self) -> bytes: def rpc_serialize(self) -> bytes:
return ( return self.signature.rpc_serialize() + self.inner.rpc_serialize()
self.signature.rpc_serialize()
+ self.inner.rpc_serialize()
)

View File

@ -1,24 +1,30 @@
from pbc_client.crypto import SenderAuthentication, sign_transaction
from pbc_client.crypto import sign_transaction, SenderAuthentication
def test_sign(): def test_sign():
sender_authentication = SenderAuthentication("01") sender_authentication = SenderAuthentication('01')
chain_id = "SpecificChainIDLocatable" chain_id = 'SpecificChainIDLocatable'
contract_address = '000000000000000000000000000000000000000001' contract_address = '000000000000000000000000000000000000000001'
signed = sign_transaction( signed = sign_transaction(
sender_authentication = sender_authentication, sender_authentication=sender_authentication,
nonce = 2, nonce=2,
valid_to_time = 3, valid_to_time=3,
gas_cost = 2, gas_cost=2,
chain_id = chain_id, chain_id=chain_id,
contract_address = contract_address, contract_address=contract_address,
transaction_rpc = bytes([0 for i in range(99)]), transaction_rpc=bytes([0 for i in range(99)]),
) )
print(signed) print(signed)
assert str(signed.inner.rpc_serialize().hex()) == '00000000000000020000000000000003000000000000000200000000000000000000000000000000000000000100000063000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' assert (
assert str(signed.hash(chain_id)) == '1be2895a85862e8dd3cc75b290cc28a22e960ae02dd5c07a73b93716f9adbee8' str(signed.inner.rpc_serialize().hex())
== '00000000000000020000000000000003000000000000000200000000000000000000000000000000000000000100000063000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
)
assert str(signed.signature) == '01f0f7d8f44919466eedbebc76bcb369bbb4c6f4f076e25c0ffe8ec9285890e53b4e39098540d088878a019c345ad73963543ce813fb9ccf4b84b0c25770452bd1' assert (
str(signed.hash(chain_id))
== '1be2895a85862e8dd3cc75b290cc28a22e960ae02dd5c07a73b93716f9adbee8'
)
assert (
str(signed.signature)
== '01f0f7d8f44919466eedbebc76bcb369bbb4c6f4f076e25c0ffe8ec9285890e53b4e39098540d088878a019c345ad73963543ce813fb9ccf4b84b0c25770452bd1'
)