Skip to content

Commit 291acc5

Browse files
committed
tree-wide: blackification
I like blake, no configuration, no opinion. https://github.com/psf/black Signed-off-by: Antoine Poinsot <darosior@protonmail.com>
1 parent fb75b61 commit 291acc5

4 files changed

Lines changed: 447 additions & 169 deletions

File tree

.github/workflows/python-package.yml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ on:
88

99
jobs:
1010
build:
11-
1211
runs-on: ubuntu-latest
1312
strategy:
1413
matrix:
@@ -23,12 +22,23 @@ jobs:
2322
- name: Installation (deps and package)
2423
run: |
2524
python -m pip install --upgrade pip
26-
pip install flake8 pytest
25+
pip install pytest
2726
pip install -r requirements.txt -r tests/requirements.txt
2827
python setup.py install
29-
- name: Linter (flake8)
30-
run: |
31-
flake8 ./bip32/ --count --show-source --statistics --max-line-length=90
3228
- name: Test with pytest
3329
run: |
3430
pytest -vvv
31+
32+
linter:
33+
runs-on: ubuntu-latest
34+
steps:
35+
- uses: actions/checkout@v2
36+
- name: Set up Python 3.9
37+
uses: actions/setup-python@v2
38+
with:
39+
python-version: 3.9
40+
- name: Run `black` on the source code
41+
run: |
42+
python -m pip install --upgrade pip
43+
pip install black
44+
python -m black --check bip32 tests

bip32/bip32.py

Lines changed: 83 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@
33
import hmac
44

55
from .utils import (
6-
HARDENED_INDEX, _derive_hardened_private_child,
7-
_derive_unhardened_private_child, _derive_public_child,
8-
_serialize_extended_key, _unserialize_extended_key,
9-
_hardened_index_in_path, _privkey_to_pubkey, _deriv_path_str_to_list
6+
HARDENED_INDEX,
7+
_derive_hardened_private_child,
8+
_derive_unhardened_private_child,
9+
_derive_public_child,
10+
_serialize_extended_key,
11+
_unserialize_extended_key,
12+
_hardened_index_in_path,
13+
_privkey_to_pubkey,
14+
_deriv_path_str_to_list,
1015
)
1116

1217

1318
class PrivateDerivationError(ValueError):
1419
"""
1520
Tried to use a derivation requiring private keys, without private keys.
1621
"""
22+
1723
pass
1824

1925

@@ -23,8 +29,16 @@ def __init__(self, message):
2329

2430

2531
class BIP32:
26-
def __init__(self, chaincode, privkey=None, pubkey=None, fingerprint=None,
27-
depth=0, index=0, network="main"):
32+
def __init__(
33+
self,
34+
chaincode,
35+
privkey=None,
36+
pubkey=None,
37+
fingerprint=None,
38+
depth=0,
39+
index=0,
40+
network="main",
41+
):
2842
"""
2943
:param chaincode: The master chaincode, used to derive keys. As bytes.
3044
:param privkey: The master private key for this index (default 0).
@@ -81,11 +95,13 @@ def get_extended_privkey_from_path(self, path):
8195
chaincode, privkey = self.master_chaincode, self.master_privkey
8296
for index in path:
8397
if index & HARDENED_INDEX:
84-
privkey, chaincode = \
85-
_derive_hardened_private_child(privkey, chaincode, index)
98+
privkey, chaincode = _derive_hardened_private_child(
99+
privkey, chaincode, index
100+
)
86101
else:
87-
privkey, chaincode = \
88-
_derive_unhardened_private_child(privkey, chaincode, index)
102+
privkey, chaincode = _derive_unhardened_private_child(
103+
privkey, chaincode, index
104+
)
89105

90106
return chaincode, privkey
91107

@@ -121,18 +137,19 @@ def get_extended_pubkey_from_path(self, path):
121137
if _hardened_index_in_path(path):
122138
for index in path:
123139
if index & HARDENED_INDEX:
124-
key, chaincode = \
125-
_derive_hardened_private_child(key, chaincode, index)
140+
key, chaincode = _derive_hardened_private_child(
141+
key, chaincode, index
142+
)
126143
else:
127-
key, chaincode = \
128-
_derive_unhardened_private_child(key, chaincode, index)
144+
key, chaincode = _derive_unhardened_private_child(
145+
key, chaincode, index
146+
)
129147
pubkey = _privkey_to_pubkey(key)
130148
# We won't need private keys for the whole path, so let's only use
131149
# public key derivation.
132150
else:
133151
for index in path:
134-
pubkey, chaincode = \
135-
_derive_public_child(pubkey, chaincode, index)
152+
pubkey, chaincode = _derive_public_child(pubkey, chaincode, index)
136153

137154
return chaincode, pubkey
138155

@@ -165,10 +182,14 @@ def get_xpriv_from_path(self, path):
165182
else:
166183
parent_pubkey = self.get_pubkey_from_path(path[:-1])
167184
chaincode, privkey = self.get_extended_privkey_from_path(path)
168-
extended_key = _serialize_extended_key(privkey, self.depth + len(path),
169-
parent_pubkey,
170-
path[-1], chaincode,
171-
self.network)
185+
extended_key = _serialize_extended_key(
186+
privkey,
187+
self.depth + len(path),
188+
parent_pubkey,
189+
path[-1],
190+
chaincode,
191+
self.network,
192+
)
172193

173194
return base58.b58encode_check(extended_key).decode()
174195

@@ -192,31 +213,41 @@ def get_xpub_from_path(self, path):
192213
else:
193214
parent_pubkey = self.get_pubkey_from_path(path[:-1])
194215
chaincode, pubkey = self.get_extended_pubkey_from_path(path)
195-
extended_key = _serialize_extended_key(pubkey, self.depth + len(path),
196-
parent_pubkey,
197-
path[-1], chaincode,
198-
self.network)
216+
extended_key = _serialize_extended_key(
217+
pubkey,
218+
self.depth + len(path),
219+
parent_pubkey,
220+
path[-1],
221+
chaincode,
222+
self.network,
223+
)
199224

200225
return base58.b58encode_check(extended_key).decode()
201226

202227
def get_master_xpriv(self):
203228
"""Get the encoded extended private key of the master private key"""
204229
if self.master_privkey is None:
205230
raise PrivateDerivationError
206-
extended_key = _serialize_extended_key(self.master_privkey, self.depth,
207-
self.parent_fingerprint,
208-
self.index,
209-
self.master_chaincode,
210-
self.network)
231+
extended_key = _serialize_extended_key(
232+
self.master_privkey,
233+
self.depth,
234+
self.parent_fingerprint,
235+
self.index,
236+
self.master_chaincode,
237+
self.network,
238+
)
211239
return base58.b58encode_check(extended_key).decode()
212240

213241
def get_master_xpub(self):
214242
"""Get the encoded extended public key of the master public key"""
215-
extended_key = _serialize_extended_key(self.master_pubkey, self.depth,
216-
self.parent_fingerprint,
217-
self.index,
218-
self.master_chaincode,
219-
self.network)
243+
extended_key = _serialize_extended_key(
244+
self.master_pubkey,
245+
self.depth,
246+
self.parent_fingerprint,
247+
self.index,
248+
self.master_chaincode,
249+
self.network,
250+
)
220251
return base58.b58encode_check(extended_key).decode()
221252

222253
@classmethod
@@ -229,11 +260,16 @@ def from_xpriv(cls, xpriv):
229260
raise InvalidInputError("'xpriv' must be a string")
230261

231262
extended_key = base58.b58decode_check(xpriv)
232-
(network, depth, fingerprint,
233-
index, chaincode, key) = _unserialize_extended_key(extended_key)
263+
(
264+
network,
265+
depth,
266+
fingerprint,
267+
index,
268+
chaincode,
269+
key,
270+
) = _unserialize_extended_key(extended_key)
234271
# We need to remove the trailing `0` before the actual private key !!
235-
return BIP32(chaincode, key[1:], None, fingerprint, depth, index,
236-
network)
272+
return BIP32(chaincode, key[1:], None, fingerprint, depth, index, network)
237273

238274
@classmethod
239275
def from_xpub(cls, xpub):
@@ -245,8 +281,14 @@ def from_xpub(cls, xpub):
245281
raise InvalidInputError("'xpub' must be a string")
246282

247283
extended_key = base58.b58decode_check(xpub)
248-
(network, depth, fingerprint,
249-
index, chaincode, key) = _unserialize_extended_key(extended_key)
284+
(
285+
network,
286+
depth,
287+
fingerprint,
288+
index,
289+
chaincode,
290+
key,
291+
) = _unserialize_extended_key(extended_key)
250292
return BIP32(chaincode, None, key, fingerprint, depth, index, network)
251293

252294
@classmethod
@@ -255,6 +297,5 @@ def from_seed(cls, seed, network="main"):
255297
256298
:param seed: The seed as bytes.
257299
"""
258-
secret = hmac.new("Bitcoin seed".encode(), seed,
259-
hashlib.sha512).digest()
300+
secret = hmac.new("Bitcoin seed".encode(), seed, hashlib.sha512).digest()
260301
return BIP32(secret[32:], secret[:32], network=network)

bip32/utils.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ def _derive_unhardened_private_child(privkey, chaincode, index):
4040
assert not index & HARDENED_INDEX
4141
pubkey = _privkey_to_pubkey(privkey)
4242
# payload is the I from the BIP. Index is 32 bits unsigned int, BE.
43-
payload = hmac.new(chaincode, pubkey + index.to_bytes(4, "big"),
44-
hashlib.sha512).digest()
43+
payload = hmac.new(
44+
chaincode, pubkey + index.to_bytes(4, "big"), hashlib.sha512
45+
).digest()
4546
try:
4647
child_private = coincurve.PrivateKey(payload[:32]).add(privkey)
4748
except ValueError:
48-
raise BIP32DerivationError("Invalid private key at index {}, try the "
49-
"next one!".format(index))
49+
raise BIP32DerivationError(
50+
"Invalid private key at index {}, try the " "next one!".format(index)
51+
)
5052
return child_private.secret, payload[32:]
5153

5254

@@ -62,13 +64,15 @@ def _derive_hardened_private_child(privkey, chaincode, index):
6264
assert isinstance(privkey, bytes) and isinstance(chaincode, bytes)
6365
assert index & HARDENED_INDEX
6466
# payload is the I from the BIP. Index is 32 bits unsigned int, BE.
65-
payload = hmac.new(chaincode, b'\x00' + privkey + index.to_bytes(4, "big"),
66-
hashlib.sha512).digest()
67+
payload = hmac.new(
68+
chaincode, b"\x00" + privkey + index.to_bytes(4, "big"), hashlib.sha512
69+
).digest()
6770
try:
6871
child_private = coincurve.PrivateKey(payload[:32]).add(privkey)
6972
except ValueError:
70-
raise BIP32DerivationError("Invalid private key at index {}, try the "
71-
"next one!".format(index))
73+
raise BIP32DerivationError(
74+
"Invalid private key at index {}, try the " "next one!".format(index)
75+
)
7276
return child_private.secret, payload[32:]
7377

7478

@@ -84,19 +88,22 @@ def _derive_public_child(pubkey, chaincode, index):
8488
assert isinstance(pubkey, bytes) and isinstance(chaincode, bytes)
8589
assert not index & HARDENED_INDEX
8690
# payload is the I from the BIP. Index is 32 bits unsigned int, BE.
87-
payload = hmac.new(chaincode, pubkey + index.to_bytes(4, "big"),
88-
hashlib.sha512).digest()
91+
payload = hmac.new(
92+
chaincode, pubkey + index.to_bytes(4, "big"), hashlib.sha512
93+
).digest()
8994
try:
9095
tmp_pub = coincurve.PublicKey.from_secret(payload[:32])
9196
except ValueError:
92-
raise BIP32DerivationError("Invalid private key at index {}, try the "
93-
"next one!".format(index))
97+
raise BIP32DerivationError(
98+
"Invalid private key at index {}, try the " "next one!".format(index)
99+
)
94100
parent_pub = coincurve.PublicKey(pubkey)
95101
try:
96102
child_pub = coincurve.PublicKey.combine_keys([tmp_pub, parent_pub])
97103
except ValueError:
98-
raise BIP32DerivationError("Invalid public key at index {}, try the "
99-
"next one!".format(index))
104+
raise BIP32DerivationError(
105+
"Invalid public key at index {}, try the " "next one!".format(index)
106+
)
100107
return child_pub.format(), payload[32:]
101108

102109

@@ -106,8 +113,7 @@ def _pubkey_to_fingerprint(pubkey):
106113
return rip.digest()[:4]
107114

108115

109-
def _serialize_extended_key(key, depth, parent, index, chaincode,
110-
network="main"):
116+
def _serialize_extended_key(key, depth, parent, index, chaincode, network="main"):
111117
"""Serialize an extended private *OR* public key, as spec by bip-0032.
112118
113119
:param key: The public or private key to serialize. Note that if this is
@@ -131,8 +137,7 @@ def _serialize_extended_key(key, depth, parent, index, chaincode,
131137
elif len(parent) == 4:
132138
fingerprint = parent
133139
else:
134-
raise ValueError("Bad parent, a fingerprint or a pubkey is"
135-
" required")
140+
raise ValueError("Bad parent, a fingerprint or a pubkey is" " required")
136141
else:
137142
fingerprint = bytes(4) # master
138143
# A privkey or a compressed pubkey
@@ -147,7 +152,7 @@ def _serialize_extended_key(key, depth, parent, index, chaincode,
147152
extended += index.to_bytes(4, "big")
148153
extended += chaincode
149154
if is_privkey:
150-
extended += b'\x00'
155+
extended += b"\x00"
151156
extended += key
152157
return extended
153158

@@ -188,7 +193,7 @@ def _deriv_path_str_to_list(strpath):
188193
"""
189194
if not REGEX_DERIVATION_PATH.match(strpath):
190195
raise ValueError("invalid format")
191-
indexes = strpath.split('/')[1:]
196+
indexes = strpath.split("/")[1:]
192197
list_path = []
193198
for i in indexes:
194199
# if HARDENED

0 commit comments

Comments
 (0)