Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v6.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand Down Expand Up @@ -40,7 +40,7 @@ repos:
args: [--notice=COPYRIGHT]
files: python
- repo: https://github.com/PyCQA/autoflake
rev: v2.2.0
rev: v2.3.1
hooks:
- id: autoflake
args:
Expand All @@ -54,31 +54,31 @@ repos:
language: python
types: [python]
- repo: https://github.com/asottile/pyupgrade
rev: v3.9.0
rev: v3.20.0
hooks:
- id: pyupgrade
args: [--py38-plus]
# exclude: *fixtures
language: python
types: [python]
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
rev: 6.1.0
hooks:
- id: isort
name: isort (black profile, in place fixes)
args: ["--profile", "black", "--filter-files"]
language: python
types: [python]
- repo: https://github.com/PyCQA/docformatter
rev: v1.7.5
rev: v1.7.7
hooks:
- id: docformatter
description: "Formats docstrings to follow PEP 257."
language: python
types: [python]
args: ["--in-place"]
- repo: https://github.com/DanielNoord/pydocstringformatter
rev: v0.7.3
rev: v0.7.5
hooks:
- id: pydocstringformatter
language: python
Expand All @@ -97,13 +97,13 @@ repos:
language: python
types: [python]
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
rev: 7.3.0
hooks:
- id: flake8
language: python
types: [python]
- repo: https://github.com/psf/black
rev: 23.7.0
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 25.9.0
hooks:
- id: black
name: black (in place fixes)
Expand All @@ -116,7 +116,7 @@ repos:
language: python
types: [python]
- repo: https://github.com/PyCQA/bandit
rev: 1.7.5
rev: 1.8.6
hooks:
- id: bandit
name: bandit (btclib)
Expand All @@ -125,7 +125,7 @@ repos:
language: python
types: [python]
- repo: https://github.com/PyCQA/bandit
rev: 1.7.5
rev: 1.8.6
hooks:
- id: bandit
name: bandit (tests)
Expand All @@ -138,7 +138,7 @@ repos:
language: python
types: [python]
- repo: https://github.com/PyCQA/pylint
rev: v3.0.0a6
rev: v3.3.9
hooks:
- id: pylint
args: [
Expand All @@ -147,20 +147,20 @@ repos:
language: python
types: [python]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.4.1
rev: v1.18.2
hooks:
- id: mypy
language: python
types: [python]
- repo: https://github.com/mgedmin/check-manifest
rev: "0.49"
rev: "0.50"
hooks:
- id: check-manifest
description: Check the completeness of MANIFEST.in for Python packages.
language: python
pass_filenames: false
always_run: true
- repo: https://github.com/regebro/pyroma
rev: "4.2"
rev: "5.0"
hooks:
- id: pyroma
4 changes: 2 additions & 2 deletions btclib/bip32/key_origin.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def decode_hd_key_paths(map_: Mapping[Octets, BIP32KeyOrigin] | None) -> HdKeyPa


def encode_to_bip32_derivs(
hd_key_paths: Mapping[bytes, BIP32KeyOrigin]
hd_key_paths: Mapping[bytes, BIP32KeyOrigin],
) -> list[_BIP32Deriv]:
"""Return the json representation of the dataclass element."""
return [
Expand All @@ -152,7 +152,7 @@ def encode_to_bip32_derivs(


def _decode_from_bip32_deriv(
bip32_deriv: Mapping[str, str]
bip32_deriv: Mapping[str, str],
) -> tuple[bytes, BIP32KeyOrigin]:
# FIXME remove size checks to allow
# the instantiation of invalid master_fingerprint and pub_key
Expand Down
35 changes: 20 additions & 15 deletions btclib/psbt/psbt_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def _deserialize_final_script_witness(k: bytes, v: bytes) -> Witness:


def _assert_valid_ripemd160_preimages(
ripemd160_preimages: Mapping[bytes, bytes]
ripemd160_preimages: Mapping[bytes, bytes],
) -> None:
for h, preimage in ripemd160_preimages.items():
if ripemd160(preimage) != h:
Expand Down Expand Up @@ -200,8 +200,9 @@ def __init__(
taproot_key_spend_signature: Octets = b"",
taproot_script_spend_signatures: Mapping[Octets, Octets] | None = None,
taproot_leaf_scripts: Mapping[Octets, tuple[Octets, int]] | None = None,
taproot_hd_key_paths: Mapping[Octets, tuple[list[Octets], BIP32KeyOrigin]]
| None = None,
taproot_hd_key_paths: (
Mapping[Octets, tuple[list[Octets], BIP32KeyOrigin]] | None
) = None,
taproot_internal_key: Octets = b"",
taproot_merkle_root: Octets = b"",
unknown: Mapping[Octets, Octets] | None = None,
Expand Down Expand Up @@ -283,12 +284,12 @@ def to_dict(self, check_validity: bool = True) -> dict[str, Any]:
self.assert_valid()

return {
"non_witness_utxo": self.non_witness_utxo.to_dict(False)
if self.non_witness_utxo
else None,
"witness_utxo": self.witness_utxo.to_dict(False)
if self.witness_utxo
else None,
"non_witness_utxo": (
self.non_witness_utxo.to_dict(False) if self.non_witness_utxo else None
),
"witness_utxo": (
self.witness_utxo.to_dict(False) if self.witness_utxo else None
),
"partial_signatures": encode_dict_bytes_bytes(self.partial_sigs),
"sig_hash": self.sig_hash_type,
# TODO make it { "asm": "", "hex": "" }
Expand Down Expand Up @@ -327,12 +328,16 @@ def from_dict(
decode_from_bip32_derivs(dict_["taproot_hd_key_paths"]),
)
return cls(
Tx.from_dict(dict_["non_witness_utxo"], False)
if dict_["non_witness_utxo"]
else None,
TxOut.from_dict(dict_["witness_utxo"], False)
if dict_["witness_utxo"]
else None,
(
Tx.from_dict(dict_["non_witness_utxo"], False)
if dict_["non_witness_utxo"]
else None
),
(
TxOut.from_dict(dict_["witness_utxo"], False)
if dict_["witness_utxo"]
else None
),
dict_["partial_signatures"],
dict_["sig_hash"],
dict_["redeem_script"],
Expand Down
5 changes: 3 additions & 2 deletions btclib/psbt/psbt_out.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ def __init__(
hd_key_paths: Mapping[Octets, BIP32KeyOrigin] | None = None,
taproot_internal_key: Octets = b"",
taproot_tree: Sequence[tuple[int, int, Octets]] | None = None,
taproot_hd_key_paths: Mapping[Octets, tuple[list[bytes], BIP32KeyOrigin]]
| None = None,
taproot_hd_key_paths: (
Mapping[Octets, tuple[list[bytes], BIP32KeyOrigin]] | None
) = None,
unknown: Mapping[Octets, Octets] | None = None,
check_validity: bool = True,
) -> None:
Expand Down
16 changes: 8 additions & 8 deletions btclib/psbt/psbt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def serialize_dict_bytes_bytes(


def encode_leaf_scripts(
dict_: Mapping[bytes, tuple[bytes, int]]
dict_: Mapping[bytes, tuple[bytes, int]],
) -> dict[str, tuple[str, int]]:
"""Return the json representation of a tap_leaf_script.

Expand All @@ -107,7 +107,7 @@ def encode_leaf_scripts(


def decode_leaf_scripts(
map_: Mapping[Octets, tuple[Octets, int]] | None
map_: Mapping[Octets, tuple[Octets, int]] | None,
) -> dict[bytes, tuple[bytes, int]]:
"""Return a tap_leaf_script from its json representation."""
if map_ is None:
Expand Down Expand Up @@ -136,7 +136,7 @@ def parse_leaf_script(v: bytes) -> tuple[bytes, int]:


def encode_taproot_tree(
list_: list[tuple[int, int, bytes]]
list_: list[tuple[int, int, bytes]],
) -> list[tuple[int, int, str]]:
"""Return the json representation of a tap_tree.

Expand All @@ -146,7 +146,7 @@ def encode_taproot_tree(


def decode_taproot_tree(
list_: Sequence[tuple[int, int, Octets]] | None
list_: Sequence[tuple[int, int, Octets]] | None,
) -> list[tuple[int, int, bytes]]:
"""Return a tap_tree from its json representation."""
if list_ is None:
Expand Down Expand Up @@ -184,7 +184,7 @@ def parse_taproot_tree(v: bytes) -> list[tuple[int, int, bytes]]:


def taproot_bip32_to_dict(
taproot_hd_key_paths: dict[bytes, tuple[list[bytes], BIP32KeyOrigin]]
taproot_hd_key_paths: dict[bytes, tuple[list[bytes], BIP32KeyOrigin]],
) -> list[dict[str, Any]]:
"""Return the json representation of a tap_bip32_derivation.

Expand All @@ -203,7 +203,7 @@ def taproot_bip32_to_dict(


def taproot_bip32_from_dict(
taproot_hd_key_paths: list[dict[str, str]]
taproot_hd_key_paths: list[dict[str, str]],
) -> dict[bytes, tuple[list[bytes], BIP32KeyOrigin]]:
"""Return a tap_bip32_derivation from its json representation."""
return {
Expand All @@ -219,7 +219,7 @@ def taproot_bip32_from_dict(


def decode_taproot_bip32(
dict_: Mapping[Octets, tuple[Sequence[Octets], BIP32KeyOrigin]] | None
dict_: Mapping[Octets, tuple[Sequence[Octets], BIP32KeyOrigin]] | None,
) -> dict[bytes, tuple[list[bytes], BIP32KeyOrigin]]:
"""Parse correctly the tap_bip32_derivation init arguments."""
if dict_ is None:
Expand Down Expand Up @@ -315,7 +315,7 @@ def assert_valid_taproot_tree(tree: list[tuple[int, int, bytes]]) -> None:


def assert_valid_taproot_bip32_derivation(
derivations: dict[bytes, tuple[list[bytes], BIP32KeyOrigin]]
derivations: dict[bytes, tuple[list[bytes], BIP32KeyOrigin]],
) -> None:
"""Fails when the public keys have not the correct length."""
for pubkey in derivations:
Expand Down
13 changes: 8 additions & 5 deletions btclib/tx/tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,14 @@ def serialize(self, include_witness: bool, check_validity: bool = True) -> bytes
b"".join(tx_in.serialize(check_validity) for tx_in in self.vin),
var_int.serialize(len(self.vout)),
b"".join(tx_out.serialize(check_validity) for tx_out in self.vout),
b"".join(
tx_in.script_witness.serialize(check_validity) for tx_in in self.vin
)
if segwit
else b"",
(
b"".join(
tx_in.script_witness.serialize(check_validity)
for tx_in in self.vin
)
if segwit
else b""
),
self.lock_time.to_bytes(4, byteorder="little", signed=False),
]
)
Expand Down
6 changes: 3 additions & 3 deletions btclib/var_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def serialize(i: int) -> bytes:
if i < 0xFD: # 1 byte
return bytes([i])
if i <= 0xFFFF: # 2 bytes
return b"\xFD" + i.to_bytes(2, byteorder="little", signed=False)
return b"\xfd" + i.to_bytes(2, byteorder="little", signed=False)
if i <= 0xFFFFFFFF: # 4 bytes
return b"\xFE" + i.to_bytes(4, byteorder="little", signed=False)
return b"\xfe" + i.to_bytes(4, byteorder="little", signed=False)
if i <= 0xFFFFFFFFFFFFFFFF: # 8 bytes
return b"\xFF" + i.to_bytes(8, byteorder="little", signed=False)
return b"\xff" + i.to_bytes(8, byteorder="little", signed=False)
raise BTClibValueError(f"integer too big for var_int encoding: '{hex_string(i)}'")
2 changes: 1 addition & 1 deletion tests/bip32/test_bip32.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_exceptions() -> None:

seed = "5b56c417303faa3fcba7e57400e120a0"
with pytest.raises(BTClibValueError, match="unknown extended key version: "):
version = b"\x04\x88\xAD\xE5"
version = b"\x04\x88\xad\xe5"
rootxprv_from_seed(seed, version)

with pytest.raises(BTClibValueError, match="too many bits for seed: "):
Expand Down
8 changes: 4 additions & 4 deletions tests/script/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ def test_op_int() -> None:

def test_serialize_bytes_command() -> None:
length = 75
b = b"\x0A" * length
b = b"\x0a" * length
assert len(serialize([b])) == length + 1
b = b"\x0A" * (length + 1)
b = b"\x0a" * (length + 1)
assert len(serialize([b])) == (length + 1) + 2

length = 255
b = b"\x0A" * length
b = b"\x0a" * length
assert len(serialize([b])) == length + 2
b = b"\x0A" * (length + 1)
b = b"\x0a" * (length + 1)
assert len(serialize([b])) == (length + 1) + 3


Expand Down
8 changes: 4 additions & 4 deletions tests/script/test_taproot.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ def test_serialize_op_success() -> None:

def test_serialize_bytes_command() -> None:
length = 75
b = b"\x0A" * length
b = b"\x0a" * length
assert len(serialize([b])) == length + 1
b = b"\x0A" * (length + 1)
b = b"\x0a" * (length + 1)
assert len(serialize([b])) == (length + 1) + 2

length = 255
b = b"\x0A" * length
b = b"\x0a" * length
assert len(serialize([b])) == length + 2
b = b"\x0A" * (length + 1)
b = b"\x0a" * (length + 1)
assert len(serialize([b])) == (length + 1) + 3


Expand Down
4 changes: 2 additions & 2 deletions tests/test_bech32.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_bech32() -> None:

invalid_checksum = [
["\x20" + " 1nwldj5", r"HRP character out of range: *"],
["\x7F" + "1axkwrx", r"HRP character out of range: *"],
["\x7f" + "1axkwrx", r"HRP character out of range: *"],
["\x80" + "1eym55h", r"HRP character out of range: *"],
["pzry9x0s0muk", r"no separator character: *"],
["1pzry9x0s0muk", r"empty HRP: *"],
Expand Down Expand Up @@ -129,7 +129,7 @@ def test_bech32m() -> None:

invalid_checksum = [
["\x20" + "1xj0phk", r"HRP character out of range: *"],
["\x7F" + "1g6xzxy", r"HRP character out of range: *"],
["\x7f" + "1g6xzxy", r"HRP character out of range: *"],
["\x80" + "1vctc34", r"HRP character out of range: *"],
["qyrz8wqd2c9m", r"no separator character: *"],
["1qyrz8wqd2c9m", r"empty HRP: *"],
Expand Down
Loading