I need to create a transaction with a specific UTXO input and output, how can I do this?
from bitcoinlib.wallets import Wallet, wallet_delete
from bitcoinlib.transactions import Transaction
from bitcoinlib.keys import Address
from bitcoinlib.keys import Key, HDKey
import requests
import configparser
wallet_delete('mywallet')
wallet = Wallet.create('mywallet')
config = configparser.ConfigParser()
config.read('config.ini')
private_key_wif = config['wallet']['private_key']
# print(f'Private Key (WIF): {private_key_wif}')
if not private_key_wif.startswith(('5', 'K', 'L')):
raise ValueError("Invalid WIF private key format")
try:
key = HDKey(private_key_wif)
except Exception as e:
raise ValueError(f"Error importing private key: {e}")
wallet.key_import(key)
utxo_txid = 'c8633ec272d7524d1335779edd2c0563fea77321c2d0b7d885052340862f86bd'
utxo_vout = 0
utxo_value = 30397
recipient_address = '3C6BP79kpi8KvKjxp5yqhimJ8nDfbS8dDi'
tx = Transaction(network='bitcoin')
sender_address = 'myaddres'
tx.add_input(prev_txid=utxo_txid, output_n=utxo_vout, address=sender_address)
tx.add_output(value=utxo_value, address=recipient_address)
fee = 13000
tx.fee = fee
wallet.utxos_update()
wallet.select_utxos(tx, min_confirms=1)
tx.sign(keys=[key])
signed_tx_hex = tx.as_hex()
print(f'Signed transaction HEX: {signed_tx_hex}')
api_url = "https://mempool.space/api/tx"
payload = signed_tx_hex
response = requests.post(api_url, data=payload)
if response.status_code == 200:
print(f'Transaction successfully sent. TXID: {response.text}')
else:
print(f'Failed to send transaction. Status code: {response.status_code}, Response: {response.text}')
Error have:
python txbroadcast.py
Traceback (most recent call last):
File “/Users/denys/Desktop/scripts/btc-runes/txbroadcast.py”, line 29, in
wallet.key_import(key)
^^^^^^^^^^^^^^^^^
AttributeError: ‘Wallet’ object has no attribute ‘key_import’
Why I have this error and how I can resolve this problem?