Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions electrum/plugins/ledger/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,18 @@ def is_witness(script: bytes) -> Tuple[bool, int, bytes]:
an int representing the witness version,
and the bytes of the witness program.
"""
if len(script) < 4 or len(script) > 42:
script_len = len(script)
# Check script length bounds early for short-circuit
if script_len < 4 or script_len > 42:
return (False, 0, b"")

if script[0] != 0 and (script[0] < 81 or script[0] > 96):
first = script[0]
if first != 0 and (first < 81 or first > 96):
return (False, 0, b"")

if script[1] + 2 == len(script):
return (True, script[0] - 0x50 if script[0] else 0, script[2:])
# Avoid repeated len(script)
if script_len == script[1] + 2:
return (True, first - 0x50 if first else 0, script[2:])

return (False, 0, b"")

Expand Down