File tree Expand file tree Collapse file tree
src/validators/crypto_addresses Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -16,9 +16,14 @@ def _decode_base58(addr: str):
1616
1717def _validate_old_btc_address (addr : str ):
1818 """Validate P2PKH and P2SH type address."""
19- if len (addr ) not in range (25 , 35 ):
19+ # Testnet P2SH addresses (version byte 0xc4) are 35 characters long.
20+ if len (addr ) not in range (25 , 36 ):
2021 return False
21- decoded_bytes = _decode_base58 (addr ).to_bytes (25 , "big" )
22+ decoded = _decode_base58 (addr )
23+ # Some 35-character strings decode to more than 25 bytes.
24+ if decoded .bit_length () > 200 :
25+ return False
26+ decoded_bytes = decoded .to_bytes (25 , "big" )
2227 header , checksum = decoded_bytes [:- 4 ], decoded_bytes [- 4 :]
2328 return checksum == sha256 (sha256 (header ).digest ()).digest ()[:4 ]
2429
Original file line number Diff line number Diff line change 1414 "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2" ,
1515 # P2SH (Pay to script hash) type
1616 "3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy" ,
17+ # Testnet P2SH type (35 characters)
18+ "2MzQwSSnBHWHqSAqtTVQ6v47XtaisrJa1Vc" ,
1719 # Bech32/segwit type
1820 "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq" ,
1921 "bc1qc7slrfxkknqcq2jevvvkdgvrt8080852dfjewde450xdlk4ugp7szw5tk9" ,
@@ -36,6 +38,10 @@ def test_returns_true_on_valid_btc_address(value: str):
3638 "1BvBMsEYstWetqTFn5Au4m4GFg7xJaNVN2" ,
3739 # incorrect checksum
3840 "3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLz" ,
41+ # incorrect testnet P2SH checksum
42+ "2MzQwSSnBHWHqSAqtTVQ6v47XtaisrJa1Vd" ,
43+ # 35 characters that decode to more than 25 bytes
44+ "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" ,
3945 ],
4046)
4147def test_returns_failed_validation_on_invalid_btc_address (value : str ):
You can’t perform that action at this time.
0 commit comments