How to use the secp256k1.Base function in secp256k1

To help you get started, we’ve selected a few secp256k1 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github icon-project / loopchain / loopchain / crypto / signature.py View on Github external
import binascii
import hashlib
import logging
from typing import Union, Type, TypeVar

import eth_keyfile
from secp256k1 import Base, ALL_FLAGS
from secp256k1 import PrivateKey, PublicKey

from loopchain.crypto.cert_serializers import DerSerializer, PemSerializer

T = TypeVar('T', bound='SignVerifier')


class SignVerifier:
    _base = Base(None, ALL_FLAGS)
    _pri = PrivateKey(ctx=_base.ctx)

    def __init__(self):
        self.address: str = None

    def verify_address(self, pubkey: bytes):
        new_address = self.address_from_pubkey(pubkey)
        if new_address != self.address:
            raise RuntimeError(f"Address is not valid."
                               f"Address({new_address}), "
                               f"Expected({self.address}")

    def verify_data(self, origin_data: bytes, signature: bytes):
        self.verify_signature(origin_data, signature, False)

    def verify_hash(self, origin_data: bytes, signature):
github hyperledger / grid-contrib / signing / sawtooth_signing / secp256k1_signer.py View on Github external
import logging
import binascii
import warnings
import hashlib
import secp256k1

try:
    # Python 2
    import pybitcointools
except ImportError:
    # Python 3
    import bitcoin as pybitcointools


LOGGER = logging.getLogger(__name__)
__CONTEXTBASE__ = secp256k1.Base(ctx=None, flags=secp256k1.ALL_FLAGS)
__CTX__ = __CONTEXTBASE__.ctx


def generate_privkey(privkey_format='wif'):
    """ Create a random private key
    Args:
        privkey_format: the format to export the key ('wif', 'hex', or 'bytes')
    Returns:
        Serialized private key suitable for subsequent calls to e.g. sign().
    """
    return _encode_privkey(secp256k1.PrivateKey(ctx=__CTX__), privkey_format)


def _encode_privkey(privkey, encoding_format='wif'):
    if encoding_format == 'bytes':
        return privkey.private_key
github hyperledger / sawtooth-core / signing / sawtooth_signing / secp256k1_signer.py View on Github external
import logging
import binascii
import warnings
import hashlib
import secp256k1

try:
    # Python 2
    import pybitcointools
except ImportError:
    # Python 3
    import bitcoin as pybitcointools


LOGGER = logging.getLogger(__name__)
__CONTEXTBASE__ = secp256k1.Base(ctx=None, flags=secp256k1.ALL_FLAGS)
__CTX__ = __CONTEXTBASE__.ctx
__PK__ = secp256k1.PublicKey(ctx=__CTX__)  # Cache object to use as factory


def generate_private_key(private_key_format='wif'):
    """ Create a random private key
    Args:
        private_key_format: the format to export the key ('wif', 'hex', or
        'bytes')
    Returns:
        Serialized private key suitable for subsequent calls to e.g. sign().
    """
    return _encode_private_key(
        secp256k1.PrivateKey(ctx=__CTX__), private_key_format)
github hyperledger-labs / private-data-objects / python / sawtooth / helpers / pdo_connect.py View on Github external
PdoContractAddEnclaves,\
    PdoContractRemoveEnclaves,\
    PdoContractEnclavesInfo,\
    PdoProvisioningKeyToStateSecretMap

from  sawtooth.pdo_protos.pdo_contract_ccl_pb2 import\
    CCL_ContractState,\
    CCL_ContractInformation,\
    CCL_TransactionPayload

from sawtooth.helpers.pdo_debug import PdoDbgDump
from sawtooth.helpers.pdo_address_helper import PdoAddressHelper

LOGGER = logging.getLogger(__name__)
STATE_TIMEOUT_SEC = 10
__CONTEXTBASE__ = secp256k1.Base(ctx=None, flags=secp256k1.ALL_FLAGS)
__CTX__ = __CONTEXTBASE__.ctx
__PK__ = secp256k1.PublicKey(ctx=__CTX__)  # Cache object to use as factory


class SignerSecp256k1Lib:
    def __init__(self, private_key):
        self._private_key = private_key
        self._public_key_bytes = None

    def sign(self, message):
        try:
            signature = self._private_key.ecdsa_sign(message)
            signature = self._private_key.ecdsa_serialize_compact(signature)
            sig_hex = signature.hex()
            return sig_hex
        except Exception as e: