def test_exception_on_bad_hash(self):
        """
        Tests Exception Handling
        Inputs an invalid (negative) hash value to the native method
        """
        d = pbt.sha256('private key')
        msghash = pbt.electrum_sig_hash('test message')
        z = -pbt.hash_to_int(msghash)
        v, r, s = pbt.ecdsa_raw_sign(msghash, d)
        yBit = v - 27
        with self.assertRaises(ValueError) as context:
            result = ecnative.recover_pubkey(str(z), str(r), str(s), int(yBit))

        self.assertTrue('hash' in str(context.exception))
    def test_exception_on_empty_param(self):
        """
        Tests Exception Handling
        Passes an empty string as an invalid argument to the native method
        """
        d = pbt.sha256('private key')
        msghash = pbt.electrum_sig_hash('test message')
        z = pbt.hash_to_int(msghash)
        v, _, s = pbt.ecdsa_raw_sign(msghash, d)
        y_bit = v - 27
        with self.assertRaises(ValueError) as context:
            _ = ecnative.recover_pubkey(str(z), str(""), str(s), int(y_bit))

        self.assertTrue('Empty string' in str(context.exception))
    def test_exception_on_bad_hash(self):
        """
        Tests Exception Handling
        Inputs an invalid (negative) hash value to the native method
        """
        d = pbt.sha256('private key')
        msghash = pbt.electrum_sig_hash('test message')
        z = -pbt.hash_to_int(msghash)
        v, r, s = pbt.ecdsa_raw_sign(msghash, d)
        yBit = v - 27
        with self.assertRaises(ValueError) as context:
            result = ecnative.recover_pubkey(
                str(z), str(r), str(s),
                int(yBit))

        self.assertTrue('hash' in str(context.exception))
    def test_exception_on_empty_param(self):
        """
        Tests Exception Handling
        Passes an empty string as an invalid argument to the native method
        """
        d = pbt.sha256('private key')
        msghash = pbt.electrum_sig_hash('test message')
        z = pbt.hash_to_int(msghash)
        v, r, s = pbt.ecdsa_raw_sign(msghash, d)
        yBit = v - 27
        with self.assertRaises(ValueError) as context:
            result = ecnative.recover_pubkey(
                str(z), str(""), str(s),
                int(yBit))

        self.assertTrue('Empty string' in str(context.exception))
    def test_exception_on_bad_sig(self):
        """
        Tests Exception Handling
        Inputs an invalid number to the native method
        """
        d = pbt.sha256('private key')
        msghash = pbt.electrum_sig_hash('test message')
        z = pbt.hash_to_int(msghash)
        v, r, s = pbt.ecdsa_raw_sign(msghash, d)
        yBit = v - 27
        badval = "58995174607243353628346858794753620798088291196940745194" \
            "58148184192713284575299999999999999h"
        with self.assertRaises(ValueError) as context:
            result = ecnative.recover_pubkey(str(z), str(badval), str(s),
                                             int(yBit))

        self.assertTrue('Invalid signature' in str(context.exception))
    def test_exception_on_bad_sig(self):
        """
        Tests Exception Handling
        Inputs an invalid number to the native method
        """
        d = pbt.sha256('private key')
        msghash = pbt.electrum_sig_hash('test message')
        z = pbt.hash_to_int(msghash)
        v, r, s = pbt.ecdsa_raw_sign(msghash, d)
        yBit = v - 27
        badval = "58995174607243353628346858794753620798088291196940745194" \
            "58148184192713284575299999999999999h"
        with self.assertRaises(ValueError) as context:
            result = ecnative.recover_pubkey(
                str(z), str(badval), str(s),
                int(yBit))

        self.assertTrue('Invalid signature' in str(context.exception))
 def sign(self, key):
     rawhash = sha256(
         rlp.encode([self.nonce, self.to, self.value, self.fee, self.data]))
     self.v, self.r, self.s = ecdsa_raw_sign(rawhash, key)
     self.sender = bin_sha256(privtopub(key)[1:])[-20:]
     return self