Skip to content

Commit

Permalink
cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
fametrano committed Feb 2, 2023
1 parent 92e1c3d commit fc290cd
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 36 deletions.
7 changes: 1 addition & 6 deletions btclib/bip32/bip32.py
Expand Up @@ -50,12 +50,7 @@
ec = secp256k1


_KEY_SIZE: list[tuple[str, int]] = [
("version", 4),
("parent_fingerprint", 4),
("chain_code", 32),
("key", 33),
]
_KEY_SIZE = [("version", 4), ("parent_fingerprint", 4), ("chain_code", 32), ("key", 33)]
_REQUIRED_LENGHT = 78


Expand Down
6 changes: 1 addition & 5 deletions btclib/bip32/slip132.py
Expand Up @@ -52,11 +52,7 @@ def address_from_xpub(xpub: BIP32Key) -> str:
if xpub.key[0] not in (2, 3):
raise BTClibValueError(f"not a public key: {xpub.b58encode()}")

version_list: list[str] = [
"bip32_pub",
"slip132_p2wpkh_pub",
"slip132_p2wpkh_p2sh_pub",
]
version_list = ["bip32_pub", "slip132_p2wpkh_pub", "slip132_p2wpkh_p2sh_pub"]
function_list: list[Callable[[Any, str], str]] = [
b58.p2pkh,
b32.p2wpkh,
Expand Down
6 changes: 1 addition & 5 deletions btclib/block/block_header.py
Expand Up @@ -26,11 +26,7 @@

_HF = hash256
_HF_LEN = 32 # should be _HF().digest_size
_KEY_SIZE: list[tuple[str, int]] = [
("previous_block_hash", _HF_LEN),
("merkle_root", 32),
("bits", 4),
]
_KEY_SIZE = [("previous_block_hash", _HF_LEN), ("merkle_root", 32), ("bits", 4)]


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion btclib/ec/curve_group_f.py
Expand Up @@ -52,7 +52,7 @@ def find_subgroup_points(ec: CurveGroup, G: Point) -> list[Point]:
err_msg = f"p is too big to count all subgroup points: {ec.p}"
raise BTClibValueError(err_msg)

points: list[Point] = [G]
points = [G]
while points[-1] != INF:
Q = ec.add(points[-1], G)
points.append(Q)
Expand Down
2 changes: 1 addition & 1 deletion btclib/ecc/borromean.py
Expand Up @@ -42,7 +42,7 @@ def _get_msg_format(msg: bytes, pubk_rings: Sequence[PubkeyRing]) -> bytes:
return hf(msg + t).digest()


SValues = List[List[int]]
SValues = Sequence[List[int]]


def _initialize(
Expand Down
4 changes: 2 additions & 2 deletions btclib/hashes.py
Expand Up @@ -12,7 +12,7 @@
from __future__ import annotations

import hashlib
from typing import Callable, Tuple
from typing import Callable, Sequence, Tuple

from btclib.alias import HashF, Octets
from btclib.utils import bytes_from_octets
Expand Down Expand Up @@ -71,7 +71,7 @@ def magic_message(msg: Octets) -> bytes:
return sha256(t)


def merkle_root(data: list[bytes], hf: Callable[[bytes | str], bytes]) -> bytes:
def merkle_root(data: Sequence[bytes], hf: Callable[[bytes | str], bytes]) -> bytes:
"""Return the Merkel tree root of a list of binary hashes.
The Merkel tree is a binary tree constructed with the provided list
Expand Down
4 changes: 2 additions & 2 deletions btclib/mnemonic/entropy.py
Expand Up @@ -24,7 +24,7 @@
import math
import secrets
from hashlib import sha512
from typing import Iterable, Union
from typing import Iterable, Sequence, Union

from btclib.alias import Octets
from btclib.exceptions import BTClibValueError
Expand Down Expand Up @@ -61,7 +61,7 @@ def wordlist_indexes_from_bin_str_entropy(entropy: BinStr, base: int) -> list[in
return list(reversed(indexes))


def bin_str_entropy_from_wordlist_indexes(indexes: list[int], base: int) -> BinStr:
def bin_str_entropy_from_wordlist_indexes(indexes: Sequence[int], base: int) -> BinStr:
"""Return the raw entropy from a list of word-list indexes.
Return the raw (i.e. binary 0/1 string) entropy from the provided
Expand Down
7 changes: 4 additions & 3 deletions btclib/script/sig_hash.py
Expand Up @@ -18,6 +18,7 @@
from __future__ import annotations

from copy import deepcopy
from typing import Sequence

from btclib import var_bytes
from btclib.alias import Octets
Expand Down Expand Up @@ -176,8 +177,8 @@ def segwit_v0(
def taproot(
transaction: Tx,
input_index: int,
amounts: list[int],
scriptpubkeys: list[ScriptPubKey],
amounts: Sequence[int],
scriptpubkeys: Sequence[ScriptPubKey],
hashtype: int,
ext_flag: int,
annex: bytes,
Expand Down Expand Up @@ -262,7 +263,7 @@ def from_tx(prevouts: list[TxOut], tx: Tx, vin_i: int, hash_type: int) -> bytes:


def _script_from_p2tr(
prevouts: list[TxOut], tx: Tx, vin_i: int, hash_type: int
prevouts: Sequence[TxOut], tx: Tx, vin_i: int, hash_type: int
) -> bytes:
witness = tx.vin[vin_i].script_witness
if len(witness.stack) == 0:
Expand Down
2 changes: 1 addition & 1 deletion tests/bip32/test_slip132.py
Expand Up @@ -87,7 +87,7 @@ def test_slip132_test_vectors() -> None:
"""
mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
kpath = "m/0/0"
test_vectors: list[tuple[bytes, str, str, str, str]] = [
test_vectors = [
(
NETWORKS["mainnet"].bip32_prv,
"m / 44h / 0h / 0h",
Expand Down
7 changes: 3 additions & 4 deletions tests/script/test_script_pub_key.py
Expand Up @@ -39,7 +39,6 @@
serialize,
type_and_payload,
)
from btclib.to_pub_key import Key


def test_eq() -> None:
Expand Down Expand Up @@ -385,7 +384,7 @@ def test_p2ms_1() -> None:
script_type, payload = type_and_payload(script_pub_key)
assert script_type == "p2ms"
assert payload == script_pub_key[:-1]
pub_keys: list[Key] = [pub_key0, pub_key1]
pub_keys = [pub_key0, pub_key1]
assert (
script_pub_key
== ScriptPubKey.p2ms(1, pub_keys, lexicographic_sorting=False).script
Expand Down Expand Up @@ -465,12 +464,12 @@ def test_p2ms_2() -> None:
pub_key0 = "04 cc71eb30d653c0c3163990c47b976f3fb3f37cccdcbedb169a1dfef58bbfbfaf f7d8a473e7e2e6d317b87bafe8bde97e3cf8f065dec022b51d11fcdd0d348ac4"
pub_key1 = "04 61cbdcc5409fb4b4d42b51d33381354d80e550078cb532a34bfa2fcfdeb7d765 19aecc62770f5b0e4ef8551946d8a540911abe3e7854a26f39f58b25c15342af"
pub_key2 = "04 79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798 483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8"
uncompressed_pub_keys: list[Key] = [pub_key0, pub_key1, pub_key2]
uncompressed_pub_keys = [pub_key0, pub_key1, pub_key2]
# mixed compressed / uncompressed public keys
pub_key0 = "04 cc71eb30d653c0c3163990c47b976f3fb3f37cccdcbedb169a1dfef58bbfbfaf f7d8a473e7e2e6d317b87bafe8bde97e3cf8f065dec022b51d11fcdd0d348ac4"
pub_key1 = "03 61cbdcc5409fb4b4d42b51d33381354d80e550078cb532a34bfa2fcfdeb7d765"
pub_key2 = "02 79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"
mixed_pub_keys: list[Key] = [pub_key0, pub_key1, pub_key2]
mixed_pub_keys = [pub_key0, pub_key1, pub_key2]

for pub_keys in (uncompressed_pub_keys, mixed_pub_keys):
for lexicographic_sorting in (True, False):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_b32.py
Expand Up @@ -61,7 +61,7 @@ def test_has_segwit_prefix() -> None:

def test_valid_address() -> None:
"""Test whether valid addresses decode to the correct output."""
valid_bc_addresses: list[tuple[str, str]] = [
valid_bc_addresses = [
(
"BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4",
"0014751e76e8199196d454941c45d1b3a323f1433bd6",
Expand All @@ -80,7 +80,7 @@ def test_valid_address() -> None:
"512079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
),
]
valid_tb_addresses: list[tuple[str, str]] = [
valid_tb_addresses = [
(
"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7",
"00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262",
Expand Down Expand Up @@ -110,7 +110,7 @@ def test_valid_address() -> None:

def test_invalid_address() -> None:
"""Test whether invalid addresses fail to decode."""
invalid_addresses: list[tuple[str, str]] = [
invalid_addresses = [
("tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty", "invalid hrp: "),
(
"tc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq5zuyut",
Expand Down Expand Up @@ -193,7 +193,7 @@ def test_invalid_address() -> None:

def test_invalid_address_enc() -> None:
"""Test whether address encoding fails on invalid input."""
invalid_address_enc: list[tuple[str, int, int, str]] = [
invalid_address_enc = [
("MAINNET", 0, 20, "'MAINNET'"),
("mainnet", 0, 21, "invalid size: "),
("mainnet", 17, 32, "invalid witness version: "),
Expand Down
4 changes: 2 additions & 2 deletions tests/test_b58.py
Expand Up @@ -40,7 +40,7 @@ def test_wif_from_prv_key() -> None:
for alt_prv_key in wif_prv_keys:
assert alt_prv_key.strip() == b58.wif_from_prv_key(alt_prv_key)

test_vectors: list[tuple[str, str, bool]] = [
test_vectors = [
("KwdMAjGmerYanjeui5SHS7JkmpZvVipYvB2LJGU1ZxJwYvP98617", "mainnet", True),
("cMzLdeGd5vEqxB8B6VFQoRopQ3sLAAvEzDAoQgvX54xwofSWj1fx", "testnet", True),
("5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ", "mainnet", False),
Expand Down Expand Up @@ -202,7 +202,7 @@ def test_p2w_p2sh() -> None:
def test_address_from_wif() -> None:
q = 0x19E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725

test_cases: list[tuple[bool, str, str, str]] = [
test_cases = [
(
False,
"mainnet",
Expand Down

0 comments on commit fc290cd

Please sign in to comment.