From 4a73a0440bb0d77bcc0c0d514632e913a5473d25 Mon Sep 17 00:00:00 2001 From: MMGen Date: Wed, 13 Mar 2019 11:06:08 +0000 Subject: [PATCH] minor changes; mmgen-tool: add 'redeem_script2addr' command --- mmgen/obj.py | 2 +- mmgen/tool.py | 7 +++++++ mmgen/util.py | 3 +++ test/tooltest2.py | 7 +++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/mmgen/obj.py b/mmgen/obj.py index 8aa5a718..392d36a1 100755 --- a/mmgen/obj.py +++ b/mmgen/obj.py @@ -678,7 +678,7 @@ class PrivKey(bytes,Hilite,InitErrors,MMGenObject): try: assert s and type(compressed) == bool and pubkey_type,'Incorrect args for PrivKey()' - assert len(s) == cls.width // 2,'Key length must be {}'.format(cls.width/2) + assert len(s) == cls.width // 2,'Key length must be {}'.format(cls.width//2) if pubkey_type == 'password': # skip WIF creation and pre-processing for passwds me = bytes.__new__(cls,hexlify(s)) else: diff --git a/mmgen/tool.py b/mmgen/tool.py index eb8d2130..841c4007 100755 --- a/mmgen/tool.py +++ b/mmgen/tool.py @@ -418,6 +418,13 @@ class MMGenToolCmdCoin(MMGenToolCmdBase): assert opt.type == 'segwit','This command is meaningful only for --type=segwit' return g.proto.pubhex2redeem_script(pubkeyhex) + def redeem_script2addr(self,redeem_scripthex:'sstr'): # new + "convert a Segwit P2SH-P2WPKH redeem script to an address" + assert opt.type == 'segwit','This command is meaningful only for --type=segwit' + assert redeem_scripthex[:4] == '0014','{!r}: invalid redeem script'.format(redeem_scripthex) + assert len(redeem_scripthex) == 44,'{} bytes: invalid redeem script length'.format(len(redeem_scripthex)//2) + return self.pubhash2addr(self.hash160(redeem_scripthex).decode()) + def pubhash2addr(self,pubhashhex:'sstr'): "convert public key hash to address" if opt.type == 'bech32': diff --git a/mmgen/util.py b/mmgen/util.py index 7fc01770..045a1b4e 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -77,6 +77,9 @@ def pformat(d): def pmsg(*args): if not args: return msg(pformat(args if len(args) > 1 else args[0])) +def Pmsg(*args): + if not args: return + Msg(pformat(args if len(args) > 1 else args[0])) def pdie(*args,exit_val=1): if not args: sys.exit(1) die(exit_val,(pformat(args if len(args) > 1 else args[0]))) diff --git a/test/tooltest2.py b/test/tooltest2.py index 4a539d2e..7871ced9 100755 --- a/test/tooltest2.py +++ b/test/tooltest2.py @@ -372,6 +372,13 @@ tests = { ['--type=segwit'], 'opt.type="segwit"' ), ], }, + 'redeem_script2addr': { + 'btc_mainnet': [ + ( ['0014d04134b9ddb7399907657514d846aa495b4e474c'], + '3AhjTiWHhVJAi1s5CfKMcLzYps12x3gZhg', + ['--type=segwit'], 'opt.type="segwit"' ), + ], + }, 'randpair': { 'btc_mainnet': [ ( [], [is_wif,is_coin_addr], ['-r0'] ) ], 'btc_testnet': [ ( [], [is_wif,is_coin_addr], ['-r0'] ) ],