1414import json
1515import os
1616import unittest
17+ import array
18+ import sys
19+ _bchr = chr
20+ _bord = ord
21+ _tobytes = lambda x : array .array ('B' , x ).tostring ()
22+ if sys .version > '3' :
23+ long = int
24+ _bchr = lambda x : bytes ([x ])
25+ _bord = lambda x : x
26+ _tobytes = bytes
1727
1828from binascii import unhexlify
1929
@@ -28,7 +38,7 @@ def load_test_vectors(name):
2838
2939def to_scriptPubKey (witver , witprog ):
3040 """Decoded bech32 address to script"""
31- return CScript ([witver ]) + CScript (bytes (witprog ))
41+ return CScript ([witver ]) + CScript (_tobytes (witprog ))
3242
3343class Test_bech32 (unittest .TestCase ):
3444
@@ -42,15 +52,15 @@ def op_decode(self, witver):
4252
4353 def test_encode_decode (self ):
4454 for exp_bin , exp_bech32 in load_test_vectors ('bech32_encode_decode.json' ):
45- exp_bin = unhexlify (exp_bin .encode ('utf8' ))
55+ exp_bin = [ _bord ( y ) for y in unhexlify (exp_bin .encode ('utf8' ))]
4656 witver = self .op_decode (exp_bin [0 ])
4757 hrp = exp_bech32 [:exp_bech32 .rindex ('1' )].lower ()
4858 self .assertEqual (exp_bin [1 ], len (exp_bin [2 :]))
4959 act_bech32 = encode (hrp , witver , exp_bin [2 :])
5060 act_bin = decode (hrp , exp_bech32 )
5161
5262 self .assertEqual (act_bech32 .lower (), exp_bech32 .lower ())
53- self .assertEqual (to_scriptPubKey (* act_bin ), exp_bin )
63+ self .assertEqual (to_scriptPubKey (* act_bin ), _tobytes ( exp_bin ) )
5464
5565class Test_CBech32Data (unittest .TestCase ):
5666 def test_from_data (self ):
0 commit comments