Skip to content

Commit fa82da4

Browse files
committed
Apply Ruff formatting to generation
Match generation.py to the pinned Ruff 0.16.2 formatter output so the cross-platform CI matrix can proceed past its formatting gate.\n\nValidation: 714 tests pass normally and with optimization; Ruff lint and formatting, mypy, both differential verifiers, and package build pass.
1 parent 51dfe54 commit fa82da4

1 file changed

Lines changed: 20 additions & 61 deletions

File tree

src/codex32/generation.py

Lines changed: 20 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ def _threshold(value: object, *, allow_zero: bool = True) -> int:
6262
if isinstance(value, bool) or not isinstance(value, int):
6363
raise InvalidThreshold("threshold must be an integer")
6464
if value not in ((0, *range(2, 10)) if allow_zero else tuple(range(2, 10))):
65-
raise InvalidThreshold(
66-
f"threshold must be {'0 or ' if allow_zero else ''}2 through 9"
67-
)
65+
raise InvalidThreshold(f"threshold must be {'0 or ' if allow_zero else ''}2 through 9")
6866
return value
6967

7068

@@ -87,33 +85,25 @@ def _index(value: object) -> str:
8785

8886

8987
def _indices(values: Sequence[str] | str) -> tuple[str, ...]:
90-
if not isinstance(values, str) and (
91-
isinstance(values, AbstractSet) or not isinstance(values, Sequence)
92-
):
88+
if not isinstance(values, str) and (isinstance(values, AbstractSet) or not isinstance(values, Sequence)):
9389
raise TypeError("indices must be an ordered sequence")
9490
if len(values) > 31:
9591
raise InvalidShareSelection("at most 31 shares may be requested")
96-
copied: tuple[object, ...] = tuple(
97-
values[position] for position in range(len(values))
98-
)
92+
copied: tuple[object, ...] = tuple(values[position] for position in range(len(values)))
9993
normalized = tuple(_index(value) for value in copied)
10094
if len(set(normalized)) != len(normalized):
10195
raise InvalidShareSelection("output indices must be distinct")
10296
return normalized
10397

10498

105-
def _selection(
106-
threshold: int, share_count: object, indices: Sequence[str] | str | None
107-
) -> tuple[str, ...]:
99+
def _selection(threshold: int, share_count: object, indices: Sequence[str] | str | None) -> tuple[str, ...]:
108100
if (share_count is None) == (indices is None):
109101
raise InvalidShareSelection("choose exactly one of share_count or indices")
110102
if share_count is not None:
111103
if isinstance(share_count, bool) or not isinstance(share_count, int):
112104
raise InvalidShareSelection("share_count must be an integer")
113105
if not threshold <= share_count <= 31:
114-
raise InvalidShareSelection(
115-
f"share_count must be from threshold {threshold} through 31"
116-
)
106+
raise InvalidShareSelection(f"share_count must be from threshold {threshold} through 31")
117107
return tuple(secrets.SystemRandom().sample(ORDINARY_INDICES, share_count))
118108
assert indices is not None
119109
selected = _indices(indices)
@@ -127,39 +117,27 @@ def _random_identifier() -> str:
127117

128118

129119
def _fingerprint_identifier(seed: bytes) -> str:
130-
return _u5_to_chars(
131-
tuple(convertbits(_fingerprint_from_seed(seed), 8, 5, pad=True)[:4])
132-
)
120+
return _u5_to_chars(tuple(convertbits(_fingerprint_from_seed(seed), 8, 5, pad=True)[:4]))
133121

134122

135-
def _random_share(
136-
profile: Profile, threshold: int, identifier: str, index: str, length: int
137-
) -> Share:
123+
def _random_share(profile: Profile, threshold: int, identifier: str, index: str, length: int) -> Share:
138124
symbols = tuple(value & 31 for value in secrets.token_bytes(length))
139125
artifact = _from_parts(profile, Header(threshold, identifier, index), symbols)
140126
assert isinstance(artifact, Share)
141127
return artifact
142128

143129

144-
def _seed_input(
145-
seed_bytes: bytes | None, byte_length: int | None
146-
) -> tuple[bytes | None, int]:
130+
def _seed_input(seed_bytes: bytes | None, byte_length: int | None) -> tuple[bytes | None, int]:
147131
if seed_bytes is not None:
148132
if not isinstance(seed_bytes, bytes):
149133
raise TypeError("seed_bytes must be bytes")
150134
if byte_length is not None:
151135
raise InvalidLength("byte_length cannot accompany seed_bytes")
152136
if len(seed_bytes) not in SEED_BYTE_LENGTHS:
153-
raise InvalidLength(
154-
"master seed must contain 16, 20, 24, 28, 32, or 64 bytes"
155-
)
137+
raise InvalidLength("master seed must contain 16, 20, 24, 28, 32, or 64 bytes")
156138
return seed_bytes, len(seed_bytes)
157139
length = DEFAULT_SEED_BYTES if byte_length is None else byte_length
158-
if (
159-
isinstance(length, bool)
160-
or not isinstance(length, int)
161-
or length not in SEED_BYTE_LENGTHS
162-
):
140+
if isinstance(length, bool) or not isinstance(length, int) or length not in SEED_BYTE_LENGTHS:
163141
raise InvalidLength("byte_length must be 16, 20, 24, 28, 32, or 64")
164142
return None, length
165143

@@ -173,19 +151,15 @@ def generate_master_seed(
173151
"""Generate or encode one unshared ``ms`` secret."""
174152
supplied, length = _seed_input(seed_bytes, byte_length)
175153
if supplied is not None:
176-
identifier = (
177-
_random_identifier() if identifier is None else _identifier(identifier)
178-
)
154+
identifier = _random_identifier() if identifier is None else _identifier(identifier)
179155
return MasterSeed.from_seed(supplied, identifier=identifier)
180156
while True:
181157
fresh = secrets.token_bytes(length)
182158
try:
183159
default_identifier = _fingerprint_identifier(fresh)
184160
except CodexError:
185161
continue
186-
identifier = (
187-
default_identifier if identifier is None else _identifier(identifier)
188-
)
162+
identifier = default_identifier if identifier is None else _identifier(identifier)
189163
return MasterSeed.from_seed(fresh, identifier=identifier)
190164

191165

@@ -194,9 +168,7 @@ def generate_core_lightning_secret(
194168
) -> CoreLightningSecret:
195169
"""Generate or encode one unshared Core Lightning HSM secret."""
196170
identifier = _random_identifier() if identifier is None else _identifier(identifier)
197-
return _secret_from_bytes(
198-
secrets.token_bytes(32) if secret_bytes is None else secret_bytes, identifier
199-
)
171+
return _secret_from_bytes(secrets.token_bytes(32) if secret_bytes is None else secret_bytes, identifier)
200172

201173

202174
class CreationCeremony:
@@ -239,9 +211,7 @@ def _start(
239211
if secret is None:
240212
self._secret, self._basis, self._direct_count = None, [], threshold
241213
else:
242-
reheadered = _from_parts(
243-
profile, Header(threshold, identifier, "s"), secret.payload_symbols
244-
)
214+
reheadered = _from_parts(profile, Header(threshold, identifier, "s"), secret.payload_symbols)
245215
assert isinstance(reheadered, (MasterSeed, CoreLightningSecret))
246216
self._secret, self._basis, self._direct_count = (
247217
reheadered,
@@ -263,9 +233,7 @@ def master_seed(
263233
"""Start a ceremony for a fresh shared Bitcoin master seed."""
264234
threshold = _threshold(threshold, allow_zero=False)
265235
_supplied, byte_length = _seed_input(None, byte_length)
266-
identifier = (
267-
_random_identifier() if identifier is None else _identifier(identifier)
268-
)
236+
identifier = _random_identifier() if identifier is None else _identifier(identifier)
269237
return cls._start(
270238
Profile.MS,
271239
_payload_length(byte_length),
@@ -287,9 +255,7 @@ def core_lightning(
287255
) -> CreationCeremony:
288256
"""Start a ceremony for a fresh shared Core Lightning secret."""
289257
threshold = _threshold(threshold, allow_zero=False)
290-
identifier = (
291-
_random_identifier() if identifier is None else _identifier(identifier)
292-
)
258+
identifier = _random_identifier() if identifier is None else _identifier(identifier)
293259
return cls._start(
294260
Profile.CL,
295261
CL_PAYLOAD_LENGTH,
@@ -312,14 +278,10 @@ def from_secret(
312278
) -> CreationCeremony:
313279
"""Start a ceremony that shares an existing validated secret."""
314280
if not isinstance(secret, (MasterSeed, CoreLightningSecret)):
315-
raise TypeError(
316-
"from_secret accepts only MasterSeed or CoreLightningSecret"
317-
)
281+
raise TypeError("from_secret accepts only MasterSeed or CoreLightningSecret")
318282
threshold = _threshold(threshold, allow_zero=False)
319283
random_identifier = identifier is None
320-
identifier = (
321-
_random_identifier() if random_identifier else _identifier(identifier)
322-
)
284+
identifier = _random_identifier() if random_identifier else _identifier(identifier)
323285
while (threshold, identifier) == (
324286
secret.header.threshold,
325287
secret.header.identifier,
@@ -351,9 +313,7 @@ def next_share(self) -> Share:
351313
if self._finished:
352314
raise CeremonyStateError("this creation ceremony is finished")
353315
if self._pending is not None:
354-
raise CeremonyStateError(
355-
"confirm the pending card before requesting another"
356-
)
316+
raise CeremonyStateError("confirm the pending card before requesting another")
357317
if self._position == len(self._indices):
358318
raise CeremonyStateError("all cards are confirmed; finish the ceremony")
359319
index = self._indices[self._position]
@@ -391,8 +351,7 @@ def confirm(self, text: str) -> ConfirmationResult:
391351
mismatched = tuple(
392352
group + 1
393353
for group in range((max(len(observed), len(expected)) + 3) // 4)
394-
if observed[group * 4 : group * 4 + 4]
395-
!= expected[group * 4 : group * 4 + 4]
354+
if observed[group * 4 : group * 4 + 4] != expected[group * 4 : group * 4 + 4]
396355
)
397356
if mismatched:
398357
return ConfirmationResult(False, mismatched)

0 commit comments

Comments
 (0)