Browse Source

minor changes; mmgen-tool: add 'redeem_script2addr' command

MMGen 6 years ago
parent
commit
4a73a0440b
4 changed files with 18 additions and 1 deletions
  1. 1 1
      mmgen/obj.py
  2. 7 0
      mmgen/tool.py
  3. 3 0
      mmgen/util.py
  4. 7 0
      test/tooltest2.py

+ 1 - 1
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:

+ 7 - 0
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':

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

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