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

This commit is contained in:
The MMGen Project 2019-03-13 11:06:08 +00:00
commit 4a73a0440b
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
4 changed files with 18 additions and 1 deletions

View file

@ -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:

View file

@ -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':

View file

@ -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])))

View file

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