Browse Source

Improve coin selection logic, allow coin disabling

MMGen 7 years ago
parent
commit
279e8872ef
5 changed files with 53 additions and 35 deletions
  1. 21 18
      mmgen/altcoin.py
  2. 3 1
      mmgen/main.py
  3. 20 11
      mmgen/protocol.py
  4. 2 1
      scripts/test-release.sh
  5. 7 4
      test/gentest.py

+ 21 - 18
mmgen/altcoin.py

@@ -38,10 +38,12 @@ class CoinInfo(object):
 	coin_constants = {}
 	coin_constants['mainnet'] = (
 #    NAME                     SYM        WIF     P2PKH             P2SH              SEGWIT TRUST
-#                                                     trust levels: 0=untested 1=low 2=med 3=high
-	('Bitcoin',               'BTC',     0x80,   (0x00,'1'),       (0x05,'3'),       True,  3),
-	('BitcoinSegwit2X',       'B2X',     0x80,   (0x00,'1'),       (0x05,'3'),       True,  2),
-	('Bcash',                 'BCH',     0x80,   (0x00,'1'),       (0x05,'3'),       False, 3),
+#                                        trust levels: 0=untested 1=low 2=med 3=high -1=disable
+#   Fork coins must be disabled here to prevent generation from incorrect sub-seed
+	('Bitcoin',               'BTC',     0x80,   (0x00,'1'),       (0x05,'3'),       True, -1),
+	('BitcoinSegwit2X',       'B2X',     0x80,   (0x00,'1'),       (0x05,'3'),       True, -1),
+	('BitcoinGold',           'BCG',     0x80,   (0x00,'1'),       (0x05,'3'),       True, -1),
+	('Bcash',                 'BCH',     0x80,   (0x00,'1'),       (0x05,'3'),       False,-1),
 	('2GiveCoin',             '2GIVE',   0xa7,   (0x27,('G','H')), None,             False, 0),
 	('42Coin',                '42',      0x88,   (0x08,'4'),       None,             False, 1),
 	('ACoin',                 'ACOIN',   0xe6,   (0x17,'A'),       None,             False, 0),
@@ -432,20 +434,21 @@ class CoinInfo(object):
 			from mmgen.util import pmsg,pdie
 #			pmsg(sym)
 #			pdie(tt)
-			if sym in tt:
-				src = tt[sym]
-				if src != trust:
-					msg("Updating trust for coin '{}': {} -> {}".format(sym,trust,src))
-					line[6] = src
-			else:
-				if trust != 0:
-					msg("Downgrading trust for coin '{}': {} -> {}".format(sym,trust,0))
-					line[6] = 0
+			if trust != -1:
+				if sym in tt:
+					src = tt[sym]
+					if src != trust:
+						msg("Updating trust for coin '{}': {} -> {}".format(sym,trust,src))
+						line[6] = src
+				else:
+					if trust != 0:
+						msg("Downgrading trust for coin '{}': {} -> {}".format(sym,trust,0))
+						line[6] = 0
 
-			if sym in cls.cross_checks:
-				if int(line[6]) == 0 and len(cls.cross_checks[sym]) > 1:
-					msg("Upgrading trust for coin '{}': {} -> {}".format(sym,line[6],1))
-					line[6] = 1
+				if sym in cls.cross_checks:
+					if int(line[6]) == 0 and len(cls.cross_checks[sym]) > 1:
+						msg("Upgrading trust for coin '{}': {} -> {}".format(sym,line[6],1))
+						line[6] = 1
 
 			print(fs.format(*line))
 		msg('Processed {} entries'.format(len(data)))
@@ -504,7 +507,7 @@ class CoinInfo(object):
 			'zcash_mini': ('ZEC',),
 			'keyconv': ( # all supported by vanitygen-plus 'keyconv' util
 				# broken: PIVX
-	 			'42','AC','AIB','ANC','ARS','ATMOS','AUR','BLK','BQC','BTC','TEST','BTCD','CCC','CCN','CDN',
+				'42','AC','AIB','ANC','ARS','ATMOS','AUR','BLK','BQC','BTC','TEST','BTCD','CCC','CCN','CDN',
 				'CLAM','CNC','CNOTE','CON','CRW','DEEPONION','DGB','DGC','DMD','DOGED','DOGE','DOPE',
 				'DVC','EFL','EMC','EXCL','FAIR','FLOZ','FTC','GAME','GAP','GCR','GRC','GRS','GUN','HAM','HODL',
 				'IXC','JBS','LBRY','LEAF','LTC','MMC','MONA','MUE','MYRIAD','MZC','NEOS','NLG','NMC','NVC',

+ 3 - 1
mmgen/main.py

@@ -55,5 +55,7 @@ def launch(what):
 			if os.getenv('MMGEN_TRACEBACK'):
 				raise
 			else:
-				sys.stderr.write('{!r}\n'.format(e[0]))
+				try:    m = u'{}\n'.format(e[0])
+				except: m = u'{!r}\n'.format(e[0])
+				sys.stderr.write(m)
 				sys.exit(2)

+ 20 - 11
mmgen/protocol.py

@@ -326,22 +326,29 @@ class ZcashTestnetProtocol(ZcashProtocol):
 
 class CoinProtocol(MMGenObject):
 	coins = {
-		'btc': (BitcoinProtocol,BitcoinTestnetProtocol),
-		'bch': (BitcoinCashProtocol,BitcoinCashTestnetProtocol),
-		'ltc': (LitecoinProtocol,LitecoinTestnetProtocol),
-		'eth': (EthereumProtocol,EthereumTestnetProtocol),
-		'etc': (EthereumClassicProtocol,EthereumClassicTestnetProtocol),
-		'zec': (ZcashProtocol,ZcashTestnetProtocol),
+		#      mainnet testnet trustlevel (None == skip)
+		'btc': (BitcoinProtocol,BitcoinTestnetProtocol,None),
+		'bch': (BitcoinCashProtocol,BitcoinCashTestnetProtocol,None),
+		'ltc': (LitecoinProtocol,LitecoinTestnetProtocol,None),
+		'eth': (EthereumProtocol,EthereumTestnetProtocol,2),
+		'etc': (EthereumClassicProtocol,EthereumClassicTestnetProtocol,2),
+		'zec': (ZcashProtocol,ZcashTestnetProtocol,2),
 	}
 	def __new__(cls,coin,testnet):
 		coin = coin.lower()
 		assert type(testnet) == bool
-		from mmgen.altcoin import CoinInfo as ci
-		all_coins = sorted(set([e[1].lower() for e in ci.coin_constants['mainnet']] + cls.coins.keys()))
 		m = "'{}': not a valid coin. Valid choices are {}"
-		assert coin in cls.coins,m.format(coin,','.join(all_coins))
+		assert coin in cls.coins,m.format(coin,','.join(cls.get_valid_coins()))
 		return cls.coins[coin][testnet]
 
+	@classmethod
+	def get_valid_coins(cls,upcase=False):
+		from mmgen.altcoin import CoinInfo as ci
+		ret = sorted(set(
+			[e[1] for e in ci.coin_constants['mainnet'] if e[6] != -1]
+			+ cls.coins.keys()))
+		return [getattr(e,('lower','upper')[upcase])() for e in ret]
+
 	@classmethod
 	def get_base_coin_from_name(cls,name):
 		for proto,foo in cls.coins.values():
@@ -352,9 +359,11 @@ class CoinProtocol(MMGenObject):
 def init_genonly_altcoins(usr_coin,trust_level=None):
 	from mmgen.altcoin import CoinInfo as ci
 	if trust_level is None:
-		if not usr_coin or usr_coin.lower() in CoinProtocol.coins: return None
+		if not usr_coin: return None # BTC
+		if usr_coin.lower() in CoinProtocol.coins:
+			return CoinProtocol.coins[usr_coin.lower()][2]
 		usr_coin = usr_coin.upper()
-		mn_coins = [e[1] for e in ci.coin_constants['mainnet']]
+		mn_coins = [e[1] for e in ci.coin_constants['mainnet'] if e[6] != -1]
 		if usr_coin not in mn_coins: return None
 		trust_level = ci.coin_constants['mainnet'][mn_coins.index(usr_coin)][6]
 	data = {}

+ 2 - 1
scripts/test-release.sh

@@ -129,7 +129,8 @@ t_alts=(
 
 	"test/gentest.py --all 2:pycoin $ROUNDS_LOW"
 	"test/gentest.py --all 2:pyethereum $ROUNDS_LOW"
-	"test/gentest.py --all 2:keyconv $ROUNDS_LOW")
+	"test/gentest.py --all 2:keyconv $ROUNDS_LOW"
+	"test/gentest.py --all 2:zcash_mini $ROUNDS_LOW")
 
 f_alts='Gen-only altcoin tests completed'
 

+ 7 - 4
test/gentest.py

@@ -107,7 +107,7 @@ def pycoin_sec2addr(sec):
 
 # pycoin/networks/all.py pycoin/networks/legacy_networks.py
 def init_external_prog():
-	global b,b_desc,ext_lib,ext_sec2addr,sp,eth,pcku,PREFIX_TRANSFORMS
+	global b,b_desc,ext_lib,ext_sec2addr,sp,eth,pcku,PREFIX_TRANSFORMS,addr_type
 	def test_support(k):
 		if b == k: return True
 		if b != 'ext' and b != k: return False
@@ -116,8 +116,11 @@ def init_external_prog():
 		return False
 	if b == 'zcash_mini' or addr_type.name == 'zcash_z':
 		import subprocess as sp
+		from mmgen.protocol import init_coin
 		ext_sec2addr = zcash_mini_sec2addr
 		ext_lib = 'zcash_mini'
+		init_coin('zec')
+		addr_type = MMGenAddrType('Z')
 	elif test_support('pyethereum'):
 		try:
 			import ethereum.utils as eth
@@ -277,11 +280,11 @@ ag = AddrGenerator(addr_type)
 
 if a and b:
 	if opt.all:
-		from mmgen.protocol import init_coin,init_genonly_altcoins
+		from mmgen.protocol import init_coin,init_genonly_altcoins,CoinProtocol
 		init_genonly_altcoins('btc',trust_level=0)
-		mmgen_supported = [e[1] for e in ci.coin_constants['mainnet']]
+		mmgen_supported = CoinProtocol.get_valid_coins(upcase=True)
 		for coin in ci.external_tests[('mainnet','testnet')[g.testnet]][ext_lib]:
-			if coin not in mmgen_supported and ext_lib != 'pyethereum': continue
+			if coin not in mmgen_supported: continue
 			init_coin(coin)
 			tmp_addr_type = addr_type if addr_type in g.proto.mmtypes else MMGenAddrType(g.proto.dfl_mmtype)
 			kg_a = KeyGenerator(tmp_addr_type,a)