def test_p2wpkh(self): """https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#examples""" pubkey = PublicKey.from_hex( '0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798' ) self.assertEqual( bech32.encode(self.hrp, self.witver, hash160(pubkey.encode(compressed=True))), 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4') address = pubkey_to_address(pubkey, version='P2WPKH') self.assertEqual(address, 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4') self.assertEqual(address_type(address), ADDRESS.P2WPKH)
def test_p2pkh(self): """https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses#See_Also""" payload = {'Random': 'Random'} data = urllib.parse.urlencode(payload).encode('ascii') req = urllib.request.Request(self.url, data) with urllib.request.urlopen(req) as response: html = response.read() tree = etree.HTML(html) private = tree.find('.//input[@name="private"]').attrib['value'] public = tree.find('.//input[@name="public"]').attrib['value'] address = tree.find('.//input[@name="Base58"]').attrib['value'] my_pubkey = PrivateKey.from_hex(private).to_public() self.assertEqual(public.lower(), my_pubkey.hex()) self.assertEqual(pubkey_to_address(my_pubkey), address) self.assertEqual(address_type(address), ADDRESS.P2PKH)
def to_address(self, addrtype: str, compressed=False) -> str: from btctools.address import pubkey_to_address if compressed is True and addrtype == 'P2PKH': return pubkey_to_address(self.encode(compressed=True), addrtype) return pubkey_to_address(self, addrtype)
def to_address(self, addrtype: str) -> str: from btctools.address import pubkey_to_address return pubkey_to_address(self, addrtype)