lnonion: add OnionFailureCode.from_int() helper
What changed, and why it matters
This commit is a small internal code cleanup in Electrum's Lightning network code. It moves an existing conversion of raw integer failure codes into a named helper method, without changing behavior. There is no user-facing or security-relevant change.
No action required. This is a non-security refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors OnionRoutingFailure.from_bytes() in electrum/lnonion.py to call a new classmethod OnionFailureCode.from_int(). The new method performs exactly the same try/except ValueError logic that was previously inline: it converts an integer to the OnionFailureCode enum if it matches a known value, otherwise leaves it as a plain int. No parsing logic, error handling, or control flow changes.
Changed components
electrum/lnonion.pyInspect captured patch +9 / −4
diff --git a/electrum/lnonion.py b/electrum/lnonion.py
index 45ed9a0..f624627 100644
--- a/electrum/lnonion.py
+++ b/electrum/lnonion.py
@@ -505,10 +505,7 @@ class OnionRoutingFailure(Exception):
@classmethod
def from_bytes(cls, failure_msg: bytes):
failure_code = int.from_bytes(failure_msg[:2], byteorder='big')
- try:
- failure_code = OnionFailureCode(failure_code)
- except ValueError:
- pass # unknown failure code
+ failure_code = OnionFailureCode.from_int(failure_code) # convert to enum, if known code
failure_data = failure_msg[2:]
return OnionRoutingFailure(failure_code, failure_data)
@@ -640,6 +637,14 @@ class OnionFailureCode(IntEnum):
TRAMPOLINE_FEE_INSUFFICIENT = NODE | 51
TRAMPOLINE_EXPIRY_TOO_SOON = NODE | 52
+ @classmethod
+ def from_int(cls, code: int) -> Union[int, 'OnionFailureCode']:
+ try:
+ code = OnionFailureCode(code)
+ except ValueError:
+ pass # unknown failure code
+ return code
+
# don't use these elsewhere, the names are ambiguous without context
del BADONION; del PERM; del NODE; del UPDATE
Why this scored 14/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.