Skip to content

Commit 53daefe

Browse files
authored
autofixed documentation (#90)
1 parent 14d04b1 commit 53daefe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+178
-493
lines changed

.pre-commit-config.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ repos:
4545
- id: pyupgrade
4646
args: [--py37-plus]
4747
# exclude: *fixtures
48+
language: python
49+
types: [python]
4850
- repo: https://github.com/PyCQA/autoflake
4951
rev: v2.0.0
5052
hooks:
@@ -57,20 +59,43 @@ repos:
5759
- --remove-duplicate-keys
5860
- --remove-unused-variables
5961
- --remove-rhs-for-unused-variables
62+
language: python
63+
types: [python]
6064
- repo: https://github.com/pycqa/isort
6165
rev: 5.11.4
6266
hooks:
6367
- id: isort
6468
name: isort (black profile, in place fixes)
6569
args: ["--profile", "black", "--filter-files"]
70+
language: python
71+
types: [python]
72+
- repo: https://github.com/myint/docformatter
73+
rev: v1.5.0
74+
hooks:
75+
- id: docformatter
76+
description: "Formats docstrings to follow PEP 257."
77+
language: python
78+
types: [python]
79+
args: ["--in-place"]
6680
- repo: https://github.com/DanielNoord/pydocstringformatter
6781
rev: v0.7.3
6882
hooks:
6983
- id: pydocstringformatter
84+
language: python
85+
types: [python]
86+
- repo: https://github.com/pycqa/pydocstyle
87+
rev: 6.2.3
88+
hooks:
89+
- id: pydocstyle
90+
additional_dependencies: [tomli]
91+
language: python
92+
types: [python]
7093
- repo: https://github.com/asottile/yesqa
7194
rev: v1.4.0
7295
hooks:
7396
- id: yesqa
97+
language: python
98+
types: [python]
7499
- repo: https://github.com/psf/black
75100
rev: 22.12.0
76101
hooks:
@@ -82,17 +107,23 @@ repos:
82107
# pre-commit's default_language_version, see
83108
# https://pre-commit.com/#top_level-default_language_version
84109
# language_version: python3.11
110+
language: python
111+
types: [python]
85112
- repo: https://github.com/PyCQA/flake8
86113
rev: 6.0.0
87114
hooks:
88115
- id: flake8
116+
language: python
117+
types: [python]
89118
- repo: https://github.com/PyCQA/bandit
90119
rev: 1.7.4
91120
hooks:
92121
- id: bandit
93122
name: bandit (btclib)
94123
args: []
95124
exclude: test_*
125+
language: python
126+
types: [python]
96127
- repo: https://github.com/PyCQA/bandit
97128
rev: 1.7.4
98129
hooks:
@@ -101,6 +132,8 @@ repos:
101132
# disable B101 (Test for use of assert) for the tests folder
102133
args: ["--skip", "B101"]
103134
exclude: btclib
135+
language: python
136+
types: [python]
104137
- repo: https://github.com/pycqa/pylint
105138
rev: v2.15.9
106139
hooks:
@@ -109,10 +142,14 @@ repos:
109142
"--score=false",
110143
"--disable=E0401", # import-error
111144
]
145+
language: python
146+
types: [python]
112147
- repo: https://github.com/pre-commit/mirrors-mypy
113148
rev: v0.991
114149
hooks:
115150
- id: mypy
151+
language: python
152+
types: [python]
116153
- repo: https://github.com/mgedmin/check-manifest
117154
rev: "0.49"
118155
hooks:

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
| | |
66
| --- | --- |
77
| Package | [![python](https://img.shields.io/pypi/pyversions/btclib.svg?logo=python)](https://pypi.python.org/pypi/btclib/) [![calver: yyy.m.d](https://img.shields.io/badge/cal_ver-yyyy.m.d-1674b1.svg?logo=calver)](https://calver.org/) [![pypi](https://img.shields.io/pypi/v/btclib.svg?logo=pypi)](https://pypi.python.org/pypi/btclib/) [![downloads](https://static.pepy.tech/badge/btclib)](https://pepy.tech/project/btclib) [![status](https://img.shields.io/pypi/status/btclib.svg)](https://pypi.python.org/pypi/btclib/) [![license](https://img.shields.io/github/license/btclib-org/btclib.svg)](https://github.com/btclib-org/btclib/blob/master/LICENSE) |
8-
| Coding standards | [![lint: pydocstringformatter](https://img.shields.io/badge/lint-pydocstringformatter-yellowgreen.svg?logo=pydocstringformatter)](https://github.com/DanielNoord/pydocstringformatter) [![lint: pyupgrade](https://img.shields.io/badge/lint-pyupgrade-yellowgreen.svg?logo=pyupgrade)](https://github.com/asottile/pyupgrade) [![lint: isort](https://img.shields.io/badge/lint-isort-yellowgreen.svg?logo=isort)](https://github.com/PyCQA/isort) [![lint: black](https://img.shields.io/badge/lint-black-yellowgreen.svg?logo=black)](https://github.com/psf/black) [![lint: autoflake](https://img.shields.io/badge/lint-autoflake-yellowgreen.svg?logo=autoflake)](https://github.com/PyCQA/autoflake) [![lint: flake8](https://img.shields.io/badge/lint-flake8-yellowgreen.svg?logo=flake8)](<https://github.com/PyCQA/flake8>) [![lint: yesqa](https://img.shields.io/badge/lint-yesqa-yellowgreen.svg?logo=yesqa)](<https://github.com/asottile/yesqa>) [![lint: pylint](https://img.shields.io/badge/lint-pylint-yellowgreen.svg?logo=pylint)](https://github.com/PyCQA/pylint) [![type check: mypy](https://img.shields.io/badge/type_check-mypy-yellowgreen.svg?logo=mypy)](http://mypy-lang.org/) [![type check: pyright](https://img.shields.io/badge/type_check-pyright-yellowgreen.svg)](https://github.com/microsoft/pyright) [![lint: sourcery](https://img.shields.io/badge/lint-sourcery-yellowgreen.svg?logo=sourcery)](https://sourcery.ai) [![security: bandit](https://img.shields.io/badge/security-bandit-yellowgreen.svg?logo=bandit)](https://github.com/PyCQA/bandit) [![lint: markdownlint-cli2](https://img.shields.io/badge/lint-markdownlint--cli2-yellowgreen.svg?logo=sourcery)](https://github.com/DavidAnson/markdownlint-cli2) |
8+
| Formatting standards | [![lint: isort](https://img.shields.io/badge/lint-isort-yellowgreen.svg?logo=isort)](https://github.com/PyCQA/isort) [![lint: black](https://img.shields.io/badge/lint-black-yellowgreen.svg?logo=black)](https://github.com/psf/black) [![lint: flake8](https://img.shields.io/badge/lint-flake8-yellowgreen.svg?logo=flake8)](<https://github.com/PyCQA/flake8>) [![lint: yesqa](https://img.shields.io/badge/lint-yesqa-yellowgreen.svg?logo=yesqa)](<https://github.com/asottile/yesqa>) [![lint: markdownlint-cli2](https://img.shields.io/badge/lint-markdownlint--cli2-yellowgreen.svg?logo=sourcery)](https://github.com/DavidAnson/markdownlint-cli2) |
9+
| Coding standards | [![type check: mypy](https://img.shields.io/badge/type_check-mypy-yellowgreen.svg?logo=mypy)](http://mypy-lang.org/) [![type check: pyright](https://img.shields.io/badge/type_check-pyright-yellowgreen.svg)](https://github.com/microsoft/pyright) [![lint: pylint](https://img.shields.io/badge/lint-pylint-yellowgreen.svg?logo=pylint)](https://github.com/PyCQA/pylint) [![lint: autoflake](https://img.shields.io/badge/lint-autoflake-yellowgreen.svg?logo=autoflake)](https://github.com/PyCQA/autoflake) [![lint: pyupgrade](https://img.shields.io/badge/lint-pyupgrade-yellowgreen.svg?logo=pyupgrade)](https://github.com/asottile/pyupgrade) [![lint: sourcery](https://img.shields.io/badge/lint-sourcery-yellowgreen.svg?logo=sourcery)](https://sourcery.ai) [![security: bandit](https://img.shields.io/badge/security-bandit-yellowgreen.svg?logo=bandit)](https://github.com/PyCQA/bandit) |
10+
| Docs | [![lint: docformatter](https://img.shields.io/badge/lint-docformatter-yellowgreen.svg?logo=docformatter)](https://github.com/PyCQA/docformatter) [![lint: pydocstringformatter](https://img.shields.io/badge/lint-pydocstringformatter-yellowgreen.svg?logo=pydocstringformatter)](https://github.com/DanielNoord/pydocstringformatter) [![lint: pydocstyle](https://img.shields.io/badge/lint-pydocstyle-yellowgreen.svg?logo=pydocstyle)](https://github.com/PyCQA/pydocstyle)|
911
| CI/CD | [![pre-commit.ci status](https://results.pre-commit.ci/badge/github/btclib-org/btclib/master.svg)](https://results.pre-commit.ci/latest/github/btclib-org/btclib/master) [![lint](https://github.com/btclib-org/btclib/actions/workflows/lint.yml/badge.svg)](https://github.com/btclib-org/btclib/actions/workflows/lint.yml) [![test](https://github.com/btclib-org/btclib/actions/workflows/test.yml/badge.svg)](https://github.com/btclib-org/btclib/actions/workflows/test.yml) [![test-py312](https://github.com/btclib-org/btclib/actions/workflows/test-py312.yml/badge.svg)](https://github.com/btclib-org/btclib/actions/workflows/test-py312.yml) [![coverage](https://coveralls.io/repos/github/btclib-org/btclib/badge.svg?logo=coveralls)](https://coveralls.io/github/btclib-org/btclib) | [![docs](https://img.shields.io/readthedocs/btclib.svg?logo=readthedocs)](https://btclib.readthedocs.io) |
1012
| Conversations | [![slack](https://img.shields.io/badge/slack-btclib_dev-white.svg?logo=slack)](https://bbt-training.slack.com/messages/C01CCJ85AES) [![Follow on Twitter](https://img.shields.io/twitter/follow/btclib?style=social&logo=twitter)](https://twitter.com/intent/follow?screen_name=btclib)|
11-
|
1213

1314
[Browse GitHub Code Repository](https://github.com/btclib-org/btclib/)
1415

btclib/b32.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959

6060

6161
def has_segwit_prefix(addr: String) -> bool:
62-
6362
str_addr = addr.strip().lower() if isinstance(addr, str) else addr.decode("ascii")
6463
return any(str_addr.startswith(f"{net.hrp}1") for net in NETWORKS.values())
6564

@@ -97,7 +96,6 @@ def power_of_2_base_conversion(
9796

9897

9998
def check_witness(wit_ver: int, wit_prg: Octets) -> bytes:
100-
10199
if not 0 <= wit_ver < 17:
102100
err_msg = "invalid witness version: "
103101
err_msg += f"{wit_ver} not in 0..16"
@@ -126,7 +124,6 @@ def address_from_witness(
126124
wit_ver: int, wit_prg: Octets, network: str = "mainnet"
127125
) -> str:
128126
"""Encode a bech32 native SegWit address from the witness."""
129-
130127
hrp = NETWORKS[network].hrp
131128
return _address_from_witness(wit_ver, wit_prg, hrp)
132129

@@ -136,7 +133,6 @@ def witness_from_address(b32addr: String) -> tuple[int, bytes, str]:
136133
137134
The returned data structure is: version, program, network.
138135
"""
139-
140136
if isinstance(b32addr, str):
141137
b32addr = b32addr.strip()
142138

btclib/b58.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
"""Base58 address and WIF functions.
1212
13-
Base58 encoding of public keys and scripts as addresses,
14-
private keys as WIFs
13+
Base58 encoding of public keys and scripts as addresses, private keys as
14+
WIFs
1515
"""
1616
from __future__ import annotations
1717

@@ -31,7 +31,6 @@ def wif_from_prv_key(
3131
prv_key: PrvKey, network: str | None = None, compressed: bool | None = None
3232
) -> str:
3333
"""Return the WIF encoding of a private key."""
34-
3534
q, net, compr = prv_keyinfo_from_prv_key(prv_key)
3635

3736
# the private key might provide network and compressed informations
@@ -58,7 +57,6 @@ def wif_from_prv_key(
5857

5958
def address_from_h160(script_type: str, h160: Octets, network: str = "mainnet") -> str:
6059
"""Return a base58 address from the payload."""
61-
6260
if script_type == "p2sh":
6361
prefix = NETWORKS[network].p2sh
6462
elif script_type == "p2pkh":
@@ -72,7 +70,6 @@ def address_from_h160(script_type: str, h160: Octets, network: str = "mainnet")
7270

7371
def h160_from_address(b58addr: String) -> tuple[str, bytes, str]:
7472
"""Return the payload from a base58 address."""
75-
7673
if isinstance(b58addr, str):
7774
b58addr = b58addr.strip()
7875
payload = b58decode(b58addr, 21)
@@ -110,7 +107,6 @@ def p2sh(script_pub_key: Octets, network: str = "mainnet") -> str:
110107

111108
def _address_from_v0_witness(wit_prg: Octets, network: str = "mainnet") -> str:
112109
"""Return the legacy base58 p2sh-wrapped SegWit v0 address."""
113-
114110
# check witness program
115111
wit_prg = b32.check_witness(0, wit_prg)
116112
redeem_script = serialize(["OP_0", wit_prg])
@@ -121,13 +117,13 @@ def _address_from_v0_witness(wit_prg: Octets, network: str = "mainnet") -> str:
121117

122118

123119
def p2wpkh_p2sh(key: Key, network: str | None = None) -> str:
124-
"""Return the p2wpkh-p2sh base58 address corresponding to a public key."""
120+
"""Return the base58 p2sh-wrapped address of a p2wpkh."""
125121
pub_key, network = pub_keyinfo_from_key(key, network, compressed=True)
126122
witness_program = hash160(pub_key)
127123
return _address_from_v0_witness(witness_program, network)
128124

129125

130126
def p2wsh_p2sh(redeem_script: Octets, network: str = "mainnet") -> str:
131-
"""Return the p2wsh-p2sh base58 address corresponding to a reedem script."""
127+
"""Return the base58 p2sh-wrapped address of a p2wsh."""
132128
witness_program = sha256(redeem_script)
133129
return _address_from_v0_witness(witness_program, network)

btclib/base58.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747

4848

4949
def _b58encode_from_int(i: int) -> bytes:
50-
5150
result = b""
5251
while i or not result:
5352
i, idx = divmod(i, __BASE)
@@ -57,7 +56,6 @@ def _b58encode_from_int(i: int) -> bytes:
5756

5857

5958
def _b58encode(v: bytes) -> bytes:
60-
6159
# preserve leading-0s
6260
# leading-0s become base58 leading-1s
6361
n_pad = len(v)
@@ -75,14 +73,12 @@ def _b58encode(v: bytes) -> bytes:
7573

7674
def b58encode(v: Octets, in_size: int | None = None) -> bytes:
7775
"""Encode a bytes-like object using Base58Check."""
78-
7976
v = bytes_from_octets(v, in_size)
8077
h256 = hash256(v)
8178
return _b58encode(v + h256[:4])
8279

8380

8481
def _b58decode_to_int(v: bytes) -> int:
85-
8682
i = 0
8783
for char in v:
8884
i *= __BASE
@@ -91,7 +87,6 @@ def _b58decode_to_int(v: bytes) -> int:
9187

9288

9389
def _b58decode(v: bytes) -> bytes:
94-
9590
if any(x not in _ALPHABET for x in v):
9691
msg = "Base58 string contains invalid characters"
9792
raise BTClibValueError(msg)
@@ -117,7 +112,6 @@ def b58decode(v: String, out_size: int | None = None) -> bytes:
117112
118113
Optionally, it also ensures required output size.
119114
"""
120-
121115
if isinstance(v, str):
122116
# do not trim spaces
123117
v = v.encode("ascii")

btclib/bech32.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060

6161
def _polymod(values: Iterable[int]) -> int:
62-
"""Internal function that computes the bech32 checksum."""
62+
"""Return the bech32 checksum."""
6363
generator = [0x3B6A57B2, 0x26508E6D, 0x1EA119FA, 0x3D4233DD, 0x2A1462B3]
6464
chk = 1
6565
for value in values:
@@ -96,7 +96,6 @@ def _verify_checksum(hrp: str, data: list[int], m: int) -> bool:
9696

9797
def _decode(bech: String) -> tuple[str, list[int], list[int]]:
9898
"""Determine a bech32 string HRP, data and checksum."""
99-
10099
if isinstance(bech, bytes):
101100
bech = bech.decode("ascii")
102101

btclib/bip32/bip32.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ def __init__(
9595
key: Octets,
9696
check_validity: bool = True,
9797
) -> None:
98-
9998
self.version = bytes_from_octets(version)
10099
self.depth = depth
101100
self.parent_fingerprint = bytes_from_octets(parent_fingerprint)
@@ -107,7 +106,6 @@ def __init__(
107106
self.assert_valid()
108107

109108
def assert_valid(self) -> None:
110-
111109
for key, size in _KEY_SIZE:
112110
value = bytes(getattr(self, key))
113111
setattr(self, key, value)
@@ -155,7 +153,6 @@ def assert_valid(self) -> None:
155153
)
156154

157155
def serialize(self, check_validity: bool = True) -> bytes:
158-
159156
if check_validity:
160157
self.assert_valid()
161158

@@ -179,7 +176,6 @@ def parse(
179176
cls: type[BIP32KeyData], xkey_bin: BinaryData, check_validity: bool = True
180177
) -> BIP32KeyData:
181178
"""Return a BIP32KeyData by parsing 73 bytes from binary data."""
182-
183179
stream = bytesio_from_binarydata(xkey_bin)
184180
xkey_bin = stream.read(_REQUIRED_LENGHT)
185181

@@ -202,7 +198,6 @@ def parse(
202198
def b58decode(
203199
cls: type[BIP32KeyData], address: String, check_validity: bool = True
204200
) -> BIP32KeyData:
205-
206201
if isinstance(address, str):
207202
address = address.strip()
208203

@@ -214,7 +209,6 @@ def _rootxprv_from_seed(
214209
seed: Octets, version: Octets = NETWORKS["mainnet"].bip32_prv
215210
) -> BIP32KeyData:
216211
"""Return BIP32 root master extended private key from seed."""
217-
218212
seed = bytes_from_octets(seed)
219213
bitlenght = len(seed) * 8
220214
if bitlenght < 128:
@@ -254,9 +248,9 @@ def _xpub_from_xprv(xprv: BIP32Key) -> BIP32KeyData:
254248
"""Neutered Derivation (ND).
255249
256250
Derivation of the extended public key corresponding to an extended
257-
private key (“neutered” as it removes the ability to sign transactions).
251+
private key (“neutered” as it removes the ability to sign
252+
transactions).
258253
"""
259-
260254
if isinstance(xprv, BIP32KeyData):
261255
xkey = copy.copy(xprv)
262256
else:
@@ -280,7 +274,8 @@ def xpub_from_xprv(xprv: BIP32Key) -> str:
280274
"""Neutered Derivation (ND).
281275
282276
Derivation of the extended public key corresponding to an extended
283-
private key (“neutered” as it removes the ability to sign transactions).
277+
private key (“neutered” as it removes the ability to sign
278+
transactions).
284279
"""
285280
xkey = _xpub_from_xprv(xprv)
286281
return xkey.b58encode()
@@ -290,6 +285,7 @@ def xpub_from_xprv(xprv: BIP32Key) -> str:
290285
class _ExtendedBIP32KeyData(BIP32KeyData):
291286
# extensions used to cache intermediate results
292287
# in multi-level derivation: do not rely on them elsewhere
288+
293289
prv_key_int: int # non-zero for private key only
294290
pub_key_point: Point # non-Infinity for public key only
295291

@@ -320,7 +316,6 @@ def __init__(
320316

321317

322318
def __child_key_derivation(xkey: _ExtendedBIP32KeyData, index: int) -> None:
323-
324319
xkey.depth += 1
325320
xkey.index = index
326321
if xkey.is_private:
@@ -371,7 +366,6 @@ def __public_key_derivation(xkey: _ExtendedBIP32KeyData, index: int) -> None:
371366
def _derive(
372367
xkey: BIP32Key, der_path: BIP32DerPath, forced_version: Octets | None = None
373368
) -> BIP32KeyData:
374-
375369
if not isinstance(xkey, BIP32KeyData):
376370
xkey = BIP32KeyData.b58decode(xkey)
377371

@@ -434,7 +428,6 @@ def _derive_from_account(
434428
branches_0_1_only: bool = True,
435429
max_index: int = 0xFFFF,
436430
) -> BIP32KeyData:
437-
438431
if not isinstance(mxkey, BIP32KeyData):
439432
mxkey = BIP32KeyData.b58decode(mxkey)
440433

@@ -465,18 +458,16 @@ def derive_from_account(
465458
) -> str:
466459
"""Derive a key with public derivation at the given branch and index.
467460
468-
It also ensures that the master key is hardened,
469-
that the branch is a standard receive or change,
470-
and that the index is not arbitrarily high.
461+
It also ensures that the master key is hardened, that the branch is
462+
a standard receive or change, and that the index is not arbitrarily
463+
high.
471464
"""
472-
473465
return _derive_from_account(
474466
mxkey, branch, address_index, branches_0_1_only, max_index
475467
).b58encode()
476468

477469

478470
def crack_prv_key(parent_xpub: BIP32Key, child_xprv: BIP32Key) -> str:
479-
480471
if isinstance(parent_xpub, BIP32KeyData):
481472
p = copy.copy(parent_xpub)
482473
else:

0 commit comments

Comments
 (0)