From 2ff7b4234655d3daa8bcdb9b4db63b5917a14c0f Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Sat, 29 Jan 2022 11:25:02 +0000 Subject: [PATCH] protocol.py: move gen-only altcoin functions to altcoin.py --- mmgen/altcoin.py | 83 +++++++++++++++++++++++++++++++++++++++++++++++ mmgen/opts.py | 2 +- mmgen/protocol.py | 81 --------------------------------------------- mmgen/tool/api.py | 3 +- test/gentest.py | 4 +-- 5 files changed, 88 insertions(+), 85 deletions(-) diff --git a/mmgen/altcoin.py b/mmgen/altcoin.py index 9c9f9d1d..6058e9d0 100755 --- a/mmgen/altcoin.py +++ b/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 diff --git a/mmgen/opts.py b/mmgen/opts.py index d0fd7d27..b6801a2b 100755 --- a/mmgen/opts.py +++ b/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 ) diff --git a/mmgen/protocol.py b/mmgen/protocol.py index 9c2f0ea2..f87e9c25 100755 --- a/mmgen/protocol.py +++ b/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 ) diff --git a/mmgen/tool/api.py b/mmgen/tool/api.py index 3ba8f91e..354fcd45 100755 --- a/mmgen/tool/api.py +++ b/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) diff --git a/test/gentest.py b/test/gentest.py index 431096da..c9ad7eb3 100755 --- a/test/gentest.py +++ b/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