Skip to content

Commit 0ae3ef7

Browse files
committed
bip21: catch urlparse exceptions and raise as InvalidBitcoinURI
1 parent 2d55bef commit 0ae3ef7

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

electrum/bip21.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from . import bitcoin
88
from .util import format_satoshis_plain
99
from .bitcoin import COIN, TOTAL_COIN_SUPPLY_LIMIT_IN_BTC
10-
from .bolt11 import decode_bolt11_invoice, BOLT11DecodeException
10+
from .bolt11 import decode_bolt11_invoice, BOLT11InvoiceException
1111

1212
# note: when checking against these, use .lower() to support case-insensitivity
1313
BITCOIN_BIP21_URI_SCHEME = 'bitcoin'
@@ -35,7 +35,11 @@ def parse_bip21_URI(uri: str) -> dict:
3535
raise InvalidBitcoinURI("Not a bitcoin address")
3636
return {'address': uri}
3737

38-
u = urllib.parse.urlparse(uri)
38+
try:
39+
u = urllib.parse.urlparse(uri)
40+
except ValueError as e:
41+
raise InvalidBitcoinURI("failed to parse uri") from e
42+
3943
if u.scheme.lower() != BITCOIN_BIP21_URI_SCHEME:
4044
raise InvalidBitcoinURI("Not a bitcoin URI")
4145
address = u.path
@@ -94,7 +98,7 @@ def parse_bip21_URI(uri: str) -> dict:
9498
if 'lightning' in out:
9599
try:
96100
lnaddr = decode_bolt11_invoice(out['lightning'])
97-
except BOLT11DecodeException as e:
101+
except BOLT11InvoiceException as e:
98102
raise InvalidBitcoinURI(f"Failed to decode 'lightning' field: {e!r}") from e
99103
amount_sat = out.get('amount')
100104
if amount_sat:

0 commit comments

Comments
 (0)