def run(self): try: NetworkAPI.broadcast_tx(self.tx) self.ui.broadcasted.emit(calc_txid(self.tx)) except ConnectionError as e: self.ui.broadcast_failed.emit("{}".format(e))
def test_cold_storage(self): if TRAVIS and sys.version_info[:2] != (3, 6): return private_key = PrivateKeyTestnet(WALLET_FORMAT_TEST) address = private_key.address prepared = PrivateKeyTestnet.prepare_transaction( address, [('n2eMqTT929pb1RDNuqEnxdaLau1rxy3efi', 1, 'jpy')]) tx_hex = private_key.sign_transaction(prepared) initial = len(private_key.get_transactions()) current = initial tries = 0 NetworkAPI.broadcast_tx_testnet(tx_hex) while tries < 15: # pragma: no cover current = len(private_key.get_transactions()) if current > initial: break time.sleep(5) tries += 1 assert current > initial
def send( self, outputs, fee=None, leftover=None, combine=True, message=None, unspents=None, ): # pragma: no cover """Creates a signed P2PKH transaction and attempts to broadcast it on the blockchain. This accepts the same arguments as :func:`~bitcash.PrivateKey.create_transaction`. :param outputs: A sequence of outputs you wish to send in the form ``(destination, amount, currency)``. The amount can be either an int, float, or string as long as it is a valid input to ``decimal.Decimal``. The currency must be :ref:`supported <supported currencies>`. :type outputs: ``list`` of ``tuple`` :param fee: The number of satoshi per byte to pay to miners. By default Bitcash will poll `<https://bitcoincashfees.earn.com>`_ and use a fee that will allow your transaction to be confirmed as soon as possible. :type fee: ``int`` :param leftover: The destination that will receive any change from the transaction. By default Bitcash will send any change to the same address you sent from. :type leftover: ``str`` :param combine: Whether or not Bitcash should use all available UTXOs to make future transactions smaller and therefore reduce fees. By default Bitcash will consolidate UTXOs. :type combine: ``bool`` :param message: A message to include in the transaction. This will be stored in the blockchain forever. Due to size limits, each message will be stored in chunks of 220 bytes. :type message: ``str`` :param unspents: The UTXOs to use as the inputs. By default Bitcash will communicate with the blockchain itself. :type unspents: ``list`` of :class:`~bitcash.network.meta.Unspent` :returns: The transaction ID. :rtype: ``str`` """ tx_hex = self.create_transaction( outputs, fee=fee, leftover=leftover, combine=combine, message=message, unspents=unspents, ) NetworkAPI.broadcast_tx(tx_hex, network=NETWORKS[self._network]) return calc_txid(tx_hex)
def prepare_transaction(cls, address, outputs, compressed=True, fee=None, leftover=None, combine=True, message=None, unspents=None): """Prepares a P2PKH transaction for offline signing. :param address: The address the funds will be sent from. :type address: ``str`` :param outputs: A sequence of outputs you wish to send in the form ``(destination, amount, currency)``. The amount can be either an int, float, or string as long as it is a valid input to ``decimal.Decimal``. The currency must be :ref:`supported <supported currencies>`. :type outputs: ``list`` of ``tuple`` :param compressed: Whether or not the ``address`` corresponds to a compressed public key. This influences the fee. :type compressed: ``bool`` :param fee: The number of satoshi per byte to pay to miners. By default Bitcash will poll `<https://bitcoincashfees.earn.com>`_ and use a fee that will allow your transaction to be confirmed as soon as possible. :type fee: ``int`` :param leftover: The destination that will receive any change from the transaction. By default Bitcash will send any change to the same address you sent from. :type leftover: ``str`` :param combine: Whether or not Bitcash should use all available UTXOs to make future transactions smaller and therefore reduce fees. By default Bitcash will consolidate UTXOs. :type combine: ``bool`` :param message: A message to include in the transaction. This will be stored in the blockchain forever. Due to size limits, each message will be stored in chunks of 40 bytes. :type message: ``str`` :param unspents: The UTXOs to use as the inputs. By default Bitcash will communicate with the blockchain itself. :type unspents: ``list`` of :class:`~bitcash.network.meta.Unspent` :returns: JSON storing data required to create an offline transaction. :rtype: ``str`` """ unspents, outputs = sanitize_tx_data( unspents or NetworkAPI.get_unspent_testnet(address), outputs, fee or get_fee(), leftover or address, combine=combine, message=message, compressed=compressed) data = { 'unspents': [unspent.to_dict() for unspent in unspents], 'outputs': outputs } return json.dumps(data, separators=(',', ':'))
def get_transactions(self): """Fetches transaction history. :rtype: ``list`` of ``str`` transaction IDs """ self.transactions[:] = NetworkAPI.get_transactions(self.address) return self.transactions
def prepare_tx(): print('Wallet address: ' + sendingAddress + '\n>> Balance: ' + satoshi_to_currency(NetworkAPI.get_balance_testnet(sendingAddress), transferCurrency) + ' BCH') print('>> Tx value ' + str(transferAmount) + ' ' + transferCurrency) tx_data = PrivateKeyTestnet.prepare_transaction( sendingAddress, [(receivingAddress, transferAmount, transferCurrency)]) output_to_json(txDataFile, tx_data)
def get_unspents(self): """Fetches all available unspent transaction outputs. :rtype: ``list`` of :class:`~bitcash.network.meta.Unspent` """ self.unspents[:] = NetworkAPI.get_unspent_testnet(self.address) self.balance = sum(unspent.amount for unspent in self.unspents) return self.unspents
def get_balance(self, currency='satoshi'): """Fetches the current balance by calling :func:`~bitcash.PrivateKey.get_balance` and returns it using :func:`~bitcash.PrivateKey.balance_as`. :param currency: One of the :ref:`supported currencies`. :type currency: ``str`` :rtype: ``str`` """ self.unspents[:] = NetworkAPI.get_unspent(self.address) self.balance = sum(unspent.amount for unspent in self.unspents) return self.balance_as(currency)
def get_balance(self, currency='satoshi'): """Fetches the current balance by calling :func:`~bitcash.PrivateKeyTestnet.get_unspents`. We do not use `~bitcash.PrivateKeyTestnet.balance_as` as Testnet coins do not have a fiat (e.g. USD) value. :param currency: One of the :ref:`supported currencies`. :type currency: ``str`` :rtype: ``str`` """ self.unspents[:] = NetworkAPI.get_unspent_testnet(self.address) self.balance = sum(unspent.amount for unspent in self.unspents) return self.balance
def get_transaction_details(id): transaction = NetworkAPI.get_transaction(id) is_sent = key.address in list( map(lambda txin: txin.address, transaction.inputs)) address = None amount = None if is_sent: address = list(map(lambda txout: txout.address, transaction.outputs))[0] amount = '{:f}'.format(transaction.outputs[0].amount + transaction.amount_fee) else: address = list(map(lambda txin: txin.address, transaction.inputs))[0] amount = '{:f}'.format( next(txout.amount for txout in transaction.outputs if txout.address == key.address)) amount = satoshi_to_currency_cached(int(amount), 'bch') tx = { 'id': transaction.txid, 'inputs': list(map(lambda txin: '{:f}'.format(txin.amount), transaction.inputs)), 'outputs': list( map(lambda txout: '{:f}'.format(txout.amount), transaction.outputs)), 'is_sent': is_sent, 'address': address, 'amount': amount } jsontx = json.dumps(tx) return jsontx
def broadcast_tx(): inputStream = open(txHexFile, 'r') transactionHex = inputStream.read() inputStream.close() broadcastTx = NetworkAPI.broadcast_tx_testnet(transactionHex) print(broadcastTx)