def __init__(self, key, compressed=True, testnet=False): if isinstance(key, str): try: key = bytes_from_hex(key) except: if is_wif_valid(key): key = PrivateKey(key) if isinstance(key, bytes): if len(key) == 32: key = PrivateKey(key, compressed=compressed, testnet=testnet) elif is_public_key_valid(key): public_key = key self.testnet = testnet self.compressed = True if len(key) == 33 else False else: raise TypeError("key invalid") if isinstance(key, PrivateKey): #: flag for testnet network private key (boolean) self.testnet = key.testnet #: flag for compressed type of corresponding public key (boolean) self.compressed = key.compressed public_key = private_to_public_key(key.key, compressed=key.compressed, hex=False) #: public key in bytes (bytes) self.key = public_key #: public key in HEX (string) self.hex = self.key.hex()
def is_public_key_valid(key): """ Check public key is valid. :param key: public key in HEX or bytes string format. :return: boolean. """ return __parent__.is_public_key_valid(key)
def test_is_public_key_valid(): pu = "04b635dbdc16dbdf4bb9cf5b55e7d03e514fb04dcef34208155c7d3ec88e9045f4" + \ "c8cbe28702911260f2a1da099a338bed4ee98f66bb8dba8031a76ab537ff6663" pk = "03b635dbdc16dbdf4bb9cf5b55e7d03e514fb04dcef34208155c7d3ec88e9045f4" assert is_public_key_valid(pu) == True assert is_public_key_valid(pk) == True assert is_public_key_valid(bytes_from_hex(pk)) == True assert is_public_key_valid(bytes_from_hex(pu)) == True pu = "63qdbdc16dbdf4bb9cf45b55e7d03e514fb04dcef34208155c7d3ec88e9045f4c8c" + \ "be28702911260f2a1da099a338bed4ee98f66bb8dba8031a76ab537ff6663" pk = "02b635dbdc16dbdf455bb9cf5b55e7d03e514fb04dcef34208155c7d3ec88e9045f4" assert is_public_key_valid(pu) == False assert is_public_key_valid(pk) == False assert is_public_key_valid("8989") == False pu = "04b635dbdc16dbdf455bb9cf5b55e7d03e514fb04dcef34208155c7d3ec88e902245f4" assert is_public_key_valid(pu) == False