From 9a9627c8ed44a8a77caea2c6632cf022f70e6f8b Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Thu, 4 Jun 2026 10:41:42 +0000 Subject: [PATCH] mmgen-tool addr2pubhash: refactor --- mmgen/addr.py | 2 +- mmgen/tool/coin.py | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/mmgen/addr.py b/mmgen/addr.py index c0ea824f..7aef97cc 100755 --- a/mmgen/addr.py +++ b/mmgen/addr.py @@ -44,7 +44,7 @@ class MMGenAddrType(HiliteStr, InitErrors, MMGenObject): extra_attrs = ImmutableAttr(tuple, set_none_ok=True) desc = ImmutableAttr(str) - pkh_fmts = ('p2pkh', 'bech32', 'ethereum') + pubhash_fmts = ('p2pkh', 'bech32', 'ethereum') mmtypes = { 'L': ati('legacy', 'std', False,'p2pkh', 'p2pkh', 'wif', (), 'Legacy uncompressed address'), 'C': ati('compressed','std', True, 'p2pkh', 'p2pkh', 'wif', (), 'Compressed P2PKH address'), diff --git a/mmgen/tool/coin.py b/mmgen/tool/coin.py index f19b54a3..b980204f 100755 --- a/mmgen/tool/coin.py +++ b/mmgen/tool/coin.py @@ -175,15 +175,18 @@ class tool_cmd(tool_cmd_base): case _: return self.proto.pubhash2addr(pubhash, self.mmtype.addr_fmt) - def addr2pubhash(self, addr: 'sstr'): - "convert coin address to public key hash" + def _addr2pub(self, addr, *, ptype): ap = self.proto.decode_addr(addr) assert ap, f'coin address {addr!r} could not be parsed' - if ap.fmt not in MMGenAddrType.pkh_fmts: + if ap.fmt not in getattr(MMGenAddrType, f'{ptype}_fmts'): from ..util import die - die(2, f'{ap.fmt} addresses cannot be converted to pubhash') + die(2, f'{ap.fmt} addresses cannot be converted to {ptype}') return ap.bytes.hex() + def addr2pubhash(self, addr: 'sstr'): + "convert coin address to public key hash" + return self._addr2pub(addr, ptype='pubhash') + def addr2scriptpubkey(self, addr: 'sstr'): "convert coin address to scriptPubKey" from ..proto.btc.tx.base import addr2scriptPubKey