def wif_to_key(wif, network=None): """This function can read the 'prefix' byte of a wif and instatiate the appropriate PrivateKey object. see: https://en.bitcoin.it/wiki/List_of_address_prefixes The prefix byte is the same for testnet and scaling-testnet. So to use scaling-testnet you must give as a parameter: network='stn' :param wif: A private key serialized to the Wallet Import Format. :type wif: ``str`` :param network: 'main', 'test' or 'stn' :type network: ``str`` """ private_key_bytes, compressed, prefix = wif_to_bytes(wif) wif_network_mismatch = "WIF prefix: '{}' does not match network: '{}'".format( prefix, network) if network == 'main': if prefix != 'main': raise ValueError(wif_network_mismatch) elif network in ['test', 'stn']: if prefix != 'test': raise ValueError(wif_network_mismatch) elif network is None: if prefix == 'main': network = 'main' elif prefix == 'test': network = 'test' else: raise Exception('bitsv issue, please open a bug report!') else: raise ValueError('network must be one of: main, test, stn') if compressed: return PrivateKey.from_bytes(private_key_bytes, network=network) else: return PrivateKey(wif, network=network)
def wif_to_key(wif): private_key_bytes, compressed, version = wif_to_bytes(wif) if version == 'main': if compressed: return PrivateKey.from_bytes(private_key_bytes) else: return PrivateKey(wif) else: if compressed: return PrivateKeyTestnet.from_bytes(private_key_bytes) else: return PrivateKeyTestnet(wif)
def __init__(self, wif=None): if wif: if isinstance(wif, str): private_key_bytes, compressed, version = wif_to_bytes(wif) self._pk = ECPrivateKey(private_key_bytes) elif isinstance(wif, ECPrivateKey): self._pk = wif compressed = True else: raise TypeError('Wallet Import Format must be a string.') else: self._pk = ECPrivateKey() compressed = True self._public_point = None self._public_key = self._pk.public_key.format(compressed=compressed)
def test_invalid_network(self): with pytest.raises(ValueError): wif_to_bytes(BITCOIN_ADDRESS)
def test_compressed(self): assert wif_to_bytes(WALLET_FORMAT_COMPRESSED_MAIN) == ( PRIVATE_KEY_BYTES, True, 'main')
def test_testnet(self): assert wif_to_bytes(WALLET_FORMAT_TEST) == (PRIVATE_KEY_BYTES, False, 'test')
def test_mainnet(self): assert wif_to_bytes(WALLET_FORMAT_MAIN) == (PRIVATE_KEY_BYTES, False, 'main')