Skip to content

Commit 7500644

Browse files
committed
Fix BTC segwit regex to accept testnet (tb) prefix
The bech32/segwit branch guard routes both `bc` (mainnet) and `tb` (testnet) HRPs into the regex, but the regex accepted `(bc|tc)`. `tc` is not a valid Bitcoin HRP (mainnet is `bc`, testnet is `tb`), and since the guard never lets a `tc`-prefixed value reach the regex, that alternative was dead code. As a result every valid testnet segwit address was rejected. Change the regex alternative to `(bc|tb)` and add a testnet address test.
1 parent 70de324 commit 7500644

2 files changed

Lines changed: 3 additions & 1 deletion

File tree

src/validators/crypto_addresses/btc_address.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def btc_address(value: str, /):
5050

5151
return (
5252
# segwit pattern
53-
re.compile(r"^(bc|tc)[0-3][02-9ac-hj-np-z]{14,74}$").match(value)
53+
re.compile(r"^(bc|tb)[0-3][02-9ac-hj-np-z]{14,74}$").match(value)
5454
if value[:2] in ("bc", "tb")
5555
else _validate_old_btc_address(value)
5656
)

tests/crypto_addresses/test_btc_address.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
# Bech32/segwit type
1818
"bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq",
1919
"bc1qc7slrfxkknqcq2jevvvkdgvrt8080852dfjewde450xdlk4ugp7szw5tk9",
20+
# Bech32/segwit type, testnet (tb) HRP
21+
"tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx",
2022
],
2123
)
2224
def test_returns_true_on_valid_btc_address(value: str):

0 commit comments

Comments
 (0)