Пример #1
0
    def create_raw_transaction(inputs, outputs):
        coins_sources = []
        coins_to = []
        total_unspent = 0
        total_spent = 0

        for intx in inputs:
            tx_out = TxOut(intx['amount'], h2b(intx['scriptPubKey']))
            coins_source = (h2b(intx['txid'])[::-1], intx['vout'], tx_out)
            coins_sources.append(coins_source)
            total_unspent += intx['amount']

        for outtx in outputs:
            self.coins_to.append((outtx['amount'], address))
            total_spent += outtx['amount']

        fee = total_unspent - total_spent

        if fee < 0:
            message = "not enough source coins (%s BTC) for destination (%s BTC). Short %s BTC" % (
                satoshi_to_btc(total_unspent), satoshi_to_btc(total_spent),
                satoshi_to_btc(-fee))
            raise Exception(message)

        unsigned_tx = UnsignedTx.standard_tx(coins_from, coins_to)
        s = io.BytesIO()
        unsigned_tx.stream(s)
        tx_bytes = s.getvalue()
        tx_hex = binascii.hexlify(tx_bytes).decode("utf8")
        return tx_hex
Пример #2
0
    def create_raw_transaction(inputs, outputs):
        coins_sources = []
        coins_to = []
        total_unspent = 0
        total_spent = 0

        for intx in inputs:
            tx_out = TxOut(intx['amount'], h2b(intx['scriptPubKey']))
            coins_source = (h2b(intx['txid'])[::-1], intx['vout'], tx_out)
            coins_sources.append(coins_source)
            total_unspent += intx['amount']

        for outtx in outputs:
            self.coins_to.append((outtx['amount'], address))
            total_spent += outtx['amount']

        fee = total_unspent - total_spent

        if fee < 0:
            message = "not enough source coins (%s BTC) for destination (%s BTC). Short %s BTC" %  (satoshi_to_btc(total_unspent), satoshi_to_btc(total_spent), satoshi_to_btc(-fee))
            raise Exception(message)
        
        unsigned_tx = UnsignedTx.standard_tx(coins_from, coins_to)
        s = io.BytesIO()
        unsigned_tx.stream(s)
        tx_bytes = s.getvalue()
        tx_hex = binascii.hexlify(tx_bytes).decode("utf8")
        return tx_hex