whitespace, cleanups, minor fixes

This commit is contained in:
The MMGen Project 2022-01-15 14:00:09 +00:00
commit 0138a6fa49
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
28 changed files with 147 additions and 93 deletions

View file

@ -279,10 +279,3 @@ class KeyGeneratorDummy(KeyGenerator):
return PubKey(
s = privhex,
privkey = privhex )
def is_bip39_str(s):
from .bip39 import bip39
return bool(bip39.tohex(s.split(),wl_id='bip39'))
def is_xmrseed(s):
return bool(baseconv.tobytes(s.split(),wl_id='xmrseed'))

View file

@ -57,10 +57,10 @@ class AddrFile(MMGenObject):
self.infile = None
def encrypt(self,desc='new key list'):
from .crypto import mmgen_encrypt
from .crypto import mmgen_encrypt,mmenc_ext
from .globalvars import g
self.fmt_data = mmgen_encrypt(self.fmt_data.encode(),desc,'')
self.ext += f'.{g.mmenc_ext}'
self.ext += f'.{mmenc_ext}'
@property
def filename(self):

View file

@ -166,6 +166,7 @@ class EthereumTokenTrackingWallet(EthereumTrackingWallet):
assert token_addr == None,'EthereumTokenTrackingWallet_chk1'
token_addr = await self.sym2addr(proto.tokensym) # returns None on failure
if not is_coin_addr(proto,token_addr):
from mmgen.exception import UnrecognizedTokenSymbol
raise UnrecognizedTokenSymbol(f'Specified token {proto.tokensym!r} could not be resolved!')
from mmgen.obj import TokenAddr
@ -246,7 +247,9 @@ Actions: [q]uit view, [p]rint to file, pager [v]iew, [w]ide view,
'l':'a_lbl_add','D':'a_addr_delete','R':'a_balance_refresh' }
async def __init__(self,proto,*args,**kwargs):
from mmgen.globalvars import g
if g.cached_balances:
from mmgen.color import yellow
self.hdr_fmt += '\n' + yellow('WARNING: Using cached balances. These may be out of date!')
await TwUnspentOutputs.__init__(self,proto,*args,**kwargs)

View file

@ -27,6 +27,7 @@ from mmgen.obj import *
from mmgen.tx import MMGenTX
from mmgen.tw import TrackingWallet
from .contract import Token
from .obj import ETHAmt,ETHNonce
@ -268,6 +269,7 @@ class EthereumMMGenTX:
\n""".replace('\t','')
t = self.txobj
td = t['data']
from mmgen.color import yellow
return fs.format(
*((t[k] if t[k] != '' else Str('None')).hl() for k in self.fmt_keys),
d = '{}... ({} bytes)'.format(td[:40],len(td)//2) if len(td) else Str('None'),

View file

@ -168,9 +168,6 @@ class BTCAmt(CoinAmt):
class BCHAmt(BTCAmt):
pass
class B2XAmt(BTCAmt):
pass
class LTCAmt(BTCAmt):
max_amt = 84000000

View file

@ -24,8 +24,14 @@ from hashlib import sha256
from .exception import *
from .util import die
def is_b58_str(s): return set(list(s)) <= set(baseconv.digits['b58'])
def is_b32_str(s): return set(list(s)) <= set(baseconv.digits['b32'])
def is_b58_str(s):
return set(list(s)) <= set(baseconv.digits['b58'])
def is_b32_str(s):
return set(list(s)) <= set(baseconv.digits['b32'])
def is_xmrseed(s):
return bool(baseconv.tobytes(s.split(),wl_id='xmrseed'))
class baseconv(object):

View file

@ -8,7 +8,6 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
#
# Unaltered except for the following changes by the MMGen Project:
# 'python3' changed to 'python' in the hashbang
# leading spaces converted to tabs
#
"""Reference implementation for Bech32 and segwit addresses."""

View file

@ -2109,6 +2109,14 @@ zoo
except:
raise ValueError(f'{seed_bits!r}: invalid seed length for BIP39 mnemonic')
@classmethod
def tobytes(cls,*args,**kwargs):
raise NotImplementedError('Method not supported')
@classmethod
def frombytes(cls,*args,**kwargs):
raise NotImplementedError('Method not supported')
@classmethod
def tohex(cls,words,wl_id,pad=None):
assert isinstance(words,(list,tuple)),'words must be list or tuple'
@ -2175,3 +2183,6 @@ zoo
@classmethod
def init_mn(cls,mn_id):
assert mn_id == 'bip39', "'mn_id' must be 'bip39'"
def is_bip39_str(s):
return bool(bip39.tohex(s.split(),wl_id='bip39'))

View file

@ -25,6 +25,8 @@ from cryptography.hazmat.backends import default_backend
from hashlib import sha256
from .common import *
mmenc_ext = 'mmenc'
def sha256_rounds(s,n):
for i in range(n):
s = sha256(s).digest()

View file

@ -266,7 +266,6 @@ class GlobalContext(Lockable):
scramble_hash_rounds = 10
subseeds = 100
mmenc_ext = 'mmenc'
salt_len = 16
aesctr_iv_len = 16
aesctr_dfl_iv = int.to_bytes(1,aesctr_iv_len,'big')

View file

@ -20,7 +20,7 @@
help.py: help notes for MMGen suite commands
"""
def help_notes_func(proto,k):
def help_notes_func(proto,po,k):
from .globalvars import g
def fee_spec_letters(use_quotes=False):

View file

@ -139,7 +139,8 @@ addr_type = MMGenAddrType(
id_str = opt.type or proto.dfl_mmtype,
errmsg = errmsg )
if len(cmd_args) < 1: opts.usage()
if len(cmd_args) < 1:
opts.usage()
if getattr(opt,'use_old_ed25519',False):
msg('Using old (slow) ed25519 module by user request')

View file

@ -57,11 +57,18 @@ def get_obj(objname,*args,**kwargs):
else:
return True if return_bool else ret
def is_addr_idx(s): return get_obj(AddrIdx, n=s, silent=True,return_bool=True)
def is_addrlist_id(s): return get_obj(AddrListID, sid=s, silent=True,return_bool=True)
def is_addr_idx(s):
return get_obj(AddrIdx, n=s, silent=True, return_bool=True)
def is_addrlist_id(s):
return get_obj(AddrListID, sid=s, silent=True, return_bool=True)
def is_mmgen_id(proto,s):
return get_obj(MMGenID, proto=proto, id_str=s, silent=True, return_bool=True)
def is_coin_addr(proto,s):
return get_obj(CoinAddr, proto=proto, addr=s, silent=True, return_bool=True)
def is_mmgen_id(proto,s): return get_obj(MMGenID, proto=proto, id_str=s, silent=True,return_bool=True)
def is_coin_addr(proto,s): return get_obj(CoinAddr, proto=proto, addr=s, silent=True,return_bool=True)
# dict that keeps a list of keys for efficient lookup by index
class IndexedDict(dict):
@ -92,9 +99,14 @@ class IndexedDict(dict):
def die(self,desc):
raise NotImplementedError(f'{desc} not implemented for type {type(self).__name__}')
class MMGenList(list,MMGenObject): pass
class MMGenDict(dict,MMGenObject): pass
class Str(str,Hilite): pass
class MMGenList(list,MMGenObject):
pass
class MMGenDict(dict,MMGenObject):
pass
class Str(str,Hilite):
pass
class Int(int,Hilite,InitErrors):
min_val = None
@ -253,8 +265,11 @@ class MMGenListItem(MMGenObject):
def _asdict(self):
return dict((k,v) for k,v in self.__dict__.items() if k in self.valid_attrs)
class MMGenIdx(Int): min_val = 1
class AddrIdx(MMGenIdx): max_digits = 7
class MMGenIdx(Int):
min_val = 1
class AddrIdx(MMGenIdx):
max_digits = 7
class MMGenRange(tuple,InitErrors,MMGenObject):
@ -428,10 +443,17 @@ class HexStr(str,Hilite,InitErrors):
except Exception as e:
return cls.init_fail(e,s)
class CoinTxID(HexStr): color,width,hexcase = 'purple',64,'lower'
class WalletPassword(HexStr): color,width,hexcase = 'blue',32,'lower'
class MoneroViewKey(HexStr): color,width,hexcase = 'cyan',64,'lower' # FIXME - no checking performed
class MMGenTxID(HexStr): color,width,hexcase = 'red',6,'upper'
class CoinTxID(HexStr):
color,width,hexcase = ('purple',64,'lower')
class WalletPassword(HexStr):
color,width,hexcase = ('blue',32,'lower')
class MoneroViewKey(HexStr):
color,width,hexcase = ('cyan',64,'lower') # FIXME - no checking performed
class MMGenTxID(HexStr):
color,width,hexcase = ('red',6,'upper')
class AddrListID(str,Hilite,InitErrors,MMGenObject):
width = 10

View file

@ -25,9 +25,10 @@ from collections import namedtuple
from .exception import InvalidPasswdFormat
from .util import ymsg,is_hex_str,is_int,keypress_confirm
from .obj import ImmutableAttr,ListItemAttr,MMGenPWIDString
from .baseconv import baseconv,is_b32_str,is_b58_str
from .baseconv import baseconv,is_b32_str,is_b58_str,is_xmrseed
from .bip39 import is_bip39_str
from .key import PrivKey
from .addr import MMGenPasswordType,AddrIdx,AddrListID,is_xmrseed,is_bip39_str
from .addr import MMGenPasswordType,AddrIdx,AddrListID
from .addrlist import (
AddrListChksum,
AddrListIDStr,
@ -197,13 +198,18 @@ class PasswordList(AddrList):
elif self.pw_fmt == 'xmrseed':
pw_len_hex = baseconv.seedlen_map_rev['xmrseed'][self.pw_len] * 2
# take most significant part
bytes_trunc = bytes.fromhex(hex_sec[:pw_len_hex])
from .protocol import init_proto
bytes_preproc = init_proto('xmr').preprocess_key(bytes_trunc,None)
bytes_preproc = init_proto('xmr').preprocess_key(
bytes.fromhex(hex_sec[:pw_len_hex]),
None )
return ' '.join(baseconv.frombytes(bytes_preproc,wl_id='xmrseed'))
else:
# take least significant part
return baseconv.fromhex(hex_sec,self.pw_fmt,pad=self.pw_len,tostr=True)[-self.pw_len:]
return baseconv.fromhex(
hex_sec,
self.pw_fmt,
pad = self.pw_len,
tostr = True )[-self.pw_len:]
def check_format(self,pw):
if not self.pw_info[self.pw_fmt].chk_func(pw):

View file

@ -27,7 +27,7 @@ from .util import msg,ymsg,Msg,ydie
from .devtools import *
from .obj import CoinAddr,MMGenAddrType
from .globalvars import g
from .amt import BTCAmt,LTCAmt,BCHAmt,B2XAmt,XMRAmt
from .amt import BTCAmt,LTCAmt,BCHAmt,XMRAmt
from .altcoins.eth.obj import ETHAmt
import mmgen.bech32 as bech32
@ -52,13 +52,13 @@ _b58a='123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
# The 'zero address':
# 1111111111111111111114oLvT2 (pubkeyhash = '\0'*20)
def _b58chk_encode(bstr):
lzeroes = len(bstr) - len(bstr.lstrip(b'\x00'))
def _b58chk_encode(in_bytes):
lzeroes = len(in_bytes) - len(in_bytes.lstrip(b'\x00'))
def do_enc(n):
while n:
yield _b58a[n % 58]
n //= 58
return ('1' * lzeroes) + ''.join(do_enc(int.from_bytes(bstr+hash256bytes(bstr)[:4],'big')))[::-1]
return ('1' * lzeroes) + ''.join(do_enc(int.from_bytes(in_bytes+hash256bytes(in_bytes)[:4],'big')))[::-1]
def _b58chk_decode(s):
lzeroes = len(s) - len(s.lstrip('1'))
@ -177,9 +177,11 @@ class CoinProtocol(MMGenObject):
return False
def coin_addr(self,addr):
from .addr import CoinAddr
return CoinAddr( proto=self, addr=addr )
def addr_type(self,id_str):
from .addr import MMGenAddrType
return MMGenAddrType( proto=self, id_str=id_str )
class Secp256k1(Base):
@ -221,7 +223,6 @@ class CoinProtocol(MMGenObject):
block0 = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'
forks = [
_finfo(478559,'00000000000000000019f112ec0a9982926f1258cdcc558dd7c3b7e5dc7fa148','BCH',False),
_finfo(None,'','B2X',True),
]
caps = ('rbf','segwit')
mmcaps = ('key','addr','rpc','tx')
@ -288,8 +289,9 @@ class CoinProtocol(MMGenObject):
def pubhash2addr(self,pubkey_hash,p2sh):
assert len(pubkey_hash) == 40, f'{len(pubkey_hash)}: invalid length for pubkey hash'
s = self.addr_fmt_to_ver_bytes(('p2pkh','p2sh')[p2sh],return_hex=True) + pubkey_hash
return _b58chk_encode(bytes.fromhex(s))
return _b58chk_encode(bytes.fromhex(
self.addr_fmt_to_ver_bytes(('p2pkh','p2sh')[p2sh],return_hex=True) + pubkey_hash
))
# Segwit:
def pubhex2redeem_script(self,pubhex):
@ -299,7 +301,8 @@ class CoinProtocol(MMGenObject):
return self.witness_vernum_hex + '14' + hash160(pubhex)
def pubhex2segwitaddr(self,pubhex):
return self.pubhash2addr(hash160(self.pubhex2redeem_script(pubhex)),p2sh=True)
return self.pubhash2addr(
hash160( self.pubhex2redeem_script(pubhex)), p2sh=True )
def pubhash2bech32addr(self,pubhash):
d = list(bytes.fromhex(pubhash))
@ -336,19 +339,6 @@ class CoinProtocol(MMGenObject):
class BitcoinCashRegtest(BitcoinCashTestnet):
halving_interval = 150
class B2X(Bitcoin):
is_fork_of = 'Bitcoin'
coin_amt = B2XAmt
max_tx_fee = B2XAmt('0.1')
forks = [
_finfo(None,'','BTC',True) # activation: 494784
]
ignore_daemon_version = False
class B2XTestnet(B2X):
addr_ver_bytes = { '6f': 'p2pkh', 'c4': 'p2sh' }
wif_ver_num = { 'std': 'ef' }
class Litecoin(Bitcoin):
block0 = '12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2'
addr_ver_bytes = { '30': 'p2pkh', '32': 'p2sh', '05': 'p2sh' } # new p2sh ver 0x32 must come first
@ -508,8 +498,10 @@ class CoinProtocol(MMGenObject):
def preprocess_key(self,sec,pubkey_type): # reduce key
from .ed25519 import l
n = int.from_bytes(sec[::-1],'big') % l
return int.to_bytes(n,self.privkey_len,'big')[::-1]
return int.to_bytes(
int.from_bytes( sec[::-1], 'big' ) % l,
self.privkey_len,
'big' )[::-1]
def parse_addr(self,addr):

View file

@ -23,7 +23,6 @@ seed.py: Seed-related classes and methods for the MMGen suite
from .common import *
from .objmethods import Hilite,InitErrors
from .obj import ImmutableAttr,get_obj
from .crypto import get_random,scramble_seed
class SeedID(str,Hilite,InitErrors):
color = 'blue'
@ -54,6 +53,7 @@ class SeedBase(MMGenObject):
def __init__(self,seed_bin=None):
if not seed_bin:
from .crypto import get_random
# Truncate random data for smaller seed lengths
seed_bin = sha256(get_random(1033)).digest()[:(opt.seed_len or g.dfl_seed_len)//8]
elif len(seed_bin)*8 not in g.seed_lens:

View file

@ -67,7 +67,7 @@ def print_help(proto,po,opts_data,opt_filter):
from mmgen.help import help_notes_func
def help_notes(k):
return help_notes_func(proto,k)
return help_notes_func(proto,po,k)
def gen_arg_tuple(func,text):
d = {'proto': proto,'help_notes':help_notes}

View file

@ -22,7 +22,7 @@ tool.py: Routines for the 'mmgen-tool' utility
from .protocol import hash160
from .common import *
from .crypto import *
from .crypto import get_random
from .key import PrivKey
from .seedsplit import MasterShareIdx
from .addr import *
@ -506,9 +506,9 @@ class MMGenToolCmdCoin(MMGenToolCmds):
pubhex = gd.kg.to_pubhex(PrivKey(
self.proto,
wif = wifkey ))
addr = gd.ag.to_addr(pubhex)
rs = gd.ag.to_segwit_redeem_script(pubhex)
return (rs,addr)
return (
gd.ag.to_segwit_redeem_script(pubhex),
gd.ag.to_addr(pubhex) )
def privhex2addr(self,privhex:'sstr',output_pubhex=False):
"generate coin address from raw private key data in hexadecimal format"
@ -742,22 +742,24 @@ class MMGenToolCmdFileCrypt(MMGenToolCmds):
def encrypt(self,infile:str,outfile='',hash_preset=''):
"encrypt a file"
data = get_data_from_file(infile,'data for encryption',binary=True)
from .crypto import mmgen_encrypt,mmenc_ext
enc_d = mmgen_encrypt(data,'user data',hash_preset)
if not outfile:
outfile = f'{os.path.basename(infile)}.{g.mmenc_ext}'
outfile = f'{os.path.basename(infile)}.{mmenc_ext}'
write_data_to_file(outfile,enc_d,'encrypted data',binary=True)
return True
def decrypt(self,infile:str,outfile='',hash_preset=''):
"decrypt a file"
enc_d = get_data_from_file(infile,'encrypted data',binary=True)
from .crypto import mmgen_decrypt,mmenc_ext
while True:
dec_d = mmgen_decrypt(enc_d,'user data',hash_preset)
if dec_d: break
msg('Trying again...')
if not outfile:
o = os.path.basename(infile)
outfile = remove_extension(o,g.mmenc_ext)
outfile = remove_extension(o,mmenc_ext)
if outfile == o: outfile += '.dec'
write_data_to_file(outfile,dec_d,'decrypted data',binary=True)
return True

View file

@ -20,21 +20,37 @@
tw: Tracking wallet methods for the MMGen suite
"""
import json
import os,json,time
from collections import namedtuple
from .exception import *
from .common import *
from string import ascii_letters,digits
from .globalvars import g
from .color import red,yellow,green
from .exception import BadTwLabel,BadTwComment,BadAgeFormat,WalletFileError
from .util import (
msg,
msg_r,
dmsg,
die,
capfirst,
suf,
fmt,
make_timestr,
check_or_create_dir,
keypress_confirm,
write_data_to_file,
get_data_from_file,
line_input,
do_pager,
write_mode,
altcoin_subclass
)
from .base_obj import AsyncInit
from .objmethods import Hilite,InitErrors,MMGenObject
from .obj import *
from .tx import is_mmgen_id,is_coin_addr
from .rpc import rpc_init
CUR_HOME,ERASE_ALL = '\033[H','\033[0J'
def CUR_RIGHT(n):
return f'\033[{n}C'
def get_tw_label(proto,s):
"""
raise an exception on a malformed comment, return None on an empty or invalid label
@ -442,6 +458,10 @@ Actions: [q]uit view, [p]rint to file, pager [v]iew, [w]ide view, add [l]abel:
from .term import get_char
prompt = self.prompt.strip() + '\b'
no_output,oneshot_msg = False,None
from .opts import opt
CUR_HOME,ERASE_ALL = '\033[H','\033[0J'
CUR_RIGHT = lambda n: f'\033[{n}C'
while True:
msg_r('' if no_output else '\n\n' if opt.no_blank else CUR_HOME+ERASE_ALL)
reply = get_char(

View file

@ -713,7 +713,8 @@ def get_words(infile,desc,prompt):
def mmgen_decrypt_file_maybe(fn,desc='',quiet=False,silent=False):
d = get_data_from_file(fn,desc,binary=True,quiet=quiet,silent=silent)
have_enc_ext = get_extension(fn) == g.mmenc_ext
from .crypto import mmenc_ext
have_enc_ext = get_extension(fn) == mmenc_ext
if have_enc_ext or not is_utf8(d):
m = ('Attempting to decrypt','Decrypting')[have_enc_ext]
qmsg(f'{m} {desc} {fn!r}')

Some files were not shown because too many files have changed in this diff Show more