From 786a10c937a7cba014068de29034b15e12bf9d25 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Tue, 18 Jan 2022 09:10:58 +0000 Subject: [PATCH] baseconv: disable Tirosh wordlist, minor cleanups in tool.py --- mmgen/baseconv.py | 11 ++++------- mmgen/tool.py | 37 +++++++++++++------------------------ 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/mmgen/baseconv.py b/mmgen/baseconv.py index f575ba09..ab36b821 100755 --- a/mmgen/baseconv.py +++ b/mmgen/baseconv.py @@ -42,7 +42,7 @@ class baseconv(object): 'b10': ('base10 string', 'base10 (decimal) string data'), 'b8': ('base8 string', 'base8 (octal) string data'), 'b6d': ('base6d (die roll)', 'base6 data using the digits from one to six'), - 'tirosh':('Tirosh mnemonic', 'base1626 mnemonic using truncated Tirosh wordlist'), # not used by wallet +# 'tirosh':('Tirosh mnemonic', 'base1626 mnemonic using truncated Tirosh wordlist'), # not used by wallet 'mmgen': ('MMGen native mnemonic', 'MMGen native mnemonic seed phrase created using old Electrum wordlist and simple base conversion'), 'xmrseed': ('Monero mnemonic', 'Monero new-style mnemonic seed phrase'), @@ -57,12 +57,12 @@ class baseconv(object): 'b8': tuple('01234567'), 'b6d': tuple('123456'), } - mn_base = 1626 # tirosh list is 1633 words long! + mn_base = 1626 wl_chksums = { 'mmgen': '5ca31424', 'xmrseed':'3c381ebb', - 'tirosh': '48f05e1f', # tirosh truncated to mn_base (1626) - # 'tirosh1633': '1a5faeff' +# 'tirosh': '48f05e1f', # tirosh truncated to mn_base +# 'tirosh1633': '1a5faeff' # tirosh list is 1633 words long! } seedlen_map = { 'b58': { 16:22, 24:33, 32:44 }, @@ -85,9 +85,6 @@ class baseconv(object): elif wl_id == 'xmrseed': from .mn_monero import words self.digits[wl_id] = words - elif wl_id == 'tirosh': - from .mn_tirosh import words - self.digits[wl_id] = words[:self.mn_base] elif wl_id not in self.digits: raise ValueError(f'{wl_id}: unrecognized mnemonic ID') diff --git a/mmgen/tool.py b/mmgen/tool.py index b605595f..b6489ff0 100755 --- a/mmgen/tool.py +++ b/mmgen/tool.py @@ -29,6 +29,7 @@ from .addr import * from .addrlist import AddrList,KeyAddrList from .passwdlist import PasswordList from .baseconv import baseconv +from .bip39 import bip39 NL = ('\n','\r\n')[g.platform=='win'] @@ -232,15 +233,12 @@ def _process_result(ret,pager=False,print_result=False): from .addr import MMGenAddrType -def conv_cls_bip39(): - from .bip39 import bip39 - return bip39 - dfl_mnemonic_fmt = 'mmgen' +mft = namedtuple('mnemonic_format',['fmt','pad','conv_cls']) mnemonic_fmts = { - 'mmgen': { 'fmt': 'words', 'conv_cls': lambda: baseconv }, - 'bip39': { 'fmt': 'bip39', 'conv_cls': conv_cls_bip39 }, - 'xmrseed': { 'fmt': 'xmrseed','conv_cls': lambda: baseconv }, + 'mmgen': mft( 'words', 'seed', baseconv ), + 'bip39': mft( 'bip39', None, bip39 ), + 'xmrseed': mft( 'xmrseed', None, baseconv ), } mn_opts_disp = _options_annot_str(mnemonic_fmts) @@ -623,22 +621,15 @@ class MMGenToolCmdMnemonic(MMGenToolCmds): def hex2mn( self, hexstr:'sstr', fmt:mn_opts_disp = dfl_mnemonic_fmt ): "convert a 16, 24 or 32-byte hexadecimal number to a mnemonic seed phrase" - if fmt == 'bip39': - from .bip39 import bip39 - return ' '.join(bip39(fmt).fromhex(hexstr)) - else: - bytestr = bytes.fromhex(hexstr) - if fmt == 'xmrseed': - bytestr = self._xmr_reduce(bytestr) - return baseconv(fmt).frombytes(bytestr,'seed',tostr=True) + if fmt == 'xmrseed': + hexstr = self._xmr_reduce(bytes.fromhex(hexstr)).hex() + f = mnemonic_fmts[fmt] + return ' '.join( f.conv_cls(fmt).fromhex(hexstr,f.pad) ) def mn2hex( self, seed_mnemonic:'sstr', fmt:mn_opts_disp = dfl_mnemonic_fmt ): "convert a mnemonic seed phrase to a hexadecimal number" - if fmt == 'bip39': - from .bip39 import bip39 - return bip39(fmt).tohex(seed_mnemonic.split()) - else: - return baseconv(fmt).tohex(seed_mnemonic.split(),'seed') + f = mnemonic_fmts[fmt] + return f.conv_cls(fmt).tohex( seed_mnemonic.split(), f.pad ) def mn2hex_interactive( self, fmt:mn_opts_disp = dfl_mnemonic_fmt, mn_len=24, print_mn=False ): "convert an interactively supplied mnemonic seed phrase to a hexadecimal number" @@ -650,13 +641,11 @@ class MMGenToolCmdMnemonic(MMGenToolCmds): def mn_stats(self, fmt:mn_opts_disp = dfl_mnemonic_fmt ): "show stats for mnemonic wordlist" - conv_cls = mnemonic_fmts[fmt]['conv_cls']() - return conv_cls(fmt).check_wordlist() + return mnemonic_fmts[fmt].conv_cls(fmt).check_wordlist() def mn_printlist( self, fmt:mn_opts_disp = dfl_mnemonic_fmt, enum=False, pager=False ): "print mnemonic wordlist" - conv_cls = mnemonic_fmts[fmt]['conv_cls']() - ret = conv_cls(fmt).get_wordlist() + ret = mnemonic_fmts[fmt].conv_cls(fmt).get_wordlist() if enum: ret = [f'{n:>4} {e}' for n,e in enumerate(ret)] return '\n'.join(ret)