def sender_property_patched(self: Transaction): if self._sender: return self._sender if self.v and self.v >= 35: v = bytes([self.v]) r = self.r.to_bytes(32, byteorder='big') s = self.s.to_bytes(32, byteorder='big') raw_tx = Transaction(self.nonce, self.gasprice, self.startgas, self.to, self.value, self.data, (self.v - 35) // 2, 0, 0) msg = keccak256(rlp.encode(raw_tx)) self._sender = decode_hex(addr_from_sig(r + s + v, msg)) return self._sender else: return sender_property_original(self)
def on_new_balance_sig(self, command: str, customer: str, sender: str, open_block_number: int, balance_message_hash: bytes, round_number: int, signature: str, customer_sig: str): #print('\nbalance sig') #debug_print([customer, sender, open_block_number, balance_message_hash, signature]) customer = to_checksum_address(customer) sender = to_checksum_address(sender) if not self.verify_customer_deposit(customer): return NO_CONTRACT_DEPOSIT current_round_number = self.jobs[customer, sender, open_block_number].round_number try: assert self.jobs[ customer, sender, open_block_number].round_number == round_number - 1 except AssertionError: print( bcolors.OKBLUE + 'gave incorrent monitor number (current %d, expected %d, received %d)' % (current_round_number, current_round_number + 1, round_number) + bcolors.ENDC) return try: assert self.jobs[customer, sender, open_block_number].last_signature != signature self.log.info('Signature is different') self.log.info( 'Current round number %d, state hash round number %d', self.jobs[customer, sender, open_block_number].round_number, round_number) final_hash = monitor_balance_message_from_state( balance_message_hash, round_number) #self.log.info('Monitors hash: (balanace message hash %s)', encode_hex(balance_message_hash)) self.log.info( 'Monitors hash: (round number %s, balanace message hash %s, final hash %s)', round_number, encode_hex(balance_message_hash), encode_hex(final_hash)) """regular""" #sig_addr = addr_from_sig(decode_hex(signature), balance_message_hash) """with receipt""" sig_addr = addr_from_sig(decode_hex(signature), final_hash) if not is_same_address(sig_addr, sender): self.log.info('balance message not signed by correct sender') return BALANCE_SIG_NOT_ACCEPTED """regular""" #customer_sig_addr = addr_from_sig(decode_hex(customer_sig), balance_message_hash) """with receipt""" customer_sig_addr = addr_from_sig(decode_hex(customer_sig), final_hash) if not is_same_address(customer_sig_addr, customer): self.log.info('customers balance sig is wrong. address: %s', customer_sig_addr) return BALANCE_SIG_NOT_ACCEPTED self.jobs[customer, sender, open_block_number].round_number += 1 receipt = self.create_signed_receipt(customer, sender, open_block_number, balance_message_hash, signature, round_number) #receipt = self.create_signed_receipt(customer, sender, open_block_number, balance_message_hash, round_number, signature) #self.customer_fair_exchange(customer, sender, open_block_number, balance_message_hash, signature) self.jobs[customer, sender, open_block_number].last_signature = signature self.jobs[customer, sender, open_block_number].last_hash = balance_message_hash self.jobs[customer, sender, open_block_number].last_customer_sig = customer_sig self.jobs[customer, sender, open_block_number].last_round_number = round_number self.jobs[customer, sender, open_block_number].all_signatures.append(signature) self.jobs[customer, sender, open_block_number].all_customer_signatures.append( customer_sig) self.jobs[customer, sender, open_block_number].all_hashes.append( balance_message_hash) self.jobs[customer, sender, open_block_number].all_round_numbers.append(round_number) self.log.info( 'Accepting new balance signature (customer %s, sender %s, open block number %d)', customer, sender, open_block_number) # self.log.info('Accepting new balance signature (customer %s, sender %s, open block number %d, \n\told hash %s, \n\tnew hash %s)', # customer, # sender, # open_block_number, # encode_hex(self.jobs[customer,sender,open_block_number].last_hash), # encode_hex(balance_message_hash) # ) self.jobs[customer, sender, open_block_number].sent_receipt = True self.log.debug( 'accepted balance signature (customer %s, sender %s, open_block_number %s\n', customer, sender, open_block_number) # self.log.info('TYPE OF RECEIPT %s', str(type(receipt))) # self.log.info('RECEIPT %s', str(receipt)) return receipt except KeyError: self.log.info('balance sig for job not being watched\n') return IGNORE_BALANCE_SIG except AssertionError: self.log.info('customer sent the same signature as before: %s\n', encode_hex(signature)) return IGNORE_SAME_BALANCE_SIG
def test_sign_v0(): msg = keccak256('hello v=0') sig = sign(SENDER_PRIVATE_KEY, msg) assert sig[-1] == 1 assert is_same_address(addr_from_sig(sig, msg), SENDER_ADDR)