Skip to content

Commit

Permalink
more 3.9 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
droserasprout committed Jan 13, 2025
1 parent 02e3ec4 commit 23d5fb9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ lint.ignore = [
target-version = "py39"
lint.extend-select = ["B", "C", "RET", "SIM"]

[tool.mypy]
python_version = "3.9"

[tool.ruff.format]
quote-style = "single"
indent-style = "space"
Expand Down
32 changes: 17 additions & 15 deletions src/pytezos/crypto/encoding.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Union

import base58

tb = bytes
Expand Down Expand Up @@ -65,7 +67,7 @@
}


def scrub_input(v: str | bytes) -> bytes:
def scrub_input(v: Union[str, bytes]) -> bytes:
if isinstance(v, bytes):
return v
elif isinstance(v, str):
Expand Down Expand Up @@ -107,7 +109,7 @@ def base58_encode(v: bytes, prefix: bytes) -> bytes:
return base58.b58encode_check(encoding[2] + v)


def _validate(v: str | bytes, prefixes: list):
def _validate(v: Union[str, bytes], prefixes: list):
if isinstance(v, str):
v = v.encode()
v = scrub_input(v)
Expand All @@ -117,7 +119,7 @@ def _validate(v: str | bytes, prefixes: list):
raise ValueError('Unknown prefix.')


def validate_pkh(v: str | bytes):
def validate_pkh(v: Union[str, bytes]):
"""Ensure parameter is a public key hash (starts with b'tz1', b'tz2', b'tz3', b'tz4')
:param v: string or bytes
Expand All @@ -126,7 +128,7 @@ def validate_pkh(v: str | bytes):
return _validate(v, prefixes=[b'tz1', b'tz2', b'tz3', b'tz4'])


def validate_l2_pkh(v: str | bytes):
def validate_l2_pkh(v: Union[str, bytes]):
"""Ensure parameter is a L2 public key hash (starts with b'txr1')
:param v: string or bytes
Expand All @@ -135,7 +137,7 @@ def validate_l2_pkh(v: str | bytes):
return _validate(v, prefixes=[b'txr1'])


def validate_sig(v: str | bytes):
def validate_sig(v: Union[str, bytes]):
"""Ensure parameter is a signature (starts with b'edsig', b'spsig', b'p2sig', b'BLsig', b'sig')
:param v: string or bytes
Expand All @@ -144,7 +146,7 @@ def validate_sig(v: str | bytes):
return _validate(v, prefixes=[b'edsig', b'spsig', b'p2sig', b'BLsig', b'sig'])


def is_pkh(v: str | bytes) -> bool:
def is_pkh(v: Union[str, bytes]) -> bool:
"""Check if value is a public key hash."""
try:
validate_pkh(v)
Expand All @@ -153,7 +155,7 @@ def is_pkh(v: str | bytes) -> bool:
return True


def is_l2_pkh(v: str | bytes) -> bool:
def is_l2_pkh(v: Union[str, bytes]) -> bool:
"""Check if value is an L2 public key hash."""
try:
validate_l2_pkh(v)
Expand All @@ -162,7 +164,7 @@ def is_l2_pkh(v: str | bytes) -> bool:
return True


def is_sig(v: str | bytes) -> bool:
def is_sig(v: Union[str, bytes]) -> bool:
"""Check if value is a signature."""
try:
validate_sig(v)
Expand All @@ -171,7 +173,7 @@ def is_sig(v: str | bytes) -> bool:
return True


def is_bh(v: str | bytes) -> bool:
def is_bh(v: Union[str, bytes]) -> bool:
"""Check if value is a block hash."""
try:
_validate(v, prefixes=[b'B'])
Expand All @@ -189,7 +191,7 @@ def is_ogh(v) -> bool:
return True


def is_kt(v: str | bytes) -> bool:
def is_kt(v: Union[str, bytes]) -> bool:
"""Check if value is a KT address."""
try:
_validate(v, prefixes=[b'KT1'])
Expand All @@ -198,7 +200,7 @@ def is_kt(v: str | bytes) -> bool:
return True


def is_sr(v: str | bytes) -> bool:
def is_sr(v: Union[str, bytes]) -> bool:
"""Check if value is a smart rollup address."""
try:
_validate(v, prefixes=[b'sr1'])
Expand All @@ -207,7 +209,7 @@ def is_sr(v: str | bytes) -> bool:
return True


def is_public_key(v: str | bytes) -> bool:
def is_public_key(v: Union[str, bytes]) -> bool:
"""Check if value is a public key."""
try:
_validate(
Expand All @@ -233,7 +235,7 @@ def is_public_key(v: str | bytes) -> bool:
return True


def is_chain_id(v: str | bytes) -> bool:
def is_chain_id(v: Union[str, bytes]) -> bool:
"""Check if value is a chain id."""
try:
_validate(v, prefixes=[b'Net'])
Expand All @@ -242,15 +244,15 @@ def is_chain_id(v: str | bytes) -> bool:
return True


def is_address(v: str | bytes) -> bool:
def is_address(v: Union[str, bytes]) -> bool:
"""Check if value is a tz/KT address"""
if isinstance(v, bytes):
v = v.decode()
address = v.split('%')[0]
return is_kt(address) or is_pkh(address) or is_sr(address)


def is_txr_address(v: str | bytes) -> bool:
def is_txr_address(v: Union[str, bytes]) -> bool:
"""Check if value is a txr1 address"""
if isinstance(v, bytes):
v = v.decode()
Expand Down
13 changes: 7 additions & 6 deletions src/pytezos/crypto/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from os.path import expanduser
from os.path import join
from typing import Optional
from typing import Union

from eth_typing import BLSPubkey
from eth_typing import BLSSignature
Expand All @@ -24,7 +25,7 @@
DEFAULT_LANGUAGE = 'english'
DEFAULT_TEZOS_DIR = '~/.tezos-client'

PassphraseInput = Optional[str | bytes]
PassphraseInput = Optional[Union[str, bytes]]


def get_passphrase(passphrase: PassphraseInput = None, alias: Optional[str] = None) -> bytes:
Expand Down Expand Up @@ -189,7 +190,7 @@ def from_public_point(
@classmethod
def from_encoded_key(
cls,
key: str | bytes,
key: Union[str, bytes],
passphrase: PassphraseInput = None,
) -> 'Key':
"""Creates a key object from a base58 encoded key.
Expand Down Expand Up @@ -272,7 +273,7 @@ def generate(
@classmethod
def from_mnemonic(
cls,
mnemonic: list[str] | str,
mnemonic: Union[list[str], str],
passphrase: str = '',
email: str = '',
validate: bool = True,
Expand Down Expand Up @@ -317,7 +318,7 @@ def from_mnemonic(
return cls.from_secret_exponent(secret_exponent, curve=curve, activation_code=activation_code)

@classmethod
def from_faucet(cls, source: str | dict) -> 'Key':
def from_faucet(cls, source: Union[str, dict]) -> 'Key':
"""Import key from a faucet file: https://teztnets.xyz/
:param source: path to the json file
Expand Down Expand Up @@ -443,7 +444,7 @@ def blinded_public_key_hash(self) -> str:
blinded_pkh = blake2b(pkh, key=key, digest_size=20).digest()
return base58_encode(blinded_pkh, b'btz1').decode()

def sign(self, message: str | bytes, generic: bool = False):
def sign(self, message: Union[str, bytes], generic: bool = False):
"""Sign a raw sequence of bytes.
:param message: sequence of bytes, raw format or hexadecimal notation
Expand Down Expand Up @@ -483,7 +484,7 @@ def sign(self, message: str | bytes, generic: bool = False):

return base58_encode(signature, prefix).decode()

def verify(self, signature: str | bytes, message: str | bytes) -> bool:
def verify(self, signature: Union[str, bytes], message: Union[str, bytes]) -> bool:
"""Verify signature, raise exception if it is not valid.
:param message: sequance of bytes, raw format or hexadecimal notation
Expand Down

0 comments on commit 23d5fb9

Please sign in to comment.