def create_payment(self, amount, wallet_id, blockchain, duck): """ Transfer coins from this wallet to other(s). Parameters: - payments: List of duples (wallet id, amount) - blockchain: The complete blockchain - duck: An interface to the Duck functions """ consumed_coins = [] created_coins = [] my_coins = self.get_coins(blockchain) # TODO: Order coins by their values my_coins[:] = [coin for coin in my_coins if coin not in consumed_coins] for coin in my_coins: if coin.value <= amount: consumed_coins.append(coin) consumed_amount = coin.value amount -= coin.value else: new_coins = self.devide_coin(coin, amount, duck) 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( Duckcoin(value=consumed_amount, wallet_id=wallet_id)) if amount == 0: break return Payment(created_coins, consumed_coins)
def test_process_payment_without_signature(self): """ Put coins in Scrooge's wallet, and transfer them to the same wallet without signing """ 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) payment_result = scrooge.process_payment(payment, []) self.assertEqual(payment_result, None)
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 create_payment(self, ids, amount, Ledger, rec_id, goofy): spent = [] balance = [] self.id = ids my_coins = self.get_coins(Ledger) #for user_id, amount in payments: my_coins[:] = [coin for coin in my_coins if coin not in spent] print "*********" for c in my_coins: print c print "*********" flag = 0 bal = 0 for coin in my_coins: if coin.value >= amount: print "coin value ", coin.value print "amount ", amount spent.append(coin) bal = coin.value - amount flag = 1 coin1 = [GoofyCoin(value=amount, user_id=rec_id)] balance.append(GoofyCoin(value=bal, user_id=ids)) goofy.create_coins(coin1) break if flag == 1: print "Payment Successful" else: print "Insufficient balance\n" return Payment(balance=balance, spent=spent)