mmgen-tool addr2pubhash: refactor

This commit is contained in:
The MMGen Project 2026-06-04 10:41:42 +00:00
commit 9a9627c8ed
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
2 changed files with 8 additions and 5 deletions

View file

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

View file

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