@ -18,13 +18,15 @@ import random
from segwit_addr import bech32_encode , decode_segwit_address , convertbits , CHARSET , Encoding
# key types
PUBKEY_ADDRESS = 0
PUBKEY_ADDRESS = 48
SCRIPT_ADDRESS = 5
SCRIPT_ADDRESS2 = 50
PUBKEY_ADDRESS_TEST = 111
SCRIPT_ADDRESS_TEST = 196
SCRIPT_ADDRESS_TEST2 = 58
PUBKEY_ADDRESS_REGTEST = 111
SCRIPT_ADDRESS_REGTEST = 196
PRIVKEY = 1 28
PRIVKEY = 1 76
PRIVKEY_TEST = 239
PRIVKEY_REGTEST = 239
@ -54,8 +56,10 @@ templates = [
# None = N/A
( ( PUBKEY_ADDRESS , ) , 20 , ( ) , ( False , ' main ' , None , None ) , pubkey_prefix , pubkey_suffix ) ,
( ( SCRIPT_ADDRESS , ) , 20 , ( ) , ( False , ' main ' , None , None ) , script_prefix , script_suffix ) ,
( ( SCRIPT_ADDRESS2 , ) , 20 , ( ) , ( False , ' main ' , None , None ) , script_prefix , script_suffix ) ,
( ( PUBKEY_ADDRESS_TEST , ) , 20 , ( ) , ( False , ' test ' , None , None ) , pubkey_prefix , pubkey_suffix ) ,
( ( SCRIPT_ADDRESS_TEST , ) , 20 , ( ) , ( False , ' test ' , None , None ) , script_prefix , script_suffix ) ,
( ( SCRIPT_ADDRESS_TEST2 , ) , 20 , ( ) , ( False , ' test ' , None , None ) , script_prefix , script_suffix ) ,
( ( PUBKEY_ADDRESS_TEST , ) , 20 , ( ) , ( False , ' signet ' , None , None ) , pubkey_prefix , pubkey_suffix ) ,
( ( SCRIPT_ADDRESS_TEST , ) , 20 , ( ) , ( False , ' signet ' , None , None ) , script_prefix , script_suffix ) ,
( ( PUBKEY_ADDRESS_REGTEST , ) , 20 , ( ) , ( False , ' regtest ' , None , None ) , pubkey_prefix , pubkey_suffix ) ,
@ -72,42 +76,42 @@ templates = [
# templates for valid bech32 sequences
bech32_templates = [
# hrp, version, witprog_size, metadata, encoding, output_prefix
( ' b c' , 0 , 20 , ( False , ' main ' , None , True ) , Encoding . BECH32 , p2wpkh_prefix ) ,
( ' b c' , 0 , 32 , ( False , ' main ' , None , True ) , Encoding . BECH32 , p2wsh_prefix ) ,
( ' b c' , 1 , 32 , ( False , ' main ' , None , True ) , Encoding . BECH32M , p2tr_prefix ) ,
( ' b c' , 2 , 2 , ( False , ' main ' , None , True ) , Encoding . BECH32M , ( OP_2 , 2 ) ) ,
( ' t b ' , 0 , 20 , ( False , ' test ' , None , True ) , Encoding . BECH32 , p2wpkh_prefix ) ,
( ' t b ' , 0 , 32 , ( False , ' test ' , None , True ) , Encoding . BECH32 , p2wsh_prefix ) ,
( ' t b ' , 1 , 32 , ( False , ' test ' , None , True ) , Encoding . BECH32M , p2tr_prefix ) ,
( ' t b ' , 3 , 16 , ( False , ' test ' , None , True ) , Encoding . BECH32M , ( OP_3 , 16 ) ) ,
( ' t b ' , 0 , 20 , ( False , ' signet ' , None , True ) , Encoding . BECH32 , p2wpkh_prefix ) ,
( ' t b ' , 0 , 32 , ( False , ' signet ' , None , True ) , Encoding . BECH32 , p2wsh_prefix ) ,
( ' t b ' , 1 , 32 , ( False , ' signet ' , None , True ) , Encoding . BECH32M , p2tr_prefix ) ,
( ' t b ' , 3 , 32 , ( False , ' signet ' , None , True ) , Encoding . BECH32M , ( OP_3 , 32 ) ) ,
( ' bc rt' , 0 , 20 , ( False , ' regtest ' , None , True ) , Encoding . BECH32 , p2wpkh_prefix ) ,
( ' bc rt' , 0 , 32 , ( False , ' regtest ' , None , True ) , Encoding . BECH32 , p2wsh_prefix ) ,
( ' bc rt' , 1 , 32 , ( False , ' regtest ' , None , True ) , Encoding . BECH32M , p2tr_prefix ) ,
( ' bc rt' , 16 , 40 , ( False , ' regtest ' , None , True ) , Encoding . BECH32M , ( OP_16 , 40 ) )
( ' lt c' , 0 , 20 , ( False , ' main ' , None , True ) , Encoding . BECH32 , p2wpkh_prefix ) ,
( ' lt c' , 0 , 32 , ( False , ' main ' , None , True ) , Encoding . BECH32 , p2wsh_prefix ) ,
( ' lt c' , 1 , 32 , ( False , ' main ' , None , True ) , Encoding . BECH32M , p2tr_prefix ) ,
( ' lt c' , 2 , 2 , ( False , ' main ' , None , True ) , Encoding . BECH32M , ( OP_2 , 2 ) ) ,
( ' t ltc ' , 0 , 20 , ( False , ' test ' , None , True ) , Encoding . BECH32 , p2wpkh_prefix ) ,
( ' t ltc ' , 0 , 32 , ( False , ' test ' , None , True ) , Encoding . BECH32 , p2wsh_prefix ) ,
( ' t ltc ' , 1 , 32 , ( False , ' test ' , None , True ) , Encoding . BECH32M , p2tr_prefix ) ,
( ' t ltc ' , 3 , 16 , ( False , ' test ' , None , True ) , Encoding . BECH32M , ( OP_3 , 16 ) ) ,
( ' t ltc ' , 0 , 20 , ( False , ' signet ' , None , True ) , Encoding . BECH32 , p2wpkh_prefix ) ,
( ' t ltc ' , 0 , 32 , ( False , ' signet ' , None , True ) , Encoding . BECH32 , p2wsh_prefix ) ,
( ' t ltc ' , 1 , 32 , ( False , ' signet ' , None , True ) , Encoding . BECH32M , p2tr_prefix ) ,
( ' t ltc ' , 3 , 32 , ( False , ' signet ' , None , True ) , Encoding . BECH32M , ( OP_3 , 32 ) ) ,
( ' rl tc ' , 0 , 20 , ( False , ' regtest ' , None , True ) , Encoding . BECH32 , p2wpkh_prefix ) ,
( ' rl tc ' , 0 , 32 , ( False , ' regtest ' , None , True ) , Encoding . BECH32 , p2wsh_prefix ) ,
( ' rl tc ' , 1 , 32 , ( False , ' regtest ' , None , True ) , Encoding . BECH32M , p2tr_prefix ) ,
( ' rl tc ' , 16 , 40 , ( False , ' regtest ' , None , True ) , Encoding . BECH32M , ( OP_16 , 40 ) )
]
# templates for invalid bech32 sequences
bech32_ng_templates = [
# hrp, version, witprog_size, encoding, invalid_bech32, invalid_checksum, invalid_char
( ' tc ' , 0 , 20 , Encoding . BECH32 , False , False , False ) ,
( ' bt ' , 1 , 32 , Encoding . BECH32M , False , False , False ) ,
( ' t b ' , 17 , 32 , Encoding . BECH32M , False , False , False ) ,
( ' bc rt' , 3 , 1 , Encoding . BECH32M , False , False , False ) ,
( ' b c' , 15 , 41 , Encoding . BECH32M , False , False , False ) ,
( ' t b ' , 0 , 16 , Encoding . BECH32 , False , False , False ) ,
( ' bc rt' , 0 , 32 , Encoding . BECH32 , True , False , False ) ,
( ' b c' , 0 , 16 , Encoding . BECH32 , True , False , False ) ,
( ' t b ' , 0 , 32 , Encoding . BECH32 , False , True , False ) ,
( ' bc rt' , 0 , 20 , Encoding . BECH32 , False , False , True ) ,
( ' b c' , 0 , 20 , Encoding . BECH32M , False , False , False ) ,
( ' t b ' , 0 , 32 , Encoding . BECH32M , False , False , False ) ,
( ' bc rt' , 0 , 20 , Encoding . BECH32M , False , False , False ) ,
( ' b c' , 1 , 32 , Encoding . BECH32 , False , False , False ) ,
( ' t b ' , 2 , 16 , Encoding . BECH32 , False , False , False ) ,
( ' bc rt' , 16 , 20 , Encoding . BECH32 , False , False , False ) ,
( ' t ltc ' , 17 , 32 , Encoding . BECH32M , False , False , False ) ,
( ' rl tc ' , 3 , 1 , Encoding . BECH32M , False , False , False ) ,
( ' lt c' , 15 , 41 , Encoding . BECH32M , False , False , False ) ,
( ' t ltc ' , 0 , 16 , Encoding . BECH32 , False , False , False ) ,
( ' rl tc ' , 0 , 32 , Encoding . BECH32 , True , False , False ) ,
( ' lt c' , 0 , 16 , Encoding . BECH32 , True , False , False ) ,
( ' t ltc ' , 0 , 32 , Encoding . BECH32 , False , True , False ) ,
( ' rl tc ' , 0 , 20 , Encoding . BECH32 , False , False , True ) ,
( ' lt c' , 0 , 20 , Encoding . BECH32M , False , False , False ) ,
( ' t ltc ' , 0 , 32 , Encoding . BECH32M , False , False , False ) ,
( ' rl tc ' , 0 , 20 , Encoding . BECH32M , False , False , False ) ,
( ' lt c' , 1 , 32 , Encoding . BECH32 , False , False , False ) ,
( ' t ltc ' , 2 , 16 , Encoding . BECH32 , False , False , False ) ,
( ' rl tc ' , 16 , 20 , Encoding . BECH32 , False , False , False ) ,
]
def is_valid ( v ) :
@ -127,7 +131,7 @@ def is_valid(v):
def is_valid_bech32 ( v ) :
''' Check vector v for bech32 validity '''
for hrp in [ ' bc' , ' tb ' , ' bc rt' ] :
for hrp in [ ' ltc' , ' tltc ' , ' rl tc ' ] :
if decode_segwit_address ( hrp , v ) != ( None , None ) :
return True
return False