Python bitcoin.ecdsa_raw_sign() Examples

The following are 8 code examples of bitcoin.ecdsa_raw_sign(). 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 bitcoin , or try the search function .
Example #1
Source File: Crypto.py    From neo-python with MIT License 6 votes vote down vote up
def Sign(message, private_key):
        """
        Sign the message with the given private key.

        Args:
            message (hexstr): message to be signed
            private_key (str): 32 byte key as a double digit hex string (e.g. having a length of 64)
        Returns:
            bytearray: the signature of the message.
        """
        Crypto.SetupSignatureCurve()

        hash = hashlib.sha256(binascii.unhexlify(message)).hexdigest()

        v, r, s = bitcoin.ecdsa_raw_sign(hash, private_key)

        rb = bytearray(r.to_bytes(32, 'big'))
        sb = bytearray(s.to_bytes(32, 'big'))

        sig = rb + sb

        return sig 
Example #2
Source File: Crypto.py    From neo-python-core with MIT License 6 votes vote down vote up
def Sign(message, private_key):
        """
        Sign the message with the given private key.

        Args:
            message (str): message to be signed
            private_key (str): 32 byte key as a double digit hex string (e.g. having a length of 64)
        Returns:
            bytearray: the signature of the message.
        """

        hash = hashlib.sha256(binascii.unhexlify(message)).hexdigest()

        v, r, s = bitcoin.ecdsa_raw_sign(hash, private_key)

        rb = bytearray(r.to_bytes(32, 'big'))
        sb = bytearray(s.to_bytes(32, 'big'))

        sig = rb + sb

        return sig 
Example #3
Source File: ecdsa.py    From solcrypto with GNU General Public License v3.0 5 votes vote down vote up
def sign(messageHash, seckey):
	return pack_signature(*b.ecdsa_raw_sign(messageHash, seckey)) 
Example #4
Source File: test_ecdsa.py    From solcrypto with GNU General Public License v3.0 5 votes vote down vote up
def test_ecdsa(self):
        # Verifies that a random sample of freshly generated keys don't
        # end up setting the 'flag' bit which replaces 'v'
        # If this test ever fails, the entire premise of this thing is fucked!
        for _ in range(0, 100):
            messageHash = randb256()
            seckey = randb256()
            pubkey = pubkey_to_ethaddr(b.privtopub(seckey))

            sig_t = b.ecdsa_raw_sign(messageHash, seckey)
            sig = sign(messageHash, seckey)
            assert unpack_signature(*sig) == sig_t

            pubkey_v = recover(messageHash, *sig)
            assert pubkey == pubkey_v 
Example #5
Source File: dash_utils.py    From dash-masternode-tool with MIT License 5 votes vote down vote up
def ecdsa_sign(msg: str, wif_priv_key: str, dash_network: str):
    """Signs a message with the Elliptic Curve algorithm.
    """

    v, r, s = bitcoin.ecdsa_raw_sign(electrum_sig_hash(msg), wif_priv_key)
    sig = bitcoin.encode_sig(v, r, s)
    pubkey = bitcoin.privkey_to_pubkey(wif_to_privkey(wif_priv_key, dash_network))

    ok = bitcoin.ecdsa_raw_verify(electrum_sig_hash(msg), bitcoin.decode_sig(sig), pubkey)
    if not ok:
        raise Exception('Bad signature!')
    return sig 
Example #6
Source File: dash_utils.py    From dash-masternode-tool with MIT License 5 votes vote down vote up
def ecdsa_sign_raw(msg_raw: bytes, wif_priv_key: str, dash_network: str):
    """Signs raw bytes (a message hash) with the Elliptic Curve algorithm.
    """

    v, r, s = bitcoin.ecdsa_raw_sign(msg_raw, wif_priv_key)
    sig = bitcoin.encode_sig(v, r, s)
    pubkey = bitcoin.privkey_to_pubkey(wif_to_privkey(wif_priv_key, dash_network))

    ok = bitcoin.ecdsa_raw_verify(msg_raw, bitcoin.decode_sig(sig), pubkey)
    if not ok:
        raise Exception('Bad signature!')
    return sig 
Example #7
Source File: crypto.py    From pydevp2p with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def ecdsa_sign(message, privkey):
    s = _encode_sig(*bitcoin.ecdsa_raw_sign(message, privkey))
    return s 
Example #8
Source File: api.py    From blockcypher-python with Apache License 2.0 5 votes vote down vote up
def make_tx_signatures(txs_to_sign, privkey_list, pubkey_list):
    """
    Loops through txs_to_sign and makes signatures using privkey_list and pubkey_list

    Not sure what privkeys and pubkeys to supply?
    Use get_input_addresses() to return a list of addresses.
    Matching those addresses to keys is up to you and how you store your private keys.
    A future version of this library may handle this for you, but it is not trivial.

    Note that if spending multisig funds the process is significantly more complicated.
    Each tx_to_sign must be signed by *each* private key.
    In a 2-of-3 transaction, two of [privkey1, privkey2, privkey3] must sign each tx_to_sign

    http://dev.blockcypher.com/#multisig-transactions
    """
    assert len(privkey_list) == len(pubkey_list) == len(txs_to_sign)
    # in the event of multiple inputs using the same pub/privkey,
    # that privkey should be included multiple times

    signatures = []
    for cnt, tx_to_sign in enumerate(txs_to_sign):
        sig = der_encode_sig(*ecdsa_raw_sign(tx_to_sign.rstrip(' \t\r\n\0'), privkey_list[cnt]))
        err_msg = 'Bad Signature: sig %s for tx %s with pubkey %s' % (
            sig,
            tx_to_sign,
            pubkey_list[cnt],
            )
        assert ecdsa_raw_verify(tx_to_sign, der_decode_sig(sig), pubkey_list[cnt]), err_msg
        signatures.append(sig)
    return signatures