Closed bigsk1 closed 8 months ago
I figured it out.... for anyone trying to figure this out also will add what I used below so the search engines pick it up.
# export BITCOIN_NODE_HOST='your_node_ip'
# export BITCOIN_NODE_PORT='your_node_port'
# export BITCOIN_RPC_USERNAME='your_rpc_username'
# export BITCOIN_RPC_PASSWORD='your_rpc_password'
# export MY_BTC_PRIVATE_KEY='your_private_key_here'
# add the above first in your terminal or add to .bashrc
import os
import sys
import json
import requests
from requests.auth import HTTPBasicAuth
from bitcoinlib.wallets import Wallet
from bitcoinlib.transactions import Output
from bitcoinlib.encoding import varstr
# Configuration (use environment variables for sensitive data)
private_key = os.getenv('MY_BTC_PRIVATE_KEY')
wallet_name = 'YOUR_WALLETS_NAME'
sender_address = 'YOUR_BTC_ADDRESS'
recipient_address = 'BTC_ADDRESS_YOUR_SENDING_TO'
amount_to_send_satoshis = int(0.0001 * 100000000)
fee_satoshis = int(0.00014 * 100000000)
message = 'YOUR_MESSAGE'
# RPC configuration
rpc_user = os.getenv('BITCOIN_RPC_USERNAME')
rpc_password = os.getenv('BITCOIN_RPC_PASSWORD')
rpc_url = f"http://{os.getenv('BITCOIN_NODE_HOST')}:{os.getenv('BITCOIN_NODE_PORT')}"
# Load the existing wallet and update UTXOs
try:
wallet = Wallet(wallet_name)
wallet.utxos_update()
utxos = wallet.utxos()
if not utxos:
print("No unspent transaction outputs available.")
sys.exit(1)
except Exception as e:
print(f"Error loading wallet or updating UTXOs: {e}")
sys.exit(1)
# Prepare inputs and outputs for the transaction
try:
inputs = [{'txid': utxo['txid'], 'output_n': utxo['output_n'], 'value': utxo['value']} for utxo in utxos]
print("Inputs prepared for the transaction:", inputs)
standard_output = Output(amount_to_send_satoshis, address=recipient_address)
lock_script = b'j' + varstr(message.encode('utf-8'))
op_return_output = Output(0, lock_script=lock_script)
outputs = [standard_output, op_return_output]
print("Outputs prepared for the transaction:", outputs)
except Exception as e:
print(f"Error preparing inputs or outputs: {e}")
sys.exit(1)
# Create and sign the transaction
try:
tx = wallet.send(outputs, fee=fee_satoshis)
print("\nTransaction created.")
print(f"TXID: {tx.txid}\n")
raw_tx_hex = tx.raw_hex()
print(f"Raw Transaction Hex: {raw_tx_hex}")
# Broadcast the transaction via RPC
headers = {'content-type': 'application/json'}
payload = {
"method": "sendrawtransaction",
"params": [raw_tx_hex],
"jsonrpc": "2.0",
"id": 0,
}
response = requests.post(rpc_url, json=payload, headers=headers, auth=HTTPBasicAuth(rpc_user, rpc_password))
response_json = response.json()
if 'error' in response_json and response_json['error']:
print("Error broadcasting transaction:", response_json['error'])
else:
txid = response_json['result']
print("Transaction broadcasted successfully. TXID:", txid)
except Exception as e:
print(f"\nAn error occurred during transaction creation or broadcast: {e}")
Like to send OP_RETURN message data with transaction, using Conda python 3.10.4 on wsl2 ubuntu. Node running on server in local network
script to create a wallet
provides wallet name, public address and private address in wif format (xptbAxxxxxxxxxxxxxxxxxxx)
send small amount to wallet addy
created a script to send transaction with message, all secretes in envs, script runs fine
Getting a TXID from running but never is sent to blockchain, vsat is high to get into next block. Output from script below.
I can run curl commands from host to node and get blockchain data so is connecting over RPC
I can get wallet details running a script, but I can not actually send a transaction even though I get a TXID. Public Address in block explorer never shows a transaction being sent, local mem pool never shows the TXID found.
On node (umbrel) ~/umbrel/app-data/bitcoin/data/app/bitcoin-config.json has rpcallowip=192.168.x.xx, but I guess it's no longer supported and I am connecting via RPC ok anyway.
In ~/.bitcoinlib/bitcoin.conf shows
I have added my node to providers and still no luck in getting transaction sent. Any Ideas? bitcoinlib.log shows nothing related to the transaction.