Esempio n. 1
0
    def send_funds_from_to(self, sender_public_key_str: str, sender_private_key_str: str,
                           recipient_public_key_str: str, value: float):
        if self.get_balance_for_public_key(sender_public_key_str) < value:
            print("Not enough balance, transaction discarded")
            return None

        if value <= 0:
            print("Value should be positive, transaction discarded")
            return None

        inputs = []
        total = 0
        for transaction_id, utxo in self.all_utxos.items():
            if utxo.is_mine(sender_public_key_str):
                total += utxo.value
                inp = TransactionInput(transaction_id)
                inputs.append(inp)

                if total >= value + self.fee:
                    break

        transaction = Transaction(sender_public_key_str, recipient_public_key_str, value, inputs)
        encoded = base64.b64decode(sender_private_key_str.encode('utf8'))
        sender_private_formated = PEM.encode(encoded, 'RSA PRIVATE KEY')
        transaction.generate_signature(import_key(sender_private_formated.encode('utf8')))
        transaction.process_transaction(self.all_utxos, self.minimum_transaction, self.fee)
        return transaction
Esempio n. 2
0
    def send_funds(self, all_utxos, recipient_public_key_str: str,
                   value: float) -> Transaction:
        self.update_utxos(all_utxos)

        if self.get_balance(all_utxos) < value:
            raise Exception("Not enough balance, transaction discarded")

        if value <= 0:
            raise Exception("Value should be positive, transaction discarded")

        inputs = []
        total = 0
        for transaction_id, utxo in self.utxos.items():
            total += utxo.value
            inp = TransactionInput(transaction_id)
            inputs.append(inp)

            if total >= value:
                break

        transaction = Transaction(self.public_key_str,
                                  recipient_public_key_str, value, inputs)
        transaction.generate_signature(self.private_key)

        for inp in inputs:
            del self.utxos[inp.transaction_output_id]

        return transaction
Esempio n. 3
0
def get_genesis_transaction(sender_wallet, recipient_wallet, value, all_utxos):
    genesis_transaction = Transaction(
        sender_wallet.public_key_as_str(),
        recipient_wallet.public_key_as_str(),
        value,
        []
    )

    genesis_transaction.generate_signature(sender_wallet.private_key)  # manually sign genesis transaction

    genesis_transaction.transaction_id = "0"  # manually set transaction id

    # manually generate transaction output
    genesis_transaction.outputs.append(TransactionOutput(
        genesis_transaction.recipient,
        genesis_transaction.value,
        genesis_transaction.transaction_id
    ))

    # its important to save our first transaction output in utxos
    all_utxos[genesis_transaction.outputs[0].id] = genesis_transaction.outputs[0]

    return genesis_transaction