Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PROPOSAL: add preload and library type #151

Closed
wants to merge 48 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
07c6c92
FEAT: add version and library type
MementoRC Mar 8, 2024
aca7cca
(fix) hasty push fails fmt
MementoRC Mar 8, 2024
84d7549
(fix) quotes mishap
MementoRC Mar 8, 2024
7a7ff10
(fix) quotes
MementoRC Mar 8, 2024
3bac042
(fix) ignore utility module in coverage
MementoRC Mar 8, 2024
867219b
(ref) update tests with pre-laoding of external libsecp256k1
MementoRC Mar 8, 2024
7b87106
(fix) add library loader at package initilization
MementoRC Mar 9, 2024
d05ab64
(fix) ignore conftest.py
MementoRC Mar 9, 2024
7bdcba8
(ref) lazy imports
MementoRC Mar 9, 2024
89df9b3
Merge branch 'master' into feat/add-version-library-info
MementoRC Mar 9, 2024
0d9d51b
(ref) reorganize preloading and conda specificity
MementoRC Mar 10, 2024
fd83fe4
(fix) import snafu
MementoRC Mar 10, 2024
658b285
(ref) lazy import warnings
MementoRC Mar 10, 2024
bf920e6
(feat) add needed helper function. engineer PKG_CONFIG_PATH
MementoRC Mar 10, 2024
1903289
(fix) remove _TBD_ placeholder. fix detection of system lib. add lib …
MementoRC Mar 10, 2024
180ce48
(lint) clean imports
MementoRC Mar 10, 2024
5b597a2
(fix) pkg-config on windows need --dont-define-prefix
MementoRC Mar 10, 2024
ace6dee
(fix) pkg-config on windows need --dont-define-prefix
MementoRC Mar 10, 2024
c52d91c
(fix) pkg-config on windows need --dont-define-prefix - hacky, final …
MementoRC Mar 10, 2024
34cca2d
(feat) add macos build with external libsecp256k1 from CONDA
MementoRC Mar 12, 2024
6e629fd
(conflict) move coincurve to src
MementoRC Mar 14, 2024
4046baa
Merge branch 'master' into feat/add-version-library-info
MementoRC Mar 14, 2024
76e2af9
(conflict) move coincurve to src
MementoRC Mar 14, 2024
773a337
(conflict) move coincurve to src
MementoRC Mar 14, 2024
095ed2b
(ref) avoid writing in src dir or have wrong info in lib_info
MementoRC Mar 14, 2024
e7e392c
(fix) do not use include_package_data
MementoRC Mar 14, 2024
3a9d65e
(fix) use package_data
MementoRC Mar 14, 2024
6116d7f
(fix) include_package_data?
MementoRC Mar 14, 2024
ce48c15
(fix) create build_lib first
MementoRC Mar 15, 2024
b5097d9
(fix) install in src to simplify the pain
MementoRC Mar 15, 2024
04d38a3
(fix) MacOs DYLD_... prior to delocate
MementoRC Mar 15, 2024
bd256d7
FEAT: add version and library type
MementoRC Mar 8, 2024
f68c3f9
(fix) import snafu
MementoRC Mar 10, 2024
17f6086
Merge branch 'master' into feat/add-version-library-info
MementoRC Mar 16, 2024
3871c31
(fix) missing commits
MementoRC Mar 16, 2024
e0dfa76
(fix) add temporary fix for pkg-config
MementoRC Mar 10, 2024
7f48a28
(feat) update to call_pkg_config
MementoRC Mar 16, 2024
6321069
Merge branch 'master' into feat/add-version-library-info
MementoRC Mar 16, 2024
48ab544
(ref) add test for __init__.py (tested after install)
MementoRC Mar 16, 2024
129fe50
(ref) remove fixture
MementoRC Mar 17, 2024
2821ea0
(feat) add tests for __init__.py. correct no-lib raise
MementoRC Mar 17, 2024
a895604
(fix) lint. remove unused pytest-mock
MementoRC Mar 17, 2024
b70cadb
(fix) add omit test_init.py in .coveragerc
MementoRC Mar 18, 2024
fdf1f6a
Merge remote-tracking branch 'origin/master' into feat/add-version-li…
MementoRC Mar 18, 2024
0bb4556
Merge remote-tracking branch 'upstream/master' into feat/add-version-…
MementoRC Mar 19, 2024
1722f9d
Merge branch 'master' into feat/add-version-library-info
MementoRC Mar 19, 2024
5958793
Merge remote-tracking branch 'upstream/master' into feat/add-preload-…
MementoRC Mar 20, 2024
f590d8f
Merge remote-tracking branch 'origin/feat/add-version-library-info' i…
MementoRC Mar 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ source =
tests

omit =
*/_secp256k1_library_info.py
*/_version.py
*/_windows_libsecp256k1.py
*/test_bench.py

Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,45 @@ jobs:
path: dist/*
if-no-files-found: error

windows-wheels-conda-x86_64:
name: Build Windows (CONDA) wheels AMD64
needs:
- test
runs-on: windows-latest

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ env.PYTHON_VERSION }}
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Install Miniconda
uses: conda-incubator/setup-miniconda@v3

- name: Build wheels
uses: pypa/[email protected]
env:
CIBW_ENVIRONMENT:
COINCURVE_IGNORE_SYSTEM_LIB="0"
COINCURVE_UPSTREAM_REF="__no_tag__"
COINCURVE_SECP256K1_BUILD='SHARED'
CIBW_BUILD: "cp312-win_amd64"
CIBW_ARCHS_WINDOWS: "AMD64"
CIBW_BEFORE_ALL: >
conda install -c conda-forge pkg-config libsecp256k1
CIBW_BUILD_VERBOSITY: 1
# pytest needs to be defined here to find 'coincurve' package?
CIBW_TEST_REQUIRES: pytest pytest-benchmark
CIBW_TEST_COMMAND: >
conda install -c conda-forge libsecp256k1 &&
python -c
"from coincurve import PrivateKey;
a=PrivateKey();
b=PrivateKey();
assert a.ecdh(b.public_key.format())==b.ecdh(a.public_key.format())

linux-wheels-arm:
name: Build Linux wheels for ARM
needs:
Expand Down
43 changes: 40 additions & 3 deletions coincurve/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
from coincurve.context import GLOBAL_CONTEXT, Context
from coincurve.keys import PrivateKey, PublicKey, PublicKeyXOnly
from coincurve.utils import verify_signature
def load_secp256k1_conda_library():
"""Load the secp256k1 library from the conda environment."""
import warnings

MementoRC marked this conversation as resolved.
Show resolved Hide resolved
from coincurve._secp256k1_library_info import SECP256K1_LIBRARY_NAME, SECP256K1_LIBRARY_TYPE
MementoRC marked this conversation as resolved.
Show resolved Hide resolved

if SECP256K1_LIBRARY_TYPE != 'EXTERNAL':
# coincurve was built with an internal library, either static or shared. It 'knows' where the library is.
MementoRC marked this conversation as resolved.
Show resolved Hide resolved
return

import os
from ctypes import CDLL
from ctypes.util import find_library

try:
# Find the library in the typical installation paths
if library := find_library(SECP256K1_LIBRARY_NAME):
CDLL(library)
return

# Find the library in the conda environment
if (conda := os.getenv('CONDA_PREFIX')) is not None:
import platform

if platform.system() == 'Windows':
library = os.path.join(conda, 'Library', 'bin', SECP256K1_LIBRARY_NAME)
else:
library = os.path.join(conda, 'lib', SECP256K1_LIBRARY_NAME)

CDLL(library)
return
except Exception as e:
warnings.warn(f'The required library {SECP256K1_LIBRARY_NAME}l is not loaded.\n{e}', stacklevel=2)


load_secp256k1_conda_library()

from coincurve.context import GLOBAL_CONTEXT, Context # noqa: E402
from coincurve.keys import PrivateKey, PublicKey, PublicKeyXOnly # noqa: E402
from coincurve.utils import verify_signature # noqa: E402

__all__ = [
'GLOBAL_CONTEXT',
Expand Down
2 changes: 2 additions & 0 deletions coincurve/_secp256k1_library_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SECP256K1_LIBRARY_TYPE = 'INTERNAL'
SECP256K1_LIBRARY_NAME = 'libsecp256k1-2.dll'
11 changes: 11 additions & 0 deletions coincurve/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file is imported from __init__.py and exec'd from setup.py

MAJOR = 19
MINOR = 0
MICRO = 1
RELEASE = True

__version__ = '%d.%d.%d' % (MAJOR, MINOR, MICRO)

if not RELEASE:
__version__ += '.dev0'
20 changes: 17 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import tarfile
from io import BytesIO
import sys
from os.path import dirname, abspath, join
from sys import path as PATH


from setuptools import Distribution as _Distribution, setup, find_packages, __version__ as setuptools_version
from setuptools._distutils import log
Expand All @@ -24,8 +27,10 @@
except ImportError:
_bdist_wheel = None

sys.path.append(os.path.abspath(os.path.dirname(__file__)))
from setup_support import absolute, build_flags, detect_dll, has_system_lib
COINCURVE_SRC_DIR = dirname(abspath(__file__))
PATH.append(COINCURVE_SRC_DIR)

from setup_support import absolute, build_flags, detect_dll, has_system_lib # noqa: E402

BUILDING_FOR_WINDOWS = detect_dll()

Expand All @@ -38,6 +43,15 @@

LIB_TARBALL_URL = f'https://github.com/bitcoin-core/secp256k1/archive/{UPSTREAM_REF}.tar.gz'

globals_ = {}
with open(join(COINCURVE_SRC_DIR, 'coincurve', '_version.py')) as fp:
exec(fp.read(), globals_) # noqa S102
__version__ = globals_['__version__']

with open(join(COINCURVE_SRC_DIR, 'coincurve', '_secp256k1_library_info.py'), 'w') as fp:
fp.write("SECP256K1_LIBRARY_TYPE = 'INTERNAL'\n")
fp.write("SECP256K1_LIBRARY_NAME = '_TBD_'\n")

# We require setuptools >= 3.3
if [int(i) for i in setuptools_version.split('.', 2)[:2]] < [3, 3]:
raise SystemExit(
Expand Down Expand Up @@ -331,7 +345,7 @@ def has_c_libraries(self):

setup(
name='coincurve',
version='19.0.1',
version=__version__,

description='Cross-platform Python CFFI bindings for libsecp256k1',
long_description=open('README.md', 'r').read(),
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,10 @@ def samples():
'X_ONLY_PUBKEY': X_ONLY_PUBKEY,
'X_ONLY_PUBKEY_INVALID': X_ONLY_PUBKEY_INVALID,
}


@pytest.fixture(autouse=True)
def load_secp256k1_library():
from coincurve import load_secp256k1_conda_library

load_secp256k1_conda_library()
Loading