Browse Source

import cleanups

The MMGen Project 3 years ago
parent
commit
29e3a07d6a
5 changed files with 25 additions and 15 deletions
  1. 2 1
      mmgen/addrfile.py
  2. 4 2
      mmgen/crypto.py
  3. 5 1
      mmgen/help.py
  4. 3 5
      mmgen/main_wallet.py
  5. 11 6
      mmgen/protocol.py

+ 2 - 1
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()

+ 4 - 2
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()

+ 5 - 1
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)()

+ 3 - 5
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)

+ 11 - 6
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: