From ca994bdd74850c58d58764db84892ed2cb92c470 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 28 Oct 2019 18:45:15 +0000 Subject: [PATCH] use baseconv.{to,from}bytes() wherever feasible - also fixes a padding bug in the b58tobytes() tool method --- mmgen/addr.py | 8 ++++---- mmgen/tool.py | 4 ++-- test/tooltest2.py | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/mmgen/addr.py b/mmgen/addr.py index c54cd016..42d2b8c7 100755 --- a/mmgen/addr.py +++ b/mmgen/addr.py @@ -170,10 +170,10 @@ class AddrGeneratorMonero(AddrGenerator): return AddrGenerator.__init__(addr_type) def b58enc(self,addr_bytes): - enc = baseconv.fromhex + enc = baseconv.frombytes l = len(addr_bytes) - a = ''.join([enc((addr_bytes[i*8:i*8+8]).hex(),'b58',pad=11,tostr=True) for i in range(l//8)]) - b = enc((addr_bytes[l-l%8:]).hex(),'b58',pad=7,tostr=True) + a = ''.join([enc(addr_bytes[i*8:i*8+8],'b58',pad=11,tostr=True) for i in range(l//8)]) + b = enc(addr_bytes[l-l%8:],'b58',pad=7,tostr=True) return a + b def to_addr(self,sk_hex): # sk_hex instead of pubhex @@ -923,7 +923,7 @@ Record this checksum: it will be used to verify the password file in the future elif pf in ('b32','b58'): pw_int = (32 if pf == 'b32' else 58) ** self.pw_len pw_bytes = pw_int.bit_length() // 8 - good_pw_len = len(baseconv.fromhex('ff'*seed.byte_len,wl_id=pf)) + good_pw_len = len(baseconv.frombytes(b'\xff'*seed.byte_len,wl_id=pf)) else: raise NotImplementedError('{!r}: unknown password format'.format(pf)) diff --git a/mmgen/tool.py b/mmgen/tool.py index 1307488c..25cc078d 100755 --- a/mmgen/tool.py +++ b/mmgen/tool.py @@ -321,11 +321,11 @@ class MMGenToolCmdUtil(MMGenToolCmdBase): def bytestob58(self,infile:str,pad=0): "convert bytes to base 58 (supply data via STDIN)" data = get_data_from_file(infile,dash=True,quiet=True,binary=True) - return baseconv.fromhex(data.hex(),'b58',pad=pad,tostr=True) + return baseconv.frombytes(data,'b58',pad=pad,tostr=True) def b58tobytes(self,b58num:'sstr',pad=0): "convert a base 58 number to bytes (warning: outputs binary data)" - return bytes.fromhex(baseconv.tohex(b58num,'b58',pad=pad)) + return baseconv.tobytes(b58num,'b58',pad=pad) def hextob58(self,hexstr:'sstr',pad=0): "convert a hexadecimal number to base 58" diff --git a/test/tooltest2.py b/test/tooltest2.py index 6694049c..32fef564 100755 --- a/test/tooltest2.py +++ b/test/tooltest2.py @@ -204,9 +204,9 @@ tests = { ( ['6h8cQN'], b'\xde\xad\xbe\xef' ), ( ['eFGDJURJykA'], b'\xde\xad\xbe\xef\xde\xad\xbe\xef' ), ( ['jpXCZedGfVQ'], b'\xff\xff\xff\xff\xff\xff\xff\xff' ), - ( ['1','pad=16'], b'\x00\x00\x00\x00\x00\x00\x00\x00' ), - ( ['1111111111','pad=16'], b'\x00\x00\x00\x00\x00\x00\x00\x00' ), - ( ['111111115Q','pad=2'], b'\xff' ), + ( ['1','pad=8'], b'\x00\x00\x00\x00\x00\x00\x00\x00' ), + ( ['1111111111','pad=8'], b'\x00\x00\x00\x00\x00\x00\x00\x00' ), + ( ['111111115Q','pad=1'], b'\xff' ), ], 'hextob58': [ ( ['deadbeef'], '6h8cQN' ),