Browse Source

protocol.py: improve handling of testnet/regtest Bech32 HRP

The MMGen Project 5 years ago
parent
commit
07131b160a
3 changed files with 5 additions and 9 deletions
  1. 0 3
      mmgen/opts.py
  2. 4 4
      mmgen/protocol.py
  3. 1 2
      test/test_py_d/ts_regtest.py

+ 0 - 3
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')

+ 4 - 4
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

+ 1 - 2
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()