def test_sign_and_verify(self): """ Sign a transaction and verify the signature """ wallet = Wallet() coins = [Scroogecoin(value=2, wallet_id=wallet.id)] transaction = CoinCreation(created_coins=coins) encoded_hash = encoded_hash_object(transaction) self.assertTrue( wallet.verify_signature(wallet.verifying_key, wallet.sign(encoded_hash), encoded_hash))
def test_process_payment_with_signature(self): """ Put coins in Scrooge's wallet, and transfer them to the same wallet """ scrooge = Scrooge() coin = Scroogecoin(value=2, wallet_id=scrooge.wallet.id) created_coins = scrooge.create_coins([coin]).transaction.created_coins payment = Payment(created_coins=[coin], consumed_coins=created_coins) signature = scrooge.wallet.sign(encoded_hash_object(payment)) payment_result = scrooge.process_payment( payment, [(scrooge.wallet.verifying_key, signature)]) self.assertFalse(payment_result == None)
def devide_coin(self, coin, value, duck): """ Devide a coin in two new coins. The paramenter 'value' is the value of one of the new coins and the value of the other is the rest. The original coin is consumed and cannot be used again. """ if value > coin.value: return created_coins = [] created_coins.append(Duckcoin(value, self.id)) created_coins.append(Duckcoin(coin.value - value, self.id)) payment = Payment(created_coins=created_coins, consumed_coins=[coin]) signature = self.sign(encoded_hash_object(payment)) new_block = duck.process_payment(payment, [(self.Public_Key, signature)]) return new_block.transaction.created_coins
def verify_signatures(self, transaction, signatures): """ Verify a list of transaction signatures """ # Verify all signatures with their corresponding # public keys for Public_Key, signature in signatures: if not self.wallet.verify_signature( Public_Key, signature, encoded_hash_object(transaction)): return False # Verify if all users whose coins will be consumed signed # the payment users = [] for Public_Key, signature in signatures: wallet_id = self.wallet.get_wallet_id_from_Public_Key(Public_Key) users.append(wallet_id) for coin in transaction.consumed_coins: if coin.wallet_id not in users: return False return True
for file in os.listdir(tran_dir): if file.endswith('.json'): file_name = tran_dir + "/" + file with open(file_name, encoding='utf_8_sig') as f: d = json.load(f) sender = d['sender'] reciver = d['reciver'] value = d['tran_value'] sender = Wallet() reciver = Wallet() payment = sender.create_payment(value, reciver.id, duck.blockchain, duck) signature = sender.sign(encoded_hash_object(payment)) payment_result = duck.process_payment( payment, [(sender.Public_Key, signature)]) sender_pk = hash_object(sender.Public_Key.to_string()) receiver_pk = hash_object(reciver.Public_Key.to_string()) sig_str = '' sig_str = duck.blockchain.temp_hash_block Sender_output = {"value": value, "pubkey": sender_pk} Reciver_input = [{"number": 1, "output": Sender_output}] Reciver_output = [{"value": value, "pubkey": receiver_pk}] Tran_file = [{ "number": duck.genesis_block_hash, "input": Reciver_input,