tool addr2pubhash: check input, rejecting non-PKH addresses

This commit is contained in:
The MMGen Project 2022-04-28 11:00:51 +00:00
commit 4e3b11a313
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
2 changed files with 5 additions and 1 deletions

View file

@ -43,6 +43,7 @@ class MMGenAddrType(str,Hilite,InitErrors,MMGenObject):
extra_attrs = ImmutableAttr(tuple,set_none_ok=True)
desc = ImmutableAttr(str)
pkh_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

@ -160,9 +160,12 @@ class tool_cmd(tool_cmd_base):
def addr2pubhash(self,addr:'sstr'):
"convert coin address to public key hash"
from ..opts import opt
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'
if ap.fmt not in MMGenAddrType.pkh_fmts:
from ..util import die
die(2,f'{ap.fmt} addresses cannot be converted to pubhash')
return ap.bytes.hex()
def addr2scriptpubkey(self,addr:'sstr'):