proto.btc: pubkey2segwitaddr() -> pubhash2segwitaddr()

- also changed: pubkey2redeem_script() -> pubhash2redeem_script()
This commit is contained in:
The MMGen Project 2022-02-14 10:18:43 +00:00
commit 10232790e7
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
4 changed files with 11 additions and 10 deletions

View file

@ -65,10 +65,10 @@ class addr_generator:
def to_addr(self,data):
return CoinAddr(
self.proto,
self.proto.pubkey2segwitaddr(data.pubkey) )
self.proto.pubhash2segwitaddr( hash160(data.pubkey)) )
def to_segwit_redeem_script(self,data): # NB: returns hex
return self.proto.pubkey2redeem_script(data.pubkey).hex()
return self.proto.pubhash2redeem_script( hash160(data.pubkey) ).hex()
class bech32(base):

View file

@ -26,8 +26,8 @@ class mainnet(mainnet):
max_tx_fee = '0.1'
ignore_daemon_version = False
def pubkey2redeem_script(self,pubkey): raise NotImplementedError
def pubkey2segwitaddr(self,pubkey): raise NotImplementedError
def pubhash2redeem_script(self,pubkey): raise NotImplementedError
def pubhash2segwitaddr(self,pubkey): raise NotImplementedError
class testnet(mainnet):
addr_ver_bytes = { '6f': 'p2pkh', 'c4': 'p2sh' }

View file

@ -105,15 +105,15 @@ class mainnet(CoinProtocol.Secp256k1): # chainparams.cpp
)
# Segwit:
def pubkey2redeem_script(self,pubkey):
def pubhash2redeem_script(self,pubhash):
# https://bitcoincore.org/en/segwit_wallet_dev/
# The P2SH redeemScript is always 22 bytes. It starts with a OP_0, followed
# by a canonical push of the keyhash (i.e. 0x0014{20-byte keyhash})
return bytes.fromhex(self.witness_vernum_hex + '14') + hash160(pubkey)
return bytes.fromhex(self.witness_vernum_hex + '14') + pubhash
def pubkey2segwitaddr(self,pubkey):
def pubhash2segwitaddr(self,pubhash):
return self.pubhash2addr(
hash160( self.pubkey2redeem_script(pubkey) ),
hash160( self.pubhash2redeem_script(pubhash) ),
p2sh = True )
def pubhash2bech32addr(self,pubhash):

View file

@ -132,14 +132,15 @@ class tool_cmd(tool_cmd_base):
pubkey = bytes.fromhex(pubkeyhex)
from ..proto.common import hash160
if self.mmtype.name == 'segwit':
return self.proto.pubkey2segwitaddr( pubkey )
return self.proto.pubhash2segwitaddr( hash160(pubkey) )
else:
return self.pubhash2addr( hash160(pubkey).hex() )
def pubhex2redeem_script(self,pubkeyhex:'sstr'): # new
"convert a hex pubkey to a Segwit P2SH-P2WPKH redeem script"
assert self.mmtype.name == 'segwit','This command is meaningful only for --type=segwit'
return self.proto.pubkey2redeem_script( bytes.fromhex(pubkeyhex) ).hex()
from ..proto.common import hash160
return self.proto.pubhash2redeem_script( hash160(bytes.fromhex(pubkeyhex)) ).hex()
def redeem_script2addr(self,redeem_scripthex:'sstr'): # new
"convert a Segwit P2SH-P2WPKH redeem script to an address"