From 937fb94470a21db460c6539eef16a894cdfd721c Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 14 Feb 2022 10:18:43 +0000 Subject: [PATCH] tool addr2pubhash(), pubhash2addr(): bugfixes --- mmgen/base_proto/bitcoin/tx/base.py | 17 +++++++++-------- mmgen/tool/coin.py | 20 +++++++++++--------- test/tooltest.py | 3 +-- test/tooltest2.py | 3 +-- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/mmgen/base_proto/bitcoin/tx/base.py b/mmgen/base_proto/bitcoin/tx/base.py index 29c3e47e..5e18e5ad 100755 --- a/mmgen/base_proto/bitcoin/tx/base.py +++ b/mmgen/base_proto/bitcoin/tx/base.py @@ -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): diff --git a/mmgen/tool/coin.py b/mmgen/tool/coin.py index 48f3d039..1ff721b4 100755 --- a/mmgen/tool/coin.py +++ b/mmgen/tool/coin.py @@ -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" diff --git a/test/tooltest.py b/test/tooltest.py index a8e0adc2..121329d7 100755 --- a/test/tooltest.py +++ b/test/tooltest.py @@ -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) diff --git a/test/tooltest2.py b/test/tooltest2.py index 86ee416c..8cbfe9f0 100755 --- a/test/tooltest2.py +++ b/test/tooltest2.py @@ -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' ),