def get_signature_recovery_value(message: bytes, signature: Signature, public_key: PublicKey) -> bytes: """ Obtains the recovery value of a standard ECDSA signature. :param message: Signed message :param signature: The signature from which the pubkey is recovered :param public_key: The public key for verifying the signature :param is_prehashed: True if the message is already pre-hashed. Default is False, and message will be hashed with SHA256 :return: The compressed byte-serialized representation of the recovered public key """ signature = bytes(signature) ecdsa_signature_size = 64 # two curve scalars if len(signature) != ecdsa_signature_size: raise ValueError( f"The signature size should be {ecdsa_signature_size} B.") hash_ctx = hashes.Hash(hashes.SHA256(), backend=backend) hash_ctx.update(message) message_hash = hash_ctx.finalize() address = canonical_address_from_umbral_key(public_key) for v in (0, 1): v_byte = bytes([v]) recovered = to_canonical_address( Account.recoverHash(message_hash, signature=signature + v_byte)) if recovered == address: return v_byte else: raise ValueError( "Signature recovery failed. " "Either the message, the signature or the public key is not correct" )
def process(self, incoming_demand): rospy.loginfo('Incoming ask %s...', str(incoming_demand)) if (incoming_demand.model == rospy.get_param('~model') and incoming_demand.token == rospy.get_param('~token') and incoming_demand.cost == 0): rospy.loginfo('Starting process...') with NamedTemporaryFile(delete=False) as tmpfile: recorder = rosbag.Bag(tmpfile.name, 'w') output = 'St.Petersburg: {{"DUST": {}, "CO": {}, "LPG": {}, "METHANE": {}, "SMOKE": {}, "HYDROGEN": {}}}'.format( *self.measurements) recorder.write('/worker/data', String(data=output)) recorder.close() # recover account of sender msg_hash = defunct_hash_message( self.demandhash(incoming_demand)) sender_account = Account.recoverHash( msg_hash, signature=incoming_demand.signature) ipfs_response = self.ipfs.add(tmpfile.name) res = Result() res.liability = sender_account res.result = ipfs_response res.success = True self.pub.publish(res) rospy.loginfo('Process complete.')
def recover_address_from_sig(): signed_timestamp_token = request.headers.get('X-Wallet-Signature') if signed_timestamp_token: # 'adabdha_'+timestamp+':'+walletSign('adabdha_'+timestamp) string_data, signed_data = signed_timestamp_token.split(':') string_data_hashed = defunct_hash_message(string_data.encode('utf-8')) recovered_eth_address = Account.recoverHash( message_hash=string_data_hashed, signature=signed_data) recovered_eth_address = eth_utils.to_normalized_address( recovered_eth_address) return recovered_eth_address else: return None
def ec_recover(message: str, signed_message: str) -> str: """ This method does not prepend the message with the prefix `\x19Ethereum Signed Message:\n32`. The caller should add the prefix to the msg/hash before calling this if the signature was produced for an ethereum-prefixed message. :param message: :param signed_message: :return: """ v, r, s = split_signature(Web3.toBytes(hexstr=signed_message)) signature_object = SignatureFix(vrs=(v, big_endian_to_int(r), big_endian_to_int(s))) return Account.recoverHash(message, signature=signature_object.to_hex_v_hacked())
def verify_message(self, message, signed_message=None, address=None, use_tron: bool = True): """ Get the address of the account that signed the message with the given hash. You must specify exactly one of: vrs or signature Args: message (str): The message in the format "hex" signed_message (AttributeDict): Signature address (str): is Address use_tron (bool): is Tron header """ if address is None: address = self.tron.default_address.base58 if not is_hex(message): raise TronError('Expected hex message input') # Determine which header to attach to the message # before encrypting or decrypting header = TRX_MESSAGE_HEADER if use_tron else ETH_MESSAGE_HEADER message_hash = self.tron.sha3(text=header + message) recovered = EthAccount.recoverHash(message_hash, signature=signed_message.signature) tron_address = '41' + recovered[2:] base58address = self.tron.address.from_hex(tron_address).decode() if base58address == address: return True raise ValueError('Signature does not match')