diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4469624e..0908a9b1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -40,7 +40,7 @@ repos: args: [--notice=COPYRIGHT] files: python - repo: https://github.com/PyCQA/autoflake - rev: v2.0.0 + rev: v2.0.1 hooks: - id: autoflake args: @@ -62,7 +62,7 @@ repos: language: python types: [python] - repo: https://github.com/pycqa/isort - rev: 5.11.4 + rev: 5.12.0 hooks: - id: isort name: isort (black profile, in place fixes) @@ -70,7 +70,7 @@ repos: language: python types: [python] - repo: https://github.com/PyCQA/docformatter - rev: v1.5.1 + rev: v1.6.0.rc1 hooks: - id: docformatter description: "Formats docstrings to follow PEP 257." @@ -103,7 +103,7 @@ repos: language: python types: [python] - repo: https://github.com/psf/black - rev: 22.12.0 + rev: 23.1.0 hooks: - id: black name: black (in place fixes) diff --git a/btclib/bip32/bip32.py b/btclib/bip32/bip32.py index 2b509109..7f7468f6 100644 --- a/btclib/bip32/bip32.py +++ b/btclib/bip32/bip32.py @@ -299,7 +299,6 @@ def __init__( key: Octets, check_validity: bool = True, ) -> None: - super().__init__( version, depth, parent_fingerprint, index, chain_code, key, False ) diff --git a/btclib/block/block.py b/btclib/block/block.py index 52bb1864..38f1d3c1 100644 --- a/btclib/block/block.py +++ b/btclib/block/block.py @@ -75,7 +75,6 @@ def __init__( transactions: Sequence[Tx] | None = None, check_validity: bool = True, ) -> None: - self.header = header # https://docs.python.org/3/tutorial/controlflow.html#default-argument-values @@ -97,7 +96,6 @@ def to_dict(self, check_validity: bool = True) -> dict[str, Any]: def from_dict( cls: type[Block], dict_: Mapping[str, Any], check_validity: bool = True ) -> Block: - return cls( BlockHeader.from_dict(dict_["header"], False), [Tx.from_dict(tx, False) for tx in dict_["transactions"]], diff --git a/btclib/block/block_header.py b/btclib/block/block_header.py index 7af6e82b..7b0a276a 100644 --- a/btclib/block/block_header.py +++ b/btclib/block/block_header.py @@ -104,7 +104,6 @@ def __init__( nonce: int = 0, check_validity: bool = True, ) -> None: - self.version = version self.previous_block_hash = bytes_from_octets(previous_block_hash) self.merkle_root = bytes_from_octets(merkle_root_) @@ -134,7 +133,6 @@ def to_dict(self, check_validity: bool = True) -> dict[str, int | float | str]: def from_dict( cls: type[BlockHeader], dict_: Mapping[str, Any], check_validity: bool = True ) -> BlockHeader: - return cls( dict_["version"], dict_["previous_block_hash"], diff --git a/btclib/ecc/dsa.py b/btclib/ecc/dsa.py index 331c0cb9..04573fc6 100644 --- a/btclib/ecc/dsa.py +++ b/btclib/ecc/dsa.py @@ -556,7 +556,6 @@ def crack_prv_key_( sig2: Sig | Octets, hf: HashF = sha256, ) -> tuple[int, int]: - if isinstance(sig1, Sig): sig1.assert_valid() else: @@ -590,7 +589,6 @@ def crack_prv_key( sig2: Sig | Octets, hf: HashF = sha256, ) -> tuple[int, int]: - msg_hash1 = reduce_to_hlen(msg1, hf) msg_hash2 = reduce_to_hlen(msg2, hf) diff --git a/btclib/ecc/rfc6979_nonce.py b/btclib/ecc/rfc6979_nonce.py index 73c7b863..83198e17 100644 --- a/btclib/ecc/rfc6979_nonce.py +++ b/btclib/ecc/rfc6979_nonce.py @@ -51,7 +51,6 @@ def challenge_( msg_hash: Octets, ec: Curve = secp256k1, hf: HashF = hashlib.sha256 ) -> int: - # the message msg_hash: a hf_len array hf_len = hf().digest_size msg_hash = bytes_from_octets(msg_hash, hf_len) diff --git a/btclib/mnemonic/entropy.py b/btclib/mnemonic/entropy.py index 41fec00c..89b1ccca 100644 --- a/btclib/mnemonic/entropy.py +++ b/btclib/mnemonic/entropy.py @@ -303,7 +303,6 @@ def bin_str_entropy_from_rolls( min_roll_number = math.ceil(bits / bits_per_roll) i = 0 for roll in rolls: - # reject invalid rolls not in [1-dice_sides] if not 0 < roll <= dice_sides: msg = f"invalid roll: {roll} is not in [1-{dice_sides}]" diff --git a/btclib/network.py b/btclib/network.py index e8638bf4..ad724744 100644 --- a/btclib/network.py +++ b/btclib/network.py @@ -101,7 +101,6 @@ def __init__( slip132_p2wsh_p2sh_pub: Octets, check_validity: bool = True, ) -> None: - object.__setattr__(self, "curve", curve) object.__setattr__(self, "magic_bytes", bytes_from_octets(magic_bytes)) object.__setattr__(self, "genesis_block", bytes_from_octets(genesis_block)) @@ -175,7 +174,6 @@ def to_dict(self, check_validity: bool = True) -> dict[str, str | None]: def from_dict( cls: type[Network], dict_: Mapping[str, Any], check_validity: bool = True ) -> Network: - return cls( CURVES[dict_["curve"]], dict_["magic_bytes"], diff --git a/btclib/psbt/psbt.py b/btclib/psbt/psbt.py index 2a0289ba..dbd6d81d 100644 --- a/btclib/psbt/psbt.py +++ b/btclib/psbt/psbt.py @@ -143,7 +143,6 @@ def assert_signable(self) -> None: self.assert_valid() for i, tx_in in enumerate(self.tx.vin): - non_witness_utxo = self.inputs[i].non_witness_utxo redeem_script = self.inputs[i].redeem_script # with python>=3.8 use walrus operator @@ -191,7 +190,6 @@ def to_dict(self, check_validity: bool = True) -> dict[str, Any]: def from_dict( cls: type[Psbt], dict_: Mapping[str, Any], check_validity: bool = True ) -> Psbt: - return cls( Tx.from_dict(dict_["tx"]), [PsbtIn.from_dict(psbt_in, False) for psbt_in in dict_["inputs"]], @@ -284,7 +282,6 @@ def b64encode(self, check_validity: bool = True) -> str: def b64decode( cls: type[Psbt], psbt_str: String, check_validity: bool = True ) -> Psbt: - if isinstance(psbt_str, str): psbt_str = psbt_str.strip() @@ -366,7 +363,6 @@ def combine_psbts(psbts: Sequence[Psbt]) -> Psbt: final_psbt.version = max(psbt.version for psbt in psbts) for psbt in psbts[1:]: - for i, inp in enumerate(final_psbt.inputs): _combine_field(psbt.inputs[i], inp, "non_witness_utxo") _combine_field(psbt.inputs[i], inp, "witness_utxo") diff --git a/btclib/psbt/psbt_in.py b/btclib/psbt/psbt_in.py index d1365c3b..00c65226 100644 --- a/btclib/psbt/psbt_in.py +++ b/btclib/psbt/psbt_in.py @@ -252,7 +252,6 @@ def to_dict(self, check_validity: bool = True) -> dict[str, Any]: def from_dict( cls: type[PsbtIn], dict_: Mapping[str, Any], check_validity: bool = True ) -> PsbtIn: - return cls( Tx.from_dict(dict_["non_witness_utxo"], False) if dict_["non_witness_utxo"] @@ -292,7 +291,6 @@ def serialize(self, check_validity: bool = True) -> bytes: ) if not self.final_script_sig and not self.final_script_witness: - if self.partial_sigs: psbt_in_bin.append( serialize_dict_bytes_bytes(PSBT_IN_PARTIAL_SIG, self.partial_sigs) diff --git a/btclib/script/script.py b/btclib/script/script.py index 4429526f..91e43caf 100644 --- a/btclib/script/script.py +++ b/btclib/script/script.py @@ -322,7 +322,6 @@ def parse(stream: BinaryData, exit_on_op_success: bool = False) -> list[Command] r: list[Command] = [] # initialize the result list while True: - t = s.read(1) # get one byte if not t: break diff --git a/btclib/script/sig_hash.py b/btclib/script/sig_hash.py index d7fc9ae0..1489a99d 100644 --- a/btclib/script/sig_hash.py +++ b/btclib/script/sig_hash.py @@ -183,7 +183,6 @@ def taproot( annex: bytes, message_extension: bytes, ) -> bytes: - if hashtype not in SIG_HASH_TYPES: raise BTClibValueError(f"Unknown hash type: {hashtype}") if hashtype & 0x03 == SINGLE and input_index >= len(transaction.vout): @@ -265,7 +264,6 @@ 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 ) -> bytes: - witness = tx.vin[vin_i].script_witness if len(witness.stack) == 0: raise BTClibValueError("empty stack") diff --git a/btclib/tx/out_point.py b/btclib/tx/out_point.py index a9491232..d49956c8 100644 --- a/btclib/tx/out_point.py +++ b/btclib/tx/out_point.py @@ -48,7 +48,6 @@ def __init__( vout: int = 0xFFFFFFFF, check_validity: bool = True, ) -> None: - object.__setattr__(self, "tx_id", bytes_from_octets(tx_id)) object.__setattr__(self, "vout", vout) @@ -80,7 +79,6 @@ def to_dict(self, check_validity: bool = True) -> dict[str, str | int]: def from_dict( cls: type[OutPoint], dict_: Mapping[str, Any], check_validity: bool = True ) -> OutPoint: - return cls(dict_["txid"], dict_["vout"], check_validity) def serialize(self, check_validity: bool = True) -> bytes: diff --git a/btclib/tx/tx.py b/btclib/tx/tx.py index 4fc63412..0d7545a8 100644 --- a/btclib/tx/tx.py +++ b/btclib/tx/tx.py @@ -125,7 +125,6 @@ def __init__( vout: Sequence[TxOut] | None = None, check_validity: bool = True, ) -> None: - self.version = version self.lock_time = lock_time # https://docs.python.org/3/tutorial/controlflow.html#default-argument-values @@ -170,7 +169,6 @@ def to_dict(self, check_validity: bool = True) -> dict[str, str | int | list[Any def from_dict( cls: type[Tx], dict_: Mapping[str, Any], check_validity: bool = True ) -> Tx: - return cls( dict_["version"], dict_["locktime"], @@ -270,7 +268,6 @@ def join_txs( shuffle_inp: bool, shuffle_out: bool, ) -> Tx: - version = max(tx.version for tx in txs) if enforce_same_version and any(tx.version != version for tx in txs): raise BTClibValueError("Version numbers are not the same") diff --git a/btclib/tx/tx_in.py b/btclib/tx/tx_in.py index 2ad0afc4..ef3337a5 100644 --- a/btclib/tx/tx_in.py +++ b/btclib/tx/tx_in.py @@ -70,7 +70,6 @@ def __init__( script_witness: Witness = Witness(), check_validity: bool = True, ) -> None: - self.prev_out = prev_out self.script_sig = bytes_from_octets(script_sig) self.sequence = sequence @@ -114,7 +113,6 @@ def to_dict(self, check_validity: bool = True) -> dict[str, Any]: def from_dict( cls: type[TxIn], dict_: Mapping[str, Any], check_validity: bool = True ) -> TxIn: - return cls( OutPoint.from_dict(dict_["prev_out"], False), dict_["scriptSig"], diff --git a/btclib/tx/tx_out.py b/btclib/tx/tx_out.py index 9aff8c6d..9ea8e42a 100644 --- a/btclib/tx/tx_out.py +++ b/btclib/tx/tx_out.py @@ -49,7 +49,6 @@ def __init__( script_pub_key: ScriptPubKey | Octets, check_validity: bool = True, ) -> None: - object.__setattr__(self, "value", value) if not isinstance(script_pub_key, ScriptPubKey): script_bytes = bytes_from_octets(script_pub_key) @@ -82,7 +81,6 @@ def to_dict(self, check_validity: bool = True) -> dict[str, Any]: def from_dict( cls: type[TxOut], dict_: Mapping[str, Any], check_validity: bool = True ) -> TxOut: - value = sats_from_btc(dict_["value"]) script_bin = dict_["scriptPubKey"] network = dict_.get("network", "mainnet") diff --git a/tests/ec/test_curve.py b/tests/ec/test_curve.py index 51f50d29..b5f5a37f 100644 --- a/tests/ec/test_curve.py +++ b/tests/ec/test_curve.py @@ -165,7 +165,6 @@ def test_exceptions() -> None: def test_aff_jac_conversions() -> None: for ec in all_curves.values(): - # just a point, not INF Q = ec.G QJ = jac_from_aff(Q) @@ -187,7 +186,6 @@ def test_aff_jac_conversions() -> None: def test_add_double_aff() -> None: """Test self-consistency of add and double in affine coordinates.""" for ec in all_curves.values(): - # add G and the infinity point assert ec.add_aff(ec.G, INF) == ec.G assert ec.add_aff(INF, ec.G) == ec.G @@ -210,7 +208,6 @@ def test_add_double_aff() -> None: def test_add_double_jac() -> None: """Test self-consistency of add and double in Jacobian coordinates.""" for ec in all_curves.values(): - # add G and the infinity point assert ec.jac_equality(ec.add_jac(ec.GJ, INFJ), ec.GJ) assert ec.jac_equality(ec.add_jac(INFJ, ec.GJ), ec.GJ) @@ -233,7 +230,6 @@ def test_add_double_jac() -> None: def test_add_double_aff_jac() -> None: """Test consistency between affine and Jacobian add/double methods.""" for ec in all_curves.values(): - # just a point, not INF Q = ec.G QJ = jac_from_aff(Q) @@ -262,7 +258,6 @@ def test_ec_repr() -> None: def test_is_on_curve() -> None: for ec in all_curves.values(): - with pytest.raises(BTClibValueError, match="point must be a tuple"): ec.is_on_curve("not a point") # type: ignore[arg-type] @@ -277,7 +272,6 @@ def test_is_on_curve() -> None: def test_negate() -> None: for ec in all_curves.values(): - # just a point, not INF Q = ec.G minus_Q = ec.negate(Q) @@ -306,7 +300,6 @@ def test_negate() -> None: def test_symmetry() -> None: """Methods to break simmetry: quadratic residue, even/odd, low/high.""" for ec in low_card_curves.values(): - # just a point, not INF Q = ec.G x_Q = Q[0] diff --git a/tests/ec/test_sec_point.py b/tests/ec/test_sec_point.py index 437fa021..99465074 100644 --- a/tests/ec/test_sec_point.py +++ b/tests/ec/test_sec_point.py @@ -39,7 +39,6 @@ def test_octets2point() -> None: for ec in all_curves.values(): - G_bytes = bytes_from_point(ec.G, ec) G_point = point_from_octets(G_bytes, ec) assert ec.G == G_point diff --git a/tests/script/test_sig_hash_taproot.py b/tests/script/test_sig_hash_taproot.py index 8e668eed..b090238d 100644 --- a/tests/script/test_sig_hash_taproot.py +++ b/tests/script/test_sig_hash_taproot.py @@ -32,7 +32,6 @@ def test_valid_taproot_key_path() -> None: data = json.load(file_) for x in filter(lambda x: "TAPROOT" in x["flags"], data): - tx = Tx.parse(x["tx"]) prevouts = [TxOut.parse(prevout) for prevout in x["prevouts"]] @@ -51,7 +50,6 @@ def test_valid_taproot_key_path() -> None: or len(witness.stack) == 2 and witness.stack[-1][0] == 0x50 ): - sighash_type = 0 # all signature = witness.stack[0][:64] if len(witness.stack[0]) == 65: @@ -72,7 +70,6 @@ def test_invalid_taproot_key_path() -> None: data = json.load(file_) for x in filter(lambda x: "failure" in x.keys(), data): - tx = Tx.parse(x["tx"]) prevouts = [TxOut.parse(prevout) for prevout in x["prevouts"]] index = x["index"] @@ -89,9 +86,7 @@ def test_invalid_taproot_key_path() -> None: or len(witness.stack) == 2 and witness.stack[-1][0] == 0x50 ): - with pytest.raises((BTClibRuntimeError, BTClibValueError, AssertionError)): - assert not x["failure"]["scriptSig"] sighash_type = 0 # all diff --git a/tests/script/test_taproot.py b/tests/script/test_taproot.py index 8336741b..b1934a79 100644 --- a/tests/script/test_taproot.py +++ b/tests/script/test_taproot.py @@ -40,7 +40,6 @@ def test_valid_script_path() -> None: data = json.load(file_) for x in data: - prevouts = [TxOut.parse(prevout) for prevout in x["prevouts"]] index = x["index"]