From 57f7cfc0e99ef58b5789f89f4355b39567f55286 Mon Sep 17 00:00:00 2001 From: MMGen Date: Thu, 31 May 2018 09:25:35 +0000 Subject: [PATCH] Move ETHAmt and ETHNonce to eth/obj.py --- mmgen/altcoins/eth/obj.py | 64 +++++++++++++++++++++++++++++++++++++++ mmgen/altcoins/eth/tw.py | 2 +- mmgen/obj.py | 39 +----------------------- 3 files changed, 66 insertions(+), 39 deletions(-) create mode 100755 mmgen/altcoins/eth/obj.py diff --git a/mmgen/altcoins/eth/obj.py b/mmgen/altcoins/eth/obj.py new file mode 100755 index 00000000..dd4868f5 --- /dev/null +++ b/mmgen/altcoins/eth/obj.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +# -*- coding: UTF-8 -*- +# +# mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution +# Copyright (C)2013-2018 The MMGen Project +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +""" +obj.py: MMGen native classes for Ethereum +""" + +# 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 * +class ETHAmt(BTCAmt): + max_prec = 18 + max_amt = 999999999 # TODO + wei = Decimal('0.000000000000000001') + Kwei = Decimal('0.000000000000001') + Mwei = Decimal('0.000000000001') + Gwei = Decimal('0.000000001') + szabo = Decimal('0.000001') + finney = Decimal('0.001') + min_coin_unit = wei + units = ('wei','Kwei','Mwei','Gwei','szabo','finney') + amt_fs = '4.18' + + def toWei(self): return int(Decimal(self) / self.wei) + def toKwei(self): return int(Decimal(self) / self.Kwei) + def toMwei(self): return int(Decimal(self) / self.Mwei) + def toGwei(self): return int(Decimal(self) / self.Gwei) + 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(cls,on_fail) + from mmgen.util import is_int + try: + assert is_int(n),"'{}': value is not an integer".format(n) + me = int.__new__(cls,n) + return me + except Exception as e: + m = "{!r}: value cannot be converted to ETH nonce ({})" + return cls.init_fail(m.format(n,e[0]),on_fail) + + @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) diff --git a/mmgen/altcoins/eth/tw.py b/mmgen/altcoins/eth/tw.py index 3fe77c56..7dde8612 100755 --- a/mmgen/altcoins/eth/tw.py +++ b/mmgen/altcoins/eth/tw.py @@ -22,7 +22,7 @@ altcoins.eth.tw: ETH tracking wallet functions and methods for the MMGen suite import json from mmgen.common import * -from mmgen.obj import * +from mmgen.obj import ETHAmt,TwMMGenID,TwComment,TwLabel from mmgen.tw import TrackingWallet,TwAddrList,TwUnspentOutputs from mmgen.addr import AddrData diff --git a/mmgen/obj.py b/mmgen/obj.py index db0fb757..f0922d4d 100755 --- a/mmgen/obj.py +++ b/mmgen/obj.py @@ -387,44 +387,7 @@ class BCHAmt(BTCAmt): pass class B2XAmt(BTCAmt): pass class LTCAmt(BTCAmt): max_amt = 84000000 -# 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 = 999999999 # TODO - wei = Decimal('0.000000000000000001') - Kwei = Decimal('0.000000000000001') - Mwei = Decimal('0.000000000001') - Gwei = Decimal('0.000000001') - szabo = Decimal('0.000001') - finney = Decimal('0.001') - min_coin_unit = wei - units = ('wei','Kwei','Mwei','Gwei','szabo','finney') - amt_fs = '4.18' - - def toWei(self): return int(Decimal(self) / self.wei) - def toKwei(self): return int(Decimal(self) / self.Kwei) - def toMwei(self): return int(Decimal(self) / self.Mwei) - def toGwei(self): return int(Decimal(self) / self.Gwei) - 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(cls,on_fail) - from mmgen.util import is_int - try: - assert is_int(n),"'{}': value is not an integer".format(n) - me = int.__new__(cls,n) - return me - except Exception as e: - m = "{!r}: value cannot be converted to ETH nonce ({})" - return cls.init_fail(m.format(n,e[0]),on_fail) - - @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) +from mmgen.altcoins.eth.obj import ETHAmt,ETHNonce class CoinAddr(str,Hilite,InitErrors,MMGenObject): color = 'cyan'