From 07131b160af13e32859ece74270e42477a27e620 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Sat, 15 Feb 2020 14:13:13 +0000 Subject: [PATCH] protocol.py: improve handling of testnet/regtest Bech32 HRP --- mmgen/opts.py | 3 --- mmgen/protocol.py | 8 ++++---- test/test_py_d/ts_regtest.py | 3 +-- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/mmgen/opts.py b/mmgen/opts.py index 72d1107c..f34e2356 100755 --- a/mmgen/opts.py +++ b/mmgen/opts.py @@ -328,9 +328,6 @@ def init(opts_data,add_opts=[],opt_filter=None,parse_only=False): check_or_create_dir(g.data_dir) # g.data_dir is finalized, so now we can do this - if g.regtest and hasattr(g.proto,'bech32_hrp_rt'): - g.proto.bech32_hrp = g.proto.bech32_hrp_rt - # Check user-set opts without modifying them if not check_opts(uopts): die(1,'Options checking failed') diff --git a/mmgen/protocol.py b/mmgen/protocol.py index b36c42d7..69e3c368 100755 --- a/mmgen/protocol.py +++ b/mmgen/protocol.py @@ -229,8 +229,7 @@ class BitcoinTestnetProtocol(BitcoinProtocol): data_subdir = 'testnet' daemon_data_subdir = 'testnet3' rpc_port = 18332 - bech32_hrp = 'tb' - bech32_hrp_rt = 'bcrt' + bech32_hrps = {'testnet':'tb','regtest':'bcrt'} class BitcoinCashProtocol(BitcoinProtocol): # TODO: assumes MSWin user installs in custom dir 'Bitcoin_ABC' @@ -301,8 +300,7 @@ class LitecoinTestnetProtocol(LitecoinProtocol): data_subdir = 'testnet' daemon_data_subdir = 'testnet4' rpc_port = 19332 - bech32_hrp = 'tltc' - bech32_hrp_rt = 'rltc' + bech32_hrps = {'testnet':'tltc','regtest':'rltc'} class BitcoinProtocolAddrgen(BitcoinProtocol): mmcaps = ('key','addr') class BitcoinTestnetProtocolAddrgen(BitcoinTestnetProtocol): mmcaps = ('key','addr') @@ -466,6 +464,8 @@ class CoinProtocol(MMGenObject): m = "{}: not a valid coin for network {}\nSupported coins: {}" assert coin in cls.coins, m.format(coin.upper(),g.network.upper(),' '.join(cls.list_coins())) proto = cls.coins[coin][testnet] + if hasattr(proto,'bech32_hrps'): + proto.bech32_hrp = proto.bech32_hrps[('testnet','regtest')[g.regtest]] return proto @classmethod diff --git a/test/test_py_d/ts_regtest.py b/test/test_py_d/ts_regtest.py index e9af946b..a1617550 100755 --- a/test/test_py_d/ts_regtest.py +++ b/test/test_py_d/ts_regtest.py @@ -231,6 +231,7 @@ class TestSuiteRegtest(TestSuiteBase,TestSuiteShared): usr_subsids = { 'bob': {}, 'alice': {} } def __init__(self,trunner,cfgs,spawn): + g.regtest = True os.environ['MMGEN_TEST_SUITE_REGTEST'] = '1' from mmgen.regtest import MMGenRegtest rt = MMGenRegtest(g.coin) @@ -555,8 +556,6 @@ class TestSuiteRegtest(TestSuiteBase,TestSuiteShared): addrfile = get_file_with_ext(self._user_dir(user),ext,no_dot=True) psave = g.proto g.proto = CoinProtocol(g.coin,True) - if hasattr(g.proto,'bech32_hrp_rt'): - g.proto.bech32_hrp = g.proto.bech32_hrp_rt silence() addr = AddrList(addrfile).data[idx].addr end_silence()