Skip to content

Commit

Permalink
Add blspy wrapper that tries to load shared library in case of import…
Browse files Browse the repository at this point in the history
… fail
  • Loading branch information
pshenmic committed Jan 28, 2024
1 parent 8197be1 commit f7d0010
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
62 changes: 62 additions & 0 deletions electrum_dash/blspy_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-

import sys
from .util import bh2u

try:
from blspy import BasicSchemeMPL, G1Element, G2Element

import_success = True
load_libdashbls = False
except ImportError:
import_success = False
load_libdashbls = True


if load_libdashbls:
import ctypes
from ctypes import cdll, create_string_buffer, byref, c_bool

if sys.platform == 'darwin':
name = '/Users/pshenmic/WebstormProjects/electrum-dash/electrum_dash/libdashbls.dylib'
elif sys.platform in ('windows', 'win32'):
name = 'libdashbls-0.dll'
else:
name = 'libdashbls.so'

try:
ldashbls = cdll.LoadLibrary(name)

ldashbls.bls_basic_verify.argtypes = [ctypes.c_char_p, ctypes.c_bool, ctypes.c_char_p, ctypes.c_char_p]
ldashbls.bls_basic_verify.restype = ctypes.c_bool
except:
load_libdashbls = False

if load_libdashbls:

class BasicSchemeMPL:
def verify(g1element, message, g2element):
return ldashbls.bls_basic_verify(g1element.bytes, g1element.isLegacy, g2element.bytes, message)

class G1Element:
bytes = b""
isLegacy = False

def __init__(self, bytes, isLegacy):
self.bytes = bytes
self.isLegacy = isLegacy

def from_bytes(bytes, isLegacy):
return G1Element(bytes, isLegacy)

class G2Element:
bytes = b""

def __init__(self, bytes):
self.bytes = bytes

def from_bytes(bytes):
return G2Element(bytes)

if not import_success and not load_libdashbls:
raise ImportError('Can not import blspy')
2 changes: 1 addition & 1 deletion electrum_dash/dash_ps_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import asyncio
import time
from enum import IntEnum
from blspy import BasicSchemeMPL, G1Element, G2Element
from .blspy_wrapper import BasicSchemeMPL, G1Element, G2Element

from .bitcoin import address_to_script
from .dash_msg import (DSPoolStatusUpdate, DSMessageIDs, ds_msg_str,
Expand Down

0 comments on commit f7d0010

Please sign in to comment.