Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_process_payment_without_signature(self):
""" Put coins in the Goofy's wallet, and transfer them
to the same wallet without signing
"""
goofy = Goofy()
coin = Goofycoin(value=2, wallet_id=goofy.wallet.id)
created_coins = goofy.create_coins([coin]).transaction.created_coins
payment = Payment(created_coins=[coin], consumed_coins=created_coins)
payment_result = goofy.process_payment(payment, [])
self.assertEqual(payment_result, None)
def test_process_payment_with_signature(self):
""" Put coins in the Goofy's wallet, and transfer them
to the same wallet
"""
goofy = Goofy()
coin = Goofycoin(value=2, wallet_id=goofy.wallet.id)
created_coins = goofy.create_coins([coin]).transaction.created_coins
payment = Payment(created_coins=[coin], consumed_coins=created_coins)
signature = goofy.wallet.sign(encoded_hash_object(payment))
payment_result = goofy.process_payment(
payment, [(goofy.wallet.verifying_key, signature)]
)
self.assertFalse(payment_result == None)
def devide_coin(self, coin, value, scrooge):
""" 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(Scroogecoin(value, self.id))
created_coins.append(Scroogecoin(coin.value - value, self.id))
payment = Payment(created_coins=created_coins, consumed_coins=[coin])
signature = self.sign(encoded_hash_object(payment))
new_block = scrooge.process_payment(
payment, [(self.verifying_key, signature)]
)
return new_block.transaction.created_coins
print("\n\n*** Pressione Ctrl+C para finalizar a aplicacao ***")
try:
while (True):
# Read menu option
option = readOption()
# Handle selected option
returnValue = None
transactionResult = TransactionResult()
print('')
if option == 1:
# Payment option
returnValue = transaction.Payment(pp, ENCODING).execute(transactionResult)
elif option == 2:
# Cancel transaction
returnValue = transaction.CancelPayment(pp).execute(transactionResult)
elif option == 3:
# Query last transaction
returnValue = transaction.QueryLastTransaction(pp).execute(transactionResult)
if returnValue is not None:
printResult(returnValue, transactionResult)
except KeyboardInterrupt:
print("\n\nFIM\n..........")
def check_coin(self, coin):
""" Check if the coin was created and was not consumed """
creation_id = coin.id.transaction_id
# Check created
if coin not in self.blocks[creation_id].transaction.created_coins:
print('WARNING: Coin creation not found')
return False
# Check not consumed
for ind in range(creation_id + 1, len(self.blocks)):
transaction = self.blocks[ind].transaction
if isinstance(transaction, Payment) and coin in transaction.consumed_coins:
print('WARNING: Double spent attempt detected')
return False
return True
consumed_coins.append(coin)
consumed_amount = coin.value
amount -= coin.value
else:
new_coins = self.devide_coin(coin, amount, scrooge)
consumed_ind = self.index_coin_value(new_coins, amount)
consumed_coins.append(new_coins[consumed_ind])
consumed_amount = amount
my_coins.append(new_coins[consumed_ind + 1])
amount = 0
created_coins.append(
Scroogecoin(value=consumed_amount, wallet_id=wallet_id)
)
if amount == 0:
break
return Payment(created_coins, consumed_coins)