def test_sign_message(): network_code = 'UNIT_TESTS' private_key = '3rzY3EENhYrWXzUqNnMEbGUr3iEzzSZrjMwJ1CgQpJpq' tx = 'Chainium' expected = 'EYzWMyZjqHkwsNFKcFEg4Q64m4jSUD7cAeKucyZ3a9MKeNmXTbRK3czqNVGj9RpkPGji9AtGiUxDtipqE3DtFPHxU' actual = crypto.sign_message(network_code, private_key, tx) assert expected == actual
def add_transaction(to, amount): t = Transaction( address, to, amount, '', public_key.dumps(), ) t.sign = sign_message(private_key.d, private_key.n, t.get_message()) r = requests.post(b_url + '/add_transaction', json=t.to_dict()) print(r.text)
def create_random_txn(self): # look at the block chain and see how much money you have if self.first_txn and (self.len_list == len(self.temp_utxo_list)): return None total_cash = self.blockchain.get_max_height_node_UTXO_pool().get_total_unspent_utxo() if len(total_cash) > 0: my_utxos_and_values = total_cash[hex(generate_hash(str((self.public_key.e,self.public_key.n))))] if not self.first_txn: self.len_list = len(my_utxos_and_values) #create blank Transactions new_txn = Transaction() # choose random (utxo, op) and add them to inputs if len(my_utxos_and_values) == 0: return None r = randint(1, len(my_utxos_and_values)) if len(my_utxos_and_values) > 1 else 1 chosen_utxos = sample(my_utxos_and_values, r) self.temp_utxo_list += chosen_utxos value = 0 for utxo, op_value in chosen_utxos: new_txn.add_input(utxo.txn_hash, utxo.index) value += op_value # Choose a 'k' random neighbours and send a random amts to them k = randint(1, len(self.node_pool)) lucky_neighbours = sample(self.node_pool.keys(), k) for neigh in lucky_neighbours: if value <= 0: break neigh_value = randint(0, value) if neigh_value == 0: continue new_txn.add_output(self.node_pool_address[neigh], neigh_value) value -= neigh_value if value > 0: new_txn.add_output(self.public_key, value) for i in range(new_txn.total_inputs()): message = new_txn.get_raw_signature(i) signature = sign_message(message=message, keyPair=self.key_pair) new_txn.add_signature(i, signature) new_txn.get_input(i).add_unlocking_script(signature, self.public_key) new_txn.finalize() self.first_txn = True return new_txn else: print(f'Node {self.id} could not create a transaction. not utxos ') return None
pub_key = pub_key.decode("utf-8") msg = f"SYS:SHARE_MY_PUBLIC_KEY:{USERNAME}:{pub_key}" client_socket.send(bytes(msg, "utf8")) # 4 share session key as encypted message if CHAT_USERNAME: # pass client_socket.send(bytes(f"SYS:REQUEST_USER_KEY:{CHAT_USERNAME}", "utf8")) raw_msg = client_socket.recv(BUFSIZ).decode("utf8") if raw_msg.startswith("SYS:"): msg = raw_msg.split(":") if msg[1] is not "NONE": encrypted_session_key = crypto.encypt_session_key( session_key, msg[1]) message_signature = crypto.sign_message(my_rsa_key.export_key(), session_key) client_socket.send( bytes( f"SYS:ENCRYPTED_SESSION_KEY:{encrypted_session_key.hex()}", "utf8")) client_socket.send( bytes(f"SYS:SIGNATURE:{message_signature.hex()}", "utf8")) signature = message_signature global_data["session_key"] = session_key print("Connected with chat application") print("Chat Start")
def sign(self, network_code, private_key): json = self.to_json() signature = crypto.sign_message(network_code, private_key, json) return {'tx': crypto.encode64(json.encode()), 'signature': signature}