tool addr2pubhash(), pubhash2addr(): bugfixes

This commit is contained in:
The MMGen Project 2022-02-14 10:18:43 +00:00
commit 937fb94470
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
4 changed files with 22 additions and 21 deletions

View file

@ -19,16 +19,17 @@ from ....opts import opt
from ....obj import MMGenObject,MMGenList,HexStr
from ....util import msg,dmsg,make_chksum_6,die
def addr2pubhash(proto,addr):
ap = proto.parse_addr(addr)
assert ap,f'coin address {addr!r} could not be parsed'
return ap.bytes.hex()
def addr2scriptPubKey(proto,addr):
def decode_addr(proto,addr):
ap = proto.parse_addr(addr)
assert ap, f'coin address {addr!r} could not be parsed'
return ap.bytes.hex()
return {
'p2pkh': '76a914' + addr2pubhash(proto,addr) + '88ac',
'p2sh': 'a914' + addr2pubhash(proto,addr) + '87',
'bech32': proto.witness_vernum_hex + '14' + addr2pubhash(proto,addr)
'p2pkh': '76a914' + decode_addr(proto,addr) + '88ac',
'p2sh': 'a914' + decode_addr(proto,addr) + '87',
'bech32': proto.witness_vernum_hex + '14' + decode_addr(proto,addr)
}[addr.addr_fmt]
def scriptPubKey2addr(proto,s):

View file

@ -129,12 +129,8 @@ class tool_cmd(tool_cmd_base):
def pubhex2addr(self,pubkeyhex:'sstr'):
"convert a hex pubkey to an address"
pubkey = bytes.fromhex(pubkeyhex)
from ..proto.common import hash160
if self.mmtype.name == 'segwit':
return self.proto.pubhash2segwitaddr( hash160(pubkey) )
else:
return self.pubhash2addr( hash160(pubkey).hex() )
return self.pubhash2addr( hash160( bytes.fromhex(pubkeyhex) ).hex() )
def pubhex2redeem_script(self,pubkeyhex:'sstr'): # new
"convert a hex pubkey to a Segwit P2SH-P2WPKH redeem script"
@ -148,20 +144,26 @@ class tool_cmd(tool_cmd_base):
assert redeem_scripthex[:4] == '0014', f'{redeem_scripthex!r}: invalid redeem script'
assert len(redeem_scripthex) == 44, f'{len(redeem_scripthex)//2} bytes: invalid redeem script length'
from ..proto.common import hash160
return self.pubhash2addr( hash160( bytes.fromhex(redeem_scripthex) ).hex() )
return self.proto.pubhash2addr(
hash160( bytes.fromhex(redeem_scripthex) ),
p2sh = True )
def pubhash2addr(self,pubhashhex:'sstr'):
"convert public key hash to address"
pubhash = bytes.fromhex(pubhashhex)
if self.mmtype.name == 'bech32':
if self.mmtype.name == 'segwit':
return self.proto.pubhash2segwitaddr( pubhash )
elif self.mmtype.name == 'bech32':
return self.proto.pubhash2bech32addr( pubhash )
else:
return self.proto.pubhash2addr( pubhash, self.mmtype.addr_fmt=='p2sh' )
def addr2pubhash(self,addr:'sstr'):
"convert coin address to public key hash"
from ..base_proto.bitcoin.tx.base import addr2pubhash
return addr2pubhash( self.proto, CoinAddr(self.proto,addr) )
ap = self.proto.parse_addr(addr)
assert ap, f'coin address {addr!r} could not be parsed'
assert ap.fmt != 'p2sh', f'p2sh addresses cannot be converted to pubhash'
return ap.bytes.hex()
def addr2scriptpubkey(self,addr:'sstr'):
"convert coin address to scriptPubKey"

View file

@ -390,8 +390,7 @@ class MMGenToolTestCmds(object):
cmd = ( '{c} {a} wif2hex {wif}' +
' | {c} {a} --type=compressed privhex2pubhex -' +
' | {c} {a} --type=segwit pubhex2redeem_script -' +
' | {c} {a} hash160 -' +
' | {c} {a} --type=segwit pubhash2addr -').format(
' | {c} {a} --type=segwit redeem_script2addr -').format(
c=' '.join(spawn_cmd),
a=' '.join(add_spawn_args),
wif=wif)

View file

@ -391,7 +391,6 @@ tests = {
'addr2pubhash': {
'btc_mainnet': [
( ['12bYUGXS8SRArZneQDN9YEEYAtEa59Rykm'], '118089d66b4a5853765e94923abdd5de4616c6e5' ),
( ['3Eevao3DRVXnYym3tdrJDqS3Wc39PQzahn'], '8e34586186551f6320fa3eb2d238a9c61ab8264b' ),
( ['bc1qxptlvmwaymaxa7pxkr2u5pn7c0508stcncv7ms'], '3057f66ddd26fa6ef826b0d5ca067ec3e8f3c178' ),
],
},
@ -399,7 +398,7 @@ tests = {
'btc_mainnet': [
( ['118089d66b4a5853765e94923abdd5de4616c6e5'], '12bYUGXS8SRArZneQDN9YEEYAtEa59Rykm',
None, 'legacy' ),
( ['8e34586186551f6320fa3eb2d238a9c61ab8264b'], '3Eevao3DRVXnYym3tdrJDqS3Wc39PQzahn',
( ['8e34586186551f6320fa3eb2d238a9c61ab8264b'], '37ZBgCBjjz9WSEzp1Zjv8sqdgmNie3Kd5s',
['--type=segwit'], 'segwit' ),
( ['3057f66ddd26fa6ef826b0d5ca067ec3e8f3c178'], 'bc1qxptlvmwaymaxa7pxkr2u5pn7c0508stcncv7ms',
['--type=bech32'], 'bech32' ),