From 92b5246c1d46419831585a3030da03380f38c4a4 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 14 Feb 2022 10:18:42 +0000 Subject: [PATCH] proto.pubhash2.*addr(): cleanups --- mmgen/addrgen.py | 2 +- mmgen/proto/btc.py | 14 ++++++++------ mmgen/proto/eth.py | 6 +++--- mmgen/proto/zec.py | 8 ++++---- mmgen/tool/coin.py | 4 ++-- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/mmgen/addrgen.py b/mmgen/addrgen.py index 3ad5219a..675a8cfc 100755 --- a/mmgen/addrgen.py +++ b/mmgen/addrgen.py @@ -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): diff --git a/mmgen/proto/btc.py b/mmgen/proto/btc.py index 3c85941c..1f1ff79c 100755 --- a/mmgen/proto/btc.py +++ b/mmgen/proto/btc.py @@ -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' } diff --git a/mmgen/proto/eth.py b/mmgen/proto/eth.py index 1964cdc4..670434ff 100755 --- a/mmgen/proto/eth.py +++ b/mmgen/proto/eth.py @@ -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'] diff --git a/mmgen/proto/zec.py b/mmgen/proto/zec.py index 628de4b7..de707ef8 100755 --- a/mmgen/proto/zec.py +++ b/mmgen/proto/zec.py @@ -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' } diff --git a/mmgen/tool/coin.py b/mmgen/tool/coin.py index 26b8c696..6f8c259a 100755 --- a/mmgen/tool/coin.py +++ b/mmgen/tool/coin.py @@ -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"