From f44d070afb8efe34df78dd4fce2aad68c15d9d4f Mon Sep 17 00:00:00 2001 From: Bortlesboat Date: Mon, 6 Apr 2026 13:51:50 -0400 Subject: [PATCH 1/2] core: raise clear error when OpenSSL library is not found On Windows, ctypes.util.find_library() can return None for all candidate library names. Passing None to LoadLibrary() raises a confusing TypeError. Check for None first and raise an EnvironmentError with an actionable message instead. Fixes #316 --- bitcoin/core/key.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/bitcoin/core/key.py b/bitcoin/core/key.py index 0f902c8c..66905242 100644 --- a/bitcoin/core/key.py +++ b/bitcoin/core/key.py @@ -23,11 +23,16 @@ import bitcoin.signature import bitcoin.core.script - -_ssl = ctypes.cdll.LoadLibrary( - ctypes.util.find_library('ssl.35') or ctypes.util.find_library('ssl') or ctypes.util.find_library('libeay32') - or ctypes.util.find_library('libcrypto') +_ssl_library = ( + ctypes.util.find_library('ssl.35') or ctypes.util.find_library('ssl') + or ctypes.util.find_library('libeay32') or ctypes.util.find_library('libcrypto') ) +if _ssl_library is None: + raise EnvironmentError( + "OpenSSL library not found. " + "Install OpenSSL and ensure it is on your PATH or system library path." + ) +_ssl = ctypes.cdll.LoadLibrary(_ssl_library) _libsecp256k1_path = ctypes.util.find_library('secp256k1') _libsecp256k1_enable_signing = False From 2633a0378b2df475dd7bed1a74ee17e2d46c9ddf Mon Sep 17 00:00:00 2001 From: Andrew Barnes Date: Sun, 12 Jul 2026 19:01:14 -0400 Subject: [PATCH 2/2] test: cover missing OpenSSL library error --- bitcoin/tests/test_key.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/bitcoin/tests/test_key.py b/bitcoin/tests/test_key.py index 6cb18b54..7f523f3d 100644 --- a/bitcoin/tests/test_key.py +++ b/bitcoin/tests/test_key.py @@ -10,6 +10,8 @@ # LICENSE file. +import subprocess +import sys import unittest from bitcoin.core.key import * @@ -38,3 +40,29 @@ def T(hex_pubkey, is_valid, is_fullyvalid, is_compressed): T('0478d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71a1518063243acd4dfe96b66e3f2ec8013c8e072cd09b3834a19f81f659cc3455', True, True, False) + + +class Test_OpenSSLLoading(unittest.TestCase): + def test_missing_library_raises_clear_error(self): + code = """ +import ctypes.util +ctypes.util.find_library = lambda name: None + +try: + import bitcoin.core.key +except EnvironmentError as exc: + assert str(exc) == ( + "OpenSSL library not found. Install OpenSSL and ensure it is on your " + "PATH or system library path." + ) +else: + raise AssertionError("bitcoin.core.key imported without OpenSSL") +""" + + result = subprocess.run( + [sys.executable, "-c", code], + capture_output=True, + text=True, + ) + + self.assertEqual(result.returncode, 0, result.stderr)