def wif_to_bytes(wif, regtest=False): private_key = b58decode_check(wif) version = private_key[:1] if version == MAIN_PRIVATE_KEY: version = "main" elif version == TEST_PRIVATE_KEY: # Regtest and testnet WIF formats are identical, so we # check the 'regtest' flag and manually set the version if regtest: version = "regtest" else: version = "test" else: raise ValueError(f"{version} does not correspond to a mainnet," f"testnet, nor regtest address.") # Remove version byte and, if present, compression flag. if len(wif) == 52 and private_key[-1] == 1: private_key, compressed = private_key[1:-1], True else: private_key, compressed = private_key[1:], False return private_key, compressed, version
def wif_checksum_check(wif): try: decoded = b58decode_check(wif) except ValueError: return False if decoded[:1] in (MAIN_PRIVATE_KEY, TEST_PRIVATE_KEY): return True return False
def wif_to_bytes(wif): private_key = b58decode_check(wif) version = private_key[:1] if version == MAIN_PRIVATE_KEY: version = 'main' elif version == TEST_PRIVATE_KEY: version = 'test' else: raise ValueError('{} does not correspond to a mainnet nor ' 'testnet address.'.format(version)) # Remove version byte and, if present, compression flag. if len(wif) == 52 and private_key[-1] == 1: private_key, compressed = private_key[1:-1], True else: private_key, compressed = private_key[1:], False return private_key, compressed, version
def test_b58decode_check_failure(self): with pytest.raises(ValueError): b58decode_check(BITCOIN_ADDRESS[:-1])
def test_b58decode_check_success(self): assert b58decode_check( BITCOIN_ADDRESS) == MAIN_PUBKEY_HASH + PUBKEY_HASH