proto.pubhash2.*addr(): cleanups

This commit is contained in:
The MMGen Project 2022-02-14 10:18:42 +00:00
commit 92b5246c1d
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
5 changed files with 18 additions and 16 deletions

View file

@ -76,7 +76,7 @@ class addr_generator:
def to_addr(self,data):
return CoinAddr(
self.proto,
self.proto.pubhash2bech32addr( hash160(data.pubkey)) )
self.proto.pubhash2bech32addr( hash160(data.pubkey) ))
class keccak(base):

View file

@ -98,10 +98,10 @@ class mainnet(CoinProtocol.Secp256k1): # chainparams.cpp
return self.parse_addr_bytes(b58chk_decode(addr))
def pubhash2addr(self,pubkey_hash,p2sh):
assert len(pubkey_hash) == 20, f'{len(pubkey_hash)}: invalid length for pubkey hash'
def pubhash2addr(self,pubhash,p2sh):
assert len(pubhash) == 20, f'{len(pubhash)}: invalid length for pubkey hash'
return b58chk_encode(
self.addr_fmt_to_ver_bytes(('p2pkh','p2sh')[p2sh],return_hex=False) + pubkey_hash
self.addr_fmt_to_ver_bytes(('p2pkh','p2sh')[p2sh],return_hex=False) + pubhash
)
# Segwit:
@ -113,12 +113,14 @@ class mainnet(CoinProtocol.Secp256k1): # chainparams.cpp
def pubkey2segwitaddr(self,pubkey):
return self.pubhash2addr(
hash160( self.pubkey2redeem_script(pubkey)), p2sh=True )
hash160( self.pubkey2redeem_script(pubkey) ),
p2sh = True )
def pubhash2bech32addr(self,pubhash):
d = list(pubhash)
import mmgen.contrib.bech32 as bech32
return bech32.bech32_encode(self.bech32_hrp,[self.witness_vernum]+bech32.convertbits(d,8,5))
return bech32.bech32_encode(
hrp = self.bech32_hrp,
data = [self.witness_vernum] + bech32.convertbits(list(pubhash),8,5) )
class testnet(mainnet):
addr_ver_bytes = { '6f': 'p2pkh', 'c4': 'p2sh' }

View file

@ -64,10 +64,10 @@ class mainnet(CoinProtocol.DummyWIF,CoinProtocol.Secp256k1):
h = self.keccak_256(addr.encode()).digest().hex()
return ''.join(addr[i].upper() if int(h[i],16) > 7 else addr[i] for i in range(len(addr)))
def pubhash2addr(self,pubkey_hash,p2sh):
assert len(pubkey_hash) == 20, f'{len(pubkey_hash)}: invalid length for {self.name} pubkey hash'
def pubhash2addr(self,pubhash,p2sh):
assert len(pubhash) == 20, f'{len(pubhash)}: invalid length for {self.name} pubkey hash'
assert not p2sh, f'{self.name} protocol has no P2SH address format'
return pubkey_hash.hex()
return pubhash.hex()
class testnet(mainnet):
chain_names = ['kovan','goerli','rinkeby']

View file

@ -38,14 +38,14 @@ class mainnet(mainnet):
else:
return super().preprocess_key(sec,pubkey_type)
def pubhash2addr(self,pubkey_hash,p2sh):
hash_len = len(pubkey_hash)
def pubhash2addr(self,pubhash,p2sh):
hash_len = len(pubhash)
if hash_len == 20:
return super().pubhash2addr(pubkey_hash,p2sh)
return super().pubhash2addr(pubhash,p2sh)
elif hash_len == 64:
raise NotImplementedError('Zcash z-addresses do not support pubhash2addr()')
else:
raise ValueError(f'{hash_len}: incorrect pubkey_hash length')
raise ValueError(f'{hash_len}: incorrect pubkey hash length')
class testnet(mainnet):
wif_ver_num = { 'std': 'ef', 'zcash_z': 'ac08' }

View file

@ -130,10 +130,10 @@ 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.pubkey2segwitaddr( pubkey )
else:
from ..proto.common import hash160
return self.pubhash2addr( hash160(pubkey).hex() )
def pubhex2redeem_script(self,pubkeyhex:'sstr'): # new
@ -147,7 +147,7 @@ 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.pubhash2addr( hash160( bytes.fromhex(redeem_scripthex) ).hex() )
def pubhash2addr(self,pubhashhex:'sstr'):
"convert public key hash to address"