Ejemplo n.º 1
0
def get_prefix(address):
    prefix = b58decode_check(address)[:1]

    if prefix == MAIN_PUBKEY_HASH:
        return 'main'
    elif prefix == TEST_PUBKEY_HASH:
        return 'test'
    else:
        raise ValueError('{} does not correspond to a mainnet nor '
                         'testnet address.'.format(prefix))
Ejemplo n.º 2
0
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))
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
def address_to_public_key_hash(address):
    get_prefix(address)
    return b58decode_check(address)[1:]
Ejemplo n.º 6
0
 def test_b58decode_check_failure(self):
     with pytest.raises(ValueError):
         b58decode_check(BITCOIN_ADDRESS[:-1])
Ejemplo n.º 7
0
 def test_b58decode_check_success(self):
     assert b58decode_check(
         BITCOIN_ADDRESS) == MAIN_PUBKEY_HASH + PUBKEY_HASH
Ejemplo n.º 8
0
def address_to_public_key_hash(address):
    # Support cashaddr and "legacy" for now.
    address = cashaddress.to_legacy_address(address)
    get_version(address)
    return b58decode_check(address)[1:]