Python ecdsa.SigningKey.generate() Examples

The following are 16 code examples of ecdsa.SigningKey.generate(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ecdsa.SigningKey , or try the search function .
Example #1
Source File: cosmos.py    From agents-aea with Apache License 2.0 6 votes vote down vote up
def _try_get_wealth(self, address: Address) -> None:
        """
        Get wealth from the faucet for the provided address.

        :param address: the address.
        :return: None
        """
        try:
            response = requests.post(
                url=COSMOS_TESTNET_FAUCET_URL, data={"Address": address}
            )
            if response.status_code == 200:
                tx_hash = response.text
                logger.info("Wealth generated, tx_hash: {}".format(tx_hash))
            else:
                logger.warning(
                    "Response: {}, Text: {}".format(response.status_code, response.text)
                )
        except Exception as e:
            logger.warning(
                "An error occured while attempting to generate wealth:\n{}".format(e)
            ) 
Example #2
Source File: client.py    From HWI with MIT License 6 votes vote down vote up
def ec_setup(self):
        # Provides the ECSDA primatives in portable way.
        # Needed to do D-H session key aggreement and then AES.
        # - should be replaced in subclasses if you have other EC libraries
        # - curve is always secp256k1
        # - values are binary strings
        # - write whatever you want onto self.

        # - setup: return 65 of public key, and 16 bytes of AES IV
        # - second call: give the pubkey of far side, calculate the shared pt on curve
        from ecdsa.curves import SECP256k1
        from ecdsa import SigningKey

        self.my_key = SigningKey.generate(curve=SECP256k1, hashfunc=sha256)
        pubkey = self.my_key.get_verifying_key().to_string()
        assert len(pubkey) == 64

        #print("my pubkey = %s" % b2a_hex(pubkey))

        return pubkey 
Example #3
Source File: keys.py    From python-bitcoin-utils with MIT License 6 votes vote down vote up
def __init__(self, wif=None, secret_exponent=None):
        """With no parameters a random key is created

        Parameters
        ----------
        wif : str, optional
            the key in WIF of WIFC format (default None)
        secret_exponent : int, optional
            used to create a specific key deterministically (default None)
        """

        if not secret_exponent and not wif:
            self.key = SigningKey.generate(curve=SECP256k1)
        else:
            if wif:
                self._from_wif(wif)
            elif secret_exponent:
                self.key = SigningKey.from_secret_exponent(secret_exponent,
                                                           curve=SECP256k1) 
Example #4
Source File: template_filters.py    From appr with Apache License 2.0 5 votes vote down vote up
def gen_private_ecdsa():
    from ecdsa import SigningKey
    sk = SigningKey.generate()
    return sk.to_pem() 
Example #5
Source File: wallet.py    From clove with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, private_key=None):
        self.private_key = private_key
        if private_key is None:
            self.private_key = SigningKey.generate(curve=SECP256k1).to_string().hex()
        self._raw_address = utils.privtoaddr(self.private_key)
        self.address = utils.checksum_encode(self._raw_address) 
Example #6
Source File: keys.py    From bitcoin_tools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def generate_keys():
    """ Gets a new  elliptic curve key pair using the SECP256K1 elliptic curve (the one used by Bitcoin).

    :return: elliptic curve key pair.
    :rtype: list
    """

    # Generate the key pair from a SECP256K1 elliptic curve.
    sk = SigningKey.generate(curve=SECP256k1)
    pk = sk.get_verifying_key()

    return sk, pk 
Example #7
Source File: crypto.py    From btcpay-python with MIT License 5 votes vote down vote up
def generate_privkey():
    sk = SigningKey.generate(curve=SECP256k1)
    pem = sk.to_pem()
    pem = pem.decode('utf-8')
    return pem 
Example #8
Source File: key_utils.py    From bitpay-python with MIT License 5 votes vote down vote up
def generate_pem():
  sk = SigningKey.generate(curve=SECP256k1)
  pem = sk.to_pem()
  pem = pem.decode("utf-8")
  return pem 
Example #9
Source File: cosmos.py    From agents-aea with Apache License 2.0 5 votes vote down vote up
def generate_private_key(cls) -> SigningKey:
        """Generate a key pair for cosmos network."""
        signing_key = SigningKey.generate(curve=SECP256k1)
        return signing_key 
Example #10
Source File: sgx.py    From sgx-kms with Apache License 2.0 5 votes vote down vote up
def generate_key_pair(self, key_dir):
        pub_key_path = os.path.join(key_dir, "public_key.pem")
        priv_key_path = os.path.join(key_dir, "private_key.pem")
        if not os.path.exists(pub_key_path) and not os.path.exists(priv_key_path):
            priv_key = SigningKey.generate(curve=NIST256p)
            pub_key = priv_key.get_verifying_key()
            open(priv_key_path, "w").write(priv_key.to_pem())
            open(pub_key_path, "w").write(pub_key.to_pem())
        else:
            priv_key = SigningKey.from_pem(open(priv_key_path).read())
            pub_key = VerifyingKey.from_pem(open(pub_key_path).read())

        pk64 = pub_key.to_string()
        pk_x, pk_y = pk64[:len(pk64)/2], pk64[len(pk64)/2:]

        hex_priv_key = priv_key.to_string()
        hex_sk = hex_priv_key.encode('hex')

        pk_x = pk_x.encode('hex')
        pk_y = pk_y.encode('hex')
        hex_priv_key_out = [hex_sk[i:i + 2]for i in range(0, len(hex_sk), 2)]
        pk_x_out = [pk_x[i:i + 2] for i in range(0,len(pk_x), 2)]
        pk_y_out = [pk_y[i:i + 2] for i in range(0,len(pk_y), 2)]

        pk_x_out.reverse()
        pk_y_out.reverse()

        pub_key = ""
        for i in range(len(pk_x_out)):
            pub_key = pub_key + pk_x_out[i]
        for i in range(len(pk_y_out)):
            pub_key = pub_key + pk_y_out[i]
        hex_priv_key_out.reverse()
        priv_key = ""
        for i in range(len(hex_priv_key_out)):
            priv_key = priv_key + hex_priv_key_out[i]

        pub_key = base64.b64encode(pub_key + '\0')
        priv_key = base64.b64encode(priv_key + '\0')
        return pub_key , priv_key 
Example #11
Source File: http.py    From storj-python-sdk with MIT License 5 votes vote down vote up
def key_generate(self):

        self.logger.info('key_generate()')

        self.logger.debug('This will replace your public and private keys in 3 seconds...')
        time.sleep(3)

        self.private_key = SigningKey.generate(curve=SECP256k1, hashfunc=sha256)
        self.public_key = self.private_key.get_verifying_key()

        s = raw_input('Export keys to file for later use? [Y/N]')
        if 'Y' in s.upper():
            self.key_export()

        self.key_register(self.public_key) 
Example #12
Source File: signing.py    From Adafruit_nRF52_nrfutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def gen_key(self, filename):
        """
        Generate a new Signing key using NIST P-256 curve
        """
        self.sk = SigningKey.generate(curve=NIST256p)

        with open(filename, "w") as sk_file:
            sk_file.write(self.sk.to_pem()) 
Example #13
Source File: wallet.py    From Foundations-of-Blockchain with MIT License 5 votes vote down vote up
def generate_private_key():

    sk = SigningKey.generate(curve=SECP256k1)
    with open(PRIV_KEY_LOC, 'wt') as file_obj:
        file_obj.write(binascii.b2a_hex(sk.to_string()).decode()) 
Example #14
Source File: digital_signature_example.py    From Foundations-of-Blockchain with MIT License 5 votes vote down vote up
def generate_key_pair(self):

        sk = SigningKey.generate(curve=SECP256k1)
        self.private_key = binascii.b2a_hex(sk.to_string()).decode()
        self.public_key = binascii.b2a_hex(sk.get_verifying_key().to_string()).decode() 
Example #15
Source File: template_filters.py    From kpm with Apache License 2.0 5 votes vote down vote up
def gen_private_ecdsa():
    from ecdsa import SigningKey
    sk = SigningKey.generate()
    return sk.to_pem() 
Example #16
Source File: sgx.py    From sgx-kms with Apache License 2.0 4 votes vote down vote up
def generate_key_pair(self):
        separator = "="
        key_dir = None
        with open("/opt/BarbiE/env.properties") as f:
            for line in f:
                if separator in line:
                    name, value = line.split(separator)
                    if name.strip() == "KEY_PAIR_DIR":
                        key_dir = value.strip()
        pub_key_path = os.path.join(key_dir, "public_key.pem")
        priv_key_path = os.path.join(key_dir, "private_key.pem")
        if not os.path.exists(pub_key_path):
            priv_key = SigningKey.generate(curve=NIST256p)
            pub_key = priv_key.get_verifying_key()
            open(priv_key_path,"w").write(priv_key.to_pem())
            open(pub_key_path,"w").write(pub_key.to_pem())
        else:
            priv_key = SigningKey.from_pem(open(priv_key_path).read())
            pub_key = VerifyingKey.from_pem(open(pub_key_path).read())

        pk64 = pub_key.to_string()
        pk_x, pk_y = pk64[:len(pk64)/2], pk64[len(pk64)/2:]
        hex_priv_key = priv_key.to_string()
        hex_sk = hex_priv_key.encode('hex')

        pk_x = pk_x.encode('hex')
        pk_y = pk_y.encode('hex')
        hex_priv_key_out = [hex_sk[i:i + 2]for i in range(0, len(hex_sk), 2)]
        pk_x_out = [pk_x[i:i + 2] for i in range(0,len(pk_x), 2)]
        pk_y_out = [pk_y[i:i + 2] for i in range(0,len(pk_y), 2)]

        pk_x_out.reverse()
        pk_y_out.reverse()

        pub_key = ""
        for i in range(len(pk_x_out)):
            pub_key = pub_key + pk_x_out[i]
        for i in range(len(pk_y_out)):
            pub_key = pub_key + pk_y_out[i]
        hex_priv_key_out.reverse()
        priv_key = ""
        for i in range(len(hex_priv_key_out)):
            priv_key = priv_key + hex_priv_key_out[i]

        pub_key = base64.b64encode(pub_key + '\0')
        priv_key = base64.b64encode(priv_key + '\0')

        return pub_key , priv_key