@ -19,13 +19,15 @@ from binascii import b2a_hex
from segwit_addr import bech32_encode , decode , convertbits , CHARSET
# 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
@ -53,8 +55,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_REGTEST , ) , 20 , ( ) , ( False , ' regtest ' , None , None ) , pubkey_prefix , pubkey_suffix ) ,
( ( SCRIPT_ADDRESS_REGTEST , ) , 20 , ( ) , ( False , ' regtest ' , None , None ) , script_prefix , script_suffix ) ,
( ( PRIVKEY , ) , 32 , ( ) , ( True , ' main ' , False , None ) , ( ) , ( ) ) ,
@ -67,28 +71,28 @@ templates = [
# templates for valid bech32 sequences
bech32_templates = [
# hrp, version, witprog_size, metadata, output_prefix
( ' bc' , 0 , 20 , ( False , ' main ' , None , True ) , p2wpkh_prefix ) ,
( ' bc' , 0 , 32 , ( False , ' main ' , None , True ) , p2wsh_prefix ) ,
( ' bc' , 1 , 2 , ( False , ' main ' , None , True ) , ( OP_1 , 2 ) ) ,
( ' t b' , 0 , 20 , ( False , ' test ' , None , True ) , p2wpkh_prefix ) ,
( ' t b' , 0 , 32 , ( False , ' test ' , None , True ) , p2wsh_prefix ) ,
( ' t b' , 2 , 16 , ( False , ' test ' , None , True ) , ( OP_2 , 16 ) ) ,
( ' bc rt' , 0 , 20 , ( False , ' regtest ' , None , True ) , p2wpkh_prefix ) ,
( ' bc rt' , 0 , 32 , ( False , ' regtest ' , None , True ) , p2wsh_prefix ) ,
( ' bc rt' , 16 , 40 , ( False , ' regtest ' , None , True ) , ( OP_16 , 40 ) )
( ' ltc' , 0 , 20 , ( False , ' main ' , None , True ) , p2wpkh_prefix ) ,
( ' ltc' , 0 , 32 , ( False , ' main ' , None , True ) , p2wsh_prefix ) ,
( ' ltc' , 1 , 2 , ( False , ' main ' , None , True ) , ( OP_1 , 2 ) ) ,
( ' t ltc' , 0 , 20 , ( False , ' test ' , None , True ) , p2wpkh_prefix ) ,
( ' t ltc' , 0 , 32 , ( False , ' test ' , None , True ) , p2wsh_prefix ) ,
( ' t ltc' , 2 , 16 , ( False , ' test ' , None , True ) , ( OP_2 , 16 ) ) ,
( ' rl tc ' , 0 , 20 , ( False , ' regtest ' , None , True ) , p2wpkh_prefix ) ,
( ' rl tc ' , 0 , 32 , ( False , ' regtest ' , None , True ) , p2wsh_prefix ) ,
( ' rl tc ' , 16 , 40 , ( False , ' regtest ' , None , True ) , ( OP_16 , 40 ) )
]
# templates for invalid bech32 sequences
bech32_ng_templates = [
# hrp, version, witprog_size, invalid_bech32, invalid_checksum, invalid_char
( ' tc ' , 0 , 20 , False , False , False ) ,
( ' t b' , 17 , 32 , False , False , False ) ,
( ' bc rt' , 3 , 1 , False , False , False ) ,
( ' b c' , 15 , 41 , False , False , False ) ,
( ' t b' , 0 , 16 , False , False , False ) ,
( ' bc rt' , 0 , 32 , True , False , False ) ,
( ' bc' , 0 , 16 , True , False , False ) ,
( ' t b' , 0 , 32 , False , True , False ) ,
( ' bc rt' , 0 , 20 , False , False , True )
( ' t ltc' , 17 , 32 , False , False , False ) ,
( ' rl tc ' , 3 , 1 , False , False , False ) ,
( ' lt c' , 15 , 41 , False , False , False ) ,
( ' t ltc' , 0 , 16 , False , False , False ) ,
( ' rl tc ' , 0 , 32 , True , False , False ) ,
( ' ltc' , 0 , 16 , True , False , False ) ,
( ' t ltc' , 0 , 32 , False , True , False ) ,
( ' rl tc ' , 0 , 20 , False , False , True )
]
def is_valid ( v ) :
@ -108,7 +112,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 ( hrp , v ) != ( None , None ) :
return True
return False