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 Bit will poll `<https://bitcoinfees.21.co>`_ 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 Bit will send any change to the same address you sent from. :type leftover: ``str`` :param combine: Whether or not Bit should use all available UTXOs to make future transactions smaller and therefore reduce fees. By default Bit 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 Bit will communicate with the blockchain itself. :type unspents: ``list`` of :class:`~bit.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_cached(), 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 create_transaction(self, outputs, fee=None, leftover=None, combine=True, message=None, unspents=None): """Creates a signed P2PKH 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 Bit will poll `<https://bitcoinfees.21.co>`_ 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 Bit will send any change to the same address you sent from. :type leftover: ``str`` :param combine: Whether or not Bit should use all available UTXOs to make future transactions smaller and therefore reduce fees. By default Bit 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 Bit will communicate with the testnet blockchain itself. :type unspents: ``list`` of :class:`~bit.network.meta.Unspent` :returns: The signed transaction as hex. :rtype: ``str`` """ unspents, outputs = sanitize_tx_data(unspents or self.unspents, outputs, fee or get_fee_cached(), leftover or self.address, combine=combine, message=message, compressed=self.is_compressed()) return create_p2pkh_transaction(self, unspents, outputs)
def get_address(message): current_user_id = message.from_user.id current_wallet = wallet_exist(current_user_id) current_key = Key(current_wallet[1]) solde = decimal.Decimal(current_key.get_balance('btc')) global destinataire if message.text == 'Annuler': msg = bot.send_message( message.chat.id, "Bonjour, voici l'adresse de votre wallet Bitcoin : \n\n```" + current_wallet[0] + "```\n\nUtilisez-la pour réapprovisionner votre porte-feuille Bitcoin.", reply_markup=markup, parse_mode="Markdown") bot.register_next_step_handler(msg, process_step) else: destinataire = str(message.text) if (check_address(destinataire) == False): msg = bot.send_message( message.chat.id, "L'adresse du destinataire est incorrecte !", reply_markup=markup, parse_mode="Markdown") bot.register_next_step_handler(msg, process_step) else: commission = decimal.Decimal(get_fee_cached() * 250 / 100000000) msg = bot.send_message( message.chat.id, "Quelle somme voulez-vous envoyer ?\n\nFonds disponibles : " + str(solde) + " BTC (~" + str( satoshi_to_currency( decimal.Decimal(solde) * decimal.Decimal(100000000), 'eur')) + " EUR).\n\n⚠️ Commission du réseau : " + str('%.6f' % commission) + " BTC (~" + str( satoshi_to_currency( commission * decimal.Decimal(100000000), 'eur')) + " EUR).", reply_markup=markup3, parse_mode="Markdown") bot.register_next_step_handler( msg, lambda msg: get_somme(msg, destinataire))
def get_address(message): current_user_id = message.from_user.id current_wallet = wallet_exist(current_user_id) current_key = Key(current_wallet[1]) solde = decimal.Decimal(current_key.get_balance('btc')) global destinataire if message.text == 'Back': msg = bot.send_message( message.chat.id, "Hello, here is the address of your Bitcoin wallet : \n\n```" + current_wallet[0] + "```\n\nUse it to fund your Bitcoin wallet.", reply_markup=markup, parse_mode="Markdown") bot.register_next_step_handler(msg, process_step) else: destinataire = str(message.text) if (check_address(destinataire) == False): msg = bot.send_message(message.chat.id, "The recipient's address is incorrect!", reply_markup=markup, parse_mode="Markdown") bot.register_next_step_handler(msg, process_step) else: commission = decimal.Decimal(get_fee_cached() * 250 / 100000000) msg = bot.send_message( message.chat.id, "How much do you want to send? \ n \ nFunds available : " + str(solde) + " BTC (~" + str( satoshi_to_currency( decimal.Decimal(solde) * decimal.Decimal(100000000), 'eur')) + " EUR).\n\nРџа№ИЈ Network Commission : " + str('%.6f' % commission) + " BTC (~" + str( satoshi_to_currency( commission * decimal.Decimal(100000000), 'eur')) + " EUR).", reply_markup=markup3, parse_mode="Markdown") bot.register_next_step_handler( msg, lambda msg: get_somme(msg, destinataire))
def create_sweep_transaction(private_keys, destination_address, amount=None, currency='satoshi', fee=None, leftover=None, message=None, compressed=True): private_key_map = {} unspents = [] for key in private_keys: utxos = key.get_unspents() unspents += utxos for utx in utxos: private_key_map[utx.txid] = key version = VERSION_1 lock_time = LOCK_TIME sequence = SEQUENCE hash_type = HASH_TYPE input_count = int_to_unknown_bytes(len(unspents), byteorder='little') # Construct the outputs, taking the fee into account sum_of_unspents = sum([int(x.amount) for x in unspents]) amount = amount or sum_of_unspents outputs = [(destination_address, sum_of_unspents, currency)] unspents, outputs = sanitize_tx_data(unspents, outputs, fee or get_fee_cached(), leftover or private_keys[0].address, combine=True, message=message, compressed=compressed, sweep=True) output_count = int_to_unknown_bytes(len(outputs), byteorder='little') output_block = construct_output_block(outputs) # Optimize for speed, not memory, by pre-computing values. inputs = [] for unspent in unspents: script = hex_to_bytes(unspent.script) script_len = int_to_unknown_bytes(len(script), byteorder='little') txid = hex_to_bytes(unspent.txid)[::-1] txindex = unspent.txindex.to_bytes(4, byteorder='little') inputs.append(TxIn(script, script_len, txid, txindex)) for i, txin in enumerate(inputs): hashed = sha256(version + input_count + b''.join(ti.txid + ti.txindex + OP_0 + sequence for ti in islice(inputs, i)) + txin.txid + txin.txindex + txin.script_len + txin.script + sequence + b''.join(ti.txid + ti.txindex + OP_0 + sequence for ti in islice(inputs, i + 1, None)) + output_count + output_block + lock_time + hash_type) private_key = private_key_map[unspents[i].txid] signature = private_key.sign(hashed) + b'\x01' public_key = private_key.public_key public_key_len = len(public_key).to_bytes(1, byteorder='little') script_sig = (len(signature).to_bytes(1, byteorder='little') + signature + public_key_len + public_key) txin.script = script_sig txin.script_len = int_to_unknown_bytes(len(script_sig), byteorder='little') return bytes_to_hex(version + input_count + construct_input_block(inputs) + output_count + output_block + lock_time)
'cSES7oBGk4Cpf6eQrb8pzYWZxcYCSPhABK7uVELPbpfYwvW6rvSf') print('---------------Address Info---------------------') print(key.address) print(key1.address) print('---------------Before Transaction---------------------') print(key.get_balance('btc')) print(key1.get_balance('btc')) # print(key.unspents) print(key.get_transactions()) print(key1.get_transactions()) print(network.get_fee(fast=False)) print(network.get_fee_cached()) # print(key.get_unspents()) # print(key1.get_unspents()) # #sess = requests.session() # tx_hash = key1.send([(key.address, 0.0005, 'btc')]) # print(tx_hash) # print('---------------After Transaction---------------------') # print(key.get_balance('btc')) # print(key1.get_balance('btc')) # # print(key.unspents) # print(key.get_transactions()) # print(key1.get_transactions())