Skip to content

Commit fda82ef

Browse files
committed
core: Find Python-bundled libcrypto on Windows
On Python 3.8+ Windows, ctypes.util.find_library() can't see Python's own DLLs/ dir because it's no longer on the DLL search path. Fall back to scanning sys.{base_,}prefix/DLLs for libcrypto-*.dll before LoadLibrary(None) raises TypeError. Layered on top of #306, which only works on systems that already have libcrypto.dll in system32 from another package. Closes #316.
1 parent 91e334d commit fda82ef

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

bitcoin/core/key.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,34 @@
1818
import ctypes
1919
import ctypes.util
2020
import hashlib
21+
import os
22+
import sys
2123
from os import urandom
2224
import bitcoin
2325
import bitcoin.signature
2426

2527
import bitcoin.core.script
2628

29+
30+
def _find_bundled_libcrypto():
31+
# On Python 3.8+ Windows, sys.{base_,}prefix/DLLs is no longer on the DLL
32+
# search path, so find_library can't see the libcrypto-*.dll Python ships.
33+
if sys.platform != 'win32':
34+
return None
35+
for base in (sys.base_prefix, sys.prefix):
36+
d = os.path.join(base, 'DLLs')
37+
if not os.path.isdir(d):
38+
continue
39+
for name in os.listdir(d):
40+
low = name.lower()
41+
if low.startswith('libcrypto-') and low.endswith('.dll'):
42+
return os.path.join(d, name)
43+
return None
44+
45+
2746
_ssl = ctypes.cdll.LoadLibrary(
2847
ctypes.util.find_library('ssl.35') or ctypes.util.find_library('ssl') or ctypes.util.find_library('libeay32')
29-
or ctypes.util.find_library('libcrypto')
48+
or ctypes.util.find_library('libcrypto') or _find_bundled_libcrypto()
3049
)
3150

3251
_libsecp256k1_path = ctypes.util.find_library('secp256k1')

0 commit comments

Comments
 (0)