def get_version(address): version = b58decode_check(address)[:1] if version == MAIN_PUBKEY_HASH: return 'main' elif version == TEST_PUBKEY_HASH: return 'test' else: raise ValueError('{} does not correspond to a mainnet nor ' 'testnet address.'.format(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 address_to_public_key_hash(address): # Raise ValueError if we cannot identify the address. get_version(address) return b58decode_check(address)[1:]
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