Browse Source

protocol.py: move gen-only altcoin functions to altcoin.py

The MMGen Project 3 years ago
parent
commit
2ff7b42346
5 changed files with 88 additions and 85 deletions
  1. 83 0
      mmgen/altcoin.py
  2. 1 1
      mmgen/opts.py
  3. 0 81
      mmgen/protocol.py
  4. 2 1
      mmgen/tool/api.py
  5. 2 2
      test/gentest.py

+ 83 - 0
mmgen/altcoin.py

@@ -701,6 +701,89 @@ class CoinInfo(object):
 		'bech32': { 'keyconv': True },
 	}
 
+def init_genonly_altcoins(usr_coin=None,testnet=False):
+	"""
+	Initialize altcoin protocol class or classes for current network.
+	If usr_coin is a core coin, initialization is skipped.
+	If usr_coin has a trust level of -1, an exception is raised.
+	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.
+	"""
+	data = { 'mainnet': (), 'testnet': () }
+	networks = ['mainnet'] + (['testnet'] if testnet else [])
+	network = 'testnet' if testnet else 'mainnet'
+
+	if usr_coin == None:
+		for network in networks:
+			data[network] = CoinInfo.get_supported_coins(network)
+		trust_level = 0
+	else:
+		from .globalvars import g
+		if usr_coin.lower() in g.core_coins: # core coin, so return immediately
+			from .protocol import CoinProtocol
+			return CoinProtocol.coins[usr_coin.lower()].trust_level
+		for network in networks:
+			data[network] = (CoinInfo.get_entry(usr_coin,network),)
+
+		cinfo = data[network][0]
+		if not cinfo:
+			raise ValueError(f'{usr_coin.upper()!r}: unrecognized coin for network {network.upper()}')
+		if cinfo.trust_level == -1:
+			raise ValueError(f'{usr_coin.upper()!r}: unsupported (disabled) coin for network {network.upper()}')
+
+		trust_level = cinfo.trust_level
+
+	create_altcoin_protos(data)
+
+	return trust_level
+
+def create_altcoin_protos(data):
+
+	from .protocol import CoinProtocol
+
+	def make_proto(e,testnet=False):
+
+		proto = ('X_' if e.name[0] in '0123456789' else '') + e.name + ('Testnet' if testnet else '')
+
+		if hasattr(CoinProtocol,proto):
+			return
+
+		def num2hexstr(n):
+			return '{:0{}x}'.format(n,(4,2)[n < 256])
+
+		setattr(
+			CoinProtocol,
+			proto,
+			type(
+				'CoinProtocol.' + proto,
+				(CoinProtocol.Bitcoin,),
+				{
+					'base_coin': e.symbol,
+					'addr_ver_bytes': dict(
+						[( num2hexstr(e.p2pkh_info[0]), 'p2pkh' )] +
+						([( num2hexstr(e.p2sh_info[0]), 'p2sh' )] if e.p2sh_info else [])
+					),
+					'wif_ver_num': { 'std': num2hexstr(e.wif_ver_num) },
+					'mmtypes':    ('L','C','S') if e.has_segwit else ('L','C'),
+					'dfl_mmtype': 'L',
+					'mmcaps':     ('key','addr'),
+				},
+			)
+		)
+
+	for e in data['mainnet']:
+		make_proto(e)
+
+	for e in data['testnet']:
+		make_proto(e,testnet=True)
+
+	for e in data['mainnet']:
+		if e.symbol.lower() in CoinProtocol.coins:
+			continue
+		CoinProtocol.coins[e.symbol.lower()] = CoinProtocol.proto_info(
+			name        = 'X_'+e.name if e.name[0] in '0123456789' else e.name,
+			trust_level = e.trust_level )
+
 if __name__ == '__main__':
 	quiet = '--quiet' in sys.argv
 	verbose = '--verbose' in sys.argv

+ 1 - 1
mmgen/opts.py

@@ -375,7 +375,7 @@ def init(
 		g.data_dir = os.path.join(g.data_dir_root,'regtest',g.coin.lower(),('alice','bob')[g.bob])
 
 	if need_proto:
-		from .protocol import init_genonly_altcoins
+		from .altcoin import init_genonly_altcoins
 		altcoin_trust_level = init_genonly_altcoins(
 			g.coin,
 			testnet = g.testnet or g.regtest )

+ 0 - 81
mmgen/protocol.py

@@ -572,84 +572,3 @@ def init_proto_from_opts():
 		testnet   = g.testnet,
 		regtest   = g.regtest,
 		tokensym  = g.token )
-
-def init_genonly_altcoins(usr_coin=None,testnet=False):
-	"""
-	Initialize altcoin protocol class or classes for current network.
-	If usr_coin is a core coin, initialization is skipped.
-	If usr_coin has a trust level of -1, an exception is raised.
-	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.
-	"""
-	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] = CoinInfo.get_supported_coins(network)
-		trust_level = 0
-	else:
-		if usr_coin.lower() in g.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] = (CoinInfo.get_entry(usr_coin,network),)
-
-		cinfo = data[network][0]
-		if not cinfo:
-			raise ValueError(f'{usr_coin.upper()!r}: unrecognized coin for network {network.upper()}')
-		if cinfo.trust_level == -1:
-			raise ValueError(f'{usr_coin.upper()!r}: unsupported (disabled) coin for network {network.upper()}')
-
-		trust_level = cinfo.trust_level
-
-	create_altcoin_protos(data)
-
-	return trust_level
-
-def create_altcoin_protos(data):
-
-	def make_proto(e,testnet=False):
-
-		proto = ('X_' if e.name[0] in '0123456789' else '') + e.name + ('Testnet' if testnet else '')
-
-		if hasattr(CoinProtocol,proto):
-			return
-
-		def num2hexstr(n):
-			return '{:0{}x}'.format(n,(4,2)[n < 256])
-
-		setattr(
-			CoinProtocol,
-			proto,
-			type(
-				'CoinProtocol.' + proto,
-				(CoinProtocol.Bitcoin,),
-				{
-					'base_coin': e.symbol,
-					'addr_ver_bytes': dict(
-						[( num2hexstr(e.p2pkh_info[0]), 'p2pkh' )] +
-						([( num2hexstr(e.p2sh_info[0]), 'p2sh' )] if e.p2sh_info else [])
-					),
-					'wif_ver_num': { 'std': num2hexstr(e.wif_ver_num) },
-					'mmtypes':    ('L','C','S') if e.has_segwit else ('L','C'),
-					'dfl_mmtype': 'L',
-					'mmcaps':     ('key','addr'),
-				},
-			)
-		)
-
-	for e in data['mainnet']:
-		make_proto(e)
-
-	for e in data['testnet']:
-		make_proto(e,testnet=True)
-
-	for e in data['mainnet']:
-		if e.symbol.lower() in CoinProtocol.coins:
-			continue
-		CoinProtocol.coins[e.symbol.lower()] = CoinProtocol.proto_info(
-			name        = 'X_'+e.name if e.name[0] in '0123456789' else e.name,
-			trust_level = e.trust_level )

+ 2 - 1
mmgen/tool/api.py

@@ -80,7 +80,8 @@ class tool_api(
 		Valid choices for coins: one of the symbols returned by the 'coins' attribute
 		Valid choices for network: 'mainnet','testnet','regtest'
 		"""
-		from ..protocol import init_proto,init_genonly_altcoins
+		from ..protocol import init_proto
+		from ..altcoin import init_genonly_altcoins
 		from ..util import warn_altcoins
 		altcoin_trust_level = init_genonly_altcoins(coinsym,testnet=network in ('testnet','regtest'))
 		warn_altcoins(coinsym,altcoin_trust_level)

+ 2 - 2
test/gentest.py

@@ -535,8 +535,8 @@ def main():
 
 from subprocess import run,PIPE,DEVNULL
 from collections import namedtuple
-from mmgen.protocol import init_proto,init_proto_from_opts,CoinProtocol,init_genonly_altcoins
-from mmgen.altcoin import CoinInfo as cinfo
+from mmgen.protocol import init_proto,init_proto_from_opts,CoinProtocol
+from mmgen.altcoin import init_genonly_altcoins,CoinInfo as cinfo
 from mmgen.key import PrivKey
 from mmgen.addr import KeyGenerator,AddrGenerator,MMGenAddrType
 from mmgen.keygen import get_backends