def send(self, outputs, fee=None, leftover=None, combine=True, message=None, unspents=None): """Creates a signed P2PKH transaction and attempts to broadcast it on the testnet blockchain. This accepts the same arguments as :func:`~bitsv.PrivateKeyTestnet.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 testnet blockchain itself. :type unspents: ``list`` of :class:`~bitsv.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_testnet(tx_hex) return calc_txid(tx_hex)
def get_transactions(self): """Fetches transaction history. :rtype: ``list`` of ``str`` transaction IDs """ self.transactions[:] = NetworkAPI.get_transactions_testnet(self.address) return self.transactions
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 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:`~bitsv.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 send(self, outputs, fee=None, leftover=None, combine=True, message=None, unspents=None, custom_pushdata=False): # pragma: no cover """Creates a signed P2PKH transaction and attempts to broadcast it on the blockchain. This accepts the same arguments as :func:`~bitsv.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 BitSV will use a fee of 1 sat/byte. :type fee: ``int`` :param leftover: The destination that will receive any change from the transaction. By default BitSV will send any change to the same address you sent from. :type leftover: ``str`` :param combine: Whether or not BitSV should use all available UTXOs to make future transactions smaller and therefore reduce fees. By default BitSV 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 100kb. :type message: ``str`` if custom_pushdata = False; ``list`` of ``tuple`` if custom_pushdata = True :param unspents: The UTXOs to use as the inputs. By default BitSV will communicate with the blockchain itself. :type unspents: ``list`` of :class:`~bitsv.network.meta.Unspent` :param custom_pushdata: Adds control over push_data elements inside of the op_return by adding the "custom_pushdata" = True / False parameter as a "switch" to the :func:`~bitsv.PrivateKey.send` function and the :func:`~bitsv.PrivateKey.create_transaction` functions. :type custom_pushdata: ``bool`` :returns: The transaction ID. :rtype: ``str`` """ self.get_unspents() tx_hex = self.create_transaction( outputs, fee=fee, leftover=leftover, combine=combine, message=message, unspents=unspents, custom_pushdata=custom_pushdata ) NetworkAPI.broadcast_tx(tx_hex) return calc_txid(tx_hex)
def get_unspents(self): """Fetches all available unspent transaction outputs. :rtype: ``list`` of :class:`~bitsv.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 prepare_transaction(cls, address, outputs, compressed=True, fee=None, leftover=None, combine=True, message=None, unspents=None, custom_pushdata=False): # pragma: no cover """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 BitSV will use a fee of 1 sat/byte :type fee: ``int`` :param leftover: The destination that will receive any change from the transaction. By default BitSV will send any change to the same address you sent from. :type leftover: ``str`` :param combine: Whether or not BitSV should use all available UTXOs to make future transactions smaller and therefore reduce fees. By default BitSV 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 100kb bytes. :type message: ``str`` if custom_pushdata = False; ``list`` of ``tuple`` if custom_pushdata = True :param unspents: The UTXOs to use as the inputs. By default BitSV will communicate with the blockchain itself. :type unspents: ``list`` of :class:`~bitsv.network.meta.Unspent` :param custom_pushdata: Adds control over push_data elements inside of the op_return by adding the "custom_pushdata" = True / False parameter as a "switch" to the :func:`~bitsv.PrivateKey.send` function and the :func:`~bitsv.PrivateKey.create_transaction` functions. :type custom_pushdata: ``bool`` :returns: JSON storing data required to create an offline transaction. :rtype: ``str`` """ unspents, outputs = sanitize_tx_data( unspents or NetworkAPI.get_unspent(address), outputs, fee or get_fee(), leftover or address, combine=combine, message=message, compressed=compressed, custom_pushdata=custom_pushdata ) data = { 'unspents': [unspent.to_dict() for unspent in unspents], 'outputs': outputs } return json.dumps(data, separators=(',', ':'))
def load_data(transaction_id): """ Retrieves a data transaction from the blockchain :param transaction_id: The transaction ID to look up """ # Get transaction transaction = NetworkAPI.get_transaction(transaction_id) # Get data data = transaction['data']['vout'][1]['scriptPubKey']['hex'] # Decode to bytes data = bytes.fromhex(data) # Handle op codes if data[1] == 76: # Clear 3 bytes data = data[2:] elif data[1] == 77: # Clear 5 bytes data = data[4:] elif data[1] == 78: # Clear 7 bytes data = data[6:] else: data = data[1:] # Decode into utf-8 data = data.decode('utf-8', errors='ignore') return data
private_key_bytes, compressed, version = wif_to_bytes(wif) if version == 'main': if compressed: return PrivateKey.from_bytes(private_key_bytes) else: return PrivateKey(wif) else: if compressed: return PrivateKeyTestnet.from_bytes(private_key_bytes) else: return PrivateKeyTestnet(wif) # Instantiate Main, Test and STN NetworkAPI instances for use by all PrivateKey[Test]'s network_api_main = NetworkAPI('main') network_api_test = NetworkAPI('test') network_api_stn = NetworkAPI('stn') class BaseKey: """This class represents a point on the elliptic curve secp256k1 and provides all necessary cryptographic functionality. You shouldn't use this class directly. :param wif: A private key serialized to the Wallet Import Format. If the argument is not supplied, a new private key will be created. The WIF compression flag will be adhered to, but the version byte is disregarded. Compression will be used by all new keys. :type wif: ``str`` :raises TypeError: If ``wif`` is not a ``str``.