From 16ed7e666c95045bf915a6b777c7eda75d05e792 Mon Sep 17 00:00:00 2001 From: f321x Date: Wed, 26 Nov 2025 13:42:20 +0100 Subject: [PATCH] lnpeer: use INVALID_ONION_VERSION for unparsable onions Use the `OnionFailureCode.INVALID_ONION_VERSION` (BADONION | PERM | 4) code when sending back `update_fail_malformed_htlc` as just sending a plain `BADONION` is not explicitly mentioned as correct in the spec. --- electrum/lnonion.py | 9 ++++++++- electrum/lnpeer.py | 5 ++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/electrum/lnonion.py b/electrum/lnonion.py index b57c04792..7760fd445 100644 --- a/electrum/lnonion.py +++ b/electrum/lnonion.py @@ -577,7 +577,14 @@ class OnionRoutingFailure(Exception): return error_bytes -class OnionParsingError(OnionRoutingFailure): pass +class OnionParsingError(OnionRoutingFailure): + """ + Onion parsing error will cause a htlc to get failed with update_fail_malformed_htlc. + Using INVALID_ONION_VERSION as there is no unspecific BADONION failure code defined in the spec + for the case we just cannot parse the onion. + """ + def __init__(self, data: bytes): + OnionRoutingFailure.__init__(self, code=OnionFailureCode.INVALID_ONION_VERSION, data=data) def construct_onion_error( diff --git a/electrum/lnpeer.py b/electrum/lnpeer.py index e263da1ca..1e8da558b 100644 --- a/electrum/lnpeer.py +++ b/electrum/lnpeer.py @@ -3229,7 +3229,6 @@ class Peer(Logger, EventListener): except Exception as parsing_exc: self.logger.warning(f"unable to parse onion: {str(parsing_exc)}") onion_parsing_error = OnionParsingError( - code=OnionFailureCodeMetaFlag.BADONION, data=sha256(onion_packet_bytes or b''), ) raise onion_parsing_error @@ -3259,9 +3258,9 @@ class Peer(Logger, EventListener): raise OnionRoutingFailure(code=OnionFailureCode.INVALID_ONION_HMAC, data=onion_hash) except Exception as e: self.logger.warning(f"error processing onion packet: {e!r}") - raise OnionParsingError(code=OnionFailureCodeMetaFlag.BADONION, data=onion_hash) + raise OnionParsingError(data=onion_hash) if self.network.config.TEST_FAIL_HTLCS_AS_MALFORMED: - raise OnionRoutingFailure(code=OnionFailureCode.INVALID_ONION_VERSION, data=onion_hash) + raise OnionParsingError(data=onion_hash) if self.network.config.TEST_FAIL_HTLCS_WITH_TEMP_NODE_FAILURE: raise OnionRoutingFailure(code=OnionFailureCode.TEMPORARY_NODE_FAILURE, data=b'') return processed_onion