def get_verifying_key(serialized_msg, serialized_sig): """Attempts to recover a public key from a message and a signature. Args: serialized_msg (str): A serialized message. serialized_sig (str): A serialized signature. Returns: str: a public key. """ v, r, s = pybitcointools.decode_sig(serialized_sig) msghash = pybitcointools.electrum_sig_hash(serialized_msg) z = pybitcointools.hash_to_int(msghash) compress = True if v >= 31 else False if compress: rec = v - 31 else: rec = v - 27 try: pubkey = nativeECDSA.recover_pubkey( str(z), str(r), str(s), int(rec)) except Exception as ex: logger.warn('Unable to extract public key from signature' + ex.args[0]) return "" pubkey = pubkey.translate(None, 'h') # strip out hex indicators from opencpp pubkey = '04' + pubkey # indicate uncompressed pubkey if compress: pubkey = pybitcointools.compress(pubkey) return pubkey
def get_verifying_key(serialized_msg, serialized_sig): """Attempts to recover a public key from a message and a signature. Args: serialized_msg (str): A serialized message. serialized_sig (str): A serialized signature. Returns: str: a public key. """ v, r, s = pybitcointools.decode_sig(serialized_sig) msghash = pybitcointools.electrum_sig_hash(serialized_msg) z = pybitcointools.hash_to_int(msghash) compress = True if v >= 31 else False if compress: rec = v - 31 else: rec = v - 27 try: pubkey = nativeECDSA.recover_pubkey( str(z), str(r), str(s), int(rec)) except Exception as ex: logger.warn('Unable to extract public key from signature' + ex.args[0]) return "" pubkey = pubkey.translate(None, 'h') # strip out hex indicators from opencpp pubkey = '04' + pubkey # indicate uncompressed pubkey if compress: pubkey = pybitcointools.compress(pubkey) return pubkey
def recover_pubkey(message, signature): v, r, s = pybitcointools.decode_sig(signature) msghash = pybitcointools.electrum_sig_hash(message) z = pybitcointools.hash_to_int(msghash) compress = True if v >= 31 else False if compress: rec = v - 31 else: rec = v - 27 try: pubkey = nativeECDSA.recover_pubkey(str(z), str(r), str(s), int(rec)) except ValueError as ex: LOGGER.warning('Unable to extract public key from signature' + ex.args[0]) return "" try: # pybitcointools package pubkey = pubkey.translate(None, 'h') except TypeError: # bitcoin package pubkey = pubkey.translate('h') pubkey = '04' + pubkey if compress: pubkey = pybitcointools.compress(pubkey) return pubkey
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 recover_pubkey(message, signature): v, r, s = pybitcointools.decode_sig(signature) msghash = pybitcointools.electrum_sig_hash(message) q = pybitcointools.ecdsa_raw_recover(msghash, (v, r, s)) # A prior implementation set the behavior that this # method should return an encoded recovered pubkey or # an empty string if there is a recovery error. This # differs from the pybitcointools implementation of # ecdsa_recover, which returns the recovered pubkey # or False, if a recovery error occurs. if not q: return "" compress = True if v >= 31 else False if compress: pubkey = pybitcointools.encode_pubkey(q, 'hex_compressed') else: pubkey = pybitcointools.encode_pubkey(q, 'hex') return pubkey
def get_verifying_key(serialized_msg, serialized_sig): """Attempts to recover a public key from a message and a signature. Args: serialized_msg (str): A serialized message. serialized_sig (str): A serialized signature. Returns: str: a public key. """ v, r, s = pybitcointools.decode_sig(serialized_sig) msghash = pybitcointools.electrum_sig_hash(serialized_msg) z = pybitcointools.hash_to_int(msghash) yBit = v - 27 try: pubkey = nativeECDSA.recover_pubkey(str(z), str(r), str(s), int(yBit)) except Exception as ex: logger.warn('Unable to extract public key from signature' + ex.args[0]) return "" pubkey = pubkey.translate(None, 'h') # strip out hex indicators from opencpp pubkey = "04" + pubkey # add header to match pybitcointools format return pubkey
def get_verifying_key(serialized_msg, serialized_sig): """Attempts to recover a public key from a message and a signature. Args: serialized_msg (str): A serialized message. serialized_sig (str): A serialized signature. Returns: str: a public key. """ v, r, s = pybitcointools.decode_sig(serialized_sig) msghash = pybitcointools.electrum_sig_hash(serialized_msg) z = pybitcointools.hash_to_int(msghash) yBit = v - 27 try: pubkey = nativeECDSA.recover_pubkey( str(z), str(r), str(s), int(yBit)) except Exception as ex: logger.warn('Unable to extract public key from signature' + ex.args[0]) return "" pubkey = pubkey.translate(None, 'h') # strip out hex indicators from opencpp pubkey = "04" + pubkey # add header to match pybitcointools format return pubkey