obj.py: create Int type, make MMGenIdx & ETHNonce its subclasses

This commit is contained in:
The MMGen Project 2019-11-14 17:12:47 +00:00
commit da13550cd1
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
4 changed files with 50 additions and 41 deletions

View file

@ -20,10 +20,10 @@
altcoins.eth.obj: Ethereum data type classes for the MMGen suite
"""
# Kwei (babbage) 3, Mwei (lovelace) 6, Gwei (shannon) 9, µETH (szabo) 12, mETH (finney) 15, ETH 18
from decimal import Decimal
from mmgen.color import *
from mmgen.obj import *
from mmgen.obj import BTCAmt,Int
# Kwei (babbage) 3, Mwei (lovelace) 6, Gwei (shannon) 9, µETH (szabo) 12, mETH (finney) 15, ETH 18
class ETHAmt(BTCAmt):
max_prec = 18
max_amt = None
@ -44,20 +44,5 @@ class ETHAmt(BTCAmt):
def toSzabo(self): return int(Decimal(self) // self.szabo)
def toFinney(self): return int(Decimal(self) // self.finney)
class ETHNonce(int,Hilite,InitErrors): # WIP
def __new__(cls,n,on_fail='die'):
if type(n) == cls: return n
cls.arg_chk(on_fail)
from mmgen.util import is_int
try:
assert is_int(n),"'{}': value is not an integer".format(n)
n = int(n)
assert n >= 0,"'{}': value is negative".format(n)
return int.__new__(cls,n)
except Exception as e:
return cls.init_fail(e,n)
@classmethod
def colorize(cls,s,color=True):
k = color if type(color) is str else cls.color # hack: override color with str value
return globals()[k](str(s)) if (color or cls.color_always) else str(s)
class ETHNonce(Int):
min_val = 0

View file

@ -181,7 +181,32 @@ class Hilite(object):
return self.colorize(self,color=False)
class Str(str,Hilite): pass
class Int(int,Hilite): pass
class Int(int,Hilite,InitErrors):
min_val = None
max_val = None
max_digits = None
color = 'red'
def __new__(cls,n,base=10,on_fail='raise'):
if type(n) == cls:
return n
cls.arg_chk(on_fail)
try:
me = int.__new__(cls,str(n),base)
if cls.min_val != None:
assert me >= cls.min_val,'is less than cls.min_val ({})'.format(cls.min_val)
if cls.max_val != None:
assert me <= cls.max_val,'is greater than cls.max_val ({})'.format(cls.max_val)
if cls.max_digits != None:
assert len(str(me)) <= cls.max_digits,'has more than {} digits'.format(cls.max_digits)
return me
except Exception as e:
return cls.init_fail(e,n)
@classmethod
def colorize(cls,n,color=True):
return Hilite.colorize(repr(n),color=color)
# For attrs that are always present in the data instance
# Reassignment and deletion forbidden
@ -285,24 +310,7 @@ class MMGenListItem(MMGenObject):
raise AttributeError(m.format(name,type(self)))
return object.__setattr__(self,name,value)
class MMGenIdx(int,InitErrors):
min_val = 1
max_val = None
max_digits = None
def __new__(cls,num,on_fail='die'):
cls.arg_chk(on_fail)
try:
assert type(num) is not float,'is float'
me = int.__new__(cls,num)
if cls.max_digits:
assert len(str(me)) <= cls.max_digits,'has more than {} digits'.format(cls.max_digits)
if cls.max_val:
assert me <= cls.max_val,'is greater than {}'.format(cls.max_val)
assert me >= cls.min_val,'is less than {}'.format(cls.min_val)
return me
except Exception as e:
return cls.init_fail(e,num)
class MMGenIdx(Int): min_val = 1
class SeedShareIdx(MMGenIdx): max_val = 1024
class SeedShareCount(SeedShareIdx): min_val = 2
class MasterShareIdx(MMGenIdx): max_val = 1024

View file

@ -16,6 +16,16 @@ from .ot_common import *
ssm = str(SeedShareCount.max_val)
tests = OrderedDict([
('Int', {
'bad': ('1L',0.0,'0.0','1.0',1.0,'s',1.1,'1.1'),
'good': (
('0',0),('-1',-1),('7',7),-1,0,1,9999999,
{'n':'0x0','base':16,'ret':0},
{'n':'0x1','base':16,'ret':1},
{'n':'0xf','base':16,'ret':15},
{'n':'0xff','base':16,'ret':255},
)
}),
('AddrIdx', {
'bad': ('s',1.1,10000000,-1,0),
'good': (('7',7),(1,1),(9999999,9999999))

View file

@ -29,7 +29,13 @@ tests = OrderedDict([
)
}),
('ETHNonce', {
'bad': ('U','z','я',-1),
'good': (('0',0),('1',1),('100',100),1,100)
'bad': ('z','я',-1,'-1',0.0,'0.0'),
'good': (
('0',0),('1',1),('100',100),1,100,
{'n':'0x0','base':16,'ret':0},
{'n':'0x1','base':16,'ret':1},
{'n':'0xf','base':16,'ret':15},
{'n':'0xff','base':16,'ret':255},
)
}),
])