From 29e3a07d6a3c5d30019daaa039a2b1f0c3e59e92 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Thu, 27 Jan 2022 11:08:07 +0000 Subject: [PATCH] import cleanups --- mmgen/addrfile.py | 3 ++- mmgen/crypto.py | 6 ++++-- mmgen/help.py | 6 +++++- mmgen/main_wallet.py | 8 +++----- mmgen/protocol.py | 17 +++++++++++------ 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/mmgen/addrfile.py b/mmgen/addrfile.py index 165e6f01..88152d94 100755 --- a/mmgen/addrfile.py +++ b/mmgen/addrfile.py @@ -34,7 +34,6 @@ from .seed import SeedID,is_seed_id from .key import PrivKey from .addr import ViewKey,AddrListID,MMGenPasswordType,is_addr_idx from .addrlist import KeyList,AddrListData,dmsg_sc -from .passwdlist import PasswordList class AddrFile(MMGenObject): desc = 'addresses' @@ -109,6 +108,7 @@ class AddrFile(MMGenObject): out.append(f'{lbl} {{') fs = ' {:<%s} {:<34}{}' % len(str(p.data[-1].idx)) + from .passwdlist import PasswordList for e in p.data: c = ' '+e.label if add_comments and e.label else '' if type(p) == KeyList: @@ -228,6 +228,7 @@ class AddrFile(MMGenObject): from .fileutil import get_lines_from_file lines = get_lines_from_file(fn,p.desc+' data',trim_comments=True) + from .passwdlist import PasswordList try: assert len(lines) >= 3, f'Too few lines in address file ({len(lines)})' ls = lines[0].split() diff --git a/mmgen/crypto.py b/mmgen/crypto.py index 255f9b33..01ba295c 100755 --- a/mmgen/crypto.py +++ b/mmgen/crypto.py @@ -21,8 +21,6 @@ crypto.py: Random number, password hashing and symmetric encryption routines for """ import os -from cryptography.hazmat.primitives.ciphers import Cipher,algorithms,modes -from cryptography.hazmat.backends import default_backend from hashlib import sha256 from collections import namedtuple @@ -115,6 +113,8 @@ def decrypt_seed(enc_seed,key,seed_id,key_id): return dec_seed def encrypt_data(data,key,iv=aesctr_dfl_iv,desc='data',verify=True): + from cryptography.hazmat.primitives.ciphers import Cipher,algorithms,modes + from cryptography.hazmat.backends import default_backend vmsg(f'Encrypting {desc}') c = Cipher(algorithms.AES(key),modes.CTR(iv),backend=default_backend()) encryptor = c.encryptor() @@ -132,6 +132,8 @@ def encrypt_data(data,key,iv=aesctr_dfl_iv,desc='data',verify=True): return enc_data def decrypt_data(enc_data,key,iv=aesctr_dfl_iv,desc='data'): + from cryptography.hazmat.primitives.ciphers import Cipher,algorithms,modes + from cryptography.hazmat.backends import default_backend vmsg_r(f'Decrypting {desc} with key...') c = Cipher(algorithms.AES(key),modes.CTR(iv),backend=default_backend()) encryptor = c.encryptor() diff --git a/mmgen/help.py b/mmgen/help.py index 01229d46..8d5a9b67 100755 --- a/mmgen/help.py +++ b/mmgen/help.py @@ -39,6 +39,10 @@ def help_notes_func(proto,po,k): class help_notes: + def MasterShareIdx(): + from .seedsplit import MasterShareIdx + return MasterShareIdx + def tool_help(): from .tool.help import main_help return main_help() @@ -312,4 +316,4 @@ subwallets from a single parent. This leaves each user with a total of two million subwallets, which should be enough for most practical purposes. """.strip() - return getattr(help_notes,k)() + ('-α' if g.debug_utf8 else '') + return getattr(help_notes,k)() diff --git a/mmgen/main_wallet.py b/mmgen/main_wallet.py index b9a457a7..9985ab16 100755 --- a/mmgen/main_wallet.py +++ b/mmgen/main_wallet.py @@ -22,8 +22,6 @@ mmgen/main_wallet: Entry point for MMGen wallet-related scripts import os from .common import * -from .obj import MMGenWalletLabel -from .seedsplit import MasterShareIdx from .wallet import Wallet,MMGenWallet from .filename import find_file_in_dir @@ -129,8 +127,8 @@ FMT CODES: 'options': lambda help_notes,s: s.format( iaction=capfirst(iaction), oaction=capfirst(oaction), - ms_min=MasterShareIdx.min_val, - ms_max=MasterShareIdx.max_val, + ms_min=help_notes('MasterShareIdx').min_val, + ms_max=help_notes('MasterShareIdx').max_val, dsl=help_notes('dfl_seed_len'), g=g, ), @@ -151,7 +149,7 @@ if invoked_as == 'subgen': ss_idx = SubSeedIdx(cmd_args.pop()) elif invoked_as == 'seedsplit': from .obj import get_obj - from .seedsplit import SeedSplitSpecifier + from .seedsplit import SeedSplitSpecifier,MasterShareIdx master_share = MasterShareIdx(opt.master_share) if opt.master_share else None if cmd_args: sss = get_obj(SeedSplitSpecifier,s=cmd_args.pop(),silent=True) diff --git a/mmgen/protocol.py b/mmgen/protocol.py index a3cc5b3f..3871a31b 100755 --- a/mmgen/protocol.py +++ b/mmgen/protocol.py @@ -23,10 +23,8 @@ protocol.py: Coin protocol functions, classes and methods import sys,os,hashlib from collections import namedtuple -from .util import msg,ymsg,Msg,ydie from .devtools import * from .globalvars import g -import mmgen.bech32 as bech32 from .amt import BTCAmt,LTCAmt,BCHAmt,XMRAmt,ETHAmt parsed_wif = namedtuple('parsed_wif',['sec','pubkey_type','compressed']) @@ -193,6 +191,7 @@ class CoinProtocol(MMGenObject): if 0 < int.from_bytes(sec,'big') < self.secp256k1_ge: return sec else: # chance of this is less than 1 in 2^127 + from .util import ydie pk = int.from_bytes(sec,'big') if pk == 0: # chance of this is 1 in 2^256 ydie(3,'Private key is zero!') @@ -200,6 +199,7 @@ class CoinProtocol(MMGenObject): ydie(3,'Private key == secp256k1_ge!') else: if not g.test_suite: + from .util import ymsg ymsg(f'Warning: private key is greater than secp256k1 group order!:\n {hexpriv}') return (pk % self.secp256k1_ge).to_bytes(self.privkey_len,'big') @@ -274,9 +274,11 @@ class CoinProtocol(MMGenObject): def parse_addr(self,addr): if 'B' in self.mmtypes and addr[:len(self.bech32_hrp)] == self.bech32_hrp: + import mmgen.bech32 as bech32 ret = bech32.decode(self.bech32_hrp,addr) if ret[0] != self.witness_vernum: + from .util import msg msg(f'{ret[0]}: Invalid witness version number') return False @@ -303,6 +305,7 @@ class CoinProtocol(MMGenObject): def pubhash2bech32addr(self,pubhash): d = list(pubhash) + import mmgen.bech32 as bech32 return bech32.bech32_encode(self.bech32_hrp,[self.witness_vernum]+bech32.convertbits(d,8,5)) class BitcoinTestnet(Bitcoin): @@ -415,6 +418,7 @@ class CoinProtocol(MMGenObject): if is_hex_str_lc(addr) and len(addr) == self.addr_len * 2: return parsed_addr( bytes.fromhex(addr), 'ethereum' ) if g.debug: + from .util import Msg Msg(f'Invalid address: {addr}') return False @@ -509,7 +513,7 @@ class CoinProtocol(MMGenObject): def parse_addr(self,addr): - from .baseconv import baseconv,is_b58_str + from .baseconv import baseconv def b58dec(addr_str): bc = baseconv('b58') @@ -576,20 +580,21 @@ def init_genonly_altcoins(usr_coin=None,testnet=False): If usr_coin is None, initializes all coins for current network with trust level >-1. Returns trust_level of usr_coin, or 0 (untrusted) if usr_coin is None. """ - from .altcoin import CoinInfo as ci data = { 'mainnet': (), 'testnet': () } networks = ['mainnet'] + (['testnet'] if testnet else []) network = 'testnet' if testnet else 'mainnet' if usr_coin == None: + from .altcoin import CoinInfo for network in networks: - data[network] = ci.get_supported_coins(network) + data[network] = CoinInfo.get_supported_coins(network) trust_level = 0 else: if usr_coin.lower() in CoinProtocol.core_coins: # core coin, so return immediately return CoinProtocol.coins[usr_coin.lower()].trust_level + from .altcoin import CoinInfo for network in networks: - data[network] = (ci.get_entry(usr_coin,network),) + data[network] = (CoinInfo.get_entry(usr_coin,network),) cinfo = data[network][0] if not cinfo: