Example #1
0
    def send(self, amount, to_addr):
        """
        Send money to the given address from this wallet.

        Args:
            amount: How much value is sent
            to_addr: Address where value is transferred to.
        """
        tx = Transaction(self.key)

        utxo = self.blockchain.scan_unspent_transactions(
                self.key.publickey())
        debit = sum(map(lambda x: x['value'], utxo))
        change = debit - amount

        if change < 0:
            raise InsufficientFundsException()

        for credit in utxo:
            signature = sign(self.key, credit['hash'])
            tx.add_in(credit['hash'],
                      signature, self.key.publickey(), credit['value'])

        tx.add_out(amount, to_addr)
        tx.add_out(change, self.key.publickey())

        self.blockchain.add(tx)